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