Ferenc Wagner
2009-Sep-05 16:41 UTC
[syslinux] [tftpd PATCH 0/5] pidfile option and syslog fix
Hi, This is my current patch queue. The patches are independent, AFAIK, so feel free to cherry-pick or reject them independently. The bottom one was needed for a successful build here; the top one contains all the modifications you requested on IRC, and more. Meanwhile I hit one of the four stray TABs in tftpd.c, and decided to get rid of them. Comments welcome. --- Ferenc Wagner (5): Implement the --pidfile option Untabify tftpd.c Fix comment typo Ensure that the log socket is available for the child Locase datarootdir, so mandir et al. find their defaults MCONFIG.in | 2 +- tftpd/tftpd.8.in | 5 ++++ tftpd/tftpd.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++------- 3 files changed, 65 insertions(+), 10 deletions(-) -- Thanks, Feri.
Ferenc Wagner
2009-Sep-05 16:41 UTC
[syslinux] [tftpd PATCH 1/5] Locase datarootdir, so mandir et al. find their defaults
Signed-off-by: Ferenc Wagner <wferi at niif.hu> --- MCONFIG.in | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-) diff --git a/MCONFIG.in b/MCONFIG.in index ecff393..2660f7e 100644 --- a/MCONFIG.in +++ b/MCONFIG.in @@ -32,7 +32,7 @@ MANDIR = @mandir@ SBINDIR = @sbindir@ # Data root directory -DATAROOTDIR = @datarootdir@ +datarootdir = @datarootdir@ # Binary suffixes O = @OBJEXT@
Ferenc Wagner
2009-Sep-05 16:41 UTC
[syslinux] [tftpd PATCH 2/5] Ensure that the log socket is available for the child
Signed-off-by: Ferenc Wagner <wferi at niif.hu> --- tftpd/tftpd.c | 9 +++++++++ 1 files changed, 9 insertions(+), 0 deletions(-) diff --git a/tftpd/tftpd.c b/tftpd/tftpd.c index 72d3e5d..721ebd8 100644 --- a/tftpd/tftpd.c +++ b/tftpd/tftpd.c @@ -870,6 +870,15 @@ int main(int argc, char **argv) /* Ignore SIGHUP */ set_signal(SIGHUP, SIG_IGN, 0); + /* Make sure the log socket is still connected. This has to be + done before the chroot, while /dev/log is still accessible. + When not running standalone, there is little chance that the + syslog daemon gets restarted by the time we get here. */ + if (secure && standalone) { + closelog(); + openlog(__progname, LOG_PID | LOG_NDELAY, LOG_DAEMON); + } + #ifdef HAVE_TCPWRAPPERS /* Verify if this was a legal request for us. This has to be done before the chroot, while /etc is still accessible. */
Signed-off-by: Ferenc Wagner <wferi at niif.hu> --- tftpd/tftpd.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-) diff --git a/tftpd/tftpd.c b/tftpd/tftpd.c index 721ebd8..1f5d9d6 100644 --- a/tftpd/tftpd.c +++ b/tftpd/tftpd.c @@ -696,7 +696,7 @@ int main(int argc, char **argv) } #endif /* Daemonize this process */ - /* Note: when running in secure mode (-s), we must not chroot, since + /* Note: when running in secure mode (-s), we must not chdir, since we are already in the proper directory. */ if (!nodaemon && daemon(secure, 0) < 0) { syslog(LOG_ERR, "cannot daemonize: %m");
Signed-off-by: Ferenc Wagner <wferi at niif.hu> --- tftpd/tftpd.c | 8 ++++---- 1 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tftpd/tftpd.c b/tftpd/tftpd.c index 1f5d9d6..ff39c85 100644 --- a/tftpd/tftpd.c +++ b/tftpd/tftpd.c @@ -364,7 +364,7 @@ int main(int argc, char **argv) srand(time(NULL) ^ getpid()); while ((c = getopt_long(argc, argv, short_options, long_options, NULL)) - != -1) + != -1) switch (c) { case '4': ai_fam = AF_INET; @@ -467,9 +467,9 @@ int main(int argc, char **argv) case 'v': verbosity++; break; - case OPT_VERBOSITY: - verbosity = atoi(optarg); - break; + case OPT_VERBOSITY: + verbosity = atoi(optarg); + break; case 'V': /* Print configuration to stdout and exit */ printf("%s\n", TFTPD_CONFIG_STR);
Ferenc Wagner
2009-Sep-05 16:42 UTC
[syslinux] [tftpd PATCH 5/5] Implement the --pidfile option
Setting the umask moved later, right before entering the select loop, so that it does not affect the permissions of the pid file. Signed-off-by: Ferenc Wagner <wferi at niif.hu> --- tftpd/tftpd.8.in | 5 +++++ tftpd/tftpd.c | 49 +++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/tftpd/tftpd.8.in b/tftpd/tftpd.8.in index 367f7ab..8486934 100644 --- a/tftpd/tftpd.8.in +++ b/tftpd/tftpd.8.in @@ -133,6 +133,11 @@ system-provided access controls for the user specified via the .B \-\-user option. .TP +\fB\-\-pidfile\fP \fIpidfile\fP, \fB\-P\fP \fIpidfile\fP When run in +standalone mode, write the process ID of the listening server into +\fIpidfile\fP. On normal termination (SIGTERM or SIGINT) the pid file +is automatically removed. +.TP \fB\-\-timeout\fP \fItimeout\fP, \fB\-t\fP \fItimeout\fP When run from .B inetd diff --git a/tftpd/tftpd.c b/tftpd/tftpd.c index ff39c85..69ff121 100644 --- a/tftpd/tftpd.c +++ b/tftpd/tftpd.c @@ -144,6 +144,13 @@ static void handle_sighup(int sig) caught_sighup = 1; } +/* Handle exit requests by SIGTERM and SIGINT */ +static volatile sig_atomic_t exit_signal = 0; +static void handle_exit(int sig) +{ + exit_signal = sig; +} + /* Handle timeout signal or timeout event */ void timer(int sig) { @@ -318,9 +325,10 @@ static struct option long_options[] = { { "retransmit", 1, NULL, 'T' }, { "port-range", 1, NULL, 'R' }, { "map-file", 1, NULL, 'm' }, + { "pidfile", 1, NULL, 'P' }, { NULL, 0, NULL, 0 } }; -static const char short_options[] = "46cspvVlLa:B:u:U:r:t:T:R:m:"; +static const char short_options[] = "46cspvVlLa:B:u:U:r:t:T:R:m:P:"; int main(int argc, char **argv) { @@ -352,6 +360,7 @@ int main(int argc, char **argv) #ifdef WITH_REGEX char *rewrite_file = NULL; #endif + const char *pidfile = NULL; u_short tp_opcode; /* basename() is way too much of a pain from a portability standpoint */ @@ -475,6 +484,9 @@ int main(int argc, char **argv) printf("%s\n", TFTPD_CONFIG_STR); exit(0); break; + case 'P': + pidfile = optarg; + break; default: syslog(LOG_ERR, "Unknown option: '%c'", optopt); break; @@ -507,16 +519,19 @@ int main(int argc, char **argv) exit(EX_NOUSER); } - if (spec_umask || !unixperms) - umask(my_umask); - #ifdef WITH_REGEX if (rewrite_file) rewrite_rules = read_remap_rules(rewrite_file); #endif + if (pidfile && !standalone) { + syslog(LOG_WARNING, "not in standalone mode, ignoring pid file"); + pidfile = NULL; + } + /* If we're running standalone, set up the input port */ if (standalone) { + FILE *pf; #ifdef HAVE_IPV6 if (ai_fam != AF_INET6) { #endif @@ -702,6 +717,20 @@ int main(int argc, char **argv) syslog(LOG_ERR, "cannot daemonize: %m"); exit(EX_OSERR); } + set_signal(SIGTERM, handle_exit, 0); + set_signal(SIGINT, handle_exit, 0); + if (pidfile) { + pf = fopen (pidfile, "w"); + if (!pf) { + syslog(LOG_ERR, "cannot open pid file '%s' for writing: %m", pidfile); + pidfile = NULL; + } else { + if (fprintf(pf, "%d\n", getpid()) < 0) + syslog(LOG_ERR, "error writing pid file '%s': %m", pidfile); + if (fclose(pf)) + syslog(LOG_ERR, "error closing pid file '%s': %m", pidfile); + } + } if (fd6 > fd4) fdmax = fd6; else @@ -734,11 +763,23 @@ int main(int argc, char **argv) lose packets as a result. */ set_signal(SIGHUP, handle_sighup, 0); + if (spec_umask || !unixperms) + umask(my_umask); + while (1) { fd_set readset; struct timeval tv_waittime; int rv; + if (exit_signal) { /* happens in standalone mode only */ + if (pidfile && unlink(pidfile)) { + syslog(LOG_WARNING, "error removing pid file '%s': %m", pidfile); + exit(EX_OSERR); + } else { + exit(0); + } + } + if (caught_sighup) { caught_sighup = 0; if (standalone) {
Reasonably Related Threads
- tftpd-hpa suggestions
- [PATCH] tftpd.c: write a pid file in standalone mode
- [PATCH 0/1] sparsify: Let --in-place capture ^C and shut down gracefully
- [PATCH v2 00/18] Add discard support.
- [PATCH 0/4] sparsify: Warn instead of error if a filesystem cannot be fstrimmed.