Comment 19 for bug 456806

Revision history for this message
Laurent Birtz (laurent-birtz) wrote :

Demonize code:

/* Become daemon */
if (daemonise) {
        pid_t pid;

        /* Fork once because Upstart makes us a session leader,
         * or we may be a session leader of an existing process
         * group.
         */
        pid = fork ();
        if (pid < 0) {
                nih_fatal ("%s: %s", _("Unable to become daemon"),
                           strerror (errno));

                exit (EXIT_ERROR);
        } else if (pid > 0) {
                exit (0);
        }

        /* Create a new session */
        setsid ();

        /* Fork again so that we're not the leader of that session */
        pid = fork ();
        if (pid < 0) {
                nih_fatal ("%s: %s", _("Unable to become daemon"),
                           strerror (errno));

                exit (EXIT_ERROR);
        } else if (pid > 0) {
                exit (0);
        }

        /* Usual daemon cleanups */
        if (chdir ("/"))
                ;
        umask (0);

        /* Send all logging output to syslog */
        //openlog (program_name, LOG_PID, LOG_DAEMON);
        //nih_log_set_logger (nih_logger_syslog);

        nih_signal_set_ignore (SIGHUP);
}

The standard input file descriptor isn't closed so mountall is still
using the terminal after the daemon is started. The daemon is thus
reading input from the terminal and modifying its settings concurrently
with whatever process is using the terminal after the launch of mountall.

On my system running 'mountall --daemon' manually will hang the shell
roughly 50% of the time. The race condition is highly
environment-dependent. Running with strace -f won't trigger the problem,
for example.

In my opinion the behavior of mountall --daemon is broken.

This bug is still present in mountall-2.1.