Skip to content

Commit 2fc7807

Browse files
committed
sudo_term_is_raw: only try to lock the fd if it is a tty
This moves sudo_isatty() to libsudo_util so sudo_term_is_raw() can use it. Fixes GitHub issue #335 --HG-- branch : 1.9
1 parent 6ef5310 commit 2fc7807

File tree

6 files changed

+42
-31
lines changed

6 files changed

+42
-31
lines changed

include/sudo_util.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,8 @@ extern int (*sudo_printf)(int msg_type, const char * restrict fmt, ...);
325325
/* term.c */
326326
#define SUDO_TERM_ISIG 0x01U
327327
#define SUDO_TERM_OFLAG 0x02U
328+
sudo_dso_public bool sudo_isatty_v1(int fd, struct stat *sbp);
329+
#define sudo_isatty(_a, _b) sudo_isatty_v1((_a), (_b))
328330
sudo_dso_public bool sudo_term_cbreak_v1(int fd);
329331
#define sudo_term_cbreak(_a) sudo_term_cbreak_v1((_a))
330332
sudo_dso_public bool sudo_term_copy_v1(int src, int dst);

lib/util/term.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <config.h>
2525

2626
#include <sys/ioctl.h>
27+
#include <sys/stat.h>
2728
#include <stdio.h>
2829
#include <stdlib.h>
2930
#include <string.h>
@@ -286,6 +287,9 @@ sudo_term_is_raw_v1(int fd)
286287
struct termios term = { 0 };
287288
debug_decl(sudo_term_is_raw, SUDO_DEBUG_UTIL);
288289

290+
if (!sudo_isatty(fd, NULL))
291+
debug_return_bool(false);
292+
289293
sudo_lock_file(fd, SUDO_LOCK);
290294
if (tcgetattr(fd, &term) == -1) {
291295
sudo_debug_printf(SUDO_DEBUG_ERROR|SUDO_DEBUG_ERRNO,
@@ -457,3 +461,31 @@ sudo_term_copy_v1(int src, int dst)
457461
sudo_lock_file(src, SUDO_UNLOCK);
458462
debug_return_bool(ret);
459463
}
464+
465+
/*
466+
* Like isatty(3) but stats the fd and stores the result in sb.
467+
* Only calls isatty(3) if fd is a character special device.
468+
* Returns true if a tty, else returns false and sets errno.
469+
*/
470+
bool
471+
sudo_isatty_v1(int fd, struct stat *sbp)
472+
{
473+
bool ret = false;
474+
struct stat sb;
475+
debug_decl(sudo_isatty, SUDO_DEBUG_EXEC);
476+
477+
if (sbp == NULL)
478+
sbp = &sb;
479+
480+
if (fstat(fd, sbp) == 0) {
481+
if (!S_ISCHR(sbp->st_mode)) {
482+
errno = ENOTTY;
483+
} else {
484+
ret = isatty(fd) == 1;
485+
}
486+
} else if (sbp != &sb) {
487+
/* Always initialize sbp. */
488+
memset(sbp, 0, sizeof(*sbp));
489+
}
490+
debug_return_bool(ret);
491+
}

lib/util/ttysize.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,13 @@ get_ttysize_ioctl(int fd, int *rowp, int *colp)
3939
struct winsize wsize;
4040
debug_decl(get_ttysize_ioctl, SUDO_DEBUG_UTIL);
4141

42-
if (fd != -1 && isatty(fd) && ioctl(fd, TIOCGWINSZ, &wsize) == 0) {
43-
if (wsize.ws_row != 0 && wsize.ws_col != 0) {
44-
*rowp = wsize.ws_row;
45-
*colp = wsize.ws_col;
46-
debug_return_int(0);
42+
if (fd != -1 && sudo_isatty(fd, NULL)) {
43+
if (ioctl(fd, TIOCGWINSZ, &wsize) == 0) {
44+
if (wsize.ws_row != 0 && wsize.ws_col != 0) {
45+
*rowp = wsize.ws_row;
46+
*colp = wsize.ws_col;
47+
debug_return_int(0);
48+
}
4749
}
4850
}
4951
debug_return_int(-1);

lib/util/util.exp.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ sudo_gettime_awake_v1
9696
sudo_gettime_mono_v1
9797
sudo_gettime_real_v1
9898
sudo_hexchar_v1
99+
sudo_isatty_v1
99100
sudo_json_add_value_as_object_v1
100101
sudo_json_add_value_v1
101102
sudo_json_close_array_v1

src/sudo.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -316,9 +316,7 @@ char *getenv_unhooked(const char *name);
316316
/* interfaces.c */
317317
int get_net_ifs(char **addrinfo);
318318

319-
/* ttyname.c */
320319
char *get_process_ttyname(char *name, size_t namelen);
321-
bool sudo_isatty(int fd, struct stat *sb);
322320

323321
/* signal.c */
324322
struct sigaction;

src/ttyname.c

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -376,27 +376,3 @@ get_process_ttyname(char *name, size_t namelen)
376376
debug_return_str(NULL);
377377
}
378378
#endif
379-
380-
/*
381-
* Like isatty(3) but stats the fd and stores the result in sb.
382-
* Only calls isatty(3) if fd is a character special device.
383-
* Returns true if a tty, else returns false and sets errno.
384-
*/
385-
bool
386-
sudo_isatty(int fd, struct stat *sb)
387-
{
388-
bool ret = false;
389-
debug_decl(sudo_isatty, SUDO_DEBUG_EXEC);
390-
391-
if (fstat(fd, sb) == 0) {
392-
if (!S_ISCHR(sb->st_mode)) {
393-
errno = ENOTTY;
394-
} else {
395-
ret = isatty(fd) == 1;
396-
}
397-
} else {
398-
/* Always initialize sb. */
399-
memset(sb, 0, sizeof(*sb));
400-
}
401-
debug_return_bool(ret);
402-
}

0 commit comments

Comments
 (0)