Refactor, wrap write() syscall in new dprint() function

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 <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg
2021-04-04 12:35:35 +02:00
parent c84c17ea8c
commit 5342881bf4
5 changed files with 41 additions and 24 deletions
+5 -5
View File
@@ -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 <CR> */
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;
}
+12 -15
View File
@@ -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);
+1 -1
View File
@@ -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;
}
+19 -1
View File
@@ -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);
+4 -2
View File
@@ -27,6 +27,8 @@
#include <sys/ttydefaults.h> /* Not included by default in musl libc */
#include <termios.h>
#include <helpers.h>
/* 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);
}
/**