diff options
author | Giampaolo Rodola <g.rodola@gmail.com> | 2015-06-20 15:26:09 +0200 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2015-06-20 15:26:09 +0200 |
commit | 5ae30c7989e30702b457c2cae69e6b7e4a8b96ed (patch) | |
tree | 14bc6993a793b19623bddfa63b341daf9239665d | |
parent | 0d6368d61c24402b567134aff1e9b067aed8bb07 (diff) | |
download | psutil-5ae30c7989e30702b457c2cae69e6b7e4a8b96ed.tar.gz |
UNIX: prevent sending signals to PID 0
-rw-r--r-- | HISTORY.rst | 3 | ||||
-rw-r--r-- | psutil/__init__.py | 10 | ||||
-rw-r--r-- | test/test_psutil.py | 4 |
3 files changed, 13 insertions, 4 deletions
diff --git a/HISTORY.rst b/HISTORY.rst index 9b86c6e8..18aa047c 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -11,6 +11,9 @@ Bug tracker at https://github.com/giampaolo/psutil/issues < 3.4. - #636: [Linux] *connections functions may swallow errors and return an incomplete list of connnections. +- #637: [UNIX] raise exception if trying to send signal to Process PID 0 as it + will affect os.getpid()'s process group instead of PID 0. + 3.0.0 - 2015-06-13 ================== diff --git a/psutil/__init__.py b/psutil/__init__.py index 16314476..7d47a14f 100644 --- a/psutil/__init__.py +++ b/psutil/__init__.py @@ -1007,10 +1007,12 @@ class Process(object): if _POSIX: def _send_signal(self, sig): - # XXX: according to "man 2 kill" PID 0 has a special - # meaning as it refers to <<every process in the process - # group of the calling process>>, so should we prevent - # it here? + if self.pid == 0: + # see "man 2 kill" + raise ValueError( + "preventing sending signal to process with PID 0 as it " + "will affect every process in the process group of the " + "calling process (os.getpid()) instead of PID 0") try: os.kill(self.pid, sig) except OSError as err: diff --git a/test/test_psutil.py b/test/test_psutil.py index a7213f4f..31655b72 100644 --- a/test/test_psutil.py +++ b/test/test_psutil.py @@ -2233,6 +2233,10 @@ class TestProcess(unittest.TestCase): except psutil.AccessDenied: pass + self.assertRaisesRegexp( + ValueError, "preventing sending signal to process with PID 0", + p.send_signal(signal.SIGTERM)) + self.assertIn(p.ppid(), (0, 1)) # self.assertEqual(p.exe(), "") p.cmdline() |