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