Subscribe to the RSS Feed

 

getlogin vs getpwuid

getlogin (2):

The getlogin() routine returns the login name of the user associated with the current session, as previously set by setlogin(). The name is normally associated with a login shell at the time a session is created, and is inherited by all processes descended from the login shell. (This is true even if some of those processes assume another user ID, for example when su(1) is used).

getpwuid (3):

The functions getpwnam() and getpwuid() search the password database for the given login name or user uid, respectively, always returning the first one encountered.

One can get the current username using getpwuid with the following code:

struct passwd *tmp;
tmp = getpwuid(getuid());
printf(“username: %s\n”, tmp->pw_name)

Also note getpwuid_r (3) which is the thread safe version of the above.

Why is it better using getpwuid than getlogin? Imagine you are setting up a chrooted/jailed environment. getpwuid will always give back consistent results, while getlogin will tell different credentials if jexec/su/sudo is used.

Posted 2009/11/17 11:28 by alex


Comments

  1. May 11, 04:30 AM

    Burberry swiss watches Says:

    This article is very good, very appealing. Affects every reader chord, hoping to share with you the exchange. Thank articles.

  2. May 19, 08:28 AM

    best mens Guess watches Says:

    This article is very good, I like,
    thank you, I’m more understanding
    This kind of good

  3. May 26, 10:31 AM

    buy women's Corum watches Says:

    This article is really good, very appealing. Hope I can talk to you, thank you for your artic

  4. Dec 15, 06:30 AM

    nicolas Says:

    Well, I’m so excited, the story too good, I like it very much [url=http://www.rolereplicas.com/best-replica-rolex-submariner-watches-cb5.html]rolex submariner replica[/url], because I’ve been looking for some information about it.

  5. Jan 30, 12:39 PM

    flash chat Says:

    Hello, I will??™t perceive approach upload your website in my rss reader. able Help me, please dd1

Leave a Comment

Add your comment. Preview then Submit.


Hidden


Textile Help