1

In Linux, I am logged in as bj:bj as non root user:group.

bj@pc-bj:~$ id
uid=1000(bj) gid=1000(bj) groups=1000(bj),5(tty),20(dialout),24(cdrom),25(floppy),29(audio),1006(internetallowed)

I want to temporary change my primary group to another group "internetallowed" to which I belong. In bash I can do this successfully by the newgrp command:

bj@pc-bj:~$ newgrp internetallowed
bj@pc-bj:~$ id
uid=1000(bj) gid=1006(internetallowed) groups=1006(internetallowed),5(tty),20(dialout),24(cdrom),25(floppy),29(audio),1000(bj)

Now I want to do this also in C# / DotNet 7.0. I've found out that newgrp uses the setgid() function from libc. So I tried this:

public static class LinuxAPI
{
    [DllImport ("libc", SetLastError = true)]
    public static extern int setgid (UInt32 gid);
    public static int GetLastError()
    {
        return Marshal.GetLastSystemError();
    }
}

static void Main(string[] args)
{
    var result = LinuxAPI.setgid(1006);  // returns -1 (error)
    var error = LinuxAPI.GetLastError();  // returns 1 (Operation not permitted)
}

setgid() fails with errno 1 (Operation not permitted). What am I doing wrong?

1 Answer 1

0

Try replacing

public static int GetLastError()
{
    return Marshal.GetLastSystemError();
}

with

public static int GetLastError()
{
     return Marshal.GetLastWin32Error();
}
Sign up to request clarification or add additional context in comments.

7 Comments

On Linux systems GetLastSystemError() should be used, according to the .net manuals. Even if i use GetLastWin32Error this won't fix the error.
Which version of the .net framework are you using?
using .NET 7.0.403
My issue is not with GetLastError. The function setgid() fails as it returns -1. GetLastError is just to have a clue.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.