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