From 3877a90e69b9fa3bc11ee3e2c1b29a1dc3841ea3 Mon Sep 17 00:00:00 2001 From: Kamil Rytarowski Date: Sat, 9 Sep 2017 20:21:31 +0200 Subject: [PATCH] netbsd: implement uv__tty_is_slave() NetBSD as an extension returns with ptsname(3) and ptsname_r(3) the slave device name for both descriptors, the master one and slave one. Workaround this problem and verify the device major and compare it with the pts driver. Major numbers for the master and the slave TTY are machine-dependent. On amd64 they are respectively 6 (ptc) and 5 (pts). PR-URL: https://github.com/libuv/libuv/pull/1533 Reviewed-By: Ben Noordhuis --- src/unix/tty.c | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/unix/tty.c b/src/unix/tty.c index b2d37f4c..357f9748 100644 --- a/src/unix/tty.c +++ b/src/unix/tty.c @@ -48,6 +48,42 @@ static int uv__tty_is_slave(const int fd) { char dummy[256]; result = ioctl(fd, TIOCPTYGNAME, &dummy) != 0; +#elif defined(__NetBSD__) + /* + * NetBSD as an extension returns with ptsname(3) and ptsname_r(3) the slave + * device name for both descriptors, the master one and slave one. + * + * Implement function to compare major device number with pts devices. + * + * The major numbers are machine-dependent, on NetBSD/amd64 they are + * respectively: + * - master tty: ptc - major 6 + * - slave tty: pts - major 5 + */ + + struct stat sb; + /* Lookup device's major for the pts driver and cache it. */ + static devmajor_t pts = NODEVMAJOR; + + if (pts == NODEVMAJOR) { + pts = getdevmajor("pts", S_IFCHR); + if (pts == NODEVMAJOR) + abort(); + } + + /* Lookup stat structure behind the file descriptor. */ + if (fstat(fd, &sb) != 0) + abort(); + + /* Assert character device. */ + if (!S_ISCHR(sb.st_mode)) + abort(); + + /* Assert valid major. */ + if (major(sb.st_rdev) == NODEVMAJOR) + abort(); + + result = (pts == major(sb.st_rdev)); #else /* Fallback to ptsname */