1 .\" Hey Emacs! This file is -*- nroff -*- source.
3 .\" Copyright 1995 James R. Van Zandt <jrv@vanzandt.mv.com>
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.
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.
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
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
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 2003-08-24 "Linux 2.4" "Linux Programmer's Manual"
30 getlogin, getlogin_r, cuserid \- get user name
32 .B #include <unistd.h>
34 .B "char *getlogin(void);"
36 .BI "int getlogin_r(char *" buf ", size_t " bufsize );
40 .BI "char *cuserid(char *" string );
42 \fBgetlogin\fP returns a pointer to a string containing the name of
43 the user logged in on the controlling terminal of the process, or a
44 null pointer if this information cannot be determined. The string is
45 statically allocated and might be overwritten on subsequent calls to
46 this function or to \fBcuserid\fP.
48 \fBgetlogin_r\fP returns this same user name in the array
53 \fBcuserid\fP returns a pointer to a string containing a user name
54 associated with the effective user ID of the process. If \fIstring\fP
55 is not a null pointer, it should be an array that can hold at least
56 \fBL_cuserid\fP characters; the string is returned in this array.
57 Otherwise, a pointer to a string in a static area is returned. This
58 string is statically allocated and might be overwritten on subsequent
59 calls to this function or to \fBgetlogin\fP.
61 The macro \fBL_cuserid\fP is an integer constant that indicates how
62 long an array you might need to store a user name. \fBL_cuserid\fP is
63 declared in \fBstdio.h\fP.
65 These functions let your program identify positively the user who is
66 running (\fBcuserid\fP) or the user who logged in this session
67 (\fBgetlogin\fP). (These can differ when set-user-ID programs are
70 For most purposes, it is more useful to use the environment variable
71 \fBLOGNAME\fP to find out who the user is. This is more flexible
72 precisely because the user can set \fBLOGNAME\fP arbitrarily.
74 \fBgetlogin\fP returns a pointer to the user name when successful,
76 \fBgetlogin_r\fP returns 0 when successful, and non-zero on failure.
81 The calling process already has the maximum allowed number of open files.
84 The system already has the maximum allowed number of open files.
87 The calling process has no controlling tty.
91 The length of the user name, including final NUL, is larger than
97 There was no corresponding entry in the utmp-file.
100 Insufficient memory to allocate passwd structure.
103 \fI/etc/passwd\fP password database file
105 \fI/var/run/utmp\fP (traditionally \fI/etc/utmp\fP;
106 some libc versions used \fI/var/adm/utmp\fP)
109 POSIX.1. System V has a \fBcuserid\fP function which uses the real
110 user ID rather than the effective user ID. The \fBcuserid\fP function
111 was included in the 1988 version of POSIX, but removed from the 1990 version.
113 OpenBSD has \fBgetlogin\fP and \fBsetlogin\fP, and a username
114 associated with a session, even if it has no controlling tty.
116 Unfortunately, it is often rather easy to fool getlogin().
117 Sometimes it does not work at all, because some program messed up
118 the utmp file. Often, it gives only the first 8 characters of
119 the login name. The user currently logged in on the controlling tty
120 of our program need not be the user who started it.
121 Avoid getlogin() for security-related purposes.
123 Note that glibc does not follow the POSIX spec and uses stdin
126 A bug. (Other recent systems, like SunOS 5.8 and HPUX 11.11 and FreeBSD 4.8
127 all return the login name also when stdin is redirected.)
129 Nobody knows precisely what cuserid() does; avoid it in portable programs.
130 Or avoid it altogether: use getpwuid(geteuid()) instead, if that is
132 DO NOT USE cuserid().