Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Current User from root mode?

I am writing a login item and I am trying to find if its possible to get the current user.

Lets say, I have logged in with user "Test" and when I execute the exe with root privileges and if I use

char *user  = getenv("USER");

user is "root".

My expected answer is "Test".

How can I get it?

I don't know if I can put objective c code in login item? Is it possible to NSUserName in login item.

NSString *user = NSUserName();
like image 1000
RLT Avatar asked Jul 14 '26 23:07

RLT


2 Answers

You want the SCDynamicStoreCopyConsoleUser function.

QA1133 gives some relevant details and caveats.

like image 98
Peter Hosey Avatar answered Jul 16 '26 14:07

Peter Hosey


Login items can be Cocoa applications, so you can use NSUserName() (which is in Foundation by the way).

Maybe also look at getuid() / geteuid()

like image 36
JeremyP Avatar answered Jul 16 '26 13:07

JeremyP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!