Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

c# - P/Invoke ioctl system call

I'm developing an application which needs to interact with Video4Linux abstraction. The application is developed in C#, using the mono framework.

The problem I'm facing is that I can't P/Invoke the ioctl system call. Or, more precisely, I can P/Invoke it, but it crashes badly.

The extern declaration is the following:

[DllImport("libc", EntryPoint = "ioctl", SetLastError = true)]
private extern static int KernelIoCtrl(int fd, int request, IntPtr data);

So far so good.

The actual routine that uses the KernelIoCtrl is the following:

protected virtual int Control(IoSpecification request, object data)
{
    GCHandle dataHandle;
    IntPtr dataPointer = IntPtr.Zero;

    try {
        // Pin I/O control data
        if (data != null) {
            dataHandle = GCHandle.Alloc(data, GCHandleType.Pinned);
            dataPointer = dataHandle.AddrOfPinnedObject();
        }
        // Perform I/O control
        int result =  KernelIoCtrl(mFileDescriptor, request.RequestCode, dataPointer);
        int errno = Marshal.GetLastWin32Error();

        // Throw exception on errors
        if (errno != (int)ErrNumber.NoError)
            throw new System.ComponentModel.Win32Exception(errno);

        return (result);
    } finally {
        if (dataPointer != IntPtr.Zero)
            dataHandle.Free();
    }
}

All the above code seems good. The class IoSpecification is used for computing the I/O request code following the header specification (basically it follows the _IOC macro declared at /usr/include/linux/asm/ioctl.h.

The data parameter is a structure, declared as follow:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct Capability
{
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
    public string Driver;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
    public string Device;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
    public string BusInfo;

    public UInt32 Version;

    public CapabilityFlags Capabilities;

    [MarshalAs(UnmanagedType.ByValArray, SizeConst=4)]
    public UInt32[] Reserved;
}

which should mimic the following structure (declared at /usr/include/linux/videodev2.h):

struct v4l2_capability {
    __u8    driver[16];     /* i.e. "bttv" */
    __u8    card[32];       /* i.e. "Hauppauge WinTV" */
    __u8    bus_info[32];   /* "PCI:" + pci_name(pci_dev) */
    __u32   version;        /* should use KERNEL_VERSION() */
    __u32   capabilities;   /* Device capabilities */
    __u32   reserved[4];
};

Before having the crash, there was a problem in the IOCTL request code computation, and the KernelIoCtrl was working as expected (returning an errno equals to EINVAL). When I corrected the bug (and indeed having the correct IOCTRL request code), the call has started to cause a crash.

In conclusion, it seems there's a problem in the structure marshalling, but I cannot see what it's going wrong.

I fear that the problem is the variable argument list, because the ioctl routine is declared as follow (taken from man):

int ioctl(int d, int request, ...);

But I saw lot of code declaring the above routine as int ioctl(int d, int request, void*);, and I can ensure that the specific IOCTRL request takes only one argument.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Example usage:

Capability capability;

if (UnsafeNativeMethods.Ioctl(handle, request, ref capability) == -1)
{
    throw new UnixIOException();
}

Capability:

[StructLayout(LayoutKind.Sequential, Size = 104)]
internal struct Capability
{
    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
    public string Driver;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
    public string Device;

    [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
    public string BusInfo;

    public uint Version;

    public CapabilityFlags Capabilities;
}

UnsafeNativeMethods:

internal static class UnsafeNativeMethods
{
    [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
    [DllImport("libc", EntryPoint = "close", SetLastError = true)]
    internal static extern int Close(IntPtr handle);

    [DllImport("libc", EntryPoint = "ioctl", SetLastError = true)]
    internal static extern int Ioctl(SafeUnixHandle handle, uint request, ref Capability capability);

    [DllImport("libc", EntryPoint = "open", SetLastError = true)]
    internal static extern SafeUnixHandle Open(string path, uint flag, int mode);

    internal static string Strerror(int error)
    {
        try
        {
            var buffer = new StringBuilder(256);
            var result = Strerror(error, buffer, (ulong)buffer.Capacity);
            return (result != -1) ? buffer.ToString() : null;
        }
        catch (EntryPointNotFoundException)
        {
            return null;
        }
    }

    [DllImport("MonoPosixHelper", EntryPoint = "Mono_Posix_Syscall_strerror_r", SetLastError = true)]
    private static extern int Strerror(int error, [Out] StringBuilder buffer, ulong length);
}

SafeUnixHandle:

[SecurityPermission(SecurityAction.InheritanceDemand, UnmanagedCode = true)]
[SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
internal sealed class SafeUnixHandle : SafeHandle
{
    [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
    private SafeUnixHandle()
        : base(new IntPtr(-1), true)
    {
    }

    public override bool IsInvalid
    {
        get { return this.handle == new IntPtr(-1); }
    }

    [ReliabilityContract(Consistency.WillNotCorruptState, Cer.MayFail)]
    protected override bool ReleaseHandle()
    {
        return UnsafeNativeMethods.Close(this.handle) != -1;
    }
}

UnixIOException:

[Serializable]
public class UnixIOException : ExternalException
{
    private readonly int nativeErrorCode;

    [SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
    public UnixIOException()
        : this(Marshal.GetLastWin32Error())
    {
    }

    [SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
    public UnixIOException(int error)
        : this(error, GetErrorMessage(error))
    {
    }

    [SecurityPermission(SecurityAction.Demand, UnmanagedCode = true)]
    public UnixIOException(string message)
        : this(Marshal.GetLastWin32Error(), message)
    {
    }

    public UnixIOException(int error, string message)
        : base(message)
    {
        this.nativeErrorCode = error;
    }

    public UnixIOException(string message, Exception innerException)
        : base(message, innerException)
    {
    }

    protected UnixIOException(SerializationInfo info, StreamingContext context)
        : base(info, context)
    {
        this.nativeErrorCode = info.GetInt32("NativeErrorCode");
    }

    public int NativeErrorCode
    {
        get { return this.nativeErrorCode; }
    }

    public override void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        if (info == null)
        {
            throw new ArgumentNullException("info");
        }

        info.AddValue("NativeErrorCode", this.nativeErrorCode);
        base.GetObjectData(info, context);
    }

    private static string GetErrorMessage(int error)
    {
        var errorDescription = UnsafeNativeMethods.Strerror(error);
        return errorDescription ?? string.Format("Unknown error (0x{0:x})", error);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...