]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/getlogin.3
ffix
[thirdparty/man-pages.git] / man3 / getlogin.3
1 .\" Hey Emacs! This file is -*- nroff -*- source.
2 .\"
3 .\" Copyright 1995 James R. Van Zandt <jrv@vanzandt.mv.com>
4 .\"
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
8 .\"
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
13 .\"
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
20 .\" professionally.
21 .\"
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
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 2007-07-26 "GNU" "Linux Programmer's Manual"
29 .SH NAME
30 getlogin, getlogin_r, cuserid \- get user name
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 user name 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 user name
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 user name.
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 user name when successful,
98 and NULL on failure.
99 .BR getlogin_r ()
100 returns 0 when successful, and non-zero on failure.
101 .SH ERRORS
102 POSIX specifies
103 .TP
104 .B EMFILE
105 The calling process already has the maximum allowed number of open files.
106 .TP
107 .B ENFILE
108 The system already has the maximum allowed number of open files.
109 .TP
110 .B ENXIO
111 The calling process has no controlling tty.
112 .TP
113 .B ERANGE
114 (getlogin_r)
115 The length of the user name, including the terminating null byte,
116 is larger than
117 .IR bufsize .
118 .LP
119 Linux/glibc also has
120 .TP
121 .B ENOENT
122 There was no corresponding entry in the utmp-file.
123 .TP
124 .B ENOMEM
125 Insufficient memory to allocate passwd structure.
126 .SH FILES
127 .TP
128 \fI/etc/passwd\fP
129 password database file
130 .TP
131 \fI/var/run/utmp\fP
132 (traditionally \fI/etc/utmp\fP;
133 some libc versions used \fI/var/adm/utmp\fP)
134 .SH "CONFORMING TO"
135 .BR getlogin ()
136 and
137 .BR getlogin_r ()
138 specified in POSIX.1-2001.
139
140 System V has a
141 .BR cuserid ()
142 function which uses the real
143 user ID rather than the effective user ID.
144 The
145 .BR cuserid ()
146 function
147 was included in the 1988 version of POSIX,
148 but removed from the 1990 version.
149 It was present in SUSv2, but removed in POSIX.1-2001.
150 .LP
151 OpenBSD has
152 .BR getlogin ()
153 and
154 .BR setlogin (),
155 and a username
156 associated with a session, even if it has no controlling tty.
157 .SH BUGS
158 Unfortunately, it is often rather easy to fool
159 .BR getlogin ().
160 Sometimes it does not work at all, because some program messed up
161 the utmp file.
162 Often, it gives only the first 8 characters of
163 the login name.
164 The user currently logged in on the controlling tty
165 of our program need not be the user who started it.
166 Avoid
167 .BR getlogin ()
168 for security-related purposes.
169 .LP
170 Note that glibc does not follow the POSIX spec and uses
171 .I stdin
172 instead of
173 .IR /dev/tty .
174 A bug.
175 (Other recent systems, like SunOS 5.8 and HP-UX 11.11 and FreeBSD 4.8
176 all return the login name also when
177 .I stdin
178 is redirected.)
179 .LP
180 Nobody knows precisely what
181 .BR cuserid ()
182 does; avoid it in portable programs.
183 Or avoid it altogether: use
184 .I getpwuid(geteuid())
185 instead, if that is
186 what you meant.
187 .B Do not use
188 .BR cuserid ().
189 .SH "SEE ALSO"
190 .BR geteuid (2),
191 .BR getuid (2)