1
0
mirror of https://github.com/systemd/systemd synced 2025-10-04 19:24:44 +02:00

Compare commits

..

No commits in common. "bd9ea0f691d3f258a92eff7c870ebb2e214ccdcd" and "2c1881f42cbe4bed7b02749dfff7f9f58f507f00" have entirely different histories.

12 changed files with 116 additions and 142 deletions

View File

@ -1236,10 +1236,10 @@ static int on_machine_removed(sd_bus_message *m, void *userdata, sd_bus_error *r
/* Tell the forwarder to exit on the next vhangup(), so that we still flush out what might be queued
* and exit then. */
r = pty_forward_honor_vhangup(forward);
r = pty_forward_set_ignore_vhangup(forward, false);
if (r < 0) {
/* On error, quit immediately. */
log_error_errno(r, "Failed to make PTY forwarder honor vhangup(): %m");
log_error_errno(r, "Failed to set ignore_vhangup flag: %m");
(void) sd_event_exit(sd_bus_get_event(sd_bus_message_get_bus(m)), EXIT_FAILURE);
}
@ -1285,8 +1285,8 @@ static int process_forward(sd_event *event, sd_bus_slot *machine_removed_slot, i
return log_error_errno(r, "Failed to run event loop: %m");
bool machine_died =
FLAGS_SET(flags, PTY_FORWARD_IGNORE_VHANGUP) &&
!pty_forward_vhangup_honored(forward);
(flags & PTY_FORWARD_IGNORE_VHANGUP) &&
pty_forward_get_ignore_vhangup(forward) == 0;
if (!arg_quiet) {
if (machine_died)

View File

@ -1818,32 +1818,17 @@ static int run_context_check_started(RunContext *c) {
}
static void run_context_check_done(RunContext *c) {
int r;
assert(c);
if (!STRPTR_IN_SET(c->active_state, "inactive", "failed") ||
c->start_job || /* our start job */
c->job) /* any other job */
return;
bool done = STRPTR_IN_SET(c->active_state, "inactive", "failed") &&
!c->start_job && /* our start job */
!c->job; /* any other job */
if (!c->forward)
return (void) sd_event_exit(c->event, EXIT_SUCCESS);
if (done && c->forward) /* If the service is gone, it's time to drain the output */
done = pty_forward_drain(c->forward);
/* If the service is gone, it's time to drain the output */
r = pty_forward_drain(c->forward);
if (r < 0) {
log_error_errno(r, "Failed to drain PTY forwarder: %m");
return (void) sd_event_exit(c->event, EXIT_FAILURE);
}
/* Tell the forwarder to exit on the next vhangup(), so that we still flush out what might be queued
* and exit then. */
r = pty_forward_honor_vhangup(c->forward);
if (r < 0) {
log_error_errno(r, "Failed to make PTY forwarder honor vhangup(): %m");
return (void) sd_event_exit(c->event, EXIT_FAILURE);
}
if (done)
(void) sd_event_exit(c->event, EXIT_SUCCESS);
}
static int map_job(sd_bus *bus, const char *member, sd_bus_message *m, sd_bus_error *error, void *userdata) {
@ -1998,8 +1983,6 @@ static int pty_forward_handler(PTYForward *f, int rcode, void *userdata) {
return log_error_errno(rcode, "Error on PTY forwarding logic: %m");
}
c->forward = pty_forward_free(c->forward);
run_context_check_done(c);
return 0;
}

View File

@ -60,7 +60,6 @@ struct PTYForward {
sd_event_source *master_event_source;
sd_event_source *sigwinch_event_source;
sd_event_source *exit_event_source;
sd_event_source *defer_event_source;
struct termios saved_stdin_attr;
struct termios saved_stdout_attr;
@ -123,7 +122,6 @@ static void pty_forward_disconnect(PTYForward *f) {
f->master_event_source = sd_event_source_unref(f->master_event_source);
f->sigwinch_event_source = sd_event_source_unref(f->sigwinch_event_source);
f->exit_event_source = sd_event_source_unref(f->exit_event_source);
f->defer_event_source = sd_event_source_unref(f->defer_event_source);
f->event = sd_event_unref(f->event);
if (f->output_fd >= 0) {
@ -251,6 +249,18 @@ static RequestOperation look_for_escape(PTYForward *f, const char *buffer, size_
return REQUEST_NOP;
}
static bool ignore_vhangup(PTYForward *f) {
assert(f);
if (f->flags & PTY_FORWARD_IGNORE_VHANGUP)
return true;
if ((f->flags & PTY_FORWARD_IGNORE_INITIAL_VHANGUP) && !f->read_from_master)
return true;
return false;
}
static bool drained(PTYForward *f) {
int q = 0;
@ -710,9 +720,9 @@ static int do_shovel(PTYForward *f) {
/* Note that EIO on the master device might be caused by vhangup() or
* temporary closing of everything on the other side, we treat it like EAGAIN
* here and try again, unless vhangup() is honored. */
* here and try again, unless ignore_vhangup is off. */
if (errno == EAGAIN || (errno == EIO && !pty_forward_vhangup_honored(f)))
if (errno == EAGAIN || (errno == EIO && ignore_vhangup(f)))
f->master_readable = false;
else if (IN_SET(errno, EPIPE, ECONNRESET, EIO)) {
f->master_readable = f->master_writable = false;
@ -776,18 +786,6 @@ static int do_shovel(PTYForward *f) {
break;
}
return 0;
}
static int shovel(PTYForward *f) {
int r;
assert(f);
r = do_shovel(f);
if (r < 0)
return pty_forward_done(f, r);
if (f->stdin_hangup || f->stdout_hangup || f->master_hangup) {
/* Exit the loop if any side hung up and if there's
* nothing more to write or nothing we could write. */
@ -805,17 +803,16 @@ static int shovel(PTYForward *f) {
return 0;
}
static int shovel_force(PTYForward *f) {
static int shovel(PTYForward *f) {
int r;
assert(f);
if (!f->master_hangup)
f->master_writable = f->master_readable = true;
if (!f->stdin_hangup)
f->stdin_readable = true;
if (!f->stdout_hangup)
f->stdout_writable = true;
r = do_shovel(f);
if (r < 0)
return pty_forward_done(f, r);
return shovel(f);
return r;
}
static int on_master_event(sd_event_source *e, int fd, uint32_t revents, void *userdata) {
@ -886,7 +883,15 @@ static int on_exit_event(sd_event_source *e, void *userdata) {
if (!pty_forward_drain(f)) {
/* If not drained, try to drain the buffer. */
r = shovel_force(f);
if (!f->master_hangup)
f->master_writable = f->master_readable = true;
if (!f->stdin_hangup)
f->stdin_readable = true;
if (!f->stdout_hangup)
f->stdout_writable = true;
r = shovel(f);
if (r < 0)
return r;
}
@ -894,23 +899,6 @@ static int on_exit_event(sd_event_source *e, void *userdata) {
return pty_forward_done(f, 0);
}
static int on_defer_event(sd_event_source *s, void *userdata) {
PTYForward *f = ASSERT_PTR(userdata);
return shovel_force(f);
}
static int pty_forward_add_defer(PTYForward *f) {
assert(f);
if (f->done)
return 0;
if (f->defer_event_source)
return sd_event_source_set_enabled(f->defer_event_source, SD_EVENT_ONESHOT);
return sd_event_add_defer(f->event, &f->defer_event_source, on_defer_event, f);
}
int pty_forward_new(
sd_event *event,
int master,
@ -1094,31 +1082,33 @@ PTYForward* pty_forward_free(PTYForward *f) {
return mfree(f);
}
int pty_forward_honor_vhangup(PTYForward *f) {
int pty_forward_set_ignore_vhangup(PTYForward *f, bool b) {
int r;
assert(f);
if ((f->flags & (PTY_FORWARD_IGNORE_VHANGUP | PTY_FORWARD_IGNORE_INITIAL_VHANGUP)) == 0)
return 0; /* nothing changed. */
f->flags &= ~(PTY_FORWARD_IGNORE_VHANGUP | PTY_FORWARD_IGNORE_INITIAL_VHANGUP);
if (f->master_hangup)
if (FLAGS_SET(f->flags, PTY_FORWARD_IGNORE_VHANGUP) == b)
return 0;
/* We shall now react to vhangup()s? Let's check if we might be in one. */
return pty_forward_add_defer(f);
SET_FLAG(f->flags, PTY_FORWARD_IGNORE_VHANGUP, b);
if (!ignore_vhangup(f)) {
/* We shall now react to vhangup()s? Let's check immediately if we might be in one. */
f->master_readable = true;
r = shovel(f);
if (r < 0)
return r;
}
bool pty_forward_vhangup_honored(const PTYForward *f) {
return 0;
}
bool pty_forward_get_ignore_vhangup(PTYForward *f) {
assert(f);
if (FLAGS_SET(f->flags, PTY_FORWARD_IGNORE_VHANGUP))
return false;
if (FLAGS_SET(f->flags, PTY_FORWARD_IGNORE_INITIAL_VHANGUP) && !f->read_from_master)
return false;
return true;
return FLAGS_SET(f->flags, PTY_FORWARD_IGNORE_VHANGUP);
}
void pty_forward_set_hangup_handler(PTYForward *f, PTYForwardHangupHandler cb, void *userdata) {
@ -1135,14 +1125,18 @@ void pty_forward_set_hotkey_handler(PTYForward *f, PTYForwardHotkeyHandler cb, v
f->hotkey_userdata = userdata;
}
int pty_forward_drain(PTYForward *f) {
bool pty_forward_drain(PTYForward *f) {
assert(f);
/* Starts draining the forwarder. This makes sure the handler function is called the next time the
* number of unprocessed bytes hits zero. */
/* Starts draining the forwarder. Specifically:
*
* - Returns true if there are no unprocessed bytes from the pty, false otherwise
*
* - Makes sure the handler function is called the next time the number of unprocessed bytes hits zero
*/
f->drain = true;
return pty_forward_add_defer(f);
return drained(f);
}
int pty_forward_set_priority(PTYForward *f, int64_t priority) {

View File

@ -28,13 +28,13 @@ extern const int pty_forward_signals[N_PTY_FORWARD_SIGNALS];
int pty_forward_new(sd_event *event, int master, PTYForwardFlags flags, PTYForward **ret);
PTYForward* pty_forward_free(PTYForward *f);
int pty_forward_honor_vhangup(PTYForward *f);
bool pty_forward_vhangup_honored(const PTYForward *f);
int pty_forward_set_ignore_vhangup(PTYForward *f, bool ignore_vhangup);
bool pty_forward_get_ignore_vhangup(PTYForward *f);
void pty_forward_set_hangup_handler(PTYForward *f, PTYForwardHangupHandler handler, void *userdata);
void pty_forward_set_hotkey_handler(PTYForward *f, PTYForwardHotkeyHandler handler, void *userdata);
int pty_forward_drain(PTYForward *f);
bool pty_forward_drain(PTYForward *f);
int pty_forward_set_priority(PTYForward *f, int64_t priority);

View File

@ -1,6 +0,0 @@
[Unit]
Description=Test for DeferReactivation=yes (service)
[Service]
Type=simple
ExecStart=sh -c 'date +%%s >>/tmp/defer-reactivation.log; sleep 5'

View File

@ -1,7 +0,0 @@
[Unit]
Description=Test for DeferReactivation=yes (timer)
[Timer]
OnCalendar=*:*:0/5
AccuracySec=1us
DeferReactivation=yes

View File

@ -0,0 +1,6 @@
[Unit]
Description=Testing systemd timers
[Service]
Type=simple
ExecStart=sh -c 'date +%%s >>/tmp/realtime-test.log ; sleep 5'

View File

@ -0,0 +1,10 @@
[Unit]
Description=Testing systemd timers
[Timer]
OnCalendar=*:*:0/5
AccuracySec=1us
DeferReactivation=true
[Install]
WantedBy=timers.target

View File

@ -357,6 +357,7 @@ if install_tests
'integration-tests/TEST-30-ONCLOCKCHANGE/TEST-30-ONCLOCKCHANGE.units',
'integration-tests/TEST-52-HONORFIRSTSHUTDOWN/TEST-52-HONORFIRSTSHUTDOWN.units',
'integration-tests/TEST-63-PATH/TEST-63-PATH.units',
'integration-tests/TEST-74-AUX-UTILS/TEST-74-AUX-UTILS.units',
'integration-tests/TEST-80-NOTIFYACCESS/TEST-80-NOTIFYACCESS.units',
]

View File

@ -1,30 +0,0 @@
#!/usr/bin/env bash
# SPDX-License-Identifier: LGPL-2.1-or-later
# shellcheck disable=SC2016
set -eux
set -o pipefail
systemctl start defer-reactivation.timer
timeout 20 bash -c 'until [[ -e /tmp/defer-reactivation.log ]]; do sleep .5; done'
timeout 60 bash -c 'until (( $(cat /tmp/defer-reactivation.log | wc -l) >= 3 )); do sleep 5; done'
systemctl stop defer-reactivation.timer
# If the 'date' command is the service called instantaneously when the timer is triggered, each time delta
# must be 10 seconds. But in a realistic situation, the command is slightly delayed after the timer is
# triggered, and the delay has some fluctuations. If a trigger event calls the command at 00:00:01.01, and
# the next event does at 00:00:10.99, the delta is calculated as 9 seconds. So, let's accept 9 here.
mindelta=9
last=
while read -r time; do
if [[ -n "$last" ]]; then
delta=$(( time - last ))
if (( delta < mindelta )); then
echo "Timer fired too early: $delta < $mindelta" >/failed
exit 1
fi
fi
last=$time
done </tmp/defer-reactivation.log

View File

@ -571,7 +571,7 @@ EnvironmentFile=-/usr/lib/systemd/systemd-asan-env
PrivateTmp=disconnected
BindPaths=/tmp/markers/
ExtensionDirectories=-${VDIR}
ExecStart=bash -x -c ' \\
ExecStart=bash -c ' \\
trap "{ \\
systemd-notify --reloading; \\
(ls /etc | grep marker || echo no-marker) >/tmp/markers/50g; \\
@ -612,7 +612,7 @@ EnvironmentFile=-/usr/lib/systemd/systemd-asan-env
PrivateTmp=disconnected
BindPaths=/tmp/markers/
ExtensionImages=-$VDIR2
ExecStart=bash -x -c ' \\
ExecStart=bash -c ' \\
trap "{ \\
systemd-notify --reloading; \\
(ls /etc | grep marker || echo no-marker) >/tmp/markers/50h; \\
@ -650,7 +650,7 @@ BindPaths=/tmp/markers/
RootImage=$MINIMAL_IMAGE.raw
ExtensionDirectories=-${VDIR}
NotifyAccess=all
ExecStart=bash -x -c ' \
ExecStart=bash -c ' \
trap '"'"' \
now=\$\$(grep "^now" /proc/timer_list | cut -d" " -f3 | rev | cut -c 4- | rev); \
printf "RELOADING=1\\nMONOTONIC_USEC=\$\${now}" | socat -t 5 - UNIX-SENDTO:\$\$NOTIFY_SOCKET; \
@ -685,7 +685,7 @@ BindPaths=/tmp/markers/
RootDirectory=/tmp/vpickminimg
ExtensionDirectories=-${VDIR}
NotifyAccess=all
ExecStart=bash -x -c ' \
ExecStart=bash -c ' \
trap '"'"' \
now=\$\$(grep "^now" /proc/timer_list | cut -d" " -f3 | rev | cut -c 4- | rev); \
printf "RELOADING=1\\nMONOTONIC_USEC=\$\${now}" | socat -t 5 - UNIX-SENDTO:\$\$NOTIFY_SOCKET; \
@ -715,7 +715,7 @@ RootImage=$MINIMAL_IMAGE.raw
ExtensionImages=-$VDIR2 /tmp/app0.raw
PrivateUsers=yes
NotifyAccess=all
ExecStart=bash -x -c ' \
ExecStart=bash -c ' \
trap '"'"' \
now=\$\$(grep "^now" /proc/timer_list | cut -d" " -f3 | rev | cut -c 4- | rev); \
printf "RELOADING=1\\nMONOTONIC_USEC=\$\${now}" | socat -t 5 - UNIX-SENDTO:\$\$NOTIFY_SOCKET; \

View File

@ -0,0 +1,23 @@
#!/usr/bin/env bash
# SPDX-License-Identifier: LGPL-2.1-or-later
set -eux
set -o pipefail
systemctl start realtime-test.timer
sleep 35
mindelta=10
last=
while read -r time; do
if [ -n "$last" ]; then
delta=$((time - last))
if [ "$delta" -lt $mindelta ]; then
echo "Timer fired too early: $delta < $mindelta" >/failed
break
fi
fi
last=$time
done </tmp/realtime-test.log
test ! -s /failed