Revert daemon pid file stuff

This commit is contained in:
badaix 2020-06-08 23:54:40 +02:00
parent 9e22f32548
commit 0d25ebafee

View file

@ -114,10 +114,6 @@ void Daemon::daemonize()
if (user_uid != (uid_t)-1 && user_uid != getuid() && setuid(user_uid) == -1) if (user_uid != (uid_t)-1 && user_uid != getuid() && setuid(user_uid) == -1)
throw SnapException("Failed to set user " + user_); 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 /// Our process ID and Session ID
pid_t pid, sid; pid_t pid, sid;
@ -126,20 +122,9 @@ void Daemon::daemonize()
if (pid < 0) if (pid < 0)
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
/// If we got a good PID, then the parent process should only exit after /// If we got a good PID, then we can exit the parent process.
/// writing the pid else systemd is confused.
if (pid > 0) 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); exit(EXIT_SUCCESS);
}
/// Change the file mode mask /// Change the file mode mask
umask(0); umask(0);
@ -161,6 +146,18 @@ void Daemon::daemonize()
exit(EXIT_FAILURE); 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 out the standard file descriptors
close(STDIN_FILENO); close(STDIN_FILENO);
close(STDOUT_FILENO); close(STDOUT_FILENO);