1157016Sdes/* $OpenBSD: daemon.c,v 1.6 2005/08/08 08:05:33 espie Exp $ */ 298937Sdes/*- 398937Sdes * Copyright (c) 1990, 1993 498937Sdes * The Regents of the University of California. All rights reserved. 598937Sdes * 698937Sdes * Redistribution and use in source and binary forms, with or without 798937Sdes * modification, are permitted provided that the following conditions 898937Sdes * are met: 998937Sdes * 1. Redistributions of source code must retain the above copyright 1098937Sdes * notice, this list of conditions and the following disclaimer. 1198937Sdes * 2. Redistributions in binary form must reproduce the above copyright 1298937Sdes * notice, this list of conditions and the following disclaimer in the 1398937Sdes * documentation and/or other materials provided with the distribution. 14124208Sdes * 3. Neither the name of the University nor the names of its contributors 1598937Sdes * may be used to endorse or promote products derived from this software 1698937Sdes * without specific prior written permission. 1798937Sdes * 1898937Sdes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 1998937Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2098937Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2198937Sdes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2298937Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2398937Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2498937Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2598937Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2698937Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2798937Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2898937Sdes * SUCH DAMAGE. 2998937Sdes */ 3098937Sdes 31157016Sdes/* OPENBSD ORIGINAL: lib/libc/gen/daemon.c */ 32157016Sdes 3398937Sdes#include "includes.h" 3498937Sdes 3598937Sdes#ifndef HAVE_DAEMON 3698937Sdes 37162852Sdes#include <sys/types.h> 38162852Sdes 39162852Sdes#ifdef HAVE_SYS_STAT_H 40162852Sdes# include <sys/stat.h> 41162852Sdes#endif 42162852Sdes 43162852Sdes#ifdef HAVE_FCNTL_H 44162852Sdes# include <fcntl.h> 45162852Sdes#endif 46162852Sdes 47162852Sdes#ifdef HAVE_UNISTD_H 48162852Sdes# include <unistd.h> 49162852Sdes#endif 50162852Sdes 5198937Sdesint 52124208Sdesdaemon(int nochdir, int noclose) 5398937Sdes{ 5498937Sdes int fd; 5598937Sdes 5698937Sdes switch (fork()) { 5798937Sdes case -1: 5898937Sdes return (-1); 5998937Sdes case 0: 6098937Sdes break; 6198937Sdes default: 6298937Sdes _exit(0); 6398937Sdes } 6498937Sdes 6598937Sdes if (setsid() == -1) 6698937Sdes return (-1); 6798937Sdes 6898937Sdes if (!nochdir) 6998937Sdes (void)chdir("/"); 7098937Sdes 7198937Sdes if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) { 7298937Sdes (void)dup2(fd, STDIN_FILENO); 7398937Sdes (void)dup2(fd, STDOUT_FILENO); 7498937Sdes (void)dup2(fd, STDERR_FILENO); 7598937Sdes if (fd > 2) 7698937Sdes (void)close (fd); 7798937Sdes } 7898937Sdes return (0); 7998937Sdes} 8098937Sdes 8198937Sdes#endif /* !HAVE_DAEMON */ 8298937Sdes 83