1GETLOGIN(3) Linux Programmer's Manual GETLOGIN(3)
2
3
4
6 getlogin, getlogin_r, cuserid - get user name
7
9 #include <unistd.h>
10
11 char *getlogin(void);
12 int getlogin_r(char *buf, size_t bufsize);
13
14 #include <stdio.h>
15
16 char *cuserid(char *string);
17
19 getlogin() returns a pointer to a string containing the name of the
20 user logged in on the controlling terminal of the process, or a null
21 pointer if this information cannot be determined. The string is stati‐
22 cally allocated and might be overwritten on subsequent calls to this
23 function or to cuserid().
24
25 getlogin_r() returns this same user name in the array buf of size buf‐
26 size.
27
28 cuserid() returns a pointer to a string containing a user name associ‐
29 ated with the effective user ID of the process. If string is not a
30 null pointer, it should be an array that can hold at least L_cuserid
31 characters; the string is returned in this array. Otherwise, a pointer
32 to a string in a static area is returned. This string is statically
33 allocated and might be overwritten on subsequent calls to this function
34 or to getlogin().
35
36 The macro L_cuserid is an integer constant that indicates how long an
37 array you might need to store a user name. L_cuserid is declared in
38 stdio.h.
39
40 These functions let your program identify positively the user who is
41 running (cuserid()) or the user who logged in this session (getlo‐
42 gin()). (These can differ when set-user-ID programs are involved.)
43
44 For most purposes, it is more useful to use the environment variable
45 LOGNAME to find out who the user is. This is more flexible precisely
46 because the user can set LOGNAME arbitrarily.
47
49 getlogin() returns a pointer to the user name when successful, and NULL
50 on failure. getlogin_r() returns 0 when successful, and non-zero on
51 failure.
52
54 POSIX specifies
55
56 EMFILE The calling process already has the maximum allowed number of
57 open files.
58
59 ENFILE The system already has the maximum allowed number of open files.
60
61 ENXIO The calling process has no controlling tty.
62
63 ERANGE (getlogin_r) The length of the user name, including the termi‐
64 nating null byte, is larger than bufsize.
65
66 Linux/glibc also has
67
68 ENOENT There was no corresponding entry in the utmp-file.
69
70 ENOMEM Insufficient memory to allocate passwd structure.
71
73 /etc/passwd password database file
74 /var/run/utmp (traditionally /etc/utmp;
75 some libc versions used /var/adm/utmp)
76
78 getlogin() and getlogin_r() specified in POSIX.1-2001.
79
80 System V has a cuserid() function which uses the real user ID rather
81 than the effective user ID. The cuserid() function was included in the
82 1988 version of POSIX, but removed from the 1990 version. It was
83 present in SUSv2, but removed in POSIX.1-2001.
84
85 OpenBSD has getlogin() and setlogin(), and a username associated with a
86 session, even if it has no controlling tty.
87
89 Unfortunately, it is often rather easy to fool getlogin(). Sometimes
90 it does not work at all, because some program messed up the utmp file.
91 Often, it gives only the first 8 characters of the login name. The user
92 currently logged in on the controlling tty of our program need not be
93 the user who started it. Avoid getlogin() for security-related pur‐
94 poses.
95
96 Note that glibc does not follow the POSIX spec and uses stdin instead
97 of /dev/tty. A bug. (Other recent systems, like SunOS 5.8 and HP-UX
98 11.11 and FreeBSD 4.8 all return the login name also when stdin is
99 redirected.)
100
101 Nobody knows precisely what cuserid() does; avoid it in portable pro‐
102 grams. Or avoid it altogether: use getpwuid(geteuid()) instead, if
103 that is what you meant. DO NOT USE cuserid().
104
106 geteuid(2), getuid(2)
107
108
109
110Linux 2.4 2003-08-24 GETLOGIN(3)