Add code to tftpd to exit cleanly (master only) on SIGHUP

This commit is contained in:
hpa 2001-07-10 22:04:14 +00:00
parent 3444b4d18c
commit fc5f978d68

View file

@ -154,6 +154,12 @@ struct options {
{ NULL, NULL } { NULL, NULL }
}; };
/* Simple handler for SIGHUP */
static volatile sig_atomic_t caught_sighup = 0;
static void handle_sighup(int sig)
{
caught_sighup = 1;
}
static void static void
@ -279,18 +285,29 @@ main(int argc, char **argv)
/* This means we don't want to wait() for children */ /* This means we don't want to wait() for children */
bsd_signal(SIGCHLD, SIG_IGN); bsd_signal(SIGCHLD, SIG_IGN);
/* Take SIGHUP and use it to set a variable. This
is polled synchronously to make sure we don't
lose packets as a result. */
bsd_signal(SIGHUP, handle_sighup);
do { do {
fd_set readset; fd_set readset;
struct timeval tv_timeout; struct timeval tv_timeout;
int rv;
FD_ZERO(&readset); FD_ZERO(&readset);
FD_SET(fd, &readset); FD_SET(fd, &readset);
tv_timeout.tv_sec = timeout; tv_timeout.tv_sec = timeout;
tv_timeout.tv_usec = 0; tv_timeout.tv_usec = 0;
if ( select(fd+1, &readset, NULL, NULL, &tv_timeout) == 0 ) if ( caught_sighup )
exit(0); /* Timeout, return to inetd */ exit(0); /* Return to inetd for respawn */
rv = select(fd+1, &readset, NULL, NULL, &tv_timeout);
if ( rv == -1 && errno == EINTR )
continue; /* Signal caught, reloop */
if ( rv <= 0 )
exit(0); /* Timeout or error, return to inetd */
fromlen = sizeof (from); fromlen = sizeof (from);
n = myrecvfrom(fd, buf, sizeof (buf), 0, n = myrecvfrom(fd, buf, sizeof (buf), 0,
@ -324,9 +341,8 @@ main(int argc, char **argv)
#endif #endif
/* /*
* Now that we have read the message out of the UDP * Now that we have read the request packet from the UDP
* socket, we fork and go back to listening to the * socket, we fork and go back to listening to the socket.
* socket.
*/ */
pid = fork(); pid = fork();
if (pid < 0) { if (pid < 0) {
@ -742,6 +758,7 @@ validate_access(char *filename, int mode, struct formats *pf)
int timeout; int timeout;
sigjmp_buf timeoutbuf; sigjmp_buf timeoutbuf;
/* Handle timeout signal */
void void
timer(int sig) timer(int sig)
{ {
@ -850,6 +867,7 @@ abort:
(void) fclose(file); (void) fclose(file);
} }
/* Bail out signal handler */
void void
justquit(int sig) justquit(int sig)
{ {