]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/getlogin.3
getent.1, intro.1, time.1, _exit.2, _syscall.2, accept.2, access.2, acct.2, adjtimex...
[thirdparty/man-pages.git] / man3 / getlogin.3
CommitLineData
361763ff 1.\" Hey Emacs! This file is -*- nroff -*- source.
fea681da
MK
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.
c13182ef 13.\"
fea681da
MK
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.
c13182ef 21.\"
fea681da
MK
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
76492ece 28.TH GETLOGIN 3 2008-06-29 "GNU" "Linux Programmer's Manual"
fea681da 29.SH NAME
18701562 30getlogin, getlogin_r, cuserid \- get username
fea681da
MK
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 );
cc4615cc
MK
41.sp
42.in -4n
43Feature Test Macro Requirements for glibc (see
44.BR feature_test_macros (7)):
45.in
46.sp
47.BR getlogin_r ():
e0bf9127 48_REENTRANT || _POSIX_C_SOURCE\ >=\ 199506L
cc4615cc
MK
49.br
50.BR cuserid ():
51_XOPEN_SOURCE
fea681da 52.SH DESCRIPTION
60a90ecd
MK
53.BR getlogin ()
54returns a pointer to a string containing the name of
fea681da 55the user logged in on the controlling terminal of the process, or a
ad3fd4c0 56NULL pointer if this information cannot be determined.
c13182ef 57The string is
fea681da 58statically allocated and might be overwritten on subsequent calls to
60a90ecd
MK
59this function or to
60.BR cuserid ().
fea681da 61.PP
60a90ecd 62.BR getlogin_r ()
18701562 63returns this same username in the array
fea681da
MK
64.I buf
65of size
66.IR bufsize .
67.PP
60a90ecd 68.BR cuserid ()
18701562 69returns a pointer to a string containing a username
c13182ef
MK
70associated with the effective user ID of the process.
71If \fIstring\fP
ad3fd4c0 72is not a NULL pointer, it should be an array that can hold at least
fea681da 73\fBL_cuserid\fP characters; the string is returned in this array.
c13182ef
MK
74Otherwise, a pointer to a string in a static area is returned.
75This
fea681da 76string is statically allocated and might be overwritten on subsequent
60a90ecd
MK
77calls to this function or to
78.BR getlogin ().
fea681da
MK
79.PP
80The macro \fBL_cuserid\fP is an integer constant that indicates how
18701562 81long an array you might need to store a username.
1608e0bd 82\fBL_cuserid\fP is declared in \fI<stdio.h>\fP.
fea681da
MK
83.PP
84These functions let your program identify positively the user who is
988db661 85running
60a90ecd
MK
86.RB ( cuserid ())
87or the user who logged in this session
88.RB ( getlogin ()).
89(These can differ when set-user-ID programs are involved.)
fea681da
MK
90.PP
91For most purposes, it is more useful to use the environment variable
c13182ef
MK
92\fBLOGNAME\fP to find out who the user is.
93This is more flexible
fea681da 94precisely because the user can set \fBLOGNAME\fP arbitrarily.
47297adb 95.SH RETURN VALUE
60a90ecd 96.BR getlogin ()
18701562 97returns a pointer to the username when successful,
fea681da 98and NULL on failure.
60a90ecd 99.BR getlogin_r ()
c7094399 100returns 0 when successful, and nonzero on failure.
fea681da
MK
101.SH ERRORS
102POSIX specifies
103.TP
104.B EMFILE
105The calling process already has the maximum allowed number of open files.
106.TP
107.B ENFILE
108The system already has the maximum allowed number of open files.
109.TP
110.B ENXIO
1285ff3d 111The calling process has no controlling terminal.
fea681da
MK
112.TP
113.B ERANGE
114(getlogin_r)
18701562 115The length of the username, including the terminating null byte,
28d88c17 116is larger than
fea681da
MK
117.IR bufsize .
118.LP
119Linux/glibc also has
120.TP
121.B ENOENT
122There was no corresponding entry in the utmp-file.
123.TP
124.B ENOMEM
125Insufficient memory to allocate passwd structure.
1de9af46
MK
126.TP
127.B ENOTTY
128Standard input didn't refer to a terminal.
129(See BUGS.)
fea681da 130.SH FILES
3ffdc54f
MK
131.TP
132\fI/etc/passwd\fP
133password database file
134.TP
135\fI/var/run/utmp\fP
136(traditionally \fI/etc/utmp\fP;
137some libc versions used \fI/var/adm/utmp\fP)
47297adb 138.SH CONFORMING TO
68e1685c
MK
139.BR getlogin ()
140and
141.BR getlogin_r ()
142specified in POSIX.1-2001.
143
60a90ecd
MK
144System V has a
145.BR cuserid ()
146function which uses the real
c13182ef 147user ID rather than the effective user ID.
60a90ecd
MK
148The
149.BR cuserid ()
150function
c13182ef 151was included in the 1988 version of POSIX,
68e1685c
MK
152but removed from the 1990 version.
153It was present in SUSv2, but removed in POSIX.1-2001.
fea681da 154.LP
60a90ecd
MK
155OpenBSD has
156.BR getlogin ()
157and
158.BR setlogin (),
159and a username
1285ff3d 160associated with a session, even if it has no controlling terminal.
fea681da 161.SH BUGS
c13182ef 162Unfortunately, it is often rather easy to fool
b5cc2ffb 163.BR getlogin ().
fea681da 164Sometimes it does not work at all, because some program messed up
c13182ef
MK
165the utmp file.
166Often, it gives only the first 8 characters of
167the login name.
1285ff3d 168The user currently logged in on the controlling terminal
fea681da 169of our program need not be the user who started it.
c13182ef 170Avoid
b5cc2ffb
MK
171.BR getlogin ()
172for security-related purposes.
fea681da 173.LP
a306ffff 174Note that glibc does not follow the POSIX specification and uses
68e1685c 175.I stdin
fea681da
MK
176instead of
177.IR /dev/tty .
c13182ef
MK
178A bug.
179(Other recent systems, like SunOS 5.8 and HP-UX 11.11 and FreeBSD 4.8
180all return the login name also when
181.I stdin
68e1685c 182is redirected.)
fea681da 183.LP
c13182ef
MK
184Nobody knows precisely what
185.BR cuserid ()
b5cc2ffb 186does; avoid it in portable programs.
c13182ef 187Or avoid it altogether: use
b5cc2ffb
MK
188.I getpwuid(geteuid())
189instead, if that is
fea681da 190what you meant.
4bd8c614 191.B Do not use
b5cc2ffb 192.BR cuserid ().
47297adb 193.SH SEE ALSO
fea681da 194.BR geteuid (2),
1bced40f
MK
195.BR getuid (2),
196.BR utmp (5)