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