Daemonizing death under FreeBSD
Brion Vibber
brion@pobox.com
Tue, 21 Oct 2003 02:04:44 -0700
--Apple-Mail-8-373054534
Content-Transfer-Encoding: 7bit
Content-Type: text/plain;
charset=US-ASCII;
format=flowed
strace seems to have issues under 5.1, but I got a ktrace/kdump output
for "memcache -d" vs "memcache". Everything seems the same up to the
fork in the daemon version, after which it immediately exits.
The man page for daemon(3) recommends not opening any files prior to
calling daemon(), since it closes the first three file descriptors on
the assumption they're stdin, stdout, and stderr... Sure enough, if I
grab the call to daemon() and move it up to right after the options
check, it seems to work.
diff -u attached, I make no guarantees that this doesn't break other
things or fail spectacularly under other conditions.
-- brion vibber (brion @ pobox.com)
--Apple-Mail-8-373054534
Content-Disposition: attachment;
filename=freebsd-daemon-quick-fix.diff
Content-Transfer-Encoding: 7bit
Content-Type: application/octet-stream;
x-unix-mode=0755;
name="freebsd-daemon-quick-fix.diff"
--- memcached.c.orig Tue Oct 21 01:53:03 2003
+++ memcached.c Tue Oct 21 01:54:43 2003
@@ -1305,6 +1305,15 @@
}
}
+ if (daemonize) {
+ int res;
+ res = daemon(0, settings.verbose);
+ if (res == -1) {
+ fprintf(stderr, "failed to daemon() in order to daemonize\n");
+ return 1;
+ }
+ }
+
/* initialize other stuff */
item_init();
event_init();
@@ -1334,15 +1343,6 @@
}
if (setgid(pw->pw_gid)<0 || setuid(pw->pw_uid)<0) {
fprintf(stderr, "failed to assume identity of user %s\n", username);
- return 1;
- }
- }
-
- if (daemonize) {
- int res;
- res = daemon(0, settings.verbose);
- if (res == -1) {
- fprintf(stderr, "failed to daemon() in order to daemonize\n");
return 1;
}
}
--Apple-Mail-8-373054534--