Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top