]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/utils.c
Merge pull request #2496 from flx42/nvidia-hook-lgpl
[mirror_lxc.git] / src / lxc / utils.c
1 /*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
7 * Daniel Lezcano <daniel.lezcano at free.fr>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #include "config.h"
25
26 #define __STDC_FORMAT_MACROS /* Required for PRIu64 to work. */
27 #include <ctype.h>
28 #include <dirent.h>
29 #include <errno.h>
30 #include <fcntl.h>
31 #include <grp.h>
32 #include <inttypes.h>
33 #include <libgen.h>
34 #include <pthread.h>
35 #include <stddef.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <sys/mman.h>
41 #include <sys/mount.h>
42 #include <sys/param.h>
43 #include <sys/prctl.h>
44 #include <sys/stat.h>
45 #include <sys/types.h>
46 #include <sys/wait.h>
47
48 #include "log.h"
49 #include "lxclock.h"
50 #include "namespace.h"
51 #include "parse.h"
52 #include "utils.h"
53
54 #ifndef HAVE_STRLCPY
55 #include "include/strlcpy.h"
56 #endif
57
58 #ifndef HAVE_STRLCAT
59 #include "include/strlcat.h"
60 #endif
61
62 #ifndef O_PATH
63 #define O_PATH 010000000
64 #endif
65
66 #ifndef O_NOFOLLOW
67 #define O_NOFOLLOW 00400000
68 #endif
69
70 lxc_log_define(utils, lxc);
71
72 /*
73 * if path is btrfs, tries to remove it and any subvolumes beneath it
74 */
75 extern bool btrfs_try_remove_subvol(const char *path);
76
77 static int _recursive_rmdir(const char *dirname, dev_t pdev,
78 const char *exclude, int level, bool onedev)
79 {
80 struct dirent *direntp;
81 DIR *dir;
82 int ret, failed=0;
83 char pathname[MAXPATHLEN];
84 bool hadexclude = false;
85
86 dir = opendir(dirname);
87 if (!dir) {
88 ERROR("failed to open %s", dirname);
89 return -1;
90 }
91
92 while ((direntp = readdir(dir))) {
93 struct stat mystat;
94 int rc;
95
96 if (!strcmp(direntp->d_name, ".") ||
97 !strcmp(direntp->d_name, ".."))
98 continue;
99
100 rc = snprintf(pathname, MAXPATHLEN, "%s/%s", dirname, direntp->d_name);
101 if (rc < 0 || rc >= MAXPATHLEN) {
102 ERROR("pathname too long");
103 failed=1;
104 continue;
105 }
106
107 if (!level && exclude && !strcmp(direntp->d_name, exclude)) {
108 ret = rmdir(pathname);
109 if (ret < 0) {
110 switch(errno) {
111 case ENOTEMPTY:
112 INFO("Not deleting snapshot %s", pathname);
113 hadexclude = true;
114 break;
115 case ENOTDIR:
116 ret = unlink(pathname);
117 if (ret)
118 INFO("Failed to remove %s", pathname);
119 break;
120 default:
121 SYSERROR("Failed to rmdir %s", pathname);
122 failed = 1;
123 break;
124 }
125 }
126 continue;
127 }
128
129 ret = lstat(pathname, &mystat);
130 if (ret) {
131 ERROR("Failed to stat %s", pathname);
132 failed = 1;
133 continue;
134 }
135
136 if (onedev && mystat.st_dev != pdev) {
137 /* TODO should we be checking /proc/self/mountinfo for
138 * pathname and not doing this if found? */
139 if (btrfs_try_remove_subvol(pathname))
140 INFO("Removed btrfs subvolume at %s\n", pathname);
141 continue;
142 }
143
144 if (S_ISDIR(mystat.st_mode)) {
145 if (_recursive_rmdir(pathname, pdev, exclude, level+1, onedev) < 0)
146 failed=1;
147 } else {
148 if (unlink(pathname) < 0) {
149 SYSERROR("Failed to delete %s", pathname);
150 failed=1;
151 }
152 }
153 }
154
155 if (rmdir(dirname) < 0 && !btrfs_try_remove_subvol(dirname) && !hadexclude) {
156 ERROR("Failed to delete %s", dirname);
157 failed=1;
158 }
159
160 ret = closedir(dir);
161 if (ret) {
162 ERROR("Failed to close directory %s", dirname);
163 failed=1;
164 }
165
166 return failed ? -1 : 0;
167 }
168
169 /* We have two different magic values for overlayfs, yay. */
170 #ifndef OVERLAYFS_SUPER_MAGIC
171 #define OVERLAYFS_SUPER_MAGIC 0x794c764f
172 #endif
173
174 #ifndef OVERLAY_SUPER_MAGIC
175 #define OVERLAY_SUPER_MAGIC 0x794c7630
176 #endif
177
178 /* In overlayfs, st_dev is unreliable. So on overlayfs we don't do the
179 * lxc_rmdir_onedev()
180 */
181 static bool is_native_overlayfs(const char *path)
182 {
183 if (has_fs_type(path, OVERLAY_SUPER_MAGIC) ||
184 has_fs_type(path, OVERLAYFS_SUPER_MAGIC))
185 return true;
186
187 return false;
188 }
189
190 /* returns 0 on success, -1 if there were any failures */
191 extern int lxc_rmdir_onedev(const char *path, const char *exclude)
192 {
193 struct stat mystat;
194 bool onedev = true;
195
196 if (is_native_overlayfs(path))
197 onedev = false;
198
199 if (lstat(path, &mystat) < 0) {
200 if (errno == ENOENT)
201 return 0;
202
203 ERROR("Failed to stat %s", path);
204 return -1;
205 }
206
207 return _recursive_rmdir(path, mystat.st_dev, exclude, 0, onedev);
208 }
209
210 /* borrowed from iproute2 */
211 extern int get_u16(unsigned short *val, const char *arg, int base)
212 {
213 unsigned long res;
214 char *ptr;
215
216 if (!arg || !*arg)
217 return -1;
218
219 errno = 0;
220 res = strtoul(arg, &ptr, base);
221 if (!ptr || ptr == arg || *ptr || res > 0xFFFF || errno != 0)
222 return -1;
223
224 *val = res;
225
226 return 0;
227 }
228
229 extern int mkdir_p(const char *dir, mode_t mode)
230 {
231 const char *tmp = dir;
232 const char *orig = dir;
233 char *makeme;
234
235 do {
236 dir = tmp + strspn(tmp, "/");
237 tmp = dir + strcspn(dir, "/");
238
239 makeme = strndup(orig, dir - orig);
240 if (*makeme) {
241 if (mkdir(makeme, mode) && errno != EEXIST) {
242 SYSERROR("failed to create directory '%s'", makeme);
243 free(makeme);
244 return -1;
245 }
246 }
247 free(makeme);
248 } while(tmp != dir);
249
250 return 0;
251 }
252
253 char *get_rundir()
254 {
255 char *rundir;
256 const char *homedir;
257 struct stat sb;
258
259 if (stat(RUNTIME_PATH, &sb) < 0)
260 return NULL;
261
262 if (geteuid() == sb.st_uid || getegid() == sb.st_gid) {
263 rundir = strdup(RUNTIME_PATH);
264 return rundir;
265 }
266
267 rundir = getenv("XDG_RUNTIME_DIR");
268 if (rundir) {
269 rundir = strdup(rundir);
270 return rundir;
271 }
272
273 INFO("XDG_RUNTIME_DIR isn't set in the environment.");
274 homedir = getenv("HOME");
275 if (!homedir) {
276 ERROR("HOME isn't set in the environment.");
277 return NULL;
278 }
279
280 rundir = malloc(sizeof(char) * (17 + strlen(homedir)));
281 if (!rundir)
282 return NULL;
283
284 sprintf(rundir, "%s/.cache/lxc/run/", homedir);
285
286 return rundir;
287 }
288
289 int wait_for_pid(pid_t pid)
290 {
291 int status, ret;
292
293 again:
294 ret = waitpid(pid, &status, 0);
295 if (ret == -1) {
296 if (errno == EINTR)
297 goto again;
298
299 return -1;
300 }
301
302 if (ret != pid)
303 goto again;
304
305 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
306 return -1;
307
308 return 0;
309 }
310
311 int lxc_wait_for_pid_status(pid_t pid)
312 {
313 int status, ret;
314
315 again:
316 ret = waitpid(pid, &status, 0);
317 if (ret == -1) {
318 if (errno == EINTR)
319 goto again;
320
321 return -1;
322 }
323
324 if (ret != pid)
325 goto again;
326
327 return status;
328 }
329
330 ssize_t lxc_write_nointr(int fd, const void *buf, size_t count)
331 {
332 ssize_t ret;
333 again:
334 ret = write(fd, buf, count);
335 if (ret < 0 && errno == EINTR)
336 goto again;
337
338 return ret;
339 }
340
341 ssize_t lxc_read_nointr(int fd, void *buf, size_t count)
342 {
343 ssize_t ret;
344 again:
345 ret = read(fd, buf, count);
346 if (ret < 0 && errno == EINTR)
347 goto again;
348
349 return ret;
350 }
351
352 ssize_t lxc_read_nointr_expect(int fd, void *buf, size_t count, const void *expected_buf)
353 {
354 ssize_t ret;
355
356 ret = lxc_read_nointr(fd, buf, count);
357 if (ret <= 0)
358 return ret;
359
360 if ((size_t)ret != count)
361 return -1;
362
363 if (expected_buf && memcmp(buf, expected_buf, count) != 0) {
364 errno = EINVAL;
365 return -1;
366 }
367
368 return ret;
369 }
370
371 #if HAVE_LIBGNUTLS
372 #include <gnutls/gnutls.h>
373 #include <gnutls/crypto.h>
374
375 __attribute__((constructor))
376 static void gnutls_lxc_init(void)
377 {
378 gnutls_global_init();
379 }
380
381 int sha1sum_file(char *fnam, unsigned char *digest)
382 {
383 char *buf;
384 int ret;
385 FILE *f;
386 long flen;
387
388 if (!fnam)
389 return -1;
390
391 f = fopen_cloexec(fnam, "r");
392 if (!f) {
393 SYSERROR("Error opening template");
394 return -1;
395 }
396
397 if (fseek(f, 0, SEEK_END) < 0) {
398 SYSERROR("Error seeking to end of template");
399 fclose(f);
400 return -1;
401 }
402
403 if ((flen = ftell(f)) < 0) {
404 SYSERROR("Error telling size of template");
405 fclose(f);
406 return -1;
407 }
408
409 if (fseek(f, 0, SEEK_SET) < 0) {
410 SYSERROR("Error seeking to start of template");
411 fclose(f);
412 return -1;
413 }
414
415 if ((buf = malloc(flen+1)) == NULL) {
416 SYSERROR("Out of memory");
417 fclose(f);
418 return -1;
419 }
420
421 if (fread(buf, 1, flen, f) != flen) {
422 SYSERROR("Failure reading template");
423 free(buf);
424 fclose(f);
425 return -1;
426 }
427
428 if (fclose(f) < 0) {
429 SYSERROR("Failre closing template");
430 free(buf);
431 return -1;
432 }
433
434 buf[flen] = '\0';
435 ret = gnutls_hash_fast(GNUTLS_DIG_SHA1, buf, flen, (void *)digest);
436 free(buf);
437 return ret;
438 }
439 #endif
440
441 char** lxc_va_arg_list_to_argv(va_list ap, size_t skip, int do_strdup)
442 {
443 va_list ap2;
444 size_t count = 1 + skip;
445 char **result;
446
447 /* first determine size of argument list, we don't want to reallocate
448 * constantly...
449 */
450 va_copy(ap2, ap);
451 while (1) {
452 char* arg = va_arg(ap2, char*);
453 if (!arg)
454 break;
455 count++;
456 }
457 va_end(ap2);
458
459 result = calloc(count, sizeof(char*));
460 if (!result)
461 return NULL;
462
463 count = skip;
464 while (1) {
465 char* arg = va_arg(ap, char*);
466 if (!arg)
467 break;
468 arg = do_strdup ? strdup(arg) : arg;
469 if (!arg)
470 goto oom;
471 result[count++] = arg;
472 }
473
474 /* calloc has already set last element to NULL*/
475 return result;
476
477 oom:
478 free(result);
479 return NULL;
480 }
481
482 const char** lxc_va_arg_list_to_argv_const(va_list ap, size_t skip)
483 {
484 return (const char**)lxc_va_arg_list_to_argv(ap, skip, 0);
485 }
486
487 struct lxc_popen_FILE *lxc_popen(const char *command)
488 {
489 int ret;
490 int pipe_fds[2];
491 pid_t child_pid;
492 struct lxc_popen_FILE *fp = NULL;
493
494 ret = pipe2(pipe_fds, O_CLOEXEC);
495 if (ret < 0)
496 return NULL;
497
498 child_pid = fork();
499 if (child_pid < 0)
500 goto on_error;
501
502 if (!child_pid) {
503 sigset_t mask;
504
505 close(pipe_fds[0]);
506
507 /* duplicate stdout */
508 if (pipe_fds[1] != STDOUT_FILENO)
509 ret = dup2(pipe_fds[1], STDOUT_FILENO);
510 else
511 ret = fcntl(pipe_fds[1], F_SETFD, 0);
512 if (ret < 0) {
513 close(pipe_fds[1]);
514 _exit(EXIT_FAILURE);
515 }
516
517 /* duplicate stderr */
518 if (pipe_fds[1] != STDERR_FILENO)
519 ret = dup2(pipe_fds[1], STDERR_FILENO);
520 else
521 ret = fcntl(pipe_fds[1], F_SETFD, 0);
522 close(pipe_fds[1]);
523 if (ret < 0)
524 _exit(EXIT_FAILURE);
525
526 /* unblock all signals */
527 ret = sigfillset(&mask);
528 if (ret < 0)
529 _exit(EXIT_FAILURE);
530
531 ret = pthread_sigmask(SIG_UNBLOCK, &mask, NULL);
532 if (ret < 0)
533 _exit(EXIT_FAILURE);
534
535 execl("/bin/sh", "sh", "-c", command, (char *)NULL);
536 _exit(127);
537 }
538
539 close(pipe_fds[1]);
540 pipe_fds[1] = -1;
541
542 fp = malloc(sizeof(*fp));
543 if (!fp)
544 goto on_error;
545
546 memset(fp, 0, sizeof(*fp));
547
548 fp->child_pid = child_pid;
549 fp->pipe = pipe_fds[0];
550
551 /* From now on, closing fp->f will also close fp->pipe. So only ever
552 * call fclose(fp->f).
553 */
554 fp->f = fdopen(pipe_fds[0], "r");
555 if (!fp->f)
556 goto on_error;
557
558 return fp;
559
560 on_error:
561 /* We can only close pipe_fds[0] if fdopen() didn't succeed or wasn't
562 * called yet. Otherwise the fd belongs to the file opened by fdopen()
563 * since it isn't dup()ed.
564 */
565 if (fp && !fp->f && pipe_fds[0] >= 0)
566 close(pipe_fds[0]);
567
568 if (pipe_fds[1] >= 0)
569 close(pipe_fds[1]);
570
571 if (fp && fp->f)
572 fclose(fp->f);
573
574 if (fp)
575 free(fp);
576
577 return NULL;
578 }
579
580 int lxc_pclose(struct lxc_popen_FILE *fp)
581 {
582 pid_t wait_pid;
583 int wstatus = 0;
584
585 if (!fp)
586 return -1;
587
588 do {
589 wait_pid = waitpid(fp->child_pid, &wstatus, 0);
590 } while (wait_pid < 0 && errno == EINTR);
591
592 fclose(fp->f);
593 free(fp);
594
595 if (wait_pid < 0)
596 return -1;
597
598 return wstatus;
599 }
600
601 char *lxc_string_replace(const char *needle, const char *replacement, const char *haystack)
602 {
603 ssize_t len = -1, saved_len = -1;
604 char *result = NULL;
605 size_t replacement_len = strlen(replacement);
606 size_t needle_len = strlen(needle);
607
608 /* should be executed exactly twice */
609 while (len == -1 || result == NULL) {
610 char *p;
611 char *last_p;
612 ssize_t part_len;
613
614 if (len != -1) {
615 result = calloc(1, len + 1);
616 if (!result)
617 return NULL;
618
619 saved_len = len;
620 }
621
622 len = 0;
623
624 for (last_p = (char *)haystack, p = strstr(last_p, needle); p; last_p = p, p = strstr(last_p, needle)) {
625 part_len = (ssize_t)(p - last_p);
626 if (result && part_len > 0)
627 memcpy(&result[len], last_p, part_len);
628
629 len += part_len;
630
631 if (result && replacement_len > 0)
632 memcpy(&result[len], replacement, replacement_len);
633
634 len += replacement_len;
635 p += needle_len;
636 }
637
638 part_len = strlen(last_p);
639 if (result && part_len > 0)
640 memcpy(&result[len], last_p, part_len);
641
642 len += part_len;
643 }
644
645 /* make sure we did the same thing twice,
646 * once for calculating length, the other
647 * time for copying data */
648 if (saved_len != len) {
649 free(result);
650 return NULL;
651 }
652
653 /* make sure we didn't overwrite any buffer,
654 * due to calloc the string should be 0-terminated */
655 if (result[len] != '\0') {
656 free(result);
657 return NULL;
658 }
659
660 return result;
661 }
662
663 bool lxc_string_in_array(const char *needle, const char **haystack)
664 {
665 for (; haystack && *haystack; haystack++)
666 if (!strcmp(needle, *haystack))
667 return true;
668
669 return false;
670 }
671
672 char *lxc_string_join(const char *sep, const char **parts, bool use_as_prefix)
673 {
674 char *result;
675 char **p;
676 size_t sep_len = strlen(sep);
677 size_t result_len = use_as_prefix * sep_len;
678 size_t buf_len;
679
680 /* calculate new string length */
681 for (p = (char **)parts; *p; p++)
682 result_len += (p > (char **)parts) * sep_len + strlen(*p);
683
684 buf_len = result_len + 1;
685 result = calloc(buf_len, 1);
686 if (!result)
687 return NULL;
688
689 if (use_as_prefix)
690 (void)strlcpy(result, sep, buf_len);
691
692 for (p = (char **)parts; *p; p++) {
693 if (p > (char **)parts)
694 (void)strlcat(result, sep, buf_len);
695
696 (void)strlcat(result, *p, buf_len);
697 }
698
699 return result;
700 }
701
702 char **lxc_normalize_path(const char *path)
703 {
704 char **components;
705 char **p;
706 size_t components_len = 0;
707 size_t pos = 0;
708
709 components = lxc_string_split(path, '/');
710 if (!components)
711 return NULL;
712
713 for (p = components; *p; p++)
714 components_len++;
715
716 /* resolve '.' and '..' */
717 for (pos = 0; pos < components_len; ) {
718 if (!strcmp(components[pos], ".") || (!strcmp(components[pos], "..") && pos == 0)) {
719 /* eat this element */
720 free(components[pos]);
721 memmove(&components[pos], &components[pos+1], sizeof(char *) * (components_len - pos));
722 components_len--;
723 } else if (!strcmp(components[pos], "..")) {
724 /* eat this and the previous element */
725 free(components[pos - 1]);
726 free(components[pos]);
727 memmove(&components[pos-1], &components[pos+1], sizeof(char *) * (components_len - pos));
728 components_len -= 2;
729 pos--;
730 } else {
731 pos++;
732 }
733 }
734
735 return components;
736 }
737
738 char *lxc_deslashify(const char *path)
739 {
740 char *dup, *p;
741 char **parts = NULL;
742 size_t n, len;
743
744 dup = strdup(path);
745 if (!dup)
746 return NULL;
747
748 parts = lxc_normalize_path(dup);
749 if (!parts) {
750 free(dup);
751 return NULL;
752 }
753
754 /* We'll end up here if path == "///" or path == "". */
755 if (!*parts) {
756 len = strlen(dup);
757 if (!len) {
758 lxc_free_array((void **)parts, free);
759 return dup;
760 }
761
762 n = strcspn(dup, "/");
763 if (n == len) {
764 free(dup);
765 lxc_free_array((void **)parts, free);
766
767 p = strdup("/");
768 if (!p)
769 return NULL;
770
771 return p;
772 }
773 }
774
775 p = lxc_string_join("/", (const char **)parts, *dup == '/');
776 free(dup);
777 lxc_free_array((void **)parts, free);
778 return p;
779 }
780
781 char *lxc_append_paths(const char *first, const char *second)
782 {
783 int ret;
784 size_t len;
785 char *result = NULL;
786 const char *pattern = "%s%s";
787
788 len = strlen(first) + strlen(second) + 1;
789 if (second[0] != '/') {
790 len += 1;
791 pattern = "%s/%s";
792 }
793
794 result = calloc(1, len);
795 if (!result)
796 return NULL;
797
798 ret = snprintf(result, len, pattern, first, second);
799 if (ret < 0 || (size_t)ret >= len) {
800 free(result);
801 return NULL;
802 }
803
804 return result;
805 }
806
807 bool lxc_string_in_list(const char *needle, const char *haystack, char _sep)
808 {
809 char *token, *str;
810 char sep[2] = { _sep, '\0' };
811 size_t len;
812
813 if (!haystack || !needle)
814 return 0;
815
816 len = strlen(haystack);
817 str = alloca(len + 1);
818 (void)strlcpy(str, haystack, len + 1);
819
820 lxc_iterate_parts(token, str, sep)
821 if (strcmp(needle, token) == 0)
822 return 1;
823
824 return 0;
825 }
826
827 char **lxc_string_split(const char *string, char _sep)
828 {
829 char *token, *str;
830 char sep[2] = {_sep, '\0'};
831 char **tmp = NULL, **result = NULL;
832 size_t result_capacity = 0;
833 size_t result_count = 0;
834 int r, saved_errno;
835 size_t len;
836
837 if (!string)
838 return calloc(1, sizeof(char *));
839
840 len = strlen(string);
841 str = alloca(len + 1);
842 (void)strlcpy(str, string, len + 1);
843
844 lxc_iterate_parts(token, str, sep) {
845 r = lxc_grow_array((void ***)&result, &result_capacity, result_count + 1, 16);
846 if (r < 0)
847 goto error_out;
848
849 result[result_count] = strdup(token);
850 if (!result[result_count])
851 goto error_out;
852
853 result_count++;
854 }
855
856 /* if we allocated too much, reduce it */
857 tmp = realloc(result, (result_count + 1) * sizeof(char *));
858 if (!tmp)
859 goto error_out;
860
861 result = tmp;
862
863 /* Make sure we don't return uninitialized memory. */
864 if (result_count == 0)
865 *result = NULL;
866
867 return result;
868
869 error_out:
870 saved_errno = errno;
871 lxc_free_array((void **)result, free);
872 errno = saved_errno;
873 return NULL;
874 }
875
876 static bool complete_word(char ***result, char *start, char *end, size_t *cap, size_t *cnt)
877 {
878 int r;
879
880 r = lxc_grow_array((void ***)result, cap, 2 + *cnt, 16);
881 if (r < 0)
882 return false;
883
884 (*result)[*cnt] = strndup(start, end - start);
885 if (!(*result)[*cnt])
886 return false;
887
888 (*cnt)++;
889
890 return true;
891 }
892
893 /*
894 * Given a a string 'one two "three four"', split into three words,
895 * one, two, and "three four"
896 */
897 char **lxc_string_split_quoted(char *string)
898 {
899 char *nextword = string, *p, state;
900 char **result = NULL;
901 size_t result_capacity = 0;
902 size_t result_count = 0;
903
904 if (!string || !*string)
905 return calloc(1, sizeof(char *));
906
907 // TODO I'm *not* handling escaped quote
908 state = ' ';
909 for (p = string; *p; p++) {
910 switch(state) {
911 case ' ':
912 if (isspace(*p))
913 continue;
914 else if (*p == '"' || *p == '\'') {
915 nextword = p;
916 state = *p;
917 continue;
918 }
919 nextword = p;
920 state = 'a';
921 continue;
922 case 'a':
923 if (isspace(*p)) {
924 complete_word(&result, nextword, p, &result_capacity, &result_count);
925 state = ' ';
926 continue;
927 }
928 continue;
929 case '"':
930 case '\'':
931 if (*p == state) {
932 complete_word(&result, nextword+1, p, &result_capacity, &result_count);
933 state = ' ';
934 continue;
935 }
936 continue;
937 }
938 }
939
940 if (state == 'a')
941 complete_word(&result, nextword, p, &result_capacity, &result_count);
942
943 return realloc(result, (result_count + 1) * sizeof(char *));
944 }
945
946 char **lxc_string_split_and_trim(const char *string, char _sep)
947 {
948 char *token, *str;
949 char sep[2] = { _sep, '\0' };
950 char **result = NULL;
951 size_t result_capacity = 0;
952 size_t result_count = 0;
953 int r, saved_errno;
954 size_t i = 0;
955 size_t len;
956
957 if (!string)
958 return calloc(1, sizeof(char *));
959
960 len = strlen(string);
961 str = alloca(len + 1);
962 (void)strlcpy(str, string, len + 1);
963
964 lxc_iterate_parts(token, str, sep) {
965 while (token[0] == ' ' || token[0] == '\t')
966 token++;
967
968 i = strlen(token);
969 while (i > 0 && (token[i - 1] == ' ' || token[i - 1] == '\t')) {
970 token[i - 1] = '\0';
971 i--;
972 }
973
974 r = lxc_grow_array((void ***)&result, &result_capacity, result_count + 1, 16);
975 if (r < 0)
976 goto error_out;
977
978 result[result_count] = strdup(token);
979 if (!result[result_count])
980 goto error_out;
981
982 result_count++;
983 }
984
985 /* if we allocated too much, reduce it */
986 return realloc(result, (result_count + 1) * sizeof(char *));
987
988 error_out:
989 saved_errno = errno;
990 lxc_free_array((void **)result, free);
991 errno = saved_errno;
992 return NULL;
993 }
994
995 void lxc_free_array(void **array, lxc_free_fn element_free_fn)
996 {
997 void **p;
998
999 for (p = array; p && *p; p++)
1000 element_free_fn(*p);
1001
1002 free((void*)array);
1003 }
1004
1005 int lxc_grow_array(void ***array, size_t *capacity, size_t new_size, size_t capacity_increment)
1006 {
1007 size_t new_capacity;
1008 void **new_array;
1009
1010 /* first time around, catch some trivial mistakes of the user
1011 * only initializing one of these */
1012 if (!*array || !*capacity) {
1013 *array = NULL;
1014 *capacity = 0;
1015 }
1016
1017 new_capacity = *capacity;
1018 while (new_size + 1 > new_capacity)
1019 new_capacity += capacity_increment;
1020
1021 if (new_capacity != *capacity) {
1022 /* we have to reallocate */
1023 new_array = realloc(*array, new_capacity * sizeof(void *));
1024 if (!new_array)
1025 return -1;
1026
1027 memset(&new_array[*capacity], 0, (new_capacity - (*capacity)) * sizeof(void *));
1028 *array = new_array;
1029 *capacity = new_capacity;
1030 }
1031
1032 /* array has sufficient elements */
1033 return 0;
1034 }
1035
1036 size_t lxc_array_len(void **array)
1037 {
1038 void **p;
1039 size_t result = 0;
1040
1041 for (p = array; p && *p; p++)
1042 result++;
1043
1044 return result;
1045 }
1046
1047 int lxc_write_to_file(const char *filename, const void *buf, size_t count,
1048 bool add_newline, mode_t mode)
1049 {
1050 int fd, saved_errno;
1051 ssize_t ret;
1052
1053 fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, mode);
1054 if (fd < 0)
1055 return -1;
1056
1057 ret = lxc_write_nointr(fd, buf, count);
1058 if (ret < 0)
1059 goto out_error;
1060
1061 if ((size_t)ret != count)
1062 goto out_error;
1063
1064 if (add_newline) {
1065 ret = lxc_write_nointr(fd, "\n", 1);
1066 if (ret != 1)
1067 goto out_error;
1068 }
1069
1070 close(fd);
1071 return 0;
1072
1073 out_error:
1074 saved_errno = errno;
1075 close(fd);
1076 errno = saved_errno;
1077 return -1;
1078 }
1079
1080 int lxc_read_from_file(const char *filename, void *buf, size_t count)
1081 {
1082 int fd = -1, saved_errno;
1083 ssize_t ret;
1084
1085 fd = open(filename, O_RDONLY | O_CLOEXEC);
1086 if (fd < 0)
1087 return -1;
1088
1089 if (!buf || !count) {
1090 char buf2[100];
1091 size_t count2 = 0;
1092
1093 while ((ret = read(fd, buf2, 100)) > 0)
1094 count2 += ret;
1095
1096 if (ret >= 0)
1097 ret = count2;
1098 } else {
1099 memset(buf, 0, count);
1100 ret = read(fd, buf, count);
1101 }
1102
1103 if (ret < 0)
1104 SYSERROR("Read %s", filename);
1105
1106 saved_errno = errno;
1107 close(fd);
1108 errno = saved_errno;
1109 return ret;
1110 }
1111
1112 void **lxc_append_null_to_array(void **array, size_t count)
1113 {
1114 void **temp;
1115
1116 /* Append NULL to the array */
1117 if (count) {
1118 temp = realloc(array, (count + 1) * sizeof(*array));
1119 if (!temp) {
1120 size_t i;
1121 for (i = 0; i < count; i++)
1122 free(array[i]);
1123 free(array);
1124 return NULL;
1125 }
1126
1127 array = temp;
1128 array[count] = NULL;
1129 }
1130
1131 return array;
1132 }
1133
1134 int randseed(bool srand_it)
1135 {
1136 /*
1137 srand pre-seed function based on /dev/urandom
1138 */
1139 unsigned int seed = time(NULL) + getpid();
1140
1141 FILE *f;
1142 f = fopen("/dev/urandom", "r");
1143 if (f) {
1144 int ret = fread(&seed, sizeof(seed), 1, f);
1145 if (ret != 1)
1146 SYSDEBUG("unable to fread /dev/urandom, fallback to time+pid rand seed");
1147
1148 fclose(f);
1149 }
1150
1151 if (srand_it)
1152 srand(seed);
1153
1154 return seed;
1155 }
1156
1157 uid_t get_ns_uid(uid_t orig)
1158 {
1159 char *line = NULL;
1160 size_t sz = 0;
1161 uid_t nsid, hostid, range;
1162 FILE *f = fopen("/proc/self/uid_map", "r");
1163 if (!f)
1164 return 0;
1165
1166 while (getline(&line, &sz, f) != -1) {
1167 if (sscanf(line, "%u %u %u", &nsid, &hostid, &range) != 3)
1168 continue;
1169
1170 if (hostid <= orig && hostid + range > orig) {
1171 nsid += orig - hostid;
1172 goto found;
1173 }
1174 }
1175
1176 nsid = 0;
1177
1178 found:
1179 fclose(f);
1180 free(line);
1181 return nsid;
1182 }
1183
1184 bool dir_exists(const char *path)
1185 {
1186 struct stat sb;
1187 int ret;
1188
1189 ret = stat(path, &sb);
1190 if (ret < 0)
1191 /* Could be something other than eexist, just say "no". */
1192 return false;
1193
1194 return S_ISDIR(sb.st_mode);
1195 }
1196
1197 /* Note we don't use SHA-1 here as we don't want to depend on HAVE_GNUTLS.
1198 * FNV has good anti collision properties and we're not worried
1199 * about pre-image resistance or one-way-ness, we're just trying to make
1200 * the name unique in the 108 bytes of space we have.
1201 */
1202 uint64_t fnv_64a_buf(void *buf, size_t len, uint64_t hval)
1203 {
1204 unsigned char *bp;
1205
1206 for(bp = buf; bp < (unsigned char *)buf + len; bp++)
1207 {
1208 /* xor the bottom with the current octet */
1209 hval ^= (uint64_t)*bp;
1210
1211 /* gcc optimised:
1212 * multiply by the 64 bit FNV magic prime mod 2^64
1213 */
1214 hval += (hval << 1) + (hval << 4) + (hval << 5) +
1215 (hval << 7) + (hval << 8) + (hval << 40);
1216 }
1217
1218 return hval;
1219 }
1220
1221 bool is_shared_mountpoint(const char *path)
1222 {
1223 char buf[LXC_LINELEN];
1224 FILE *f;
1225 int i;
1226 char *p, *p2;
1227
1228 f = fopen("/proc/self/mountinfo", "r");
1229 if (!f)
1230 return 0;
1231
1232 while (fgets(buf, LXC_LINELEN, f)) {
1233 for (p = buf, i = 0; p && i < 4; i++)
1234 p = strchr(p + 1, ' ');
1235 if (!p)
1236 continue;
1237
1238 p2 = strchr(p + 1, ' ');
1239 if (!p2)
1240 continue;
1241
1242 *p2 = '\0';
1243 if (strcmp(p + 1, path) == 0) {
1244 /* This is the path. Is it shared? */
1245 p = strchr(p2 + 1, ' ');
1246 if (p && strstr(p, "shared:")) {
1247 fclose(f);
1248 return true;
1249 }
1250 }
1251 }
1252
1253 fclose(f);
1254 return false;
1255 }
1256
1257 /*
1258 * Detect whether / is mounted MS_SHARED. The only way I know of to
1259 * check that is through /proc/self/mountinfo.
1260 * I'm only checking for /. If the container rootfs or mount location
1261 * is MS_SHARED, but not '/', then you're out of luck - figuring that
1262 * out would be too much work to be worth it.
1263 */
1264 int detect_shared_rootfs(void)
1265 {
1266 if (is_shared_mountpoint("/"))
1267 return 1;
1268 return 0;
1269 }
1270
1271 bool switch_to_ns(pid_t pid, const char *ns) {
1272 int fd, ret;
1273 char nspath[MAXPATHLEN];
1274
1275 /* Switch to new ns */
1276 ret = snprintf(nspath, MAXPATHLEN, "/proc/%d/ns/%s", pid, ns);
1277 if (ret < 0 || ret >= MAXPATHLEN)
1278 return false;
1279
1280 fd = open(nspath, O_RDONLY);
1281 if (fd < 0) {
1282 SYSERROR("Failed to open %s", nspath);
1283 return false;
1284 }
1285
1286 ret = setns(fd, 0);
1287 if (ret) {
1288 SYSERROR("Failed to set process %d to %s of %d.", pid, ns, fd);
1289 close(fd);
1290 return false;
1291 }
1292
1293 close(fd);
1294 return true;
1295 }
1296
1297 /*
1298 * looking at fs/proc_namespace.c, it appears we can
1299 * actually expect the rootfs entry to very specifically contain
1300 * " - rootfs rootfs "
1301 * IIUC, so long as we've chrooted so that rootfs is not our root,
1302 * the rootfs entry should always be skipped in mountinfo contents.
1303 */
1304 bool detect_ramfs_rootfs(void)
1305 {
1306 FILE *f;
1307 char *p, *p2;
1308 char *line = NULL;
1309 size_t len = 0;
1310 int i;
1311
1312 f = fopen("/proc/self/mountinfo", "r");
1313 if (!f)
1314 return false;
1315
1316 while (getline(&line, &len, f) != -1) {
1317 for (p = line, i = 0; p && i < 4; i++)
1318 p = strchr(p + 1, ' ');
1319 if (!p)
1320 continue;
1321
1322 p2 = strchr(p + 1, ' ');
1323 if (!p2)
1324 continue;
1325
1326 *p2 = '\0';
1327 if (strcmp(p + 1, "/") == 0) {
1328 /* This is '/'. Is it the ramfs? */
1329 p = strchr(p2 + 1, '-');
1330 if (p && strncmp(p, "- rootfs rootfs ", 16) == 0) {
1331 free(line);
1332 fclose(f);
1333 return true;
1334 }
1335 }
1336 }
1337
1338 free(line);
1339 fclose(f);
1340 return false;
1341 }
1342
1343 char *on_path(const char *cmd, const char *rootfs) {
1344 char *entry = NULL, *path = NULL;
1345 char cmdpath[MAXPATHLEN];
1346 int ret;
1347
1348 path = getenv("PATH");
1349 if (!path)
1350 return NULL;
1351
1352 path = strdup(path);
1353 if (!path)
1354 return NULL;
1355
1356 lxc_iterate_parts(entry, path, ":") {
1357 if (rootfs)
1358 ret = snprintf(cmdpath, MAXPATHLEN, "%s/%s/%s", rootfs, entry, cmd);
1359 else
1360 ret = snprintf(cmdpath, MAXPATHLEN, "%s/%s", entry, cmd);
1361 if (ret < 0 || ret >= MAXPATHLEN)
1362 continue;
1363
1364 if (access(cmdpath, X_OK) == 0) {
1365 free(path);
1366 return strdup(cmdpath);
1367 }
1368 }
1369
1370 free(path);
1371 return NULL;
1372 }
1373
1374 bool file_exists(const char *f)
1375 {
1376 struct stat statbuf;
1377
1378 return stat(f, &statbuf) == 0;
1379 }
1380
1381 bool cgns_supported(void)
1382 {
1383 return file_exists("/proc/self/ns/cgroup");
1384 }
1385
1386 /* historically lxc-init has been under /usr/lib/lxc and under
1387 * /usr/lib/$ARCH/lxc. It now lives as $prefix/sbin/init.lxc.
1388 */
1389 char *choose_init(const char *rootfs)
1390 {
1391 char *retv = NULL;
1392 const char *empty = "",
1393 *tmp;
1394 int ret, env_set = 0;
1395
1396 if (!getenv("PATH")) {
1397 if (setenv("PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", 0))
1398 SYSERROR("Failed to setenv");
1399
1400 env_set = 1;
1401 }
1402
1403 retv = on_path("init.lxc", rootfs);
1404
1405 if (env_set) {
1406 if (unsetenv("PATH"))
1407 SYSERROR("Failed to unsetenv");
1408 }
1409
1410 if (retv)
1411 return retv;
1412
1413 retv = malloc(PATH_MAX);
1414 if (!retv)
1415 return NULL;
1416
1417 if (rootfs)
1418 tmp = rootfs;
1419 else
1420 tmp = empty;
1421
1422 ret = snprintf(retv, PATH_MAX, "%s/%s/%s", tmp, SBINDIR, "/init.lxc");
1423 if (ret < 0 || ret >= PATH_MAX) {
1424 ERROR("pathname too long");
1425 goto out1;
1426 }
1427
1428 if (access(retv, X_OK) == 0)
1429 return retv;
1430
1431 ret = snprintf(retv, PATH_MAX, "%s/%s/%s", tmp, LXCINITDIR, "/lxc/lxc-init");
1432 if (ret < 0 || ret >= PATH_MAX) {
1433 ERROR("pathname too long");
1434 goto out1;
1435 }
1436
1437 if (access(retv, X_OK) == 0)
1438 return retv;
1439
1440 ret = snprintf(retv, PATH_MAX, "%s/usr/lib/lxc/lxc-init", tmp);
1441 if (ret < 0 || ret >= PATH_MAX) {
1442 ERROR("pathname too long");
1443 goto out1;
1444 }
1445
1446 if (access(retv, X_OK) == 0)
1447 return retv;
1448
1449 ret = snprintf(retv, PATH_MAX, "%s/sbin/lxc-init", tmp);
1450 if (ret < 0 || ret >= PATH_MAX) {
1451 ERROR("pathname too long");
1452 goto out1;
1453 }
1454
1455 if (access(retv, X_OK) == 0)
1456 return retv;
1457
1458 /*
1459 * Last resort, look for the statically compiled init.lxc which we
1460 * hopefully bind-mounted in.
1461 * If we are called during container setup, and we get to this point,
1462 * then the init.lxc.static from the host will need to be bind-mounted
1463 * in. So we return NULL here to indicate that.
1464 */
1465 if (rootfs)
1466 goto out1;
1467
1468 ret = snprintf(retv, PATH_MAX, "/init.lxc.static");
1469 if (ret < 0 || ret >= PATH_MAX) {
1470 WARN("Nonsense - name /lxc.init.static too long");
1471 goto out1;
1472 }
1473
1474 if (access(retv, X_OK) == 0)
1475 return retv;
1476
1477 out1:
1478 free(retv);
1479 return NULL;
1480 }
1481
1482 int print_to_file(const char *file, const char *content)
1483 {
1484 FILE *f;
1485 int ret = 0;
1486
1487 f = fopen(file, "w");
1488 if (!f)
1489 return -1;
1490
1491 if (fprintf(f, "%s", content) != strlen(content))
1492 ret = -1;
1493
1494 fclose(f);
1495 return ret;
1496 }
1497
1498 int is_dir(const char *path)
1499 {
1500 struct stat statbuf;
1501 int ret;
1502
1503 ret = stat(path, &statbuf);
1504 if (ret == 0 && S_ISDIR(statbuf.st_mode))
1505 return 1;
1506
1507 return 0;
1508 }
1509
1510 /*
1511 * Given the '-t' template option to lxc-create, figure out what to
1512 * do. If the template is a full executable path, use that. If it
1513 * is something like 'sshd', then return $templatepath/lxc-sshd.
1514 * On success return the template, on error return NULL.
1515 */
1516 char *get_template_path(const char *t)
1517 {
1518 int ret, len;
1519 char *tpath;
1520
1521 if (t[0] == '/' && access(t, X_OK) == 0) {
1522 tpath = strdup(t);
1523 return tpath;
1524 }
1525
1526 len = strlen(LXCTEMPLATEDIR) + strlen(t) + strlen("/lxc-") + 1;
1527
1528 tpath = malloc(len);
1529 if (!tpath)
1530 return NULL;
1531
1532 ret = snprintf(tpath, len, "%s/lxc-%s", LXCTEMPLATEDIR, t);
1533 if (ret < 0 || ret >= len) {
1534 free(tpath);
1535 return NULL;
1536 }
1537
1538 if (access(tpath, X_OK) < 0) {
1539 SYSERROR("bad template: %s", t);
1540 free(tpath);
1541 return NULL;
1542 }
1543
1544 return tpath;
1545 }
1546
1547 /*
1548 * @path: a pathname where / replaced with '\0'.
1549 * @offsetp: pointer to int showing which path segment was last seen.
1550 * Updated on return to reflect the next segment.
1551 * @fulllen: full original path length.
1552 * Returns a pointer to the next path segment, or NULL if done.
1553 */
1554 static char *get_nextpath(char *path, int *offsetp, int fulllen)
1555 {
1556 int offset = *offsetp;
1557
1558 if (offset >= fulllen)
1559 return NULL;
1560
1561 while (path[offset] != '\0' && offset < fulllen)
1562 offset++;
1563
1564 while (path[offset] == '\0' && offset < fulllen)
1565 offset++;
1566
1567 *offsetp = offset;
1568 return (offset < fulllen) ? &path[offset] : NULL;
1569 }
1570
1571 /*
1572 * Check that @subdir is a subdir of @dir. @len is the length of
1573 * @dir (to avoid having to recalculate it).
1574 */
1575 static bool is_subdir(const char *subdir, const char *dir, size_t len)
1576 {
1577 size_t subdirlen = strlen(subdir);
1578
1579 if (subdirlen < len)
1580 return false;
1581
1582 if (strncmp(subdir, dir, len) != 0)
1583 return false;
1584
1585 if (dir[len-1] == '/')
1586 return true;
1587
1588 if (subdir[len] == '/' || subdirlen == len)
1589 return true;
1590
1591 return false;
1592 }
1593
1594 /*
1595 * Check if the open fd is a symlink. Return -ELOOP if it is. Return
1596 * -ENOENT if we couldn't fstat. Return 0 if the fd is ok.
1597 */
1598 static int check_symlink(int fd)
1599 {
1600 struct stat sb;
1601 int ret;
1602
1603 ret = fstat(fd, &sb);
1604 if (ret < 0)
1605 return -ENOENT;
1606
1607 if (S_ISLNK(sb.st_mode))
1608 return -ELOOP;
1609
1610 return 0;
1611 }
1612
1613 /*
1614 * Open a file or directory, provided that it contains no symlinks.
1615 *
1616 * CAVEAT: This function must not be used for other purposes than container
1617 * setup before executing the container's init
1618 */
1619 static int open_if_safe(int dirfd, const char *nextpath)
1620 {
1621 int newfd = openat(dirfd, nextpath, O_RDONLY | O_NOFOLLOW);
1622 if (newfd >= 0) /* Was not a symlink, all good. */
1623 return newfd;
1624
1625 if (errno == ELOOP)
1626 return newfd;
1627
1628 if (errno == EPERM || errno == EACCES) {
1629 /* We're not root (cause we got EPERM) so try opening with
1630 * O_PATH.
1631 */
1632 newfd = openat(dirfd, nextpath, O_PATH | O_NOFOLLOW);
1633 if (newfd >= 0) {
1634 /* O_PATH will return an fd for symlinks. We know
1635 * nextpath wasn't a symlink at last openat, so if fd is
1636 * now a link, then something * fishy is going on.
1637 */
1638 int ret = check_symlink(newfd);
1639 if (ret < 0) {
1640 close(newfd);
1641 newfd = ret;
1642 }
1643 }
1644 }
1645
1646 return newfd;
1647 }
1648
1649 /*
1650 * Open a path intending for mounting, ensuring that the final path
1651 * is inside the container's rootfs.
1652 *
1653 * CAVEAT: This function must not be used for other purposes than container
1654 * setup before executing the container's init
1655 *
1656 * @target: path to be opened
1657 * @prefix_skip: a part of @target in which to ignore symbolic links. This
1658 * would be the container's rootfs.
1659 *
1660 * Return an open fd for the path, or <0 on error.
1661 */
1662 static int open_without_symlink(const char *target, const char *prefix_skip)
1663 {
1664 int curlen = 0, dirfd, fulllen, i;
1665 char *dup = NULL;
1666
1667 fulllen = strlen(target);
1668
1669 /* make sure prefix-skip makes sense */
1670 if (prefix_skip && strlen(prefix_skip) > 0) {
1671 curlen = strlen(prefix_skip);
1672 if (!is_subdir(target, prefix_skip, curlen)) {
1673 ERROR("WHOA there - target '%s' didn't start with prefix '%s'",
1674 target, prefix_skip);
1675 return -EINVAL;
1676 }
1677
1678 /*
1679 * get_nextpath() expects the curlen argument to be
1680 * on a (turned into \0) / or before it, so decrement
1681 * curlen to make sure that happens
1682 */
1683 if (curlen)
1684 curlen--;
1685 } else {
1686 prefix_skip = "/";
1687 curlen = 0;
1688 }
1689
1690 /* Make a copy of target which we can hack up, and tokenize it */
1691 if ((dup = strdup(target)) == NULL) {
1692 SYSERROR("Out of memory checking for symbolic link");
1693 return -ENOMEM;
1694 }
1695
1696 for (i = 0; i < fulllen; i++) {
1697 if (dup[i] == '/')
1698 dup[i] = '\0';
1699 }
1700
1701 dirfd = open(prefix_skip, O_RDONLY);
1702 if (dirfd < 0)
1703 goto out;
1704
1705 while (1) {
1706 int newfd, saved_errno;
1707 char *nextpath;
1708
1709 if ((nextpath = get_nextpath(dup, &curlen, fulllen)) == NULL)
1710 goto out;
1711
1712 newfd = open_if_safe(dirfd, nextpath);
1713 saved_errno = errno;
1714 close(dirfd);
1715
1716 dirfd = newfd;
1717 if (newfd < 0) {
1718 errno = saved_errno;
1719 if (errno == ELOOP)
1720 SYSERROR("%s in %s was a symbolic link!", nextpath, target);
1721
1722 goto out;
1723 }
1724 }
1725
1726 out:
1727 free(dup);
1728 return dirfd;
1729 }
1730
1731 /*
1732 * Safely mount a path into a container, ensuring that the mount target
1733 * is under the container's @rootfs. (If @rootfs is NULL, then the container
1734 * uses the host's /)
1735 *
1736 * CAVEAT: This function must not be used for other purposes than container
1737 * setup before executing the container's init
1738 */
1739 int safe_mount(const char *src, const char *dest, const char *fstype,
1740 unsigned long flags, const void *data, const char *rootfs)
1741 {
1742 int destfd, ret, saved_errno;
1743 /* Only needs enough for /proc/self/fd/<fd>. */
1744 char srcbuf[50], destbuf[50];
1745 int srcfd = -1;
1746 const char *mntsrc = src;
1747
1748 if (!rootfs)
1749 rootfs = "";
1750
1751 /* todo - allow symlinks for relative paths if 'allowsymlinks' option is passed */
1752 if (flags & MS_BIND && src && src[0] != '/') {
1753 INFO("this is a relative bind mount");
1754
1755 srcfd = open_without_symlink(src, NULL);
1756 if (srcfd < 0)
1757 return srcfd;
1758
1759 ret = snprintf(srcbuf, 50, "/proc/self/fd/%d", srcfd);
1760 if (ret < 0 || ret > 50) {
1761 close(srcfd);
1762 ERROR("Out of memory");
1763 return -EINVAL;
1764 }
1765 mntsrc = srcbuf;
1766 }
1767
1768 destfd = open_without_symlink(dest, rootfs);
1769 if (destfd < 0) {
1770 if (srcfd != -1) {
1771 saved_errno = errno;
1772 close(srcfd);
1773 errno = saved_errno;
1774 }
1775
1776 return destfd;
1777 }
1778
1779 ret = snprintf(destbuf, 50, "/proc/self/fd/%d", destfd);
1780 if (ret < 0 || ret > 50) {
1781 if (srcfd != -1)
1782 close(srcfd);
1783
1784 close(destfd);
1785 ERROR("Out of memory");
1786 return -EINVAL;
1787 }
1788
1789 ret = mount(mntsrc, destbuf, fstype, flags, data);
1790 saved_errno = errno;
1791 if (srcfd != -1)
1792 close(srcfd);
1793
1794 close(destfd);
1795 if (ret < 0) {
1796 errno = saved_errno;
1797 SYSERROR("Failed to mount %s onto %s", src ? src : "(null)", dest);
1798 return ret;
1799 }
1800
1801 return 0;
1802 }
1803
1804 /*
1805 * Mount a proc under @rootfs if proc self points to a pid other than
1806 * my own. This is needed to have a known-good proc mount for setting
1807 * up LSMs both at container startup and attach.
1808 *
1809 * @rootfs : the rootfs where proc should be mounted
1810 *
1811 * Returns < 0 on failure, 0 if the correct proc was already mounted
1812 * and 1 if a new proc was mounted.
1813 *
1814 * NOTE: not to be called from inside the container namespace!
1815 */
1816 int lxc_mount_proc_if_needed(const char *rootfs)
1817 {
1818 char path[MAXPATHLEN];
1819 int link_to_pid, linklen, mypid, ret;
1820 char link[LXC_NUMSTRLEN64] = {0};
1821
1822 ret = snprintf(path, MAXPATHLEN, "%s/proc/self", rootfs);
1823 if (ret < 0 || ret >= MAXPATHLEN) {
1824 SYSERROR("proc path name too long");
1825 return -1;
1826 }
1827
1828 linklen = readlink(path, link, LXC_NUMSTRLEN64);
1829
1830 ret = snprintf(path, MAXPATHLEN, "%s/proc", rootfs);
1831 if (ret < 0 || ret >= MAXPATHLEN) {
1832 SYSERROR("proc path name too long");
1833 return -1;
1834 }
1835
1836 /* /proc not mounted */
1837 if (linklen < 0) {
1838 if (mkdir(path, 0755) && errno != EEXIST)
1839 return -1;
1840
1841 goto domount;
1842 } else if (linklen >= LXC_NUMSTRLEN64) {
1843 link[linklen - 1] = '\0';
1844 ERROR("readlink returned truncated content: \"%s\"", link);
1845 return -1;
1846 }
1847
1848 mypid = lxc_raw_getpid();
1849 INFO("I am %d, /proc/self points to \"%s\"", mypid, link);
1850
1851 if (lxc_safe_int(link, &link_to_pid) < 0)
1852 return -1;
1853
1854 /* correct procfs is already mounted */
1855 if (link_to_pid == mypid)
1856 return 0;
1857
1858 ret = umount2(path, MNT_DETACH);
1859 if (ret < 0)
1860 WARN("failed to umount \"%s\" with MNT_DETACH", path);
1861
1862 domount:
1863 /* rootfs is NULL */
1864 if (!strcmp(rootfs, ""))
1865 ret = mount("proc", path, "proc", 0, NULL);
1866 else
1867 ret = safe_mount("proc", path, "proc", 0, NULL, rootfs);
1868 if (ret < 0)
1869 return -1;
1870
1871 INFO("mounted /proc in container for security transition");
1872 return 1;
1873 }
1874
1875 int open_devnull(void)
1876 {
1877 int fd = open("/dev/null", O_RDWR);
1878
1879 if (fd < 0)
1880 SYSERROR("Can't open /dev/null");
1881
1882 return fd;
1883 }
1884
1885 int set_stdfds(int fd)
1886 {
1887 int ret;
1888
1889 if (fd < 0)
1890 return -1;
1891
1892 ret = dup2(fd, STDIN_FILENO);
1893 if (ret < 0)
1894 return -1;
1895
1896 ret = dup2(fd, STDOUT_FILENO);
1897 if (ret < 0)
1898 return -1;
1899
1900 ret = dup2(fd, STDERR_FILENO);
1901 if (ret < 0)
1902 return -1;
1903
1904 return 0;
1905 }
1906
1907 int null_stdfds(void)
1908 {
1909 int ret = -1;
1910 int fd;
1911
1912 fd = open_devnull();
1913 if (fd >= 0) {
1914 ret = set_stdfds(fd);
1915 close(fd);
1916 }
1917
1918 return ret;
1919 }
1920
1921 /*
1922 * Return the number of lines in file @fn, or -1 on error
1923 */
1924 int lxc_count_file_lines(const char *fn)
1925 {
1926 FILE *f;
1927 char *line = NULL;
1928 size_t sz = 0;
1929 int n = 0;
1930
1931 f = fopen_cloexec(fn, "r");
1932 if (!f)
1933 return -1;
1934
1935 while (getline(&line, &sz, f) != -1) {
1936 n++;
1937 }
1938
1939 free(line);
1940 fclose(f);
1941 return n;
1942 }
1943
1944 /* Check whether a signal is blocked by a process. */
1945 /* /proc/pid-to-str/status\0 = (5 + 21 + 7 + 1) */
1946 #define __PROC_STATUS_LEN (6 + (LXC_NUMSTRLEN64) + 7 + 1)
1947 bool task_blocks_signal(pid_t pid, int signal)
1948 {
1949 int ret;
1950 char status[__PROC_STATUS_LEN];
1951 FILE *f;
1952 uint64_t sigblk = 0, one = 1;
1953 size_t n = 0;
1954 bool bret = false;
1955 char *line = NULL;
1956
1957 ret = snprintf(status, __PROC_STATUS_LEN, "/proc/%d/status", pid);
1958 if (ret < 0 || ret >= __PROC_STATUS_LEN)
1959 return bret;
1960
1961 f = fopen(status, "r");
1962 if (!f)
1963 return bret;
1964
1965 while (getline(&line, &n, f) != -1) {
1966 char *numstr;
1967
1968 if (strncmp(line, "SigBlk:", 7))
1969 continue;
1970
1971 numstr = lxc_trim_whitespace_in_place(line + 7);
1972 ret = lxc_safe_uint64(numstr, &sigblk, 16);
1973 if (ret < 0)
1974 goto out;
1975
1976 break;
1977 }
1978
1979 if (sigblk & (one << (signal - 1)))
1980 bret = true;
1981
1982 out:
1983 free(line);
1984 fclose(f);
1985 return bret;
1986 }
1987
1988 static int lxc_append_null_to_list(void ***list)
1989 {
1990 int newentry = 0;
1991 void **tmp;
1992
1993 if (*list)
1994 for (; (*list)[newentry]; newentry++) {
1995 ;
1996 }
1997
1998 tmp = realloc(*list, (newentry + 2) * sizeof(void **));
1999 if (!tmp)
2000 return -1;
2001
2002 *list = tmp;
2003 (*list)[newentry + 1] = NULL;
2004
2005 return newentry;
2006 }
2007
2008 int lxc_append_string(char ***list, char *entry)
2009 {
2010 char *copy;
2011 int newentry;
2012
2013 newentry = lxc_append_null_to_list((void ***)list);
2014 if (newentry < 0)
2015 return -1;
2016
2017 copy = strdup(entry);
2018 if (!copy)
2019 return -1;
2020
2021 (*list)[newentry] = copy;
2022
2023 return 0;
2024 }
2025
2026 int lxc_preserve_ns(const int pid, const char *ns)
2027 {
2028 int ret;
2029 /* 5 /proc + 21 /int_as_str + 3 /ns + 20 /NS_NAME + 1 \0 */
2030 #define __NS_PATH_LEN 50
2031 char path[__NS_PATH_LEN];
2032
2033 /* This way we can use this function to also check whether namespaces
2034 * are supported by the kernel by passing in the NULL or the empty
2035 * string.
2036 */
2037 ret = snprintf(path, __NS_PATH_LEN, "/proc/%d/ns%s%s", pid,
2038 !ns || strcmp(ns, "") == 0 ? "" : "/",
2039 !ns || strcmp(ns, "") == 0 ? "" : ns);
2040 if (ret < 0 || (size_t)ret >= __NS_PATH_LEN) {
2041 errno = EFBIG;
2042 return -1;
2043 }
2044
2045 return open(path, O_RDONLY | O_CLOEXEC);
2046 }
2047
2048 int lxc_safe_uint(const char *numstr, unsigned int *converted)
2049 {
2050 char *err = NULL;
2051 unsigned long int uli;
2052
2053 while (isspace(*numstr))
2054 numstr++;
2055
2056 if (*numstr == '-')
2057 return -EINVAL;
2058
2059 errno = 0;
2060 uli = strtoul(numstr, &err, 0);
2061 if (errno == ERANGE && uli == ULONG_MAX)
2062 return -ERANGE;
2063
2064 if (err == numstr || *err != '\0')
2065 return -EINVAL;
2066
2067 if (uli > UINT_MAX)
2068 return -ERANGE;
2069
2070 *converted = (unsigned int)uli;
2071 return 0;
2072 }
2073
2074 int lxc_safe_ulong(const char *numstr, unsigned long *converted)
2075 {
2076 char *err = NULL;
2077 unsigned long int uli;
2078
2079 while (isspace(*numstr))
2080 numstr++;
2081
2082 if (*numstr == '-')
2083 return -EINVAL;
2084
2085 errno = 0;
2086 uli = strtoul(numstr, &err, 0);
2087 if (errno == ERANGE && uli == ULONG_MAX)
2088 return -ERANGE;
2089
2090 if (err == numstr || *err != '\0')
2091 return -EINVAL;
2092
2093 *converted = uli;
2094 return 0;
2095 }
2096
2097 int lxc_safe_uint64(const char *numstr, uint64_t *converted, int base)
2098 {
2099 char *err = NULL;
2100 uint64_t u;
2101
2102 while (isspace(*numstr))
2103 numstr++;
2104
2105 if (*numstr == '-')
2106 return -EINVAL;
2107
2108 errno = 0;
2109 u = strtoull(numstr, &err, base);
2110 if (errno == ERANGE && u == ULLONG_MAX)
2111 return -ERANGE;
2112
2113 if (err == numstr || *err != '\0')
2114 return -EINVAL;
2115
2116 *converted = u;
2117 return 0;
2118 }
2119
2120 int lxc_safe_int(const char *numstr, int *converted)
2121 {
2122 char *err = NULL;
2123 signed long int sli;
2124
2125 errno = 0;
2126 sli = strtol(numstr, &err, 0);
2127 if (errno == ERANGE && (sli == LONG_MAX || sli == LONG_MIN))
2128 return -ERANGE;
2129
2130 if (errno != 0 && sli == 0)
2131 return -EINVAL;
2132
2133 if (err == numstr || *err != '\0')
2134 return -EINVAL;
2135
2136 if (sli > INT_MAX || sli < INT_MIN)
2137 return -ERANGE;
2138
2139 *converted = (int)sli;
2140 return 0;
2141 }
2142
2143 int lxc_safe_long(const char *numstr, long int *converted)
2144 {
2145 char *err = NULL;
2146 signed long int sli;
2147
2148 errno = 0;
2149 sli = strtol(numstr, &err, 0);
2150 if (errno == ERANGE && (sli == LONG_MAX || sli == LONG_MIN))
2151 return -ERANGE;
2152
2153 if (errno != 0 && sli == 0)
2154 return -EINVAL;
2155
2156 if (err == numstr || *err != '\0')
2157 return -EINVAL;
2158
2159 *converted = sli;
2160 return 0;
2161 }
2162
2163 int lxc_safe_long_long(const char *numstr, long long int *converted)
2164 {
2165 char *err = NULL;
2166 signed long long int sli;
2167
2168 errno = 0;
2169 sli = strtoll(numstr, &err, 0);
2170 if (errno == ERANGE && (sli == LLONG_MAX || sli == LLONG_MIN))
2171 return -ERANGE;
2172
2173 if (errno != 0 && sli == 0)
2174 return -EINVAL;
2175
2176 if (err == numstr || *err != '\0')
2177 return -EINVAL;
2178
2179 *converted = sli;
2180 return 0;
2181 }
2182
2183 int lxc_switch_uid_gid(uid_t uid, gid_t gid)
2184 {
2185 if (setgid(gid) < 0) {
2186 SYSERROR("Failed to switch to gid %d.", gid);
2187 return -errno;
2188 }
2189 NOTICE("Switched to gid %d.", gid);
2190
2191 if (setuid(uid) < 0) {
2192 SYSERROR("Failed to switch to uid %d.", uid);
2193 return -errno;
2194 }
2195 NOTICE("Switched to uid %d.", uid);
2196
2197 return 0;
2198 }
2199
2200 /* Simple covenience function which enables uniform logging. */
2201 int lxc_setgroups(int size, gid_t list[])
2202 {
2203 if (setgroups(size, list) < 0) {
2204 SYSERROR("Failed to setgroups().");
2205 return -errno;
2206 }
2207 NOTICE("Dropped additional groups.");
2208
2209 return 0;
2210 }
2211
2212 static int lxc_get_unused_loop_dev_legacy(char *loop_name)
2213 {
2214 struct dirent *dp;
2215 struct loop_info64 lo64;
2216 DIR *dir;
2217 int dfd = -1, fd = -1, ret = -1;
2218
2219 dir = opendir("/dev");
2220 if (!dir)
2221 return -1;
2222
2223 while ((dp = readdir(dir))) {
2224 if (strncmp(dp->d_name, "loop", 4) != 0)
2225 continue;
2226
2227 dfd = dirfd(dir);
2228 if (dfd < 0)
2229 continue;
2230
2231 fd = openat(dfd, dp->d_name, O_RDWR);
2232 if (fd < 0)
2233 continue;
2234
2235 ret = ioctl(fd, LOOP_GET_STATUS64, &lo64);
2236 if (ret < 0) {
2237 if (ioctl(fd, LOOP_GET_STATUS64, &lo64) == 0 ||
2238 errno != ENXIO) {
2239 close(fd);
2240 fd = -1;
2241 continue;
2242 }
2243 }
2244
2245 ret = snprintf(loop_name, LO_NAME_SIZE, "/dev/%s", dp->d_name);
2246 if (ret < 0 || ret >= LO_NAME_SIZE) {
2247 close(fd);
2248 fd = -1;
2249 continue;
2250 }
2251
2252 break;
2253 }
2254
2255 closedir(dir);
2256
2257 if (fd < 0)
2258 return -1;
2259
2260 return fd;
2261 }
2262
2263 static int lxc_get_unused_loop_dev(char *name_loop)
2264 {
2265 int loop_nr, ret;
2266 int fd_ctl = -1, fd_tmp = -1;
2267
2268 fd_ctl = open("/dev/loop-control", O_RDWR | O_CLOEXEC);
2269 if (fd_ctl < 0)
2270 return -ENODEV;
2271
2272 loop_nr = ioctl(fd_ctl, LOOP_CTL_GET_FREE);
2273 if (loop_nr < 0)
2274 goto on_error;
2275
2276 ret = snprintf(name_loop, LO_NAME_SIZE, "/dev/loop%d", loop_nr);
2277 if (ret < 0 || ret >= LO_NAME_SIZE)
2278 goto on_error;
2279
2280 fd_tmp = open(name_loop, O_RDWR | O_CLOEXEC);
2281 if (fd_tmp < 0)
2282 goto on_error;
2283
2284 on_error:
2285 close(fd_ctl);
2286 return fd_tmp;
2287 }
2288
2289 int lxc_prepare_loop_dev(const char *source, char *loop_dev, int flags)
2290 {
2291 int ret;
2292 struct loop_info64 lo64;
2293 int fd_img = -1, fret = -1, fd_loop = -1;
2294
2295 fd_loop = lxc_get_unused_loop_dev(loop_dev);
2296 if (fd_loop < 0) {
2297 if (fd_loop == -ENODEV)
2298 fd_loop = lxc_get_unused_loop_dev_legacy(loop_dev);
2299 else
2300 goto on_error;
2301 }
2302
2303 fd_img = open(source, O_RDWR | O_CLOEXEC);
2304 if (fd_img < 0)
2305 goto on_error;
2306
2307 ret = ioctl(fd_loop, LOOP_SET_FD, fd_img);
2308 if (ret < 0)
2309 goto on_error;
2310
2311 memset(&lo64, 0, sizeof(lo64));
2312 lo64.lo_flags = flags;
2313
2314 ret = ioctl(fd_loop, LOOP_SET_STATUS64, &lo64);
2315 if (ret < 0)
2316 goto on_error;
2317
2318 fret = 0;
2319
2320 on_error:
2321 if (fd_img >= 0)
2322 close(fd_img);
2323
2324 if (fret < 0 && fd_loop >= 0) {
2325 close(fd_loop);
2326 fd_loop = -1;
2327 }
2328
2329 return fd_loop;
2330 }
2331
2332 int lxc_unstack_mountpoint(const char *path, bool lazy)
2333 {
2334 int ret;
2335 int umounts = 0;
2336
2337 pop_stack:
2338 ret = umount2(path, lazy ? MNT_DETACH : 0);
2339 if (ret < 0) {
2340 /* We consider anything else than EINVAL deadly to prevent going
2341 * into an infinite loop. (The other alternative is constantly
2342 * parsing /proc/self/mountinfo which is yucky and probably
2343 * racy.)
2344 */
2345 if (errno != EINVAL)
2346 return -errno;
2347 } else {
2348 /* Just stop counting when this happens. That'd just be so
2349 * stupid that we won't even bother trying to report back the
2350 * correct value anymore.
2351 */
2352 if (umounts != INT_MAX)
2353 umounts++;
2354
2355 /* We succeeded in umounting. Make sure that there's no other
2356 * mountpoint stacked underneath.
2357 */
2358 goto pop_stack;
2359 }
2360
2361 return umounts;
2362 }
2363
2364 int run_command(char *buf, size_t buf_size, int (*child_fn)(void *), void *args)
2365 {
2366 pid_t child;
2367 int ret, fret, pipefd[2];
2368 ssize_t bytes;
2369
2370 /* Make sure our callers do not receive uninitialized memory. */
2371 if (buf_size > 0 && buf)
2372 buf[0] = '\0';
2373
2374 if (pipe(pipefd) < 0) {
2375 SYSERROR("failed to create pipe");
2376 return -1;
2377 }
2378
2379 child = lxc_raw_clone(0);
2380 if (child < 0) {
2381 close(pipefd[0]);
2382 close(pipefd[1]);
2383 SYSERROR("failed to create new process");
2384 return -1;
2385 }
2386
2387 if (child == 0) {
2388 /* Close the read-end of the pipe. */
2389 close(pipefd[0]);
2390
2391 /* Redirect std{err,out} to write-end of the
2392 * pipe.
2393 */
2394 ret = dup2(pipefd[1], STDOUT_FILENO);
2395 if (ret >= 0)
2396 ret = dup2(pipefd[1], STDERR_FILENO);
2397
2398 /* Close the write-end of the pipe. */
2399 close(pipefd[1]);
2400
2401 if (ret < 0) {
2402 SYSERROR("failed to duplicate std{err,out} file descriptor");
2403 _exit(EXIT_FAILURE);
2404 }
2405
2406 /* Does not return. */
2407 child_fn(args);
2408 ERROR("failed to exec command");
2409 _exit(EXIT_FAILURE);
2410 }
2411
2412 /* close the write-end of the pipe */
2413 close(pipefd[1]);
2414
2415 if (buf && buf_size > 0) {
2416 bytes = read(pipefd[0], buf, buf_size - 1);
2417 if (bytes > 0)
2418 buf[bytes - 1] = '\0';
2419 }
2420
2421 fret = wait_for_pid(child);
2422 /* close the read-end of the pipe */
2423 close(pipefd[0]);
2424
2425 return fret;
2426 }
2427
2428 char *must_concat(const char *first, ...)
2429 {
2430 va_list args;
2431 char *cur, *dest;
2432 size_t cur_len, it_len;
2433
2434 dest = must_copy_string(first);
2435 cur_len = it_len = strlen(first);
2436
2437 va_start(args, first);
2438 while ((cur = va_arg(args, char *)) != NULL) {
2439 it_len = strlen(cur);
2440
2441 dest = must_realloc(dest, cur_len + it_len + 1);
2442
2443 (void)memcpy(dest + cur_len, cur, it_len);
2444 cur_len += it_len;
2445 }
2446 va_end(args);
2447
2448 dest[cur_len] = 0;
2449 return dest;
2450 }
2451
2452 char *must_make_path(const char *first, ...)
2453 {
2454 va_list args;
2455 char *cur, *dest;
2456 size_t full_len = strlen(first);
2457 size_t buf_len;
2458
2459 dest = must_copy_string(first);
2460
2461 va_start(args, first);
2462 while ((cur = va_arg(args, char *)) != NULL) {
2463 full_len += strlen(cur);
2464 if (cur[0] != '/')
2465 full_len++;
2466
2467 buf_len = full_len + 1;
2468 dest = must_realloc(dest, buf_len);
2469
2470 if (cur[0] != '/')
2471 (void)strlcat(dest, "/", buf_len);
2472 (void)strlcat(dest, cur, buf_len);
2473 }
2474 va_end(args);
2475
2476 return dest;
2477 }
2478
2479 char *must_append_path(char *first, ...)
2480 {
2481 char *cur;
2482 size_t full_len;
2483 va_list args;
2484 char *dest = first;
2485 size_t buf_len;
2486
2487 full_len = strlen(first);
2488 va_start(args, first);
2489 while ((cur = va_arg(args, char *)) != NULL) {
2490 full_len += strlen(cur);
2491 if (cur[0] != '/')
2492 full_len++;
2493
2494 buf_len = full_len + 1;
2495 dest = must_realloc(dest, buf_len);
2496
2497 if (cur[0] != '/')
2498 (void)strlcat(dest, "/", buf_len);
2499 (void)strlcat(dest, cur, buf_len);
2500 }
2501 va_end(args);
2502
2503 return dest;
2504 }
2505
2506 char *must_copy_string(const char *entry)
2507 {
2508 char *ret;
2509
2510 if (!entry)
2511 return NULL;
2512
2513 do {
2514 ret = strdup(entry);
2515 } while (!ret);
2516
2517 return ret;
2518 }
2519
2520 void *must_realloc(void *orig, size_t sz)
2521 {
2522 void *ret;
2523
2524 do {
2525 ret = realloc(orig, sz);
2526 } while (!ret);
2527
2528 return ret;
2529 }
2530
2531 bool is_fs_type(const struct statfs *fs, fs_type_magic magic_val)
2532 {
2533 return (fs->f_type == (fs_type_magic)magic_val);
2534 }
2535
2536 bool has_fs_type(const char *path, fs_type_magic magic_val)
2537 {
2538 bool has_type;
2539 int ret;
2540 struct statfs sb;
2541
2542 ret = statfs(path, &sb);
2543 if (ret < 0)
2544 return false;
2545
2546 has_type = is_fs_type(&sb, magic_val);
2547 if (!has_type && magic_val == RAMFS_MAGIC)
2548 WARN("When the ramfs it a tmpfs statfs() might report tmpfs");
2549
2550 return has_type;
2551 }
2552
2553 bool lxc_nic_exists(char *nic)
2554 {
2555 #define __LXC_SYS_CLASS_NET_LEN 15 + IFNAMSIZ + 1
2556 char path[__LXC_SYS_CLASS_NET_LEN];
2557 int ret;
2558 struct stat sb;
2559
2560 if (!strcmp(nic, "none"))
2561 return true;
2562
2563 ret = snprintf(path, __LXC_SYS_CLASS_NET_LEN, "/sys/class/net/%s", nic);
2564 if (ret < 0 || (size_t)ret >= __LXC_SYS_CLASS_NET_LEN)
2565 return false;
2566
2567 ret = stat(path, &sb);
2568 if (ret < 0)
2569 return false;
2570
2571 return true;
2572 }
2573
2574 int lxc_make_tmpfile(char *template, bool rm)
2575 {
2576 int fd, ret;
2577 mode_t msk;
2578
2579 msk = umask(0022);
2580 fd = mkstemp(template);
2581 umask(msk);
2582 if (fd < 0)
2583 return -1;
2584
2585 if (!rm)
2586 return fd;
2587
2588 ret = unlink(template);
2589 if (ret < 0) {
2590 close(fd);
2591 return -1;
2592 }
2593
2594 return fd;
2595 }
2596
2597 int parse_byte_size_string(const char *s, int64_t *converted)
2598 {
2599 int ret, suffix_len;
2600 long long int conv;
2601 int64_t mltpl, overflow;
2602 char *end;
2603 char dup[LXC_NUMSTRLEN64 + 2];
2604 char suffix[3] = {0};
2605
2606 if (!s || !strcmp(s, ""))
2607 return -EINVAL;
2608
2609 end = stpncpy(dup, s, sizeof(dup) - 1);
2610 if (*end != '\0')
2611 return -EINVAL;
2612
2613 if (isdigit(*(end - 1)))
2614 suffix_len = 0;
2615 else if (isalpha(*(end - 1)))
2616 suffix_len = 1;
2617 else
2618 return -EINVAL;
2619
2620 if (suffix_len > 0 && (end - 2) == dup && !isdigit(*(end - 2)))
2621 return -EINVAL;
2622
2623 if (suffix_len > 0 && isalpha(*(end - 2)))
2624 suffix_len++;
2625
2626 if (suffix_len > 0) {
2627 memcpy(suffix, end - suffix_len, suffix_len);
2628 *(suffix + suffix_len) = '\0';
2629 *(end - suffix_len) = '\0';
2630 }
2631 dup[lxc_char_right_gc(dup, strlen(dup))] = '\0';
2632
2633 ret = lxc_safe_long_long(dup, &conv);
2634 if (ret < 0)
2635 return -ret;
2636
2637 if (suffix_len != 2) {
2638 *converted = conv;
2639 return 0;
2640 }
2641
2642 if (strcasecmp(suffix, "KB") == 0)
2643 mltpl = 1024;
2644 else if (strcasecmp(suffix, "MB") == 0)
2645 mltpl = 1024 * 1024;
2646 else if (strcasecmp(suffix, "GB") == 0)
2647 mltpl = 1024 * 1024 * 1024;
2648 else
2649 return -EINVAL;
2650
2651 overflow = conv * mltpl;
2652 if (conv != 0 && (overflow / conv) != mltpl)
2653 return -ERANGE;
2654
2655 *converted = overflow;
2656 return 0;
2657 }
2658
2659 uint64_t lxc_find_next_power2(uint64_t n)
2660 {
2661 /* 0 is not valid input. We return 0 to the caller since 0 is not a
2662 * valid power of two.
2663 */
2664 if (n == 0)
2665 return 0;
2666
2667 if (!(n & (n - 1)))
2668 return n;
2669
2670 while (n & (n - 1))
2671 n = n & (n - 1);
2672
2673 n = n << 1;
2674 return n;
2675 }
2676
2677 int lxc_set_death_signal(int signal)
2678 {
2679 int ret;
2680 pid_t ppid;
2681
2682 ret = prctl(PR_SET_PDEATHSIG, signal, 0, 0, 0);
2683
2684 /* Check whether we have been orphaned. */
2685 ppid = (pid_t)syscall(SYS_getppid);
2686 if (ppid == 1) {
2687 pid_t self;
2688
2689 self = lxc_raw_getpid();
2690 ret = kill(self, SIGKILL);
2691 if (ret < 0)
2692 return -1;
2693 }
2694
2695 if (ret < 0) {
2696 SYSERROR("Failed to set PR_SET_PDEATHSIG to %d", signal);
2697 return -1;
2698 }
2699
2700 return 0;
2701 }
2702
2703 void remove_trailing_newlines(char *l)
2704 {
2705 char *p = l;
2706
2707 while (*p)
2708 p++;
2709
2710 while (--p >= l && *p == '\n')
2711 *p = '\0';
2712 }
2713
2714 int fd_cloexec(int fd, bool cloexec)
2715 {
2716 int oflags, nflags;
2717
2718 oflags = fcntl(fd, F_GETFD, 0);
2719 if (oflags < 0)
2720 return -errno;
2721
2722 if (cloexec)
2723 nflags = oflags | FD_CLOEXEC;
2724 else
2725 nflags = oflags & ~FD_CLOEXEC;
2726
2727 if (nflags == oflags)
2728 return 0;
2729
2730 if (fcntl(fd, F_SETFD, nflags) < 0)
2731 return -errno;
2732
2733 return 0;
2734 }
2735
2736 int recursive_destroy(char *dirname)
2737 {
2738 int ret;
2739 struct dirent *direntp;
2740 DIR *dir;
2741 int r = 0;
2742
2743 dir = opendir(dirname);
2744 if (!dir)
2745 return -1;
2746
2747 while ((direntp = readdir(dir))) {
2748 char *pathname;
2749 struct stat mystat;
2750
2751 if (!strcmp(direntp->d_name, ".") ||
2752 !strcmp(direntp->d_name, ".."))
2753 continue;
2754
2755 pathname = must_make_path(dirname, direntp->d_name, NULL);
2756
2757 ret = lstat(pathname, &mystat);
2758 if (ret < 0) {
2759 if (!r)
2760 WARN("Failed to stat \"%s\"", pathname);
2761
2762 r = -1;
2763 goto next;
2764 }
2765
2766 if (!S_ISDIR(mystat.st_mode))
2767 goto next;
2768
2769 ret = recursive_destroy(pathname);
2770 if (ret < 0)
2771 r = -1;
2772
2773 next:
2774 free(pathname);
2775 }
2776
2777 ret = rmdir(dirname);
2778 if (ret < 0) {
2779 if (!r)
2780 SYSWARN("Failed to delete \"%s\"", dirname);
2781
2782 r = -1;
2783 }
2784
2785 ret = closedir(dir);
2786 if (ret < 0) {
2787 if (!r)
2788 SYSWARN("Failed to delete \"%s\"", dirname);
2789
2790 r = -1;
2791 }
2792
2793 return r;
2794 }