Sunday, October 9, 2022

[SOLVED] For Linux, how can I kill all processes in a session (with same SID) using system calls?

Issue

I am trying to figure how to kill all processses in a session (with the same SID) using system calls with C. I am not interested in to just kill all with a specific PGID since not all processes I am interested about does not have the same PGID, but they have the same SID.

My research have only found this, there Graeme made an excellent answer for scripts: https://unix.stackexchange.com/questions/124127/kill-all-descendant-processes

I would be pleased to get an answer for how it would be possible to kill all direct descendant children and even more pleased how I could kill all children within the session.

Or is what I am asking possible? I am not intrested in a solution there I am simply listing the PIDs of the parents descendant.


Solution

You can always use the /proc/ file system to query processes (see proc(5) for more). In particular you can then scan the /proc/PID/ directories (where PID is some numerical name like 1234, it is the relevant pid, so process of pid 1234 is described in /proc/1234/ pseudo-directory; hence you could readdir the /proc/ directory and find every numerical name inside it) and check which processes have a defined parent pid. You'll read sequentially pseudo-files like /proc/1234/status (and its PPid: line). See also this answer and that one.



Answered By - Basile Starynkevitch
Answer Checked By - Willingham (WPSolving Volunteer)