Check if the ttymaj/ttymin match is actually a tty

This commit is contained in:
sin 2015-01-16 16:02:25 +00:00
parent a5c30abf8f
commit 8a95422ff1
1 changed files with 12 additions and 3 deletions

View File

@ -2,6 +2,7 @@
#include <sys/types.h> #include <sys/types.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <fcntl.h>
#include <dirent.h> #include <dirent.h>
#include <limits.h> #include <limits.h>
#include <stdio.h> #include <stdio.h>
@ -25,6 +26,7 @@ ttytostr(int tty_maj, int tty_min, char *str, size_t n)
struct dirent *dp; struct dirent *dp;
DIR *dirp; DIR *dirp;
char path[PATH_MAX]; char path[PATH_MAX];
int fd;
switch (tty_maj) { switch (tty_maj) {
case 136: case 136:
@ -60,14 +62,21 @@ ttytostr(int tty_maj, int tty_min, char *str, size_t n)
} }
if (stat(path, &sb) < 0) { if (stat(path, &sb) < 0) {
weprintf("stat %s:", dp->d_name); weprintf("stat %s:", path);
return -1; return -1;
} }
if ((int)major(sb.st_rdev) == tty_maj && if ((int)major(sb.st_rdev) == tty_maj &&
(int)minor(sb.st_rdev) == tty_min) { (int)minor(sb.st_rdev) == tty_min) {
strlcpy(str, dp->d_name, n); fd = open(path, O_RDONLY);
break; if (fd < 0)
continue;
if (isatty(fd)) {
strlcpy(str, dp->d_name, n);
close(fd);
break;
}
close(fd);
} }
} }