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