문제

A strange problem happens using NSHomeDirectory,

NSString *homeDirectory = NSHomeDirectory();
NSLog(@"homeDirectory is: %@",homeDirectory);

As standard user homeDirectory is /User/Paul, but when the app runs as root the value of homeDirectory is now /var/root.

Is this a bug, or maybe is it considered normal when the user is root? How can I always find the location of the Home folder of the "standard users" who initially launched my app and then acquiring root privileges?

도움이 되었습니까?

해결책

It appears that the home directory of the root user is indeed set to /var/root on OSX.

On linux it's usually /root/

I guess you can take the value of the environment variable $SUDO_USER if the application was run as root through sudo and use that to derive the home directory.

[[[NSProcessInfo processInfo] environment] objectForKey:@"SUDO_USER"]

Remember that files created by the application when it's run as root will be owned by user root by default.

Also, maybe you'd like to consider why you are even running this application as root in the first place. Maybe there is a better way to do it.

다른 팁

what you are seeing is because when running as root you actually SWITCH the active user. Rights & (prior and former) Belongings (in this case directories/files) included!

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top