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