io-util: introduce loop_write_full that takes a timeout

Also drop do_poll as the use case is covered
by timeout.
This commit is contained in:
Mike Yuan
2023-09-07 20:30:44 +08:00
parent 82b7bf8c1c
commit e22c60a9d5
34 changed files with 103 additions and 81 deletions
+20 -21
View File
@@ -181,7 +181,7 @@ static int backspace_chars(int ttyfd, size_t p) {
for (size_t i = 0; i < p; i++)
memcpy(buf + 3 * i, "\b \b", 3);
return loop_write(ttyfd, buf, 3*p, false);
return loop_write(ttyfd, buf, 3 * p);
}
static int backspace_string(int ttyfd, const char *str) {
@@ -252,7 +252,7 @@ int ask_password_plymouth(
if (!packet)
return -ENOMEM;
r = loop_write(fd, packet, n + 1, true);
r = loop_write_full(fd, packet, n + 1, USEC_INFINITY);
if (r < 0)
return r;
@@ -311,7 +311,7 @@ int ask_password_plymouth(
if (asprintf(&packet, "*\002%c%s%n", (int) (strlen(message) + 1), message, &n) < 0)
return -ENOMEM;
r = loop_write(fd, packet, n+1, true);
r = loop_write_full(fd, packet, n + 1, USEC_INFINITY);
if (r < 0)
return r;
@@ -429,20 +429,21 @@ int ask_password_tty(
use_color = colors_enabled();
if (use_color)
(void) loop_write(ttyfd, ANSI_HIGHLIGHT, SIZE_MAX, false);
(void) loop_write(ttyfd, ANSI_HIGHLIGHT, SIZE_MAX);
(void) loop_write(ttyfd, message, SIZE_MAX, false);
(void) loop_write(ttyfd, " ", 1, false);
(void) loop_write(ttyfd, message, SIZE_MAX);
(void) loop_write(ttyfd, " ", 1);
if (!FLAGS_SET(flags, ASK_PASSWORD_SILENT) && !FLAGS_SET(flags, ASK_PASSWORD_ECHO)) {
if (use_color)
(void) loop_write(ttyfd, ansi_grey(), SIZE_MAX, false);
(void) loop_write(ttyfd, PRESS_TAB, SIZE_MAX, false);
(void) loop_write(ttyfd, ansi_grey(), SIZE_MAX);
(void) loop_write(ttyfd, PRESS_TAB, SIZE_MAX);
press_tab_visible = true;
}
if (use_color)
(void) loop_write(ttyfd, ANSI_NORMAL, SIZE_MAX, false);
(void) loop_write(ttyfd, ANSI_NORMAL, SIZE_MAX);
new_termios = old_termios;
new_termios.c_lflag &= ~(ICANON|ECHO);
@@ -527,7 +528,7 @@ int ask_password_tty(
if (c == 4) { /* C-d also known as EOT */
if (ttyfd >= 0)
(void) loop_write(ttyfd, SKIPPED, SIZE_MAX, false);
(void) loop_write(ttyfd, SKIPPED, SIZE_MAX);
goto skipped;
}
@@ -577,10 +578,10 @@ int ask_password_tty(
* first key (and only as first key), or ... */
if (ttyfd >= 0)
(void) loop_write(ttyfd, NO_ECHO, SIZE_MAX, false);
(void) loop_write(ttyfd, NO_ECHO, SIZE_MAX);
} else if (ttyfd >= 0)
(void) loop_write(ttyfd, "\a", 1, false);
(void) loop_write(ttyfd, "\a", 1);
} else if (c == '\t' && !FLAGS_SET(flags, ASK_PASSWORD_SILENT)) {
@@ -590,13 +591,13 @@ int ask_password_tty(
/* ... or by pressing TAB at any time. */
if (ttyfd >= 0)
(void) loop_write(ttyfd, NO_ECHO, SIZE_MAX, false);
(void) loop_write(ttyfd, NO_ECHO, SIZE_MAX);
} else if (p >= sizeof(passphrase)-1) {
/* Reached the size limit */
if (ttyfd >= 0)
(void) loop_write(ttyfd, "\a", 1, false);
(void) loop_write(ttyfd, "\a", 1);
} else {
passphrase[p++] = c;
@@ -606,13 +607,11 @@ int ask_password_tty(
n = utf8_encoded_valid_unichar(passphrase + codepoint, SIZE_MAX);
if (n >= 0) {
if (FLAGS_SET(flags, ASK_PASSWORD_ECHO))
(void) loop_write(ttyfd, passphrase + codepoint, n, false);
(void) loop_write(ttyfd, passphrase + codepoint, n);
else
(void) loop_write(
ttyfd,
special_glyph(SPECIAL_GLYPH_BULLET),
SIZE_MAX,
false);
(void) loop_write(ttyfd,
special_glyph(SPECIAL_GLYPH_BULLET),
SIZE_MAX);
codepoint = p;
}
}
@@ -644,7 +643,7 @@ skipped:
finish:
if (ttyfd >= 0 && reset_tty) {
(void) loop_write(ttyfd, "\n", 1, false);
(void) loop_write(ttyfd, "\n", 1);
(void) tcsetattr(ttyfd, TCSADRAIN, &old_termios);
}
+1 -1
View File
@@ -382,7 +382,7 @@ static int make_credential_host_secret(
if (r < 0)
goto fail;
r = loop_write(fd, &buf, sizeof(buf), false);
r = loop_write(fd, &buf, sizeof(buf));
if (r < 0)
goto fail;
+2 -2
View File
@@ -275,7 +275,7 @@ int copy_data_fd(int fd) {
/* If there were remaining bytes (i.e. read into memory, but not written out yet) from the
* failed copy operation, let's flush them out next. */
r = loop_write(tmp_fd, remains, remains_size, false);
r = loop_write(tmp_fd, remains, remains_size);
if (r < 0)
return r;
}
@@ -318,7 +318,7 @@ int copy_data_fd(int fd) {
if (remains_size > 0) {
/* Then, copy in any read but not yet written bytes. */
r = loop_write(tmp_fd, remains, remains_size, false);
r = loop_write(tmp_fd, remains, remains_size);
if (r < 0)
return r;
}
+2 -2
View File
@@ -3348,7 +3348,7 @@ int dissected_image_acquire_metadata(DissectedImage *m, DissectImageFlags extra_
if (r < 0)
fd = r;
else {
r = loop_write(fds[2*k+1], &class, sizeof(class), false);
r = loop_write(fds[2*k+1], &class, sizeof(class));
if (r < 0)
goto inner_fail; /* Propagate the error to the parent */
}
@@ -3374,7 +3374,7 @@ int dissected_image_acquire_metadata(DissectedImage *m, DissectImageFlags extra_
}
}
r = loop_write(fds[2*k+1], &found, sizeof(found), false);
r = loop_write(fds[2*k+1], &found, sizeof(found));
if (r < 0)
goto inner_fail;
+1 -1
View File
@@ -826,7 +826,7 @@ int parse_elf_object(int fd, const char *executable, bool fork_disable_dump, cha
* Failure is ignored, because partial output is still useful. */
(void) fcntl(return_pipe[1], F_SETPIPE_SZ, len);
r = loop_write(return_pipe[1], buf, len, false);
r = loop_write(return_pipe[1], buf, len);
if (r == -EAGAIN)
log_warning("Write failed, backtrace will be truncated.");
else if (r < 0)
+1 -1
View File
@@ -162,7 +162,7 @@ int machine_id_setup(const char *root, bool force_transient, sd_id128_t machine_
*
* Otherwise write the machine-id directly to disk. */
if (force_transient) {
r = loop_write(fd, "uninitialized\n", SIZE_MAX, false);
r = loop_write(fd, "uninitialized\n", SIZE_MAX);
if (r < 0)
return log_error_errno(r, "Failed to write uninitialized %s: %m", etc_machine_id);
+2 -2
View File
@@ -204,7 +204,7 @@ void pager_open(PagerFlags flags) {
* secure mode. Thus, start the pager specified through
* envvars only when $SYSTEMD_PAGERSECURE was explicitly set
* as well. */
r = loop_write(exe_name_pipe[1], pager_args[0], strlen(pager_args[0]) + 1, false);
r = loop_write(exe_name_pipe[1], pager_args[0], strlen(pager_args[0]) + 1);
if (r < 0) {
log_error_errno(r, "Failed to write pager name to socket: %m");
_exit(EXIT_FAILURE);
@@ -225,7 +225,7 @@ void pager_open(PagerFlags flags) {
if (use_secure_mode && !STR_IN_SET(pagers[i], "less", "(built-in)"))
continue;
r = loop_write(exe_name_pipe[1], pagers[i], strlen(pagers[i]) + 1, false);
r = loop_write(exe_name_pipe[1], pagers[i], strlen(pagers[i]) + 1);
if (r < 0) {
log_error_errno(r, "Failed to write pager name to socket: %m");
_exit(EXIT_FAILURE);
+1 -1
View File
@@ -4433,7 +4433,7 @@ static int tpm2_userspace_log(
if (lseek(fd, 0, SEEK_END) == (off_t) -1)
return log_error_errno(errno, "Failed to seek to end of JSON log: %m");
r = loop_write(fd, f, SIZE_MAX, /* do_poll= */ false);
r = loop_write(fd, f, SIZE_MAX);
if (r < 0)
return log_error_errno(r, "Failed to write JSON data to log: %m");