'sigaction not working when used with ctypes

I'm trying to adapt sigaction to be used with ctypes:

import signal
import ctypes as c
import os

libc = c.cdll.LoadLibrary("libc.so.6")

class sig_value(c.Union):
    _fields_ = [
        ("sival_int", c.c_int),
        ("sival_ptr", c.c_void_p)
    ]

class siginfo_t(c.Structure):
    _fields_ = [
        ("si_signo", c.c_int),
        ("si_code", c.c_int),
        ("si_value", sig_value),
        ("si_errno", c.c_int),
        ("si_pid", c.c_int),
        ("si_uid", c.c_int),
        ("si_addr", c.c_void_p),
        ("si_status", c.c_int),
        ("si_band", c.c_int),
    ]

handler_c_type = c.CFUNCTYPE(None, c.c_int, c.POINTER(siginfo_t), c.c_void_p)

class sigaction_t(c.Structure):
    _fields_ = [
        ("sa_handler", c.c_void_p),
        ("sa_sigaction", handler_c_type),
        ("sa_mask", c.c_int),
        ("sa_flags", c.c_int),
        ("sa_restorer", c.c_void_p),
    ]

libc.sigaction.argtypes = c.c_int,c.POINTER(sigaction_t),c.POINTER(sigaction_t)
libc.sigemptyset.argtypes = c.POINTER(c.c_int),

def py_handler(sig, siginfo, ucontext):
    print(sig, siginfo, ucontext)

handler = handler_c_type(py_handler)

sa = sigaction_t()
sa_mask = c.c_int(0)
sa.sa_mask = sa_mask
libc.sigemptyset(c.byref(sa_mask))
sa.sa_sigaction = handler
sa.sa_flags = 0x4 #SA_SIGINFO
libc.sigaction(c.c_int(signal.SIGUSR2), c.byref(sa), None)

In this code, I'm just setting a handler to receive SIGUSR2 signal with a value issued by sigqueue. Even though this code is not crashing, python's default handler for SIGUSR2 is the one that's being called. So when I receive the signal, the script exits. What could I be missing here?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source