Search code examples
cunixpid

When accepting PIDs as input, is there any sane validation that can take place?


I have a piece of code that accepts the PID of a process to perform an operation upon.

Aside from performing any syscalls to validate the process (something that occurs later on) is there something I can do to assert a process ID is sane? e.g. I will never accept 0 since that doesn't make sense for the application.

Are there any concrete assertions/properties that can be utilized to do some naive sanity checking on PID values?


Solution

  • If you're on Linux, you can try doing a access("/proc/$PID/"). Or more generally, you can do a kill(pid, 0) as explained in this answer to see if the process exists.

    Of course, whatever you do, a syscall will be involved