diff --git a/common/daemon.cpp b/common/daemon.cpp index ba82d2d3..a61d3277 100644 --- a/common/daemon.cpp +++ b/common/daemon.cpp @@ -114,10 +114,6 @@ void Daemon::daemonize() if (user_uid != (uid_t)-1 && user_uid != getuid() && setuid(user_uid) == -1) throw SnapException("Failed to set user " + user_); - /// Try to lock file - if (lockf(pidFilehandle_, F_TLOCK, 0) == -1) - throw SnapException("Could not lock PID lock file \"" + pidfile_ + "\""); - /// Our process ID and Session ID pid_t pid, sid; @@ -126,20 +122,9 @@ void Daemon::daemonize() if (pid < 0) exit(EXIT_FAILURE); - /// If we got a good PID, then the parent process should only exit after - /// writing the pid else systemd is confused. + /// If we got a good PID, then we can exit the parent process. if (pid > 0) - { - char str[10]; - /// Get and format PID - sprintf(str, "%d\n", pid); - - /// write pid to lockfile - if (write(pidFilehandle_, str, strlen(str)) != (int)strlen(str)) - throw SnapException("Could not write PID to lock file \"" + pidfile_ + "\""); - exit(EXIT_SUCCESS); - } /// Change the file mode mask umask(0); @@ -161,6 +146,18 @@ void Daemon::daemonize() exit(EXIT_FAILURE); } + /// Try to lock file + if (lockf(pidFilehandle_, F_TLOCK, 0) == -1) + throw SnapException("Could not lock PID lock file \"" + pidfile_ + "\". Is the daemon already running?"); + + char str[10]; + /// Get and format PID + sprintf(str, "%d\n", getpid()); + + /// write pid to lockfile + if (write(pidFilehandle_, str, strlen(str)) != (int)strlen(str)) + throw SnapException("Could not write PID to lock file \"" + pidfile_ + "\""); + /// Close out the standard file descriptors close(STDIN_FILENO); close(STDOUT_FILENO);