]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/getlogin.3
385b97765229e0a7a26eb0e2d3904ae2d710a989
[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 2003-08-24 "Linux 2.4" "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 .SH DESCRIPTION
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.
47 .PP
48 \fBgetlogin_r\fP returns this same user name in the array
49 .I buf
50 of size
51 .IR bufsize .
52 .PP
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.
60 .PP
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.
64 .PP
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
68 involved.)
69 .PP
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.
73 .SH "RETURN VALUE"
74 \fBgetlogin\fP returns a pointer to the user name when successful,
75 and NULL on failure.
76 \fBgetlogin_r\fP returns 0 when successful, and non-zero on failure.
77 .SH ERRORS
78 POSIX specifies
79 .TP
80 .B EMFILE
81 The calling process already has the maximum allowed number of open files.
82 .TP
83 .B ENFILE
84 The system already has the maximum allowed number of open files.
85 .TP
86 .B ENXIO
87 The calling process has no controlling tty.
88 .TP
89 .B ERANGE
90 (getlogin_r)
91 The length of the user name, including final NUL, is larger than
92 .IR bufsize .
93 .LP
94 Linux/glibc also has
95 .TP
96 .B ENOENT
97 There was no corresponding entry in the utmp-file.
98 .TP
99 .B ENOMEM
100 Insufficient memory to allocate passwd structure.
101 .SH FILES
102 .nf
103 \fI/etc/passwd\fP password database file
104 .br
105 \fI/var/run/utmp\fP (traditionally \fI/etc/utmp\fP;
106 some libc versions used \fI/var/adm/utmp\fP)
107 .fi
108 .SH "CONFORMING TO"
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.
112 .LP
113 OpenBSD has \fBgetlogin\fP and \fBsetlogin\fP, and a username
114 associated with a session, even if it has no controlling tty.
115 .SH BUGS
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.
122 .LP
123 Note that glibc does not follow the POSIX spec and uses stdin
124 instead of
125 .IR /dev/tty .
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.)
128 .LP
129 Nobody knows precisely what cuserid() does; avoid it in portable programs.
130 Or avoid it altogether: use getpwuid(geteuid()) instead, if that is
131 what you meant.
132 DO NOT USE cuserid().
133 .SH "SEE ALSO"
134 .BR geteuid (2),
135 .BR getuid (2)