Back to DaemonApplication class

Method changeIdentity

protected bool
changeIdentity
()
Method to change the identity of the daemon process and resources.
Returns
  • bool True if identity successfully changed
Since
  • 1.7.0
-
  • \Joomla\CMS\Application\posix_setuid()

Method changeIdentity - Source code

/**
 * Method to change the identity of the daemon process and resources.
 *
 * @return  boolean  True if identity successfully changed
 *
 * @since   1.7.0
 * @see     posix_setuid()
 */
protected function changeIdentity()
{
    // Get the group and user ids to set for the daemon.
    $uid = (int) $this->config->get('application_uid', 0);
    $gid = (int) $this->config->get('application_gid', 0);
    // Get the application process id file path.
    $file = $this->config->get('application_pid_file');
    // Change the user id for the process id file if necessary.
    if ($uid && fileowner($file) != $uid && !@chown($file, $uid)) {
        Log::add('Unable to change user ownership of the process id file.', Log::ERROR);
        return false;
    }
    // Change the group id for the process id file if necessary.
    if ($gid && filegroup($file) != $gid && !@chgrp($file, $gid)) {
        Log::add('Unable to change group ownership of the process id file.', Log::ERROR);
        return false;
    }
    // Set the correct home directory for the process.
    if ($uid && ($info = posix_getpwuid($uid)) && is_dir($info['dir'])) {
        system('export HOME="' . $info['dir'] . '"');
    }
    // Change the user id for the process necessary.
    if ($uid && posix_getuid() != $uid && !@posix_setuid($uid)) {
        Log::add('Unable to change user ownership of the process.', Log::ERROR);
        return false;
    }
    // Change the group id for the process necessary.
    if ($gid && posix_getgid() != $gid && !@posix_setgid($gid)) {
        Log::add('Unable to change group ownership of the process.', Log::ERROR);
        return false;
    }
    // Get the user and group information based on uid and gid.
    $user = posix_getpwuid($uid);
    $group = posix_getgrgid($gid);
    Log::add('Changed daemon identity to ' . $user['name'] . ':' . $group['name'], Log::INFO);
    return true;
}