1GETLOGIN(3) Linux Programmer's Manual GETLOGIN(3)
2
3
4
6 getlogin, getlogin_r, cuserid - get username
7
9 #include <unistd.h>
10
11 char *getlogin(void);
12 int getlogin_r(char *buf, size_t bufsize);
13
14 #include <stdio.h>
15
16 char *cuserid(char *string);
17
18 Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
19
20 getlogin_r():
21 _POSIX_C_SOURCE >= 199506L
22
23 cuserid():
24 Since glibc 2.24:
25 (_XOPEN_SOURCE && ! (_POSIX_C_SOURCE >= 200112L)
26 || _GNU_SOURCE
27 Up to and including glibc 2.23:
28 _XOPEN_SOURCE
29
31 getlogin() returns a pointer to a string containing the name of the
32 user logged in on the controlling terminal of the process, or a null
33 pointer if this information cannot be determined. The string is stati‐
34 cally allocated and might be overwritten on subsequent calls to this
35 function or to cuserid().
36
37 getlogin_r() returns this same username in the array buf of size buf‐
38 size.
39
40 cuserid() returns a pointer to a string containing a username associ‐
41 ated with the effective user ID of the process. If string is not a
42 null pointer, it should be an array that can hold at least L_cuserid
43 characters; the string is returned in this array. Otherwise, a pointer
44 to a string in a static area is returned. This string is statically
45 allocated and might be overwritten on subsequent calls to this function
46 or to getlogin().
47
48 The macro L_cuserid is an integer constant that indicates how long an
49 array you might need to store a username. L_cuserid is declared in
50 <stdio.h>.
51
52 These functions let your program identify positively the user who is
53 running (cuserid()) or the user who logged in this session (getlo‐
54 gin()). (These can differ when set-user-ID programs are involved.)
55
56 For most purposes, it is more useful to use the environment variable
57 LOGNAME to find out who the user is. This is more flexible precisely
58 because the user can set LOGNAME arbitrarily.
59
61 getlogin() returns a pointer to the username when successful, and NULL
62 on failure, with errno set to indicate the error. getlogin_r() returns
63 0 when successful, and nonzero on failure.
64
66 POSIX specifies:
67
68 EMFILE The per-process limit on the number of open file descriptors has
69 been reached.
70
71 ENFILE The system-wide limit on the total number of open files has been
72 reached.
73
74 ENXIO The calling process has no controlling terminal.
75
76 ERANGE (getlogin_r) The length of the username, including the terminat‐
77 ing null byte ('\0'), is larger than bufsize.
78
79 Linux/glibc also has:
80
81 ENOENT There was no corresponding entry in the utmp-file.
82
83 ENOMEM Insufficient memory to allocate passwd structure.
84
85 ENOTTY Standard input didn't refer to a terminal. (See BUGS.)
86
88 /etc/passwd
89 password database file
90
91 /var/run/utmp
92 (traditionally /etc/utmp; some libc versions used /var/adm/utmp)
93
95 For an explanation of the terms used in this section, see at‐
96 tributes(7).
97
98 ┌─────────────┬───────────────┬────────────────────────────────────────┐
99 │Interface │ Attribute │ Value │
100 ├─────────────┼───────────────┼────────────────────────────────────────┤
101 │getlogin() │ Thread safety │ MT-Unsafe race:getlogin race:utent │
102 │ │ │ sig:ALRM timer locale │
103 ├─────────────┼───────────────┼────────────────────────────────────────┤
104 │getlogin_r() │ Thread safety │ MT-Unsafe race:utent sig:ALRM timer │
105 │ │ │ locale │
106 ├─────────────┼───────────────┼────────────────────────────────────────┤
107 │cuserid() │ Thread safety │ MT-Unsafe race:cuserid/!string locale │
108 └─────────────┴───────────────┴────────────────────────────────────────┘
109 In the above table, utent in race:utent signifies that if any of the
110 functions setutent(3), getutent(3), or endutent(3) are used in parallel
111 in different threads of a program, then data races could occur. getlo‐
112 gin() and getlogin_r() call those functions, so we use race:utent to
113 remind users.
114
116 getlogin() and getlogin_r(): POSIX.1-2001, POSIX.1-2008.
117
118 System V has a cuserid() function which uses the real user ID rather
119 than the effective user ID. The cuserid() function was included in the
120 1988 version of POSIX, but removed from the 1990 version. It was
121 present in SUSv2, but removed in POSIX.1-2001.
122
123 OpenBSD has getlogin() and setlogin(), and a username associated with a
124 session, even if it has no controlling terminal.
125
127 Unfortunately, it is often rather easy to fool getlogin(). Sometimes
128 it does not work at all, because some program messed up the utmp file.
129 Often, it gives only the first 8 characters of the login name. The us‐
130 er currently logged in on the controlling terminal of our program need
131 not be the user who started it. Avoid getlogin() for security-related
132 purposes.
133
134 Note that glibc does not follow the POSIX specification and uses stdin
135 instead of /dev/tty. A bug. (Other recent systems, like SunOS 5.8 and
136 HP-UX 11.11 and FreeBSD 4.8 all return the login name also when stdin
137 is redirected.)
138
139 Nobody knows precisely what cuserid() does; avoid it in portable pro‐
140 grams. Or avoid it altogether: use getpwuid(geteuid()) instead, if
141 that is what you meant. Do not use cuserid().
142
144 logname(1), geteuid(2), getuid(2), utmp(5)
145
147 This page is part of release 5.13 of the Linux man-pages project. A
148 description of the project, information about reporting bugs, and the
149 latest version of this page, can be found at
150 https://www.kernel.org/doc/man-pages/.
151
152
153
154GNU 2021-03-22 GETLOGIN(3)