[PATCH] cu/tip leaves tty in exclusive mode
Bruce M. Simpson
bms at FreeBSD.org
Tue Apr 29 15:12:57 UTC 2008
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
-------------- next part --------------
#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);
}
More information about the freebsd-current
mailing list