OSDN Git Service

Ok, this commit is _huge_ and its gonna change the world. I've
[uclinux-h8/uClibc.git] / libc / unistd / daemon.c
1 /* vi: set sw=4 ts=4: */
2 /* daemon implementation for uClibc
3  *
4  * Copyright (c) 1991, 1993
5  *      The Regents of the University of California.  All rights reserved.
6  *
7  * Modified for uClibc by Erik Andersen <andersee@debian.org>
8  *
9  * This program is free software; you can redistribute it and/or modify it
10  * under the terms of the GNU Library General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or (at your
12  * option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
17  * for more details.
18  *
19  * You should have received a copy of the GNU Library General Public License
20  * along with this program; if not, write to the Free Software Foundation,
21  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22  *
23  * Original copyright notice is retained at the end of this file.
24  */
25
26 #include <stdio.h>
27 #include <features.h>
28 #include <fcntl.h>
29 #include <paths.h>
30 #include <unistd.h>
31
32 /* Note that this file should not be compiled in 
33  * unless __UCLIBC_HAS_MMU__ is defined */
34
35 int daemon( int nochdir, int noclose )
36 {
37         int fd;
38
39         switch (fork()) {
40                 case -1:
41                         return(-1);
42                 case 0:
43                         break;
44                 default:
45                         _exit(0);
46         }
47
48         if (setsid() == -1)
49                 return(-1);
50
51         /* Make certain we are not a session leader, or else we
52          * might reacquire a controlling terminal */
53         if (fork())
54                 _exit(0);
55
56         if (!nochdir)
57                 chdir("/");
58
59         if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
60                 dup2(fd, STDIN_FILENO);
61                 dup2(fd, STDOUT_FILENO);
62                 dup2(fd, STDERR_FILENO);
63                 if (fd > 2)
64                         close(fd);
65         }
66         return(0);
67 }
68
69
70
71 /*-
72  * Copyright (c) 1990, 1993
73  *      The Regents of the University of California.  All rights reserved.
74  *
75  * Redistribution and use in source and binary forms, with or without
76  * modification, are permitted provided that the following conditions
77  * are met:
78  * 1. Redistributions of source code must retain the above copyright
79  *    notice, this list of conditions and the following disclaimer.
80  * 2. Redistributions in binary form must reproduce the above copyright
81  *    notice, this list of conditions and the following disclaimer in the
82  *    documentation and/or other materials provided with the distribution.
83  *
84  * 3. <BSD Advertising Clause omitted per the July 22, 1999 licensing change 
85  *              ftp://ftp.cs.berkeley.edu/pub/4bsd/README.Impt.License.Change> 
86  *
87  * 4. Neither the name of the University nor the names of its contributors
88  *    may be used to endorse or promote products derived from this software
89  *    without specific prior written permission.
90  *
91  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
92  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
93  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
94  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
95  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
96  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
97  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
98  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
99  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
100  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
101  * SUCH DAMAGE.
102  */
103
104