From f8170e41fe131d129edab0370716adf666f67cea Mon Sep 17 00:00:00 2001 From: ed Date: Thu, 7 May 2009 13:49:48 +0000 Subject: 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. --- lib/libc/gen/Makefile.inc | 4 +-- lib/libc/gen/Symbol.map | 1 + lib/libc/gen/tcgetsid.3 | 3 +- lib/libc/gen/tcsetsid.3 | 92 +++++++++++++++++++++++++++++++++++++++++++++++ lib/libc/gen/termios.c | 12 +++++++ 5 files changed, 109 insertions(+), 3 deletions(-) create mode 100644 lib/libc/gen/tcsetsid.3 (limited to 'lib/libc') diff --git a/lib/libc/gen/Makefile.inc b/lib/libc/gen/Makefile.inc index 0ee2ffb..a4badb9 100644 --- a/lib/libc/gen/Makefile.inc +++ b/lib/libc/gen/Makefile.inc @@ -70,8 +70,8 @@ MAN+= alarm.3 arc4random.3 \ siginterrupt.3 signal.3 sigsetops.3 sleep.3 \ statvfs.3 stringlist.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 \ - ttyname.3 tzset.3 ualarm.3 ucontext.3 ulimit.3 uname.3 \ + tcsendbreak.3 tcsetattr.3 tcsetpgrp.3 tcsetsid.3 time.3 times.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 MLINKS+=arc4random.3 arc4random_addrandom.3 arc4random.3 arc4random_stir.3 \ diff --git a/lib/libc/gen/Symbol.map b/lib/libc/gen/Symbol.map index 69cf703..2d23153 100644 --- a/lib/libc/gen/Symbol.map +++ b/lib/libc/gen/Symbol.map @@ -363,6 +363,7 @@ FBSD_1.1 { posix_spawnattr_setsigmask; posix_spawnp; tcgetsid; + tcsetsid; }; FBSDprivate_1.0 { diff --git a/lib/libc/gen/tcgetsid.3 b/lib/libc/gen/tcgetsid.3 index 11c8d0c..a270a0b 100644 --- a/lib/libc/gen/tcgetsid.3 +++ b/lib/libc/gen/tcgetsid.3 @@ -63,7 +63,8 @@ is not the controlling terminal. .Sh SEE ALSO .Xr getsid 2 , .Xr setsid 2 , -.Xr tcgetpgrp 3 +.Xr tcgetpgrp 3 , +.Xr tcsetsid 3 .Sh STANDARDS The .Fn tcgetsid diff --git a/lib/libc/gen/tcsetsid.3 b/lib/libc/gen/tcsetsid.3 new file mode 100644 index 0000000..d0f1d98 --- /dev/null +++ b/lib/libc/gen/tcsetsid.3 @@ -0,0 +1,92 @@ +.\" Copyright (c) 2009 Ed Schouten +.\" 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. diff --git a/lib/libc/gen/termios.c b/lib/libc/gen/termios.c index 4c6dcff..85ca4e3 100644 --- a/lib/libc/gen/termios.c +++ b/lib/libc/gen/termios.c @@ -110,6 +110,18 @@ tcgetsid(int fd) 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 cfgetospeed(t) const struct termios *t; -- cgit v1.1