daemon.c revision 55682
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.
1355682Smarkm * 3. All advertising materials mentioning features or use of this software
1455682Smarkm *    must display the following acknowledgement:
1555682Smarkm *	This product includes software developed by the University of
1655682Smarkm *	California, Berkeley and its contributors.
1755682Smarkm * 4. Neither the name of the University nor the names of its contributors
1855682Smarkm *    may be used to endorse or promote products derived from this software
1955682Smarkm *    without specific prior written permission.
2055682Smarkm *
2155682Smarkm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2255682Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2355682Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2455682Smarkm * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2555682Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2655682Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2755682Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2855682Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2955682Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3055682Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3155682Smarkm * SUCH DAMAGE.
3255682Smarkm */
3355682Smarkm
3455682Smarkm#if defined(LIBC_SCCS) && !defined(lint)
3555682Smarkmstatic char sccsid[] = "@(#)daemon.c	8.1 (Berkeley) 6/4/93";
3655682Smarkm#endif /* LIBC_SCCS and not lint */
3755682Smarkm
3855682Smarkm#ifdef HAVE_CONFIG_H
3955682Smarkm#include <config.h>
4055682Smarkm#endif
4155682Smarkm
4255682SmarkmRCSID("$Id: daemon.c,v 1.3 1997/10/04 21:55:48 joda Exp $");
4355682Smarkm
4455682Smarkm#ifndef HAVE_DAEMON
4555682Smarkm
4655682Smarkm#ifdef HAVE_FCNTL_H
4755682Smarkm#include <fcntl.h>
4855682Smarkm#endif
4955682Smarkm#ifdef HAVE_PATHS_H
5055682Smarkm#include <paths.h>
5155682Smarkm#endif
5255682Smarkm#ifdef HAVE_UNISTD_H
5355682Smarkm#include <unistd.h>
5455682Smarkm#endif
5555682Smarkm
5655682Smarkm#include "roken.h"
5755682Smarkm
5855682Smarkmint
5955682Smarkmdaemon(int nochdir, int noclose)
6055682Smarkm{
6155682Smarkm    int fd;
6255682Smarkm
6355682Smarkm    switch (fork()) {
6455682Smarkm    case -1:
6555682Smarkm	return (-1);
6655682Smarkm    case 0:
6755682Smarkm	break;
6855682Smarkm    default:
6955682Smarkm	_exit(0);
7055682Smarkm    }
7155682Smarkm
7255682Smarkm    if (setsid() == -1)
7355682Smarkm	return (-1);
7455682Smarkm
7555682Smarkm    if (!nochdir)
7655682Smarkm	chdir("/");
7755682Smarkm
7855682Smarkm    if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
7955682Smarkm	dup2(fd, STDIN_FILENO);
8055682Smarkm	dup2(fd, STDOUT_FILENO);
8155682Smarkm	dup2(fd, STDERR_FILENO);
8255682Smarkm	if (fd > 2)
8355682Smarkm	    close (fd);
8455682Smarkm    }
8555682Smarkm    return (0);
8655682Smarkm}
8755682Smarkm
8855682Smarkm#endif /* HAVE_DAEMON */
89