Add tcsetsid(3).

The entire world seems to use the non-standard TIOCSCTTY ioctl to make a
TTY a controlling terminal of a session. Even though tcsetsid(3) is also
non-standard, I think it's a lot better to use in our own source code,
mainly because it's similar to tcsetpgrp(), tcgetpgrp() and tcgetsid().

I stole the idea from QNX. They do it the other way around; their
TIOCSCTTY is just a wrapper around tcsetsid(). tcsetsid() then calls
into an IPC framework.
This commit is contained in:
ed 2009-05-07 13:49:48 +00:00
parent e0ce6b415e
commit f8170e41fe
7 changed files with 118 additions and 7 deletions

View File

@ -70,8 +70,8 @@ MAN+= alarm.3 arc4random.3 \
siginterrupt.3 signal.3 sigsetops.3 sleep.3 \ siginterrupt.3 signal.3 sigsetops.3 sleep.3 \
statvfs.3 stringlist.3 \ statvfs.3 stringlist.3 \
strtofflags.3 sysconf.3 sysctl.3 syslog.3 tcgetpgrp.3 tcgetsid.3 \ strtofflags.3 sysconf.3 sysctl.3 syslog.3 tcgetpgrp.3 tcgetsid.3 \
tcsendbreak.3 tcsetattr.3 tcsetpgrp.3 time.3 times.3 timezone.3 \ tcsendbreak.3 tcsetattr.3 tcsetpgrp.3 tcsetsid.3 time.3 times.3 \
ttyname.3 tzset.3 ualarm.3 ucontext.3 ulimit.3 uname.3 \ timezone.3 ttyname.3 tzset.3 ualarm.3 ucontext.3 ulimit.3 uname.3 \
unvis.3 usleep.3 utime.3 valloc.3 vis.3 wordexp.3 unvis.3 usleep.3 utime.3 valloc.3 vis.3 wordexp.3
MLINKS+=arc4random.3 arc4random_addrandom.3 arc4random.3 arc4random_stir.3 \ MLINKS+=arc4random.3 arc4random_addrandom.3 arc4random.3 arc4random_stir.3 \

View File

@ -363,6 +363,7 @@ FBSD_1.1 {
posix_spawnattr_setsigmask; posix_spawnattr_setsigmask;
posix_spawnp; posix_spawnp;
tcgetsid; tcgetsid;
tcsetsid;
}; };
FBSDprivate_1.0 { FBSDprivate_1.0 {

View File

@ -63,7 +63,8 @@ is not the controlling terminal.
.Sh SEE ALSO .Sh SEE ALSO
.Xr getsid 2 , .Xr getsid 2 ,
.Xr setsid 2 , .Xr setsid 2 ,
.Xr tcgetpgrp 3 .Xr tcgetpgrp 3 ,
.Xr tcsetsid 3
.Sh STANDARDS .Sh STANDARDS
The The
.Fn tcgetsid .Fn tcgetsid

92
lib/libc/gen/tcsetsid.3 Normal file
View File

@ -0,0 +1,92 @@
.\" Copyright (c) 2009 Ed Schouten <ed@FreeBSD.org>
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\" notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\" notice, this list of conditions and the following disclaimer in the
.\" documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" $FreeBSD$
.\"
.Dd May 4, 2009
.Dt TCSETSID 3
.Os
.Sh NAME
.Nm tcsetsid
.Nd set session ID associated with a controlling terminal
.Sh LIBRARY
.Lb libc
.Sh SYNOPSIS
.In sys/types.h
.In termios.h
.Ft int
.Fn tcsetsid "int fd" "pid_t pid"
.Sh DESCRIPTION
The
.Fn tcsetsid
function sets associates a session identified by
.Fa pid
with a controlling terminal specified by
.Fa fd .
.Pp
This implementation only allows the controlling terminal to be changed
by the session leader itself.
This implies that
.Fa pid
always has to be equal to the process ID.
.Pp
It is unsupported to associate with a terminal that already has an
associated session.
Conversely, it is also unsupported to associate to a terminal when
the session is already associated with a different terminal.
.Sh ERRORS
If an error occurs,
.Fn tcsetsid
returns -1 and the global variable
.Va errno
is set to indicate the error, as follows:
.Bl -tag -width Er
.It Bq Er EBADF
The
.Fa fd
argument is not a valid file descriptor.
.It Bq Er ENOTTY
The file descriptor represented by
.Fa fd
is not a terminal.
.It Bq Er EINVAL
The
.Fa pid
argument is not equal to the session ID of the calling process.
.It Bq Er EPERM
The calling process is not a session leader.
.It Bq Er EPERM
The session already has an associated terminal or the terminal already
has an associated session.
.El
.Sh SEE ALSO
.Xr getsid 2 ,
.Xr setsid 2 ,
.Xr tcgetpgrp 3 ,
.Xr tcgetsid 3
.Sh HISTORY
A
.Fn tcsetsid
function first appeared in QNX.
It does not comply to any standard.

View File

@ -110,6 +110,18 @@ tcgetsid(int fd)
return ((pid_t)s); return ((pid_t)s);
} }
int
tcsetsid(int fd, pid_t pid)
{
if (pid != getsid(0)) {
errno = EINVAL;
return (-1);
}
return (_ioctl(fd, TIOCSCTTY, NULL));
}
speed_t speed_t
cfgetospeed(t) cfgetospeed(t)
const struct termios *t; const struct termios *t;

View File

@ -37,17 +37,21 @@ static char sccsid[] = "@(#)login_tty.c 8.1 (Berkeley) 6/4/93";
#endif /* LIBC_SCCS and not lint */ #endif /* LIBC_SCCS and not lint */
#include <sys/param.h> #include <sys/param.h>
#include <sys/ioctl.h>
#include <libutil.h> #include <libutil.h>
#include <stdlib.h> #include <stdlib.h>
#include <termios.h>
#include <unistd.h> #include <unistd.h>
int int
login_tty(int fd) login_tty(int fd)
{ {
(void) setsid(); pid_t s;
if (ioctl(fd, TIOCSCTTY, (char *)NULL) == -1)
s = setsid();
if (s == -1)
return (-1);
if (tcsetsid(fd, s) == -1)
return (-1); return (-1);
(void) dup2(fd, 0); (void) dup2(fd, 0);
(void) dup2(fd, 1); (void) dup2(fd, 1);

View File

@ -272,8 +272,9 @@ int tcsendbreak(int, int);
#if __POSIX_VISIBLE >= 200112 || __BSD_VISIBLE #if __POSIX_VISIBLE >= 200112 || __BSD_VISIBLE
pid_t tcgetsid(int); pid_t tcgetsid(int);
#endif #endif
#if __BSD_VISIBLE #if __BSD_VISIBLE
int tcsetsid(int, pid_t);
void cfmakeraw(struct termios *); void cfmakeraw(struct termios *);
int cfsetspeed(struct termios *, speed_t); int cfsetspeed(struct termios *, speed_t);
#endif #endif