OSDN Git Service

01163b29a50ae986604caf3c36f0417d27c5824a
[linuxjm/LDP_man-pages.git] / original / 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 2013-04-19 "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 .br
50 .BR cuserid ():
51 _XOPEN_SOURCE
52 .SH DESCRIPTION
53 .BR getlogin ()
54 returns a pointer to a string containing the name of
55 the user logged in on the controlling terminal of the process, or a
56 null pointer if this information cannot be determined.
57 The string is
58 statically allocated and might be overwritten on subsequent calls to
59 this function or to
60 .BR cuserid ().
61 .PP
62 .BR getlogin_r ()
63 returns this same username in the array
64 .I buf
65 of size
66 .IR bufsize .
67 .PP
68 .BR cuserid ()
69 returns a pointer to a string containing a username
70 associated with the effective user ID of the process.
71 If \fIstring\fP
72 is not a null pointer, it should be an array that can hold at least
73 \fBL_cuserid\fP characters; the string is returned in this array.
74 Otherwise, a pointer to a string in a static area is returned.
75 This
76 string is statically allocated and might be overwritten on subsequent
77 calls to this function or to
78 .BR getlogin ().
79 .PP
80 The macro \fBL_cuserid\fP is an integer constant that indicates how
81 long an array you might need to store a username.
82 \fBL_cuserid\fP is declared in \fI<stdio.h>\fP.
83 .PP
84 These functions let your program identify positively the user who is
85 running
86 .RB ( cuserid ())
87 or the user who logged in this session
88 .RB ( getlogin ()).
89 (These can differ when set-user-ID programs are involved.)
90 .PP
91 For most purposes, it is more useful to use the environment variable
92 \fBLOGNAME\fP to find out who the user is.
93 This is more flexible
94 precisely because the user can set \fBLOGNAME\fP arbitrarily.
95 .SH RETURN VALUE
96 .BR getlogin ()
97 returns a pointer to the username when successful,
98 and NULL on failure, with
99 .I errno
100 set to indicate the cause of the error.
101 .BR getlogin_r ()
102 returns 0 when successful, and nonzero on failure.
103 .SH ERRORS
104 POSIX specifies
105 .TP
106 .B EMFILE
107 The calling process already has the maximum allowed number of open files.
108 .TP
109 .B ENFILE
110 The system already has the maximum allowed number of open files.
111 .TP
112 .B ENXIO
113 The calling process has no controlling terminal.
114 .TP
115 .B ERANGE
116 (getlogin_r)
117 The length of the username, including the terminating null byte (\(aq\\0\(aq),
118 is larger than
119 .IR bufsize .
120 .LP
121 Linux/glibc also has
122 .TP
123 .B ENOENT
124 There was no corresponding entry in the utmp-file.
125 .TP
126 .B ENOMEM
127 Insufficient memory to allocate passwd structure.
128 .TP
129 .B ENOTTY
130 Standard input didn't refer to a terminal.
131 (See BUGS.)
132 .SH FILES
133 .TP
134 \fI/etc/passwd\fP
135 password database file
136 .TP
137 \fI/var/run/utmp\fP
138 (traditionally \fI/etc/utmp\fP;
139 some libc versions used \fI/var/adm/utmp\fP)
140 .SH ATTRIBUTES
141 .SS Multithreading (see pthreads(7))
142 The
143 .BR getlogin ()
144 function is not thread-safe.
145 .LP
146 The
147 .BR getlogin_r ()
148 function is thread-safe.
149 .LP
150 The
151 .BR cuserid ()
152 function is thread-safe with exceptions.
153 It is not thread-safe if called with a NULL parameter.
154 .SH CONFORMING TO
155 .BR getlogin ()
156 and
157 .BR getlogin_r ()
158 specified in POSIX.1-2001.
159
160 System V has a
161 .BR cuserid ()
162 function which uses the real
163 user ID rather than the effective user ID.
164 The
165 .BR cuserid ()
166 function
167 was included in the 1988 version of POSIX,
168 but removed from the 1990 version.
169 It was present in SUSv2, but removed in POSIX.1-2001.
170 .LP
171 OpenBSD has
172 .BR getlogin ()
173 and
174 .BR setlogin (),
175 and a username
176 associated with a session, even if it has no controlling terminal.
177 .SH BUGS
178 Unfortunately, it is often rather easy to fool
179 .BR getlogin ().
180 Sometimes it does not work at all, because some program messed up
181 the utmp file.
182 Often, it gives only the first 8 characters of
183 the login name.
184 The user currently logged in on the controlling terminal
185 of our program need not be the user who started it.
186 Avoid
187 .BR getlogin ()
188 for security-related purposes.
189 .LP
190 Note that glibc does not follow the POSIX specification and uses
191 .I stdin
192 instead of
193 .IR /dev/tty .
194 A bug.
195 (Other recent systems, like SunOS 5.8 and HP-UX 11.11 and FreeBSD 4.8
196 all return the login name also when
197 .I stdin
198 is redirected.)
199 .LP
200 Nobody knows precisely what
201 .BR cuserid ()
202 does; avoid it in portable programs.
203 Or avoid it altogether: use
204 .I getpwuid(geteuid())
205 instead, if that is
206 what you meant.
207 .B Do not use
208 .BR cuserid ().
209 .SH SEE ALSO
210 .BR geteuid (2),
211 .BR getuid (2),
212 .BR utmp (5)
213 .SH COLOPHON
214 This page is part of release 3.79 of the Linux
215 .I man-pages
216 project.
217 A description of the project,
218 information about reporting bugs,
219 and the latest version of this page,
220 can be found at
221 \%http://www.kernel.org/doc/man\-pages/.