1GETLOGIN(3P) POSIX Programmer's Manual GETLOGIN(3P)
2
3
4
6 This manual page is part of the POSIX Programmer's Manual. The Linux
7 implementation of this interface may differ (consult the corresponding
8 Linux manual page for details of Linux behavior), or the interface may
9 not be implemented on Linux.
10
12 getlogin, getlogin_r - get login name
13
15 #include <unistd.h>
16
17 char *getlogin(void);
18
19
20 int getlogin_r(char *name, size_t namesize);
21
22
24 The getlogin() function shall return a pointer to a string containing
25 the user name associated by the login activity with the controlling
26 terminal of the current process. If getlogin() returns a non-null
27 pointer, then that pointer points to the name that the user logged in
28 under, even if there are several login names with the same user ID.
29
30 The getlogin() function need not be reentrant. A function that is not
31 required to be reentrant is not required to be thread-safe.
32
33 The getlogin_r() function shall put the name associated by the login
34 activity with the controlling terminal of the current process in the
35 character array pointed to by name. The array is namesize characters
36 long and should have space for the name and the terminating null char‐
37 acter. The maximum size of the login name is {LOGIN_NAME_MAX}.
38
39 If getlogin_r() is successful, name points to the name the user used at
40 login, even if there are several login names with the same user ID.
41
43 Upon successful completion, getlogin() shall return a pointer to the
44 login name or a null pointer if the user's login name cannot be found.
45 Otherwise, it shall return a null pointer and set errno to indicate the
46 error.
47
48 The return value from getlogin() may point to static data whose content
49 is overwritten by each call.
50
51 If successful, the getlogin_r() function shall return zero; otherwise,
52 an error number shall be returned to indicate the error.
53
55 The getlogin() and getlogin_r() functions may fail if:
56
57 EMFILE {OPEN_MAX} file descriptors are currently open in the calling
58 process.
59
60 ENFILE The maximum allowable number of files is currently open in the
61 system.
62
63 ENXIO The calling process has no controlling terminal.
64
65
66 The getlogin_r() function may fail if:
67
68 ERANGE The value of namesize is smaller than the length of the string
69 to be returned including the terminating null character.
70
71
72 The following sections are informative.
73
75 Getting the User Login Name
76 The following example calls the getlogin() function to obtain the name
77 of the user associated with the calling process, and passes this infor‐
78 mation to the getpwnam() function to get the associated user database
79 information.
80
81
82 #include <unistd.h>
83 #include <sys/types.h>
84 #include <pwd.h>
85 #include <stdio.h>
86 ...
87 char *lgn;
88 struct passwd *pw;
89 ...
90 if ((lgn = getlogin()) == NULL || (pw = getpwnam(lgn)) == NULL) {
91 fprintf(stderr, "Get of user information failed.\n"); exit(1);
92 }
93
95 Three names associated with the current process can be determined: get‐
96 pwuid( geteuid()) shall return the name associated with the effective
97 user ID of the process; getlogin() shall return the name associated
98 with the current login activity; and getpwuid( getuid()) shall return
99 the name associated with the real user ID of the process.
100
101 The getlogin_r() function is thread-safe and returns values in a user-
102 supplied buffer instead of possibly using a static data area that may
103 be overwritten by each call.
104
106 The getlogin() function returns a pointer to the user's login name. The
107 same user ID may be shared by several login names. If it is desired to
108 get the user database entry that is used during login, the result of
109 getlogin() should be used to provide the argument to the getpwnam()
110 function. (This might be used to determine the user's login shell, par‐
111 ticularly where a single user has multiple login shells with distinct
112 login names, but the same user ID.)
113
114 The information provided by the cuserid() function, which was origi‐
115 nally defined in the POSIX.1-1988 standard and subsequently removed,
116 can be obtained by the following:
117
118
119 getpwuid(geteuid())
120
121 while the information provided by historical implementations of
122 cuserid() can be obtained by:
123
124
125 getpwuid(getuid())
126
127 The thread-safe version of this function places the user name in a
128 user-supplied buffer and returns a non-zero value if it fails. The non-
129 thread-safe version may return the name in a static data area that may
130 be overwritten by each call.
131
133 None.
134
136 getpwnam(), getpwuid(), geteuid(), getuid(), the Base Definitions vol‐
137 ume of IEEE Std 1003.1-2001, <limits.h>, <unistd.h>
138
140 Portions of this text are reprinted and reproduced in electronic form
141 from IEEE Std 1003.1, 2003 Edition, Standard for Information Technology
142 -- Portable Operating System Interface (POSIX), The Open Group Base
143 Specifications Issue 6, Copyright (C) 2001-2003 by the Institute of
144 Electrical and Electronics Engineers, Inc and The Open Group. In the
145 event of any discrepancy between this version and the original IEEE and
146 The Open Group Standard, the original IEEE and The Open Group Standard
147 is the referee document. The original Standard can be obtained online
148 at http://www.opengroup.org/unix/online.html .
149
150
151
152IEEE/The Open Group 2003 GETLOGIN(3P)