1 /*
2  * Copyright (C) 2012      Citrix Ltd.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Lesser General Public License as published
6  * by the Free Software Foundation; version 2.1 only. with the special
7  * exception on linking described in file LICENSE.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU Lesser General Public License for more details.
13  */
14 /*
15  * Internal child process machinery for use by other parts of libxl
16  */
17 
18 #include "libxl_osdeps.h" /* must come before any other headers */
19 
20 #include "libxl_internal.h"
21 
22 /*
23  * carefd arrangements
24  *
25  * carefd_begin and _unlock take out the no_forking lock, which we
26  * also take and release in our pthread_atfork handlers.  So when this
27  * lock is held the whole process cannot fork.  We therefore protect
28  * our fds from leaking into children made by other threads.
29  *
30  * We maintain a list of all the carefds, so that if the application
31  * wants to fork a long-running but non-execing child, we can close
32  * them all.
33  *
34  * So the record function sets CLOEXEC for the benefit of execing
35  * children, and makes a note of the fd for the benefit of non-execing
36  * ones.
37  */
38 
39 struct libxl__carefd {
40     LIBXL_LIST_ENTRY(libxl__carefd) entry;
41     int fd;
42 };
43 
44 static pthread_mutex_t no_forking = PTHREAD_MUTEX_INITIALIZER;
45 static int atfork_registered;
46 static LIBXL_LIST_HEAD(, libxl__carefd) carefds =
47     LIBXL_LIST_HEAD_INITIALIZER(carefds);
48 
49 /* Protected against concurrency by no_forking.  sigchld_users is
50  * protected against being interrupted by SIGCHLD (and thus read
51  * asynchronously by the signal handler) by sigchld_defer (see
52  * below). */
53 static bool sigchld_installed; /* 0 means not */
54 static pthread_mutex_t sigchld_defer_mutex = PTHREAD_MUTEX_INITIALIZER;
55 static LIBXL_LIST_HEAD(, libxl_ctx) sigchld_users =
56     LIBXL_LIST_HEAD_INITIALIZER(sigchld_users);
57 static struct sigaction sigchld_saved_action;
58 
59 static void sigchld_removehandler_core(void); /* idempotent */
60 static void sigchld_user_remove(libxl_ctx *ctx); /* idempotent */
61 static void sigchld_sethandler_raw(void (*handler)(int), struct sigaction *old);
62 
63 static void defer_sigchld(void);
64 static void release_sigchld(void);
65 
atfork_lock(void)66 static void atfork_lock(void)
67 {
68     int r = pthread_mutex_lock(&no_forking);
69     assert(!r);
70 }
71 
atfork_unlock(void)72 static void atfork_unlock(void)
73 {
74     int r = pthread_mutex_unlock(&no_forking);
75     assert(!r);
76 }
77 
libxl__atfork_init(libxl_ctx * ctx)78 int libxl__atfork_init(libxl_ctx *ctx)
79 {
80     int r, rc;
81 
82     atfork_lock();
83     if (atfork_registered) { rc = 0; goto out; }
84 
85     r = pthread_atfork(atfork_lock, atfork_unlock, atfork_unlock);
86     if (r) {
87         assert(r == ENOMEM);
88         libxl__alloc_failed(ctx, __func__, 0,0);
89     }
90 
91     atfork_registered = 1;
92     rc = 0;
93  out:
94     atfork_unlock();
95     return rc;
96 }
97 
libxl__carefd_begin(void)98 void libxl__carefd_begin(void) { atfork_lock(); }
libxl__carefd_unlock(void)99 void libxl__carefd_unlock(void) { atfork_unlock(); }
100 
libxl__carefd_record(libxl_ctx * ctx,int fd)101 libxl__carefd *libxl__carefd_record(libxl_ctx *ctx, int fd)
102 {
103     libxl__carefd *cf = 0;
104 
105     libxl_fd_set_cloexec(ctx, fd, 1);
106     cf = libxl__zalloc(&ctx->nogc_gc, sizeof(*cf));
107     cf->fd = fd;
108     LIBXL_LIST_INSERT_HEAD(&carefds, cf, entry);
109     return cf;
110 }
111 
libxl__carefd_opened(libxl_ctx * ctx,int fd)112 libxl__carefd *libxl__carefd_opened(libxl_ctx *ctx, int fd)
113 {
114     libxl__carefd *cf = 0;
115     int saved_errno = errno;
116 
117     if (fd >= 0)
118         cf = libxl__carefd_record(ctx, fd);
119     libxl__carefd_unlock();
120     errno = saved_errno;
121     return cf;
122 }
123 
libxl_postfork_child_noexec(libxl_ctx * ctx)124 void libxl_postfork_child_noexec(libxl_ctx *ctx)
125 {
126     /*
127      * Anything running without the no_forking lock (atfork_lock)
128      * might be interrupted by fork.  But libxl functions other than
129      * this one are then forbidden to the child.
130      *
131      * Conversely, this function might interrupt any other libxl
132      * operation (even though that other operation has the libxl ctx
133      * lock).  We don't take the lock ourselves, since we are running
134      * in the child and if the lock is held the thread that took it no
135      * longer exists.  To prevent us being interrupted by another call
136      * to ourselves (whether in another thread or by virtue of another
137      * fork) we take the atfork lock ourselves.
138      */
139     libxl__carefd *cf, *cf_tmp;
140     int r;
141 
142     atfork_lock();
143 
144     LIBXL_LIST_FOREACH_SAFE(cf, &carefds, entry, cf_tmp) {
145         if (cf->fd >= 0) {
146             r = close(cf->fd);
147             if (r)
148                 LIBXL__LOG_ERRNO(ctx, LIBXL__LOG_WARNING,
149                                  "failed to close fd=%d"
150                                  " (perhaps of another libxl ctx)", cf->fd);
151         }
152         free(cf);
153     }
154     LIBXL_LIST_INIT(&carefds);
155 
156     if (sigchld_installed) {
157         /* We are in theory not at risk of concurrent execution of the
158          * SIGCHLD handler, because the application should call
159          * libxl_postfork_child_noexec before the child forks again.
160          * (If the SIGCHLD was in flight in the parent at the time of
161          * the fork, the thread it was delivered on exists only in the
162          * parent so is not our concern.)
163          *
164          * But in case the application violated this rule (and did so
165          * while multithreaded in the child), we use our deferral
166          * machinery.  The result is that the SIGCHLD may then be lost
167          * (i.e. signaled to the now-defunct libxl ctx(s)).  But at
168          * least we won't execute undefined behaviour (by examining
169          * the list in the signal handler concurrently with clearing
170          * it here), and since we won't actually reap the new children
171          * things will in fact go OK if the application doesn't try to
172          * use SIGCHLD, but instead just waits for the child(ren). */
173         defer_sigchld();
174 
175         LIBXL_LIST_INIT(&sigchld_users);
176         /* After this the ->sigchld_user_registered entries in the
177          * now-obsolete contexts may be lies.  But that's OK because
178          * no-one will look at them. */
179 
180         release_sigchld();
181         sigchld_removehandler_core();
182     }
183 
184     atfork_unlock();
185 }
186 
libxl__carefd_close(libxl__carefd * cf)187 int libxl__carefd_close(libxl__carefd *cf)
188 {
189     if (!cf) return 0;
190     atfork_lock();
191     int r = cf->fd < 0 ? 0 : close(cf->fd);
192     int esave = errno;
193     LIBXL_LIST_REMOVE(cf, entry);
194     atfork_unlock();
195     free(cf);
196     errno = esave;
197     return r;
198 }
199 
libxl__carefd_fd(const libxl__carefd * cf)200 int libxl__carefd_fd(const libxl__carefd *cf)
201 {
202     if (!cf) return -1;
203     return cf->fd;
204 }
205 
206 /*
207  * Low-level functions for child process handling, including
208  * the main SIGCHLD handler.
209  */
210 
211 /* Like waitpid(,,WNOHANG) but handles all errors except ECHILD. */
checked_waitpid(libxl__egc * egc,pid_t want,int * status)212 static pid_t checked_waitpid(libxl__egc *egc, pid_t want, int *status)
213 {
214     for (;;) {
215         pid_t got = waitpid(want, status, WNOHANG);
216         if (got != -1)
217             return got;
218         if (errno == ECHILD)
219             return got;
220         if (errno == EINTR)
221             continue;
222         LIBXL__EVENT_DISASTER(egc, "waitpid() failed", errno, 0);
223         return 0;
224     }
225 }
226 
227 static void sigchld_selfpipe_handler(libxl__egc *egc, libxl__ev_fd *ev,
228                                      int fd, short events, short revents);
229 
sigchld_handler(int signo)230 static void sigchld_handler(int signo)
231 {
232     /* This function has to be reentrant!  Luckily it is. */
233 
234     libxl_ctx *notify;
235     int esave = errno;
236 
237     int r = pthread_mutex_lock(&sigchld_defer_mutex);
238     assert(!r);
239 
240     LIBXL_LIST_FOREACH(notify, &sigchld_users, sigchld_users_entry) {
241         int e = libxl__self_pipe_wakeup(notify->sigchld_selfpipe[1]);
242         if (e) abort(); /* errors are probably EBADF, very bad */
243     }
244 
245     r = pthread_mutex_unlock(&sigchld_defer_mutex);
246     assert(!r);
247 
248     errno = esave;
249 }
250 
sigchld_sethandler_raw(void (* handler)(int),struct sigaction * old)251 static void sigchld_sethandler_raw(void (*handler)(int), struct sigaction *old)
252 {
253     struct sigaction ours;
254     int r;
255 
256     memset(&ours,0,sizeof(ours));
257     ours.sa_handler = handler;
258     sigemptyset(&ours.sa_mask);
259     ours.sa_flags = SA_NOCLDSTOP | SA_RESTART;
260     r = sigaction(SIGCHLD, &ours, old);
261     assert(!r);
262 }
263 
264 /*
265  * SIGCHLD deferral
266  *
267  * sigchld_defer and sigchld_release are a bit like using sigprocmask
268  * to block the signal only they work for the whole process.  Sadly
269  * this has to be done by setting a special handler that records the
270  * "pendingness" of the signal here in the program.  How tedious.
271  *
272  * A property of this approach is that the signal handler itself
273  * must be reentrant (see the comment in release_sigchld).
274  *
275  * Callers have the atfork_lock so there is no risk of concurrency
276  * within these functions, aside from the risk of being interrupted by
277  * the signal.  We use sigchld_defer_mutex to guard against the
278  * possibility of the real signal handler being still running on
279  * another thread.
280  */
281 
282 static volatile sig_atomic_t sigchld_occurred_while_deferred;
283 
sigchld_handler_when_deferred(int signo)284 static void sigchld_handler_when_deferred(int signo)
285 {
286     sigchld_occurred_while_deferred = 1;
287 }
288 
defer_sigchld(void)289 static void defer_sigchld(void)
290 {
291     assert(sigchld_installed);
292 
293     sigchld_sethandler_raw(sigchld_handler_when_deferred, 0);
294 
295     /* Now _this thread_ cannot any longer be interrupted by the
296      * signal, so we can take the mutex without risk of deadlock.  If
297      * another thread is in the signal handler, either it or we will
298      * block and wait for the other. */
299 
300     int r = pthread_mutex_lock(&sigchld_defer_mutex);
301     assert(!r);
302 }
303 
release_sigchld(void)304 static void release_sigchld(void)
305 {
306     assert(sigchld_installed);
307 
308     int r = pthread_mutex_unlock(&sigchld_defer_mutex);
309     assert(!r);
310 
311     sigchld_sethandler_raw(sigchld_handler, 0);
312     if (sigchld_occurred_while_deferred) {
313         sigchld_occurred_while_deferred = 0;
314         /* We might get another SIGCHLD here, in which case
315          * sigchld_handler will be interrupted and re-entered.
316          * This is OK. */
317         sigchld_handler(SIGCHLD);
318     }
319 }
320 
321 /*
322  * Meat of the child process handling.
323  */
324 
sigchld_removehandler_core(void)325 static void sigchld_removehandler_core(void) /* idempotent */
326 {
327     struct sigaction was;
328     int r;
329 
330     if (!sigchld_installed)
331         return;
332 
333     r = sigaction(SIGCHLD, &sigchld_saved_action, &was);
334     assert(!r);
335     assert(!(was.sa_flags & SA_SIGINFO));
336     assert(was.sa_handler == sigchld_handler);
337 
338     sigchld_installed = 0;
339 }
340 
sigchld_installhandler_core(void)341 static void sigchld_installhandler_core(void) /* idempotent */
342 {
343     if (sigchld_installed)
344         return;
345 
346     sigchld_installed = 1;
347 
348     sigchld_sethandler_raw(sigchld_handler, &sigchld_saved_action);
349 
350     assert(((void)"application must negotiate with libxl about SIGCHLD",
351             !(sigchld_saved_action.sa_flags & SA_SIGINFO) &&
352             (sigchld_saved_action.sa_handler == SIG_DFL ||
353              sigchld_saved_action.sa_handler == SIG_IGN)));
354 }
355 
sigchld_user_remove(libxl_ctx * ctx)356 static void sigchld_user_remove(libxl_ctx *ctx) /* idempotent */
357 {
358     if (!ctx->sigchld_user_registered)
359         return;
360 
361     atfork_lock();
362     defer_sigchld();
363 
364     LIBXL_LIST_REMOVE(ctx, sigchld_users_entry);
365 
366     release_sigchld();
367 
368     if (LIBXL_LIST_EMPTY(&sigchld_users))
369         sigchld_removehandler_core();
370 
371     atfork_unlock();
372 
373     ctx->sigchld_user_registered = 0;
374 }
375 
libxl__sigchld_notneeded(libxl__gc * gc)376 void libxl__sigchld_notneeded(libxl__gc *gc) /* non-reentrant, idempotent */
377 {
378     sigchld_user_remove(CTX);
379     libxl__ev_fd_deregister(gc, &CTX->sigchld_selfpipe_efd);
380 }
381 
libxl__sigchld_needed(libxl__gc * gc)382 int libxl__sigchld_needed(libxl__gc *gc) /* non-reentrant, idempotent */
383 {
384     int rc;
385 
386     if (CTX->sigchld_selfpipe[0] < 0) {
387         rc = libxl__pipe_nonblock(CTX, CTX->sigchld_selfpipe);
388         if (rc) goto out;
389     }
390     if (!libxl__ev_fd_isregistered(&CTX->sigchld_selfpipe_efd)) {
391         rc = libxl__ev_fd_register(gc, &CTX->sigchld_selfpipe_efd,
392                                    sigchld_selfpipe_handler,
393                                    CTX->sigchld_selfpipe[0], POLLIN);
394         if (rc) goto out;
395     } else {
396         rc = libxl__ev_fd_modify(gc, &CTX->sigchld_selfpipe_efd, POLLIN);
397         if (rc) goto out;
398     }
399     if (!CTX->sigchld_user_registered) {
400         atfork_lock();
401 
402         sigchld_installhandler_core();
403 
404         defer_sigchld();
405 
406         LIBXL_LIST_INSERT_HEAD(&sigchld_users, CTX, sigchld_users_entry);
407 
408         release_sigchld();
409         atfork_unlock();
410 
411         CTX->sigchld_user_registered = 1;
412     }
413 
414     rc = 0;
415  out:
416     return rc;
417 }
418 
chldmode_ours(libxl_ctx * ctx,bool creating)419 static bool chldmode_ours(libxl_ctx *ctx, bool creating)
420 {
421     switch (ctx->childproc_hooks->chldowner) {
422     case libxl_sigchld_owner_libxl:
423         return creating || !LIBXL_LIST_EMPTY(&ctx->children);
424     case libxl_sigchld_owner_mainloop:
425         return 0;
426     case libxl_sigchld_owner_libxl_always:
427     case libxl_sigchld_owner_libxl_always_selective_reap:
428         return 1;
429     }
430     abort();
431 }
432 
perhaps_sigchld_notneeded(libxl__gc * gc)433 static void perhaps_sigchld_notneeded(libxl__gc *gc)
434 {
435     if (!chldmode_ours(CTX, 0))
436         libxl__sigchld_notneeded(gc);
437 }
438 
perhaps_sigchld_needed(libxl__gc * gc,bool creating)439 static int perhaps_sigchld_needed(libxl__gc *gc, bool creating)
440 {
441     int rc;
442 
443     if (chldmode_ours(CTX, creating)) {
444         rc = libxl__sigchld_needed(gc);
445         if (rc) return rc;
446     }
447     return 0;
448 }
449 
childproc_reaped_ours(libxl__egc * egc,libxl__ev_child * ch,int status)450 static void childproc_reaped_ours(libxl__egc *egc, libxl__ev_child *ch,
451                                  int status)
452 {
453     pid_t pid = ch->pid;
454     LIBXL_LIST_REMOVE(ch, entry);
455     ch->pid = -1;
456     ch->callback(egc, ch, pid, status);
457 }
458 
childproc_reaped(libxl__egc * egc,pid_t pid,int status)459 static int childproc_reaped(libxl__egc *egc, pid_t pid, int status)
460 {
461     EGC_GC;
462     libxl__ev_child *ch;
463 
464     LIBXL_LIST_FOREACH(ch, &CTX->children, entry)
465         if (ch->pid == pid)
466             goto found;
467 
468     /* not found */
469     return ERROR_UNKNOWN_CHILD;
470 
471  found:
472     childproc_reaped_ours(egc, ch, status);
473 
474     perhaps_sigchld_notneeded(gc);
475 
476     return 0;
477 }
478 
libxl_childproc_reaped(libxl_ctx * ctx,pid_t pid,int status)479 int libxl_childproc_reaped(libxl_ctx *ctx, pid_t pid, int status)
480 {
481     EGC_INIT(ctx);
482     CTX_LOCK;
483     assert(CTX->childproc_hooks->chldowner
484            == libxl_sigchld_owner_mainloop);
485     int rc = childproc_reaped(egc, pid, status);
486     CTX_UNLOCK;
487     EGC_FREE;
488     return rc;
489 }
490 
childproc_checkall(libxl__egc * egc)491 static void childproc_checkall(libxl__egc *egc)
492 {
493     EGC_GC;
494     libxl__ev_child *ch;
495 
496     for (;;) {
497         int status;
498         pid_t got;
499 
500         LIBXL_LIST_FOREACH(ch, &CTX->children, entry) {
501             got = checked_waitpid(egc, ch->pid, &status);
502             if (got)
503                 goto found;
504         }
505         /* not found */
506         return;
507 
508     found:
509         if (got == -1) {
510             LIBXL__EVENT_DISASTER
511                 (egc, "waitpid() gave ECHILD but we have a child",
512                  ECHILD, 0);
513             /* it must have finished but we don't know its status */
514             status = 255<<8; /* no wait.h macro for this! */
515             assert(WIFEXITED(status));
516             assert(WEXITSTATUS(status)==255);
517             assert(!WIFSIGNALED(status));
518             assert(!WIFSTOPPED(status));
519         }
520         childproc_reaped_ours(egc, ch, status);
521         /* we need to restart the loop, as children may have been edited */
522     }
523 }
524 
libxl_childproc_sigchld_occurred(libxl_ctx * ctx)525 void libxl_childproc_sigchld_occurred(libxl_ctx *ctx)
526 {
527     EGC_INIT(ctx);
528     CTX_LOCK;
529     assert(CTX->childproc_hooks->chldowner
530            == libxl_sigchld_owner_mainloop);
531     childproc_checkall(egc);
532     CTX_UNLOCK;
533     EGC_FREE;
534 }
535 
sigchld_selfpipe_handler(libxl__egc * egc,libxl__ev_fd * ev,int fd,short events,short revents)536 static void sigchld_selfpipe_handler(libxl__egc *egc, libxl__ev_fd *ev,
537                                      int fd, short events, short revents)
538 {
539     /* May make callbacks into the application for child processes.
540      * So, this function may unlock and relock the CTX.  This is OK
541      * because event callback functions are always called with the CTX
542      * locked exactly once, and from code which copes with reentrancy.
543      * (See also the comment in afterpoll_internal.) */
544     EGC_GC;
545 
546     int selfpipe = CTX->sigchld_selfpipe[0];
547 
548     if (revents & ~POLLIN) {
549         LOG(ERROR, "unexpected poll event 0x%x on SIGCHLD self pipe", revents);
550         LIBXL__EVENT_DISASTER(egc,
551                               "unexpected poll event on SIGCHLD self pipe",
552                               0, 0);
553     }
554     assert(revents & POLLIN);
555 
556     int e = libxl__self_pipe_eatall(selfpipe);
557     if (e) LIBXL__EVENT_DISASTER(egc, "read sigchld pipe", e, 0);
558 
559     if (CTX->childproc_hooks->chldowner
560         == libxl_sigchld_owner_libxl_always_selective_reap) {
561         childproc_checkall(egc);
562         return;
563     }
564 
565     while (chldmode_ours(CTX, 0) /* in case the app changes the mode */) {
566         int status;
567         pid_t pid = checked_waitpid(egc, -1, &status);
568 
569         if (pid == 0 || pid == -1 /* ECHILD */)
570             return;
571 
572         int rc = childproc_reaped(egc, pid, status);
573 
574         if (rc) {
575             if (CTX->childproc_hooks->reaped_callback) {
576                 CTX_UNLOCK;
577                 rc = CTX->childproc_hooks->reaped_callback
578                         (pid, status, CTX->childproc_user);
579                 CTX_LOCK;
580                 if (rc != 0 && rc != ERROR_UNKNOWN_CHILD) {
581                     char disasterbuf[200];
582                     snprintf(disasterbuf, sizeof(disasterbuf), " reported by"
583                              " libxl_childproc_hooks->reaped_callback"
584                              " (for pid=%lu, status=%d; error code %d)",
585                              (unsigned long)pid, status, rc);
586                     LIBXL__EVENT_DISASTER(egc, disasterbuf, 0, 0);
587                     return;
588                 }
589             } else {
590                 rc = ERROR_UNKNOWN_CHILD;
591             }
592             if (rc)
593                 libxl_report_child_exitstatus(CTX, XTL_WARN,
594                                 "unknown child", (long)pid, status);
595         }
596     }
597 }
598 
libxl__ev_child_fork(libxl__gc * gc,libxl__ev_child * ch,libxl__ev_child_callback * death)599 pid_t libxl__ev_child_fork(libxl__gc *gc, libxl__ev_child *ch,
600                            libxl__ev_child_callback *death)
601 {
602     CTX_LOCK;
603     int rc;
604 
605     perhaps_sigchld_needed(gc, 1);
606 
607     pid_t pid =
608         CTX->childproc_hooks->fork_replacement
609         ? CTX->childproc_hooks->fork_replacement(CTX->childproc_user)
610         : fork();
611     if (pid == -1) {
612         LOGE(ERROR, "fork failed");
613         rc = ERROR_FAIL;
614         goto out;
615     }
616 
617     if (!pid) {
618         /* woohoo! */
619         if (CTX->xsh) {
620             xs_daemon_destroy_postfork(CTX->xsh);
621             CTX->xsh = NULL; /* turns mistakes into crashes */
622         }
623         /* Yes, CTX is left locked in the child. */
624         return 0;
625     }
626 
627     ch->pid = pid;
628     ch->callback = death;
629     LIBXL_LIST_INSERT_HEAD(&CTX->children, ch, entry);
630     rc = pid;
631 
632  out:
633     perhaps_sigchld_notneeded(gc);
634     CTX_UNLOCK;
635     return rc;
636 }
637 
libxl_childproc_setmode(libxl_ctx * ctx,const libxl_childproc_hooks * hooks,void * user)638 void libxl_childproc_setmode(libxl_ctx *ctx, const libxl_childproc_hooks *hooks,
639                              void *user)
640 {
641     GC_INIT(ctx);
642     CTX_LOCK;
643 
644     assert(LIBXL_LIST_EMPTY(&CTX->children));
645 
646     if (!hooks)
647         hooks = &libxl__childproc_default_hooks;
648 
649     ctx->childproc_hooks = hooks;
650     ctx->childproc_user = user;
651 
652     perhaps_sigchld_notneeded(gc);
653     perhaps_sigchld_needed(gc, 0); /* idempotent, ok to ignore errors for now */
654 
655     CTX_UNLOCK;
656     GC_FREE;
657 }
658 
659 const libxl_childproc_hooks libxl__childproc_default_hooks = {
660     libxl_sigchld_owner_libxl, 0, 0
661 };
662 
libxl__ev_child_xenstore_reopen(libxl__gc * gc,const char * what)663 int libxl__ev_child_xenstore_reopen(libxl__gc *gc, const char *what) {
664     int rc;
665 
666     assert(!CTX->xsh);
667     CTX->xsh = xs_daemon_open();
668     if (!CTX->xsh) {
669         LOGE(ERROR, "%s: xenstore reopen failed", what);
670         rc = ERROR_FAIL;  goto out;
671     }
672 
673     libxl_fd_set_cloexec(CTX, xs_fileno(CTX->xsh), 1);
674 
675     return 0;
676 
677  out:
678     return rc;
679 }
680 
681 /*
682  * Local variables:
683  * mode: C
684  * c-basic-offset: 4
685  * indent-tabs-mode: nil
686  * End:
687  */
688