Skip to content

closes bpo-38712: Add signal.pidfd_send_signal. #17070

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Doc/library/signal.rst
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,19 @@ The :mod:`signal` module defines the following functions:
.. versionadded:: 3.8


.. function:: pidfd_send_signal(pidfd, sig, siginfo=None, flags=0)

Send signal *sig* to the process referred to by file descriptor *pidfd*.
Python does not currently support the *siginfo* parameter; it must be
``None``. The *flags* argument is provided for future extensions; no flag
values are currently defined.

See the :manpage:`pidfd_send_signal(2)` man page for more information.

.. availability:: Linux 5.1+
.. versionadded:: 3.9


.. function:: pthread_kill(thread_id, signalnum)

Send the signal *signalnum* to the thread *thread_id*, another thread in the
Expand Down
7 changes: 7 additions & 0 deletions Doc/whatsnew/3.9.rst
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,13 @@ now raises :exc:`ImportError` instead of :exc:`ValueError` for invalid relative
import attempts.
(Contributed by Ngalim Siregar in :issue:`37444`.)

signal
------

Exposed the Linux-specific :func:`signal.pidfd_send_signal` for sending to
signals to a process using a file descriptor instead of a pid. (:issue:`38712`)


Optimizations
=============

Expand Down
19 changes: 19 additions & 0 deletions Lib/test/test_signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -1273,6 +1273,25 @@ def handler(a, b):
self.assertTrue(is_ok)


class PidfdSignalTest(unittest.TestCase):

@unittest.skipUnless(
hasattr(signal, "pidfd_send_signal"),
"pidfd support not built in",
)
def test_pidfd_send_signal(self):
with self.assertRaises(OSError) as cm:
signal.pidfd_send_signal(0, signal.SIGINT)
if cm.exception.errno == errno.ENOSYS:
self.skipTest("kernel does not support pidfds")
self.assertEqual(cm.exception.errno, errno.EBADF)
my_pidfd = os.open(f'/proc/{os.getpid()}', os.O_DIRECTORY)
self.addCleanup(os.close, my_pidfd)
with self.assertRaisesRegexp(TypeError, "^siginfo must be None$"):
signal.pidfd_send_signal(my_pidfd, signal.SIGINT, object(), 0)
with self.assertRaises(KeyboardInterrupt):
signal.pidfd_send_signal(my_pidfd, signal.SIGINT)

def tearDownModule():
support.reap_children()

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Add the Linux-specific :func:`signal.pidfd_send_signal` function, which
allows sending a signal to a process identified by a file descriptor rather
than a pid.
76 changes: 75 additions & 1 deletion Modules/clinic/signalmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions Modules/signalmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
#ifdef HAVE_SIGNAL_H
#include <signal.h>
#endif
#ifdef HAVE_SYS_SYSCALL_H
#include <sys/syscall.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
Expand Down Expand Up @@ -1250,6 +1253,38 @@ signal_pthread_kill_impl(PyObject *module, unsigned long thread_id,
#endif /* #if defined(HAVE_PTHREAD_KILL) */


#if defined(__linux__) && defined(__NR_pidfd_send_signal)
/*[clinic input]
signal.pidfd_send_signal

pidfd: int
signalnum: int
siginfo: object = None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO, we already have a SiginfoType for siginfo.
Is there any reason not to check the type for it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now this parameter is required to be None.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@benjaminp Got it :)

flags: int = 0
/

Send a signal to a process referred to by a pid file descriptor.
[clinic start generated code]*/

static PyObject *
signal_pidfd_send_signal_impl(PyObject *module, int pidfd, int signalnum,
PyObject *siginfo, int flags)
/*[clinic end generated code: output=2d59f04a75d9cbdf input=2a6543a1f4ac2000]*/

{
if (siginfo != Py_None) {
PyErr_SetString(PyExc_TypeError, "siginfo must be None");
return NULL;
}
if (syscall(__NR_pidfd_send_signal, pidfd, signalnum, NULL, flags) < 0) {
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}
Py_RETURN_NONE;
}
#endif



/* List of functions defined in the module -- some of the methoddefs are
defined to nothing if the corresponding C function is not available. */
Expand All @@ -1265,6 +1300,7 @@ static PyMethodDef signal_methods[] = {
{"set_wakeup_fd", (PyCFunction)(void(*)(void))signal_set_wakeup_fd, METH_VARARGS | METH_KEYWORDS, set_wakeup_fd_doc},
SIGNAL_SIGINTERRUPT_METHODDEF
SIGNAL_PAUSE_METHODDEF
SIGNAL_PIDFD_SEND_SIGNAL_METHODDEF
SIGNAL_PTHREAD_KILL_METHODDEF
SIGNAL_PTHREAD_SIGMASK_METHODDEF
SIGNAL_SIGPENDING_METHODDEF
Expand Down