Reformat code

This commit is contained in:
badaix 2020-06-07 13:35:39 +02:00
parent 6d2f1a2b3e
commit be40e66c63

View file

@ -51,118 +51,118 @@ Daemon::~Daemon()
void Daemon::daemonize() void Daemon::daemonize()
{ {
std::string pidfileDir(pidfile_.substr(0, pidfile_.find_last_of('/'))); std::string pidfileDir(pidfile_.substr(0, pidfile_.find_last_of('/')));
utils::file::mkdirRecursive(pidfileDir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); utils::file::mkdirRecursive(pidfileDir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
/// Ensure only one copy /// Ensure only one copy
pidFilehandle_ = open(pidfile_.c_str(), O_RDWR|O_CREAT, 0644); pidFilehandle_ = open(pidfile_.c_str(), O_RDWR | O_CREAT, 0644);
if (pidFilehandle_ == -1 ) if (pidFilehandle_ == -1)
{ {
/// Couldn't open lock file /// Couldn't open lock file
throw SnapException("Could not open PID lock file \"" + pidfile_ + "\""); throw SnapException("Could not open PID lock file \"" + pidfile_ + "\"");
} }
uid_t user_uid = (uid_t)-1; uid_t user_uid = (uid_t)-1;
gid_t user_gid = (gid_t)-1; gid_t user_gid = (gid_t)-1;
std::string user_name; std::string user_name;
#ifdef FREEBSD // #ifdef FREEBSD
bool had_group = false; // bool had_group = false;
#endif // #endif
if (!user_.empty()) if (!user_.empty())
{ {
struct passwd *pwd = getpwnam(user_.c_str()); struct passwd* pwd = getpwnam(user_.c_str());
if (pwd == nullptr) if (pwd == nullptr)
throw SnapException("no such user \"" + user_ + "\""); throw SnapException("no such user \"" + user_ + "\"");
user_uid = pwd->pw_uid; user_uid = pwd->pw_uid;
user_gid = pwd->pw_gid; user_gid = pwd->pw_gid;
user_name = strdup(user_.c_str()); user_name = strdup(user_.c_str());
/// this is needed by libs such as arts /// this is needed by libs such as arts
setenv("HOME", pwd->pw_dir, true); setenv("HOME", pwd->pw_dir, true);
} }
if (!group_.empty()) if (!group_.empty())
{ {
struct group *grp = getgrnam(group_.c_str()); struct group* grp = getgrnam(group_.c_str());
if (grp == nullptr) if (grp == nullptr)
throw SnapException("no such group \"" + group_ + "\""); throw SnapException("no such group \"" + group_ + "\"");
user_gid = grp->gr_gid; user_gid = grp->gr_gid;
#ifdef FREEBSD // #ifdef FREEBSD
had_group = true; // had_group = true;
#endif // #endif
} }
if (chown(pidfile_.c_str(), user_uid, user_gid) == -1) if (chown(pidfile_.c_str(), user_uid, user_gid) == -1)
{ {
/// Couldn't open lock file /// Couldn't open lock file
throw SnapException("Could not chown PID lock file \"" + pidfile_ + "\""); throw SnapException("Could not chown PID lock file \"" + pidfile_ + "\"");
} }
/// set gid /// set gid
if (user_gid != (gid_t)-1 && user_gid != getgid() && setgid(user_gid) == -1) if (user_gid != (gid_t)-1 && user_gid != getgid() && setgid(user_gid) == -1)
throw SnapException("Failed to set group " + cpt::to_string((int)user_gid)); throw SnapException("Failed to set group " + cpt::to_string((int)user_gid));
//#if defined(FREEBSD) && !defined(MACOS) //#if defined(FREEBSD) && !defined(MACOS)
//#ifdef FREEBSD //#ifdef FREEBSD
/// init supplementary groups /// init supplementary groups
/// (must be done before we change our uid) /// (must be done before we change our uid)
/// no need to set the new user's supplementary groups if we are already this user /// no need to set the new user's supplementary groups if we are already this user
// if (!had_group && user_uid != getuid() && initgroups(user_name, user_gid) == -1) // if (!had_group && user_uid != getuid() && initgroups(user_name, user_gid) == -1)
// throw SnapException("Failed to set supplementary groups of user \"" + user + "\""); // throw SnapException("Failed to set supplementary groups of user \"" + user + "\"");
//#endif //#endif
/// set uid /// set uid
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 /// Try to lock file
if (lockf(pidFilehandle_, F_TLOCK, 0) == -1) if (lockf(pidFilehandle_, F_TLOCK, 0) == -1)
throw SnapException("Could not lock PID lock file \"" + pidfile_ + "\""); 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;
/// Fork off the parent process /// Fork off the parent process
pid = fork(); pid = fork();
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 the parent process should only exit after
/// writing the pid else systemd is confused. /// writing the pid else systemd is confused.
if (pid > 0) if (pid > 0)
{ {
char str[10]; char str[10];
/// Get and format PID /// Get and format PID
sprintf(str, "%d\n", pid); sprintf(str, "%d\n", pid);
/// write pid to lockfile /// write pid to lockfile
if (write(pidFilehandle_, str, strlen(str)) != (int)strlen(str)) if (write(pidFilehandle_, str, strlen(str)) != (int)strlen(str))
throw SnapException("Could not write PID to lock file \"" + pidfile_ + "\""); 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);
/// Open any logs here /// Open any logs here
/// Create a new SID for the child process /// Create a new SID for the child process
sid = setsid(); sid = setsid();
if (sid < 0) if (sid < 0)
{ {
/// Log the failure /// Log the failure
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
/// Change the current working directory /// Change the current working directory
if ((chdir("/")) < 0) if ((chdir("/")) < 0)
{ {
/// Log the failure /// Log the failure
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
/// Close out the standard file descriptors /// Close out the standard file descriptors
close(STDIN_FILENO); close(STDIN_FILENO);
close(STDOUT_FILENO); close(STDOUT_FILENO);
close(STDERR_FILENO); close(STDERR_FILENO);
} }