]>
Commit | Line | Data |
---|---|---|
fea681da MK |
1 | .\" Copyright 1995 James R. Van Zandt <jrv@vanzandt.mv.com> |
2 | .\" | |
93015253 | 3 | .\" %%%LICENSE_START(VERBATIM) |
fea681da MK |
4 | .\" Permission is granted to make and distribute verbatim copies of this |
5 | .\" manual provided the copyright notice and this permission notice are | |
6 | .\" preserved on all copies. | |
7 | .\" | |
8 | .\" Permission is granted to copy and distribute modified versions of this | |
9 | .\" manual under the conditions for verbatim copying, provided that the | |
10 | .\" entire resulting derived work is distributed under the terms of a | |
11 | .\" permission notice identical to this one. | |
c13182ef | 12 | .\" |
fea681da MK |
13 | .\" Since the Linux kernel and libraries are constantly changing, this |
14 | .\" manual page may be incorrect or out-of-date. The author(s) assume no | |
15 | .\" responsibility for errors or omissions, or for damages resulting from | |
16 | .\" the use of the information contained herein. The author(s) may not | |
17 | .\" have taken the same level of care in the production of this manual, | |
18 | .\" which is licensed free of charge, as they might when working | |
19 | .\" professionally. | |
c13182ef | 20 | .\" |
fea681da MK |
21 | .\" Formatted or processed versions of this manual, if unaccompanied by |
22 | .\" the source, must acknowledge the copyright and authors of this work. | |
4b72fb64 | 23 | .\" %%%LICENSE_END |
fea681da MK |
24 | .\" |
25 | .\" Changed Tue Sep 19 01:49:29 1995, aeb: moved from man2 to man3 | |
26 | .\" added ref to /etc/utmp, added BUGS section, etc. | |
27 | .\" modified 2003 Walter Harms, aeb - added getlogin_r, note on stdin use | |
b8efb414 | 28 | .TH GETLOGIN 3 2016-10-08 "GNU" "Linux Programmer's Manual" |
fea681da | 29 | .SH NAME |
18701562 | 30 | getlogin, getlogin_r, cuserid \- get username |
fea681da MK |
31 | .SH SYNOPSIS |
32 | .B #include <unistd.h> | |
68e4db0a | 33 | .PP |
fea681da MK |
34 | .B "char *getlogin(void);" |
35 | .br | |
36 | .BI "int getlogin_r(char *" buf ", size_t " bufsize ); | |
dbfe9c70 | 37 | .PP |
fea681da | 38 | .B #include <stdio.h> |
68e4db0a | 39 | .PP |
fea681da | 40 | .BI "char *cuserid(char *" string ); |
68e4db0a | 41 | .PP |
cc4615cc MK |
42 | .in -4n |
43 | Feature Test Macro Requirements for glibc (see | |
44 | .BR feature_test_macros (7)): | |
45 | .in | |
68e4db0a | 46 | .PP |
cc4615cc | 47 | .BR getlogin_r (): |
790980e0 MK |
48 | .\" Deprecated: _REENTRANT || |
49 | _POSIX_C_SOURCE\ >=\ 199506L | |
68e4db0a | 50 | .PP |
cc4615cc | 51 | .BR cuserid (): |
8ff35845 MK |
52 | .nf |
53 | Since glibc 2.24: | |
54 | (_XOPEN_SOURCE && ! (_POSIX_C_SOURCE >= 200112L) | |
55 | || __GNU_SOURCE | |
56 | Up to and including glibc 2.23: | |
57 | _XOPEN_SOURCE | |
58 | .fi | |
fea681da | 59 | .SH DESCRIPTION |
60a90ecd MK |
60 | .BR getlogin () |
61 | returns a pointer to a string containing the name of | |
fea681da | 62 | the user logged in on the controlling terminal of the process, or a |
b437fdd9 | 63 | null pointer if this information cannot be determined. |
c13182ef | 64 | The string is |
fea681da | 65 | statically allocated and might be overwritten on subsequent calls to |
60a90ecd MK |
66 | this function or to |
67 | .BR cuserid (). | |
fea681da | 68 | .PP |
60a90ecd | 69 | .BR getlogin_r () |
18701562 | 70 | returns this same username in the array |
fea681da MK |
71 | .I buf |
72 | of size | |
73 | .IR bufsize . | |
74 | .PP | |
60a90ecd | 75 | .BR cuserid () |
18701562 | 76 | returns a pointer to a string containing a username |
c13182ef MK |
77 | associated with the effective user ID of the process. |
78 | If \fIstring\fP | |
b437fdd9 | 79 | is not a null pointer, it should be an array that can hold at least |
fea681da | 80 | \fBL_cuserid\fP characters; the string is returned in this array. |
c13182ef MK |
81 | Otherwise, a pointer to a string in a static area is returned. |
82 | This | |
fea681da | 83 | string is statically allocated and might be overwritten on subsequent |
60a90ecd MK |
84 | calls to this function or to |
85 | .BR getlogin (). | |
fea681da MK |
86 | .PP |
87 | The macro \fBL_cuserid\fP is an integer constant that indicates how | |
18701562 | 88 | long an array you might need to store a username. |
1608e0bd | 89 | \fBL_cuserid\fP is declared in \fI<stdio.h>\fP. |
fea681da MK |
90 | .PP |
91 | These functions let your program identify positively the user who is | |
988db661 | 92 | running |
60a90ecd MK |
93 | .RB ( cuserid ()) |
94 | or the user who logged in this session | |
95 | .RB ( getlogin ()). | |
96 | (These can differ when set-user-ID programs are involved.) | |
fea681da MK |
97 | .PP |
98 | For most purposes, it is more useful to use the environment variable | |
c13182ef MK |
99 | \fBLOGNAME\fP to find out who the user is. |
100 | This is more flexible | |
fea681da | 101 | precisely because the user can set \fBLOGNAME\fP arbitrarily. |
47297adb | 102 | .SH RETURN VALUE |
60a90ecd | 103 | .BR getlogin () |
18701562 | 104 | returns a pointer to the username when successful, |
c0ebcc7a MK |
105 | and NULL on failure, with |
106 | .I errno | |
107 | set to indicate the cause of the error. | |
60a90ecd | 108 | .BR getlogin_r () |
c7094399 | 109 | returns 0 when successful, and nonzero on failure. |
fea681da MK |
110 | .SH ERRORS |
111 | POSIX specifies | |
112 | .TP | |
113 | .B EMFILE | |
26c32fab | 114 | The per-process limit on the number of open file descriptors has been reached. |
fea681da MK |
115 | .TP |
116 | .B ENFILE | |
e258766b | 117 | The system-wide limit on the total number of open files has been reached. |
fea681da MK |
118 | .TP |
119 | .B ENXIO | |
1285ff3d | 120 | The calling process has no controlling terminal. |
fea681da MK |
121 | .TP |
122 | .B ERANGE | |
123 | (getlogin_r) | |
f24e3a3a | 124 | The length of the username, including the terminating null byte (\(aq\\0\(aq), |
28d88c17 | 125 | is larger than |
fea681da MK |
126 | .IR bufsize . |
127 | .LP | |
128 | Linux/glibc also has | |
129 | .TP | |
130 | .B ENOENT | |
131 | There was no corresponding entry in the utmp-file. | |
132 | .TP | |
133 | .B ENOMEM | |
134 | Insufficient memory to allocate passwd structure. | |
1de9af46 MK |
135 | .TP |
136 | .B ENOTTY | |
137 | Standard input didn't refer to a terminal. | |
138 | (See BUGS.) | |
fea681da | 139 | .SH FILES |
3ffdc54f MK |
140 | .TP |
141 | \fI/etc/passwd\fP | |
142 | password database file | |
143 | .TP | |
144 | \fI/var/run/utmp\fP | |
145 | (traditionally \fI/etc/utmp\fP; | |
146 | some libc versions used \fI/var/adm/utmp\fP) | |
1454246e | 147 | .SH ATTRIBUTES |
edb60ed0 PH |
148 | For an explanation of the terms used in this section, see |
149 | .BR attributes (7). | |
150 | .TS | |
151 | allbox; | |
152 | lb lb lb | |
153 | l l l. | |
154 | Interface Attribute Value | |
155 | T{ | |
1454246e | 156 | .BR getlogin () |
1e2afa78 MS |
157 | T} Thread safety T{ |
158 | MT-Unsafe race:getlogin race:utent | |
159 | .br | |
160 | sig:ALRM timer locale | |
161 | T} | |
edb60ed0 | 162 | T{ |
1454246e | 163 | .BR getlogin_r () |
1e2afa78 MS |
164 | T} Thread safety T{ |
165 | MT-Unsafe race:utent sig:ALRM timer | |
166 | .br | |
167 | locale | |
168 | T} | |
edb60ed0 | 169 | T{ |
1454246e | 170 | .BR cuserid () |
edb60ed0 PH |
171 | T} Thread safety MT-Unsafe race:cuserid/!string locale |
172 | .TE | |
847e0d88 | 173 | .sp 1 |
1e2afa78 MS |
174 | In the above table, |
175 | .I utent | |
176 | in | |
177 | .I race:utent | |
178 | signifies that if any of the functions | |
60570269 MK |
179 | .BR setutent (3), |
180 | .BR getutent (3), | |
1e2afa78 | 181 | or |
58f5b793 MK |
182 | .BR endutent (3) |
183 | are used in parallel in different threads of a program, | |
184 | then data races could occur. | |
1e2afa78 MS |
185 | .BR getlogin () |
186 | and | |
187 | .BR getlogin_r () | |
58f5b793 MK |
188 | call those functions, |
189 | so we use race:utent to remind users. | |
47297adb | 190 | .SH CONFORMING TO |
68e1685c MK |
191 | .BR getlogin () |
192 | and | |
d7298fcf MK |
193 | .BR getlogin_r (): |
194 | POSIX.1-2001, POSIX.1-2008. | |
847e0d88 | 195 | .PP |
60a90ecd MK |
196 | System V has a |
197 | .BR cuserid () | |
198 | function which uses the real | |
c13182ef | 199 | user ID rather than the effective user ID. |
60a90ecd MK |
200 | The |
201 | .BR cuserid () | |
202 | function | |
c13182ef | 203 | was included in the 1988 version of POSIX, |
68e1685c MK |
204 | but removed from the 1990 version. |
205 | It was present in SUSv2, but removed in POSIX.1-2001. | |
fea681da | 206 | .LP |
60a90ecd MK |
207 | OpenBSD has |
208 | .BR getlogin () | |
209 | and | |
210 | .BR setlogin (), | |
211 | and a username | |
1285ff3d | 212 | associated with a session, even if it has no controlling terminal. |
fea681da | 213 | .SH BUGS |
c13182ef | 214 | Unfortunately, it is often rather easy to fool |
b5cc2ffb | 215 | .BR getlogin (). |
fea681da | 216 | Sometimes it does not work at all, because some program messed up |
c13182ef MK |
217 | the utmp file. |
218 | Often, it gives only the first 8 characters of | |
219 | the login name. | |
1285ff3d | 220 | The user currently logged in on the controlling terminal |
fea681da | 221 | of our program need not be the user who started it. |
c13182ef | 222 | Avoid |
b5cc2ffb MK |
223 | .BR getlogin () |
224 | for security-related purposes. | |
fea681da | 225 | .LP |
a306ffff | 226 | Note that glibc does not follow the POSIX specification and uses |
68e1685c | 227 | .I stdin |
fea681da MK |
228 | instead of |
229 | .IR /dev/tty . | |
c13182ef MK |
230 | A bug. |
231 | (Other recent systems, like SunOS 5.8 and HP-UX 11.11 and FreeBSD 4.8 | |
232 | all return the login name also when | |
233 | .I stdin | |
68e1685c | 234 | is redirected.) |
fea681da | 235 | .LP |
c13182ef MK |
236 | Nobody knows precisely what |
237 | .BR cuserid () | |
b5cc2ffb | 238 | does; avoid it in portable programs. |
c13182ef | 239 | Or avoid it altogether: use |
b5cc2ffb MK |
240 | .I getpwuid(geteuid()) |
241 | instead, if that is | |
fea681da | 242 | what you meant. |
4bd8c614 | 243 | .B Do not use |
b5cc2ffb | 244 | .BR cuserid (). |
47297adb | 245 | .SH SEE ALSO |
2a47cc96 | 246 | .BR logname (1), |
fea681da | 247 | .BR geteuid (2), |
1bced40f MK |
248 | .BR getuid (2), |
249 | .BR utmp (5) |