1GETLOGIN(3P)               POSIX Programmer's Manual              GETLOGIN(3P)
2
3
4

PROLOG

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
11

NAME

13       getlogin, getlogin_r — get login name
14

SYNOPSIS

16       #include <unistd.h>
17
18       char *getlogin(void);
19       int getlogin_r(char *name, size_t namesize);
20

DESCRIPTION

22       The getlogin() function shall return a pointer to a  string  containing
23       the  user  name  associated  by the login activity with the controlling
24       terminal of the current  process.  If  getlogin()  returns  a  non-null
25       pointer,  then  that pointer points to the name that the user logged in
26       under, even if there are several login names with the same user ID.
27
28       The getlogin() function need not be thread-safe.
29
30       The getlogin_r() function shall put the name associated  by  the  login
31       activity  with  the  controlling terminal of the current process in the
32       character array pointed to by name.  The array is  namesize  characters
33       long  and should have space for the name and the terminating null char‐
34       acter. The maximum size of the login name is {LOGIN_NAME_MAX}.
35
36       If getlogin_r() is successful, name points to the name the user used at
37       login, even if there are several login names with the same user ID.
38
39       The getlogin() and getlogin_r() functions may make use of file descrip‐
40       tors 0, 1, and 2 to  find  the  controlling  terminal  of  the  current
41       process, examining each in turn until the terminal is found. If in this
42       case none of these three file descriptors is open  to  the  controlling
43       terminal,  these functions may fail. The method used to find the termi‐
44       nal associated with a file descriptor may depend on the file descriptor
45       being open to the actual terminal device, not /dev/tty.
46

RETURN VALUE

48       Upon  successful  completion,  getlogin() shall return a pointer to the
49       login name or a null pointer if the user's login name cannot be  found.
50       Otherwise, it shall return a null pointer and set errno to indicate the
51       error.
52
53       The application shall not modify  the  string  returned.  The  returned
54       pointer might be invalidated or the string content might be overwritten
55       by a subsequent call to getlogin().
56
57       If successful, the getlogin_r() function shall return zero;  otherwise,
58       an error number shall be returned to indicate the error.
59

ERRORS

61       These functions may fail if:
62
63       EMFILE All  file  descriptors  available  to  the process are currently
64              open.
65
66       ENFILE The maximum allowable number of files is currently open  in  the
67              system.
68
69       ENOTTY None  of the file descriptors 0, 1, or 2 is open to the control‐
70              ling terminal of the current process.
71
72       ENXIO  The calling process has no controlling terminal.
73
74       The getlogin_r() function may fail if:
75
76       ERANGE The value of namesize is smaller than the length of  the  string
77              to be returned including the terminating null character.
78
79       The following sections are informative.
80

EXAMPLES

82   Getting the User Login Name S
83       The  following example calls the getlogin() function to obtain the name
84       of the user associated with the calling process, and passes this infor‐
85       mation  to  the getpwnam() function to get the associated user database
86       information.
87
88           #include <unistd.h>
89           #include <sys/types.h>
90           #include <pwd.h>
91           #include <stdio.h>
92           ...
93           char *lgn;
94           struct passwd *pw;
95           ...
96           if ((lgn = getlogin()) == NULL || (pw = getpwnam(lgn)) == NULL) {
97               fprintf(stderr, "Get of user information failed.\n"); exit(1);
98               }
99

APPLICATION USAGE

101       Three names associated with the current process can be determined: get‐
102       pwuid(geteuid())  shall  return  the name associated with the effective
103       user ID of the process; getlogin() shall  return  the  name  associated
104       with  the  current  login activity; and getpwuid(getuid()) shall return
105       the name associated with the real user ID of the process.
106
107       The getlogin_r() function is thread-safe and returns values in a  user-
108       supplied  buffer  instead of possibly using a static data area that may
109       be overwritten by each call.
110

RATIONALE

112       The getlogin() function returns a pointer to the user's login name. The
113       same  user ID may be shared by several login names. If it is desired to
114       get the user database entry that is used during login,  the  result  of
115       getlogin()  should  be  used  to provide the argument to the getpwnam()
116       function. (This might be used to determine the user's login shell, par‐
117       ticularly  where  a single user has multiple login shells with distinct
118       login names, but the same user ID.)
119
120       The information provided by the cuserid() function,  which  was  origi‐
121       nally  defined  in  the POSIX.1‐1988 standard and subsequently removed,
122       can be obtained by the following:
123
124           getpwuid(geteuid())
125
126       while  the  information  provided  by  historical  implementations   of
127       cuserid() can be obtained by:
128
129           getpwuid(getuid())
130
131       The  thread-safe  version  of  this  function places the user name in a
132       user-supplied buffer and returns a non-zero value if it fails. The non-
133       thread-safe  version may return the name in a static data area that may
134       be overwritten by each call.
135

FUTURE DIRECTIONS

137       None.
138

SEE ALSO

140       getpwnam(), getpwuid(), geteuid(), getuid()
141
142       The Base Definitions volume of POSIX.1‐2008, <limits.h>, <unistd.h>
143
145       Portions of this text are reprinted and reproduced in  electronic  form
146       from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology
147       -- Portable Operating System Interface (POSIX),  The  Open  Group  Base
148       Specifications Issue 7, Copyright (C) 2013 by the Institute of Electri‐
149       cal and Electronics Engineers,  Inc  and  The  Open  Group.   (This  is
150       POSIX.1-2008  with  the  2013  Technical Corrigendum 1 applied.) In the
151       event of any discrepancy between this version and the original IEEE and
152       The  Open Group Standard, the original IEEE and The Open Group Standard
153       is the referee document. The original Standard can be  obtained  online
154       at http://www.unix.org/online.html .
155
156       Any  typographical  or  formatting  errors that appear in this page are
157       most likely to have been introduced during the conversion of the source
158       files  to  man page format. To report such errors, see https://www.ker
159       nel.org/doc/man-pages/reporting_bugs.html .
160
161
162
163IEEE/The Open Group                  2013                         GETLOGIN(3P)
Impressum