daemon.c

00001 /*      $NetBSD: daemon.c,v 1.9 2003/08/07 16:42:46 agc Exp $   */
00002 
00003 /*-
00004  * Copyright (c) 1990, 1993
00005  *      The Regents of the University of California.  All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions
00009  * are met:
00010  * 1. Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer.
00012  * 2. Redistributions in binary form must reproduce the above copyright
00013  *    notice, this list of conditions and the following disclaimer in the
00014  *    documentation and/or other materials provided with the distribution.
00015  * 3. Neither the name of the University nor the names of its contributors
00016  *    may be used to endorse or promote products derived from this software
00017  *    without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00020  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00023  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00024  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00025  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00026  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00027  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00028  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00029  * SUCH DAMAGE.
00030  */
00031 
00032 #include <missing/daemon.h>
00033 
00034 #if !defined(OS_WIN) && defined(HAVE_FORK)
00035 
00036 #ifndef _PATH_DEVNULL
00037 #define _PATH_DEVNULL "/dev/null"
00038 #endif
00039 
00040 #include <fcntl.h>
00041 #ifdef HAVE_PATHS
00042 #include <paths.h>
00043 #endif
00044 #include <stdlib.h>
00045 #include <unistd.h>
00046 #include <stdio.h>
00047 
00048 int daemon(int nochdir, int noclose)
00049 {
00050         int fd;
00051 
00052         switch (fork()) {
00053         case -1:
00054                 return (-1);
00055         case 0:
00056                 break;
00057         default:
00058                 _exit(0);
00059         }
00060 
00061         if (setsid() == -1)
00062                 return (-1);
00063 
00064         if (!nochdir)
00065                 (void)chdir("/");
00066 
00067         if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
00068                 (void)dup2(fd, STDIN_FILENO);
00069                 (void)dup2(fd, STDOUT_FILENO);
00070                 (void)dup2(fd, STDERR_FILENO);
00071                 if (fd > STDERR_FILENO)
00072                         (void)close(fd);
00073         }
00074         return (0);
00075 }
00076 
00077 #else   /* OS_WIN || !HAVE_FORK */
00078 
00079 int daemon(int nochdir, int noclose)
00080 {
00081     u_unused_args(nochdir, noclose);
00082     u_warn("daemon(3) not implemented: fork(2) is not available on target OS");
00083     return -1;
00084 }
00085 #endif  /* !OS_WIN && HAVE_FORK */

←Products
© 2005-2012 - KoanLogic S.r.l. - All rights reserved