Re: [PATCH] cu/tip leaves tty in exclusive mode

From: Bruce M. Simpson <bms_at_FreeBSD.org>
Date: Tue, 29 Apr 2008 16:12:54 +0100
Bruce M Simpson wrote:
> Hi,
>
> I think I saw a bug. I have an app which creates ptys on its own. I 
> connect to them manually with cu -l.
>
> It appears that cu doesn't clean up after itself... this is a quick fix

Here's another way of fixing it. Perhaps this should go in tools for now?

Only the superuser can clear the TS_XCLUDE bit according to kern/tty.c.

There doesn't seem to be any way of resetting the TIONXCL flag on a tty 
without rebooting -- there's no sysctl or anything else, stty doesn't 
know about it, and it does not have a TERMIOS flag.

cheers
BMS

#include <fcntl.h>
#include <sys/ioctl.h>
#include <errno.h>
#include <err.h>
#include <libgen.h>
#include <sysexits.h>
#include <unistd.h>

#define DEVPATHNAME "/dev"

int
main(int argc, char *argv[])
{
	char *progname;
	char *ttyname;
	int fd;
	int dofree;

	dofree = 0;

	progname = basename(argv[0]);
	if (argc != 2)
		errx(EX_USAGE, "usage: %s <ttyname>\n", progname);
	if (geteuid() != 0)
		errx(EX_NOPERM, "Sorry\n");

	if (argv[1][0] == '/') {
		ttyname = argv[1];
	} else {
		size_t len, maxpath, result;

		len = strlen(argv[1]) + sizeof(DEVPATHNAME) + 1;

		maxpath = pathconf(DEVPATHNAME, _PC_PATH_MAX);
		if (len > maxpath) {
			warnc(ENAMETOOLONG, ttyname);
			exit(EX_DATAERR);
		}

		ttyname = malloc(len);
		if (ttyname == NULL) {
			warnc(ENOMEM, "malloc");
			exit(EX_OSERR);
		}
		dofree = 1;

		result = snprintf(ttyname, len, "%s/%s", DEVPATHNAME, argv[1]);
		if (result >= len)
			warnc(ENOMEM, "snprintf");
	}

	fd = open(ttyname, O_RDWR);
	if (fd == -1) {
		warnc(errno, "open %s", ttyname);
		if (dofree)
			free(ttyname);
		exit(EX_OSERR);
	}

	if (0 != ioctl(fd, TIOCNXCL, 0))
		warnc(errno, "ioctl TIOCNXCL %s", ttyname);

	if (dofree)
		free(ttyname);
	exit(0);
}
Received on Tue Apr 29 2008 - 13:12:57 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:39:30 UTC