]> git.ipfire.org Git - thirdparty/dovecot/core.git/commitdiff
Added API for waiting child processes.
authorTimo Sirainen <tss@iki.fi>
Tue, 21 Oct 2008 21:28:46 +0000 (00:28 +0300)
committerTimo Sirainen <tss@iki.fi>
Tue, 21 Oct 2008 21:28:46 +0000 (00:28 +0300)
--HG--
branch : HEAD

src/lib/Makefile.am
src/lib/child-wait.c [new file with mode: 0644]
src/lib/child-wait.h [new file with mode: 0644]

index eec90de88ea279fa7b36a0ea91e2aa526c0873ee..1a8b4abdf060460f8afbe3e0d84a3fe729bc980c 100644 (file)
@@ -15,6 +15,7 @@ liblib_a_SOURCES = \
        base64.c \
        bsearch-insert-pos.c \
        buffer.c \
+       child-wait.c \
        close-keep-errno.c \
        compat.c \
        crc32.c \
@@ -115,6 +116,7 @@ headers = \
        base64.h \
        bsearch-insert-pos.h \
        buffer.h \
+       child-wait.h \
        close-keep-errno.h \
        compat.h \
        crc32.h \
diff --git a/src/lib/child-wait.c b/src/lib/child-wait.c
new file mode 100644 (file)
index 0000000..97fab18
--- /dev/null
@@ -0,0 +1,108 @@
+/* Copyright (c) 2007-2008 Icecap authors, see the included COPYING file */
+
+#include "lib.h"
+#include "lib-signals.h"
+#include "hash.h"
+#include "child-wait.h"
+
+#include <sys/wait.h>
+
+struct child_wait {
+       unsigned int pid_count;
+
+       child_wait_callback_t *callback;
+       void *context;
+};
+
+struct hash_table *child_pids;
+
+#undef child_wait_new_with_pid
+struct child_wait *
+child_wait_new_with_pid(pid_t pid, child_wait_callback_t *callback,
+                       void *context)
+{
+       struct child_wait *wait;
+
+       wait = i_new(struct child_wait, 1);
+       wait->callback = callback;
+       wait->context = context;
+
+       if (pid != (pid_t)-1)
+               child_wait_add_pid(wait, pid);
+       return wait;
+}
+
+void child_wait_free(struct child_wait **_wait)
+{
+       struct child_wait *wait = *_wait;
+       struct hash_iterate_context *iter;
+       void *key, *value;
+
+       *_wait = NULL;
+
+       if (wait->pid_count > 0) {
+               /* this should be rare, so iterating hash is fast enough */
+               iter = hash_iterate_init(child_pids);
+               while (hash_iterate(iter, &key, &value)) {
+                       if (value == wait) {
+                               hash_remove(child_pids, key);
+                               if (--wait->pid_count == 0)
+                                       break;
+                       }
+               }
+               hash_iterate_deinit(&iter);
+       }
+
+       i_free(wait);
+}
+
+void child_wait_add_pid(struct child_wait *wait, pid_t pid)
+{
+       wait->pid_count++;
+       hash_insert(child_pids, POINTER_CAST(pid), wait);
+}
+
+void child_wait_remove_pid(struct child_wait *wait, pid_t pid)
+{
+       wait->pid_count--;
+       hash_remove(child_pids, POINTER_CAST(pid));
+}
+
+static void
+sigchld_handler(int signo ATTR_UNUSED, void *context ATTR_UNUSED)
+{
+       struct child_wait_status status;
+
+       while ((status.pid = waitpid(-1, &status.status, WNOHANG)) > 0) {
+               status.wait = hash_lookup(child_pids, POINTER_CAST(status.pid));
+               if (status.wait != NULL) {
+                       child_wait_remove_pid(status.wait, status.pid);
+                       status.wait->callback(&status, status.wait->context);
+               }
+       }
+
+       if (status.pid == -1 && errno != EINTR && errno != ECHILD)
+               i_error("waitpid() failed: %m");
+}
+
+void child_wait_init(void)
+{
+       child_pids = hash_create(default_pool, default_pool, 0, NULL, NULL);
+
+       lib_signals_set_handler(SIGCHLD, TRUE, sigchld_handler, NULL);
+}
+
+void child_wait_deinit(void)
+{
+       struct hash_iterate_context *iter;
+       void *key, *value;
+
+       lib_signals_unset_handler(SIGCHLD, sigchld_handler, NULL);
+
+       iter = hash_iterate_init(child_pids);
+       while (hash_iterate(iter, &key, &value))
+               i_free(value);
+       hash_iterate_deinit(&iter);
+
+       hash_destroy(&child_pids);
+}
diff --git a/src/lib/child-wait.h b/src/lib/child-wait.h
new file mode 100644 (file)
index 0000000..b5052af
--- /dev/null
@@ -0,0 +1,36 @@
+#ifndef CHILD_WAIT_H
+#define CHILD_WAIT_H
+
+struct child_wait_status {
+       struct child_wait *wait;
+
+       pid_t pid;
+       int status;
+};
+
+typedef void child_wait_callback_t(const struct child_wait_status *status,
+                                  void *context);
+
+struct child_wait *
+child_wait_new_with_pid(pid_t pid, child_wait_callback_t *callback,
+                       void *context);
+#ifdef CONTEXT_TYPE_SAFETY
+#  define child_wait_new_with_pid(pid, callback, context) \
+       ({(void)(1 ? 0 : callback((const struct child_wait_status *)0, \
+                                 context)); \
+         child_wait_new_with_pid(pid, (child_wait_callback_t *)callback, context); })
+#else
+#  define child_wait_new_with_pid(pid, callback, context) \
+         child_wait_new_with_pid(pid, (child_wait_callback_t *)callback, context)
+#endif
+#define child_wait_new(callback, context) \
+       child_wait_new_with_pid((pid_t)-1, callback, context)
+void child_wait_free(struct child_wait **wait);
+
+void child_wait_add_pid(struct child_wait *wait, pid_t pid);
+void child_wait_remove_pid(struct child_wait *wait, pid_t pid);
+
+void child_wait_init(void);
+void child_wait_deinit(void);
+
+#endif