3

Is there a way to change PID, PPID, SID of a running process? It would make sense for the answer to be no, but I'd like to make sure.

3

A process can set its own PGID and SID with the system calls setpgid setsid. The target group/session can't be chosen arbitrarily: setpgid can only move to another process group in the same session, or create a new process group whose PGID is equal to the PID; setsid can only move the process to its own session, making the SID equal to the PID.

These calls are reserved to the process itself: a process cannot change another process's PGID or SID, with one exception: a process can change its children's PGID if they're still running the original process image (i.e. they haven't called execve to run a different program).

Some systems may allow other behaviors but I don't think any modern Unix system deviates fundamentally.

It is possible to indirectly change a process's PGID or SID by using a debugger to make the process call the setpgid or setsid system call (via ptrace). Since this requires ptrace permission, it must be done from another process running as root or as the same user, and there must not be any restriction on debugging (many modern Linux system require the debugger to be an ancestor of the debuggee).

A process's PID never changes. A process's PPID can only change once, and only for one reason: when the parent dies, the PPID changes from the parent's PID to 1 (the process is adopted by init).

Note that in some systems, a process can have different PID values (and consequently also PPID/PGID/SID since they all start as the PID of some process) depending on how you look at it. For example, with Linux namespaces, each process has a potentially different PID in each namespace where it's visible.

  • So using setpgid and setsid, the process can continually change its PGID and SID (albeit with the restrictions you outlined)? – extremeaxe5 Aug 12 '18 at 23:27
  • There is a typo; I think you meant PGID in the first line. I'll remove this comment once you've changed it. – extremeaxe5 Aug 12 '18 at 23:27
  • @extremeaxe5 PGID: yes. SID: no, since the only possible change is to the PID, and that doesn't change. – Gilles Aug 12 '18 at 23:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.