+/**
+ * Internal helper function for 'jobs_call'.
+ * @see jobs_call, jobs_enter, jobs_leave
+ */
+static void call_cb(int signum, void *closure, struct jobloop *jobloop)
+{
+ struct sync *sync = closure;
+ sync->callback(signum, sync->arg);
+ jobs_leave(jobloop);
+}
+
+/**
+ * Calls synchronously the job represented by 'callback' and 'arg1'
+ * for the 'group' and the 'timeout' and waits for its completion.
+ * @param group The group of the job or NULL when no group.
+ * @param timeout The maximum execution time in seconds of the job
+ * or 0 for unlimited time.
+ * @param callback The function to execute for achieving the job.
+ * Its first parameter is either 0 on normal flow
+ * or the signal number that broke the normal flow.
+ * The remaining parameter is the parameter 'arg1'
+ * given here.
+ * @param arg The second argument for 'callback'
+ * @return 0 in case of success or -1 in case of error
+ */
+int jobs_call(
+ void *group,
+ int timeout,
+ void (*callback)(int, void*),
+ void *arg)
+{
+ struct sync sync;
+
+ sync.callback = callback;
+ sync.arg = arg;
+ return jobs_enter(group, timeout, call_cb, &sync);
+}
+
+/**
+ * Gets a sd_event item for the current thread.
+ * @return a sd_event or NULL in case of error
+ */
+struct sd_event *jobs_get_sd_event()
+{
+ struct events *events;
+ struct thread *me;
+ int rc;
+
+ pthread_mutex_lock(&mutex);
+
+ /* search events on stack */
+ me = current;
+ while (me && !me->events)
+ me = me->upper;
+ if (me)
+ /* return the stacked events */
+ events = me->events;
+ else {
+ /* search an available events */
+ events = events_get();
+ if (!events) {
+ /* not found, check if creation possible */
+ if (nevents >= allowed) {
+ ERROR("not possible to add a new event");
+ events = NULL;
+ } else {
+ events = malloc(sizeof *events);
+ if (events && (rc = sd_event_new(&events->event)) >= 0) {
+ if (nevents < started || start_one_thread() >= 0) {
+ events->runs = 0;
+ events->next = first_events;
+ first_events = events;
+ } else {
+ ERROR("can't start thread for events");
+ sd_event_unref(events->event);
+ free(events);
+ events = NULL;
+ }
+ } else {
+ if (!events) {
+ ERROR("out of memory");
+ errno = ENOMEM;
+ } else {
+ free(events);
+ ERROR("creation of sd_event failed: %m");
+ events = NULL;
+ errno = -rc;
+ }
+ }
+ }
+ }
+ if (events) {
+ /* */
+ me = current;
+ if (me) {
+ events->runs = 1;
+ me->events = events;
+ } else {
+ WARNING("event returned for unknown thread!");
+ }
+ }