From 5342881bf4b216ff6c1512fbe325bf51187e91d5 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Thu, 1 Apr 2021 14:05:57 +0200 Subject: [PATCH] Refactor, wrap write() syscall in new dprint() function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fixes the GCC warning for unchecked return value from write(): ... ignoring return value of ‘write’, declared with attribute warn_unused_result The new dprint() function wraps write() in a bounded retry-loop that catches EINTR. The length argument is optional computed with strlen() if omitted (zero). Signed-off-by: Joachim Wiberg --- src/exec.c | 10 +++++----- src/getty.c | 27 ++++++++++++--------------- src/helpers.c | 2 +- src/helpers.h | 20 +++++++++++++++++++- src/stty.c | 6 ++++-- 5 files changed, 41 insertions(+), 24 deletions(-) diff --git a/src/exec.c b/src/exec.c index 0d0a728c..db92c136 100644 --- a/src/exec.c +++ b/src/exec.c @@ -316,7 +316,7 @@ static int activate_console(int noclear, int nowait) return 1; if (!noclear) - (void)write(STDERR_FILENO, "\e[r\e[H\e[J", 9); + dprint(STDERR_FILENO, "\e[r\e[H\e[J", 9); /* Disable ECHO, XON/OFF while waiting for */ if (!tcgetattr(STDIN_FILENO, &orig)) { @@ -340,16 +340,16 @@ static int activate_console(int noclear, int nowait) continue; } - (void)write(STDERR_FILENO, clr, strlen(clr)); - (void)write(STDERR_FILENO, msg, strlen(msg)); + dprint(STDERR_FILENO, clr, strlen(clr)); + dprint(STDERR_FILENO, msg, strlen(msg)); while (read(STDIN_FILENO, &c, 1) == 1 && c != '\r') continue; if (fexist(SYNC_STOPPED)) continue; - (void)write(STDERR_FILENO, clr, strlen(clr)); - (void)write(STDERR_FILENO, cup, strlen(cup)); + dprint(STDERR_FILENO, clr, strlen(clr)); + dprint(STDERR_FILENO, cup, strlen(cup)); ret = 1; break; } diff --git a/src/getty.c b/src/getty.c index 5cf064d8..a3a6713f 100644 --- a/src/getty.c +++ b/src/getty.c @@ -41,9 +41,6 @@ #define _PATH_LOGIN "/bin/login" #endif -#define print(s) (void)write(STDOUT_FILENO, s, strlen(s)) - - /* * Read one character from stdin. */ @@ -54,7 +51,7 @@ static int readch(char *tty) st = read(STDIN_FILENO, &ch1, 1); if (st == 0) { - print("\n"); + dprint(STDOUT_FILENO, "\n", 0); _exit(0); } @@ -74,31 +71,31 @@ static void do_parse(char *line, struct utsname *uts, char *tty) s0 = line; for (s = line; *s != 0; s++) { if (*s == '\\') { - (void)write(1, s0, s - s0); + dprint(STDOUT_FILENO, s0, s - s0); s0 = s + 2; switch (*++s) { case 'l': - print(tty); + dprint(STDOUT_FILENO, tty, 0); break; case 'm': - print(uts->machine); + dprint(STDOUT_FILENO, uts->machine, 0); break; case 'n': - print(uts->nodename); + dprint(STDOUT_FILENO, uts->nodename, 0); break; #ifdef _GNU_SOURCE case 'o': - print(uts->domainname); + dprint(STDOUT_FILENO, uts->domainname, 0); break; #endif case 'r': - print(uts->release); + dprint(STDOUT_FILENO, uts->release, 0); break; case 's': - print(uts->sysname); + dprint(STDOUT_FILENO, uts->sysname, 0); break; case 'v': - print(uts->version); + dprint(STDOUT_FILENO, uts->version, 0); break; case 0: goto leave; @@ -109,7 +106,7 @@ static void do_parse(char *line, struct utsname *uts, char *tty) } leave: - (void)write(1, s0, s - s0); + dprint(STDOUT_FILENO, s0, s - s0); } /* @@ -159,7 +156,7 @@ static void do_getty(char *tty, char *name, size_t len) while ((ch = readch(tty)) != '\n') { if (ch == CTRL('U')) { while (np > name) { - (void)write(1, "\b \b", 3); + dprint(STDOUT_FILENO, "\b \b", 3); np--; } continue; @@ -215,7 +212,7 @@ int getty(char *tty, speed_t speed, char *term, char *user) /* Set up TTY, re-enabling ISIG et al. */ stty(STDIN_FILENO, speed); - (void)write(STDERR_FILENO, cln, strlen(cln)); + dprint(STDERR_FILENO, cln, strlen(cln)); /* The getty process is responsible for the UTMP login record */ utmp_set_login(tty, NULL); diff --git a/src/helpers.c b/src/helpers.c index 6ccf713e..afe29d88 100644 --- a/src/helpers.c +++ b/src/helpers.c @@ -170,7 +170,7 @@ ssize_t cprintf(const char *fmt, ...) va_end(ap); for (i = 0; i < num_cons; i++) - (void)write(fds[i], buf, size); + dprint(fds[i], buf, size); return size; } diff --git a/src/helpers.h b/src/helpers.h index a6f3a115..03fac197 100644 --- a/src/helpers.h +++ b/src/helpers.h @@ -70,7 +70,7 @@ void show_progress (pstyle_t style); int getty (char *tty, speed_t speed, char *term, char *user); int sh (char *tty); -int stty (int fd, speed_t speed); +void stty (int fd, speed_t speed); speed_t stty_parse_speed(char *baud); void print_banner (const char *heading); @@ -109,6 +109,24 @@ static inline int create(char *path, mode_t mode, uid_t uid, gid_t gid) return 0; } +static inline int dprint(int fd, const char *s, size_t len) +{ + size_t loop = 3; + int rc = -1; + + if (!len) + len = strlen(s); + + while (loop--) { + rc = write(fd, s, len); + if (rc == -1 && errno == EINTR) + continue; + break; + } + + return rc; +} + int ismnt (char *file, char *dir, char *mode); int fismnt (char *dir); diff --git a/src/stty.c b/src/stty.c index b3408489..b3aa52a5 100644 --- a/src/stty.c +++ b/src/stty.c @@ -27,6 +27,8 @@ #include /* Not included by default in musl libc */ #include +#include + /* B0 means; keep kernel default */ speed_t stty_parse_speed(char *baud) { @@ -123,10 +125,10 @@ void stty(int fd, speed_t speed) tcsetattr(fd, TCSAFLUSH, &term); /* Show cursor again, if it was hidden previously */ - write(fd, "\033[?25h", 6); + dprint(fd, "\033[?25h", 6); /* Enable line wrap, if disabled previously */ - write(fd, "\033[7h", 4); + dprint(fd, "\033[7h", 4); } /**