P /调用:致命错误。无效的程序:尝试从运行时类型安全代码中调用NativeCallable方法

问题描述 投票:-1回答:1

我正在尝试使用libstd的sigaction在Linux系统(Ubuntu)的后台任务中捕获致命信号。这是我的Program类和DllImport声明:

using System;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;

namespace Signal
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            var pid = LibStd.GetPid();
            Console.WriteLine($"PID: {pid}");

            var sa = new Sigaction()
            {
                Handler = SignalHandler
            };

            LibStd.SigEmptySet(sa.Mask);
            var ptr = Marshal.AllocHGlobal(Marshal.SizeOf(sa));
            Marshal.StructureToPtr(sa, ptr, true);

            _ = Task.Run(async() =>
            {
                while (true)
                {
                    // Check SIGUSR1
                    if (LibStd.Sigaction(10, ptr, IntPtr.Zero) == -1)
                    {
                        // error
                    }

                    await Task.Delay(1);
                }
            });

            while (true)
            {
                // Do something
            }
        }

        static LibStd.SignalHandler SignalHandler = new LibStd.SignalHandler(Handler);

        static void Handler(int sig)
        {
            Console.WriteLine($"RECEIVED SIGNAL {sig}");
        }
    }

    [StructLayout(LayoutKind.Sequential)]
    internal struct Sigaction
    {
        public LibStd.SignalHandler Handler;
        public IntPtr Mask;
        public int Flags;
    }

    internal static class LibStd
    {
        private const string LibName = "c";

        [DllImport(LibName, EntryPoint = "getpid")]
        internal static extern int GetPid();

        [DllImport(LibName, EntryPoint = "sigaction", CallingConvention = CallingConvention.Cdecl)]
        internal static extern int Sigaction(int sig, IntPtr act, IntPtr oldact);

        [DllImport(LibName, EntryPoint = "sigemptyset", CallingConvention = CallingConvention.Cdecl)]
        internal static extern int SigEmptySet(IntPtr mask);

        [UnmanagedFunctionPointer(CallingConvention.Cdecl)]
        internal delegate void SignalHandler(int t);
    }
}

我需要在后台检查信号,因为我将来想将此代码放入桌面GUI应用程序中,因此信号处理不应阻塞UI线程。但是,当我运行该程序时,然后调用kill -SIGUSR1 <pid>,它将在以下错误输出到控制台:致命错误。无效的程序:尝试从运行时类型安全的代码中调用NativeCallable方法。如果我将信号处理放入主while (true) { }循环中,它将按预期工作。如果有人可以帮助我修复它,那就太好了。谢谢

c# linux signals pinvoke
1个回答
1
投票

对于Ubuntu 18,我发现需要的结构是

[StructLayout(LayoutKind.Sequential)]
internal struct Sigaction
{
    public LibStd.SignalHandler Handler;
    public IntPtr Mask;
    public int Flags;
    public LibStd.SignalHandler sa_restorer;
}

考虑不启动用于信号处理的线程

static private void ListenToSignal()
{
    var sa = new Sigaction()
    {
        Handler = SignalHandler
    };

    LibStd.SigEmptySet(sa.Mask);
    var ptr = Marshal.AllocHGlobal(Marshal.SizeOf(sa));
    Marshal.StructureToPtr(sa, ptr, true);

    if (LibStd.Sigaction(10, ptr, IntPtr.Zero) == -1)
    {
        Console.WriteLine("Sigaction -1");
    }
    Console.WriteLine("Sigaction done");        
}

这将在主线程上设置处理。当信号到达时,它也会被处理。无需继续轮询Sigaction

© www.soinside.com 2019 - 2024. All rights reserved.