1*62891Sbostic.\" Copyright (c) 1989, 1991, 1993 2*62891Sbostic.\" The Regents of the University of California. All rights reserved. 338066Skfall.\" 450487Scael.\" %sccs.include.redist.roff% 538066Skfall.\" 6*62891Sbostic.\" @(#)getlogin.2 8.1 (Berkeley) 06/09/93 738066Skfall.\" 847208Scael.Dd 947208Scael.Dt GETLOGIN 2 1047208Scael.Os BSD 4.2 1147208Scael.Sh NAME 1247208Scael.Nm getlogin , 1347208Scael.Nm setlogin 1447208Scael.Nd get/set login name 1547208Scael.Sh SYNOPSIS 1647208Scael.Fd #include <unistd.h> 1747208Scael.Ft char * 1847208Scael.Fn getlogin void 1947208Scael.Ft int 2047208Scael.Fn setlogin "const char *name" 2147208Scael.Sh DESCRIPTION 2243655SkarelsThe 2347208Scael.Fn getlogin 2443655Skarelsroutine 2543655Skarelsreturns the login name of the user associated with the current session, 2643655Skarelsas previously set by 2747208Scael.Fn setlogin . 2843655SkarelsThe name is normally associated with a login shell 2943655Skarelsat the time a session is created, 3043655Skarelsand is inherited by all processes descended from the login shell. 3143655Skarels(This is true even if some of those processes assume another user ID, 3243655Skarelsfor example when 3347208Scael.Xr su 1 3443655Skarelsis used.) 3547208Scael.Pp 3647208Scael.Fn Setlogin 3743655Skarelssets the login name of the user associated with the current session to 3847208Scael.Fa name . 3943655SkarelsThis call is restricted to the super-user, and 4043655Skarelsis normally used only when a new session is being created on behalf 4143655Skarelsof the named user 4243655Skarels(for example, at login time, or when a remote shell is invoked). 4347208Scael.Sh RETURN VALUES 4443655SkarelsIf a call to 4547208Scael.Fn getlogin 4643655Skarelssucceeds, it returns a pointer to a null-terminated string in a static buffer. 4750487ScaelIf the name has not been set, it returns 4850487Scael.Dv NULL . 4943655SkarelsIf a call to 5047208Scael.Fn setlogin 5143655Skarelssucceeds, a value of 0 is returned. If 5247208Scael.Fn setlogin 5347208Scaelfails, a value of -1 is returned and an error code is 5447208Scaelplaced in the global location 5547208Scael.Va errno . 5647208Scael.Sh ERRORS 5738066SkfallThe following errors may be returned by these calls: 5847208Scael.Bl -tag -width Er 5947208Scael.It Bq Er EFAULT 6047208ScaelThe 6147208Scael.Fa name 6247208Scaelparameter gave an 6338066Skfallinvalid address. 6447208Scael.It Bq Er EINVAL 6547208ScaelThe 6647208Scael.Fa name 6747208Scaelparameter 6843655Skarelspointed to a string that was too long. 6947208ScaelLogin names are limited to 7047208Scael.Dv MAXLOGNAME 7147208Scael(from 7247208Scael.Ao Pa sys/param.h Ac ) 7343655Skarelscharacters, currently 12. 7447208Scael.It Bq Er EPERM 7543655SkarelsThe caller tried to set the login name and was not the super-user. 7647208Scael.El 7747208Scael.Sh SEE ALSO 7847208Scael.Xr setsid 2 7947208Scael.Sh BUGS 8043655SkarelsLogin names are limited in length by 8147208Scael.Fn setlogin . 8243655SkarelsHowever, lower limits are placed on login names elsewhere in the system 8347208Scael.Pf ( Dv UT_NAMESIZE 8447208Scaelin 8547208Scael.Ao Pa utmp.h Ac ) . 8647208Scael.Pp 8743655SkarelsIn earlier versions of the system, 8847208Scael.Fn getlogin 8943655Skarelsfailed unless the process was associated with a login terminal. 9043655SkarelsThe current implementation (using 9147208Scael.Fn setlogin ) 9243655Skarelsallows getlogin to succeed even when the process has no controlling terminal. 9343655SkarelsIn earlier versions of the system, the value returned by 9447208Scael.Fn getlogin 9543655Skarelscould not be trusted without checking the user ID. 9643655SkarelsPortable programs should probably still make this check. 9747208Scael.Sh HISTORY 9847208ScaelThe 9962890Sbostic.Fn getlogin 10062890Sbosticfunction first appeared in 4.4BSD. 101