Merge pull request #32917 from YHNdnzj/ptyfwd-cleanup

ptyfwd: a few fixes/cleanups
This commit is contained in:
Luca Boccassi 2024-05-19 14:44:54 +02:00 committed by GitHub
commit d4d6a9e4ae
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 31 additions and 27 deletions

View File

@ -1133,11 +1133,17 @@ static int transient_service_set_properties(sd_bus_message *m, const char *pty_p
} }
if (pty_path) { if (pty_path) {
_cleanup_close_ int pty_slave = -EBADF;
pty_slave = open_terminal(pty_path, O_RDWR|O_NOCTTY|O_CLOEXEC);
if (pty_slave < 0)
return pty_slave;
r = sd_bus_message_append(m, r = sd_bus_message_append(m,
"(sv)(sv)(sv)(sv)", "(sv)(sv)(sv)(sv)",
"StandardInput", "s", "tty", "StandardInputFileDescriptor", "h", pty_slave,
"StandardOutput", "s", "tty", "StandardOutputFileDescriptor", "h", pty_slave,
"StandardError", "s", "tty", "StandardErrorFileDescriptor", "h", pty_slave,
"TTYPath", "s", pty_path); "TTYPath", "s", pty_path);
if (r < 0) if (r < 0)
return bus_log_create_error(r); return bus_log_create_error(r);
@ -1496,11 +1502,13 @@ static int on_properties_changed(sd_bus_message *m, void *userdata, sd_bus_error
} }
static int pty_forward_handler(PTYForward *f, int rcode, void *userdata) { static int pty_forward_handler(PTYForward *f, int rcode, void *userdata) {
RunContext *c = userdata; RunContext *c = ASSERT_PTR(userdata);
assert(f); assert(f);
if (rcode < 0) { if (rcode == -ECANCELED)
log_debug_errno(rcode, "PTY forwarder disconnected.");
else if (rcode < 0) {
sd_event_exit(c->event, EXIT_FAILURE); sd_event_exit(c->event, EXIT_FAILURE);
return log_error_errno(rcode, "Error on PTY forwarding logic: %m"); return log_error_errno(rcode, "Error on PTY forwarding logic: %m");
} }

View File

@ -570,7 +570,7 @@ static int do_shovel(PTYForward *f) {
f->stdin_event_source = sd_event_source_unref(f->stdin_event_source); f->stdin_event_source = sd_event_source_unref(f->stdin_event_source);
} else } else
return log_error_errno(errno, "read(): %m"); return log_error_errno(errno, "Failed to read from pty input fd: %m");
} else if (k == 0) { } else if (k == 0) {
/* EOF on stdin */ /* EOF on stdin */
f->stdin_readable = false; f->stdin_readable = false;
@ -625,7 +625,7 @@ static int do_shovel(PTYForward *f) {
f->master_event_source = sd_event_source_unref(f->master_event_source); f->master_event_source = sd_event_source_unref(f->master_event_source);
} else } else
return log_error_errno(errno, "read(): %m"); return log_error_errno(errno, "Failed to read from pty master fd: %m");
} else { } else {
f->read_from_master = true; f->read_from_master = true;
size_t scan_index = f->out_buffer_full; size_t scan_index = f->out_buffer_full;
@ -649,7 +649,7 @@ static int do_shovel(PTYForward *f) {
f->stdout_hangup = true; f->stdout_hangup = true;
f->stdout_event_source = sd_event_source_unref(f->stdout_event_source); f->stdout_event_source = sd_event_source_unref(f->stdout_event_source);
} else } else
return log_error_errno(errno, "write(): %m"); return log_error_errno(errno, "Failed to write to pty output fd: %m");
} else { } else {
@ -767,7 +767,7 @@ int pty_forward_new(
if (!f) if (!f)
return -ENOMEM; return -ENOMEM;
*f = (struct PTYForward) { *f = (PTYForward) {
.flags = flags, .flags = flags,
.master = -EBADF, .master = -EBADF,
.input_fd = -EBADF, .input_fd = -EBADF,
@ -845,14 +845,15 @@ int pty_forward_new(
(void) ioctl(master, TIOCSWINSZ, &ws); (void) ioctl(master, TIOCSWINSZ, &ws);
if (!(flags & PTY_FORWARD_READ_ONLY)) { if (!FLAGS_SET(flags, PTY_FORWARD_READ_ONLY)) {
int same; bool same;
assert(f->input_fd >= 0); assert(f->input_fd >= 0);
same = fd_inode_same(f->input_fd, f->output_fd); r = fd_inode_same(f->input_fd, f->output_fd);
if (same < 0) if (r < 0)
return same; return r;
same = r > 0;
if (tcgetattr(f->input_fd, &f->saved_stdin_attr) >= 0) { if (tcgetattr(f->input_fd, &f->saved_stdin_attr) >= 0) {
struct termios raw_stdin_attr; struct termios raw_stdin_attr;
@ -917,10 +918,12 @@ int pty_forward_new(
PTYForward *pty_forward_free(PTYForward *f) { PTYForward *pty_forward_free(PTYForward *f) {
if (!f) if (!f)
return NULL; return NULL;
pty_forward_disconnect(f); pty_forward_disconnect(f);
free(f->background_color); free(f->background_color);
free(f->title); free(f->title);
free(f->title_prefix); free(f->title_prefix);
return mfree(f); return mfree(f);
} }
@ -940,15 +943,14 @@ int pty_forward_set_ignore_vhangup(PTYForward *f, bool b) {
assert(f); assert(f);
if (!!(f->flags & PTY_FORWARD_IGNORE_VHANGUP) == b) if (FLAGS_SET(f->flags, PTY_FORWARD_IGNORE_VHANGUP) == b)
return 0; return 0;
SET_FLAG(f->flags, PTY_FORWARD_IGNORE_VHANGUP, b); SET_FLAG(f->flags, PTY_FORWARD_IGNORE_VHANGUP, b);
if (!ignore_vhangup(f)) { if (!ignore_vhangup(f)) {
/* We shall now react to vhangup()s? Let's check /* We shall now react to vhangup()s? Let's check immediately if we might be in one. */
* immediately if we might be in one */
f->master_readable = true; f->master_readable = true;
r = shovel(f); r = shovel(f);
@ -962,7 +964,7 @@ int pty_forward_set_ignore_vhangup(PTYForward *f, bool b) {
bool pty_forward_get_ignore_vhangup(PTYForward *f) { bool pty_forward_get_ignore_vhangup(PTYForward *f) {
assert(f); assert(f);
return !!(f->flags & PTY_FORWARD_IGNORE_VHANGUP); return FLAGS_SET(f->flags, PTY_FORWARD_IGNORE_VHANGUP);
} }
bool pty_forward_is_done(PTYForward *f) { bool pty_forward_is_done(PTYForward *f) {
@ -994,6 +996,7 @@ bool pty_forward_drain(PTYForward *f) {
int pty_forward_set_priority(PTYForward *f, int64_t priority) { int pty_forward_set_priority(PTYForward *f, int64_t priority) {
int r; int r;
assert(f); assert(f);
if (f->stdin_event_source) { if (f->stdin_event_source) {

View File

@ -1572,16 +1572,9 @@ static int run_virtual_machine(int kvm_device_fd, int vhost_device_fd) {
case CONSOLE_INTERACTIVE: { case CONSOLE_INTERACTIVE: {
_cleanup_free_ char *pty_path = NULL; _cleanup_free_ char *pty_path = NULL;
master = posix_openpt(O_RDWR|O_NOCTTY|O_CLOEXEC|O_NONBLOCK); master = openpt_allocate(O_RDWR|O_NONBLOCK, &pty_path);
if (master < 0) if (master < 0)
return log_error_errno(errno, "Failed to acquire pseudo tty: %m"); return log_error_errno(master, "Failed to setup pty: %m");
r = ptsname_malloc(master, &pty_path);
if (r < 0)
return log_error_errno(r, "Failed to determine tty name: %m");
if (unlockpt(master) < 0)
return log_error_errno(errno, "Failed to unlock tty: %m");
if (strv_extend_many( if (strv_extend_many(
&cmdline, &cmdline,