Merge pull request #32954 from YHNdnzj/run-forwarder-exit

run: when disconnected from PTY forwarder, exit event loop if not --wait
This commit is contained in:
Luca Boccassi
2024-05-21 20:37:17 +02:00
committed by GitHub
3 changed files with 26 additions and 9 deletions
+15 -6
View File
@@ -64,8 +64,8 @@
other service, and thus shows up in the output of <command>systemctl list-units</command> like any other unit. It
will run in a clean and detached execution environment, with the service manager as its parent process. In this
mode, <command>systemd-run</command> will start the service asynchronously in the background and return after the
command has begun execution (unless <option>--no-block</option> or <option>--wait</option> are specified, see
below).</para>
command has begun execution (unless <option>--no-block</option>, <option>--wait</option>, <option>--pipe</option>,
or <option>--pty</option> are specified, see below).</para>
<para>If a command is run as transient scope unit, it will be executed by <command>systemd-run</command>
itself as parent process and will thus inherit the execution environment of the caller. However, the
@@ -325,6 +325,11 @@
to the terminal <command>systemd-run</command> is invoked on, via a pseudo TTY device. This allows running
programs that expect interactive user input/output as services, such as interactive command shells.</para>
<para>This option will result in <command>systemd-run</command> synchronously waiting for
the transient service to terminate, similar to specifying <option>--wait</option>. If specified
along with <option>--wait</option>, <command>systemd-run</command> won't exit when manually disconnecting
from the pseudo TTY device.</para>
<para>Note that
<citerefentry><refentrytitle>machinectl</refentrytitle><manvolnum>1</manvolnum></citerefentry>'s
<command>shell</command> command is usually a better alternative for requesting a new, interactive login
@@ -341,15 +346,19 @@
<listitem><para>If specified, standard input, output, and error of the transient service are inherited from the
<command>systemd-run</command> command itself. This allows <command>systemd-run</command>
to be used within shell pipelines.
Note that this mode is not suitable for interactive command shells and similar, as the
service process will not become a TTY controller when invoked on a terminal. Use <option>--pty</option> instead
in that case.</para>
to be used within shell pipelines.</para>
<para>Note that this mode is not suitable for interactive command shells and similar, as the
service process will not become a TTY controller when invoked on a terminal. Use <option>--pty</option>
instead in that case.</para>
<para>When both <option>--pipe</option> and <option>--pty</option> are used in combination the more appropriate
option is automatically determined and used. Specifically, when invoked with standard input, output and error
connected to a TTY <option>--pty</option> is used, and otherwise <option>--pipe</option>.</para>
<para>This option will result in <command>systemd-run</command> synchronously waiting for
the transient service to terminate, similar to specifying <option>--wait</option>.</para>
<para>When this option is used the original file descriptors <command>systemd-run</command> receives are passed
to the service processes as-is. If the service runs with different privileges than
<command>systemd-run</command>, this means the service might not be able to reopen the passed file
+8 -3
View File
@@ -1432,7 +1432,7 @@ static void run_context_check_done(RunContext *c) {
else
done = true;
if (c->forward && done) /* If the service is gone, it's time to drain the output */
if (c->forward && !pty_forward_is_done(c->forward) && done) /* If the service is gone, it's time to drain the output */
done = pty_forward_drain(c->forward);
if (done)
@@ -1506,9 +1506,14 @@ static int pty_forward_handler(PTYForward *f, int rcode, void *userdata) {
assert(f);
if (rcode == -ECANCELED)
if (rcode == -ECANCELED) {
log_debug_errno(rcode, "PTY forwarder disconnected.");
else if (rcode < 0) {
if (!arg_wait)
return sd_event_exit(c->event, EXIT_SUCCESS);
/* If --wait is specified, we'll only exit the pty forwarding, but will continue to wait
* for the service to end. If the user hits ^C we'll exit too. */
} else if (rcode < 0) {
sd_event_exit(c->event, EXIT_FAILURE);
return log_error_errno(rcode, "Error on PTY forwarding logic: %m");
}
+3
View File
@@ -763,6 +763,9 @@ int pty_forward_new(
struct winsize ws;
int r;
assert(master >= 0);
assert(ret);
f = new(PTYForward, 1);
if (!f)
return -ENOMEM;