123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104 |
- #include <stdio.h>
- #include <features.h>
- #include <fcntl.h>
- #include <paths.h>
- #include <unistd.h>
- int daemon( int nochdir, int noclose )
- {
- int fd;
- switch (fork()) {
- case -1:
- return(-1);
- case 0:
- break;
- default:
- _exit(0);
- }
- if (setsid() == -1)
- return(-1);
-
- if (fork())
- _exit(0);
- if (!nochdir)
- chdir("/");
- if (!noclose && (fd = open(_PATH_DEVNULL, O_RDWR, 0)) != -1) {
- dup2(fd, STDIN_FILENO);
- dup2(fd, STDOUT_FILENO);
- dup2(fd, STDERR_FILENO);
- if (fd > 2)
- close(fd);
- }
- return(0);
- }
|