简体   繁体   中英

Terminate all child process in LInux

I am developing a sandbox on linux. And now i am confused terminating all process in the sandbox. My sandbox works as follows: At first only one process run in the sandbox. Then it can create several child process. And child process will create their subprocess also. And parent process may exit at some time before its children exited. At last sandbox will terminate all the process.

I used to do this by using killall or pkill -u with a unique user attached to the sandbox.But it seems doesn't work on the program which uses fork() fastly.

Then I search for the source code of pkill and realized that pkill is lose of atomicity.

So how could i achieve my goal ?

You could use process groups setpgid(2) and sessions setsid(2) , but I don't qualify what you do as a sandbox (in particular because if one of the processes is setuid or change its process group or session itself, you'll lose it; read execve(2) carefully and several times!). Notice that kill(2) with a negative pid kills an entire process group.

Read a good book like Advanced Linux Programming . Consider also using chroot(2) .

And explain what and why you really want to do. sandboxing is harder that what you think. See also capabilities(7) , credentials(7) and SElinux .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM