我有一个权限为 4750 的进程。我的 Linux 系统中存在两个用户。root 用户和 appz 用户。该进程继承了以“appz”用户身份运行的进程管理器的权限。
我有两个基本的例程:
void do_root (void)
{
int status;
status = seteuid (euid);
if (status < 0) {
exit (status);
}
}
/* undo root permissions */
void undo_root (void)
{
int status;
status = seteuid (ruid);
if (status < 0) {
exit (status);
}
status = setuid(ruid);
if (status < 0) {
exit (status);
}
}
我的流程如下:
int main() {
undo_root();
do some stuff;
do_root();
bind( port 80); //needs root perm
undo_root();
while(1) {
accept commads()
if ( commands needs root user access)
{
do_root();
execute();
undo_root();
}
}
如您所见,我想以 root 身份执行一些命令。我正在尝试暂时放弃权限,如果任务需要 root 访问权限,我将命令包装在 do_root 和 undo_root 调用之间。
但是,我的程序似乎无法正常工作。
规范的方法是什么?