Using .NET PInvoke for Linux system functions

Using .NET PInvoke for Linux system functions

If you’ve developed Windows applications with .NET, you may have found yourself in a situation where the framework did not provide the APIs you needed. When that happens, you first need to identify the system APIs and then make them available using PInvoke. A website like pinvoke.net provides copy-and-pasteable code snippets for many Win32 API functions.

.NET Platform Invoke (PInvoke) makes it easy to consume native libraries. In this article, we’ll take a look at using PInvoke for Linux system functions.

PInvoking Linux

In case you are not familiar with PInvoke, let’s look at a simple example:

[DllImport("mylibrary")]
public static extern int foo();

This makes available the function foo from the native library mylibrary. This function accepts no arguments and returns an int. .NET takes care of marshaling the argument types. It is possible to use managed types (like strings) in these signatures, which will be automagically marshaled.

PInvoke works at the application binary interface (ABI) level: It respects the binary calling conventions of the platform on which it is running.

Linux is a UNIX flavor operating system. It provides many of the APIs prescribed by POSIX and SUS; however, these APIs are defined using the C programming language. They are not APIs at the binary level (ABIs).

For example, the standards describe that there should be positive values for indicating certain types of errors. These values are named (for example, EAGAIN), but their values are not defined. This means different platforms can (and will) have different values for these names. This makes it impossible to add a const int to a C# file that has the correct value for all platforms.

Another example is struct, which is used in the API to define a number of fields; however, the order is not fixed, and platforms are allowed to add extra fields. So again, we can’t describe a struct in C# that matches the native struct on all platforms.

The generic way to solve this is to introduce a native shim library. This library provides its own set of functions, constants, and structs that are platform-independent with a fixed ABI. This API gets PInvoked by .NET. Within the library, this API calls the native functions.

If you want to PInvoke the system C library without using a shim, you can use libc as the library name. The .NET Core runtime maps this to the system C library. For example, we can expose the kill function as follows:

[DllImport("libc", SetLastError = true))]
public static extern int kill(int pid, int sig);

SetLastError means the function uses errno to indicate what went wrong. Marshal.GetLastWin32Error can be used to retrieve errno.

Everything you need to grow your career.

With your free Red Hat Developer program membership, unlock our library of cheat sheets and ebooks on next-generation application development.

SIGN UP

Mono.Posix.NETStandard

The Mono.Posix.NETStandard package provides system functions. It uses native shims to work across a number of platforms. If we download the package from nuget.org, we can take a look inside:

$ unzip -l mono.posix.netstandard.1.0.0.nupkg
Archive:  mono.posix.netstandard.1.0.0.nupkg
  Length  	Date	Time	Name
