I need to run an external program when some events happen (the node with the global IP crashes, I want to run a script to take over the global IP on a new host). It can be used to send administrators an e-mails or do whatever you like. = From: FUJITA Tomonori <fujita.tomonori at lab.ntt.co.jp> Subject: [PATCH] add a helper function to run an external program The new helper function can be used to run an external program when a specific event happens. Signed-off-by: FUJITA Tomonori <fujita.tomonori at lab.ntt.co.jp> --- usr/tgtd.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ usr/tgtd.h | 2 + 2 files changed, 65 insertions(+), 0 deletions(-) diff --git a/usr/tgtd.c b/usr/tgtd.c index 18ad7be..8fbefb1 100644 --- a/usr/tgtd.c +++ b/usr/tgtd.c @@ -252,6 +252,69 @@ void tgt_remove_sched_event(struct event_data *evt) } } +struct ext_prog_info { + void (*callback)(void *data, int result); + void *data; +}; + +static void run_ext_callback(int fd, int events, void *data) +{ + int ret, result; + struct ext_prog_info *ex = data; + + ret = read(fd, &result, sizeof(result)); + if (ret != sizeof(result)) { + result = -EINVAL; + eprintf("failed to get the result."); + } + + if (ex->callback) + ex->callback(ex->data, result); + + tgt_event_del(fd); + close(fd); + free(data); +} + +int run_ext_program(const char *cmd, + void (*callback)(void *data, int result), void *data) +{ + pid_t pid; + int fds[2], ret; + struct ext_prog_info *ex; + + ex = zalloc(sizeof(*ex)); + if (!ex) + return -ENOMEM; + + ret = pipe(fds); + if (ret < 0) { + free(ex); + return ret; + } + + eprintf("%d %d\n", fds[0], fds[1]); + + ex->callback = callback; + ex->data = data; + + tgt_event_add(fds[0], EPOLLIN, run_ext_callback, ex); + + pid = fork(); + if (pid < 0) + return pid; + + if (!pid) { + ret = system(cmd); + write(fds[1], &ret, sizeof(ret)); + return 0; + } + + close(fds[1]); + + return 0; +} + static int tgt_exec_scheduled(void) { struct list_head *last_sched; diff --git a/usr/tgtd.h b/usr/tgtd.h index b8541c8..9c44994 100644 --- a/usr/tgtd.h +++ b/usr/tgtd.h @@ -347,4 +347,6 @@ int do_tgt_event_add(int efd, struct list_head *list, int fd, int events, void do_tgt_event_del(int efd, struct list_head *list, int fd); int do_tgt_event_modify(int efd, struct list_head *list, int fd, int events); +int run_ext_program(const char *cmd, + void (*callback)(void *data, int result), void *data); #endif -- 1.6.5 -- To unsubscribe from this list: send the line "unsubscribe stgt" in the body of a message to majordomo at vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html |