]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/daemon.php
Ticket 753
[quix0rs-gnu-social.git] / lib / daemon.php
index 09144afe730e2119c75d373dfbccdb6b8098f2c2..359a4343b5efc2a7a2e7f0557715b53dc1fecbcb 100644 (file)
@@ -28,6 +28,7 @@ class Daemon {
        function background() {
                $pid = pcntl_fork();
                if ($pid < 0) { # error
+                       common_log(LOG_ERR, "Could not fork.");
                        return false;
                } else if ($pid > 0) { # parent
                        common_log(LOG_INFO, "Successfully forked.");
@@ -63,12 +64,15 @@ class Daemon {
                        return false;
                }
                
-               file_put_contents($pidfilename, posix_getpid());
+           return file_put_contents($pidfilename, posix_getpid() . "\n");
        }
 
        function clearPidFile() {
                $pidfilename = $this->pidFilename();
-               unlink($pidfilename);
+               if (!$pidfilename) {
+                   return false;
+               }
+               return unlink($pidfilename);
        }
        
        function pidFilename() {
@@ -80,21 +84,33 @@ class Daemon {
                if (!$name) {
                        return NULL;
                }
-               return $piddir . '/' . $name;
+               return $piddir . '/' . $name . '.pid';
        }
 
        function changeUser() {
 
-               if (common_config('daemon', 'user')) {
-                       $user_info = posix_getpwnam(common_config('daemon', 'user'));
-                       common_log(LOG_INFO, "Setting user to " . common_config('daemon', 'user'));
-                       posix_setuid($user_info['uid']);
+               $username = common_config('daemon', 'user');
+               
+               if ($username) {
+                       $user_info = posix_getpwnam($username);
+                       if (!$user_info) {
+                               common_log(LOG_WARNING, 'Ignoring unknown user for daemon: ' . $username);
+                       } else {
+                               common_log(LOG_INFO, "Setting user to " . $username);
+                               posix_setuid($user_info['uid']);
+                       }
                }
+
+               $groupname = common_config('daemon', 'group');
                
-               if (common_config('daemon', 'group')) {
-                       $group_info = posix_getgrnam(common_config('daemon', 'group'));
-                       common_log(LOG_INFO, "Setting group to " . common_config('daemon', 'group'));
-                       posix_setgid($group_info['gid']);
+               if ($groupname) {
+                       $group_info = posix_getgrnam($groupname);
+                       if (!$group_info) {
+                               common_log(LOG_WARNING, 'Ignoring unknown group for daemon: ' . $groupname);
+                       } else {
+                               common_log(LOG_INFO, "Setting group to " . $groupname);
+                               posix_setgid($group_info['gid']);
+                       }
                }
        }