diff options
author | yar <yar@FreeBSD.org> | 2006-01-21 13:06:37 +0000 |
---|---|---|
committer | yar <yar@FreeBSD.org> | 2006-01-21 13:06:37 +0000 |
commit | 1e8c9a78423addd890e6bfcd7a6e0c8a67a455b8 (patch) | |
tree | 2f35ab5470640302ea150d034cc0238fca6934fb /libexec | |
parent | 8ab4cc00ee67852a0a14ebd0da2f7dc76df7cc4d (diff) | |
download | FreeBSD-src-1e8c9a78423addd890e6bfcd7a6e0c8a67a455b8.zip FreeBSD-src-1e8c9a78423addd890e6bfcd7a6e0c8a67a455b8.tar.gz |
In the daemon code, check for and report possible errors
from accept(2) and fork(2). Also close all unneeded fds
in the child process, namely listening sockets for all
address families and the fd initially obtained from accept(2).
(The main ftpd code operates on stdin/stdout anyway as it
has been designed for running from inetd.)
MFC after: 5 days
Diffstat (limited to 'libexec')
-rw-r--r-- | libexec/ftpd/ftpd.c | 36 |
1 files changed, 22 insertions, 14 deletions
diff --git a/libexec/ftpd/ftpd.c b/libexec/ftpd/ftpd.c index a5a60a4..60779b4 100644 --- a/libexec/ftpd/ftpd.c +++ b/libexec/ftpd/ftpd.c @@ -495,22 +495,29 @@ main(int argc, char *argv[], char **envp) fd = accept(ctl_sock[i], (struct sockaddr *)&his_addr, &addrlen); - if (fd >= 0) { - if ((pid = fork()) == 0) - break; - else - close(fd); + if (fd == -1) { + syslog(LOG_WARNING, + "accept: %m"); + continue; + } + switch (pid = fork()) { + case 0: + /* child */ + (void) dup2(fd, 0); + (void) dup2(fd, 1); + (void) close(fd); + for (i = 1; i <= *ctl_sock; i++) + close(ctl_sock[i]); + if (pfh != NULL) + pidfile_close(pfh); + goto gotchild; + case -1: + syslog(LOG_WARNING, "fork: %m"); + /* FALLTHROUGH */ + default: + close(fd); } } - if (pid == 0) { - /* child */ - (void) dup2(fd, 0); - (void) dup2(fd, 1); - close(ctl_sock[i]); - if (pfh != NULL) - pidfile_close(pfh); - break; - } } } else { addrlen = sizeof(his_addr); @@ -520,6 +527,7 @@ main(int argc, char *argv[], char **envp) } } +gotchild: sa.sa_handler = SIG_DFL; (void)sigaction(SIGCHLD, &sa, NULL); |