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 int getlogin_r(char *name, size_t namesize);
19
21 The getlogin() function shall return a pointer to a string containing
22 the user name associated by the login activity with the controlling
23 terminal of the current process. If getlogin() returns a non-null
24 pointer, then that pointer points to the name that the user logged in
25 under, even if there are several login names with the same user ID.
26
27 The getlogin() function need not be thread-safe.
28
29 The getlogin_r() function shall put the name associated by the login
30 activity with the controlling terminal of the current process in the
31 character array pointed to by name. The array is namesize characters
32 long and should have space for the name and the terminating null char‐
33 acter. The maximum size of the login name is {LOGIN_NAME_MAX}.
34
35 If getlogin_r() is successful, name points to the name the user used at
36 login, even if there are several login names with the same user ID.
37
38 The getlogin() and getlogin_r() functions may make use of file descrip‐
39 tors 0, 1, and 2 to find the controlling terminal of the current
40 process, examining each in turn until the terminal is found. If in this
41 case none of these three file descriptors is open to the controlling
42 terminal, these functions may fail. The method used to find the termi‐
43 nal associated with a file descriptor may depend on the file descriptor
44 being open to the actual terminal device, not /dev/tty.
45
47 Upon successful completion, getlogin() shall return a pointer to the
48 login name or a null pointer if the user's login name cannot be found.
49 Otherwise, it shall return a null pointer and set errno to indicate the
50 error.
51
52 The application shall not modify the string returned. The returned
53 pointer might be invalidated or the string content might be overwritten
54 by a subsequent call to getlogin(). The returned pointer and the
55 string content might also be invalidated if the calling thread is ter‐
56 minated.
57
58 If successful, the getlogin_r() function shall return zero; otherwise,
59 an error number shall be returned to indicate the error.
60
62 These functions may fail if:
63
64 EMFILE All file descriptors available to the process are currently
65 open.
66
67 ENFILE The maximum allowable number of files is currently open in the
68 system.
69
70 ENOTTY None of the file descriptors 0, 1, or 2 is open to the control‐
71 ling terminal of the current process.
72
73 ENXIO The calling process has no controlling terminal.
74
75 The getlogin_r() function may fail if:
76
77 ERANGE The value of namesize is smaller than the length of the string
78 to be returned including the terminating null character.
79
80 The following sections are informative.
81
83 Getting the User Login Name S
84 The following example calls the getlogin() function to obtain the name
85 of the user associated with the calling process, and passes this infor‐
86 mation to the getpwnam() function to get the associated user database
87 information.
88
89
90 #include <unistd.h>
91 #include <sys/types.h>
92 #include <pwd.h>
93 #include <stdio.h>
94 ...
95 char *lgn;
96 struct passwd *pw;
97 ...
98 if ((lgn = getlogin()) == NULL || (pw = getpwnam(lgn)) == NULL) {
99 fprintf(stderr, "Get of user information failed.\n"); exit(1);
100 }
101
103 Three names associated with the current process can be determined: get‐
104 pwuid(geteuid()) shall return the name associated with the effective
105 user ID of the process; getlogin() shall return the name associated
106 with the current login activity; and getpwuid(getuid()) shall return
107 the name associated with the real user ID of the process.
108
109 The getlogin_r() function is thread-safe and returns values in a user-
110 supplied buffer instead of possibly using a static data area that may
111 be overwritten by each call.
112
114 The getlogin() function returns a pointer to the user's login name. The
115 same user ID may be shared by several login names. If it is desired to
116 get the user database entry that is used during login, the result of
117 getlogin() should be used to provide the argument to the getpwnam()
118 function. (This might be used to determine the user's login shell, par‐
119 ticularly where a single user has multiple login shells with distinct
120 login names, but the same user ID.)
121
122 The information provided by the cuserid() function, which was origi‐
123 nally defined in the POSIX.1‐1988 standard and subsequently removed,
124 can be obtained by the following:
125
126
127 getpwuid(geteuid())
128
129 while the information provided by historical implementations of
130 cuserid() can be obtained by:
131
132
133 getpwuid(getuid())
134
135 The thread-safe version of this function places the user name in a
136 user-supplied buffer and returns a non-zero value if it fails. The non-
137 thread-safe version may return the name in a static data area that may
138 be overwritten by each call.
139
141 None.
142
144 getpwnam(), getpwuid(), geteuid(), getuid()
145
146 The Base Definitions volume of POSIX.1‐2017, <limits.h>, <unistd.h>
147
149 Portions of this text are reprinted and reproduced in electronic form
150 from IEEE Std 1003.1-2017, Standard for Information Technology -- Por‐
151 table Operating System Interface (POSIX), The Open Group Base Specifi‐
152 cations Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of
153 Electrical and Electronics Engineers, Inc and The Open Group. In the
154 event of any discrepancy between this version and the original IEEE and
155 The Open Group Standard, the original IEEE and The Open Group Standard
156 is the referee document. The original Standard can be obtained online
157 at http://www.opengroup.org/unix/online.html .
158
159 Any typographical or formatting errors that appear in this page are
160 most likely to have been introduced during the conversion of the source
161 files to man page format. To report such errors, see https://www.ker‐
162 nel.org/doc/man-pages/reporting_bugs.html .
163
164
165
166IEEE/The Open Group 2017 GETLOGIN(3P)