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#include <config.h> 3555682Smarkm 3655682Smarkm#ifndef HAVE_DAEMON 3755682Smarkm 3855682Smarkm#ifdef HAVE_FCNTL_H 3955682Smarkm#include <fcntl.h> 4055682Smarkm#endif 4155682Smarkm#ifdef HAVE_PATHS_H 4255682Smarkm#include <paths.h> 4355682Smarkm#endif 4455682Smarkm#ifdef HAVE_UNISTD_H 4555682Smarkm#include <unistd.h> 4655682Smarkm#endif 4755682Smarkm 4855682Smarkm#include "roken.h" 4955682Smarkm 50233294SstasROKEN_LIB_FUNCTION int ROKEN_LIB_CALL 5155682Smarkmdaemon(int nochdir, int noclose) 5255682Smarkm{ 5355682Smarkm int fd; 5455682Smarkm 5555682Smarkm switch (fork()) { 5655682Smarkm case -1: 5755682Smarkm return (-1); 5855682Smarkm case 0: 5955682Smarkm break; 6055682Smarkm default: 6155682Smarkm _exit(0); 6255682Smarkm } 6355682Smarkm 6455682Smarkm if (setsid() == -1) 6555682Smarkm return (-1); 6655682Smarkm 6755682Smarkm if (!nochdir) 6855682Smarkm chdir("/"); 6955682Smarkm 7055682Smarkm if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) { 7155682Smarkm dup2(fd, STDIN_FILENO); 7255682Smarkm dup2(fd, STDOUT_FILENO); 7355682Smarkm dup2(fd, STDERR_FILENO); 7455682Smarkm if (fd > 2) 7555682Smarkm close (fd); 7655682Smarkm } 7755682Smarkm return (0); 7855682Smarkm} 7955682Smarkm 8055682Smarkm#endif /* HAVE_DAEMON */ 81