return 0;
}
+static bool check_idle(void *userdata) {
+ Context *c = userdata;
+ assert(c);
+
+ return hashmap_isempty(c->clients);
+}
+
int main(int argc, char *argv[]) {
Context context = {};
Client *c;
if (r < 0)
goto finish;
- r = bus_event_loop_with_idle(context.event, context.bus, "org.freedesktop.DBus", DEFAULT_EXIT_USEC);
+ r = bus_event_loop_with_idle(context.event, context.bus, "org.freedesktop.DBus", DEFAULT_EXIT_USEC, check_idle, &context);
if (r < 0) {
log_error("Failed to run event loop: %s", strerror(-r));
goto finish;
goto finish;
}
- r = bus_event_loop_with_idle(event, bus, "org.freedesktop.hostname1", DEFAULT_EXIT_USEC);
+ r = bus_event_loop_with_idle(event, bus, "org.freedesktop.hostname1", DEFAULT_EXIT_USEC, NULL, NULL);
if (r < 0) {
log_error("Failed to run event loop: %s", strerror(-r));
goto finish;
return 0;
}
-int bus_event_loop_with_idle(sd_event *e, sd_bus *bus, const char *name, usec_t timeout) {
+int bus_event_loop_with_idle(
+ sd_event *e,
+ sd_bus *bus,
+ const char *name,
+ usec_t timeout,
+ check_idle_t check_idle,
+ void *userdata) {
bool exiting = false;
int r, code;
assert(name);
for (;;) {
+ bool idle;
+
r = sd_event_get_state(e);
if (r < 0)
return r;
-
if (r == SD_EVENT_FINISHED)
break;
- r = sd_event_run(e, exiting ? (uint64_t) -1 : timeout);
+ if (check_idle)
+ idle = check_idle(userdata);
+ else
+ idle = true;
+
+ r = sd_event_run(e, exiting || !idle ? (uint64_t) -1 : timeout);
if (r < 0)
return r;
int bus_async_unregister_and_exit(sd_event *e, sd_bus *bus, const char *name);
-int bus_event_loop_with_idle(sd_event *e, sd_bus *bus, const char *name, usec_t timeout);
+typedef bool (*check_idle_t)(void *userdata);
+
+int bus_event_loop_with_idle(sd_event *e, sd_bus *bus, const char *name, usec_t timeout, check_idle_t check_idle, void *userdata);
int bus_name_has_owner(sd_bus *c, const char *name, sd_bus_error *error);
goto finish;
}
- r = bus_event_loop_with_idle(event, bus, "org.freedesktop.locale1", DEFAULT_EXIT_USEC);
+ r = bus_event_loop_with_idle(event, bus, "org.freedesktop.locale1", DEFAULT_EXIT_USEC, NULL, NULL);
if (r < 0) {
log_error("Failed to run event loop: %s", strerror(-r));
goto finish;
goto finish;
}
- r = bus_event_loop_with_idle(event, bus, "org.freedesktop.timedate1", DEFAULT_EXIT_USEC);
+ r = bus_event_loop_with_idle(event, bus, "org.freedesktop.timedate1", DEFAULT_EXIT_USEC, NULL, NULL);
if (r < 0) {
log_error("Failed to run event loop: %s", strerror(-r));
goto finish;