appliance: Touch cached appliance files so they don't get tmp cleaned.
[libguestfs.git] / src / appliance.c
1 /* libguestfs
2  * Copyright (C) 2010 Red Hat Inc.
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library 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 GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18
19 #include <config.h>
20
21 #include <errno.h>
22 #include <dirent.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <stdarg.h>
26 #include <unistd.h>
27 #include <string.h>
28 #include <fcntl.h>
29 #include <time.h>
30 #include <sys/stat.h>
31 #include <sys/select.h>
32 #include <sys/wait.h>
33 #include <utime.h>
34
35 #ifdef HAVE_SYS_TYPES_H
36 #include <sys/types.h>
37 #endif
38
39 #include "guestfs.h"
40 #include "guestfs-internal.h"
41 #include "guestfs-internal-actions.h"
42 #include "guestfs_protocol.h"
43
44 /* Old-style appliance is going to be obsoleted. */
45 static const char *kernel_name = "vmlinuz." host_cpu;
46 static const char *initrd_name = "initramfs." host_cpu ".img";
47
48 static int find_path (guestfs_h *g, int (*pred) (guestfs_h *g, const char *pelem, void *data), void *data, char **pelem);
49 static int dir_contains_file (const char *dir, const char *file);
50 static int dir_contains_files (const char *dir, ...);
51 static int contains_ordinary_appliance (guestfs_h *g, const char *path, void *data);
52 static int contains_supermin_appliance (guestfs_h *g, const char *path, void *data);
53 static char *calculate_supermin_checksum (guestfs_h *g, const char *supermin_path);
54 static int check_for_cached_appliance (guestfs_h *g, const char *supermin_path, const char *checksum, uid_t uid, char **kernel, char **initrd, char **appliance);
55 static int build_supermin_appliance (guestfs_h *g, const char *supermin_path, const char *checksum, uid_t uid, char **kernel, char **initrd, char **appliance);
56 static int hard_link_to_cached_appliance (guestfs_h *g, const char *cachedir, char **kernel, char **initrd, char **appliance);
57 static int run_supermin_helper (guestfs_h *g, const char *supermin_path, const char *cachedir, size_t cdlen);
58
59 /* Locate or build the appliance.
60  *
61  * This function locates or builds the appliance as necessary,
62  * handling the supermin appliance, caching of supermin-built
63  * appliances, or using an ordinary appliance.
64  *
65  * The return value is 0 = good, -1 = error.  Returned in '*kernel'
66  * will be the name of the kernel to use, '*initrd' the name of the
67  * initrd, '*appliance' the name of the ext2 root filesystem.
68  * '*appliance' can be NULL, meaning that we are using an ordinary
69  * (non-ext2) appliance.  All three strings must be freed by the
70  * caller.  However the referenced files themselves must not be
71  * deleted.
72  *
73  * The process is as follows:
74  *
75  * (1) Look for the first element of g->path which contains a
76  * supermin appliance skeleton.  If no element has this, skip
77  * straight to step (5).
78  *
79  * (2) Calculate the checksum of this supermin appliance.
80  *
81  * (3) Check whether a cached appliance with the checksum calculated
82  * in (2) exists and passes basic security checks.  If so, return
83  * this appliance.
84  *
85  * (4) Try to build the supermin appliance.  If this is successful,
86  * return it.
87  *
88  * (5) Check each element of g->path, looking for an ordinary appliance.
89  * If one is found, return it.
90  *
91  * The supermin appliance cache directory lives in
92  * $TMPDIR/.guestfs-$UID/ and consists of four files:
93  *
94  *   $TMPDIR/.guestfs-$UID/checksum       - the checksum
95  *   $TMPDIR/.guestfs-$UID/kernel         - symlink to the kernel
96  *   $TMPDIR/.guestfs-$UID/initrd         - the febootstrap initrd
97  *   $TMPDIR/.guestfs-$UID/root           - the appliance
98  *
99  * Since multiple instances of libguestfs with the same UID may be
100  * racing to create an appliance, we need to be careful when building
101  * and using the appliance.
102  *
103  * If a cached appliance with checksum exists (step (2) above) then we
104  * make a hard link to it with our current PID, so that we have a copy
105  * even if the appliance is replaced by another process building an
106  * appliance afterwards:
107  *
108  *   $TMPDIR/.guestfs-$UID/kernel.$PID
109  *   $TMPDIR/.guestfs-$UID/initrd.$PID
110  *   $TMPDIR/.guestfs-$UID/root.$PID
111  *
112  * A lock is taken on "checksum" while we perform the link.
113  *
114  * Linked files are deleted by a garbage collection sweep which can be
115  * initiated by any libguestfs process with the same UID when the
116  * corresponding PID no longer exists.  (This is safe: the parent is
117  * always around in guestfs_launch() while qemu is starting up, and
118  * after that qemu will either have finished with the files or be
119  * holding them open, so we can unlink them).
120  *
121  * When building a new appliance (step (3)), it is built into randomly
122  * named temporary files in the $TMPDIR.  Then a lock is acquired on
123  * $TMPDIR/.guestfs-$UID/checksum (this file being created if
124  * necessary), the files are renamed into their final location, and
125  * the lock is released.
126  */
127 int
128 guestfs___build_appliance (guestfs_h *g,
129                            char **kernel, char **initrd, char **appliance)
130 {
131   int r;
132   uid_t uid = geteuid ();
133
134   /* Step (1). */
135   char *supermin_path;
136   r = find_path (g, contains_supermin_appliance, NULL, &supermin_path);
137   if (r == -1)
138     return -1;
139
140   if (r == 1) {
141     /* Step (2): calculate checksum. */
142     char *checksum = calculate_supermin_checksum (g, supermin_path);
143     if (checksum) {
144       /* Step (3): cached appliance exists? */
145       r = check_for_cached_appliance (g, supermin_path, checksum, uid,
146                                       kernel, initrd, appliance);
147       if (r != 0) {
148         free (supermin_path);
149         free (checksum);
150         return r == 1 ? 0 : -1;
151       }
152
153       /* Step (4): build supermin appliance. */
154       r = build_supermin_appliance (g, supermin_path, checksum, uid,
155                                     kernel, initrd, appliance);
156       free (supermin_path);
157       free (checksum);
158       return r;
159     }
160     free (supermin_path);
161   }
162
163   /* Step (5). */
164   char *path;
165   r = find_path (g, contains_ordinary_appliance, NULL, &path);
166   if (r == -1)
167     return -1;
168
169   if (r == 1) {
170     size_t len = strlen (path);
171     *kernel = safe_malloc (g, len + strlen (kernel_name) + 2);
172     *initrd = safe_malloc (g, len + strlen (initrd_name) + 2);
173     sprintf (*kernel, "%s/%s", path, kernel_name);
174     sprintf (*initrd, "%s/%s", path, initrd_name);
175     *appliance = NULL;
176
177     free (path);
178     return 0;
179   }
180
181   error (g, _("cannot find any suitable libguestfs supermin or ordinary appliance on LIBGUESTFS_PATH (search path: %s)"),
182          g->path);
183   return -1;
184 }
185
186 static int
187 contains_ordinary_appliance (guestfs_h *g, const char *path, void *data)
188 {
189   return dir_contains_files (path, kernel_name, initrd_name, NULL);
190 }
191
192 static int
193 contains_supermin_appliance (guestfs_h *g, const char *path, void *data)
194 {
195   return dir_contains_files (path, "supermin.d", NULL);
196 }
197
198 /* supermin_path is a path which is known to contain a supermin
199  * appliance.  Using febootstrap-supermin-helper -f checksum calculate
200  * the checksum so we can see if it is cached.
201  */
202 static char *
203 calculate_supermin_checksum (guestfs_h *g, const char *supermin_path)
204 {
205   size_t len = 2 * strlen (supermin_path) + 256;
206   char cmd[len];
207   int pass_u_g_args = getuid () != geteuid () || getgid () != getegid ();
208
209   if (!pass_u_g_args)
210     snprintf (cmd, len,
211               "febootstrap-supermin-helper%s "
212               "-f checksum "
213               "'%s/supermin.d' "
214               host_cpu,
215               g->verbose ? " --verbose" : "",
216               supermin_path);
217   else
218     snprintf (cmd, len,
219               "febootstrap-supermin-helper%s "
220               "-u %i "
221               "-g %i "
222               "-f checksum "
223               "'%s/supermin.d' "
224               host_cpu,
225               g->verbose ? " --verbose" : "",
226               geteuid (), getegid (),
227               supermin_path);
228
229   if (g->verbose)
230     guestfs___print_timestamped_message (g, "%s", cmd);
231
232   /* Errors here are non-fatal, so we don't need to call error(). */
233   FILE *pp = popen (cmd, "r");
234   if (pp == NULL)
235     return NULL;
236
237   char checksum[256];
238   if (fgets (checksum, sizeof checksum, pp) == NULL) {
239     pclose (pp);
240     return NULL;
241   }
242
243   if (pclose (pp) == -1) {
244     perror ("pclose");
245     return NULL;
246   }
247
248   len = strlen (checksum);
249
250   if (len < 16) {               /* sanity check */
251     fprintf (stderr, "libguestfs: internal error: febootstrap-supermin-helper -f checksum returned a short string\n");
252     return NULL;
253   }
254
255   if (len > 0 && checksum[len-1] == '\n')
256     checksum[--len] = '\0';
257
258   return safe_strndup (g, checksum, len);
259 }
260
261 static int
262 process_exists (int pid)
263 {
264   if (kill (pid, 0) == 0)
265     return 1;
266
267   if (errno == ESRCH)
268     return 0;
269
270   return -1;
271 }
272
273 /* Garbage collect appliance hard links.  Files that match
274  * (kernel|initrd|root).$PID where the corresponding PID doesn't exist
275  * are deleted.  Note that errors in this function don't matter.
276  * There may also be other libguestfs processes racing to do the same
277  * thing here.
278  */
279 static void
280 garbage_collect_appliances (const char *cachedir)
281 {
282   DIR *dir;
283   struct dirent *d;
284   int pid;
285
286   dir = opendir (cachedir);
287   if (dir == NULL)
288     return;
289
290   while ((d = readdir (dir)) != NULL) {
291     if (sscanf (d->d_name, "kernel.%d", &pid) == 1 &&
292         process_exists (pid) == 0)
293       unlinkat (dirfd (dir), d->d_name, 0);
294     else if (sscanf (d->d_name, "initrd.%d", &pid) == 1 &&
295              process_exists (pid) == 0)
296       unlinkat (dirfd (dir), d->d_name, 0);
297     else if (sscanf (d->d_name, "root.%d", &pid) == 1 &&
298              process_exists (pid) == 0)
299       unlinkat (dirfd (dir), d->d_name, 0);
300   }
301
302   closedir (dir);
303 }
304
305 static int
306 check_for_cached_appliance (guestfs_h *g,
307                             const char *supermin_path, const char *checksum,
308                             uid_t uid,
309                             char **kernel, char **initrd, char **appliance)
310 {
311   const char *tmpdir = guestfs_tmpdir ();
312
313   /* len must be longer than the length of any pathname we can
314    * generate in this function.
315    */
316   size_t len = strlen (tmpdir) + 128;
317   char cachedir[len];
318   snprintf (cachedir, len, "%s/.guestfs-%d", tmpdir, uid);
319   char filename[len];
320   snprintf (filename, len, "%s/checksum", cachedir);
321
322   (void) mkdir (cachedir, 0755);
323   (void) utime (cachedir, NULL);
324
325   /* See if the cache directory exists and passes some simple checks
326    * to make sure it has not been tampered with.
327    */
328   struct stat statbuf;
329   if (lstat (cachedir, &statbuf) == -1)
330     return 0;
331   if (statbuf.st_uid != uid) {
332     error (g, _("security: cached appliance %s is not owned by UID %d"),
333            filename, uid);
334     return -1;
335   }
336   if (!S_ISDIR (statbuf.st_mode)) {
337     error (g, _("security: cached appliance %s is not a directory (mode %o)"),
338            filename, statbuf.st_mode);
339     return -1;
340   }
341   if ((statbuf.st_mode & 0022) != 0) {
342     error (g, _("security: cached appliance %s is writable by group or other (mode %o)"),
343            cachedir, statbuf.st_mode);
344     return -1;
345   }
346
347   garbage_collect_appliances (cachedir);
348
349   /* Try to open and acquire a lock on the checksum file. */
350   int fd = open (filename, O_RDONLY);
351   if (fd == -1)
352     return 0;
353   (void) futimens (fd, NULL);
354   struct flock fl;
355   fl.l_type = F_RDLCK;
356   fl.l_whence = SEEK_SET;
357   fl.l_start = 0;
358   fl.l_len = 1;
359  again:
360   if (fcntl (fd, F_SETLKW, &fl) == -1) {
361     if (errno == EINTR)
362       goto again;
363     perrorf (g, "fcntl: F_SETLKW: %s", filename);
364     close (fd);
365     return -1;
366   }
367
368   /* Read the checksum file. */
369   size_t clen = strlen (checksum);
370   char checksum_on_disk[clen];
371   ssize_t rr = read (fd, checksum_on_disk, clen);
372   if (rr == -1) {
373     perrorf (g, "read: %s", filename);
374     close (fd);
375     return -1;
376   }
377   if ((size_t) rr != clen) {
378     close (fd);
379     return 0;
380   }
381
382   if (memcmp (checksum, checksum_on_disk, clen) != 0) {
383     close (fd);
384     return 0;
385   }
386
387   /* At this point, cachedir exists, and checksum matches, and we have
388    * a read lock on the checksum file.  Make hard links to the files.
389    */
390   if (hard_link_to_cached_appliance (g, cachedir,
391                                      kernel, initrd, appliance) == -1) {
392     close (fd);
393     return -1;
394   }
395
396   /* Releases the lock on checksum. */
397   if (close (fd) == -1) {
398     perrorf (g, "close");
399     return -1;
400   }
401
402   /* Exists! */
403   return 1;
404 }
405
406 /* Build supermin appliance from supermin_path to $TMPDIR/.guestfs-$UID.
407  *
408  * Returns:
409  * 0 = built
410  * -1 = error (aborts launch)
411  */
412 static int
413 build_supermin_appliance (guestfs_h *g,
414                           const char *supermin_path, const char *checksum,
415                           uid_t uid,
416                           char **kernel, char **initrd, char **appliance)
417 {
418   if (g->verbose)
419     guestfs___print_timestamped_message (g, "begin building supermin appliance");
420
421   const char *tmpdir = guestfs_tmpdir ();
422
423   /* len must be longer than the length of any pathname we can
424    * generate in this function.
425    */
426   size_t len = strlen (tmpdir) + 128;
427
428   /* Build the appliance into a temporary directory. */
429   char tmpcd[len];
430   snprintf (tmpcd, len, "%s/guestfs.XXXXXX", tmpdir);
431
432   if (mkdtemp (tmpcd) == NULL) {
433     perrorf (g, "mkdtemp");
434     return -1;
435   }
436
437   if (g->verbose)
438     guestfs___print_timestamped_message (g, "run febootstrap-supermin-helper");
439
440   int r = run_supermin_helper (g, supermin_path, tmpcd, len);
441   if (r == -1)
442     return -1;
443
444   if (g->verbose)
445     guestfs___print_timestamped_message (g, "finished building supermin appliance");
446
447   char cachedir[len];
448   snprintf (cachedir, len, "%s/.guestfs-%d", tmpdir, uid);
449   char filename[len];
450   char filename2[len];
451   snprintf (filename, len, "%s/checksum", cachedir);
452
453   /* Open and acquire write lock on checksum file.  The file might
454    * not exist, in which case we want to create it.
455    */
456   int fd = open (filename, O_WRONLY|O_CREAT, 0755);
457   if (fd == -1) {
458     perrorf (g, "open: %s", filename);
459     return -1;
460   }
461   struct flock fl;
462   fl.l_type = F_WRLCK;
463   fl.l_whence = SEEK_SET;
464   fl.l_start = 0;
465   fl.l_len = 1;
466  again:
467   if (fcntl (fd, F_SETLKW, &fl) == -1) {
468     if (errno == EINTR)
469       goto again;
470     perrorf (g, "fcntl: F_SETLKW: %s", filename);
471     close (fd);
472     return -1;
473   }
474
475   /* At this point we have acquired a write lock on the checksum
476    * file so we go ahead and replace it with the new checksum, and
477    * rename in appliance files into this directory.
478    */
479   size_t clen = strlen (checksum);
480   if (ftruncate (fd, clen) == -1) {
481     perrorf (g, "ftruncate: %s", filename);
482     close (fd);
483     return -1;
484   }
485
486   ssize_t rr = write (fd, checksum, clen);
487   if (rr == -1) {
488     perrorf (g, "write: %s", filename);
489     close (fd);
490     return -1;
491   }
492   if ((size_t) rr != clen) {
493     error (g, "partial write: %s", filename);
494     close (fd);
495     return -1;
496   }
497
498   snprintf (filename, len, "%s/kernel", tmpcd);
499   snprintf (filename2, len, "%s/kernel", cachedir);
500   unlink (filename2);
501   if (rename (filename, filename2) == -1) {
502     perrorf (g, "rename: %s %s", filename, filename2);
503     close (fd);
504     return -1;
505   }
506
507   snprintf (filename, len, "%s/initrd", tmpcd);
508   snprintf (filename2, len, "%s/initrd", cachedir);
509   unlink (filename2);
510   if (rename (filename, filename2) == -1) {
511     perrorf (g, "rename: %s %s", filename, filename2);
512     close (fd);
513     return -1;
514   }
515
516   snprintf (filename, len, "%s/root", tmpcd);
517   snprintf (filename2, len, "%s/root", cachedir);
518   unlink (filename2);
519   if (rename (filename, filename2) == -1) {
520     perrorf (g, "rename: %s %s", filename, filename2);
521     close (fd);
522     return -1;
523   }
524
525   rmdir (tmpcd);
526
527   /* Now finish off by linking to the cached appliance and returning it. */
528   if (hard_link_to_cached_appliance (g, cachedir,
529                                      kernel, initrd, appliance) == -1) {
530     close (fd);
531     return -1;
532   }
533
534   /* Releases the lock on checksum. */
535   if (close (fd) == -1) {
536     perrorf (g, "close");
537     return -1;
538   }
539
540   return 0;
541 }
542
543 /* NB: lock on checksum file must be held when this is called. */
544 static int
545 hard_link_to_cached_appliance (guestfs_h *g,
546                                const char *cachedir,
547                                char **kernel, char **initrd, char **appliance)
548 {
549   pid_t pid = getpid ();
550   size_t len = strlen (cachedir) + 32;
551
552   *kernel = safe_malloc (g, len);
553   *initrd = safe_malloc (g, len);
554   *appliance = safe_malloc (g, len);
555   snprintf (*kernel, len, "%s/kernel.%d", cachedir, pid);
556   snprintf (*initrd, len, "%s/initrd.%d", cachedir, pid);
557   snprintf (*appliance, len, "%s/root.%d", cachedir, pid);
558
559   char filename[len];
560   snprintf (filename, len, "%s/kernel", cachedir);
561   (void) unlink (*kernel);
562   if (link (filename, *kernel) == -1) {
563     perrorf (g, "link: %s %s", filename, *kernel);
564     goto error;
565   }
566   (void) lutimes (filename, NULL); /* lutimes because it's a symlink */
567
568   snprintf (filename, len, "%s/initrd", cachedir);
569   (void) unlink (*initrd);
570   if (link (filename, *initrd) == -1) {
571     perrorf (g, "link: %s %s", filename, *initrd);
572     goto error;
573   }
574   (void) utime (filename, NULL);
575
576   snprintf (filename, len, "%s/root", cachedir);
577   (void) unlink (*appliance);
578   if (link (filename, *appliance) == -1) {
579     perrorf (g, "link: %s %s", filename, *appliance);
580     goto error;
581   }
582   (void) utime (filename, NULL);
583
584   return 0;
585
586  error:
587   free (*kernel);
588   free (*initrd);
589   free (*appliance);
590   return -1;
591 }
592
593 /* Run febootstrap-supermin-helper and tell it to generate the
594  * appliance.
595  */
596 static int
597 run_supermin_helper (guestfs_h *g, const char *supermin_path,
598                      const char *cachedir, size_t cdlen)
599 {
600   size_t pathlen = strlen (supermin_path);
601
602   const char *argv[30];
603   size_t i = 0;
604
605   char uid[32];
606   snprintf (uid, sizeof uid, "%i", geteuid ());
607   char gid[32];
608   snprintf (gid, sizeof gid, "%i", getegid ());
609   char supermin_d[pathlen + 32];
610   snprintf (supermin_d, pathlen + 32, "%s/supermin.d", supermin_path);
611   char kernel[cdlen + 32];
612   snprintf (kernel, cdlen + 32, "%s/kernel", cachedir);
613   char initrd[cdlen + 32];
614   snprintf (initrd, cdlen + 32, "%s/initrd", cachedir);
615   char root[cdlen + 32];
616   snprintf (root, cdlen + 32, "%s/root", cachedir);
617
618   int pass_u_g_args = getuid () != geteuid () || getgid () != getegid ();
619
620   argv[i++] = "febootstrap-supermin-helper";
621   if (g->verbose)
622     argv[i++] = "--verbose";
623   if (pass_u_g_args) {
624     argv[i++] = "-u";
625     argv[i++] = uid;
626     argv[i++] = "-g";
627     argv[i++] = gid;
628   }
629   argv[i++] = "-f";
630   argv[i++] = "ext2";
631   argv[i++] = supermin_d;
632   argv[i++] = host_cpu;
633   argv[i++] = kernel;
634   argv[i++] = initrd;
635   argv[i++] = root;
636   argv[i++] = NULL;
637
638   pid_t pid = fork ();
639   if (pid == -1) {
640     perrorf (g, "fork");
641     return -1;
642   }
643
644   if (pid > 0) {                /* Parent. */
645     if (g->verbose)
646       guestfs___print_timestamped_argv (g, argv);
647
648     int status;
649     if (waitpid (pid, &status, 0) == -1) {
650       perrorf (g, "waitpid");
651       return -1;
652     }
653     if (!WIFEXITED (status) || WEXITSTATUS (status) != 0) {
654       error (g, _("external command failed, see earlier error messages"));
655       return -1;
656     }
657     return 0;
658   }
659
660   /* Child. */
661
662   /* Set a sensible umask in the subprocess, so kernel and initrd
663    * output files are world-readable (RHBZ#610880).
664    */
665   umask (0022);
666
667   execvp ("febootstrap-supermin-helper", (char * const *) argv);
668   perror ("execvp");
669   _exit (EXIT_FAILURE);
670 }
671
672 /* Search elements of g->path, returning the first path element which
673  * matches the predicate function 'pred'.
674  *
675  * Function 'pred' must return a true or false value.  If it returns
676  * -1 then the entire search is aborted.
677  *
678  * Return values:
679  * 1 = a path element matched, it is returned in *pelem_ret and must be
680  *     freed by the caller,
681  * 0 = no path element matched, *pelem_ret is set to NULL, or
682  * -1 = error which aborts the launch process
683  */
684 static int
685 find_path (guestfs_h *g,
686            int (*pred) (guestfs_h *g, const char *pelem, void *data),
687            void *data,
688            char **pelem_ret)
689 {
690   size_t len;
691   int r;
692   const char *pelem = g->path;
693
694   /* Note that if g->path is an empty string, we want to check the
695    * current directory (for backwards compatibility with
696    * libguestfs < 1.5.4).
697    */
698   do {
699     len = strcspn (pelem, ":");
700
701     /* Empty element or "." means current directory. */
702     if (len == 0)
703       *pelem_ret = safe_strdup (g, ".");
704     else
705       *pelem_ret = safe_strndup (g, pelem, len);
706
707     r = pred (g, *pelem_ret, data);
708     if (r == -1) {
709       free (*pelem_ret);
710       return -1;
711     }
712
713     if (r != 0)                 /* predicate matched */
714       return 1;
715
716     free (*pelem_ret);
717
718     if (pelem[len] == ':')
719       pelem += len + 1;
720     else
721       pelem += len;
722   } while (*pelem);
723
724   /* Predicate didn't match on any path element. */
725   *pelem_ret = NULL;
726   return 0;
727 }
728
729 /* Returns true iff file is contained in dir. */
730 static int
731 dir_contains_file (const char *dir, const char *file)
732 {
733   size_t dirlen = strlen (dir);
734   size_t filelen = strlen (file);
735   size_t len = dirlen + filelen + 2;
736   char path[len];
737
738   snprintf (path, len, "%s/%s", dir, file);
739   return access (path, F_OK) == 0;
740 }
741
742 /* Returns true iff every listed file is contained in 'dir'. */
743 static int
744 dir_contains_files (const char *dir, ...)
745 {
746   va_list args;
747   const char *file;
748
749   va_start (args, dir);
750   while ((file = va_arg (args, const char *)) != NULL) {
751     if (!dir_contains_file (dir, file)) {
752       va_end (args);
753       return 0;
754     }
755   }
756   va_end (args);
757   return 1;
758 }