projects
/
oweals
/
musl.git
/ blob
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
history
|
raw
|
HEAD
make pthread_create return EAGAIN on resource failure, as required by POSIX
[oweals/musl.git]
/
src
/
linux
/
daemon.c
1
#include <fcntl.h>
2
#include <unistd.h>
3
4
int daemon(int nochdir, int noclose)
5
{
6
int fd;
7
8
switch(fork()) {
9
case 0: break;
10
case -1: return -1;
11
default: _exit(0);
12
}
13
14
if (setsid() < 0) return -1;
15
16
switch(fork()) {
17
case 0: break;
18
case -1: return -1;
19
default: _exit(0);
20
}
21
22
if (!nochdir) chdir("/");
23
if (!noclose && (fd = open("/dev/null", O_RDWR)) >= 0) {
24
dup2(fd, 0);
25
dup2(fd, 1);
26
dup2(fd, 2);
27
if (fd > 2) close(fd);
28
}
29
30
return 0;
31
}