svn commit: r191882 - in head: lib/libc/gen lib/libutil sys/sys

Ed Schouten ed at FreeBSD.org
Thu May 7 13:49:52 UTC 2009


Author: ed
Date: Thu May  7 13:49:48 2009
New Revision: 191882
URL: http://svn.freebsd.org/changeset/base/191882

Log:
  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.

Added:
  head/lib/libc/gen/tcsetsid.3   (contents, props changed)
Modified:
  head/lib/libc/gen/Makefile.inc
  head/lib/libc/gen/Symbol.map
  head/lib/libc/gen/tcgetsid.3
  head/lib/libc/gen/termios.c
  head/lib/libutil/login_tty.c
  head/sys/sys/termios.h

Modified: head/lib/libc/gen/Makefile.inc
==============================================================================
--- head/lib/libc/gen/Makefile.inc	Thu May  7 13:08:26 2009	(r191881)
+++ head/lib/libc/gen/Makefile.inc	Thu May  7 13:49:48 2009	(r191882)
@@ -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 \

Modified: head/lib/libc/gen/Symbol.map
==============================================================================
--- head/lib/libc/gen/Symbol.map	Thu May  7 13:08:26 2009	(r191881)
+++ head/lib/libc/gen/Symbol.map	Thu May  7 13:49:48 2009	(r191882)
@@ -363,6 +363,7 @@ FBSD_1.1 {
 	posix_spawnattr_setsigmask;
 	posix_spawnp;
 	tcgetsid;
+	tcsetsid;
 };
 
 FBSDprivate_1.0 {

Modified: head/lib/libc/gen/tcgetsid.3
==============================================================================
--- head/lib/libc/gen/tcgetsid.3	Thu May  7 13:08:26 2009	(r191881)
+++ head/lib/libc/gen/tcgetsid.3	Thu May  7 13:49:48 2009	(r191882)
@@ -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

Added: head/lib/libc/gen/tcsetsid.3
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/lib/libc/gen/tcsetsid.3	Thu May  7 13:49:48 2009	(r191882)
@@ -0,0 +1,92 @@
+.\" Copyright (c) 2009 Ed Schouten <ed at 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.

Modified: head/lib/libc/gen/termios.c
==============================================================================
--- head/lib/libc/gen/termios.c	Thu May  7 13:08:26 2009	(r191881)
+++ head/lib/libc/gen/termios.c	Thu May  7 13:49:48 2009	(r191882)
@@ -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;

Modified: head/lib/libutil/login_tty.c
==============================================================================
--- head/lib/libutil/login_tty.c	Thu May  7 13:08:26 2009	(r191881)
+++ head/lib/libutil/login_tty.c	Thu May  7 13:49:48 2009	(r191882)
@@ -37,17 +37,21 @@ static char sccsid[] = "@(#)login_tty.c	
 #endif /* LIBC_SCCS and not lint */
 
 #include <sys/param.h>
-#include <sys/ioctl.h>
 
 #include <libutil.h>
 #include <stdlib.h>
+#include <termios.h>
 #include <unistd.h>
 
 int
 login_tty(int fd)
 {
-	(void) setsid();
-	if (ioctl(fd, TIOCSCTTY, (char *)NULL) == -1)
+	pid_t s;
+
+	s = setsid();
+	if (s == -1)
+		return (-1);
+	if (tcsetsid(fd, s) == -1)
 		return (-1);
 	(void) dup2(fd, 0);
 	(void) dup2(fd, 1);

Modified: head/sys/sys/termios.h
==============================================================================
--- head/sys/sys/termios.h	Thu May  7 13:08:26 2009	(r191881)
+++ head/sys/sys/termios.h	Thu May  7 13:49:48 2009	(r191882)
@@ -272,8 +272,9 @@ int	tcsendbreak(int, int);
 #if __POSIX_VISIBLE >= 200112 || __BSD_VISIBLE
 pid_t	tcgetsid(int);
 #endif
-
 #if __BSD_VISIBLE
+int	tcsetsid(int, pid_t);
+
 void	cfmakeraw(struct termios *);
 int	cfsetspeed(struct termios *, speed_t);
 #endif


More information about the svn-src-head mailing list