ptyfwd: simplify error handling in shovel()

We treat all errors the same, hence let's just have a common handling
for that, and normalize how we propagate errors from shovel().

No change in behaviour, just some refactoring.
This commit is contained in:
Lennart Poettering
2024-02-09 18:27:01 +01:00
parent 8496b938ab
commit 8f1fe0410d
+27 -23
View File
@@ -478,7 +478,7 @@ static int pty_forward_ansi_process(PTYForward *f, size_t offset) {
return 0;
}
static int shovel(PTYForward *f) {
static int do_shovel(PTYForward *f) {
ssize_t k;
int r;
@@ -489,17 +489,17 @@ static int shovel(PTYForward *f) {
/* Erase the first line when we start */
f->out_buffer = background_color_sequence(f);
if (!f->out_buffer)
return pty_forward_done(f, log_oom());
return log_oom();
if (!strextend(&f->out_buffer, ANSI_ERASE_TO_END_OF_LINE))
return pty_forward_done(f, log_oom());
return log_oom();
}
if (f->title) {
if (!strextend(&f->out_buffer,
ANSI_WINDOW_TITLE_PUSH
"\x1b]2;", f->title, "\a"))
return pty_forward_done(f, log_oom());
return log_oom();
}
if (f->out_buffer) {
@@ -512,7 +512,7 @@ static int shovel(PTYForward *f) {
/* Make sure we always have room for at least one "line" */
void *p = realloc(f->out_buffer, LINE_MAX);
if (!p)
return pty_forward_done(f, log_oom());
return log_oom();
f->out_buffer = p;
f->out_buffer_size = MALLOC_SIZEOF_SAFE(p);
@@ -535,10 +535,8 @@ static int shovel(PTYForward *f) {
f->stdin_hangup = true;
f->stdin_event_source = sd_event_source_unref(f->stdin_event_source);
} else {
log_error_errno(errno, "read(): %m");
return pty_forward_done(f, -errno);
}
} else
return log_error_errno(errno, "read(): %m");
} else if (k == 0) {
/* EOF on stdin */
f->stdin_readable = false;
@@ -549,7 +547,7 @@ static int shovel(PTYForward *f) {
/* Check if ^] has been pressed three times within one second. If we get this we quite
* immediately. */
if (look_for_escape(f, f->in_buffer + f->in_buffer_full, k))
return pty_forward_done(f, -ECANCELED);
return -ECANCELED;
f->in_buffer_full += (size_t) k;
}
@@ -567,10 +565,8 @@ static int shovel(PTYForward *f) {
f->master_hangup = true;
f->master_event_source = sd_event_source_unref(f->master_event_source);
} else {
log_error_errno(errno, "write(): %m");
return pty_forward_done(f, -errno);
}
} else
return log_error_errno(errno, "write(): %m");
} else {
assert(f->in_buffer_full >= (size_t) k);
memmove(f->in_buffer, f->in_buffer + k, f->in_buffer_full - k);
@@ -594,10 +590,8 @@ static int shovel(PTYForward *f) {
f->master_hangup = true;
f->master_event_source = sd_event_source_unref(f->master_event_source);
} else {
log_error_errno(errno, "read(): %m");
return pty_forward_done(f, -errno);
}
} else
return log_error_errno(errno, "read(): %m");
} else {
f->read_from_master = true;
size_t scan_index = f->out_buffer_full;
@@ -605,7 +599,7 @@ static int shovel(PTYForward *f) {
r = pty_forward_ansi_process(f, scan_index);
if (r < 0)
return pty_forward_done(f, log_error_errno(r, "Failed to scan for ANSI sequences: %m"));
return log_error_errno(r, "Failed to scan for ANSI sequences: %m");
}
}
@@ -620,10 +614,8 @@ static int shovel(PTYForward *f) {
f->stdout_writable = false;
f->stdout_hangup = true;
f->stdout_event_source = sd_event_source_unref(f->stdout_event_source);
} else {
log_error_errno(errno, "write(): %m");
return pty_forward_done(f, -errno);
}
} else
return log_error_errno(errno, "write(): %m");
} else {
@@ -656,6 +648,18 @@ static int shovel(PTYForward *f) {
return 0;
}
static int shovel(PTYForward *f) {
int r;
assert(f);
r = do_shovel(f);
if (r < 0)
return pty_forward_done(f, r);
return r;
}
static int on_master_event(sd_event_source *e, int fd, uint32_t revents, void *userdata) {
PTYForward *f = ASSERT_PTR(userdata);