From 420db23714ffcab87caa67bae0e3de9f42222cf9 Mon Sep 17 00:00:00 2001 From: "Todd C. Miller" Date: Tue, 3 Aug 2010 11:17:56 -0400 Subject: [PATCH] Quiet gcc warnings on glibc systems that use warn_unused_result for write(2) and others. --- plugins/sudoers/check.c | 3 ++- plugins/sudoers/logging.c | 3 ++- plugins/sudoers/sudoreplay.c | 9 ++++++--- plugins/sudoers/visudo.c | 18 +++++++++++++----- src/exec_pty.c | 15 ++++++++++----- src/get_pty.c | 3 ++- src/tgetpass.c | 25 +++++++++++++++++-------- 7 files changed, 52 insertions(+), 24 deletions(-) diff --git a/plugins/sudoers/check.c b/plugins/sudoers/check.c index bafcc5e55..a644080f3 100644 --- a/plugins/sudoers/check.c +++ b/plugins/sudoers/check.c @@ -216,7 +216,8 @@ update_timestamp(char *timestampdir, char *timestampfile) log_error(NO_EXIT|USE_ERRNO, "Can't open %s", timestampfile); else { lock_file(fd, SUDO_LOCK); - write(fd, &tty_info, sizeof(tty_info)); + if (write(fd, &tty_info, sizeof(tty_info)) != sizeof(tty_info)) + log_error(NO_EXIT|USE_ERRNO, "Can't write %s", timestampfile); close(fd); } } else { diff --git a/plugins/sudoers/logging.c b/plugins/sudoers/logging.c index 217ac77c6..8e2ea1004 100644 --- a/plugins/sudoers/logging.c +++ b/plugins/sudoers/logging.c @@ -452,7 +452,8 @@ send_mail(const char *fmt, ...) /* Daemonize - disassociate from session/tty. */ if (setsid() == -1) warning("setsid"); - (void) chdir("/"); + if (chdir("/") == -1) + warning("chdir(/)"); if ((fd = open(_PATH_DEVNULL, O_RDWR, 0644)) != -1) { (void) dup2(fd, STDIN_FILENO); (void) dup2(fd, STDOUT_FILENO); diff --git a/plugins/sudoers/sudoreplay.c b/plugins/sudoers/sudoreplay.c index bf416a601..1f7e3d4aa 100644 --- a/plugins/sudoers/sudoreplay.c +++ b/plugins/sudoers/sudoreplay.c @@ -312,9 +312,12 @@ main(int argc, char *argv[]) error(1, "unable to open %s", path); cp = NULL; len = 0; - getline(&cp, &len, lfile); /* log */ - getline(&cp, &len, lfile); /* cwd */ - getline(&cp, &len, lfile); /* command */ + /* Pull out command (third line). */ + if (getline(&cp, &len, lfile) == -1 || + getline(&cp, &len, lfile) == -1 || + getline(&cp, &len, lfile) == -1) { + error(1, "invalid log file %s", path); + } printf("Replaying sudo session: %s", cp); free(cp); fclose(lfile); diff --git a/plugins/sudoers/visudo.c b/plugins/sudoers/visudo.c index d20a0dec4..03ba088c5 100644 --- a/plugins/sudoers/visudo.c +++ b/plugins/sudoers/visudo.c @@ -290,7 +290,8 @@ edit_sudoers(struct sudoersfile *sp, char *editor, char *args, int lineno) /* Add missing newline at EOF if needed. */ if (nread > 0 && buf[nread - 1] != '\n') { buf[0] = '\n'; - write(tfd, buf, 1); + if (write(tfd, buf, 1) != 1) + error(1, "write error"); } } (void) close(tfd); @@ -483,8 +484,14 @@ install_sudoers(struct sudoersfile *sp, int oldperms) if (stat(sp->path, &sb) == -1) #endif error(1, "can't stat %s", sp->path); - (void) chown(sp->tpath, sb.st_uid, sb.st_gid); - (void) chmod(sp->tpath, sb.st_mode & 0777); + if (chown(sp->tpath, sb.st_uid, sb.st_gid) != 0) { + warning("unable to set (uid, gid) of %s to (%d, %d)", + sp->tpath, sb.st_uid, sb.st_gid); + } + if (chmod(sp->tpath, sb.st_mode & 0777) != 0) { + warning("unable to change mode of %s to 0%o", sp->tpath, + (sb.st_mode & 0777)); + } } else { if (chown(sp->tpath, SUDOERS_UID, SUDOERS_GID) != 0) { warning("unable to set (uid, gid) of %s to (%d, %d)", @@ -1143,8 +1150,9 @@ quit(int signo) { cleanup(signo); #define emsg " exiting due to signal.\n" - write(STDERR_FILENO, getprogname(), strlen(getprogname())); - write(STDERR_FILENO, emsg, sizeof(emsg) - 1); + if (write(STDERR_FILENO, getprogname(), strlen(getprogname())) == -1 || + write(STDERR_FILENO, emsg, sizeof(emsg) - 1) == -1) + /* shut up glibc */; _exit(signo); } diff --git a/src/exec_pty.c b/src/exec_pty.c index 165e3dc33..0e8adc321 100644 --- a/src/exec_pty.c +++ b/src/exec_pty.c @@ -639,10 +639,14 @@ pty_close(struct command_status *cstat) const char *reason = strsignal(signo); n = io_fds[SFD_USERTTY] != -1 ? io_fds[SFD_USERTTY] : STDOUT_FILENO; - write(n, reason, strlen(reason)); - if (WCOREDUMP(cstat->val)) - write(n, " (core dumped)", 14); - write(n, "\n", 1); + if (write(n, reason, strlen(reason)) != -1) { + if (WCOREDUMP(cstat->val)) { + if (write(n, " (core dumped)", 14) == -1) + /* shut up glibc */; + } + if (write(n, "\n", 1) == -1) + /* shut up glibc */; + } } } } @@ -875,7 +879,8 @@ exec_monitor(struct command_details *details, char *argv[], char *envp[], exec_pty(details, argv, envp); cstat.type = CMD_ERRNO; cstat.val = errno; - write(errpipe[1], &cstat, sizeof(cstat)); + if (write(errpipe[1], &cstat, sizeof(cstat)) == -1) + /* shut up glibc */; _exit(1); } close(errpipe[1]); diff --git a/src/get_pty.c b/src/get_pty.c index 15d653c1b..72873a0fb 100644 --- a/src/get_pty.c +++ b/src/get_pty.c @@ -67,7 +67,8 @@ get_pty(int *master, int *slave, char *name, size_t namesz, uid_t ttyuid) if (openpty(master, slave, name, NULL, NULL) != 0) return(0); - (void) chown(name, ttyuid, ttygid); + if (chown(name, ttyuid, ttygid) != 0) + return(0); return(1); } diff --git a/src/tgetpass.c b/src/tgetpass.c index a0c6578fa..ad88086b8 100644 --- a/src/tgetpass.c +++ b/src/tgetpass.c @@ -150,8 +150,10 @@ restart: sa.sa_handler = SIG_IGN; (void) sigaction(SIGPIPE, &sa, &savepipe); - if (prompt) - (void) write(output, prompt, strlen(prompt)); + if (prompt) { + if (write(output, prompt, strlen(prompt)) == -1) + goto restore; + } if (timeout > 0) alarm(timeout); @@ -159,9 +161,12 @@ restart: alarm(0); save_errno = errno; - if (neednl || pass == NULL) - (void) write(output, "\n", 1); + if (neednl || pass == NULL) { + if (write(output, "\n", 1) == -1) + goto restore; + } +restore: /* Restore old tty settings and signals. */ if (!ISSET(flags, TGP_ECHO)) term_restore(input, 1); @@ -277,20 +282,23 @@ getln(int fd, char *buf, size_t bufsiz, int feedback) if (feedback) { if (c == term_kill) { while (cp > buf) { - (void) write(fd, "\b \b", 3); + if (write(fd, "\b \b", 3) == -1) + break; --cp; } left = bufsiz; continue; } else if (c == term_erase) { if (cp > buf) { - (void) write(fd, "\b \b", 3); + if (write(fd, "\b \b", 3) == -1) + break; --cp; left++; } continue; } - (void) write(fd, "*", 1); + if (write(fd, "*", 1) == -1) + /* shut up glibc */; } *cp++ = c; } @@ -298,7 +306,8 @@ getln(int fd, char *buf, size_t bufsiz, int feedback) if (feedback) { /* erase stars */ while (cp > buf) { - (void) write(fd, "\b \b", 3); + if (write(fd, "\b \b", 3) == -1) + break; --cp; } } -- 2.40.0