From 76fdcece687af8d82e7b5a6c065988ab80c914cb Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Sat, 25 Apr 2015 19:59:32 +0200 Subject: bus_read invokes callback with null message to notify that it is listening on the bus MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/bus.c | 11 +++++++++++ src/bus.h | 6 ++++++ src/cmdline.c | 14 +++++++++----- 3 files changed, 26 insertions(+), 5 deletions(-) (limited to 'src') diff --git a/src/bus.c b/src/bus.c index 5875b2b..f4f62c2 100644 --- a/src/bus.c +++ b/src/bus.c @@ -629,6 +629,12 @@ fail: * * 0: stop listening * * 1: continue listening * * -1: an error has occurred + * However, the function [`bus_read`] will invoke + * `callback` with `message` one time directly after + * it has started listening on the bus. This is to + * the the program now it can safely continue with + * any action that requires that the programs is + * listening on the port. * @return 0 on success, -1 on error */ int @@ -636,6 +642,11 @@ bus_read(const bus_t *bus, int (*callback)(const char *message, void *user_data) { int r; t(release_semaphore(bus, S, SEM_UNDO)); + t(r = callback(NULL, user_data)); + if (!r) { + t(acquire_semaphore(bus, S, SEM_UNDO)); + return 0; + } for (;;) { t(release_semaphore(bus, Q, 0)); t(zero_semaphore(bus, Q)); diff --git a/src/bus.h b/src/bus.h index 267cac9..abae46b 100644 --- a/src/bus.h +++ b/src/bus.h @@ -162,6 +162,12 @@ int bus_write(const bus_t *bus, const char *message); * * 0: stop listening * * 1: continue listening * * -1: an error has occurred + * However, the function [`bus_read`] will invoke + * `callback` with `message` one time directly after + * it has started listening on the bus. This is to + * the the program now it can safely continue with + * any action that requires that the programs is + * listening on the port. * @return 0 on success, -1 on error */ int bus_read(const bus_t *bus, int (*callback)(const char *message, void *user_data), void *user_data); diff --git a/src/cmdline.c b/src/cmdline.c index 40d0136..f9505cc 100644 --- a/src/cmdline.c +++ b/src/cmdline.c @@ -59,8 +59,10 @@ static const char *command; static int spawn_continue(const char *message, void *user_data) { - pid_t pid = fork(); - if (pid) + pid_t pid; + if (!message) + return 1; + if ((pid = fork())) return pid == -1 ? -1 : 1; setenv("arg", message, 1); execlp("sh", "sh", "-c", command, NULL); @@ -75,13 +77,15 @@ spawn_continue(const char *message, void *user_data) * * @param message The received message * @param user_data Not used - * @return 0 (stop listening) on success, -1 on error + * @return 0 (stop listening) on success, -1 on error, or 1 if `message` is `NULL` */ static int spawn_break(const char *message, void *user_data) { - pid_t pid = fork(); - if (pid) + pid_t pid; + if (!message) + return 1; + if (pid = fork()) return pid == -1 ? -1 : 0; setenv("arg", message, 1); execlp("sh", "sh", "-c", command, NULL); -- cgit v1.2.3-70-g09d2