linux-sysprog · advanced · ~30 min
Memorize the conventional double-fork daemon recipe.
Return the canonical daemonization steps as an array of integer constants:
enum {
STEP_FORK_1=1, STEP_SETSID=2, STEP_FORK_2=3, STEP_CHDIR_ROOT=4,
STEP_UMASK_0=5, STEP_CLOSE_STDIN=6, STEP_REDIRECT_STDOUT_NULL=7,
STEP_REDIRECT_STDERR_NULL=8
};
int daemon_steps(int *out, int max);
Fill out with the steps in the correct order, returning the count.
Becoming a 'real' daemon requires a precise dance: fork twice, setsid, chdir /, umask 0, close fds, redirect stdio. Get the order wrong and your daemon leaks a controlling terminal or stays in the session group — both are pretty bad bugs.
max >= 8.
Sequence of 8 step ids in correct order.
No syscalls actually executed.
enum {
STEP_FORK_1=1, STEP_SETSID=2, STEP_FORK_2=3, STEP_CHDIR_ROOT=4,
STEP_UMASK_0=5, STEP_CLOSE_STDIN=6, STEP_REDIRECT_STDOUT_NULL=7,
STEP_REDIRECT_STDERR_NULL=8
};
int daemon_steps(int *out, int max) { /* TODO */ return 0; }
setsid before the first fork (fails — the parent is process group leader); only forking once (your daemon can still acquire a controlling tty); chdir after closing fds (chdir is fine but order is conventional).
max < 8 — return as many as fit.
O(1).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.