daemon.c revision 178825
155682Smarkm/*-
255682Smarkm * Copyright (c) 1990, 1993
355682Smarkm *	The Regents of the University of California.  All rights reserved.
455682Smarkm *
555682Smarkm * Redistribution and use in source and binary forms, with or without
655682Smarkm * modification, are permitted provided that the following conditions
755682Smarkm * are met:
855682Smarkm * 1. Redistributions of source code must retain the above copyright
955682Smarkm *    notice, this list of conditions and the following disclaimer.
1055682Smarkm * 2. Redistributions in binary form must reproduce the above copyright
1155682Smarkm *    notice, this list of conditions and the following disclaimer in the
1255682Smarkm *    documentation and/or other materials provided with the distribution.
13178825Sdfr * 3. Neither the name of the University nor the names of its contributors
1455682Smarkm *    may be used to endorse or promote products derived from this software
1555682Smarkm *    without specific prior written permission.
1655682Smarkm *
1755682Smarkm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1855682Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1955682Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2055682Smarkm * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2155682Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2255682Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2355682Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2455682Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2555682Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2655682Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2755682Smarkm * SUCH DAMAGE.
2855682Smarkm */
2955682Smarkm
3055682Smarkm#if defined(LIBC_SCCS) && !defined(lint)
3155682Smarkmstatic char sccsid[] = "@(#)daemon.c	8.1 (Berkeley) 6/4/93";
3255682Smarkm#endif /* LIBC_SCCS and not lint */
3355682Smarkm
3455682Smarkm#ifdef HAVE_CONFIG_H
3555682Smarkm#include <config.h>
3655682Smarkm#endif
3755682Smarkm
38178825SdfrRCSID("$Id: daemon.c 14773 2005-04-12 11:29:18Z lha $");
3955682Smarkm
4055682Smarkm#ifndef HAVE_DAEMON
4155682Smarkm
4255682Smarkm#ifdef HAVE_FCNTL_H
4355682Smarkm#include <fcntl.h>
4455682Smarkm#endif
4555682Smarkm#ifdef HAVE_PATHS_H
4655682Smarkm#include <paths.h>
4755682Smarkm#endif
4855682Smarkm#ifdef HAVE_UNISTD_H
4955682Smarkm#include <unistd.h>
5055682Smarkm#endif
5155682Smarkm
5255682Smarkm#include "roken.h"
5355682Smarkm
54178825Sdfrint ROKEN_LIB_FUNCTION
5555682Smarkmdaemon(int nochdir, int noclose)
5655682Smarkm{
5755682Smarkm    int fd;
5855682Smarkm
5955682Smarkm    switch (fork()) {
6055682Smarkm    case -1:
6155682Smarkm	return (-1);
6255682Smarkm    case 0:
6355682Smarkm	break;
6455682Smarkm    default:
6555682Smarkm	_exit(0);
6655682Smarkm    }
6755682Smarkm
6855682Smarkm    if (setsid() == -1)
6955682Smarkm	return (-1);
7055682Smarkm
7155682Smarkm    if (!nochdir)
7255682Smarkm	chdir("/");
7355682Smarkm
7455682Smarkm    if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
7555682Smarkm	dup2(fd, STDIN_FILENO);
7655682Smarkm	dup2(fd, STDOUT_FILENO);
7755682Smarkm	dup2(fd, STDERR_FILENO);
7855682Smarkm	if (fd > 2)
7955682Smarkm	    close (fd);
8055682Smarkm    }
8155682Smarkm    return (0);
8255682Smarkm}
8355682Smarkm
8455682Smarkm#endif /* HAVE_DAEMON */
85