xref: /freebsd-14.2/lib/libc/sys/getlogin.2 (revision b2c76c41)
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.\"	@(#)getlogin.2	8.1 (Berkeley) 6/9/93
29.\"
30.Dd September 9, 2020
31.Dt GETLOGIN 2
32.Os
33.Sh NAME
34.Nm getlogin ,
35.Nm getlogin_r ,
36.Nm setlogin
37.Nd get/set login name
38.Sh LIBRARY
39.Lb libc
40.Sh SYNOPSIS
41.In unistd.h
42.Ft char *
43.Fn getlogin void
44.In sys/param.h
45.Ft int
46.Fn getlogin_r "char *name" "size_t len"
47.Ft int
48.Fn setlogin "const char *name"
49.Sh DESCRIPTION
50The
51.Fn getlogin
52routine
53returns the login name of the user associated with the current session,
54as previously set by
55.Fn setlogin .
56The name is normally associated with a login shell
57at the time a session is created,
58and is inherited by all processes descended from the login shell.
59(This is true even if some of those processes assume another user ID,
60for example when
61.Xr su 1
62is used).
63.Pp
64The
65.Fn getlogin_r
66function
67provides the same service as
68.Fn getlogin
69except the caller must provide the buffer
70.Fa name
71with length
72.Fa len
73bytes
74to hold the result.
75The buffer should be at least
76.Dv MAXLOGNAME
77bytes in length.
78.Pp
79The
80.Fn setlogin
81system call
82sets the login name of the user associated with the current session to
83.Fa name .
84This system call is restricted to the super-user, and
85is normally used only when a new session is being created on behalf
86of the named user
87(for example, at login time, or when a remote shell is invoked).
88.Pp
89.Em NOTE :
90There is only one login name per session.
91.Pp
92It is
93.Em CRITICALLY
94important to ensure that
95.Fn setlogin
96is only ever called after the process has taken adequate steps to ensure
97that it is detached from its parent's session.
98Making a
99.Fn setsid
100system call is the
101.Em ONLY
102way to do this.
103The
104.Xr daemon 3
105function calls
106.Fn setsid
107which is an ideal way of detaching from a controlling terminal and
108forking into the background.
109.Pp
110In particular, doing a
111.Fn ioctl ttyfd TIOCNOTTY ...\&
112or
113.Fn setpgrp ...\&
114is
115.Em NOT
116sufficient.
117.Pp
118Once a parent process does a
119.Fn setsid
120system call, it is acceptable for some child of that process to then do a
121.Fn setlogin
122even though it is not the session leader, but beware that ALL processes
123in the session will change their login name at the same time, even the
124parent.
125.Pp
126This is not the same as the traditional UNIX behavior of inheriting privilege.
127.Pp
128Since the
129.Fn setlogin
130system call is restricted to the super-user, it is assumed that (like
131all other privileged programs) the programmer has taken adequate
132precautions to prevent security violations.
133.Sh RETURN VALUES
134If a call to
135.Fn getlogin
136succeeds, it returns a pointer to a null-terminated string in a static buffer,
137or
138.Dv NULL
139if the name has not been set.
140The
141.Fn getlogin_r
142function
143returns zero if successful, or the error number upon failure.
144.Pp
145.Rv -std setlogin
146.Sh ERRORS
147The following errors may be returned by these calls:
148.Bl -tag -width Er
149.It Bq Er EFAULT
150The
151.Fa name
152argument gave an
153invalid address.
154.It Bq Er EINVAL
155The
156.Fa name
157argument
158pointed to a string that was too long.
159Login names are limited to
160.Dv MAXLOGNAME
161(from
162.In sys/param.h )
163characters, currently 33 including null.
164.It Bq Er EPERM
165The caller tried to set the login name and was not the super-user.
166.It Bq Er ERANGE
167The size of the buffer is smaller than the result to be returned.
168.El
169.Sh SEE ALSO
170.Xr setsid 2 ,
171.Xr daemon 3
172.Sh STANDARDS
173The
174.Fn getlogin
175system call
176and
177the
178.Fn getlogin_r
179function
180conform to
181.St -p1003.1-96 .
182.Sh HISTORY
183The
184.Fn getlogin
185system call first appeared in
186.Bx 4.4 .
187The return value of
188.Fn getlogin_r
189was changed from earlier versions of
190.Fx
191to be conformant with
192.St -p1003.1-96 .
193.Sh BUGS
194In earlier versions of the system,
195.Fn getlogin
196failed unless the process was associated with a login terminal.
197The current implementation (using
198.Fn setlogin )
199allows getlogin to succeed even when the process has no controlling terminal.
200In earlier versions of the system, the value returned by
201.Fn getlogin
202could not be trusted without checking the user ID.
203Portable programs should probably still make this check.
204