1
1
mirror of https://github.com/systemd/systemd-stable.git synced 2025-08-26 17:50:11 +03:00

sd-event: check clock argument to sd_event_now()

sd_event_now() is a public function, so we must check all
arguments for validity. Update man page and add tests.

Sample debug message:
Assertion 'IN_SET(clock, CLOCK_REALTIME, CLOCK_REALTIME_ALARM, CLOCK_MONOTONIC, CLOCK_BOOTTIME, CLOCK_BOOTTIME_ALARM)' failed at src/libsystemd/sd-event/sd-event.c:2719, function sd_event_now(). Ignoring.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2016-01-11 17:19:25 -05:00
parent 45ea658326
commit 2c86ba5a51
3 changed files with 42 additions and 3 deletions

View File

@ -114,13 +114,19 @@
</varlistentry>
<varlistentry>
<term><constant>-EOPNOTSUPP</constant></term>
<listitem><para>Unsupported clock type.
</para></listitem>
</varlistentry>
<varlistentry>
<term><constant>-ECHILD</constant></term>
<listitem><para>The event loop object was created in a
different process.</para></listitem>
</varlistentry>
</variablelist>
</refsect1>

View File

@ -2751,6 +2751,12 @@ _public_ int sd_event_now(sd_event *e, clockid_t clock, uint64_t *usec) {
assert_return(e, -EINVAL);
assert_return(usec, -EINVAL);
assert_return(!event_pid_changed(e), -ECHILD);
assert_return(IN_SET(clock,
CLOCK_REALTIME,
CLOCK_REALTIME_ALARM,
CLOCK_MONOTONIC,
CLOCK_BOOTTIME,
CLOCK_BOOTTIME_ALARM), -EOPNOTSUPP);
if (!dual_timestamp_is_set(&e->timestamp)) {
/* Implicitly fall back to now() if we never ran
@ -2770,8 +2776,7 @@ _public_ int sd_event_now(sd_event *e, clockid_t clock, uint64_t *usec) {
*usec = e->timestamp.monotonic;
break;
case CLOCK_BOOTTIME:
case CLOCK_BOOTTIME_ALARM:
default:
*usec = e->timestamp_boottime;
break;
}

View File

@ -264,6 +264,30 @@ static void test_basic(void) {
safe_close_pair(k);
}
static void test_sd_event_now(void) {
_cleanup_(sd_event_unrefp) sd_event *e = NULL;
uint64_t event_now;
assert_se(sd_event_new(&e) >= 0);
assert_se(sd_event_now(e, CLOCK_MONOTONIC, &event_now) > 0);
assert_se(sd_event_now(e, CLOCK_REALTIME, &event_now) > 0);
assert_se(sd_event_now(e, CLOCK_REALTIME_ALARM, &event_now) > 0);
assert_se(sd_event_now(e, CLOCK_BOOTTIME, &event_now) > 0);
assert_se(sd_event_now(e, CLOCK_BOOTTIME_ALARM, &event_now) > 0);
assert_se(sd_event_now(e, -1, &event_now) == -EOPNOTSUPP);
assert_se(sd_event_now(e, 900 /* arbitrary big number */, &event_now) == -EOPNOTSUPP);
assert_se(sd_event_run(e, 0) == 0);
assert_se(sd_event_now(e, CLOCK_MONOTONIC, &event_now) == 0);
assert_se(sd_event_now(e, CLOCK_REALTIME, &event_now) == 0);
assert_se(sd_event_now(e, CLOCK_REALTIME_ALARM, &event_now) == 0);
assert_se(sd_event_now(e, CLOCK_BOOTTIME, &event_now) == 0);
assert_se(sd_event_now(e, CLOCK_BOOTTIME_ALARM, &event_now) == 0);
assert_se(sd_event_now(e, -1, &event_now) == -EOPNOTSUPP);
assert_se(sd_event_now(e, 900 /* arbitrary big number */, &event_now) == -EOPNOTSUPP);
}
static int last_rtqueue_sigval = 0;
static int n_rtqueue = 0;
@ -324,7 +348,11 @@ static void test_rtqueue(void) {
int main(int argc, char *argv[]) {
log_set_max_level(LOG_DEBUG);
log_parse_environment();
test_basic();
test_sd_event_now();
test_rtqueue();
return 0;