我是root时,如何获取登录用户的用户名或主目录? [英] How do I get user name or home dir of the logged-in user when I'm root?

查看:490
本文介绍了我是root时,如何获取登录用户的用户名或主目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序以root用户身份运行,但是是从常规(Admin)用户启动的,例如使用sudo命令(但不一定,例如,我不能简单地检查env var SUDO_USER).

My app runs as the root user, but was launched from the regular (Admin) user, e.g. using the sudo command (but not necessarily, i.e. I cannot simply check the env var SUDO_USER, for instance).

如何确定登录到用户界面会话中的用户名或其主目录?

How can I determine the user name or its homedir that is logged in to the User Interface session?

是否存在可以为我提供该信息的Cocoa,Foundation或CoreFoundation函数?

Is there a Cocoa, Foundation or CoreFoundation function that can give me that information?

推荐答案

即使对于以root用户运行的应用程序,"HOME"环境变量似乎仍设置为登录用户.因此,以下是获取该用户的主文件夹的方法:

It appears the "HOME" environment variable remains set to the logged-in user, even for apps running under the root user. So, here's how to get the home folder for that user:

NSString *homeDir = NSProcessInfo.processInfo.environment[@"HOME"];

这篇关于我是root时,如何获取登录用户的用户名或主目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