forked from mirrors/tftp-hpa-google
Use replacement library functions to daemonize, rather than #ifdef hell
This commit is contained in:
parent
6124dcbe2d
commit
059de7ce20
4 changed files with 70 additions and 33 deletions
37
lib/daemon.c
Normal file
37
lib/daemon.c
Normal file
|
@ -0,0 +1,37 @@
|
|||
/*
|
||||
* daemon.c - "daemonize" a process
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
int daemon(int nochdir, int noclose)
|
||||
{
|
||||
int nullfd;
|
||||
pid_t f;
|
||||
|
||||
if (!nochdir) {
|
||||
if (chdir("/"))
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (!noclose) {
|
||||
if ((nullfd = open("/dev/null", O_RDWR)) < 0 ||
|
||||
dup2(nullfd, 0) < 0 ||
|
||||
dup2(nullfd, 1) < 0 ||
|
||||
dup2(nullfd, 2) < 0)
|
||||
return -1;
|
||||
close(nullfd);
|
||||
}
|
||||
|
||||
f = fork();
|
||||
if (f < 0)
|
||||
return -1;
|
||||
else if (f > 0)
|
||||
_exit(0);
|
||||
|
||||
#ifdef HAVE_SETSID
|
||||
return setsid();
|
||||
#else
|
||||
return 0;
|
||||
#endif
|
||||
}
|
25
lib/dup2.c
Normal file
25
lib/dup2.c
Normal file
|
@ -0,0 +1,25 @@
|
|||
/*
|
||||
* dup2.c
|
||||
*
|
||||
* Ersatz dup2() for really ancient systems
|
||||
*/
|
||||
|
||||
#include "config.h"
|
||||
|
||||
int dup2(int oldfd, int newfd)
|
||||
{
|
||||
int rv, nfd;
|
||||
|
||||
close(newfd);
|
||||
|
||||
nfd = rv = dup(oldfd);
|
||||
|
||||
if (rv >= 0 && rv != newfd) {
|
||||
rv = dup2(oldfd, newfd);
|
||||
close(nfd);
|
||||
}
|
||||
|
||||
return rv;
|
||||
}
|
||||
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue