mirror of
https://kernel.googlesource.com/pub/scm/network/tftp/tftp-hpa
synced 2025-05-01 12:29:54 +03:00
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
|
@ -142,7 +142,6 @@ AC_SEARCH_LIBS(gethostbyname, [nsl resolv], , [AC_MSG_ERROR(gethostbyname not fo
|
||||||
AC_SEARCH_LIBS(inet_aton, [nsl resolv], , [AC_MSG_ERROR(inet_aton not found)])
|
AC_SEARCH_LIBS(inet_aton, [nsl resolv], , [AC_MSG_ERROR(inet_aton not found)])
|
||||||
AC_SEARCH_LIBS(herror, [nsl resolv], , [AC_MSG_ERROR(herror not found)])
|
AC_SEARCH_LIBS(herror, [nsl resolv], , [AC_MSG_ERROR(herror not found)])
|
||||||
|
|
||||||
AC_CHECK_FUNCS(dup2)
|
|
||||||
AC_CHECK_FUNCS(fcntl)
|
AC_CHECK_FUNCS(fcntl)
|
||||||
AC_CHECK_FUNCS(setsid)
|
AC_CHECK_FUNCS(setsid)
|
||||||
AC_CHECK_FUNCS(recvmsg)
|
AC_CHECK_FUNCS(recvmsg)
|
||||||
|
@ -174,9 +173,11 @@ SRCROOT=`cd $srcdir && pwd`
|
||||||
OBJROOT=`pwd`
|
OBJROOT=`pwd`
|
||||||
|
|
||||||
XTRA=false
|
XTRA=false
|
||||||
AC_SEARCH_LIBS(xmalloc, iberty, , [XTRA=true ; AC_LIBOBJ(xmalloc)])
|
AC_SEARCH_LIBS(xmalloc, iberty, , [XTRA=true; AC_LIBOBJ(xmalloc)])
|
||||||
AC_SEARCH_LIBS(xstrdup, iberty, , [XTRA=true ; AC_LIBOBJ(xstrdup)])
|
AC_SEARCH_LIBS(xstrdup, iberty, , [XTRA=true; AC_LIBOBJ(xstrdup)])
|
||||||
AC_SEARCH_LIBS(bsd_signal, bsd, , [XTRA=true ; AC_LIBOBJ(bsdsignal)])
|
AC_SEARCH_LIBS(bsd_signal, bsd, , [XTRA=true; AC_LIBOBJ(bsdsignal)])
|
||||||
|
AC_CHECK_FUNCS(daemon, , [XTRA=true; AC_LIBOBJ(daemon)])
|
||||||
|
AC_CHECK_FUNCS(dup2, , [XTRA=true; AC_LIBOBJ(dup2)])
|
||||||
if $XTRA
|
if $XTRA
|
||||||
then
|
then
|
||||||
XTRALIBS="$OBJROOT/lib/libxtra.a $XTRALIBS"
|
XTRALIBS="$OBJROOT/lib/libxtra.a $XTRALIBS"
|
||||||
|
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
/* tftp-hpa: $Id$ */
|
|
||||||
|
|
||||||
/* $OpenBSD: tftpd.c,v 1.13 1999/06/23 17:01:36 deraadt Exp $ */
|
/* $OpenBSD: tftpd.c,v 1.13 1999/06/23 17:01:36 deraadt Exp $ */
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -499,34 +497,10 @@ main(int argc, char **argv)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Daemonize this process */
|
/* Daemonize this process */
|
||||||
{
|
if (daemon(0, 0) < 0) {
|
||||||
pid_t f = fork();
|
syslog(LOG_ERR, "cannot daemonize: %m");
|
||||||
int nfd;
|
|
||||||
if ( f > 0 )
|
|
||||||
exit(0);
|
|
||||||
if ( f < 0 ) {
|
|
||||||
syslog(LOG_ERR, "cannot fork: %m");
|
|
||||||
exit(EX_OSERR);
|
exit(EX_OSERR);
|
||||||
}
|
}
|
||||||
nfd = open("/dev/null", O_RDWR);
|
|
||||||
if ( nfd >= 3 ) {
|
|
||||||
#ifdef HAVE_DUP2
|
|
||||||
dup2(nfd, 0);
|
|
||||||
dup2(nfd, 1);
|
|
||||||
dup2(nfd, 2);
|
|
||||||
#else
|
|
||||||
close(0); dup(nfd);
|
|
||||||
close(1); dup(nfd);
|
|
||||||
close(2); dup(nfd);
|
|
||||||
#endif
|
|
||||||
close(nfd);
|
|
||||||
} else if ( nfd < 0 ) {
|
|
||||||
close(0); close(1); close(2);
|
|
||||||
}
|
|
||||||
#ifdef HAVE_SETSID
|
|
||||||
setsid();
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
/* 0 is our socket descriptor */
|
/* 0 is our socket descriptor */
|
||||||
close(1); close(2);
|
close(1); close(2);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue