diff options
author | Yaroslav Tykhiy <ytykhiy@gmail.com> | 2006-01-21 13:06:37 +0000 |
---|---|---|
committer | Yaroslav Tykhiy <ytykhiy@gmail.com> | 2006-01-21 13:06:37 +0000 |
commit | a599a64a9f7c48b27f8f282e74569d35b1b3968b (patch) | |
tree | 2f35ab5470640302ea150d034cc0238fca6934fb /libexec/ftpd | |
parent | f014cff4cd6c5447d47b77c065f22c889ce47d21 (diff) | |
download | src-a599a64a9f7c48b27f8f282e74569d35b1b3968b.tar.gz src-a599a64a9f7c48b27f8f282e74569d35b1b3968b.zip |
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
Notes
Notes:
svn path=/head/; revision=154634
Diffstat (limited to 'libexec/ftpd')
-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 a5a60a421be9..60779b4ebef9 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); |