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?