diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 2007-06-28 21:43:37 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 2007-06-28 21:43:37 +0000 |
commit | fd9e33aa7849cf7f7bb59912ec065d9d027d9ed4 (patch) | |
tree | 6c83245cf67797defdff04375d844e1a3e4d6a1c /libexec/identd/identd.c | |
parent | 407481b866fe55a776965e56142aff89bb3491a9 (diff) |
Add a SIGCHLD handler for background mode instead of doing waitpid()
at a single point in the event loop (which would always leave
a single zombie around after the first connection).
Also use daemon() instead of doing it by hand. OK deraadt@ beck@
Diffstat (limited to 'libexec/identd/identd.c')
-rw-r--r-- | libexec/identd/identd.c | 26 |
1 files changed, 15 insertions, 11 deletions
diff --git a/libexec/identd/identd.c b/libexec/identd/identd.c index fea1a295bc3..fa6e4886680 100644 --- a/libexec/identd/identd.c +++ b/libexec/identd/identd.c @@ -1,4 +1,4 @@ -/* $OpenBSD: identd.c,v 1.42 2005/12/06 22:05:22 deraadt Exp $ */ +/* $OpenBSD: identd.c,v 1.43 2007/06/28 21:43:36 millert Exp $ */ /* * This program is in the public domain and may be used freely by anyone @@ -58,6 +58,7 @@ char *charset_name = ""; static pid_t child_pid; void usage(void); +void sigchld(int); char * gethost(struct sockaddr_storage *ss); void @@ -271,14 +272,7 @@ main(int argc, char *argv[]) struct servent *sp; int fd; - if (fork()) - exit(0); - - close(0); - close(1); - close(2); - - if (fork()) + if (daemon(0, 0) != 0) exit(0); fd = socket(AF_INET, SOCK_STREAM, 0); @@ -341,6 +335,8 @@ main(int argc, char *argv[]) int nfds, fd; struct pollfd pfd[1]; + signal(SIGCHLD, sigchld); + /* * Loop and dispatch client handling processes */ @@ -402,8 +398,6 @@ main(int argc, char *argv[]) * And fork, then close the fd if we are the parent. */ child_pid = fork(); - while (waitpid(-1, NULL, WNOHANG) > 0) - ; } while (child_pid && (close(fd), 1)); /* @@ -500,3 +494,13 @@ error(char *fmt, ...) va_end(ap); exit(1); } + +void +sigchld(int signo) +{ + pid_t pid; + + do { + pid = waitpid(-1, NULL, WNOHANG); + } while (pid > 0 || (pid == -1 && errno == EINTR)); +} |