setuid 创建一个子进程

setuid create one child process

本文关键字:一个 子进程 创建 setuid      更新时间:2023-10-16

当我尝试在我的程序中setuid时,它会创建 2 个进程。 一个是父进程,另一个是子进程。

int isRoot()
{
if (getuid() != 0)
{
return 0;
}
else
{
return 1;
}
}
int main()
{
printf("%s n n",getUserName());
printf("%dn",getuid());
if(!isRoot())
{
printf("This program must be run as root/sudo user!!");
exit(0);
}
seteuid(1000);
while (1) {}
printf("%s n n",getUserName());
}

当我使用命令检查ps它向我显示两个不同的过程时:

sagar@sagar-desktop:~$ ps -aef | grep a.out
root     20293 18135  0 15:36 pts/7    00:00:00 sudo ./a.out
sagar    20294 20293 99 15:36 pts/7    00:00:17 ./a.out

谁能告诉我为什么它创建两个进程而不是一个进程?

你错了。这就是sudo x所做的。它在新进程中调用x作为根并等待它,因此这两个进程。

不,这里只有一个流程副本。

root     20293 18135  0 15:36 pts/7    00:00:00 sudo ./a.out

这是sudo,而不是你的进程:./a.out是它的命令行参数。然后,它会将您的进程生成为子进程,正如您注意到的那样:

sagar    20294 20293 99 15:36 pts/7    00:00:17 ./a.out