[lxc-devel] [PATCH v2 5/7] pass namespace handles to the stop hook

Serge Hallyn serge.hallyn at ubuntu.com
Fri Oct 2 13:56:38 UTC 2015


Quoting Wolfgang Bumiller (w.bumiller at proxmox.com):
> 
> 
> > On September 30, 2015 at 6:07 PM Serge Hallyn <serge.hallyn at ubuntu.com> wrote:
> > 
> > 
> > Quoting Wolfgang Bumiller (w.bumiller at proxmox.com):
> > > Signed-off-by: Wolfgang Bumiller <w.bumiller at proxmox.com>
> > > ---
> > >  src/lxc/start.c | 21 +++++++++++++++++++--
> > >  1 file changed, 19 insertions(+), 2 deletions(-)
> > > 
> > > diff --git a/src/lxc/start.c b/src/lxc/start.c
> > > index a1eb961..38e7e97 100644
> > > --- a/src/lxc/start.c
> > > +++ b/src/lxc/start.c
> > > @@ -486,15 +486,32 @@ out_free:
> > >  
> > >  void lxc_fini(const char *name, struct lxc_handler *handler)
> > >  {
> > > -	int i;
> > > +	int i, rc;
> > > +	pid_t self = getpid();
> > > +	char **namespaces = (char**)malloc((LXC_NS_MAX+1) * sizeof(char*));
> > > +	size_t namespace_count = 0;
> > >  
> > >  	/* The STOPPING state is there for future cleanup code
> > >  	 * which can take awhile
> > >  	 */
> > >  	lxc_set_state(name, handler, STOPPING);
> > >  
> > > -	if (run_lxc_hooks(name, "stop", handler->conf, handler->lxcpath, NULL))
> > > +	for (i = 0; i < LXC_NS_MAX; i++) {
> > > +		if (handler->nsfd[i] != -1) {
> > 
> > Hm, wait - what happens if handler->nsfd[i] == -1?  I.e. if there is no
> > network
> > namespace.  namespaces[i] will be undefined, right?
> 
> Yes, which is why I index by `namespace_count` and not `i` both when print()ing

Yup,  I missed that you were prefixing with 'mnt:' etc and so thought you
were doing it differntely.  looks good, thx

> as well as free()ing, and set the last [namespace_count] index to NULL as that's
> how run_script_argv() finds the end.
> 
> But, I don't free(namespaces) which is rather embarrassing... (actually
> the embarrassing part is allocating it with malloc() in the first place)
> I should just:
> -+	char **namespaces = (char**)malloc((LXC_NS_MAX+1) * sizeof(char*));
> ++	char *namespaces[LXC_NS_MAX+1];
> 
> > > +			rc = asprintf(&namespaces[namespace_count], "%s:/proc/%d/fd/%d",
> > > +			              ns_info[i].proc_name, self, handler->nsfd[i]);
> > > +			if (rc == -1) {
> > > +				SYSERROR("failed to allocate memory");
> > > +				break;
> > > +			}
> > > +			++namespace_count;
> > > +		}
> > > +	}
> > > +	namespaces[namespace_count] = NULL;
> > > +	if (run_lxc_hooks(name, "stop", handler->conf, handler->lxcpath,
> > > namespaces))
> > >  		ERROR("failed to run stop hooks for container '%s'.", name);
> > > +	while (namespace_count--)
> > > +		free(namespaces[namespace_count]);
> > >  	for (i = 0; i < LXC_NS_MAX; i++) {
> > >  		if (handler->nsfd[i] != -1) {
> > >  			close(handler->nsfd[i]);
> > > -- 
> > > 2.1.4
> > > 
> > > 
> > > _______________________________________________
> > > lxc-devel mailing list
> > > lxc-devel at lists.linuxcontainers.org
> > > http://lists.linuxcontainers.org/listinfo/lxc-devel
> > _______________________________________________
> > lxc-devel mailing list
> > lxc-devel at lists.linuxcontainers.org
> > http://lists.linuxcontainers.org/listinfo/lxc-devel
> 


More information about the lxc-devel mailing list