1.\" Copyright (c) 1989, 1991, 1993 2.\" The Regents of the University of California. All rights reserved. 3.\" 4.\" Redistribution and use in source and binary forms, with or without 5.\" modification, are permitted provided that the following conditions 6.\" are met: 7.\" 1. Redistributions of source code must retain the above copyright 8.\" notice, this list of conditions and the following disclaimer. 9.\" 2. Redistributions in binary form must reproduce the above copyright 10.\" notice, this list of conditions and the following disclaimer in the 11.\" documentation and/or other materials provided with the distribution. 12.\" 3. Neither the name of the University nor the names of its contributors 13.\" may be used to endorse or promote products derived from this software 14.\" without specific prior written permission. 15.\" 16.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 17.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 20.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26.\" SUCH DAMAGE. 27.\" 28.Dd September 9, 2020 29.Dt GETLOGIN 2 30.Os 31.Sh NAME 32.Nm getlogin , 33.Nm getlogin_r , 34.Nm setlogin 35.Nd get/set login name 36.Sh LIBRARY 37.Lb libc 38.Sh SYNOPSIS 39.In unistd.h 40.Ft char * 41.Fn getlogin void 42.In sys/param.h 43.Ft int 44.Fn getlogin_r "char *name" "size_t len" 45.Ft int 46.Fn setlogin "const char *name" 47.Sh DESCRIPTION 48The 49.Fn getlogin 50routine 51returns the login name of the user associated with the current session, 52as previously set by 53.Fn setlogin . 54The name is normally associated with a login shell 55at the time a session is created, 56and is inherited by all processes descended from the login shell. 57(This is true even if some of those processes assume another user ID, 58for example when 59.Xr su 1 60is used). 61.Pp 62The 63.Fn getlogin_r 64function 65provides the same service as 66.Fn getlogin 67except the caller must provide the buffer 68.Fa name 69with length 70.Fa len 71bytes 72to hold the result. 73The buffer should be at least 74.Dv MAXLOGNAME 75bytes in length. 76.Pp 77The 78.Fn setlogin 79system call 80sets the login name of the user associated with the current session to 81.Fa name . 82This system call is restricted to the super-user, and 83is normally used only when a new session is being created on behalf 84of the named user 85(for example, at login time, or when a remote shell is invoked). 86.Pp 87.Em NOTE : 88There is only one login name per session. 89.Pp 90It is 91.Em CRITICALLY 92important to ensure that 93.Fn setlogin 94is only ever called after the process has taken adequate steps to ensure 95that it is detached from its parent's session. 96Making a 97.Fn setsid 98system call is the 99.Em ONLY 100way to do this. 101The 102.Xr daemon 3 103function calls 104.Fn setsid 105which is an ideal way of detaching from a controlling terminal and 106forking into the background. 107.Pp 108In particular, doing a 109.Fn ioctl ttyfd TIOCNOTTY ...\& 110or 111.Fn setpgrp ...\& 112is 113.Em NOT 114sufficient. 115.Pp 116Once a parent process does a 117.Fn setsid 118system call, it is acceptable for some child of that process to then do a 119.Fn setlogin 120even though it is not the session leader, but beware that ALL processes 121in the session will change their login name at the same time, even the 122parent. 123.Pp 124This is not the same as the traditional UNIX behavior of inheriting privilege. 125.Pp 126Since the 127.Fn setlogin 128system call is restricted to the super-user, it is assumed that (like 129all other privileged programs) the programmer has taken adequate 130precautions to prevent security violations. 131.Sh RETURN VALUES 132If a call to 133.Fn getlogin 134succeeds, it returns a pointer to a null-terminated string in a static buffer, 135or 136.Dv NULL 137if the name has not been set. 138The 139.Fn getlogin_r 140function 141returns zero if successful, or the error number upon failure. 142.Pp 143.Rv -std setlogin 144.Sh ERRORS 145The following errors may be returned by these calls: 146.Bl -tag -width Er 147.It Bq Er EFAULT 148The 149.Fa name 150argument gave an 151invalid address. 152.It Bq Er EINVAL 153The 154.Fa name 155argument 156pointed to a string that was too long. 157Login names are limited to 158.Dv MAXLOGNAME 159(from 160.In sys/param.h ) 161characters, currently 33 including null. 162.It Bq Er EPERM 163The caller tried to set the login name and was not the super-user. 164.It Bq Er ERANGE 165The size of the buffer is smaller than the result to be returned. 166.El 167.Sh SEE ALSO 168.Xr setsid 2 , 169.Xr daemon 3 170.Sh STANDARDS 171The 172.Fn getlogin 173system call 174and 175the 176.Fn getlogin_r 177function 178conform to 179.St -p1003.1-96 . 180.Sh HISTORY 181The 182.Fn getlogin 183system call first appeared in 184.Bx 4.4 . 185The return value of 186.Fn getlogin_r 187was changed from earlier versions of 188.Fx 189to be conformant with 190.St -p1003.1-96 . 191.Sh BUGS 192In earlier versions of the system, 193.Fn getlogin 194failed unless the process was associated with a login terminal. 195The current implementation (using 196.Fn setlogin ) 197allows getlogin to succeed even when the process has no controlling terminal. 198In earlier versions of the system, the value returned by 199.Fn getlogin 200could not be trusted without checking the user ID. 201Portable programs should probably still make this check. 202