diff options
author | Keir Fraser <keir.fraser@citrix.com> | 2010-04-12 17:41:58 +0100 |
---|---|---|
committer | Keir Fraser <keir.fraser@citrix.com> | 2010-04-12 17:41:58 +0100 |
commit | aa8978e2d94470c0563f91df4c504446df82b9c6 (patch) | |
tree | f61be2e4c0b54d2d2f03b53c9c8d313b3d835efe /tools/xenstore/xs.c | |
parent | 25887df0dea37702586c16e1baf6681ed3ecd8de (diff) | |
download | xen-aa8978e2d94470c0563f91df4c504446df82b9c6.tar.gz xen-aa8978e2d94470c0563f91df4c504446df82b9c6.tar.bz2 xen-aa8978e2d94470c0563f91df4c504446df82b9c6.zip |
xenstore,libxl: cleanup of xenstore connections across fork()
Provide a new function xs_daemon_destroy_postfork which can be called
by a libxenstore user who has called fork, to close the fd for the
connection to xenstored and free the memory, without trying to do
anything to any threads which libxenstore may have created.
Use this new function in libxl_fork, to avoid accidental use of a
xenstore connection in both parent and child.
Also, fix the doc comment for libxl_spawn_spawn to have the success
return codes the right way round.
Signed-off-by: Ian Jackson <Ian.Jackson@eu.citrix.com>
Diffstat (limited to 'tools/xenstore/xs.c')
-rw-r--r-- | tools/xenstore/xs.c | 52 |
1 files changed, 33 insertions, 19 deletions
diff --git a/tools/xenstore/xs.c b/tools/xenstore/xs.c index 9707d19ca2..5b05268481 100644 --- a/tools/xenstore/xs.c +++ b/tools/xenstore/xs.c @@ -223,10 +223,39 @@ struct xs_handle *xs_domain_open(void) return get_handle(xs_domain_dev()); } -void xs_daemon_close(struct xs_handle *h) -{ +static void close_free_msgs(struct xs_handle *h) { struct xs_stored_msg *msg, *tmsg; + list_for_each_entry_safe(msg, tmsg, &h->reply_list, list) { + free(msg->body); + free(msg); + } + + list_for_each_entry_safe(msg, tmsg, &h->watch_list, list) { + free(msg->body); + free(msg); + } +} + +static void close_fds_free(struct xs_handle *h) { + if (h->watch_pipe[0] != -1) { + close(h->watch_pipe[0]); + close(h->watch_pipe[1]); + } + + close(h->fd); + + free(h); +} + +void xs_daemon_destroy_postfork(struct xs_handle *h) +{ + close_free_msgs(h); + close_fds_free(h); +} + +void xs_daemon_close(struct xs_handle *h) +{ mutex_lock(&h->request_mutex); mutex_lock(&h->reply_mutex); mutex_lock(&h->watch_mutex); @@ -239,28 +268,13 @@ void xs_daemon_close(struct xs_handle *h) } #endif - list_for_each_entry_safe(msg, tmsg, &h->reply_list, list) { - free(msg->body); - free(msg); - } - - list_for_each_entry_safe(msg, tmsg, &h->watch_list, list) { - free(msg->body); - free(msg); - } + close_free_msgs(h); mutex_unlock(&h->request_mutex); mutex_unlock(&h->reply_mutex); mutex_unlock(&h->watch_mutex); - if (h->watch_pipe[0] != -1) { - close(h->watch_pipe[0]); - close(h->watch_pipe[1]); - } - - close(h->fd); - - free(h); + close_fds_free(h); } static bool read_all(int fd, void *data, unsigned int len) |