1 GETLOGIN(3C) Standard C Library Functions GETLOGIN(3C) 2 3 4 5 NAME 6 getlogin, getlogin_r - get login name 7 8 SYNOPSIS 9 #include <unistd.h> 10 11 char *getlogin(void); 12 13 14 char *getlogin_r(char *name, int namelen); 15 16 17 Standard conforming 18 cc [ flag ... ] file... -D_POSIX_PTHREAD_SEMANTICS [ library ... ] 19 20 int getlogin_r(char *name, size_t namesize); 21 22 23 DESCRIPTION 24 The getlogin() function returns a pointer to the login name as found in 25 /var/adm/utmpx. It can be used in conjunction with getpwnam(3C) to 26 locate the correct password file entry when the same user ID is shared 27 by several login names. 28 29 30 The login name plus the terminating null byte can be up to 33 31 characters in length. Newly-compiled programs should use the 32 LOGIN_NAME_MAX symbol, defined in <limits.h>, to size the buffer. 33 Older programs that call getlogin() expect only the legacy 9-character 34 length. These automatically link to a version of the getlogin() 35 functions that truncates longer login names. It's also possible to 36 compile new programs that link to truncating versions of these 37 functions by defining __USE_LEGACY_LOGNAME__ in the compile 38 environment. 39 40 41 Some older programs will correctly handle long login names returned by 42 the getlogin() function. For this case, the user compatibility library 43 /usr/lib/getloginx.so.1 redirects to a version of the getlogin() 44 function that returns the long name. This library should be added to 45 such an application at runtime using LD_PRELOAD. 46 47 48 If getlogin() is called within a process that is not attached to a 49 terminal, it returns a null pointer. The correct procedure for 50 determining the login name is to call cuserid(3C), or to call 51 getlogin() and if it fails to call getpwuid(3C). 52 53 54 The getlogin_r() function has the same functionality as getlogin() 55 except that the caller must supply a buffer name with length namelen to 56 store the result. The name buffer should be at least LOGIN_NAME_MAX 57 bytes in size (defined in <limits.h>). The POSIX version (see 58 standards(5)) of getlogin_r() takes a namesize parameter of type 59 size_t. If the size of the supplied buffer is less than the size of 60 LOGIN_NAME_MAX and the name, including the null terminator, does not 61 fit inside the buffer, than an error will be generated. Otherwise, the 62 buffer name will be updated with the login name. 63 64 65 RETURN VALUES 66 Upon successful completion, getlogin() returns a pointer to the login 67 name or a null pointer if the user's login name cannot be found. 68 Otherwise it returns a null pointer and sets errno to indicate the 69 error. 70 71 72 The standard-conforming getlogin_r() returns 0 if successful, or the 73 error number upon failure. 74 75 ERRORS 76 The getlogin_r() function will fail if: 77 78 ERANGE 79 The size of the buffer is smaller than the result to be 80 returned. 81 82 83 EINVAL 84 And entry for the current user was not found in the 85 /var/adm/utmpx file. 86 87 88 89 The getlogin() and getlogin_r() functions may fail if: 90 91 EMFILE 92 There are {OPEN_MAX} file descriptors currently open in the 93 calling process. 94 95 96 ENFILE 97 The maximum allowable number of files is currently open in 98 the system. 99 100 101 ENXIO 102 The calling process has no controlling terminal. 103 104 105 106 The getlogin_r() function may fail if: 107 108 ERANGE 109 The size of the buffer is smaller than the result to be 110 returned. 111 112 113 USAGE 114 The return value of getlogin() points to thread-specific data whose 115 content is overwritten on each call by the same thread. 116 117 118 Three names associated with the current process can be determined: 119 getpwuid(geteuid()) returns the name associated with the effective user 120 ID of the process; getlogin() returns the name associated with the 121 current login activity; and getpwuid(getuid()) returns the name 122 associated with the real user ID of the process. 123 124 FILES 125 /var/adm/utmpx 126 user access and administration information 127 128 129 /usr/lib/getloginx.so.1 130 A compatibility library that returns long login names 131 to older applications. 132 133 134 /usr/lib/64/getloginx.so.1 135 A 64-bit compatibility library to return long login 136 names. 137 138 139 ATTRIBUTES 140 See attributes(5) for descriptions of the following attributes: 141 142 143 144 145 +--------------------+-----------------+ 146 | ATTRIBUTE TYPE | ATTRIBUTE VALUE | 147 +--------------------+-----------------+ 148 |Interface Stability | Standard | 149 +--------------------+-----------------+ 150 |MT-Level | See below. | 151 +--------------------+-----------------+ 152 153 SEE ALSO 154 geteuid(2), getuid(2), cuserid(3C), getgrnam(3C), getpwnam(3C), 155 getpwuid(3C), utmpx(4), attributes(5), standards(5) 156 157 NOTES 158 When compiling multithreaded programs, see Intro(3). 159 160 161 The getlogin() function is safe to use in multithreaded applications, 162 but is discouraged. The getlogin_r() function should be used instead. 163 164 165 Solaris 2.4 and earlier releases provided a getlogin_r() as specified 166 in POSIX.1c Draft 6. The final POSIX.1c standard changed the interface 167 as described above. Support for the Draft 6 interface is provided for 168 compatibility only and may not be supported in future releases. New 169 applications and libraries should use the standard-conforming 170 interface. 171 172 173 174 March 15, 2014 GETLOGIN(3C)