(no subject)
Quoted 1 line
From Ian Jackson <[email protected]> # This line is ignored.
From: Ian Jackson <[email protected]>
Subject: [RFC PATCH 0/6] libxl: New child process handling In-Reply-To:
This is the first draft of my new libxl API for child process handling.
1/6 libxl: Fix leak of ctx->lock
2/6 libxl: abolish libxl_ctx_postfork
3/6 tools: Correct PTHREAD options in config/StdGNU.mk 4/6 libxl: Protect fds with CLOEXEC even with forking threads 5/6 libxl: libxl_event.c:beforepoll_internal, REQUIRE_FDS 6/6 libxl: event API: new facilities for waiting for subprocesses
Please comment. I'm particularly keen on comments about:
* The correctness of the pthread_atfork logic in 4/6.
* The sufficiency for applications of the child handling API in 6/6.
* The portability of the pthread command line option changes in 3/6.
* Correctness :-).
If you want to apply it to make sense of, you'll need to know that this series is on top of my recently-posted 3-patch libxl event fixup series.
Thanks,
Ian.
PATCH 1/6 libxl: Fix leak of ctx->lock
A mutex created with pthread_mutex_init, like ctx->lock, may need to be destroyed with pthread_mutex_destroy.
Also, previously, if libxl__init_recursive_mutex failed, the nascent ctx would be leaked. Add some comments which will hopefully make these kind of mistakes less likely in future.
--- tools/libxl/libxl.c | 17 +++++++++++++---- 1 files changed, 13 insertions(+), 4 deletions(-)
tools/libxl/libxl.c +13 -4
@@ -39,10 +39,7 @@ int libxl_ctx_alloc(libxl_ctx **pctx, int version,memset (ctx,0 ,sizeof (libxl_ctx)); ctx->lg = lg; -if (libxl__init_recursive_mutex (ctx, &ctx->lock) <0 ) { -LIBXL__LOG (ctx,LIBXL__LOG_ERROR ,"Failed to initialize mutex" ); -return ERROR_FAIL ; - } +/* First initialise pointers (cannot fail) */ LIBXL_TAILQ_INIT (&ctx->occurred);
@@ -61,6 +58,16 @@ int libxl_ctx_alloc(libxl_ctx **pctx, int version,LIBXL_TAILQ_INIT (&ctx->death_list);libxl__ev_xswatch_init (&ctx->death_watch); +/* The mutex is special because we can't idempotently destroy it */ + +if (libxl__init_recursive_mutex (ctx, &ctx->lock) <0 ) { +LIBXL__LOG (ctx,LIBXL__LOG_ERROR ,"Failed to initialize mutex" ); +free (ctx); + ctx =0 ; + } + +/* Now ctx is safe for ctx_free; failures simply set rc and "goto out" */ + rc =libxl__poller_init (ctx, &ctx->poller_app);if (rc) goto out;
@@ -150,6 +157,8 @@ int libxl_ctx_free(libxl_ctx *ctx)discard_events (&ctx->occurred); +pthread_mutex_destroy (&ctx->lock); +GC_FREE ;free (ctx);return 0 ;
Signature · 1 line
1.7.2.5
Re: [PATCH 1/6] libxl: Fix leak of ctx->lock
Quoted 8 lines · Ian Jackson
A mutex created with
pthread_mutex_init, likectx->lock, may need to be destroyed withpthread_mutex_destroy.Also, previously, if
libxl__init_recursive_mutexfailed, the nascent ctx would be leaked. Add some comments which will hopefully make these kind of mistakes less likely in future.Signed-off-by Ian Jackson <[email protected]>
Quoted 46 lines
--- tools/libxl/libxl.c | 17 +++++++++++++---- 1 files changed, 13 insertions(+), 4 deletions(-)tools/libxl/libxl.c +13
-4@@ -39,10 +39,7 @@ int libxl_ctx_alloc(libxl_ctx **pctx, int version,memset (ctx,0 ,sizeof (libxl_ctx)); ctx->lg = lg; -if (libxl__init_recursive_mutex (ctx, &ctx->lock) <0 ) { -LIBXL__LOG (ctx,LIBXL__LOG_ERROR ,"Failed to initialize mutex" ); -return ERROR_FAIL ; - } +/* First initialise pointers (cannot fail) */ LIBXL_TAILQ_INIT (&ctx->occurred);@@ -61,6 +58,16 @@ int libxl_ctx_alloc(libxl_ctx **pctx, int version,LIBXL_TAILQ_INIT (&ctx->death_list);libxl__ev_xswatch_init (&ctx->death_watch); +/* The mutex is special because we can't idempotently destroy it */ + +if (libxl__init_recursive_mutex (ctx, &ctx->lock) <0 ) { +LIBXL__LOG (ctx,LIBXL__LOG_ERROR ,"Failed to initialize mutex" ); +free (ctx); + ctx =0 ; + } + +/* Now ctx is safe for ctx_free; failures simply set rc and "goto out" */ + rc =libxl__poller_init (ctx, &ctx->poller_app);if (rc) goto out;@@ -150,6 +157,8 @@ int libxl_ctx_free(libxl_ctx *ctx)discard_events (&ctx->occurred); +pthread_mutex_destroy (&ctx->lock); +GC_FREE ;free (ctx);return 0 ;
PATCH 2/6 libxl: abolish libxl_ctx_postfork
In reply to Ian Jacksonlibxl's task has become too complicated (particularly in the presence of both forking and multithreading) to support reuse of the same libxl_ctx after fork.
So abolish libxl_ctx_fork. xl instead simply initialises a new libxl_ctx.
--- tools/libxl/libxl.h | 1 - tools/libxl/libxl_utils.c | 7 ------- tools/libxl/xl.c | 8 ++++++++ tools/libxl/xl.h | 1 + tools/libxl/xl_cmdimpl.c | 8 ++------ 5 files changed, 11 insertions(+), 14 deletions(-)
tools/libxl/libxl.h +0 -1
@@ -312,7 +312,6 @@ int libxl_ctx_alloc(libxl_ctx **pctx, int version,unsigned flags/* none currently defined */ , xentoollog_logger *lg);int libxl_ctx_free (libxl_ctx *ctx/* 0 is OK */ ); -int libxl_ctx_postfork (libxl_ctx *ctx);/* domain related functions */ int libxl_init_create_info (libxl_ctx *ctx,libxl_domain_create_info *c_info);
tools/libxl/libxl_utils.c +0 -7
@@ -399,13 +399,6 @@ READ_WRITE_EXACTLY(read, 1, /* */) READ_WRITE_EXACTLY(write, 0, const) -int libxl_ctx_postfork (libxl_ctx *ctx) { -if (ctx->xsh)xs_daemon_destroy_postfork (ctx->xsh); - ctx->xsh =xs_daemon_open (); -if (!ctx->xsh)return ERROR_FAIL ; -return 0 ; -} -pid_t libxl_fork (libxl_ctx *ctx) {pid_t pid;
tools/libxl/xl.c +8 -0
@@ -95,6 +95,14 @@ static void parse_global_config(const char *configfile,xlu_cfg_destroy (config); } +void postfork (void ) +{ +if (libxl_ctx_alloc (&ctx,LIBXL_VERSION ,0 , (xentoollog_logger *)logger)) { +fprintf (stderr,"cannot reinit xl context after fork\n" ); +exit (-1 ); + } +} +int main (int argc,char **argv) {int opt =0 ;
tools/libxl/xl.h +1 -0
@@ -104,6 +104,7 @@ struct cmd_spec *cmdtable_lookup(const char *s);extern libxl_ctx *ctx;extern xentoollog_logger_stdiostream *logger; +void postfork (void );/* global options */ extern int autoballoon;
tools/libxl/xl_cmdimpl.c +2 -6
@@ -1422,7 +1422,7 @@ static int autoconnect_console(libxl_ctx *ctx, uint32_t domid, void *priv) }else if (*pid >0 )return 0 ; -libxl_ctx_postfork (ctx); +postfork ();sleep (1 );libxl_primary_console_exec (ctx, domid);
@@ -1709,11 +1709,7 @@ start: goto out; } - rc =libxl_ctx_postfork (ctx); -if (rc) { -LOG ("failed to reinitialise context after fork" ); -exit (-1 ); - } +postfork ();if (asprintf (&name,"xl-%s" , d_config.c_info.name) <0 ) {LOG ("Failed to allocate memory in asprintf" );
Signature · 1 line
1.7.2.5
Re: [PATCH 2/6] libxl: abolish libxl_ctx_postfork
Quoted 8 lines · Ian Jackson
libxl's task has become too complicated (particularly in the presence of both forking and multithreading) to support reuse of the same
libxl_ctxafter fork.So abolish
libxl_ctx_fork. xl instead simply initialises a newlibxl_ctx.Signed-off-by Ian Jackson <[email protected]>
PATCH 3/6 tools: Correct PTHREAD options in config/StdGNU.mk
In reply to Ian JacksonIt is not correct to say -lpthread. The correct option is -pthread, which may have sundry other effects on code generation etc. It needs to be passed both to compilation and linking.
So abolish PTHREAD_LIBS in StdGNU.mk, and add PTHREAD_CFLAGS and PTHREAD_LDFLAGS instead. Fix the one user (libxc).
There are still some other users in tree which pass -pthread or -lpthread by adding it as a literal to their own compiler options. These will be fixed in a later version of this patch.
--- config/StdGNU.mk | 4 +++- tools/libxc/Makefile | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-)
config/StdGNU.mk +3 -1
@@ -68,10 +68,12 @@ XEN_SCRIPT_DIR = $(XEN_CONFIG_DIR)/scripts SOCKET_LIBS = CURSES_LIBS = -lncurses -PTHREAD_LIBS = -lpthread UTIL_LIBS = -lutil DLOPEN_LIBS = -ldl +PTHREAD_CFLAGS = -pthread +PTHREAD_LDFLAGS = -pthread + SONAME_LDFLAG = -soname SHLIB_LDFLAGS = -shared
tools/libxc/Makefile +3 -1
@@ -72,6 +72,8 @@ CFLAGS += -I. $(CFLAGS_xeninclude) # Needed for posix_fadvise64() in xc_linux.c CFLAGS-$(CONFIG_Linux) += -D_GNU_SOURCE +CFLAGS += $(PTHREAD_CFLAGS) + # Define this to make it possible to run valgrind on code linked with these # libraries. #CFLAGS += -DVALGRIND -O0 -ggdb3
@@ -156,7 +158,7 @@ libxenctrl.so.$(MAJOR): libxenctrl.so.$(MAJOR).$(MINOR) ln -sf $< $@ libxenctrl.so.$(MAJOR).$(MINOR): $(CTRL_PIC_OBJS) - $(CC) $(LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenctrl.so.$(MAJOR) $(SHLIB_LDFLAGS) -o $@ $^ $(DLOPEN_LIBS) $(PTHREAD_LIBS) $(APPEND_LDFLAGS) + $(CC) $(LDFLAGS) $(PTHREAD_LDFLAGS) -Wl,$(SONAME_LDFLAG) -Wl,libxenctrl.so.$(MAJOR) $(SHLIB_LDFLAGS) -o $@ $^ $(DLOPEN_LIBS) $(PTHREAD_LIBS) $(APPEND_LDFLAGS) # libxenguest
Signature · 1 line
1.7.2.5
PATCH 4/6 libxl: Protect fds with CLOEXEC even with forking threads
In reply to Ian JacksonWe introduce a new "carefd" concept, which relates to fds that we care about not being inherited by long-lived children.
As yet we do not use this anywherein libxl. Until all locations in libxl which make such fds are converted, libxl__postfork may not work entirely properly. If these locations do not use O_CLOEXEC (or use calls for which there is no O_CLOEXEC) then multithreaded programs may not work properly.
This introduces a new API call libxl_postfork_child_noexec which must be called by applications which make long-running non-execing children. Add the appropriate call to xl's postfork function.
On Linux pthread_atfork demands compilation with -pthread, so add PTHREAD_CFLAGS, _LDFLAGS, _LIBS to the corresponding variables in the libxl Makefile. It is not clear why we got away without these before.
--- tools/libxl/Makefile | 6 +- tools/libxl/libxl.c | 3 + tools/libxl/libxl_event.h | 10 +++ tools/libxl/libxl_fork.c | 167 ++++++++++++++++++++++++++++++++++++++++++ tools/libxl/libxl_internal.h | 65 ++++++++++++++++ tools/libxl/xl.c | 3 + 6 files changed, 253 insertions(+), 1 deletions(-) create mode 100644 tools/libxl/libxl_fork.c
tools/libxl/Makefile +5 -1
@@ -26,6 +26,10 @@ endif LIBXL_LIBS = LIBXL_LIBS = $(LDLIBS_libxenctrl) $(LDLIBS_libxenguest) $(LDLIBS_libxenstore) $(LDLIBS_libblktapctl) $(UTIL_LIBS) $(LIBUUID_LIBS) +CFLAGS += $(PTHREAD_CFLAGS) +LDFLAGS += $(PTHREAD_LDFLAGS) +LIBXL_LIBS += $(PTHREAD_LIBS) + LIBXLU_LIBS = LIBXL_OBJS-y = osdeps.o libxl_paths.o libxl_bootloader.o flexarray.o
@@ -53,7 +57,7 @@ LIBXL_LIBS += -lyajl LIBXL_OBJS = flexarray.o libxl.o libxl_create.o libxl_dm.o libxl_pci.o \ libxl_dom.o libxl_exec.o libxl_xshelp.o libxl_device.o \ libxl_internal.o libxl_utils.o libxl_uuid.o libxl_json.o \ - libxl_qmp.o libxl_event.o $(LIBXL_OBJS-y) + libxl_qmp.o libxl_event.o libxl_fork.o $(LIBXL_OBJS-y) LIBXL_OBJS += _libxl_types.o libxl_flask.o _libxl_types_internal.o $(LIBXL_OBJS): CFLAGS += $(CFLAGS_libxenctrl) $(CFLAGS_libxenguest) $(CFLAGS_libxenstore) $(CFLAGS_libblktapctl)
tools/libxl/libxl.c +3 -0
@@ -68,6 +68,9 @@ int libxl_ctx_alloc(libxl_ctx **pctx, int version,/* Now ctx is safe for ctx_free; failures simply set rc and "goto out" */ + rc =libxl__atfork_init (ctx); +if (rc) goto out; + rc =libxl__poller_init (ctx, &ctx->poller_app);if (rc) goto out;
tools/libxl/libxl_event.h +10 -0
@@ -369,6 +369,16 @@ void libxl_osevent_occurred_fd(libxl_ctx *ctx, void *for_libxl,*/ void libxl_osevent_occurred_timeout (libxl_ctx *ctx,void *for_libxl); + +/* +* An application which initialises a libxl_ctx in a parent process +* and then forks a child which does not quickly exec, must +* instead libxl__postfork_child_noexec in the child. One call +* for all libxl contexts is sufficient. +*/ +void libxl_postfork_child_noexec (libxl_ctx *ctx); + +#endif /*
tools/libxl/libxl_fork.c new file +167 -0
167 lines not on this page · raw message
tools/libxl/libxl_internal.h +65 -0
77 lines not on this page · raw message
tools/libxl/xl.c +3 -0
@@ -97,6 +97,9 @@ static void parse_global_config(const char *configfile,void postfork (void ) { +libxl_postfork_child_noexec (ctx);/* in case we don't exit/exec */ + ctx =0 ; +if (libxl_ctx_alloc (&ctx,LIBXL_VERSION ,0 , (xentoollog_logger *)logger)) {fprintf (stderr,"cannot reinit xl context after fork\n" );exit (-1 );
Signature · 1 line
1.7.2.5
Re: [PATCH 4/6] libxl: Protect fds with CLOEXEC even with forking threads
Quoted 4 lines · Ian Jackson
We introduce a new "carefd" concept, which relates to fds that we care about not being inherited by long-lived children.
As yet we do not use this anywherein libxl. Until all locations in
"anywherein"
Quoted 5 lines
libxl which make such fds are converted,
libxl__postforkmay not work entirely properly. If these locations do not useO_CLOEXEC(or use calls for which there is noO_CLOEXEC) then multithreaded programs may not work properly.
Quoted 7 lines
This introduces a new API call
libxl_postfork_child_noexecwhich must be called by applications which make long-running non-execing children. Add the appropriate call to xl's postfork function.On Linux
pthread_atforkdemands compilation with -pthread, so addPTHREAD_CFLAGS,_LDFLAGS,_LIBSto the corresponding variables in the libxlMakefile. It is not clear why we got away without these before.
Since I assume the Makefile bit could safely be split out and applied, that bit is:
Quoted 6 lines
tools/libxl/libxl_event.h +0
-0@@ -369,6 +369,16 @@ void libxl_osevent_occurred_fd(libxl_ctx *ctx, void *for_libxl,*/
tools/libxl/libxl_event.h void libxl_osevent_occurred_fd(libxl_ctx *ctx, void *for_libxl, · line 379
Quoted 10 lines
tools/libxl/libxl_event.h +8
-0void libxl_osevent_occurred_timeout (libxl_ctx *ctx,void *for_libxl); + +/* +* An application which initialises a libxl_ctx in a parent process +* and then forks a child which does not quickly exec, must +* instead libxl__postfork_child_noexec in the child. One call +* for all libxl contexts is sufficient. +*/ +void libxl_postfork_child_noexec (libxl_ctx *ctx);
Is the ctx passed here required to be one of the existing ctx's or a newly allocated one in this context?
Quoted 45 lines
tools/libxl/libxl_event.h +2
-0+ +#endif /* tools/libxl/libxl_fork.c new file +34
-0@@ -0,0 +1,167 @@ +/* +* Copyright (C) 2012 Citrix Ltd. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU Lesser General Public License as published +* by the Free Software Foundation; version 2.1 only. with the special +* exception on linking described in file LICENSE. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Lesser General Public License for more details. +*/ +/* +* Internal child process machinery for use by other parts of libxl +*/ + +#include "libxl_osdeps.h" /* must come before any other headers */ + +#include "libxl_internal.h" + +/* +* carefd arrangements +* +* carefd_begin and _unlock take out the no_forking lock, which we +* also take and release in our pthread_atfork handlers. So when this +* lock is held the whole process cannot fork. We therefore protect +* our fds from leaking into children made by other threads. +* +* We maintain a list of all the carefds, so that if the application +* wants to fork a long-running but non-execing child, we can close +* them all. +* +* So the record function sets CLOEXEC for the benefit of execing
tools/libxl/libxl_fork.c line 44
Quoted 10 lines
tools/libxl/libxl_fork.c new file +10
-0+* children, and makes a note of the fd for the benefit of non-execing +* ones. +*/ + +struct libxl__carefd { +LIBXL_LIST_ENTRY (libxl__carefd) entry; +int fd; +}; + +static pthread_mutex_t no_forking =PTHREAD_MUTEX_INITIALIZER ;
We previously had trouble with PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP not being defined on NetBSD.
http://www.daemon-systems.org/man/pthread_mutex_init.3.html suggests PTHREAD_MUTEX_INITIALIZER is but perhaps we should confirm?
tools/libxl/libxl_fork.c line 45
Quoted 1 line
tools/libxl/libxl_fork.c new file +1
-0+static int atfork_registered;
Does pthread_atfork persist into the children?
If the parent has set this then it will be set for the child too, which if the answer to my question is "No" is a problem.
I suspect the answer is yes though.
Quoted 11 lines
tools/libxl/libxl_fork.c new file +11
-0+static LIBXL_LIST_HEAD(, libxl__carefd) carefds = +LIBXL_LIST_HEAD_INITIALIZER (carefds); + +static void atfork_lock (void ) +{ +int r =pthread_mutex_lock (&no_forking); +assert (!r); +} + +static void atfork_unlock (void ) +{
tools/libxl/libxl_fork.c line 66
Quoted 10 lines
tools/libxl/libxl_fork.c new file +10
-0+int r =pthread_mutex_unlock (&no_forking); +assert (!r); +} + +int libxl__atfork_init (libxl_ctx *ctx) +{ +int r, rc; + +atfork_lock (); +if (atfork_registered)
Missing an unlock?
Quoted 8 lines
tools/libxl/libxl_fork.c new file +8
-0+return 0 ; + + r =pthread_atfork (atfork_lock, atfork_unlock, atfork_unlock); +if (r) { +LIBXL__LOG_ERRNO (ctx,LIBXL__LOG_ERROR ,"pthread_atfork failed" ); + rc =ERROR_NOMEM ; + goto out; + }
tools/libxl/libxl_fork.c line 84
Quoted 10 lines
tools/libxl/libxl_fork.c new file +10
-0+ + atfork_registered =1 ; + rc =0 ; + out: +atfork_unlock (); +return rc; +} + +void libxl__carefd_begin (void ) {atfork_lock (); } +void libxl__carefd_unlock (void ) {atfork_unlock (); }
This naming seems asymmetric.
Ian.
Re: [PATCH 4/6] libxl: Protect fds with CLOEXEC even with forking threads
2012/2/20 Ian Campbell <[email protected]>:
Quoted 104 lines · Ian Jackson
Quoted 4 lines
We introduce a new "carefd" concept, which relates to fds that we care about not being inherited by long-lived children.
As yet we do not use this anywherein libxl. Until all locations in
"anywherein"
Quoted 5 lines
libxl which make such fds are converted,
libxl__postforkmay not work entirely properly. If these locations do not useO_CLOEXEC(or use calls for which there is noO_CLOEXEC) then multithreaded programs may not work properly.Quoted 7 lines
This introduces a new API call
libxl_postfork_child_noexecwhich must be called by applications which make long-running non-execing children. Add the appropriate call to xl's postfork function.On Linux
pthread_atforkdemands compilation with -pthread, so addPTHREAD_CFLAGS,_LDFLAGS,_LIBSto the corresponding variables in the libxlMakefile. It is not clear why we got away without these before.Since I assume the
Makefilebit could safely be split out and applied, that bit is:Acked-by Ian Campbell <[email protected]>Quoted 6 lines
tools/libxl/libxl_event.h +0
-0@@ -369,6 +369,16 @@ void libxl_osevent_occurred_fd(libxl_ctx *ctx, void *for_libxl,*/ tools/libxl/libxl_event.h void libxl_osevent_occurred_fd(libxl_ctx *ctx, void *for_libxl, · line 379
Quoted 10 lines
tools/libxl/libxl_event.h +8
-0void libxl_osevent_occurred_timeout(libxl_ctx *ctx, void *for_libxl); + +/* +* An application which initialises a libxl_ctx in a parent process +* and then forks a child which does not quickly exec, must +* instead libxl__postfork_child_noexec in the child. One call +* for all libxl contexts is sufficient. +*/ +void libxl_postfork_child_noexec (libxl_ctx *ctx);Is the ctx passed here required to be one of the existing ctx's or a newly allocated one in this context?
Quoted 45 lines
tools/libxl/libxl_event.h +2
-0+ + #endif /*tools/libxl/libxl_fork.c new file +34
-0@@ -0,0 +1,167 @@ +/* +* Copyright (C) 2012 Citrix Ltd. +* +* This program is free software; you can redistribute it and/or modify +* it under the terms of the GNU Lesser General Public License as published +* by the Free Software Foundation; version 2.1 only. with the special +* exception on linking described in file LICENSE. +* +* This program is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU Lesser General Public License for more details. +*/ +/* +* Internal child process machinery for use by other parts of libxl +*/ + +#include "libxl_osdeps.h" /* must come before any other headers */ + +#include "libxl_internal.h" + +/* +* carefd arrangements +* +* carefd_begin and _unlock take out the no_forking lock, which we +* also take and release in our pthread_atfork handlers. So when this +* lock is held the whole process cannot fork. We therefore protect +* our fds from leaking into children made by other threads. +* +* We maintain a list of all the carefds, so that if the application +* wants to fork a long-running but non-execing child, we can close +* them all. +* +* So the record function sets CLOEXEC for the benefit of execing tools/libxl/libxl_fork.c line 44
Quoted 10 lines
tools/libxl/libxl_fork.c new file +10
-0+* children, and makes a note of the fd for the benefit of non-execing +* ones. +*/ + +struct libxl__carefd { + LIBXL_LIST_ENTRY(libxl__carefd) entry; + int fd; +}; + +static pthread_mutex_t no_forking =PTHREAD_MUTEX_INITIALIZER ;We previously had trouble with
PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NPnot being defined on NetBSD.
Quoted 2 lines
http://www.daemon-systems.org/man/pthread_mutex_init.3.html suggests
PTHREAD_MUTEX_INITIALIZERis but perhaps we should confirm?
Just tested on a NetBSD system, and PTHREAD_MUTEX_INITIALIZER macro is allowed.
Quoted 54 lines
tools/libxl/libxl_fork.c line 45
Quoted 1 line
tools/libxl/libxl_fork.c new file +1
-0+static int atfork_registered;Does
pthread_atforkpersist into the children?If the parent has set this then it will be set for the child too, which if the answer to my question is "No" is a problem.
I suspect the answer is yes though.
Quoted 11 lines
tools/libxl/libxl_fork.c new file +11
-0+static LIBXL_LIST_HEAD(, libxl__carefd) carefds = + LIBXL_LIST_HEAD_INITIALIZER(carefds); + +static void atfork_lock (void ) +{ + int r = pthread_mutex_lock(&no_forking); + assert(!r); +} + +static void atfork_unlock (void ) +{tools/libxl/libxl_fork.c line 66
Quoted 10 lines
tools/libxl/libxl_fork.c new file +10
-0+ int r = pthread_mutex_unlock(&no_forking); + assert(!r); +} + +int libxl__atfork_init (libxl_ctx *ctx) +{ + int r, rc; + + atfork_lock(); + if (atfork_registered)Missing an unlock?
Quoted 8 lines
tools/libxl/libxl_fork.c new file +8
-0+ return 0; + + r = pthread_atfork(atfork_lock, atfork_unlock, atfork_unlock); + if (r) { + LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_ERROR, "pthread_atfork failed"); + rc = ERROR_NOMEM; + goto out; + }tools/libxl/libxl_fork.c line 84
Quoted 10 lines
tools/libxl/libxl_fork.c new file +10
-0+ + atfork_registered = 1; + rc = 0; + out: + atfork_unlock(); + return rc; +} + +void libxl__carefd_begin (void ) {atfork_lock (); } +void libxl__carefd_unlock (void ) {atfork_unlock (); }This naming seems asymmetric.
Quoted 7 lines
Ian.
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xensource.com/xen-devel
Re: [PATCH 4/6] libxl: Protect fds with CLOEXEC even with forking threads
In reply to Ian CampbellIan Campbell writes ("Re: [Xen-devel] [PATCH 4/6] libxl: Protect fds with CLOEXEC even with forking threads"):
Quoted 4 lines · Ian Jackson
Quoted 1 line
As yet we do not use this anywherein libxl. Until all locations in
"anywherein"
Fixed.
Quoted 9 lines
Quoted 9 lines
libxl which make such fds are converted,
libxl__postforkmay not work entirely properly. If these locations do not useO_CLOEXEC(or use calls for which there is noO_CLOEXEC) then multithreaded programs may not work properly.This introduces a new API call
libxl_postfork_child_noexecwhich must be called by applications which make long-running non-execing children. Add the appropriate call to xl's postfork function.
Quoted 7 lines
Quoted 3 lines
On Linux
pthread_atforkdemands compilation with -pthread, so addPTHREAD_CFLAGS,_LDFLAGS,_LIBSto the corresponding variables in the libxlMakefile. It is not clear why we got away without these before.Since I assume the
Makefilebit could safely be split out and applied, that bit is:Acked-by Ian Campbell <[email protected]>
I'll split it out into a separate patch.
Quoted 10 lines
tools/libxl/libxl_event.h void libxl_osevent_occurred_fd(libxl_ctx *ctx, void *for_libxl, · line 379
Quoted 7 lines
tools/libxl/libxl_event.h +7
-0+/* +* An application which initialises a libxl_ctx in a parent process +* and then forks a child which does not quickly exec, must +* instead libxl__postfork_child_noexec in the child. One call +* for all libxl contexts is sufficient. +*/ +void libxl_postfork_child_noexec (libxl_ctx *ctx);Is the ctx passed here required to be one of the existing ctx's or a newly allocated one in this context?
I will clarify this comment.
Quoted 7 lines
Quoted 1 line
+static
pthread_mutex_tno_forking=PTHREAD_MUTEX_INITIALIZER;We previously had trouble with
PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NPnot being defined on NetBSD.http://www.daemon-systems.org/man/pthread_mutex_init.3.html suggests
PTHREAD_MUTEX_INITIALIZERis but perhaps we should confirm?
PTHREAD_MUTEX_INITIALIZER is in POSIX and the _NP one isn't. But I see Roger has confirmed it's on BSD too, which is good.
Quoted 8 lines
Quoted 1 line
+static int
atfork_registered;Does
pthread_atforkpersist into the children?If the parent has set this then it will be set for the child too, which if the answer to my question is "No" is a problem.
I suspect the answer is yes though.
Yes. (Although I can't find any explicit statement, I think it's obvious.)
Quoted 5 lines
tools/libxl/libxl_fork.c line 67
Quoted 3 lines
tools/libxl/libxl_fork.c new file +3
-0+atfork_lock (); +if (atfork_registered) +return 0 ;Missing an unlock?
Silly me, I should be using "goto out" as that's what it's for.
Quoted 4 lines
tools/libxl/libxl_fork.c line 84
Quoted 2 lines
tools/libxl/libxl_fork.c new file +2
-0+void libxl__carefd_begin (void ) {atfork_lock (); } +void libxl__carefd_unlock (void ) {atfork_unlock (); }This naming seems asymmetric.
It's not a symmetric situation. The complete sequence is one of these three:
libxl__carefd_begin
libxl__carefd_record
libxl__carefd_unlock
...
libxl__carefd_close
or
libxl__carefd_begin
libxl__carefd_opened
...
libxl__carefd_close
or
libxl__carefd_begin
libxl__carefd_unlock
I'm open to suggestions for alternative names.
Ian.
PATCH 5/6 libxl: libxl_event.c:beforepoll_internal, REQUIRE_FDS
In reply to Ian JacksonIntroduce definition and use of a new function-local macro REQUIRE_FDS to avoid repeatedly spelling out which fds we are interested in.
We are going to introduce a new fd for the SIGCHLD self-pipe.
--- tools/libxl/libxl_event.c | 82 ++++++++++++++++++++++++++++++-------------- 1 files changed, 56 insertions(+), 26 deletions(-)
tools/libxl/libxl_event.c +56 -26
@@ -593,6 +593,45 @@ static int beforepoll_internal(libxl__gc *gc, libxl__poller *poller,int rc;/* +* We need to look at the fds we want twice: firstly, to count +* them so we can make the rindex array big enough, and secondly +* to actually fill the arrays in. +* +* To ensure correctness and avoid repeating the logic for +* deciding which fds are relevant, we define a macro +* REQUIRE_FDS( BODY ) +* which calls +* do{ +* int req_fd; +* int req_events; +* BODY; +* }while(0) +* for each fd with a nonzero events. This is invoked twice. +* +* The definition of REQUIRE_FDS is simplified with the helper +* macro +* void REQUIRE_FD(int req_fd, int req_events, BODY); +*/ + +#define REQUIRE_FDS (BODY ) do{ \ + \ + LIBXL_LIST_FOREACH(efd, &CTX->efds, entry) \ + REQUIRE_FD(efd->fd, efd->events, BODY); \ + \ + REQUIRE_FD(poller->wakeup_pipe[0], POLLIN, BODY); \ + \ + }while(0) + +#define REQUIRE_FD (req_fd_, req_events_,BODY ) do{ \ + int req_events = (req_events_); \ + int req_fd = (req_fd_); \ + if (req_events) { \ + BODY; \ + } \ + }while(0) + + +/* * In order to be able to efficiently find the libxl__ev_fd * for a struct poll during _afterpoll, we maintain a shadow * data structure in CTX->fd_beforepolled: each slot in
@@ -609,13 +648,13 @@ static int beforepoll_internal(libxl__gc *gc, libxl__poller *poller,* not to mess with fd_rindex. */ -int maxfd = poller->wakeup_pipe[0 ] +1 ; - LIBXL_LIST_FOREACH(efd, &CTX->efds, entry) { -if (!efd->events) -continue ; -if (efd->fd >= maxfd) - maxfd = efd->fd +1 ; - } +int maxfd =0 ; + +REQUIRE_FDS ({ +if (req_fd >= maxfd) + maxfd = req_fd +1 ; + }); +/* make sure our array is as big as *nfds_io */ if (poller->fd_rindex_allocd < maxfd) {assert (maxfd <INT_MAX /sizeof (int ) /2 );
@@ -630,25 +669,16 @@ static int beforepoll_internal(libxl__gc *gc, libxl__poller *poller,int used =0 ; -#define REQUIRE_FD (req_fd, req_events, efd) do{ \ - if ((req_events)) { \ - if (used < *nfds_io) { \ - fds[used].fd = (req_fd); \ - fds[used].events = (req_events); \ - fds[used].revents = 0; \ - assert((req_fd) < poller->fd_rindex_allocd); \ - poller->fd_rindex[(req_fd)] = used; \ - } \ - used++; \ - } \ - }while(0) - - LIBXL_LIST_FOREACH(efd, &CTX->efds, entry) -REQUIRE_FD (efd->fd, efd->events, efd); - -REQUIRE_FD (poller->wakeup_pipe[0 ],POLLIN ,0 ); - -#undef REQUIRE_FD +REQUIRE_FDS ({ +if (used < *nfds_io) { + fds[used].fd = req_fd; + fds[used].events = req_events; + fds[used].revents =0 ; +assert (req_fd < poller->fd_rindex_allocd); + poller->fd_rindex[req_fd] = used; + } + used++; + }); rc = used <= *nfds_io ?0 :ERROR_BUFFERFULL ;
Signature · 1 line
1.7.2.5
PATCH 6/6 libxl: event API: new facilities for waiting for subprocesses
In reply to Ian JacksonThe current arrangements in libxl for spawning subprocesses have two key problems: (i) they make unwarranted (and largely undocumented) assumptions about the caller's use of subprocesses, (ii) they aren't integrated into the event system and can't be made asynchronous etc.
So replace them with a new set of facilities.
Primarily, from the point of view of code inside libxl, this is libxl__ev_child_fork which is both (a) a version of fork() and (b) an event source which generates a callback when the child dies.
Quoted 1 line
From the point of view of the application, we fully document our use
of SIGCHLD. The application can tell us whether it wants to own SIGCHLD or not; if it does, it has to tell us about deaths of our children.
Currently there are no callers in libxl which use these facilities. All code in libxl which forks needs to be converted and libxl_fork needse to be be abolished.
--- tools/libxl/libxl.c | 17 +++- tools/libxl/libxl_event.c | 52 +++++++-- tools/libxl/libxl_event.h | 142 ++++++++++++++++++++++++- tools/libxl/libxl_fork.c | 242 ++++++++++++++++++++++++++++++++++++++++++ tools/libxl/libxl_internal.h | 61 ++++++++++- 5 files changed, 494 insertions(+), 20 deletions(-)
tools/libxl/libxl.c +16 -1
@@ -39,7 +39,7 @@ int libxl_ctx_alloc(libxl_ctx **pctx, int version,memset (ctx,0 ,sizeof (libxl_ctx)); ctx->lg = lg; -/* First initialise pointers (cannot fail) */ +/* First initialise pointers etc. (cannot fail) */ LIBXL_TAILQ_INIT (&ctx->occurred);
@@ -58,6 +58,11 @@ int libxl_ctx_alloc(libxl_ctx **pctx, int version,LIBXL_TAILQ_INIT (&ctx->death_list);libxl__ev_xswatch_init (&ctx->death_watch); + ctx->childproc_hooks = &libxl__childproc_default_hooks; + ctx->childproc_user =0 ; + + ctx->sigchld_selfpipe[0 ] =-1 ; +/* The mutex is special because we can't idempotently destroy it */ if (libxl__init_recursive_mutex (ctx, &ctx->lock) <0 ) {
@@ -160,6 +165,16 @@ int libxl_ctx_free(libxl_ctx *ctx)discard_events (&ctx->occurred); +/* If we have outstanding children, then the application inherits +* them; we wish the application good luck with understanding +* this if and when it reaps them. */ +libxl__sigchld_removehandler (ctx); + +if (ctx->sigchld_selfpipe[0 ] >=0 ) { +close (ctx->sigchld_selfpipe[0 ]); +close (ctx->sigchld_selfpipe[1 ]); + } +pthread_mutex_destroy (&ctx->lock);GC_FREE ;
tools/libxl/libxl_event.c +41 -11
84 lines not on this page · raw message
tools/libxl/libxl_event.h +137 -5
160 lines not on this page · raw message
tools/libxl/libxl_fork.c +242 -0
266 lines not on this page · raw message
tools/libxl/libxl_internal.h +58 -3
92 lines not on this page · raw message
Signature · 1 line
1.7.2.5