1

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?

Qix - MONICA WAS MISTREATED
  • 14,451
  • 16
  • 82
  • 145
  • 1
    Check that it's not negative as well or course? And if you're on a Linux system check that it isn't bigger than the value in `/proc/sys/kernel/pid_max` (could possibly be retrieved with a [`sysctl`](http://man7.org/linux/man-pages/man2/sysctl.2.html) call) perhaps? – Some programmer dude Feb 28 '17 at 08:48
  • 2
    [Max user processes](http://stackoverflow.com/questions/9361816/maximum-number-of-processes-in-linux). @Someprogrammerdude: Have a related question. If the max number of processes is say 50,000 then does it means `0<=pid<=49999` or any `number >= 0` can be pid (ofcourse number re-presentable by 32 bits) – sameerkn Feb 28 '17 at 08:54
  • 1
    The range is correct (`0 <= pid <= 49999` in your example). – Some programmer dude Feb 28 '17 at 08:57

3 Answers3

3

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

Community
  • 1
  • 1
silen
  • 524
  • 6
  • 5
1

Try the kill() function, with a signal of zero. Here's a snippet from the man page for kill() on Ubuntu:

int kill(pid_t pid, int sig);

If sig is 0, then no signal is sent, but error checking is still performed; this can be used to check for the existence of a process ID or process group ID.

Toby Speight
  • 27,591
  • 48
  • 66
  • 103
TonyB
  • 927
  • 6
  • 13
0

Why? Stop second guessing users. The kernel will do all sanity checking for you. That is the most valid input validation for a PID - is the operation I'm trying to do successful. 0 is a valid number for a PID in certain situations. So are negative numbers.

If your code runs as a privileged process and you're trying to limit the damage it can do, then you'd need some serious validation rather than "sanity checking". But if it doesn't then there no reason for you to do anything. "No such process" is a good enough error message.

Art
  • 19,807
  • 1
  • 34
  • 60
  • 1
    Because the operation I'm performing with the PID is lengthy and expensive and I'd rather fail fast on stupid input than let the system error out because some query of currently running processes failed. There _are_ reasons for validating input even if the system is going to do a similar check. It's a **sanity** check for a reason. It's basically an assertion during development, debugging, etc. – Qix - MONICA WAS MISTREATED Feb 28 '17 at 09:00
  • @Qix You call it a sanity check. I call it: the reason why almost no operating system today can have more than 99999 processes because input validation from the 70s and 80s is still polluting code today. I've worked on changing the valid PID range in a kernel and while we could reasonably fix things that broke above 30k (not 2^15, but actually 30k) there was too much stuff to fix that broke if it we bumped the number into a 6 digit range. – Art Feb 28 '17 at 09:22