stat-util: also add stat_verify_char() and use it everywhere
This commit is contained in:
@@ -225,6 +225,21 @@ int fd_verify_block(int fd) {
|
||||
return verify_stat_at(fd, /* path= */ NULL, /* follow= */ false, stat_verify_block, /* verify= */ true);
|
||||
}
|
||||
|
||||
int stat_verify_char(const struct stat *st) {
|
||||
assert(st);
|
||||
|
||||
if (S_ISDIR(st->st_mode))
|
||||
return -EISDIR;
|
||||
|
||||
if (S_ISLNK(st->st_mode))
|
||||
return -ELOOP;
|
||||
|
||||
if (!S_ISCHR(st->st_mode))
|
||||
return -EBADFD;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int stat_verify_device_node(const struct stat *st) {
|
||||
assert(st);
|
||||
|
||||
|
||||
@@ -31,6 +31,8 @@ int fd_verify_linked(int fd);
|
||||
int stat_verify_block(const struct stat *st);
|
||||
int fd_verify_block(int fd);
|
||||
|
||||
int stat_verify_char(const struct stat *st);
|
||||
|
||||
int stat_verify_device_node(const struct stat *st);
|
||||
int is_device_node(const char *path);
|
||||
|
||||
|
||||
@@ -1861,6 +1861,8 @@ int terminal_set_cursor_position(int fd, unsigned row, unsigned column) {
|
||||
}
|
||||
|
||||
static int terminal_verify_same(int input_fd, int output_fd) {
|
||||
int r;
|
||||
|
||||
assert(input_fd >= 0);
|
||||
assert(output_fd >= 0);
|
||||
|
||||
@@ -1871,15 +1873,17 @@ static int terminal_verify_same(int input_fd, int output_fd) {
|
||||
if (fstat(input_fd, &sti) < 0)
|
||||
return -errno;
|
||||
|
||||
if (!S_ISCHR(sti.st_mode)) /* TTYs are character devices */
|
||||
return -ENOTTY;
|
||||
r = stat_verify_char(&sti); /* TTYs are character devices */
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
struct stat sto;
|
||||
if (fstat(output_fd, &sto) < 0)
|
||||
return -errno;
|
||||
|
||||
if (!S_ISCHR(sto.st_mode))
|
||||
return -ENOTTY;
|
||||
r = stat_verify_char(&sto);
|
||||
if (r < 0)
|
||||
return r;
|
||||
|
||||
if (sti.st_rdev != sto.st_rdev)
|
||||
return -ENOLINK;
|
||||
|
||||
Reference in New Issue
Block a user