]>
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> |
b467714b | 34 | #include <pthread.h> |
d983b93c | 35 | #include <stddef.h> |
a1e5280d CB |
36 | #include <stdio.h> |
37 | #include <stdlib.h> | |
61a1d519 | 38 | #include <string.h> |
981f6029 | 39 | #include <unistd.h> |
e3642c43 | 40 | #include <sys/mman.h> |
6e4bb2e0 | 41 | #include <sys/mount.h> |
a1e5280d CB |
42 | #include <sys/param.h> |
43 | #include <sys/prctl.h> | |
44 | #include <sys/stat.h> | |
9be53773 SH |
45 | #include <sys/types.h> |
46 | #include <sys/wait.h> | |
e3642c43 DL |
47 | |
48 | #include "log.h" | |
025ed0f3 | 49 | #include "lxclock.h" |
51d0854c | 50 | #include "namespace.h" |
e3db0162 | 51 | #include "parse.h" |
981f6029 | 52 | #include "utils.h" |
e3642c43 | 53 | |
43f984ea DJ |
54 | #ifndef HAVE_STRLCPY |
55 | #include "include/strlcpy.h" | |
56 | #endif | |
57 | ||
bd583214 DJ |
58 | #ifndef HAVE_STRLCAT |
59 | #include "include/strlcat.h" | |
60 | #endif | |
61 | ||
4928c718 SG |
62 | #ifndef O_PATH |
63 | #define O_PATH 010000000 | |
64 | #endif | |
65 | ||
66 | #ifndef O_NOFOLLOW | |
67 | #define O_NOFOLLOW 00400000 | |
68 | #endif | |
69 | ||
ac2cecc4 | 70 | lxc_log_define(utils, lxc); |
e3642c43 | 71 | |
4295c5de SH |
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 | ||
41dc7155 | 77 | static int _recursive_rmdir(const char *dirname, dev_t pdev, |
0cc417b2 | 78 | const char *exclude, int level, bool onedev) |
60bf62d4 | 79 | { |
74f96976 | 80 | struct dirent *direntp; |
60bf62d4 SH |
81 | DIR *dir; |
82 | int ret, failed=0; | |
83 | char pathname[MAXPATHLEN]; | |
18aa217b | 84 | bool hadexclude = false; |
60bf62d4 SH |
85 | |
86 | dir = opendir(dirname); | |
87 | if (!dir) { | |
b103ceac | 88 | ERROR("failed to open %s", dirname); |
4355ab5f | 89 | return -1; |
60bf62d4 SH |
90 | } |
91 | ||
74f96976 | 92 | while ((direntp = readdir(dir))) { |
60bf62d4 SH |
93 | struct stat mystat; |
94 | int rc; | |
95 | ||
60bf62d4 SH |
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 | } | |
18aa217b SH |
106 | |
107 | if (!level && exclude && !strcmp(direntp->d_name, exclude)) { | |
108 | ret = rmdir(pathname); | |
109 | if (ret < 0) { | |
110 | switch(errno) { | |
111 | case ENOTEMPTY: | |
0cc417b2 | 112 | INFO("Not deleting snapshot %s", pathname); |
18aa217b SH |
113 | hadexclude = true; |
114 | break; | |
115 | case ENOTDIR: | |
116 | ret = unlink(pathname); | |
117 | if (ret) | |
b103ceac | 118 | INFO("Failed to remove %s", pathname); |
18aa217b SH |
119 | break; |
120 | default: | |
b103ceac | 121 | SYSERROR("Failed to rmdir %s", pathname); |
18aa217b SH |
122 | failed = 1; |
123 | break; | |
124 | } | |
125 | } | |
126 | continue; | |
127 | } | |
128 | ||
60bf62d4 SH |
129 | ret = lstat(pathname, &mystat); |
130 | if (ret) { | |
b103ceac | 131 | ERROR("Failed to stat %s", pathname); |
4295c5de | 132 | failed = 1; |
60bf62d4 SH |
133 | continue; |
134 | } | |
b14fc100 | 135 | |
4295c5de SH |
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); | |
60bf62d4 | 141 | continue; |
4295c5de | 142 | } |
b14fc100 | 143 | |
60bf62d4 | 144 | if (S_ISDIR(mystat.st_mode)) { |
0cc417b2 | 145 | if (_recursive_rmdir(pathname, pdev, exclude, level+1, onedev) < 0) |
60bf62d4 SH |
146 | failed=1; |
147 | } else { | |
148 | if (unlink(pathname) < 0) { | |
b103ceac | 149 | SYSERROR("Failed to delete %s", pathname); |
60bf62d4 SH |
150 | failed=1; |
151 | } | |
152 | } | |
153 | } | |
154 | ||
4295c5de | 155 | if (rmdir(dirname) < 0 && !btrfs_try_remove_subvol(dirname) && !hadexclude) { |
b103ceac | 156 | ERROR("Failed to delete %s", dirname); |
4295c5de | 157 | failed=1; |
60bf62d4 SH |
158 | } |
159 | ||
025ed0f3 | 160 | ret = closedir(dir); |
025ed0f3 | 161 | if (ret) { |
b103ceac | 162 | ERROR("Failed to close directory %s", dirname); |
60bf62d4 SH |
163 | failed=1; |
164 | } | |
165 | ||
4355ab5f | 166 | return failed ? -1 : 0; |
60bf62d4 SH |
167 | } |
168 | ||
29a11a7f CB |
169 | /* We have two different magic values for overlayfs, yay. */ |
170 | #ifndef OVERLAYFS_SUPER_MAGIC | |
0cc417b2 | 171 | #define OVERLAYFS_SUPER_MAGIC 0x794c764f |
29a11a7f CB |
172 | #endif |
173 | ||
174 | #ifndef OVERLAY_SUPER_MAGIC | |
0cc417b2 | 175 | #define OVERLAY_SUPER_MAGIC 0x794c7630 |
29a11a7f CB |
176 | #endif |
177 | ||
178 | /* In overlayfs, st_dev is unreliable. So on overlayfs we don't do the | |
179 | * lxc_rmdir_onedev() | |
0cc417b2 SH |
180 | */ |
181 | static bool is_native_overlayfs(const char *path) | |
182 | { | |
29a11a7f CB |
183 | if (has_fs_type(path, OVERLAY_SUPER_MAGIC) || |
184 | has_fs_type(path, OVERLAYFS_SUPER_MAGIC)) | |
0cc417b2 | 185 | return true; |
29a11a7f | 186 | |
0cc417b2 SH |
187 | return false; |
188 | } | |
189 | ||
4355ab5f | 190 | /* returns 0 on success, -1 if there were any failures */ |
41dc7155 | 191 | extern int lxc_rmdir_onedev(const char *path, const char *exclude) |
60bf62d4 SH |
192 | { |
193 | struct stat mystat; | |
0cc417b2 SH |
194 | bool onedev = true; |
195 | ||
41dc7155 | 196 | if (is_native_overlayfs(path)) |
0cc417b2 | 197 | onedev = false; |
60bf62d4 SH |
198 | |
199 | if (lstat(path, &mystat) < 0) { | |
067650d0 SH |
200 | if (errno == ENOENT) |
201 | return 0; | |
41dc7155 | 202 | |
b103ceac | 203 | ERROR("Failed to stat %s", path); |
4355ab5f | 204 | return -1; |
60bf62d4 SH |
205 | } |
206 | ||
0cc417b2 | 207 | return _recursive_rmdir(path, mystat.st_dev, exclude, 0, onedev); |
60bf62d4 SH |
208 | } |
209 | ||
9ddaf3bf | 210 | /* borrowed from iproute2 */ |
7c11d57a | 211 | extern int get_u16(unsigned short *val, const char *arg, int base) |
9ddaf3bf JHS |
212 | { |
213 | unsigned long res; | |
214 | char *ptr; | |
215 | ||
216 | if (!arg || !*arg) | |
217 | return -1; | |
218 | ||
09bbd745 | 219 | errno = 0; |
9ddaf3bf | 220 | res = strtoul(arg, &ptr, base); |
09bbd745 | 221 | if (!ptr || ptr == arg || *ptr || res > 0xFFFF || errno != 0) |
9ddaf3bf JHS |
222 | return -1; |
223 | ||
224 | *val = res; | |
225 | ||
226 | return 0; | |
227 | } | |
228 | ||
3ce74686 | 229 | extern int mkdir_p(const char *dir, mode_t mode) |
1b09f2c0 | 230 | { |
3ce74686 SH |
231 | const char *tmp = dir; |
232 | const char *orig = dir; | |
860fc865 RW |
233 | char *makeme; |
234 | ||
235 | do { | |
236 | dir = tmp + strspn(tmp, "/"); | |
237 | tmp = dir + strcspn(dir, "/"); | |
b14fc100 | 238 | |
d74325c4 | 239 | makeme = strndup(orig, dir - orig); |
860fc865 RW |
240 | if (*makeme) { |
241 | if (mkdir(makeme, mode) && errno != EEXIST) { | |
959aee9c | 242 | SYSERROR("failed to create directory '%s'", makeme); |
d74325c4 | 243 | free(makeme); |
860fc865 RW |
244 | return -1; |
245 | } | |
246 | } | |
d74325c4 | 247 | free(makeme); |
860fc865 | 248 | } while(tmp != dir); |
1b09f2c0 | 249 | |
98663823 | 250 | return 0; |
1b09f2c0 | 251 | } |
2a59a681 | 252 | |
44b9ae4b | 253 | char *get_rundir() |
9e60f51d | 254 | { |
97a696c6 SG |
255 | char *rundir; |
256 | const char *homedir; | |
9650c735 | 257 | struct stat sb; |
9e60f51d | 258 | |
b14fc100 | 259 | if (stat(RUNTIME_PATH, &sb) < 0) |
9650c735 | 260 | return NULL; |
9650c735 TA |
261 | |
262 | if (geteuid() == sb.st_uid || getegid() == sb.st_gid) { | |
c580b8d2 | 263 | rundir = strdup(RUNTIME_PATH); |
d6470e71 SG |
264 | return rundir; |
265 | } | |
97a696c6 SG |
266 | |
267 | rundir = getenv("XDG_RUNTIME_DIR"); | |
44b9ae4b SG |
268 | if (rundir) { |
269 | rundir = strdup(rundir); | |
270 | return rundir; | |
271 | } | |
97a696c6 | 272 | |
44b9ae4b SG |
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; | |
97a696c6 SG |
278 | } |
279 | ||
44b9ae4b | 280 | rundir = malloc(sizeof(char) * (17 + strlen(homedir))); |
b14fc100 | 281 | if (!rundir) |
282 | return NULL; | |
283 | ||
44b9ae4b SG |
284 | sprintf(rundir, "%s/.cache/lxc/run/", homedir); |
285 | ||
9e60f51d DE |
286 | return rundir; |
287 | } | |
288 | ||
9be53773 SH |
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) { | |
71b9b8ed | 296 | if (errno == EINTR) |
9be53773 | 297 | goto again; |
b14fc100 | 298 | |
9be53773 SH |
299 | return -1; |
300 | } | |
b14fc100 | 301 | |
9be53773 SH |
302 | if (ret != pid) |
303 | goto again; | |
b14fc100 | 304 | |
9be53773 SH |
305 | if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) |
306 | return -1; | |
b14fc100 | 307 | |
9be53773 SH |
308 | return 0; |
309 | } | |
c797a220 CS |
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; | |
b14fc100 | 320 | |
c797a220 CS |
321 | return -1; |
322 | } | |
b14fc100 | 323 | |
c797a220 CS |
324 | if (ret != pid) |
325 | goto again; | |
b14fc100 | 326 | |
c797a220 CS |
327 | return status; |
328 | } | |
92f023dc | 329 | |
b14fc100 | 330 | ssize_t lxc_write_nointr(int fd, const void *buf, size_t count) |
92f023dc | 331 | { |
650468bb | 332 | ssize_t ret; |
92f023dc CS |
333 | again: |
334 | ret = write(fd, buf, count); | |
335 | if (ret < 0 && errno == EINTR) | |
336 | goto again; | |
b14fc100 | 337 | |
92f023dc CS |
338 | return ret; |
339 | } | |
340 | ||
b14fc100 | 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; | |
b14fc100 | 348 | |
92f023dc CS |
349 | return ret; |
350 | } | |
351 | ||
b14fc100 | 352 | ssize_t lxc_read_nointr_expect(int fd, void *buf, size_t count, const void *expected_buf) |
92f023dc | 353 | { |
650468bb | 354 | ssize_t ret; |
b14fc100 | 355 | |
92f023dc CS |
356 | ret = lxc_read_nointr(fd, buf, count); |
357 | if (ret <= 0) | |
358 | return ret; | |
b14fc100 | 359 | |
650468bb | 360 | if ((size_t)ret != count) |
92f023dc | 361 | return -1; |
b14fc100 | 362 | |
92f023dc CS |
363 | if (expected_buf && memcmp(buf, expected_buf, count) != 0) { |
364 | errno = EINVAL; | |
365 | return -1; | |
366 | } | |
b14fc100 | 367 | |
92f023dc CS |
368 | return ret; |
369 | } | |
3ce74686 SH |
370 | |
371 | #if HAVE_LIBGNUTLS | |
372 | #include <gnutls/gnutls.h> | |
373 | #include <gnutls/crypto.h> | |
41246cee DE |
374 | |
375 | __attribute__((constructor)) | |
376 | static void gnutls_lxc_init(void) | |
377 | { | |
378 | gnutls_global_init(); | |
379 | } | |
380 | ||
3ce74686 SH |
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; | |
b14fc100 | 390 | |
025ed0f3 | 391 | f = fopen_cloexec(fnam, "r"); |
7be677a8 | 392 | if (!f) { |
3ce74686 SH |
393 | SYSERROR("Error opening template"); |
394 | return -1; | |
395 | } | |
b14fc100 | 396 | |
3ce74686 SH |
397 | if (fseek(f, 0, SEEK_END) < 0) { |
398 | SYSERROR("Error seeking to end of template"); | |
dd1d77f9 | 399 | fclose(f); |
3ce74686 SH |
400 | return -1; |
401 | } | |
b14fc100 | 402 | |
3ce74686 SH |
403 | if ((flen = ftell(f)) < 0) { |
404 | SYSERROR("Error telling size of template"); | |
dd1d77f9 | 405 | fclose(f); |
3ce74686 SH |
406 | return -1; |
407 | } | |
b14fc100 | 408 | |
3ce74686 SH |
409 | if (fseek(f, 0, SEEK_SET) < 0) { |
410 | SYSERROR("Error seeking to start of template"); | |
dd1d77f9 | 411 | fclose(f); |
3ce74686 SH |
412 | return -1; |
413 | } | |
b14fc100 | 414 | |
3ce74686 SH |
415 | if ((buf = malloc(flen+1)) == NULL) { |
416 | SYSERROR("Out of memory"); | |
dd1d77f9 | 417 | fclose(f); |
3ce74686 SH |
418 | return -1; |
419 | } | |
b14fc100 | 420 | |
3ce74686 SH |
421 | if (fread(buf, 1, flen, f) != flen) { |
422 | SYSERROR("Failure reading template"); | |
423 | free(buf); | |
dd1d77f9 | 424 | fclose(f); |
3ce74686 SH |
425 | return -1; |
426 | } | |
b14fc100 | 427 | |
dd1d77f9 | 428 | if (fclose(f) < 0) { |
3ce74686 SH |
429 | SYSERROR("Failre closing template"); |
430 | free(buf); | |
431 | return -1; | |
432 | } | |
b14fc100 | 433 | |
3ce74686 SH |
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 | |
61a1d519 CS |
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; | |
b14fc100 | 462 | |
61a1d519 CS |
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 | } | |
db27c8d7 | 486 | |
8bd8018e | 487 | struct lxc_popen_FILE *lxc_popen(const char *command) |
ebec9176 | 488 | { |
3f323207 | 489 | int ret; |
ebec9176 AM |
490 | int pipe_fds[2]; |
491 | pid_t child_pid; | |
8bd8018e | 492 | struct lxc_popen_FILE *fp = NULL; |
ebec9176 | 493 | |
8bd8018e CB |
494 | ret = pipe2(pipe_fds, O_CLOEXEC); |
495 | if (ret < 0) | |
ebec9176 | 496 | return NULL; |
ebec9176 AM |
497 | |
498 | child_pid = fork(); | |
8bd8018e CB |
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]); | |
03f618af | 514 | _exit(EXIT_FAILURE); |
3f323207 CB |
515 | } |
516 | ||
8bd8018e CB |
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) | |
03f618af | 524 | _exit(EXIT_FAILURE); |
8bd8018e CB |
525 | |
526 | /* unblock all signals */ | |
527 | ret = sigfillset(&mask); | |
528 | if (ret < 0) | |
03f618af | 529 | _exit(EXIT_FAILURE); |
8bd8018e | 530 | |
b467714b | 531 | ret = pthread_sigmask(SIG_UNBLOCK, &mask, NULL); |
8bd8018e | 532 | if (ret < 0) |
03f618af | 533 | _exit(EXIT_FAILURE); |
8bd8018e CB |
534 | |
535 | execl("/bin/sh", "sh", "-c", command, (char *)NULL); | |
03f618af | 536 | _exit(127); |
ebec9176 AM |
537 | } |
538 | ||
8bd8018e CB |
539 | close(pipe_fds[1]); |
540 | pipe_fds[1] = -1; | |
ebec9176 | 541 | |
8bd8018e CB |
542 | fp = malloc(sizeof(*fp)); |
543 | if (!fp) | |
544 | goto on_error; | |
b14fc100 | 545 | |
7e50ec0b | 546 | memset(fp, 0, sizeof(*fp)); |
ebec9176 AM |
547 | |
548 | fp->child_pid = child_pid; | |
8bd8018e | 549 | fp->pipe = pipe_fds[0]; |
ebec9176 | 550 | |
7e50ec0b CB |
551 | /* From now on, closing fp->f will also close fp->pipe. So only ever |
552 | * call fclose(fp->f). | |
553 | */ | |
8bd8018e CB |
554 | fp->f = fdopen(pipe_fds[0], "r"); |
555 | if (!fp->f) | |
556 | goto on_error; | |
ebec9176 | 557 | |
8bd8018e | 558 | return fp; |
ebec9176 | 559 | |
8bd8018e | 560 | on_error: |
7e50ec0b CB |
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) | |
8bd8018e CB |
566 | close(pipe_fds[0]); |
567 | ||
568 | if (pipe_fds[1] >= 0) | |
569 | close(pipe_fds[1]); | |
ebec9176 | 570 | |
7e50ec0b CB |
571 | if (fp && fp->f) |
572 | fclose(fp->f); | |
573 | ||
574 | if (fp) | |
575 | free(fp); | |
576 | ||
ebec9176 AM |
577 | return NULL; |
578 | } | |
579 | ||
8bd8018e | 580 | int lxc_pclose(struct lxc_popen_FILE *fp) |
ebec9176 | 581 | { |
ebec9176 | 582 | pid_t wait_pid; |
8bd8018e | 583 | int wstatus = 0; |
ebec9176 | 584 | |
8bd8018e | 585 | if (!fp) |
ebec9176 | 586 | return -1; |
ebec9176 AM |
587 | |
588 | do { | |
8bd8018e CB |
589 | wait_pid = waitpid(fp->child_pid, &wstatus, 0); |
590 | } while (wait_pid < 0 && errno == EINTR); | |
ebec9176 | 591 | |
8bd8018e CB |
592 | fclose(fp->f); |
593 | free(fp); | |
594 | ||
595 | if (wait_pid < 0) | |
ebec9176 | 596 | return -1; |
ebec9176 AM |
597 | |
598 | return wstatus; | |
599 | } | |
600 | ||
502657d5 CS |
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; | |
b14fc100 | 618 | |
502657d5 CS |
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); | |
b14fc100 | 628 | |
502657d5 | 629 | len += part_len; |
b14fc100 | 630 | |
502657d5 CS |
631 | if (result && replacement_len > 0) |
632 | memcpy(&result[len], replacement, replacement_len); | |
b14fc100 | 633 | |
502657d5 CS |
634 | len += replacement_len; |
635 | p += needle_len; | |
636 | } | |
b14fc100 | 637 | |
502657d5 CS |
638 | part_len = strlen(last_p); |
639 | if (result && part_len > 0) | |
640 | memcpy(&result[len], last_p, part_len); | |
b14fc100 | 641 | |
502657d5 CS |
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 */ | |
97bc2422 CB |
648 | if (saved_len != len) { |
649 | free(result); | |
650 | return NULL; | |
651 | } | |
b14fc100 | 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; | |
b14fc100 | 668 | |
502657d5 CS |
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; | |
bd583214 | 678 | size_t buf_len; |
502657d5 CS |
679 | |
680 | /* calculate new string length */ | |
681 | for (p = (char **)parts; *p; p++) | |
682 | result_len += (p > (char **)parts) * sep_len + strlen(*p); | |
683 | ||
bd583214 DJ |
684 | buf_len = result_len + 1; |
685 | result = calloc(buf_len, 1); | |
502657d5 CS |
686 | if (!result) |
687 | return NULL; | |
688 | ||
689 | if (use_as_prefix) | |
bd583214 | 690 | (void)strlcpy(result, sep, buf_len); |
43f984ea | 691 | |
502657d5 CS |
692 | for (p = (char **)parts; *p; p++) { |
693 | if (p > (char **)parts) | |
bd583214 | 694 | (void)strlcat(result, sep, buf_len); |
b14fc100 | 695 | |
bd583214 | 696 | (void)strlcat(result, *p, buf_len); |
502657d5 CS |
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; | |
b14fc100 | 712 | |
502657d5 CS |
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 | ||
eda0afd4 | 738 | char *lxc_deslashify(const char *path) |
aeb3682f | 739 | { |
eda0afd4 | 740 | char *dup, *p; |
c56a9652 CB |
741 | char **parts = NULL; |
742 | size_t n, len; | |
aeb3682f | 743 | |
eda0afd4 CB |
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 | } | |
aeb3682f | 753 | |
c56a9652 CB |
754 | /* We'll end up here if path == "///" or path == "". */ |
755 | if (!*parts) { | |
eda0afd4 | 756 | len = strlen(dup); |
f85b16a1 | 757 | if (!len) { |
eda0afd4 CB |
758 | lxc_free_array((void **)parts, free); |
759 | return dup; | |
f85b16a1 | 760 | } |
b14fc100 | 761 | |
eda0afd4 | 762 | n = strcspn(dup, "/"); |
c56a9652 | 763 | if (n == len) { |
eda0afd4 CB |
764 | free(dup); |
765 | lxc_free_array((void **)parts, free); | |
766 | ||
c56a9652 CB |
767 | p = strdup("/"); |
768 | if (!p) | |
eda0afd4 CB |
769 | return NULL; |
770 | ||
771 | return p; | |
c56a9652 CB |
772 | } |
773 | } | |
774 | ||
eda0afd4 CB |
775 | p = lxc_string_join("/", (const char **)parts, *dup == '/'); |
776 | free(dup); | |
f85b16a1 | 777 | lxc_free_array((void **)parts, free); |
eda0afd4 | 778 | return p; |
aeb3682f TA |
779 | } |
780 | ||
24b51482 CS |
781 | char *lxc_append_paths(const char *first, const char *second) |
782 | { | |
e8eb3956 CB |
783 | int ret; |
784 | size_t len; | |
24b51482 | 785 | char *result = NULL; |
e8eb3956 | 786 | const char *pattern = "%s%s"; |
24b51482 | 787 | |
e8eb3956 | 788 | len = strlen(first) + strlen(second) + 1; |
24b51482 CS |
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 | ||
e8eb3956 CB |
798 | ret = snprintf(result, len, pattern, first, second); |
799 | if (ret < 0 || (size_t)ret >= len) { | |
800 | free(result); | |
801 | return NULL; | |
802 | } | |
803 | ||
24b51482 CS |
804 | return result; |
805 | } | |
806 | ||
502657d5 CS |
807 | bool lxc_string_in_list(const char *needle, const char *haystack, char _sep) |
808 | { | |
84c5549b | 809 | char *token, *str; |
502657d5 | 810 | char sep[2] = { _sep, '\0' }; |
43f984ea | 811 | size_t len; |
502657d5 CS |
812 | |
813 | if (!haystack || !needle) | |
814 | return 0; | |
815 | ||
43f984ea DJ |
816 | len = strlen(haystack); |
817 | str = alloca(len + 1); | |
818 | (void)strlcpy(str, haystack, len + 1); | |
819 | ||
84c5549b | 820 | lxc_iterate_parts(token, str, sep) |
502657d5 CS |
821 | if (strcmp(needle, token) == 0) |
822 | return 1; | |
502657d5 CS |
823 | |
824 | return 0; | |
825 | } | |
826 | ||
827 | char **lxc_string_split(const char *string, char _sep) | |
828 | { | |
84c5549b | 829 | char *token, *str; |
605ea1f7 CB |
830 | char sep[2] = {_sep, '\0'}; |
831 | char **tmp = NULL, **result = NULL; | |
502657d5 CS |
832 | size_t result_capacity = 0; |
833 | size_t result_count = 0; | |
834 | int r, saved_errno; | |
43f984ea | 835 | size_t len; |
502657d5 CS |
836 | |
837 | if (!string) | |
838 | return calloc(1, sizeof(char *)); | |
839 | ||
43f984ea DJ |
840 | len = strlen(string); |
841 | str = alloca(len + 1); | |
842 | (void)strlcpy(str, string, len + 1); | |
843 | ||
84c5549b | 844 | lxc_iterate_parts(token, str, sep) { |
502657d5 CS |
845 | r = lxc_grow_array((void ***)&result, &result_capacity, result_count + 1, 16); |
846 | if (r < 0) | |
847 | goto error_out; | |
b14fc100 | 848 | |
502657d5 CS |
849 | result[result_count] = strdup(token); |
850 | if (!result[result_count]) | |
851 | goto error_out; | |
b14fc100 | 852 | |
502657d5 CS |
853 | result_count++; |
854 | } | |
855 | ||
856 | /* if we allocated too much, reduce it */ | |
605ea1f7 CB |
857 | tmp = realloc(result, (result_count + 1) * sizeof(char *)); |
858 | if (!tmp) | |
859 | goto error_out; | |
b14fc100 | 860 | |
605ea1f7 | 861 | result = tmp; |
b14fc100 | 862 | |
605ea1f7 CB |
863 | /* Make sure we don't return uninitialized memory. */ |
864 | if (result_count == 0) | |
865 | *result = NULL; | |
b14fc100 | 866 | |
605ea1f7 | 867 | return result; |
b14fc100 | 868 | |
502657d5 CS |
869 | error_out: |
870 | saved_errno = errno; | |
871 | lxc_free_array((void **)result, free); | |
872 | errno = saved_errno; | |
873 | return NULL; | |
874 | } | |
875 | ||
3dca1af0 SH |
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; | |
b14fc100 | 883 | |
3dca1af0 SH |
884 | (*result)[*cnt] = strndup(start, end - start); |
885 | if (!(*result)[*cnt]) | |
886 | return false; | |
b14fc100 | 887 | |
3dca1af0 SH |
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 | ||
502657d5 CS |
946 | char **lxc_string_split_and_trim(const char *string, char _sep) |
947 | { | |
84c5549b | 948 | char *token, *str; |
502657d5 CS |
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; | |
43f984ea | 955 | size_t len; |
502657d5 CS |
956 | |
957 | if (!string) | |
958 | return calloc(1, sizeof(char *)); | |
959 | ||
43f984ea DJ |
960 | len = strlen(string); |
961 | str = alloca(len + 1); | |
962 | (void)strlcpy(str, string, len + 1); | |
963 | ||
84c5549b | 964 | lxc_iterate_parts(token, str, sep) { |
502657d5 CS |
965 | while (token[0] == ' ' || token[0] == '\t') |
966 | token++; | |
b14fc100 | 967 | |
502657d5 CS |
968 | i = strlen(token); |
969 | while (i > 0 && (token[i - 1] == ' ' || token[i - 1] == '\t')) { | |
970 | token[i - 1] = '\0'; | |
971 | i--; | |
972 | } | |
b14fc100 | 973 | |
502657d5 CS |
974 | r = lxc_grow_array((void ***)&result, &result_capacity, result_count + 1, 16); |
975 | if (r < 0) | |
976 | goto error_out; | |
b14fc100 | 977 | |
502657d5 CS |
978 | result[result_count] = strdup(token); |
979 | if (!result[result_count]) | |
980 | goto error_out; | |
b14fc100 | 981 | |
502657d5 CS |
982 | result_count++; |
983 | } | |
984 | ||
985 | /* if we allocated too much, reduce it */ | |
986 | return realloc(result, (result_count + 1) * sizeof(char *)); | |
b14fc100 | 987 | |
502657d5 CS |
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; | |
b14fc100 | 998 | |
502657d5 CS |
999 | for (p = array; p && *p; p++) |
1000 | element_free_fn(*p); | |
b14fc100 | 1001 | |
502657d5 CS |
1002 | free((void*)array); |
1003 | } | |
1004 | ||
b14fc100 | 1005 | int lxc_grow_array(void ***array, size_t *capacity, size_t new_size, size_t capacity_increment) |
502657d5 CS |
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; | |
b14fc100 | 1020 | |
502657d5 CS |
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; | |
b14fc100 | 1026 | |
502657d5 CS |
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 | ||
7cea5905 CB |
1047 | int lxc_write_to_file(const char *filename, const void *buf, size_t count, |
1048 | bool add_newline, mode_t mode) | |
0e95426b CS |
1049 | { |
1050 | int fd, saved_errno; | |
1051 | ssize_t ret; | |
1052 | ||
7cea5905 | 1053 | fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, mode); |
0e95426b CS |
1054 | if (fd < 0) |
1055 | return -1; | |
b14fc100 | 1056 | |
0e95426b CS |
1057 | ret = lxc_write_nointr(fd, buf, count); |
1058 | if (ret < 0) | |
799f29ab | 1059 | goto out_error; |
b14fc100 | 1060 | |
0e95426b CS |
1061 | if ((size_t)ret != count) |
1062 | goto out_error; | |
b14fc100 | 1063 | |
0e95426b CS |
1064 | if (add_newline) { |
1065 | ret = lxc_write_nointr(fd, "\n", 1); | |
1066 | if (ret != 1) | |
1067 | goto out_error; | |
1068 | } | |
b14fc100 | 1069 | |
0e95426b CS |
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 | ||
b14fc100 | 1080 | int lxc_read_from_file(const char *filename, void *buf, size_t count) |
0e95426b CS |
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; | |
6d1400b5 | 1092 | |
0e95426b CS |
1093 | while ((ret = read(fd, buf2, 100)) > 0) |
1094 | count2 += ret; | |
6d1400b5 | 1095 | |
0e95426b CS |
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) | |
6d1400b5 | 1104 | SYSERROR("Read %s", filename); |
0e95426b CS |
1105 | |
1106 | saved_errno = errno; | |
1107 | close(fd); | |
1108 | errno = saved_errno; | |
1109 | return ret; | |
1110 | } | |
799f29ab ÇO |
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) { | |
84760c11 | 1120 | size_t i; |
799f29ab ÇO |
1121 | for (i = 0; i < count; i++) |
1122 | free(array[i]); | |
1123 | free(array); | |
1124 | return NULL; | |
1125 | } | |
b14fc100 | 1126 | |
799f29ab ÇO |
1127 | array = temp; |
1128 | array[count] = NULL; | |
1129 | } | |
b14fc100 | 1130 | |
799f29ab ÇO |
1131 | return array; |
1132 | } | |
508c263e SH |
1133 | |
1134 | int randseed(bool srand_it) | |
1135 | { | |
1136 | /* | |
1137 | srand pre-seed function based on /dev/urandom | |
1138 | */ | |
091045f8 | 1139 | unsigned int seed = time(NULL) + getpid(); |
508c263e SH |
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) | |
7874d81a | 1146 | SYSDEBUG("unable to fread /dev/urandom, fallback to time+pid rand seed"); |
1147 | ||
508c263e SH |
1148 | fclose(f); |
1149 | } | |
1150 | ||
1151 | if (srand_it) | |
1152 | srand(seed); | |
1153 | ||
1154 | return seed; | |
1155 | } | |
5d897655 SH |
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; | |
b14fc100 | 1169 | |
5d897655 SH |
1170 | if (hostid <= orig && hostid + range > orig) { |
1171 | nsid += orig - hostid; | |
1172 | goto found; | |
1173 | } | |
1174 | } | |
1175 | ||
1176 | nsid = 0; | |
b14fc100 | 1177 | |
5d897655 SH |
1178 | found: |
1179 | fclose(f); | |
1180 | free(line); | |
1181 | return nsid; | |
1182 | } | |
c476bdce SH |
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) | |
1a0e70ac | 1191 | /* Could be something other than eexist, just say "no". */ |
c476bdce | 1192 | return false; |
b14fc100 | 1193 | |
c476bdce SH |
1194 | return S_ISDIR(sb.st_mode); |
1195 | } | |
93c379f0 ÇO |
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 | } | |
2c6f3fc9 | 1220 | |
f6310f18 | 1221 | bool is_shared_mountpoint(const char *path) |
2c6f3fc9 | 1222 | { |
f6310f18 | 1223 | char buf[LXC_LINELEN]; |
2c6f3fc9 SH |
1224 | FILE *f; |
1225 | int i; | |
f6310f18 | 1226 | char *p, *p2; |
2c6f3fc9 SH |
1227 | |
1228 | f = fopen("/proc/self/mountinfo", "r"); | |
1229 | if (!f) | |
1230 | return 0; | |
b14fc100 | 1231 | |
eab15c1e CB |
1232 | while (fgets(buf, LXC_LINELEN, f)) { |
1233 | for (p = buf, i = 0; p && i < 4; i++) | |
1234 | p = strchr(p + 1, ' '); | |
2c6f3fc9 SH |
1235 | if (!p) |
1236 | continue; | |
b14fc100 | 1237 | |
eab15c1e | 1238 | p2 = strchr(p + 1, ' '); |
2c6f3fc9 SH |
1239 | if (!p2) |
1240 | continue; | |
b14fc100 | 1241 | |
2c6f3fc9 | 1242 | *p2 = '\0'; |
f6310f18 LT |
1243 | if (strcmp(p + 1, path) == 0) { |
1244 | /* This is the path. Is it shared? */ | |
eab15c1e | 1245 | p = strchr(p2 + 1, ' '); |
2c6f3fc9 SH |
1246 | if (p && strstr(p, "shared:")) { |
1247 | fclose(f); | |
f6310f18 | 1248 | return true; |
2c6f3fc9 SH |
1249 | } |
1250 | } | |
1251 | } | |
b14fc100 | 1252 | |
2c6f3fc9 | 1253 | fclose(f); |
f6310f18 LT |
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; | |
2c6f3fc9 SH |
1268 | return 0; |
1269 | } | |
0e6e3a41 | 1270 | |
51d0854c DY |
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) { | |
a9cb0fb8 | 1282 | SYSERROR("Failed to open %s", nspath); |
51d0854c DY |
1283 | return false; |
1284 | } | |
1285 | ||
1286 | ret = setns(fd, 0); | |
1287 | if (ret) { | |
a9cb0fb8 | 1288 | SYSERROR("Failed to set process %d to %s of %d.", pid, ns, fd); |
51d0854c DY |
1289 | close(fd); |
1290 | return false; | |
1291 | } | |
b14fc100 | 1292 | |
51d0854c DY |
1293 | close(fd); |
1294 | return true; | |
1295 | } | |
1296 | ||
b7f954bb SH |
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 | */ | |
fa454c8e | 1304 | bool detect_ramfs_rootfs(void) |
b7f954bb | 1305 | { |
b7f954bb | 1306 | FILE *f; |
fa454c8e CB |
1307 | char *p, *p2; |
1308 | char *line = NULL; | |
1309 | size_t len = 0; | |
b7f954bb | 1310 | int i; |
b7f954bb SH |
1311 | |
1312 | f = fopen("/proc/self/mountinfo", "r"); | |
1313 | if (!f) | |
fa454c8e CB |
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, ' '); | |
b7f954bb SH |
1319 | if (!p) |
1320 | continue; | |
b14fc100 | 1321 | |
fa454c8e | 1322 | p2 = strchr(p + 1, ' '); |
b7f954bb SH |
1323 | if (!p2) |
1324 | continue; | |
b14fc100 | 1325 | |
b7f954bb | 1326 | *p2 = '\0'; |
fa454c8e | 1327 | if (strcmp(p + 1, "/") == 0) { |
1a0e70ac | 1328 | /* This is '/'. Is it the ramfs? */ |
fa454c8e | 1329 | p = strchr(p2 + 1, '-'); |
b7f954bb | 1330 | if (p && strncmp(p, "- rootfs rootfs ", 16) == 0) { |
fa454c8e | 1331 | free(line); |
b7f954bb | 1332 | fclose(f); |
8ce1abc2 | 1333 | INFO("Rootfs is located on ramfs"); |
fa454c8e | 1334 | return true; |
b7f954bb SH |
1335 | } |
1336 | } | |
1337 | } | |
b14fc100 | 1338 | |
fa454c8e | 1339 | free(line); |
b7f954bb | 1340 | fclose(f); |
fa454c8e | 1341 | return false; |
b7f954bb SH |
1342 | } |
1343 | ||
df6a2945 | 1344 | char *on_path(const char *cmd, const char *rootfs) { |
84c5549b | 1345 | char *entry = NULL, *path = NULL; |
0e6e3a41 SG |
1346 | char cmdpath[MAXPATHLEN]; |
1347 | int ret; | |
1348 | ||
1349 | path = getenv("PATH"); | |
1350 | if (!path) | |
8afb3e61 | 1351 | return NULL; |
0e6e3a41 SG |
1352 | |
1353 | path = strdup(path); | |
1354 | if (!path) | |
8afb3e61 | 1355 | return NULL; |
0e6e3a41 | 1356 | |
84c5549b | 1357 | lxc_iterate_parts(entry, path, ":") { |
9d9c111c SH |
1358 | if (rootfs) |
1359 | ret = snprintf(cmdpath, MAXPATHLEN, "%s/%s/%s", rootfs, entry, cmd); | |
1360 | else | |
1361 | ret = snprintf(cmdpath, MAXPATHLEN, "%s/%s", entry, cmd); | |
0e6e3a41 | 1362 | if (ret < 0 || ret >= MAXPATHLEN) |
84c5549b | 1363 | continue; |
0e6e3a41 SG |
1364 | |
1365 | if (access(cmdpath, X_OK) == 0) { | |
1366 | free(path); | |
8afb3e61 | 1367 | return strdup(cmdpath); |
0e6e3a41 | 1368 | } |
0e6e3a41 SG |
1369 | } |
1370 | ||
1371 | free(path); | |
8afb3e61 | 1372 | return NULL; |
0e6e3a41 | 1373 | } |
76a26f55 SH |
1374 | |
1375 | bool file_exists(const char *f) | |
1376 | { | |
1377 | struct stat statbuf; | |
1378 | ||
1379 | return stat(f, &statbuf) == 0; | |
1380 | } | |
9d9c111c | 1381 | |
12983ba4 SH |
1382 | bool cgns_supported(void) |
1383 | { | |
1384 | return file_exists("/proc/self/ns/cgroup"); | |
1385 | } | |
1386 | ||
9d9c111c SH |
1387 | /* historically lxc-init has been under /usr/lib/lxc and under |
1388 | * /usr/lib/$ARCH/lxc. It now lives as $prefix/sbin/init.lxc. | |
1389 | */ | |
1390 | char *choose_init(const char *rootfs) | |
1391 | { | |
1392 | char *retv = NULL; | |
370ec268 SF |
1393 | const char *empty = "", |
1394 | *tmp; | |
9d9c111c | 1395 | int ret, env_set = 0; |
9d9c111c SH |
1396 | |
1397 | if (!getenv("PATH")) { | |
1398 | if (setenv("PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", 0)) | |
1399 | SYSERROR("Failed to setenv"); | |
b14fc100 | 1400 | |
9d9c111c SH |
1401 | env_set = 1; |
1402 | } | |
1403 | ||
1404 | retv = on_path("init.lxc", rootfs); | |
1405 | ||
1406 | if (env_set) { | |
1407 | if (unsetenv("PATH")) | |
1408 | SYSERROR("Failed to unsetenv"); | |
1409 | } | |
1410 | ||
1411 | if (retv) | |
1412 | return retv; | |
1413 | ||
1414 | retv = malloc(PATH_MAX); | |
1415 | if (!retv) | |
1416 | return NULL; | |
1417 | ||
1418 | if (rootfs) | |
370ec268 | 1419 | tmp = rootfs; |
9d9c111c | 1420 | else |
370ec268 SF |
1421 | tmp = empty; |
1422 | ||
1423 | ret = snprintf(retv, PATH_MAX, "%s/%s/%s", tmp, SBINDIR, "/init.lxc"); | |
9d9c111c SH |
1424 | if (ret < 0 || ret >= PATH_MAX) { |
1425 | ERROR("pathname too long"); | |
1426 | goto out1; | |
1427 | } | |
b14fc100 | 1428 | |
e57cd7e9 | 1429 | if (access(retv, X_OK) == 0) |
9d9c111c SH |
1430 | return retv; |
1431 | ||
370ec268 | 1432 | ret = snprintf(retv, PATH_MAX, "%s/%s/%s", tmp, LXCINITDIR, "/lxc/lxc-init"); |
9d9c111c SH |
1433 | if (ret < 0 || ret >= PATH_MAX) { |
1434 | ERROR("pathname too long"); | |
1435 | goto out1; | |
1436 | } | |
b14fc100 | 1437 | |
e57cd7e9 | 1438 | if (access(retv, X_OK) == 0) |
9d9c111c SH |
1439 | return retv; |
1440 | ||
370ec268 | 1441 | ret = snprintf(retv, PATH_MAX, "%s/usr/lib/lxc/lxc-init", tmp); |
9d9c111c SH |
1442 | if (ret < 0 || ret >= PATH_MAX) { |
1443 | ERROR("pathname too long"); | |
1444 | goto out1; | |
1445 | } | |
b14fc100 | 1446 | |
e57cd7e9 | 1447 | if (access(retv, X_OK) == 0) |
9d9c111c SH |
1448 | return retv; |
1449 | ||
370ec268 | 1450 | ret = snprintf(retv, PATH_MAX, "%s/sbin/lxc-init", tmp); |
9d9c111c SH |
1451 | if (ret < 0 || ret >= PATH_MAX) { |
1452 | ERROR("pathname too long"); | |
1453 | goto out1; | |
1454 | } | |
b14fc100 | 1455 | |
e57cd7e9 | 1456 | if (access(retv, X_OK) == 0) |
9d9c111c SH |
1457 | return retv; |
1458 | ||
1459 | /* | |
1460 | * Last resort, look for the statically compiled init.lxc which we | |
1461 | * hopefully bind-mounted in. | |
1462 | * If we are called during container setup, and we get to this point, | |
1463 | * then the init.lxc.static from the host will need to be bind-mounted | |
1464 | * in. So we return NULL here to indicate that. | |
1465 | */ | |
1466 | if (rootfs) | |
1467 | goto out1; | |
1468 | ||
1469 | ret = snprintf(retv, PATH_MAX, "/init.lxc.static"); | |
1470 | if (ret < 0 || ret >= PATH_MAX) { | |
1471 | WARN("Nonsense - name /lxc.init.static too long"); | |
1472 | goto out1; | |
1473 | } | |
b14fc100 | 1474 | |
e57cd7e9 | 1475 | if (access(retv, X_OK) == 0) |
9d9c111c SH |
1476 | return retv; |
1477 | ||
1478 | out1: | |
1479 | free(retv); | |
1480 | return NULL; | |
1481 | } | |
735f2c6e TA |
1482 | |
1483 | int print_to_file(const char *file, const char *content) | |
1484 | { | |
1485 | FILE *f; | |
1486 | int ret = 0; | |
1487 | ||
1488 | f = fopen(file, "w"); | |
1489 | if (!f) | |
1490 | return -1; | |
b14fc100 | 1491 | |
735f2c6e TA |
1492 | if (fprintf(f, "%s", content) != strlen(content)) |
1493 | ret = -1; | |
b14fc100 | 1494 | |
735f2c6e TA |
1495 | fclose(f); |
1496 | return ret; | |
1497 | } | |
e1daebd9 SH |
1498 | |
1499 | int is_dir(const char *path) | |
1500 | { | |
1501 | struct stat statbuf; | |
b14fc100 | 1502 | int ret; |
1503 | ||
1504 | ret = stat(path, &statbuf); | |
e1daebd9 SH |
1505 | if (ret == 0 && S_ISDIR(statbuf.st_mode)) |
1506 | return 1; | |
b14fc100 | 1507 | |
e1daebd9 SH |
1508 | return 0; |
1509 | } | |
6010a416 SG |
1510 | |
1511 | /* | |
1512 | * Given the '-t' template option to lxc-create, figure out what to | |
1513 | * do. If the template is a full executable path, use that. If it | |
1514 | * is something like 'sshd', then return $templatepath/lxc-sshd. | |
1515 | * On success return the template, on error return NULL. | |
1516 | */ | |
1517 | char *get_template_path(const char *t) | |
1518 | { | |
1519 | int ret, len; | |
1520 | char *tpath; | |
1521 | ||
1522 | if (t[0] == '/' && access(t, X_OK) == 0) { | |
1523 | tpath = strdup(t); | |
1524 | return tpath; | |
1525 | } | |
1526 | ||
1527 | len = strlen(LXCTEMPLATEDIR) + strlen(t) + strlen("/lxc-") + 1; | |
b14fc100 | 1528 | |
6010a416 SG |
1529 | tpath = malloc(len); |
1530 | if (!tpath) | |
1531 | return NULL; | |
b14fc100 | 1532 | |
6010a416 SG |
1533 | ret = snprintf(tpath, len, "%s/lxc-%s", LXCTEMPLATEDIR, t); |
1534 | if (ret < 0 || ret >= len) { | |
1535 | free(tpath); | |
1536 | return NULL; | |
1537 | } | |
b14fc100 | 1538 | |
6010a416 SG |
1539 | if (access(tpath, X_OK) < 0) { |
1540 | SYSERROR("bad template: %s", t); | |
1541 | free(tpath); | |
1542 | return NULL; | |
1543 | } | |
1544 | ||
1545 | return tpath; | |
1546 | } | |
0a4be28d | 1547 | |
592fd47a SH |
1548 | /* |
1549 | * @path: a pathname where / replaced with '\0'. | |
1550 | * @offsetp: pointer to int showing which path segment was last seen. | |
1551 | * Updated on return to reflect the next segment. | |
1552 | * @fulllen: full original path length. | |
1553 | * Returns a pointer to the next path segment, or NULL if done. | |
1554 | */ | |
1555 | static char *get_nextpath(char *path, int *offsetp, int fulllen) | |
1556 | { | |
1557 | int offset = *offsetp; | |
1558 | ||
1559 | if (offset >= fulllen) | |
1560 | return NULL; | |
1561 | ||
1562 | while (path[offset] != '\0' && offset < fulllen) | |
1563 | offset++; | |
b14fc100 | 1564 | |
592fd47a SH |
1565 | while (path[offset] == '\0' && offset < fulllen) |
1566 | offset++; | |
1567 | ||
1568 | *offsetp = offset; | |
1569 | return (offset < fulllen) ? &path[offset] : NULL; | |
1570 | } | |
1571 | ||
1572 | /* | |
1573 | * Check that @subdir is a subdir of @dir. @len is the length of | |
1574 | * @dir (to avoid having to recalculate it). | |
1575 | */ | |
1576 | static bool is_subdir(const char *subdir, const char *dir, size_t len) | |
1577 | { | |
1578 | size_t subdirlen = strlen(subdir); | |
1579 | ||
1580 | if (subdirlen < len) | |
1581 | return false; | |
b14fc100 | 1582 | |
592fd47a SH |
1583 | if (strncmp(subdir, dir, len) != 0) |
1584 | return false; | |
b14fc100 | 1585 | |
592fd47a SH |
1586 | if (dir[len-1] == '/') |
1587 | return true; | |
b14fc100 | 1588 | |
592fd47a SH |
1589 | if (subdir[len] == '/' || subdirlen == len) |
1590 | return true; | |
b14fc100 | 1591 | |
592fd47a SH |
1592 | return false; |
1593 | } | |
1594 | ||
1595 | /* | |
1596 | * Check if the open fd is a symlink. Return -ELOOP if it is. Return | |
1597 | * -ENOENT if we couldn't fstat. Return 0 if the fd is ok. | |
1598 | */ | |
1599 | static int check_symlink(int fd) | |
1600 | { | |
1601 | struct stat sb; | |
b14fc100 | 1602 | int ret; |
1603 | ||
1604 | ret = fstat(fd, &sb); | |
592fd47a SH |
1605 | if (ret < 0) |
1606 | return -ENOENT; | |
b14fc100 | 1607 | |
592fd47a SH |
1608 | if (S_ISLNK(sb.st_mode)) |
1609 | return -ELOOP; | |
b14fc100 | 1610 | |
592fd47a SH |
1611 | return 0; |
1612 | } | |
1613 | ||
1614 | /* | |
1615 | * Open a file or directory, provided that it contains no symlinks. | |
1616 | * | |
1617 | * CAVEAT: This function must not be used for other purposes than container | |
1618 | * setup before executing the container's init | |
1619 | */ | |
1620 | static int open_if_safe(int dirfd, const char *nextpath) | |
1621 | { | |
1622 | int newfd = openat(dirfd, nextpath, O_RDONLY | O_NOFOLLOW); | |
1a0e70ac | 1623 | if (newfd >= 0) /* Was not a symlink, all good. */ |
592fd47a SH |
1624 | return newfd; |
1625 | ||
1626 | if (errno == ELOOP) | |
1627 | return newfd; | |
1628 | ||
1629 | if (errno == EPERM || errno == EACCES) { | |
1a0e70ac CB |
1630 | /* We're not root (cause we got EPERM) so try opening with |
1631 | * O_PATH. | |
1632 | */ | |
592fd47a SH |
1633 | newfd = openat(dirfd, nextpath, O_PATH | O_NOFOLLOW); |
1634 | if (newfd >= 0) { | |
1a0e70ac CB |
1635 | /* O_PATH will return an fd for symlinks. We know |
1636 | * nextpath wasn't a symlink at last openat, so if fd is | |
1637 | * now a link, then something * fishy is going on. | |
592fd47a SH |
1638 | */ |
1639 | int ret = check_symlink(newfd); | |
1640 | if (ret < 0) { | |
1641 | close(newfd); | |
1642 | newfd = ret; | |
1643 | } | |
1644 | } | |
1645 | } | |
1646 | ||
1647 | return newfd; | |
1648 | } | |
1649 | ||
1650 | /* | |
1651 | * Open a path intending for mounting, ensuring that the final path | |
1652 | * is inside the container's rootfs. | |
1653 | * | |
1654 | * CAVEAT: This function must not be used for other purposes than container | |
1655 | * setup before executing the container's init | |
1656 | * | |
1657 | * @target: path to be opened | |
1658 | * @prefix_skip: a part of @target in which to ignore symbolic links. This | |
1659 | * would be the container's rootfs. | |
1660 | * | |
1661 | * Return an open fd for the path, or <0 on error. | |
1662 | */ | |
1663 | static int open_without_symlink(const char *target, const char *prefix_skip) | |
1664 | { | |
1665 | int curlen = 0, dirfd, fulllen, i; | |
1666 | char *dup = NULL; | |
1667 | ||
1668 | fulllen = strlen(target); | |
1669 | ||
1670 | /* make sure prefix-skip makes sense */ | |
01074e5b | 1671 | if (prefix_skip && strlen(prefix_skip) > 0) { |
592fd47a SH |
1672 | curlen = strlen(prefix_skip); |
1673 | if (!is_subdir(target, prefix_skip, curlen)) { | |
1674 | ERROR("WHOA there - target '%s' didn't start with prefix '%s'", | |
1675 | target, prefix_skip); | |
1676 | return -EINVAL; | |
1677 | } | |
b14fc100 | 1678 | |
592fd47a SH |
1679 | /* |
1680 | * get_nextpath() expects the curlen argument to be | |
1681 | * on a (turned into \0) / or before it, so decrement | |
1682 | * curlen to make sure that happens | |
1683 | */ | |
1684 | if (curlen) | |
1685 | curlen--; | |
1686 | } else { | |
1687 | prefix_skip = "/"; | |
1688 | curlen = 0; | |
1689 | } | |
1690 | ||
1691 | /* Make a copy of target which we can hack up, and tokenize it */ | |
1692 | if ((dup = strdup(target)) == NULL) { | |
1693 | SYSERROR("Out of memory checking for symbolic link"); | |
1694 | return -ENOMEM; | |
1695 | } | |
b14fc100 | 1696 | |
592fd47a SH |
1697 | for (i = 0; i < fulllen; i++) { |
1698 | if (dup[i] == '/') | |
1699 | dup[i] = '\0'; | |
1700 | } | |
1701 | ||
1702 | dirfd = open(prefix_skip, O_RDONLY); | |
1703 | if (dirfd < 0) | |
1704 | goto out; | |
b14fc100 | 1705 | |
592fd47a SH |
1706 | while (1) { |
1707 | int newfd, saved_errno; | |
1708 | char *nextpath; | |
1709 | ||
1710 | if ((nextpath = get_nextpath(dup, &curlen, fulllen)) == NULL) | |
1711 | goto out; | |
b14fc100 | 1712 | |
592fd47a SH |
1713 | newfd = open_if_safe(dirfd, nextpath); |
1714 | saved_errno = errno; | |
1715 | close(dirfd); | |
b14fc100 | 1716 | |
592fd47a SH |
1717 | dirfd = newfd; |
1718 | if (newfd < 0) { | |
1719 | errno = saved_errno; | |
1720 | if (errno == ELOOP) | |
1721 | SYSERROR("%s in %s was a symbolic link!", nextpath, target); | |
b14fc100 | 1722 | |
592fd47a SH |
1723 | goto out; |
1724 | } | |
1725 | } | |
1726 | ||
1727 | out: | |
1728 | free(dup); | |
1729 | return dirfd; | |
1730 | } | |
1731 | ||
1732 | /* | |
1733 | * Safely mount a path into a container, ensuring that the mount target | |
1734 | * is under the container's @rootfs. (If @rootfs is NULL, then the container | |
1735 | * uses the host's /) | |
1736 | * | |
1737 | * CAVEAT: This function must not be used for other purposes than container | |
1738 | * setup before executing the container's init | |
1739 | */ | |
1740 | int safe_mount(const char *src, const char *dest, const char *fstype, | |
1741 | unsigned long flags, const void *data, const char *rootfs) | |
1742 | { | |
1a0e70ac CB |
1743 | int destfd, ret, saved_errno; |
1744 | /* Only needs enough for /proc/self/fd/<fd>. */ | |
1745 | char srcbuf[50], destbuf[50]; | |
1746 | int srcfd = -1; | |
592fd47a SH |
1747 | const char *mntsrc = src; |
1748 | ||
1749 | if (!rootfs) | |
1750 | rootfs = ""; | |
1751 | ||
1752 | /* todo - allow symlinks for relative paths if 'allowsymlinks' option is passed */ | |
1753 | if (flags & MS_BIND && src && src[0] != '/') { | |
1754 | INFO("this is a relative bind mount"); | |
b14fc100 | 1755 | |
592fd47a SH |
1756 | srcfd = open_without_symlink(src, NULL); |
1757 | if (srcfd < 0) | |
1758 | return srcfd; | |
b14fc100 | 1759 | |
592fd47a SH |
1760 | ret = snprintf(srcbuf, 50, "/proc/self/fd/%d", srcfd); |
1761 | if (ret < 0 || ret > 50) { | |
1762 | close(srcfd); | |
1763 | ERROR("Out of memory"); | |
1764 | return -EINVAL; | |
1765 | } | |
1766 | mntsrc = srcbuf; | |
1767 | } | |
1768 | ||
1769 | destfd = open_without_symlink(dest, rootfs); | |
1770 | if (destfd < 0) { | |
88e078ba CB |
1771 | if (srcfd != -1) { |
1772 | saved_errno = errno; | |
592fd47a | 1773 | close(srcfd); |
88e078ba CB |
1774 | errno = saved_errno; |
1775 | } | |
b14fc100 | 1776 | |
592fd47a SH |
1777 | return destfd; |
1778 | } | |
1779 | ||
1780 | ret = snprintf(destbuf, 50, "/proc/self/fd/%d", destfd); | |
1781 | if (ret < 0 || ret > 50) { | |
1782 | if (srcfd != -1) | |
1783 | close(srcfd); | |
b14fc100 | 1784 | |
592fd47a SH |
1785 | close(destfd); |
1786 | ERROR("Out of memory"); | |
1787 | return -EINVAL; | |
1788 | } | |
1789 | ||
1790 | ret = mount(mntsrc, destbuf, fstype, flags, data); | |
1791 | saved_errno = errno; | |
1792 | if (srcfd != -1) | |
1793 | close(srcfd); | |
b14fc100 | 1794 | |
592fd47a SH |
1795 | close(destfd); |
1796 | if (ret < 0) { | |
1797 | errno = saved_errno; | |
0103eb53 | 1798 | SYSERROR("Failed to mount %s onto %s", src ? src : "(null)", dest); |
592fd47a SH |
1799 | return ret; |
1800 | } | |
1801 | ||
1802 | return 0; | |
1803 | } | |
1804 | ||
ced03a01 SH |
1805 | /* |
1806 | * Mount a proc under @rootfs if proc self points to a pid other than | |
1807 | * my own. This is needed to have a known-good proc mount for setting | |
1808 | * up LSMs both at container startup and attach. | |
1809 | * | |
1810 | * @rootfs : the rootfs where proc should be mounted | |
1811 | * | |
1812 | * Returns < 0 on failure, 0 if the correct proc was already mounted | |
1813 | * and 1 if a new proc was mounted. | |
f267d666 BP |
1814 | * |
1815 | * NOTE: not to be called from inside the container namespace! | |
ced03a01 | 1816 | */ |
943144d9 | 1817 | int lxc_mount_proc_if_needed(const char *rootfs) |
ced03a01 SH |
1818 | { |
1819 | char path[MAXPATHLEN]; | |
6b1ba5d6 CB |
1820 | int link_to_pid, linklen, mypid, ret; |
1821 | char link[LXC_NUMSTRLEN64] = {0}; | |
ced03a01 SH |
1822 | |
1823 | ret = snprintf(path, MAXPATHLEN, "%s/proc/self", rootfs); | |
1824 | if (ret < 0 || ret >= MAXPATHLEN) { | |
1825 | SYSERROR("proc path name too long"); | |
1826 | return -1; | |
1827 | } | |
fc2ad9dc | 1828 | |
6b1ba5d6 | 1829 | linklen = readlink(path, link, LXC_NUMSTRLEN64); |
fc2ad9dc | 1830 | |
ced03a01 | 1831 | ret = snprintf(path, MAXPATHLEN, "%s/proc", rootfs); |
d539a2b2 CB |
1832 | if (ret < 0 || ret >= MAXPATHLEN) { |
1833 | SYSERROR("proc path name too long"); | |
1834 | return -1; | |
1835 | } | |
fc2ad9dc CB |
1836 | |
1837 | /* /proc not mounted */ | |
1838 | if (linklen < 0) { | |
1839 | if (mkdir(path, 0755) && errno != EEXIST) | |
1840 | return -1; | |
b14fc100 | 1841 | |
ced03a01 | 1842 | goto domount; |
6b1ba5d6 CB |
1843 | } else if (linklen >= LXC_NUMSTRLEN64) { |
1844 | link[linklen - 1] = '\0'; | |
1845 | ERROR("readlink returned truncated content: \"%s\"", link); | |
1846 | return -1; | |
fc2ad9dc CB |
1847 | } |
1848 | ||
0059379f | 1849 | mypid = lxc_raw_getpid(); |
6b1ba5d6 CB |
1850 | INFO("I am %d, /proc/self points to \"%s\"", mypid, link); |
1851 | ||
2d036cca CB |
1852 | if (lxc_safe_int(link, &link_to_pid) < 0) |
1853 | return -1; | |
fc2ad9dc | 1854 | |
6b1ba5d6 CB |
1855 | /* correct procfs is already mounted */ |
1856 | if (link_to_pid == mypid) | |
1857 | return 0; | |
fc2ad9dc | 1858 | |
6b1ba5d6 CB |
1859 | ret = umount2(path, MNT_DETACH); |
1860 | if (ret < 0) | |
1861 | WARN("failed to umount \"%s\" with MNT_DETACH", path); | |
ced03a01 SH |
1862 | |
1863 | domount: | |
fc2ad9dc | 1864 | /* rootfs is NULL */ |
6b1ba5d6 | 1865 | if (!strcmp(rootfs, "")) |
f267d666 BP |
1866 | ret = mount("proc", path, "proc", 0, NULL); |
1867 | else | |
1868 | ret = safe_mount("proc", path, "proc", 0, NULL, rootfs); | |
f267d666 | 1869 | if (ret < 0) |
ced03a01 | 1870 | return -1; |
f267d666 | 1871 | |
fc2ad9dc | 1872 | INFO("mounted /proc in container for security transition"); |
ced03a01 SH |
1873 | return 1; |
1874 | } | |
69aeabac | 1875 | |
f8dd0275 | 1876 | int open_devnull(void) |
69aeabac | 1877 | { |
f8dd0275 AM |
1878 | int fd = open("/dev/null", O_RDWR); |
1879 | ||
1880 | if (fd < 0) | |
1881 | SYSERROR("Can't open /dev/null"); | |
1882 | ||
1883 | return fd; | |
1884 | } | |
69aeabac | 1885 | |
f8dd0275 AM |
1886 | int set_stdfds(int fd) |
1887 | { | |
bbbf65ee CB |
1888 | int ret; |
1889 | ||
69aeabac TA |
1890 | if (fd < 0) |
1891 | return -1; | |
1892 | ||
bbbf65ee CB |
1893 | ret = dup2(fd, STDIN_FILENO); |
1894 | if (ret < 0) | |
f8dd0275 | 1895 | return -1; |
bbbf65ee CB |
1896 | |
1897 | ret = dup2(fd, STDOUT_FILENO); | |
1898 | if (ret < 0) | |
f8dd0275 | 1899 | return -1; |
bbbf65ee CB |
1900 | |
1901 | ret = dup2(fd, STDERR_FILENO); | |
1902 | if (ret < 0) | |
f8dd0275 AM |
1903 | return -1; |
1904 | ||
1905 | return 0; | |
1906 | } | |
1907 | ||
1908 | int null_stdfds(void) | |
1909 | { | |
1910 | int ret = -1; | |
b14fc100 | 1911 | int fd; |
f8dd0275 | 1912 | |
b14fc100 | 1913 | fd = open_devnull(); |
f8dd0275 AM |
1914 | if (fd >= 0) { |
1915 | ret = set_stdfds(fd); | |
1916 | close(fd); | |
1917 | } | |
69aeabac | 1918 | |
69aeabac TA |
1919 | return ret; |
1920 | } | |
ccb4cabe SH |
1921 | |
1922 | /* | |
1923 | * Return the number of lines in file @fn, or -1 on error | |
1924 | */ | |
1925 | int lxc_count_file_lines(const char *fn) | |
1926 | { | |
1927 | FILE *f; | |
1928 | char *line = NULL; | |
1929 | size_t sz = 0; | |
1930 | int n = 0; | |
1931 | ||
1932 | f = fopen_cloexec(fn, "r"); | |
1933 | if (!f) | |
1934 | return -1; | |
1935 | ||
1936 | while (getline(&line, &sz, f) != -1) { | |
1937 | n++; | |
1938 | } | |
b14fc100 | 1939 | |
ccb4cabe SH |
1940 | free(line); |
1941 | fclose(f); | |
1942 | return n; | |
1943 | } | |
1adbd020 | 1944 | |
330ae3d3 | 1945 | /* Check whether a signal is blocked by a process. */ |
de3c491b | 1946 | /* /proc/pid-to-str/status\0 = (5 + 21 + 7 + 1) */ |
eabf1ea9 | 1947 | #define __PROC_STATUS_LEN (6 + (LXC_NUMSTRLEN64) + 7 + 1) |
573ad77f | 1948 | bool task_blocks_signal(pid_t pid, int signal) |
330ae3d3 | 1949 | { |
330ae3d3 | 1950 | int ret; |
de3c491b | 1951 | char status[__PROC_STATUS_LEN]; |
eabf1ea9 | 1952 | FILE *f; |
573ad77f | 1953 | uint64_t sigblk = 0, one = 1; |
eabf1ea9 CB |
1954 | size_t n = 0; |
1955 | bool bret = false; | |
1956 | char *line = NULL; | |
330ae3d3 | 1957 | |
de3c491b CB |
1958 | ret = snprintf(status, __PROC_STATUS_LEN, "/proc/%d/status", pid); |
1959 | if (ret < 0 || ret >= __PROC_STATUS_LEN) | |
330ae3d3 CB |
1960 | return bret; |
1961 | ||
1962 | f = fopen(status, "r"); | |
1963 | if (!f) | |
1964 | return bret; | |
1965 | ||
1966 | while (getline(&line, &n, f) != -1) { | |
573ad77f CB |
1967 | char *numstr; |
1968 | ||
eabf1ea9 | 1969 | if (strncmp(line, "SigBlk:", 7)) |
6fbcbe3b CB |
1970 | continue; |
1971 | ||
573ad77f CB |
1972 | numstr = lxc_trim_whitespace_in_place(line + 7); |
1973 | ret = lxc_safe_uint64(numstr, &sigblk, 16); | |
1974 | if (ret < 0) | |
6fbcbe3b | 1975 | goto out; |
573ad77f CB |
1976 | |
1977 | break; | |
330ae3d3 CB |
1978 | } |
1979 | ||
573ad77f | 1980 | if (sigblk & (one << (signal - 1))) |
330ae3d3 CB |
1981 | bret = true; |
1982 | ||
1983 | out: | |
1984 | free(line); | |
1985 | fclose(f); | |
1986 | return bret; | |
1987 | } | |
000dfda7 CB |
1988 | |
1989 | static int lxc_append_null_to_list(void ***list) | |
1990 | { | |
1991 | int newentry = 0; | |
1992 | void **tmp; | |
1993 | ||
1994 | if (*list) | |
1995 | for (; (*list)[newentry]; newentry++) { | |
1996 | ; | |
1997 | } | |
1998 | ||
1999 | tmp = realloc(*list, (newentry + 2) * sizeof(void **)); | |
2000 | if (!tmp) | |
2001 | return -1; | |
2002 | ||
2003 | *list = tmp; | |
2004 | (*list)[newentry + 1] = NULL; | |
2005 | ||
2006 | return newentry; | |
2007 | } | |
2008 | ||
2009 | int lxc_append_string(char ***list, char *entry) | |
2010 | { | |
000dfda7 | 2011 | char *copy; |
a54694f8 CB |
2012 | int newentry; |
2013 | ||
2014 | newentry = lxc_append_null_to_list((void ***)list); | |
2015 | if (newentry < 0) | |
2016 | return -1; | |
000dfda7 CB |
2017 | |
2018 | copy = strdup(entry); | |
2019 | if (!copy) | |
2020 | return -1; | |
2021 | ||
2022 | (*list)[newentry] = copy; | |
2023 | ||
2024 | return 0; | |
2025 | } | |
a687256f CB |
2026 | |
2027 | int lxc_preserve_ns(const int pid, const char *ns) | |
2028 | { | |
2029 | int ret; | |
a052913d CB |
2030 | /* 5 /proc + 21 /int_as_str + 3 /ns + 20 /NS_NAME + 1 \0 */ |
2031 | #define __NS_PATH_LEN 50 | |
2032 | char path[__NS_PATH_LEN]; | |
a687256f | 2033 | |
4d8ac866 CB |
2034 | /* This way we can use this function to also check whether namespaces |
2035 | * are supported by the kernel by passing in the NULL or the empty | |
2036 | * string. | |
2037 | */ | |
a052913d | 2038 | ret = snprintf(path, __NS_PATH_LEN, "/proc/%d/ns%s%s", pid, |
4d8ac866 CB |
2039 | !ns || strcmp(ns, "") == 0 ? "" : "/", |
2040 | !ns || strcmp(ns, "") == 0 ? "" : ns); | |
ea918412 | 2041 | if (ret < 0 || (size_t)ret >= __NS_PATH_LEN) { |
2042 | errno = EFBIG; | |
2043 | return -1; | |
2044 | } | |
a687256f CB |
2045 | |
2046 | return open(path, O_RDONLY | O_CLOEXEC); | |
2047 | } | |
6bc2eafe CB |
2048 | |
2049 | int lxc_safe_uint(const char *numstr, unsigned int *converted) | |
2050 | { | |
2051 | char *err = NULL; | |
2052 | unsigned long int uli; | |
2053 | ||
643c1984 CB |
2054 | while (isspace(*numstr)) |
2055 | numstr++; | |
2056 | ||
2057 | if (*numstr == '-') | |
2058 | return -EINVAL; | |
2059 | ||
6bc2eafe CB |
2060 | errno = 0; |
2061 | uli = strtoul(numstr, &err, 0); | |
643c1984 | 2062 | if (errno == ERANGE && uli == ULONG_MAX) |
ff0e49c7 | 2063 | return -ERANGE; |
6bc2eafe | 2064 | |
643c1984 | 2065 | if (err == numstr || *err != '\0') |
6bc2eafe CB |
2066 | return -EINVAL; |
2067 | ||
2068 | if (uli > UINT_MAX) | |
2069 | return -ERANGE; | |
2070 | ||
8c57d930 | 2071 | *converted = (unsigned int)uli; |
6bc2eafe CB |
2072 | return 0; |
2073 | } | |
b5f845e7 | 2074 | |
681188c1 CB |
2075 | int lxc_safe_ulong(const char *numstr, unsigned long *converted) |
2076 | { | |
2077 | char *err = NULL; | |
2078 | unsigned long int uli; | |
2079 | ||
2080 | while (isspace(*numstr)) | |
2081 | numstr++; | |
2082 | ||
2083 | if (*numstr == '-') | |
2084 | return -EINVAL; | |
2085 | ||
2086 | errno = 0; | |
2087 | uli = strtoul(numstr, &err, 0); | |
2088 | if (errno == ERANGE && uli == ULONG_MAX) | |
2089 | return -ERANGE; | |
2090 | ||
2091 | if (err == numstr || *err != '\0') | |
2092 | return -EINVAL; | |
2093 | ||
2094 | *converted = uli; | |
2095 | return 0; | |
2096 | } | |
2097 | ||
573ad77f | 2098 | int lxc_safe_uint64(const char *numstr, uint64_t *converted, int base) |
eacebcc3 FA |
2099 | { |
2100 | char *err = NULL; | |
2101 | uint64_t u; | |
2102 | ||
2103 | while (isspace(*numstr)) | |
2104 | numstr++; | |
2105 | ||
2106 | if (*numstr == '-') | |
2107 | return -EINVAL; | |
2108 | ||
2109 | errno = 0; | |
573ad77f | 2110 | u = strtoull(numstr, &err, base); |
eacebcc3 FA |
2111 | if (errno == ERANGE && u == ULLONG_MAX) |
2112 | return -ERANGE; | |
2113 | ||
2114 | if (err == numstr || *err != '\0') | |
2115 | return -EINVAL; | |
2116 | ||
2117 | *converted = u; | |
2118 | return 0; | |
2119 | } | |
2120 | ||
b5f845e7 CB |
2121 | int lxc_safe_int(const char *numstr, int *converted) |
2122 | { | |
2123 | char *err = NULL; | |
2124 | signed long int sli; | |
2125 | ||
2126 | errno = 0; | |
2127 | sli = strtol(numstr, &err, 0); | |
643c1984 | 2128 | if (errno == ERANGE && (sli == LONG_MAX || sli == LONG_MIN)) |
ff0e49c7 | 2129 | return -ERANGE; |
643c1984 CB |
2130 | |
2131 | if (errno != 0 && sli == 0) | |
ff0e49c7 | 2132 | return -EINVAL; |
b5f845e7 | 2133 | |
643c1984 | 2134 | if (err == numstr || *err != '\0') |
b5f845e7 CB |
2135 | return -EINVAL; |
2136 | ||
643c1984 | 2137 | if (sli > INT_MAX || sli < INT_MIN) |
b5f845e7 CB |
2138 | return -ERANGE; |
2139 | ||
2140 | *converted = (int)sli; | |
2141 | return 0; | |
2142 | } | |
8c57d930 CB |
2143 | |
2144 | int lxc_safe_long(const char *numstr, long int *converted) | |
2145 | { | |
2146 | char *err = NULL; | |
2147 | signed long int sli; | |
2148 | ||
2149 | errno = 0; | |
2150 | sli = strtol(numstr, &err, 0); | |
643c1984 | 2151 | if (errno == ERANGE && (sli == LONG_MAX || sli == LONG_MIN)) |
ff0e49c7 | 2152 | return -ERANGE; |
8c57d930 | 2153 | |
643c1984 | 2154 | if (errno != 0 && sli == 0) |
ff0e49c7 | 2155 | return -EINVAL; |
8c57d930 | 2156 | |
643c1984 CB |
2157 | if (err == numstr || *err != '\0') |
2158 | return -EINVAL; | |
8c57d930 CB |
2159 | |
2160 | *converted = sli; | |
2161 | return 0; | |
2162 | } | |
dbaf55a3 | 2163 | |
b037bc67 CB |
2164 | int lxc_safe_long_long(const char *numstr, long long int *converted) |
2165 | { | |
2166 | char *err = NULL; | |
2167 | signed long long int sli; | |
2168 | ||
2169 | errno = 0; | |
2170 | sli = strtoll(numstr, &err, 0); | |
2171 | if (errno == ERANGE && (sli == LLONG_MAX || sli == LLONG_MIN)) | |
2172 | return -ERANGE; | |
2173 | ||
2174 | if (errno != 0 && sli == 0) | |
2175 | return -EINVAL; | |
2176 | ||
2177 | if (err == numstr || *err != '\0') | |
2178 | return -EINVAL; | |
2179 | ||
2180 | *converted = sli; | |
2181 | return 0; | |
2182 | } | |
2183 | ||
dbaf55a3 CB |
2184 | int lxc_switch_uid_gid(uid_t uid, gid_t gid) |
2185 | { | |
2186 | if (setgid(gid) < 0) { | |
2187 | SYSERROR("Failed to switch to gid %d.", gid); | |
2188 | return -errno; | |
2189 | } | |
2190 | NOTICE("Switched to gid %d.", gid); | |
2191 | ||
2192 | if (setuid(uid) < 0) { | |
2193 | SYSERROR("Failed to switch to uid %d.", uid); | |
2194 | return -errno; | |
2195 | } | |
2196 | NOTICE("Switched to uid %d.", uid); | |
2197 | ||
2198 | return 0; | |
2199 | } | |
2200 | ||
2201 | /* Simple covenience function which enables uniform logging. */ | |
2202 | int lxc_setgroups(int size, gid_t list[]) | |
2203 | { | |
2204 | if (setgroups(size, list) < 0) { | |
2205 | SYSERROR("Failed to setgroups()."); | |
2206 | return -errno; | |
2207 | } | |
2208 | NOTICE("Dropped additional groups."); | |
2209 | ||
2210 | return 0; | |
2211 | } | |
c6868a1f CB |
2212 | |
2213 | static int lxc_get_unused_loop_dev_legacy(char *loop_name) | |
2214 | { | |
2215 | struct dirent *dp; | |
2216 | struct loop_info64 lo64; | |
2217 | DIR *dir; | |
2218 | int dfd = -1, fd = -1, ret = -1; | |
2219 | ||
2220 | dir = opendir("/dev"); | |
2221 | if (!dir) | |
2222 | return -1; | |
2223 | ||
2224 | while ((dp = readdir(dir))) { | |
c6868a1f CB |
2225 | if (strncmp(dp->d_name, "loop", 4) != 0) |
2226 | continue; | |
2227 | ||
2228 | dfd = dirfd(dir); | |
2229 | if (dfd < 0) | |
2230 | continue; | |
2231 | ||
2232 | fd = openat(dfd, dp->d_name, O_RDWR); | |
2233 | if (fd < 0) | |
2234 | continue; | |
2235 | ||
2236 | ret = ioctl(fd, LOOP_GET_STATUS64, &lo64); | |
2237 | if (ret < 0) { | |
2238 | if (ioctl(fd, LOOP_GET_STATUS64, &lo64) == 0 || | |
2239 | errno != ENXIO) { | |
2240 | close(fd); | |
2241 | fd = -1; | |
2242 | continue; | |
2243 | } | |
2244 | } | |
2245 | ||
2246 | ret = snprintf(loop_name, LO_NAME_SIZE, "/dev/%s", dp->d_name); | |
2247 | if (ret < 0 || ret >= LO_NAME_SIZE) { | |
2248 | close(fd); | |
2249 | fd = -1; | |
2250 | continue; | |
2251 | } | |
2252 | ||
2253 | break; | |
2254 | } | |
2255 | ||
2256 | closedir(dir); | |
2257 | ||
2258 | if (fd < 0) | |
2259 | return -1; | |
2260 | ||
2261 | return fd; | |
2262 | } | |
2263 | ||
2264 | static int lxc_get_unused_loop_dev(char *name_loop) | |
2265 | { | |
2266 | int loop_nr, ret; | |
2267 | int fd_ctl = -1, fd_tmp = -1; | |
2268 | ||
2269 | fd_ctl = open("/dev/loop-control", O_RDWR | O_CLOEXEC); | |
2270 | if (fd_ctl < 0) | |
2271 | return -ENODEV; | |
2272 | ||
2273 | loop_nr = ioctl(fd_ctl, LOOP_CTL_GET_FREE); | |
2274 | if (loop_nr < 0) | |
2275 | goto on_error; | |
2276 | ||
2277 | ret = snprintf(name_loop, LO_NAME_SIZE, "/dev/loop%d", loop_nr); | |
2278 | if (ret < 0 || ret >= LO_NAME_SIZE) | |
2279 | goto on_error; | |
2280 | ||
2281 | fd_tmp = open(name_loop, O_RDWR | O_CLOEXEC); | |
2282 | if (fd_tmp < 0) | |
2283 | goto on_error; | |
2284 | ||
2285 | on_error: | |
2286 | close(fd_ctl); | |
2287 | return fd_tmp; | |
2288 | } | |
2289 | ||
2290 | int lxc_prepare_loop_dev(const char *source, char *loop_dev, int flags) | |
2291 | { | |
2292 | int ret; | |
2293 | struct loop_info64 lo64; | |
2294 | int fd_img = -1, fret = -1, fd_loop = -1; | |
2295 | ||
2296 | fd_loop = lxc_get_unused_loop_dev(loop_dev); | |
2297 | if (fd_loop < 0) { | |
2298 | if (fd_loop == -ENODEV) | |
2299 | fd_loop = lxc_get_unused_loop_dev_legacy(loop_dev); | |
2300 | else | |
2301 | goto on_error; | |
2302 | } | |
2303 | ||
2304 | fd_img = open(source, O_RDWR | O_CLOEXEC); | |
2305 | if (fd_img < 0) | |
2306 | goto on_error; | |
2307 | ||
2308 | ret = ioctl(fd_loop, LOOP_SET_FD, fd_img); | |
2309 | if (ret < 0) | |
2310 | goto on_error; | |
2311 | ||
2312 | memset(&lo64, 0, sizeof(lo64)); | |
2313 | lo64.lo_flags = flags; | |
2314 | ||
2315 | ret = ioctl(fd_loop, LOOP_SET_STATUS64, &lo64); | |
2316 | if (ret < 0) | |
2317 | goto on_error; | |
2318 | ||
2319 | fret = 0; | |
2320 | ||
2321 | on_error: | |
2322 | if (fd_img >= 0) | |
2323 | close(fd_img); | |
2324 | ||
2325 | if (fret < 0 && fd_loop >= 0) { | |
2326 | close(fd_loop); | |
2327 | fd_loop = -1; | |
2328 | } | |
2329 | ||
2330 | return fd_loop; | |
2331 | } | |
74251e49 CB |
2332 | |
2333 | int lxc_unstack_mountpoint(const char *path, bool lazy) | |
2334 | { | |
2335 | int ret; | |
2336 | int umounts = 0; | |
2337 | ||
2338 | pop_stack: | |
2339 | ret = umount2(path, lazy ? MNT_DETACH : 0); | |
2340 | if (ret < 0) { | |
2341 | /* We consider anything else than EINVAL deadly to prevent going | |
2342 | * into an infinite loop. (The other alternative is constantly | |
2343 | * parsing /proc/self/mountinfo which is yucky and probably | |
2344 | * racy.) | |
2345 | */ | |
2346 | if (errno != EINVAL) | |
2347 | return -errno; | |
2348 | } else { | |
b4a40f7b CB |
2349 | /* Just stop counting when this happens. That'd just be so |
2350 | * stupid that we won't even bother trying to report back the | |
2351 | * correct value anymore. | |
2352 | */ | |
2353 | if (umounts != INT_MAX) | |
2354 | umounts++; | |
b14fc100 | 2355 | |
74251e49 CB |
2356 | /* We succeeded in umounting. Make sure that there's no other |
2357 | * mountpoint stacked underneath. | |
2358 | */ | |
74251e49 CB |
2359 | goto pop_stack; |
2360 | } | |
2361 | ||
2362 | return umounts; | |
2363 | } | |
ea3a694f CB |
2364 | |
2365 | int run_command(char *buf, size_t buf_size, int (*child_fn)(void *), void *args) | |
2366 | { | |
2367 | pid_t child; | |
2368 | int ret, fret, pipefd[2]; | |
2369 | ssize_t bytes; | |
2370 | ||
46210729 | 2371 | /* Make sure our callers do not receive uninitialized memory. */ |
ea3a694f CB |
2372 | if (buf_size > 0 && buf) |
2373 | buf[0] = '\0'; | |
2374 | ||
2375 | if (pipe(pipefd) < 0) { | |
2376 | SYSERROR("failed to create pipe"); | |
2377 | return -1; | |
2378 | } | |
2379 | ||
2d728b2f | 2380 | child = lxc_raw_clone(0); |
ea3a694f CB |
2381 | if (child < 0) { |
2382 | close(pipefd[0]); | |
2383 | close(pipefd[1]); | |
2384 | SYSERROR("failed to create new process"); | |
2385 | return -1; | |
2386 | } | |
2387 | ||
2388 | if (child == 0) { | |
2389 | /* Close the read-end of the pipe. */ | |
2390 | close(pipefd[0]); | |
2391 | ||
2392 | /* Redirect std{err,out} to write-end of the | |
2393 | * pipe. | |
2394 | */ | |
2395 | ret = dup2(pipefd[1], STDOUT_FILENO); | |
2396 | if (ret >= 0) | |
2397 | ret = dup2(pipefd[1], STDERR_FILENO); | |
2398 | ||
2399 | /* Close the write-end of the pipe. */ | |
2400 | close(pipefd[1]); | |
2401 | ||
2402 | if (ret < 0) { | |
2403 | SYSERROR("failed to duplicate std{err,out} file descriptor"); | |
d8b3f9c3 | 2404 | _exit(EXIT_FAILURE); |
ea3a694f CB |
2405 | } |
2406 | ||
2407 | /* Does not return. */ | |
2408 | child_fn(args); | |
2409 | ERROR("failed to exec command"); | |
d8b3f9c3 | 2410 | _exit(EXIT_FAILURE); |
ea3a694f CB |
2411 | } |
2412 | ||
2413 | /* close the write-end of the pipe */ | |
2414 | close(pipefd[1]); | |
2415 | ||
7a643c7c CB |
2416 | if (buf && buf_size > 0) { |
2417 | bytes = read(pipefd[0], buf, buf_size - 1); | |
2418 | if (bytes > 0) | |
2419 | buf[bytes - 1] = '\0'; | |
2420 | } | |
ea3a694f CB |
2421 | |
2422 | fret = wait_for_pid(child); | |
2423 | /* close the read-end of the pipe */ | |
2424 | close(pipefd[0]); | |
2425 | ||
2426 | return fret; | |
2427 | } | |
04ad7ffe | 2428 | |
eb5c2e6a WB |
2429 | char *must_concat(const char *first, ...) |
2430 | { | |
2431 | va_list args; | |
2432 | char *cur, *dest; | |
2433 | size_t cur_len, it_len; | |
2434 | ||
2435 | dest = must_copy_string(first); | |
2436 | cur_len = it_len = strlen(first); | |
2437 | ||
2438 | va_start(args, first); | |
2439 | while ((cur = va_arg(args, char *)) != NULL) { | |
2440 | it_len = strlen(cur); | |
2441 | ||
2442 | dest = must_realloc(dest, cur_len + it_len + 1); | |
2443 | ||
2444 | (void)memcpy(dest + cur_len, cur, it_len); | |
2445 | cur_len += it_len; | |
2446 | } | |
2447 | va_end(args); | |
2448 | ||
2449 | dest[cur_len] = 0; | |
2450 | return dest; | |
2451 | } | |
2452 | ||
04ad7ffe CB |
2453 | char *must_make_path(const char *first, ...) |
2454 | { | |
2455 | va_list args; | |
2456 | char *cur, *dest; | |
2457 | size_t full_len = strlen(first); | |
bd583214 | 2458 | size_t buf_len; |
04ad7ffe CB |
2459 | |
2460 | dest = must_copy_string(first); | |
2461 | ||
2462 | va_start(args, first); | |
2463 | while ((cur = va_arg(args, char *)) != NULL) { | |
2464 | full_len += strlen(cur); | |
2465 | if (cur[0] != '/') | |
2466 | full_len++; | |
efed99a4 | 2467 | |
bd583214 DJ |
2468 | buf_len = full_len + 1; |
2469 | dest = must_realloc(dest, buf_len); | |
efed99a4 | 2470 | |
04ad7ffe | 2471 | if (cur[0] != '/') |
bd583214 DJ |
2472 | (void)strlcat(dest, "/", buf_len); |
2473 | (void)strlcat(dest, cur, buf_len); | |
04ad7ffe CB |
2474 | } |
2475 | va_end(args); | |
2476 | ||
2477 | return dest; | |
2478 | } | |
2479 | ||
0c3deb94 CB |
2480 | char *must_append_path(char *first, ...) |
2481 | { | |
2482 | char *cur; | |
2483 | size_t full_len; | |
2484 | va_list args; | |
2485 | char *dest = first; | |
bd583214 | 2486 | size_t buf_len; |
0c3deb94 CB |
2487 | |
2488 | full_len = strlen(first); | |
2489 | va_start(args, first); | |
2490 | while ((cur = va_arg(args, char *)) != NULL) { | |
2491 | full_len += strlen(cur); | |
0c3deb94 CB |
2492 | if (cur[0] != '/') |
2493 | full_len++; | |
2494 | ||
bd583214 DJ |
2495 | buf_len = full_len + 1; |
2496 | dest = must_realloc(dest, buf_len); | |
0c3deb94 CB |
2497 | |
2498 | if (cur[0] != '/') | |
bd583214 DJ |
2499 | (void)strlcat(dest, "/", buf_len); |
2500 | (void)strlcat(dest, cur, buf_len); | |
0c3deb94 CB |
2501 | } |
2502 | va_end(args); | |
2503 | ||
2504 | return dest; | |
2505 | } | |
2506 | ||
04ad7ffe CB |
2507 | char *must_copy_string(const char *entry) |
2508 | { | |
2509 | char *ret; | |
2510 | ||
2511 | if (!entry) | |
2512 | return NULL; | |
b14fc100 | 2513 | |
04ad7ffe CB |
2514 | do { |
2515 | ret = strdup(entry); | |
2516 | } while (!ret); | |
2517 | ||
2518 | return ret; | |
2519 | } | |
2520 | ||
2521 | void *must_realloc(void *orig, size_t sz) | |
2522 | { | |
2523 | void *ret; | |
2524 | ||
2525 | do { | |
2526 | ret = realloc(orig, sz); | |
2527 | } while (!ret); | |
2528 | ||
2529 | return ret; | |
2530 | } | |
a035c53a CB |
2531 | |
2532 | bool is_fs_type(const struct statfs *fs, fs_type_magic magic_val) | |
2533 | { | |
2534 | return (fs->f_type == (fs_type_magic)magic_val); | |
2535 | } | |
2536 | ||
2537 | bool has_fs_type(const char *path, fs_type_magic magic_val) | |
2538 | { | |
2539 | bool has_type; | |
2540 | int ret; | |
2541 | struct statfs sb; | |
2542 | ||
2543 | ret = statfs(path, &sb); | |
2544 | if (ret < 0) | |
2545 | return false; | |
2546 | ||
2547 | has_type = is_fs_type(&sb, magic_val); | |
2548 | if (!has_type && magic_val == RAMFS_MAGIC) | |
2549 | WARN("When the ramfs it a tmpfs statfs() might report tmpfs"); | |
2550 | ||
2551 | return has_type; | |
2552 | } | |
d75c14e2 CB |
2553 | |
2554 | bool lxc_nic_exists(char *nic) | |
2555 | { | |
2556 | #define __LXC_SYS_CLASS_NET_LEN 15 + IFNAMSIZ + 1 | |
2557 | char path[__LXC_SYS_CLASS_NET_LEN]; | |
2558 | int ret; | |
2559 | struct stat sb; | |
2560 | ||
2561 | if (!strcmp(nic, "none")) | |
2562 | return true; | |
2563 | ||
2564 | ret = snprintf(path, __LXC_SYS_CLASS_NET_LEN, "/sys/class/net/%s", nic); | |
2565 | if (ret < 0 || (size_t)ret >= __LXC_SYS_CLASS_NET_LEN) | |
2566 | return false; | |
2567 | ||
2568 | ret = stat(path, &sb); | |
2569 | if (ret < 0) | |
2570 | return false; | |
2571 | ||
2572 | return true; | |
2573 | } | |
127c6e70 CB |
2574 | |
2575 | int lxc_make_tmpfile(char *template, bool rm) | |
2576 | { | |
2577 | int fd, ret; | |
c3b818a0 | 2578 | mode_t msk; |
127c6e70 | 2579 | |
c3b818a0 | 2580 | msk = umask(0022); |
127c6e70 | 2581 | fd = mkstemp(template); |
c3b818a0 | 2582 | umask(msk); |
127c6e70 CB |
2583 | if (fd < 0) |
2584 | return -1; | |
2585 | ||
2586 | if (!rm) | |
2587 | return fd; | |
2588 | ||
2589 | ret = unlink(template); | |
2590 | if (ret < 0) { | |
2591 | close(fd); | |
2592 | return -1; | |
2593 | } | |
2594 | ||
2595 | return fd; | |
2596 | } | |
e4636123 | 2597 | |
e3db0162 CB |
2598 | int parse_byte_size_string(const char *s, int64_t *converted) |
2599 | { | |
2600 | int ret, suffix_len; | |
2601 | long long int conv; | |
2602 | int64_t mltpl, overflow; | |
2603 | char *end; | |
2604 | char dup[LXC_NUMSTRLEN64 + 2]; | |
a13560af | 2605 | char suffix[3] = {0}; |
e3db0162 CB |
2606 | |
2607 | if (!s || !strcmp(s, "")) | |
2608 | return -EINVAL; | |
2609 | ||
ccd42a31 | 2610 | end = stpncpy(dup, s, sizeof(dup) - 1); |
e3db0162 CB |
2611 | if (*end != '\0') |
2612 | return -EINVAL; | |
2613 | ||
2614 | if (isdigit(*(end - 1))) | |
2615 | suffix_len = 0; | |
2616 | else if (isalpha(*(end - 1))) | |
2617 | suffix_len = 1; | |
2618 | else | |
2619 | return -EINVAL; | |
2620 | ||
c2229b24 | 2621 | if (suffix_len > 0 && (end - 2) == dup && !isdigit(*(end - 2))) |
e3db0162 CB |
2622 | return -EINVAL; |
2623 | ||
358b8c81 CB |
2624 | if (suffix_len > 0 && isalpha(*(end - 2))) |
2625 | suffix_len++; | |
e3db0162 CB |
2626 | |
2627 | if (suffix_len > 0) { | |
2628 | memcpy(suffix, end - suffix_len, suffix_len); | |
2629 | *(suffix + suffix_len) = '\0'; | |
2630 | *(end - suffix_len) = '\0'; | |
2631 | } | |
2632 | dup[lxc_char_right_gc(dup, strlen(dup))] = '\0'; | |
2633 | ||
2634 | ret = lxc_safe_long_long(dup, &conv); | |
2635 | if (ret < 0) | |
2636 | return -ret; | |
2637 | ||
2638 | if (suffix_len != 2) { | |
2639 | *converted = conv; | |
2640 | return 0; | |
2641 | } | |
2642 | ||
6d276edc | 2643 | if (strcasecmp(suffix, "KB") == 0) |
e3db0162 | 2644 | mltpl = 1024; |
6d276edc | 2645 | else if (strcasecmp(suffix, "MB") == 0) |
e3db0162 | 2646 | mltpl = 1024 * 1024; |
6d276edc | 2647 | else if (strcasecmp(suffix, "GB") == 0) |
e3db0162 CB |
2648 | mltpl = 1024 * 1024 * 1024; |
2649 | else | |
2650 | return -EINVAL; | |
2651 | ||
2652 | overflow = conv * mltpl; | |
2653 | if (conv != 0 && (overflow / conv) != mltpl) | |
2654 | return -ERANGE; | |
2655 | ||
2656 | *converted = overflow; | |
2657 | return 0; | |
2658 | } | |
6222c3f4 CB |
2659 | |
2660 | uint64_t lxc_find_next_power2(uint64_t n) | |
2661 | { | |
2662 | /* 0 is not valid input. We return 0 to the caller since 0 is not a | |
2663 | * valid power of two. | |
2664 | */ | |
2665 | if (n == 0) | |
2666 | return 0; | |
2667 | ||
2668 | if (!(n & (n - 1))) | |
2669 | return n; | |
2670 | ||
2671 | while (n & (n - 1)) | |
2672 | n = n & (n - 1); | |
2673 | ||
2674 | n = n << 1; | |
2675 | return n; | |
2676 | } | |
1fd0f41e CB |
2677 | |
2678 | int lxc_set_death_signal(int signal) | |
2679 | { | |
2680 | int ret; | |
2681 | pid_t ppid; | |
2682 | ||
2683 | ret = prctl(PR_SET_PDEATHSIG, signal, 0, 0, 0); | |
2684 | ||
2685 | /* Check whether we have been orphaned. */ | |
2686 | ppid = (pid_t)syscall(SYS_getppid); | |
2687 | if (ppid == 1) { | |
2688 | pid_t self; | |
2689 | ||
2690 | self = lxc_raw_getpid(); | |
2691 | ret = kill(self, SIGKILL); | |
2692 | if (ret < 0) | |
2693 | return -1; | |
2694 | } | |
2695 | ||
2696 | if (ret < 0) { | |
2697 | SYSERROR("Failed to set PR_SET_PDEATHSIG to %d", signal); | |
2698 | return -1; | |
2699 | } | |
2700 | ||
2701 | return 0; | |
2702 | } | |
7ad37670 CB |
2703 | |
2704 | void remove_trailing_newlines(char *l) | |
2705 | { | |
2706 | char *p = l; | |
2707 | ||
2708 | while (*p) | |
2709 | p++; | |
2710 | ||
2711 | while (--p >= l && *p == '\n') | |
2712 | *p = '\0'; | |
2713 | } | |
a9d4ebc1 CB |
2714 | |
2715 | int fd_cloexec(int fd, bool cloexec) | |
2716 | { | |
2717 | int oflags, nflags; | |
2718 | ||
2719 | oflags = fcntl(fd, F_GETFD, 0); | |
2720 | if (oflags < 0) | |
2721 | return -errno; | |
2722 | ||
2723 | if (cloexec) | |
2724 | nflags = oflags | FD_CLOEXEC; | |
2725 | else | |
2726 | nflags = oflags & ~FD_CLOEXEC; | |
2727 | ||
2728 | if (nflags == oflags) | |
2729 | return 0; | |
2730 | ||
2731 | if (fcntl(fd, F_SETFD, nflags) < 0) | |
2732 | return -errno; | |
2733 | ||
2734 | return 0; | |
2735 | } | |
d7ab0375 | 2736 | |
2737 | int recursive_destroy(char *dirname) | |
2738 | { | |
2739 | int ret; | |
2740 | struct dirent *direntp; | |
2741 | DIR *dir; | |
2742 | int r = 0; | |
2743 | ||
2744 | dir = opendir(dirname); | |
2745 | if (!dir) | |
2746 | return -1; | |
2747 | ||
2748 | while ((direntp = readdir(dir))) { | |
2749 | char *pathname; | |
2750 | struct stat mystat; | |
2751 | ||
2752 | if (!strcmp(direntp->d_name, ".") || | |
2753 | !strcmp(direntp->d_name, "..")) | |
2754 | continue; | |
2755 | ||
2756 | pathname = must_make_path(dirname, direntp->d_name, NULL); | |
2757 | ||
2758 | ret = lstat(pathname, &mystat); | |
2759 | if (ret < 0) { | |
2760 | if (!r) | |
2761 | WARN("Failed to stat \"%s\"", pathname); | |
2762 | ||
2763 | r = -1; | |
2764 | goto next; | |
2765 | } | |
2766 | ||
2767 | if (!S_ISDIR(mystat.st_mode)) | |
2768 | goto next; | |
2769 | ||
2770 | ret = recursive_destroy(pathname); | |
2771 | if (ret < 0) | |
2772 | r = -1; | |
2773 | ||
2774 | next: | |
2775 | free(pathname); | |
2776 | } | |
2777 | ||
2778 | ret = rmdir(dirname); | |
2779 | if (ret < 0) { | |
2780 | if (!r) | |
2781 | SYSWARN("Failed to delete \"%s\"", dirname); | |
2782 | ||
2783 | r = -1; | |
2784 | } | |
2785 | ||
2786 | ret = closedir(dir); | |
2787 | if (ret < 0) { | |
2788 | if (!r) | |
2789 | SYSWARN("Failed to delete \"%s\"", dirname); | |
2790 | ||
2791 | r = -1; | |
2792 | } | |
2793 | ||
2794 | return r; | |
2795 | } |