Child processes of a daemon process

I'm writing a daemon process to execute programs and then restart them if they exit with a status of something other than EXIT_SUCCESS; but these programs will probably not want to be daemon processes themselves. If I use fork() and then call execv() will the new child process be a daemon process too?

I tried running firefox and it didn't work; so I'm guessing no; in which case, how can I start the child processes as normal processes?
Last edited on
To be a "daemon" in UNIX all it means is that the process closes stdin, stdout, and stderr,
and then backgrounds itself by doing a fork()/exec() and allowing the parent process to
exit normally.
I know; my question is asking whether I can start child processes that have stdin, stdout and stderr.

My main function is pretty much this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
int main()
{
    pid_t pid = fork();

    switch (pid) {
        case -1:
            perror("fork");
            exit(EXIT_FAILURE);
        case 0:
            umask(0);

            int conf_fd = 0;
            pid_t sid = 0;

            if ((conf_fd = open("/etc/respawntab", O_RDONLY)) == -1) {
                perror("/etc/respawntab");
                exit(EXIT_FAILURE);
            }

            if ((pid = getpid()) == -1) {
                perror("getpid");
                exit(EXIT_FAILURE);
            }

            if ((sid = setsid()) == -1) {
                perror("setsid");
                exit(EXIT_FAILURE);
            }

            if ((chdir("/")) == -1) {
                perror("chdir: /");
                exit(EXIT_FAILURE);
            }

            close(STDIN_FILENO);
            close(STDOUT_FILENO);
            close(STDERR_FILENO);

            exit(respawnd(conf_fd, pid, sid));
        default:
            exit(EXIT_SUCCESS);
    }

    exit(EXIT_SUCCESS);
}
@jsmith,
I guess the only way to do this would be to dup() stdin, stdout and stderr, store those somewhere, and then re-open them when I create another child process (one that doesn't need to be a deamon. Would this work? Is there a better way of doing it?
That would work.

The whole idea seems a little bit weird.
jsmith wrote:
That would work.

Hooray :)

The whole idea seems a little bit weird.

It's based on the MINIX 3 "reincarnation server." It's a process that can detect driver and server problems or crashes and then restarts or removes the offending driver. It's why MINIX rarely crashes. Unless there's an error with the reincarnation server itself, the file system server or the kernel, it pretty much never crashes because the drivers are all outside of the kernel (except the CLOCK and SYSTEM tasks). I got Tanenbaum and Woodhull's book a few days ago :)

My version runs like a UNIX daemon. My plan is to have it in /etc/rc.d/ and start it from /etc/rc.conf on boot. Then it'll parse a file (/etc/respawntab) and run all of the processes within the file. Then, if any of the processes is terminated with a status other than EXIT_SUCCESS it gives the option of restarting the process.
Last edited on
Just curious why you're going to all that trouble when init gives you (almost) all of this already.
Fun.
Topic archived. No new replies allowed.