diff options
| author | Hauke Mehrtens | 2026-04-13 08:31:21 +0000 |
|---|---|---|
| committer | Hauke Mehrtens | 2026-05-15 00:29:10 +0000 |
| commit | 778ccbbf5f8aa90e368100897f59322314287ab4 (patch) | |
| tree | 30da5fb85baf08e7349ab6eef4ffda9eecfed883 | |
| parent | add5389470f0f5534a1b7ef79f69e98c95a5ba82 (diff) | |
| download | uhttpd-778ccbbf5f8aa90e368100897f59322314287ab4.tar.gz | |
main: fix daemonization stdio redirection and fd leak
Two bugs in the daemon child setup:
1. The condition `cur_fd > 0` fails to redirect stdin/stdout/stderr
when open("/dev/null") returns fd 0 (possible if stdin was already
closed before daemonising). Change to `cur_fd >= 0` to handle
that case correctly.
2. After the three dup2() calls the original file descriptor
`cur_fd` is never closed. If open() returned a descriptor number
greater than 2 (the common case), that descriptor is leaked into
the server process for its entire lifetime. Add `close(cur_fd)`
when `cur_fd > 2` to release it.
3. Change the open() flags from O_WRONLY to O_RDWR so that fd 0
(stdin) is also opened in a readable state, matching normal daemon
practice.
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Signed-off-by: Hauke Mehrtens <hauke@hauke-m.de>
| -rw-r--r-- | main.c | 6 |
1 files changed, 4 insertions, 2 deletions
@@ -642,11 +642,13 @@ int main(int argc, char **argv) if (chdir("/")) perror("chdir()"); - cur_fd = open("/dev/null", O_WRONLY); - if (cur_fd > 0) { + cur_fd = open("/dev/null", O_RDWR); + if (cur_fd >= 0) { dup2(cur_fd, 0); dup2(cur_fd, 1); dup2(cur_fd, 2); + if (cur_fd > 2) + close(cur_fd); } break; |