Re: add a "notty" flag to ttys(5) for init(8)

"Greg A. Woods" <[email protected]> Mon, 19 May 2025 14:42:05 -0700
Newsgroups gmane.os.netbsd.devel.userlevel
Organization Planix, Inc.
Message-ID <[email protected]>
At Sat, 17 May 2025 08:41:32 +0000 (UTC), RVP <[email protected]> wrote:
Subject: Re: add a "notty" flag to ttys(5) for init(8)
>
> On Fri, 18 Apr 2025, Greg A. Woods wrote:
>
> > I figured the best way would be to run it from init(8) so that it would
> > be running at all times while logins are allowed, and so that init(8)
> > could restart it should it ever die.
> >
> > The problem was that monit, like most programs, doesn't want arbitrary
> > arguments on its command line, and the normal way init(8) starts a
> > "getty" is to explicitly pass the terminal name as an additional
> > argument.  I could have named the session "-I", but that's UGLY.
> >
>
> You can already do this with an empty `tty' field:

That arguably exposes a bug in getttyent()!

So, yes, you could do that, though perhaps only for one entry, but I
think it would be very wrong.  It effectively "breaks" getttynam() for
one.  It may make the [wu]tmpx files a bit messy too.

I propose the following fix, though note it could expose further bugs in
callers of getttyent() and/or getttynam():

Index: lib/libc/gen/getttyent.c
===================================================================
RCS file: /cvs/master/m-NetBSD/main/src/lib/libc/gen/getttyent.c,v
retrieving revision 1.26
diff -u -r1.26 getttyent.c
--- lib/libc/gen/getttyent.c	30 Jun 2013 10:07:43 -0000	1.26
+++ lib/libc/gen/getttyent.c	19 May 2025 19:13:32 -0000
@@ -100,8 +100,10 @@
 		errno = 0;
 		line = fparseln(tf, &len, &lineno, NULL, FPARSELN_UNESCALL);
 		if (line == NULL) {
-			if (errno != 0)
-				warn(__func__);
+			if (errno != 0) {
+				warn("%s: %s, %lu",
+				     __func__, _PATH_TTYS, (unsigned long)lineno);
+			}
 			return NULL;
 		}
 		for (p = line; *p && isspace((unsigned char)*p); p++)
@@ -111,7 +113,22 @@
 		free(line);
 	}

-	tty.ty_name = p;
+	if (*p == '\0') {
+		warnx("%s: %s, %lu: no tty name!",
+		      __func__, _PATH_TTYS, (unsigned long)lineno);
+#if 0
+		/*
+		 * XXX arguably this is an error in the entry, but the manual says:
+		 *
+		 *     "If any of the fields pointing to character strings are
+		 *     unspecified, they are returned as null pointers."
+		 */
+		return NULL;
+#else
+		tty.ty_name = NULL;
+#endif
+	} else
+		tty.ty_name = p;
 	p = skip(p, &zapchar);
 	if (*(tty.ty_getty = p) == '\0')
 		tty.ty_getty = tty.ty_type = NULL;

--
					Greg A. Woods <[email protected]>

Kelowna, BC     +1 250 762-7675           RoboHack <[email protected]>
Planix, Inc. <[email protected]>     Avoncote Farms <[email protected]>
signature.asc (application/pgp-signature, 195 B)
-----BEGIN PGP SIGNATURE-----

iF0EABECAB0WIQRuK6dmwVAucmRxuh9mfXG3eL/0fwUCaCulpAAKCRBmfXG3eL/0
f95TAKCwTlI/6j3f7Jg6O4HiENZqhqWYlACeNriNJb5RuTUgtfErZwhxqdcX0/Q=
=y/mN
-----END PGP SIGNATURE-----