Back to index

glibc  2.9
daemon.c
Go to the documentation of this file.
00001 /*-
00002  * Copyright (c) 1990, 1993
00003  *     The Regents of the University of California.  All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 4. Neither the name of the University nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  */
00029 
00030 #if defined(LIBC_SCCS) && !defined(lint)
00031 static char sccsid[] = "@(#)daemon.c      8.1 (Berkeley) 6/4/93";
00032 #endif /* LIBC_SCCS and not lint */
00033 
00034 #include <errno.h>
00035 #include <fcntl.h>
00036 #include <paths.h>
00037 #include <unistd.h>
00038 #include <sys/stat.h>
00039 
00040 #include <device-nrs.h>
00041 #include <not-cancel.h>
00042 
00043 int
00044 daemon(nochdir, noclose)
00045        int nochdir, noclose;
00046 {
00047        int fd;
00048 
00049        switch (__fork()) {
00050        case -1:
00051               return (-1);
00052        case 0:
00053               break;
00054        default:
00055               _exit(0);
00056        }
00057 
00058        if (__setsid() == -1)
00059               return (-1);
00060 
00061        if (!nochdir)
00062               (void)__chdir("/");
00063 
00064        if (!noclose) {
00065               struct stat64 st;
00066 
00067               if ((fd = open_not_cancel(_PATH_DEVNULL, O_RDWR, 0)) != -1
00068                   && (__builtin_expect (__fxstat64 (_STAT_VER, fd, &st), 0)
00069                      == 0)) {
00070                      if (__builtin_expect (S_ISCHR (st.st_mode), 1) != 0
00071 #if defined DEV_NULL_MAJOR && defined DEV_NULL_MINOR
00072                          && (st.st_rdev
00073                             == makedev (DEV_NULL_MAJOR, DEV_NULL_MINOR))
00074 #endif
00075                          ) {
00076                             (void)__dup2(fd, STDIN_FILENO);
00077                             (void)__dup2(fd, STDOUT_FILENO);
00078                             (void)__dup2(fd, STDERR_FILENO);
00079                             if (fd > 2)
00080                                    (void)__close (fd);
00081                      } else {
00082                             /* We must set an errno value since no
00083                                function call actually failed.  */
00084                             close_not_cancel_no_status (fd);
00085                             __set_errno (ENODEV);
00086                             return -1;
00087                      }
00088               } else {
00089                      close_not_cancel_no_status (fd);
00090                      return -1;
00091               }
00092        }
00093        return (0);
00094 }