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