Ignore linux process signals such as SIGTERM using sigaction from .NET app

85 views Asked by At

I am not really familiar with .NET interop. Am trying to implement the ignoring of linux process signals in .NET, using a call to sigaction.

The interop definitions I could come up with are as follows:

    public delegate void __sighandler_t(int signal);
    public delegate void sa_restorer();

    [StructLayout(LayoutKind.Sequential)]
    public struct __sigset_t
    {
        ulong[] __val; // 1024 / (8 * 8)
    }

    [StructLayout(LayoutKind.Sequential)]
    public struct sigaction
  {
    public __sighandler_t sa_handler;
    /* Additional set of signals to be blocked.  */
    public __sigset_t sa_mask;
    /* Special flags.  */
    public int sa_flags;
    /* Restore handler.  */
    public sa_restorer sa_restorer;
  };

In attempting to write a sample implementation of ignoring a SIGTERM, I try

var returnCode = NetlinkDemo.sigaction(15, IntPtr.Zero, IntPtr.Zero);
System.Console.WriteLine($"sigaction 1 returned [{returnCode}]");

sigaction act = new sigaction();
act.sa_flags = 0;
act.sa_handler = HandleSignal;

IntPtr actPtr = Marshal.AllocHGlobal(Marshal.SizeOf<sigaction>());
Marshal.StructureToPtr<sigaction>(act, actPtr, true);

returnCode = NetlinkDemo.sigaction(15, actPtr, IntPtr.Zero);
System.Console.WriteLine($"sigaction 2 returned [{returnCode}]");

private static void HandleSignal(int signal)
{
    System.Console.WriteLine($"Handling signal [{signal}]");
}

This is throwing an error on Marshal.AllocHGlobal: System.ArgumentException: Type 'myProject.sigaction' cannot be marshaled as an unmanaged structure; no meaningful size or offset can be computed.

Are my interop definitions wrong ? Am I doing something that does not make sense ?

0

There are 0 answers