I’m writing a program that requires admin privileges (an updater). At some point, we need to start another program and let the first one end. So the parent process makes a fork, detaches from its child and ends execution.
As root (using “su” for exemple), everything works as expected.
As a regular user, nothing special happens (except of course for writing files as root).
But, using sudo, as soon as the parent process ends its execution, its child dies as well.
I wrote a minimal program to debug this issue:
int main()
{
std::cout << "start: " << getuid() << std::endl;
if (auto pid = fork() == 0)
{
setpgid(0, 0);
std::cout << "fork has detached" << std::endl;
sleep(1);
std::ofstream test("/root/test.txt");
std::cout << "fork: " << getuid() << " " << test.is_open() << std::endl;
}
else
{
setpgid(pid, 0);
std::cout << "parent: " << getuid() << std::endl;
}
return 0;
}
I expect as output:
start: 0
parent: 0
fork has detached
fork: 0 1
But with sudo I only get:
start: 0
parent: 0
fork has detached
If I add a wait() call in the parent, the child works as expected with sudo. So the child dies with its parent. The debug tools I tried only showed traces for the parent, how can I track the detached child as well?
Why does using sudo makes my program behave this way?
I can work around this issue for my project, but I’m interested in the explanation of this strange behaviour.
Maxime Beluguet is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.