本文整理汇总了PHP中posix_setgid函数的典型用法代码示例。如果您正苦于以下问题:PHP posix_setgid函数的具体用法?PHP posix_setgid怎么用?PHP posix_setgid使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了posix_setgid函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: setuidgid
public static function setuidgid($user)
{
$uid = posix_getuid();
if ($uid !== 0) {
throw new \RuntimeException("setuidgid is only root");
}
$nam = posix_getpwnam($user);
if (!$nam) {
throw new \RuntimeException("unkonwn user \"{$user}\"");
}
$uid = $nam['uid'];
$gid = $nam['gid'];
if (!posix_setgid($gid)) {
throw new \RuntimeException("unable setgid({$gid})");
}
if (!posix_setegid($gid)) {
throw new \RuntimeException("unable setegid({$gid})");
}
if (!posix_setuid($uid)) {
throw new \RuntimeException("unable setuid({$uid})");
}
if (!posix_seteuid($uid)) {
throw new \RuntimeException("unable seteuid({$uid})");
}
}
示例2: onWorkerStart
/**
* 此事件在worker进程启动时发生。这里创建的对象可以在worker进程生命周期内使用。
*
* @param ISwoole $sw
* @param int $worker_id
*/
function onWorkerStart($sw, $worker_id)
{
$this->ctx->pid = getmypid();
$user = posix_getpwnam($this->ctx->cfgs['default']['owner']['user']);
posix_setuid($user['uid']);
posix_setgid($user['gid']);
$this->worker_id = $worker_id;
}
示例3: setUser
/**
* 设置进程运行账号
* @param [type] $user [description]
*/
public static function setUser($user)
{
$userInfo = posix_getpwnam($user);
if (!$userInfo) {
return;
}
posix_setgid($userInfo['gid']);
posix_setuid($userInfo['uid']);
}
示例4: setUid
public static function setUid($uid, $gid)
{
if (!posix_setgid($gid)) {
// 必须先设置GID, 再设置UID
throw new Exception("Unable to set GID: " . posix_strerror(posix_get_last_error()));
}
if (!posix_setuid($uid)) {
throw new Exception("Unable to set UID: " . posix_strerror(posix_get_last_error()));
}
}
示例5: change_identity
/**
* Change the identity to a non-priv user
*/
function change_identity($uid, $gid)
{
if (!posix_setgid($gid)) {
print "Unable to setgid to " . $gid . "!\n";
exit;
}
if (!posix_setuid($uid)) {
print "Unable to setuid to " . $uid . "!\n";
exit;
}
}
示例6: changeUser
/**
* 改变进程的用户ID
* @param $user
*/
static function changeUser($user)
{
if (!function_exists('posix_getpwnam')) {
trigger_error(__METHOD__ . ": require posix extension.");
return;
}
$user = posix_getpwnam($user);
if ($user) {
posix_setuid($user['uid']);
posix_setgid($user['gid']);
}
}
示例7: onStart
function onStart($serv)
{
if (!defined('WEBROOT')) {
define('WEBROOT', $this->config['server']['webroot']);
}
if (isset($this->config['server']['user'])) {
$user = posix_getpwnam($this->config['server']['user']);
if ($user) {
posix_setuid($user['uid']);
posix_setgid($user['gid']);
}
}
$this->log(self::SOFTWARE . ". running. on {$this->server->host}:{$this->server->port}");
}
示例8: __construct
/**
* 构造方法
*/
public function __construct($setting)
{
$this->config = $setting['config'];
$this->cronPath = $setting['cron_path'];
if (isset($setting['group'])) {
$groupinfo = posix_getpwnam($setting['group']);
posix_setgid($groupinfo['gid']);
}
if (isset($setting['user'])) {
$userinfo = posix_getgrnam($setting['user']);
posix_setuid($groupinfo['uid']);
}
include __DIR__ . '/ParseCrontab.php';
include __DIR__ . '/ParseInterval.php';
}
示例9: dropPrivileges
protected function dropPrivileges(array $server)
{
if (!array_key_exists('user', $server) and !array_key_exists('group', $server)) {
// nothing to do
return;
}
if (posix_getuid() != 0) {
echo "\n[Warning] Can't change uid/gid because aip is not run by superuser\n";
return;
}
if (isset($server['user'])) {
posix_setuid(self::getUserId($server['user']));
}
if (isset($server['group'])) {
posix_setgid(self::getGroupId($server['group']));
}
}
示例10: run
function run($work)
{
if (1 === posix_getppid()) {
return;
}
if ($this->user) {
if (!posix_setgid($this->user['gid']) || !posix_setuid($this->user['uid'])) {
throw new \RuntimeException("Unable to switch to user '{$this->user['name']}'");
}
}
Utility::fork(function () use($work) {
if (-1 === posix_setsid()) {
throw new \RuntimeException('Unable to set setsid()');
}
if (false === chdir('/')) {
throw new \RuntimeException('Unable to chdir(\'/\')');
}
umask(0);
Utility::fork($work);
});
}
示例11: changeUser
/**
* @return bool
*/
private function changeUser()
{
$user = $this->container->getConfig()->get('command.user');
// Bypass cache commands as we need the sudoer user to run the commands
if (null !== $user && (!isset($_SERVER['argv'][1]) || $_SERVER['argv'][1] !== 'flushall') && (!isset($_SERVER['argv'][1]) || $_SERVER['argv'][1] !== 'redis:flushall') && (!isset($_SERVER['argv'][1]) || $_SERVER['argv'][1] !== 'apc:flushall')) {
$name = $user;
$user = posix_getpwnam($user);
posix_setgid($user['gid']);
posix_setuid($user['uid']);
if (posix_geteuid() !== (int) $user['uid']) {
$output = new ConsoleOutput();
$formatter = new FormatterHelper();
$output->writeln('', true);
$errorMessages = array('', ' [Error] ', ' Could not change user to ' . $name . ' ', '');
$formattedBlock = $formatter->formatBlock($errorMessages, 'error');
$output->writeln($formattedBlock);
$output->writeln('', true);
return false;
}
}
return true;
}
示例12: posix_getgrnam
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
***********************************************************/
/**
* @file migratetest.php
* @brief Test migration function
*
* @return 0 for success, 1 for failure.
**/
/* User must be in group fossy! */
$GID = posix_getgrnam("fossy");
posix_setgid($GID['gid']);
$Group = `groups`;
if (!preg_match("/\\sfossy\\s/", $Group) && posix_getgid() != $GID['gid']) {
print "FATAL: You must be in group 'fossy' to update the FOSSology database.\n";
exit(1);
}
/* Initialize the program configuration variables */
$SysConf = array();
// fo system configuration variables
$PG_CONN = 0;
// Database connection
$Plugins = array();
/* defaults */
$Verbose = false;
$DatabaseName = "fossology";
$UpdateLiceneseRef = false;
示例13: setProcessUser
/**
* 尝试设置运行当前进程的用户
* @return void
*/
protected static function setProcessUser($user_name)
{
if (empty($user_name) || posix_getuid() !== 0) {
return;
}
$user_info = posix_getpwnam($user_name);
if ($user_info['uid'] != posix_getuid() || $user_info['gid'] != posix_getgid()) {
if (!posix_setgid($user_info['gid']) || !posix_setuid($user_info['uid'])) {
self::log('Notice : Can not run woker as ' . $user_name . " , You shuld be root\n", true);
}
}
}
示例14: changeUser
function changeUser()
{
$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 ($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']);
}
}
}
示例15: changeUser
/**
* @throws Exception
*/
private function changeUser()
{
$user = $this->getConfig()->getUser();
if ($user) {
$user = posix_getpwnam($user);
if (posix_geteuid() !== (int) $user['uid']) {
posix_setgid($user['gid']);
posix_setuid($user['uid']);
if (posix_geteuid() !== (int) $user['uid']) {
$message = "Unable to change user to {$user['uid']}";
if (null !== $this->logger) {
$this->logger->error($message);
}
throw new Exception($message);
}
}
}
}