tools: xentoolcore_restrict_all: Do deregistration before close
authorIan Jackson <ian.jackson@eu.citrix.com>
Tue, 14 Nov 2017 12:15:42 +0000 (12:15 +0000)
committerIan Jackson <Ian.Jackson@eu.citrix.com>
Tue, 28 Nov 2017 18:10:16 +0000 (18:10 +0000)
commit9976f3874d4cca829f2d2916feab18615337bb5c
treedf0b8750044174d317b2fd41917f5480690421e6
parent7f080956e9eed821fd42013bef11c1a2873fbeba
tools: xentoolcore_restrict_all: Do deregistration before close

Closing the fd before unhooking it from the list runs the risk that a
concurrent thread calls xentoolcore_restrict_all will operate on the
old fd value, which might refer to a new fd by then.  So we need to do
it in the other order.

Sadly this weakens the guarantee provided by xentoolcore_restrict_all
slightly, but not (I think) in a problematic way.  It would be
possible to implement the previous guarantee, but it would involve
replacing all of the close() calls in all of the individual osdep
parts of all of the individual libraries with calls to a new function
which does
   dup2("/dev/null", thing->fd);
   pthread_mutex_lock(&handles_lock);
   thing->fd = -1;
   pthread_mutex_unlock(&handles_lock);
   close(fd);
which would be terribly tedious.

Signed-off-by: Ian Jackson <Ian.Jackson@eu.citrix.com>
Acked-by: Wei Liu <wei.liu2@citrix.com>
Reviewed-by: Ross Lagerwall <ross.lagerwall@citrix.com>
Release-acked-by: Julien Grall <julien.grall@linaro.org>
tools/libs/call/core.c
tools/libs/devicemodel/core.c
tools/libs/evtchn/core.c
tools/libs/foreignmemory/core.c
tools/libs/gnttab/gnttab_core.c
tools/libs/toolcore/include/xentoolcore.h
tools/libs/toolcore/include/xentoolcore_internal.h
tools/xenstore/xs.c