The Open Group Base Specifications Issue 6
IEEE Std 1003.1, 2004 Edition
Copyright © 2001-2004 The IEEE and The Open Group, All Rights reserved.
A newer edition of this document exists here

NAME

getlogin, getlogin_r - get login name

SYNOPSIS

#include <unistd.h>

char *getlogin(void);

[TSF] [Option Start] int getlogin_r(char *name, size_t namesize); [Option End]

DESCRIPTION

The getlogin() function shall return a pointer to a string containing the user name associated by the login activity with the controlling terminal of the current process. If getlogin() returns a non-null pointer, then that pointer points to the name that the user logged in under, even if there are several login names with the same user ID.

The getlogin() function need not be reentrant. A function that is not required to be reentrant is not required to be thread-safe.

[TSF] [Option Start] The getlogin_r() function shall put the name associated by the login activity with the controlling terminal of the current process in the character array pointed to by name. The array is namesize characters long and should have space for the name and the terminating null character. The maximum size of the login name is {LOGIN_NAME_MAX}.

If getlogin_r() is successful, name points to the name the user used at login, even if there are several login names with the same user ID. [Option End]

RETURN VALUE

Upon successful completion, getlogin() shall return a pointer to the login name or a null pointer if the user's login name cannot be found. Otherwise, it shall return a null pointer and set errno to indicate the error.

The return value from getlogin() may point to static data whose content is overwritten by each call.

[TSF] [Option Start] If successful, the getlogin_r() function shall return zero; otherwise, an error number shall be returned to indicate the error. [Option End]

ERRORS

The getlogin() and getlogin_r() functions may fail if:

[EMFILE]
{OPEN_MAX} file descriptors are currently open in the calling process.
[ENFILE]
The maximum allowable number of files is currently open in the system.
[ENXIO]
The calling process has no controlling terminal.

The getlogin_r() function may fail if:

[ERANGE]
[TSF] [Option Start] The value of namesize is smaller than the length of the string to be returned including the terminating null character. [Option End]

The following sections are informative.

EXAMPLES

Getting the User Login Name

The following example calls the getlogin() function to obtain the name of the user associated with the calling process, and passes this information to the getpwnam() function to get the associated user database information.

#include <unistd.h>
#include <sys/types.h>
#include <pwd.h>
#include <stdio.h>
...
char *lgn;
struct passwd *pw;
...
if ((lgn = getlogin()) == NULL || (pw = getpwnam(lgn)) == NULL) {
    fprintf(stderr, "Get of user information failed.\n"); exit(1);
    }

APPLICATION USAGE

Three names associated with the current process can be determined: getpwuid( geteuid()) shall return the name associated with the effective user ID of the process; getlogin() shall return the name associated with the current login activity; and getpwuid( getuid()) shall return the name associated with the real user ID of the process.

The getlogin_r() function is thread-safe and returns values in a user-supplied buffer instead of possibly using a static data area that may be overwritten by each call.

RATIONALE

The getlogin() function returns a pointer to the user's login name. The same user ID may be shared by several login names. If it is desired to get the user database entry that is used during login, the result of getlogin() should be used to provide the argument to the getpwnam() function. (This might be used to determine the user's login shell, particularly where a single user has multiple login shells with distinct login names, but the same user ID.)

The information provided by the cuserid() function, which was originally defined in the POSIX.1-1988 standard and subsequently removed, can be obtained by the following:

getpwuid(geteuid())

while the information provided by historical implementations of cuserid() can be obtained by:

getpwuid(getuid())

The thread-safe version of this function places the user name in a user-supplied buffer and returns a non-zero value if it fails. The non-thread-safe version may return the name in a static data area that may be overwritten by each call.

FUTURE DIRECTIONS

None.

SEE ALSO

getpwnam(), getpwuid(), geteuid(), getuid(), the Base Definitions volume of IEEE Std 1003.1-2001, <limits.h>, <unistd.h>

CHANGE HISTORY

First released in Issue 1. Derived from System V Release 2.0.

Issue 5

Normative text previously in the APPLICATION USAGE section is moved to the RETURN VALUE section.

The getlogin_r() function is included for alignment with the POSIX Threads Extension.

A note indicating that the getlogin() function need not be reentrant is added to the DESCRIPTION.

Issue 6

The getlogin_r() function is marked as part of the Thread-Safe Functions option.

In the DESCRIPTION, the note about reentrancy is expanded to cover thread-safety.

The following new requirements on POSIX implementations derive from alignment with the Single UNIX Specification:

The APPLICATION USAGE section is updated to include a note on the thread-safe function and its avoidance of possibly using a static data area.

End of informative text.

UNIX ® is a registered Trademark of The Open Group.
POSIX ® is a registered Trademark of The IEEE.
[ Main Index | XBD | XCU | XSH | XRAT ]