Merge pull request #29071 from YHNdnzj/loop-write-full
io-util: introduce loop_write_full and use it where appropriate
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
@@ -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);
|
||||
|
||||
@@ -4430,7 +4430,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");
|
||||
|
||||
|
||||
+1
-41
@@ -1,7 +1,6 @@
|
||||
/* SPDX-License-Identifier: LGPL-2.1-or-later */
|
||||
|
||||
#include <errno.h>
|
||||
#include <poll.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
@@ -22,9 +21,6 @@
|
||||
|
||||
static int write_to_terminal(const char *tty, const char *message) {
|
||||
_cleanup_close_ int fd = -EBADF;
|
||||
const char *p;
|
||||
size_t left;
|
||||
usec_t end;
|
||||
|
||||
assert(tty);
|
||||
assert(message);
|
||||
@@ -35,43 +31,7 @@ static int write_to_terminal(const char *tty, const char *message) {
|
||||
if (!isatty(fd))
|
||||
return -ENOTTY;
|
||||
|
||||
p = message;
|
||||
left = strlen(message);
|
||||
|
||||
end = usec_add(now(CLOCK_MONOTONIC), TIMEOUT_USEC);
|
||||
|
||||
while (left > 0) {
|
||||
ssize_t n;
|
||||
usec_t t;
|
||||
int k;
|
||||
|
||||
t = now(CLOCK_MONOTONIC);
|
||||
if (t >= end)
|
||||
return -ETIME;
|
||||
|
||||
k = fd_wait_for_event(fd, POLLOUT, end - t);
|
||||
if (ERRNO_IS_NEG_TRANSIENT(k))
|
||||
continue;
|
||||
if (k < 0)
|
||||
return k;
|
||||
if (k == 0)
|
||||
return -ETIME;
|
||||
|
||||
n = write(fd, p, left);
|
||||
if (n < 0) {
|
||||
if (ERRNO_IS_TRANSIENT(errno))
|
||||
continue;
|
||||
|
||||
return -errno;
|
||||
}
|
||||
|
||||
assert((size_t) n <= left);
|
||||
|
||||
p += n;
|
||||
left -= n;
|
||||
}
|
||||
|
||||
return 0;
|
||||
return loop_write_full(fd, message, SIZE_MAX, TIMEOUT_USEC);
|
||||
}
|
||||
|
||||
#if ENABLE_UTMP
|
||||
|
||||
Reference in New Issue
Block a user