Subject: Re: On /dev/console, /dev/constty and the TIOCCONS ioctl
To: <>
From: David Laight <david@l8s.co.uk>
List: tech-kern
Date: 10/04/2003 10:26:22
> ... and post a diff.

See below:
The cons.c change is that required to revert to the old behaviour,
I also added cn_redirect() to localise the constty redirect.

If the change had been larger I wouldn't have committed it.

The fundamental problem I was trying to solve is that you can't
have use /dev/console as a terminal and support TIOCCONS redirecting
console output elsewhere.  NetBSD, as shipped, tries to allow both.

There are basically 2 solutions:
1) Stop TIOCCONS redirecting /dev/console and require programs use (say)
   /dev/conslog to write 'console' messages.
2) Don't run terminal sessions on /dev/console, requiring either:
   a) The terminal session be run on /dev/constty
   b) The terminal session be run on the underlying tty device (eg /dev/ttyE0)

Since terminal sessions run on multiple devices, it seems less problematical
to change the terminal device.

There are other problems in this code, some just don't bear thinking about [1].
When someone finally writes devfs /dev/constty can be made a synonym for the
real tty, and many of these problems will disappear.

	David

[1] there are null pointer possibilities in the ctty TIOCCONS area.

Index: cons.c
===================================================================
RCS file: /cvsroot/src/sys/dev/cons.c,v
retrieving revision 1.50
diff -u -p -r1.50 cons.c
--- cons.c	2003/10/03 13:15:52	1.50
+++ cons.c	2003/10/04 08:54:45
@@ -110,19 +110,14 @@ const struct cdevsw cons_cdevsw = {
 
 struct	tty *constty = NULL;	/* virtual console output device */
 struct	consdev *cn_tab;	/* physical console device info */
-struct	vnode *cn_devvp[2];	/* vnode for underlying device. */
+struct	vnode *cn_devvp;	/* vnode for underlying device. */
 
 int
 cnopen(dev_t dev, int flag, int mode, struct proc *p)
 {
 	const struct cdevsw *cdev;
 	dev_t cndev;
-	int unit;
 
-	unit = minor(dev);
-	if (unit > 1)
-		return ENODEV;
-
 	if (cn_tab == NULL)
 		return (0);
 
@@ -153,9 +148,9 @@ cnopen(dev_t dev, int flag, int mode, st
 	if (cdev == NULL)
 		return (ENXIO);
 
-	if (cn_devvp[unit] == NULLVP) {
+	if (cn_devvp == NULLVP) {
 		/* try to get a reference on its vnode, but fail silently */
-		cdevvp(cndev, &cn_devvp[unit]);
+		cdevvp(cndev, &cn_devvp);
 	}
 	return ((*cdev->d_open)(cndev, flag, mode, p));
 }
@@ -165,9 +160,6 @@ cnclose(dev_t dev, int flag, int mode, s
 {
 	const struct cdevsw *cdev;
 	struct vnode *vp;
-	int unit;
-
-	unit = minor(dev);
 
 	if (cn_tab == NULL)
 		return (0);
@@ -181,10 +173,10 @@ cnclose(dev_t dev, int flag, int mode, s
 	cdev = cdevsw_lookup(dev);
 	if (cdev == NULL)
 		return (ENXIO);
-	if (cn_devvp[unit] != NULLVP) {
+	if (cn_devvp != NULLVP) {
 		/* release our reference to real dev's vnode */
-		vrele(cn_devvp[unit]);
-		cn_devvp[unit] = NULLVP;
+		vrele(cn_devvp);
+		cn_devvp = NULLVP;
 	}
 	if (vfinddev(dev, VCHR, &vp) && vcount(vp))
 		return (0);
@@ -414,7 +406,7 @@ cn_redirect(dev_t *devp, int is_read, in
 	 * If there's no real console, return ENXIO.
 	 */
 	*error = ENXIO;
-	if (constty != NULL && minor(dev) == 0 &&
+	if (constty != NULL &&
 	    (cn_tab == NULL || (cn_tab->cn_pri != CN_REMOTE ))) {
 		if (is_read) {
 			*error = 0;
Index: init.c
===================================================================
RCS file: /cvsroot/src/sbin/init/init.c,v
retrieving revision 1.62
retrieving revision 1.63
diff -u -p -r1.62 -r1.63
--- init.c	2003/09/11 12:51:51	1.62
+++ init.c	2003/10/03 13:31:32	1.63
@@ -1,4 +1,4 @@
-/*	$NetBSD: init.c,v 1.62 2003/09/11 12:51:51 dsl Exp $	*/
+/*	$NetBSD: init.c,v 1.63 2003/10/03 13:31:32 dsl Exp $	*/
 
 /*-
  * Copyright (c) 1991, 1993
@@ -42,7 +42,7 @@ __COPYRIGHT("@(#) Copyright (c) 1991, 19
 #if 0
 static char sccsid[] = "@(#)init.c	8.2 (Berkeley) 4/28/95";
 #else
-__RCSID("$NetBSD: init.c,v 1.62 2003/09/11 12:51:51 dsl Exp $");
+__RCSID("$NetBSD: init.c,v 1.63 2003/10/03 13:31:32 dsl Exp $");
 #endif
 #endif /* not lint */
 
@@ -577,7 +577,10 @@ single_user(void)
 		/*
 		 * Start the single user session.
 		 */
-		setctty(_PATH_CONSOLE);
+		if (access(_PATH_CONSTTY, F_OK) == 0)
+			setctty(_PATH_CONSTTY);
+		else
+			setctty(_PATH_CONSOLE);
 
 #ifdef SECURE
 		/*
Index: MAKEDEV
===================================================================
RCS file: /cvsroot/src/etc/etc.i386/MAKEDEV,v
retrieving revision 1.206
retrieving revision 1.207
diff -u -p -r1.206 -r1.207
--- MAKEDEV	2003/09/19 11:11:50	1.206
+++ MAKEDEV	2003/10/03 14:35:37	1.207
@@ -1,6 +1,6 @@
 #!/bin/sh -
 #
-#	$NetBSD: MAKEDEV,v 1.206 2003/09/19 11:11:50 pooka Exp $
+#	$NetBSD: MAKEDEV,v 1.207 2003/10/03 14:35:37 dsl Exp $
 #
 # Copyright (c) 1990 The Regents of the University of California.
 # All rights reserved.
@@ -264,8 +264,9 @@ isdns)
 	;;
 
 std)
-	rm -f console drum mem kmem null zero io klog ksyms
+	rm -f console constty drum mem kmem null zero io klog ksyms
 	mknod console		c 0 0
+	mknod constty		c 0 1
 	mknod drum		c 4 0	; chmod 640 drum ; chgrp kmem drum
 	mknod kmem		c 2 1	; chmod 640 kmem ; chgrp kmem kmem
 	mknod mem		c 2 0	; chmod 640 mem	; chgrp kmem mem
Index: ttys
===================================================================
RCS file: /cvsroot/src/etc/etc.i386/ttys,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -p -r1.16 -r1.17
--- ttys	2003/10/03 14:54:21	1.16
+++ ttys	2003/10/04 07:41:12	1.17
@@ -1,10 +1,10 @@
 #
 #	from: @(#)ttys	5.1 (Berkeley) 4/17/89
-#	$NetBSD: ttys,v 1.16 2003/10/03 14:54:21 dsl Exp $
+#	$NetBSD: ttys,v 1.17 2003/10/04 07:41:12 dsl Exp $
 #
 # name	getty				type	status		comments
 #
-constty	"/usr/libexec/getty Pc"		vt100	on secure
+console	"/usr/libexec/getty Pc"		vt100	on secure
 ttyE0	"/usr/libexec/getty Pc"		vt220	off secure
 ttyE1	"/usr/libexec/getty Pc"		vt220	off secure
 ttyE2	"/usr/libexec/getty Pc"		vt220	off secure
Index: paths.h
===================================================================
RCS file: /cvsroot/src/include/paths.h,v
retrieving revision 1.24
retrieving revision 1.25
diff -u -p -r1.24 -r1.25
--- paths.h	2003/08/07 09:44:10	1.24
+++ paths.h	2003/10/03 13:17:12	1.25
@@ -1,4 +1,4 @@
-/*	$NetBSD: paths.h,v 1.24 2003/08/07 09:44:10 agc Exp $	*/
+/*	$NetBSD: paths.h,v 1.25 2003/10/03 13:17:12 dsl Exp $	*/
 
 /*
  * Copyright (c) 1989, 1993
@@ -56,6 +56,7 @@
 #define	_PATH_BSHELL	"/bin/sh"
 #define	_PATH_CLOCKCTL	"/dev/clockctl"
 #define	_PATH_CONSOLE	"/dev/console"
+#define	_PATH_CONSTTY	"/dev/constty"
 #define	_PATH_CSHELL	"/bin/csh"
 #define	_PATH_DEFTAPE	"/dev/nrst0"
 #define	_PATH_DEVDB	"/var/run/dev.db"


-- 
David Laight: david@l8s.co.uk