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