Get Current User from root mode?

北慕城南 提交于 2019-12-21 22:26:02

问题


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();

回答1:


You want the SCDynamicStoreCopyConsoleUser function.

QA1133 gives some relevant details and caveats.




回答2:


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

Maybe also look at getuid() / geteuid()



来源:https://stackoverflow.com/questions/7583338/get-current-user-from-root-mode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!