I am trying to look at the source code of the passwd and gpasswd programs. These tools check the effective UID set to zero to complete execution successfully. But I am a noob to C and couldn't find the line of code that checks if the process is being run by UID == 0. I would appreciate any hints or solutions.


1 Answer 1


If you’re referring to the “shadow-utils” implementations of passwd, you’ll find the test here:

     * The program behaves differently when executed by root than when
     * executed by a normal user.
    amroot = (getuid () == 0);

In gpasswd, the test is defined in two phases; first the UID is stored:

     * Make a note of whether or not this command was invoked by root.
     * This will be used to bypass certain checks later on. Also, set
     * the real user ID to match the effective user ID. This will
     * prevent the invoker from issuing signals which would interfere
     * with this command.
    bywho = getuid ();

then that’s used in the amroot macro:

/* Indicate if gpasswd was called by root */
#define amroot  (0 == bywho)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.