WinForms / console application in Mono, how to find out that it runs as root

How can we execute such executables in two ways, such as "sudo mono test.exe" and "mono test.exe".

Now I want to know how to determine if this application is running as root inside the application itself.

I tried to check the username as shown below and see if they match "root",

Thread.CurrentPrincipal.Identity.Name

Process.GetCurrentProcess (). StartInfo.UserName

AppDomain.CurrentDomain.ApplicationIdentity.FullName

The first two are always empty lines, and the third is a NullReferenceException.

Please advise if this is workable on Mono 2.6.

+3
source share
1

- DllImport libc getuid(). root, getuid() 0; , UID:

using System.Runtime.InteropServices;

public class Program
{
    [DllImport ("libc")]
    public static extern uint getuid ();

    public static void Main()
    {
        if (getuid() == 0) {
            System.Console.WriteLine("I'm running as root!");
        } else {
            System.Console.WriteLine("Not root...");
        }
    }
}

Mono 2.6.

EDIT: , getuid() Mono.Unix.Native.Syscall Mono.Posix:

using Mono.Unix.Native;

public class Program
{
    public static void Main()
    {
        if (Syscall.getuid() == 0) {
            System.Console.WriteLine("I'm running as root!");
        } else {
            System.Console.WriteLine("Not root...");
        }
    }
}

, . , , UID - , ; , root, root.

+2

Source: https://habr.com/ru/post/1740623/


All Articles