BSD 4_3_Net_2 release
[unix-history] / usr / src / lib / libc / sys / getlogin.2
CommitLineData
931b8415 1.\" Copyright (c) 1989, 1991 The Regents of the University of California.
1cde5d8e
KF
2.\" All rights reserved.
3.\"
af359dea
C
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. All advertising materials mentioning features or use of this software
13.\" must display the following acknowledgement:
14.\" This product includes software developed by the University of
15.\" California, Berkeley and its contributors.
16.\" 4. Neither the name of the University nor the names of its contributors
17.\" may be used to endorse or promote products derived from this software
18.\" without specific prior written permission.
1cde5d8e 19.\"
af359dea
C
20.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30.\" SUCH DAMAGE.
1cde5d8e 31.\"
af359dea
C
32.\" @(#)getlogin.2 6.3 (Berkeley) 7/23/91
33.\"
34.Dd July 23, 1991
931b8415
CL
35.Dt GETLOGIN 2
36.Os BSD 4.2
37.Sh NAME
38.Nm getlogin ,
39.Nm setlogin
40.Nd get/set login name
41.Sh SYNOPSIS
42.Fd #include <unistd.h>
43.Ft char *
44.Fn getlogin void
45.Ft int
46.Fn setlogin "const char *name"
47.Sh DESCRIPTION
d79c3924 48The
931b8415 49.Fn getlogin
d79c3924
MK
50routine
51returns the login name of the user associated with the current session,
52as previously set by
931b8415 53.Fn setlogin .
d79c3924
MK
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
931b8415 59.Xr su 1
d79c3924 60is used.)
931b8415
CL
61.Pp
62.Fn Setlogin
d79c3924 63sets the login name of the user associated with the current session to
931b8415 64.Fa name .
d79c3924
MK
65This call is restricted to the super-user, and
66is normally used only when a new session is being created on behalf
67of the named user
68(for example, at login time, or when a remote shell is invoked).
931b8415 69.Sh RETURN VALUES
d79c3924 70If a call to
931b8415 71.Fn getlogin
d79c3924 72succeeds, it returns a pointer to a null-terminated string in a static buffer.
af359dea
C
73If the name has not been set, it returns
74.Dv NULL .
d79c3924 75If a call to
931b8415 76.Fn setlogin
d79c3924 77succeeds, a value of 0 is returned. If
931b8415
CL
78.Fn setlogin
79fails, a value of -1 is returned and an error code is
80placed in the global location
81.Va errno .
82.Sh ERRORS
1cde5d8e 83The following errors may be returned by these calls:
931b8415
CL
84.Bl -tag -width Er
85.It Bq Er EFAULT
86The
87.Fa name
88parameter gave an
1cde5d8e 89invalid address.
931b8415
CL
90.It Bq Er EINVAL
91The
92.Fa name
93parameter
d79c3924 94pointed to a string that was too long.
931b8415
CL
95Login names are limited to
96.Dv MAXLOGNAME
97(from
98.Ao Pa sys/param.h Ac )
d79c3924 99characters, currently 12.
931b8415 100.It Bq Er EPERM
d79c3924 101The caller tried to set the login name and was not the super-user.
931b8415
CL
102.El
103.Sh SEE ALSO
104.Xr setsid 2
105.Sh BUGS
d79c3924 106Login names are limited in length by
931b8415 107.Fn setlogin .
d79c3924 108However, lower limits are placed on login names elsewhere in the system
931b8415
CL
109.Pf ( Dv UT_NAMESIZE
110in
111.Ao Pa utmp.h Ac ) .
112.Pp
d79c3924 113In earlier versions of the system,
931b8415 114.Fn getlogin
d79c3924
MK
115failed unless the process was associated with a login terminal.
116The current implementation (using
931b8415 117.Fn setlogin )
d79c3924
MK
118allows getlogin to succeed even when the process has no controlling terminal.
119In earlier versions of the system, the value returned by
931b8415 120.Fn getlogin
d79c3924
MK
121could not be trusted without checking the user ID.
122Portable programs should probably still make this check.
931b8415
CL
123.Sh HISTORY
124The
af359dea
C
125.Nm getlogin
126function call is
127.Ud .