Skip to content

Commit

Permalink
Use NODEV macro instead of explicit (dev_t)-1.
Browse files Browse the repository at this point in the history
Also fix an assignment of dev_t to -1 that should be NODEV.
Bug #1074.
  • Loading branch information
millert committed Nov 28, 2024
1 parent 73cbe4e commit d5028a0
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 19 deletions.
6 changes: 5 additions & 1 deletion include/sudo_compat.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* SPDX-License-Identifier: ISC
*
* Copyright (c) 1996, 1998-2005, 2008, 2009-2023
* Copyright (c) 1996, 1998-2005, 2008, 2009-2024
* Todd C. Miller <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
Expand Down Expand Up @@ -40,6 +40,10 @@
* Macros and functions that may be missing on some operating systems.
*/

#ifndef NODEV
# define NODEV ((dev_t)-1) /* non-existent device */
#endif

/*
* Given the pointer x to the member m of the struct s, return
* a pointer to the containing structure.
Expand Down
2 changes: 1 addition & 1 deletion lib/util/ttyname_dev.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* SPDX-License-Identifier: ISC
*
* Copyright (c) 2012-2018 Todd C. Miller <[email protected]>
* Copyright (c) 2012-2018, 2024 Todd C. Miller <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
Expand Down
4 changes: 2 additions & 2 deletions plugins/sudoers/policy.c
Original file line number Diff line number Diff line change
Expand Up @@ -398,7 +398,7 @@ sudoers_policy_deserialize_info(struct sudoers_context *ctx, void *v,
ctx->user.gid = (gid_t)-1;
ctx->user.uid = (gid_t)-1;
ctx->user.umask = (mode_t)-1;
ctx->user.ttydev = (dev_t)-1;
ctx->user.ttydev = NODEV;
for (cur = info->user_info; *cur != NULL; cur++) {
if (MATCHES(*cur, "user=")) {
CHECK(*cur, "user=");
Expand Down Expand Up @@ -597,7 +597,7 @@ sudoers_policy_deserialize_info(struct sudoers_context *ctx, void *v,
}

/* ttydev is only set in user_info[] for API 1.22 and above. */
if (ctx->user.ttydev == (dev_t)-1 && ctx->user.ttypath != NULL) {
if (ctx->user.ttydev == NODEV && ctx->user.ttypath != NULL) {
struct stat sb;
if (stat(ctx->user.ttypath, &sb) == 0)
ctx->user.ttydev = sb.st_rdev;
Expand Down
4 changes: 2 additions & 2 deletions plugins/sudoers/timestamp.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* SPDX-License-Identifier: ISC
*
* Copyright (c) 2014-2023 Todd C. Miller <[email protected]>
* Copyright (c) 2014-2024 Todd C. Miller <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
Expand Down Expand Up @@ -397,7 +397,7 @@ ts_init_key(const struct sudoers_context *ctx,
sudo_warnx("unknown time stamp ticket type %d", ticket_type);
FALLTHROUGH;
case tty:
if (ctx->user.ttydev != (dev_t)-1) {
if (ctx->user.ttydev != NODEV) {
/* tty-based time stamp */
entry->type = TS_TTY;
entry->u.ttydev = ctx->user.ttydev;
Expand Down
4 changes: 2 additions & 2 deletions src/regress/ttyname/check_ttyname.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ main(int argc, char *argv[])
char *tty_libc = NULL, *tty_sudo = NULL;
char pathbuf[PATH_MAX];
bool verbose = false;
dev_t ttydev = -1;
dev_t ttydev = NODEV;
int ch, errors = 0, ntests = 1;

initprogname(argc > 0 ? argv[0] : "check_ttyname");
Expand All @@ -87,7 +87,7 @@ main(int argc, char *argv[])

/* Lookup tty name using kernel info if possible. */
ttydev = get_process_ttyname(pathbuf, sizeof(pathbuf));
if (ttydev != (dev_t)-1) {
if (ttydev != NODEV) {
char numbuf[STRLEN_MAX_SIGNED(long long) + 1];
const char *errstr;
dev_t newdev;
Expand Down
2 changes: 1 addition & 1 deletion src/sudo.c
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ get_user_info(struct user_details *ud)
}

ttydev = get_process_ttyname(path, sizeof(path));
if (ttydev != (dev_t)-1) {
if (ttydev != NODEV) {
if (asprintf(&info[++i], "ttydev=%lld", (long long)ttydev) == -1)
goto oom;
/* The terminal device file may be missing in a chroot() jail. */
Expand Down
20 changes: 10 additions & 10 deletions src/ttyname.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ get_process_ttyname(char *name, size_t namelen)
struct sudo_kinfo_proc *ki_proc = NULL;
size_t size = sizeof(*ki_proc);
int mib[6], rc, serrno = errno;
dev_t ttydev = (dev_t)-1;
dev_t ttydev = NODEV;
debug_decl(get_process_ttyname, SUDO_DEBUG_UTIL);

/*
Expand Down Expand Up @@ -133,7 +133,7 @@ get_process_ttyname(char *name, size_t namelen)
}
errno = ENOENT;
if (rc != -1) {
if ((dev_t)ki_proc->sudo_kp_tdev != (dev_t)-1) {
if ((dev_t)ki_proc->sudo_kp_tdev != NODEV) {
errno = serrno;
ttydev = (dev_t)ki_proc->sudo_kp_tdev;
if (sudo_ttyname_dev(ttydev, name, namelen) == NULL) {
Expand Down Expand Up @@ -162,7 +162,7 @@ get_process_ttyname(char *name, size_t namelen)
dev_t
get_process_ttyname(char *name, size_t namelen)
{
dev_t ttydev = (dev_t)-1;
dev_t ttydev = NODEV;
struct psinfo psinfo;
char path[PATH_MAX];
ssize_t nread;
Expand All @@ -181,7 +181,7 @@ get_process_ttyname(char *name, size_t namelen)
ttydev = makedev(major64(psinfo.pr_ttydev), minor64(psinfo.pr_ttydev));
#endif
/* On AIX, pr_ttydev is 0 (not -1) when no terminal is present. */
if (ttydev != 0 && ttydev != (dev_t)-1) {
if (ttydev != 0 && ttydev != NODEV) {
errno = serrno;
if (sudo_ttyname_dev(ttydev, name, namelen) == NULL) {
sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO,
Expand All @@ -192,7 +192,7 @@ get_process_ttyname(char *name, size_t namelen)
}
goto done;
}
ttydev = (dev_t)-1;
ttydev = NODEV;
}
} else {
struct stat sb;
Expand All @@ -216,7 +216,7 @@ get_process_ttyname(char *name, size_t namelen)
errno = ENOENT;

done:
if (ttydev == (dev_t)-1)
if (ttydev == NODEV)
sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO|SUDO_DEBUG_ERRNO,
"unable to resolve tty via %s", path);

Expand All @@ -233,7 +233,7 @@ dev_t
get_process_ttyname(char *name, size_t namelen)
{
const char path[] = "/proc/self/stat";
dev_t ttydev = (dev_t)-1;
dev_t ttydev = NODEV;
char *cp, buf[1024];
int serrno = errno;
pid_t ppid = 0;
Expand Down Expand Up @@ -335,7 +335,7 @@ get_process_ttyname(char *name, size_t namelen)
done:
if (fd != -1)
close(fd);
if (ttydev == (dev_t)-1)
if (ttydev == NODEV)
sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO|SUDO_DEBUG_ERRNO,
"unable to resolve tty via %s", path);

Expand All @@ -351,7 +351,7 @@ get_process_ttyname(char *name, size_t namelen)
dev_t
get_process_ttyname(char *name, size_t namelen)
{
dev_t ttydev = (dev_t)-1;
dev_t ttydev = NODEV;
int rc, serrno = errno;
struct pst_status pst;
debug_decl(get_process_ttyname, SUDO_DEBUG_UTIL);
Expand Down Expand Up @@ -418,6 +418,6 @@ get_process_ttyname(char *name, size_t namelen)
sudo_debug_printf(SUDO_DEBUG_WARN|SUDO_DEBUG_LINENO|SUDO_DEBUG_ERRNO,
"unable to resolve tty via ttyname");
errno = ENOENT;
debug_return_dev_t((dev_t)-1);
debug_return_dev_t(NODEV);
}
#endif

0 comments on commit d5028a0

Please sign in to comment.