/etc/passwd
password database file
Get username
Part of getlogin(3).
char *getlogin(void);
int
getlogin_r(size_t
bufsize;
char
buf
[bufsize]
,
size_t
bufsize);
char *cuserid(char
*string);
_POSIX_C_SOURCE >=
199506L
Since glibc
2.24:
Up to
and
including
glibc
2.23:
_XOPEN_SOURCE
Standard C library (libc, -lc)
getlogin() returns a pointer to a string containing the name of the user logged in on the controlling terminal of the process, or a null pointer if this information cannot be determined. The string is statically allocated and might be overwritten on subsequent calls to this function or to cuserid().
getlogin_r() returns this same username in the array buf of size bufsize.
cuserid() returns a pointer to a string containing a username associated with the effective user ID of the process. If string is not a null pointer, it should be an array that can hold at least L_cuserid characters; the string is returned in this array. Otherwise, a pointer to a string in a static area is returned. This string is statically allocated and might be overwritten on subsequent calls to this function or to getlogin().
The macro L_cuserid is an integer constant that indicates how long an array you might need to store a username. L_cuserid is declared in <stdio.h>.
These functions let your program identify positively the user who is running (cuserid()) or the user who logged in this session (getlogin()). (These can differ when set-user-ID programs are involved.)
For most purposes, it is more useful to use the environment variable LOGNAME to find out who the user is. This is more flexible precisely because the user can set LOGNAME arbitrarily.
getlogin() returns a pointer to the username when successful, and NULL on failure, with errno set to indicate the error. getlogin_r() returns 0 when successful, and nonzero on failure.
POSIX specifies:
The per-process limit on the number of open file descriptors has been reached.
The system-wide limit on the total number of open files has been reached.
The calling process has no controlling terminal.
(getlogin_r) The length of the username, including the terminating null byte ('\0'), is larger than bufsize.
Linux/glibc also has:
There was no corresponding entry in the utmp-file.
Insufficient memory to allocate passwd structure.
Standard input didn't refer to a terminal. (See BUGS.)
/etc/passwd
password database file
/var/run/utmp
(traditionally /etc/utmp; some libc versions used /var/adm/utmp)
For an explanation of the terms used in this section, see attributes(7).
| Interface | Attribute | Value |
| getlogin() | Thread safety | MT-Unsafe race:getlogin race:utent sig:ALRM timer locale |
| getlogin_r() | Thread safety | MT-Unsafe race:utent sig:ALRM timer locale |
| cuserid() | Thread safety | MT-Unsafe race:cuserid/!string locale |
In the above table, utent in race:utent signifies that if any of the functions setutent(3), getutent(3), or endutent(3) are used in parallel in different threads of a program, then data races could occur. getlogin() and getlogin_r() call those functions, so we use race:utent to remind users.
OpenBSD has getlogin() and setlogin(), and a username associated with a session, even if it has no controlling terminal.