---------  ---------- -----   ----
  	516  02-28-2018 15:18   _rels/.rels
  	814  02-28-2018 15:18   Mono.Posix.NETStandard.nuspec
	13376  02-28-2018 15:18   lib/net40/Mono.Posix.NETStandard.dll
	13376  02-28-2018 15:18   ref/net40/Mono.Posix.NETStandard.dll
   189504  02-28-2018 15:18   ref/netstandard2.0/Mono.Posix.NETStandard.dll
   189504  02-28-2018 15:18   runtimes/linux-arm/lib/netstandard2.0/Mono.Posix.NETStandard.dll
   183240  02-28-2018 15:18   runtimes/linux-arm/native/libMonoPosixHelper.so
   189504  02-28-2018 15:18   runtimes/linux-arm64/lib/netstandard2.0/Mono.Posix.NETStandard.dll
   246736  02-28-2018 15:18   runtimes/linux-arm64/native/libMonoPosixHelper.so
   189504  02-28-2018 15:18   runtimes/linux-armel/lib/netstandard2.0/Mono.Posix.NETStandard.dll
   236480  02-28-2018 15:18   runtimes/linux-armel/native/libMonoPosixHelper.so
   189504  02-28-2018 15:18   runtimes/linux-x64/lib/netstandard2.0/Mono.Posix.NETStandard.dll
   920882  02-28-2018 15:18   runtimes/linux-x64/native/libMonoPosixHelper.so
   189504  02-28-2018 15:18   runtimes/linux-x86/lib/netstandard2.0/Mono.Posix.NETStandard.dll
   766620  02-28-2018 15:18   runtimes/linux-x86/native/libMonoPosixHelper.so
   189504  02-28-2018 15:18   runtimes/osx/lib/netstandard2.0/Mono.Posix.NETStandard.dll
   535888  02-28-2018 15:18   runtimes/osx/native/libMonoPosixHelper.dylib
   189504  02-28-2018 15:18   runtimes/win-x64/lib/netstandard2.0/Mono.Posix.NETStandard.dll
  1495800  02-28-2018 15:18   runtimes/win-x64/native/libMonoPosixHelper.dll
	87600  02-28-2018 15:18   runtimes/win-x64/native/MonoPosixHelper.dll
   189504  02-28-2018 15:18   runtimes/win-x86/lib/netstandard2.0/Mono.Posix.NETStandard.dll
  1256296  02-28-2018 15:18   runtimes/win-x86/native/libMonoPosixHelper.dll
   400120  02-28-2018 15:18   runtimes/win-x86/native/MonoPosixHelper.dll
  	592  02-28-2018 15:18   [Content_Types].xml
  	692  02-28-2018 15:18   package/services/metadata/core-properties/4b5c471cadba4490965015071f90289f.psmdcp
 	9475  10-11-2018 20:23   .signature.p7s
---------                 	-------
  7874039                 	26 files

The libMonoPosixHelper.* files are the native shim libraries. The directories under runtimes are the supported platforms. There is Linux support for arm, arm64, armel, x64 and x86. The package also provides support for Windows and OS X. The Linux libraries are for flavors of Linux that are based on glibc (GNU C library), such as Fedora and Red Hat Enterprise Linux (RHEL). Some things won’t work on flavors of Linux that are based on musl, such as Alpine. .NET Core applications using this package will pick the proper variant under the runtimes folder based on the platform on which they are running. This is based on the runtime identifier (RID) graph.

Tmds.LibC

A while back, I blogged about a Linux-specific transport for Kestrel. This library uses Linux-specific APIs that are not provided by .NET Core and Mono.Posix.NETStandard. So far, the library has used a native shim to make those functions accessible. A compiled version for glibc x64 is in the NuGet package. This means the Transport doesn’t work on other architectures or with other C libraries.

As explained before, one way to solve this is to recompile the shim for different platforms. While that is easy to do in theory, in practice, there is quite a bit of work involved. On each platform, the native libraries need to be built. Then those libraries need to be collected in a single NuGet package. I wanted to try a different approach: a fully managed implementation.

The managed implementation captures the platform ABIs in managed code. For each platform, we build an assembly that matches that platform’s ABI. The differences between platforms are small, so we can share a lot of code between them.

A nice effect of capturing the platform ABIs is that our .NET API can be very close to the C API. We don’t need to introduce shim structs or shim constants.

For example, the setsockopt option for changing a socket option has this C API:

int setsockopt(int sockfd, int level, int optname, const void *optval, socklen_t optlen);

The C# API looks like this:

public static int setsockopt(int socket, int level, int optname, void* optval, socklen_t optlen);

As you can see, the signatures are virtually the same.

Because these system functions may be useful for other developers, I’ve collected them in a separate library, named Tmds.LibC. You can find the source code on GitHub and packages are published to NuGet.org. The Kestrel Linux Transport has been updated to use this library. The Transport does not need to deal with platform specifics, and it now runs on arm32, arm64, and x64.

Conclusion

In this article, we looked at .NET PInvoke and how PInvoke differs on Linux compared to Windows. We then explored how Mono.Posix.NETStandard provides a set of POSIX functions on various platforms using native shims. Finally, we took a look at Tmds.LibC, which provides Linux system functions for a number architectures using a fully managed implementation.

Take advantage of your Red Hat Developers membership and download Red Hat Enterprise Linux today at no cost.

Share