Tuesday 5 September 2017

linux - Why does sudo -n on Mac OS X always return 0?


It looks like sudo -n on Mac OS X (10.10.3) always returns 0 as a return code.


So something like this will always show 0, regardless of whether the user has authenticated with his password first:


sudo -n true &> /dev/null ; echo $?

In the case that the user does not have a valid sudo session, I would expect to see 1 as an exit code, but on OS X, it always returns 0.


I'm asking since I've seen this pattern suggested to check for sudo access, e.g. here: https://superuser.com/a/587876/119764


What's the official (???) behavior of sudo -n? Does it differ by version or OS, or am I missing something?



Answer



I have the same issue. Referring to the man page,



Otherwise, sudo quits with an exit value of 1 if there is a configuration/permission problem or if sudo cannot execute the given command.  In the latter case the error string is printed to stderr.  If sudo cannot stat(2) one or more entries in the user's PATH an error is printed on stderr.  (If the directory does not exist or if it is not really a directory, the entry is ignored and no error is printed.)  This should not happen under normal circumstances.  The most common reason for stat(2) to return "permission denied" is if you are running an automounter and one of the directories in your PATH is on a machine that is currently unreachable.



So it seems to be a bug in OS X.


No comments:

Post a Comment

Where does Skype save my contact's avatars in Linux?

I'm using Skype on Linux. Where can I find images cached by skype of my contact's avatars? Answer I wanted to get those Skype avat...