1getlogin(3)                Library Functions Manual                getlogin(3)
2
3
4

NAME

6       getlogin, getlogin_r, cuserid - get username
7

LIBRARY

9       Standard C library (libc, -lc)
10

SYNOPSIS

12       #include <unistd.h>
13
14       char *getlogin(void);
15       int getlogin_r(char buf[.bufsize], size_t bufsize);
16
17       #include <stdio.h>
18
19       char *cuserid(char *string);
20
21   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
22
23       getlogin_r():
24           _POSIX_C_SOURCE >= 199506L
25
26       cuserid():
27           Since glibc 2.24:
28               (_XOPEN_SOURCE && ! (_POSIX_C_SOURCE >= 200112L)
29                   || _GNU_SOURCE
30           Up to and including glibc 2.23:
31               _XOPEN_SOURCE
32

DESCRIPTION

34       getlogin()  returns  a  pointer  to a string containing the name of the
35       user logged in on the controlling terminal of the process,  or  a  null
36       pointer if this information cannot be determined.  The string is stati‐
37       cally allocated and might be overwritten on subsequent  calls  to  this
38       function or to cuserid().
39
40       getlogin_r()  returns  this same username in the array buf of size buf‐
41       size.
42
43       cuserid() returns a pointer to a string containing a  username  associ‐
44       ated  with  the  effective  user ID of the process.  If string is not a
45       null pointer, it should be an array that can hold  at  least  L_cuserid
46       characters; the string is returned in this array.  Otherwise, a pointer
47       to a string in a static area is returned.  This  string  is  statically
48       allocated and might be overwritten on subsequent calls to this function
49       or to getlogin().
50
51       The macro L_cuserid is an integer constant that indicates how  long  an
52       array  you  might  need  to store a username.  L_cuserid is declared in
53       <stdio.h>.
54
55       These functions let your program identify positively the  user  who  is
56       running  (cuserid())  or  the  user  who logged in this session (getlo‐
57       gin()).  (These can differ when set-user-ID programs are involved.)
58
59       For most purposes, it is more useful to use  the  environment  variable
60       LOGNAME  to  find out who the user is.  This is more flexible precisely
61       because the user can set LOGNAME arbitrarily.
62

RETURN VALUE

64       getlogin() returns a pointer to the username when successful, and  NULL
65       on failure, with errno set to indicate the error.  getlogin_r() returns
66       0 when successful, and nonzero on failure.
67

ERRORS

69       POSIX specifies:
70
71       EMFILE The per-process limit on the number of open file descriptors has
72              been reached.
73
74       ENFILE The system-wide limit on the total number of open files has been
75              reached.
76
77       ENXIO  The calling process has no controlling terminal.
78
79       ERANGE (getlogin_r) The length of the username, including the terminat‐
80              ing null byte ('\0'), is larger than bufsize.
81
82       Linux/glibc also has:
83
84       ENOENT There was no corresponding entry in the utmp-file.
85
86       ENOMEM Insufficient memory to allocate passwd structure.
87
88       ENOTTY Standard input didn't refer to a terminal.  (See BUGS.)
89

FILES

91       /etc/passwd
92              password database file
93
94       /var/run/utmp
95              (traditionally /etc/utmp; some libc versions used /var/adm/utmp)
96

ATTRIBUTES

98       For  an  explanation  of  the  terms  used  in  this  section,  see at‐
99       tributes(7).
100
101       ┌─────────────┬───────────────┬────────────────────────────────────────┐
102Interface    Attribute     Value                                  
103       ├─────────────┼───────────────┼────────────────────────────────────────┤
104getlogin()   │ Thread safety │ MT-Unsafe race:getlogin race:utent     │
105       │             │               │ sig:ALRM timer locale                  │
106       ├─────────────┼───────────────┼────────────────────────────────────────┤
107getlogin_r() │ Thread safety │ MT-Unsafe race:utent sig:ALRM timer    │
108       │             │               │ locale                                 │
109       ├─────────────┼───────────────┼────────────────────────────────────────┤
110cuserid()    │ Thread safety │ MT-Unsafe race:cuserid/!string locale  │
111       └─────────────┴───────────────┴────────────────────────────────────────┘
112       In the above table, utent in race:utent signifies that if  any  of  the
113       functions setutent(3), getutent(3), or endutent(3) are used in parallel
114       in different threads of a program, then data races could occur.  getlo‐
115       gin()  and  getlogin_r()  call those functions, so we use race:utent to
116       remind users.
117

VERSIONS

119       OpenBSD has getlogin() and setlogin(), and a username associated with a
120       session, even if it has no controlling terminal.
121

STANDARDS

123       getlogin()
124       getlogin_r()
125              POSIX.1-2008.
126
127       cuserid()
128              None.
129

STANDARDS

131       getlogin()
132       getlogin_r():
133              POSIX.1-2001.  OpenBSD.
134
135       cuserid()
136              System  V,  POSIX.1-1988.  Removed in POSIX.1-1990.  SUSv2.  Re‐
137              moved in POSIX.1-2001.
138
139              System V has a cuserid() function which uses the  real  user  ID
140              rather than the effective user ID.
141

BUGS

143       Unfortunately,  it  is often rather easy to fool getlogin().  Sometimes
144       it does not work at all, because some program messed up the utmp  file.
145       Often, it gives only the first 8 characters of the login name.  The us‐
146       er currently logged in on the controlling terminal of our program  need
147       not  be the user who started it.  Avoid getlogin() for security-related
148       purposes.
149
150       Note that glibc does not follow the POSIX specification and uses  stdin
151       instead of /dev/tty.  A bug.  (Other recent systems, like SunOS 5.8 and
152       HP-UX 11.11 and FreeBSD 4.8 all return the login name also  when  stdin
153       is redirected.)
154
155       Nobody  knows  precisely what cuserid() does; avoid it in portable pro‐
156       grams.  Or avoid it altogether:  use  getpwuid(geteuid())  instead,  if
157       that is what you meant.  Do not use cuserid().
158

SEE ALSO

160       logname(1), geteuid(2), getuid(2), utmp(5)
161
162
163
164Linux man-pages 6.04              2023-03-30                       getlogin(3)
Impressum