临时更改 运行 进程的标识

Temporarily change identity of running process

我想临时更改 运行 root 进程的身份。

我读过的方法是先调用 setegid,然后调用 seteuid

问题是该组不知何故被忽略了:

<?php
# This is executed as root.
var_dump(posix_setegid(61));   # localaccounts(61)
var_dump(posix_seteuid(502));  # bot(502)

$fp = fopen("/tmp/test", "w+b");

system("ls -la /tmp/test");

unlink("/tmp/test");

输出:

$ sudo php -f /tmp/test.php
bool(true)
bool(true)
-rw-r--r--  1 bot  wheel  0 Feb  2 17:49 /tmp/test

但我希望它输出:

-rw-r--r--  1 bot  localaccounts  0 Feb  2 17:49 /tmp/test

我是不是漏掉了什么?

更新:它可以在 linux 机器上运行。这是什么奇怪的 OSX 怪癖吗?

Mac os x 派生自 Unix 的 BSD 分支,并且

According to BSD Unix semantics, the group ownership given to a newly created file is unconditionally inherited from the group ownership of the directory in which it is created.

(来自 https://en.m.wikipedia.org/wiki/User_identifier#Effective_user_ID - 我找不到更好的来源)

创建文件后,您始终可以使用chgrp更改组