Annotation of /trunk/mkinitrd-magellan/klibc/usr/klibc/daemon.c
Parent Directory | Revision Log
Revision 532 -
(hide annotations)
(download)
Sat Sep 1 22:45:15 2007 UTC (16 years, 9 months ago) by niro
File MIME type: text/plain
File size: 495 byte(s)
Sat Sep 1 22:45:15 2007 UTC (16 years, 9 months ago) by niro
File MIME type: text/plain
File size: 495 byte(s)
-import if magellan mkinitrd; it is a fork of redhats mkinitrd-5.0.8 with all magellan patches and features; deprecates magellan-src/mkinitrd
1 | niro | 532 | /* |
2 | * daemon.c - "daemonize" a process | ||
3 | */ | ||
4 | |||
5 | #include <stdlib.h> | ||
6 | #include <unistd.h> | ||
7 | #include <fcntl.h> | ||
8 | |||
9 | int daemon(int nochdir, int noclose) | ||
10 | { | ||
11 | int nullfd; | ||
12 | pid_t f; | ||
13 | |||
14 | if (!nochdir) { | ||
15 | if (chdir("/")) | ||
16 | return -1; | ||
17 | } | ||
18 | |||
19 | if (!noclose) { | ||
20 | if ((nullfd = open("/dev/null", O_RDWR)) < 0 || | ||
21 | dup2(nullfd, 0) < 0 || | ||
22 | dup2(nullfd, 1) < 0 || | ||
23 | dup2(nullfd, 2) < 0) | ||
24 | return -1; | ||
25 | close(nullfd); | ||
26 | } | ||
27 | |||
28 | f = fork(); | ||
29 | if (f < 0) | ||
30 | return -1; | ||
31 | else if (f > 0) | ||
32 | _exit(0); | ||
33 | |||
34 | return setsid(); | ||
35 | } |