Home > database >  How to p/invoke getpwnam() from libc in C#?
How to p/invoke getpwnam() from libc in C#?

Time:10-29

Let's start with documentation: https://man7.org/linux/man-pages/man3/getpwnam.3.html

Having this, I made a following C# code:

using System;
using System.Runtime.InteropServices;

if (args.Length < 1) {
    Console.Error.WriteLine("Provide user name.");
    Environment.Exit(-1);
}

var name = args[0];

if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) {
    Syscall.getpwnam(name, out var passwd);
    Console.WriteLine($"User = {name}, UID = {passwd.Uid}, GID = {passwd.Gid}");
    passwd = GetPasswd(name);
    Console.WriteLine($"User = {name}, UID = {passwd.Uid}, GID = {passwd.Gid}");
}
else {
    Console.WriteLine("It supposed to be run on Linux.");
}

static Passwd GetPasswd(string name) {
    var bufsize = 16384;
    var buf = new byte[bufsize];
    var passwd = new Passwd();
    Syscall.getpwnam_r(name, passwd, buf, (uint)bufsize, out var result);
    return result;
}

public struct Passwd {
    public string Name;
    public string Password;
    public uint Uid;
    public uint Gid;
    public string Gecos;
    public string Directory;
    public string Shell;
}

static class Syscall {

    [DllImport("libc", SetLastError = true)]
    public static extern void getpwnam(string name, out Passwd passwd);

    [DllImport("libc", SetLastError = true)]
    public static extern void getpwnam_r(string name, Passwd passwd, byte[] buf, uint bufsize, out Passwd result);

}

It doesn't work.

Here's what I get:

User = service, UID = 0, GID = 0
Segmentation fault (core dumped)

What am I doing wrong?

How should I call it to get the actual structure? I'm not interested in returned strings. All I care of are Uid and Gid values.

CodePudding user response:

As linked documentation mentions - this function accepts one parameter - name, and returns pointer to structure with data. So signature should be:

[DllImport("libc", SetLastError = true)]
public static extern IntPtr getpwnam(string name);

And then:

// we have pointer here
var passwdPtr = Syscall.getpwnam(name);
// don't forget to check if pointer is not IntPtr.Zero.
// interpret data at pointer as structure
var passwd = Marshal.PtrToStructure<Passwd>(passwdPtr);
Console.WriteLine($"User = {passwd.Name}, UID = {passwd.Uid}, GID = {passwd.Gid}");
  • Related