]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/utils.c
lxc-destroy: actually work if underlying fs is overlayfs
[mirror_lxc.git] / src / lxc / utils.c
1 /*
2 * lxc: linux Container library
3 *
4 * (C) Copyright IBM Corp. 2007, 2008
5 *
6 * Authors:
7 * Daniel Lezcano <daniel.lezcano at free.fr>
8 *
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 #include "config.h"
25
26 #include <errno.h>
27 #include <unistd.h>
28 #include <stdlib.h>
29 #include <stddef.h>
30 #include <string.h>
31 #include <sys/types.h>
32 #include <sys/vfs.h>
33 #include <sys/stat.h>
34 #include <sys/mman.h>
35 #include <sys/param.h>
36 #include <sys/mount.h>
37 #include <dirent.h>
38 #include <fcntl.h>
39 #include <libgen.h>
40 #include <sys/types.h>
41 #include <sys/wait.h>
42 #include <assert.h>
43 #include <sys/prctl.h>
44
45 #include "utils.h"
46 #include "log.h"
47 #include "lxclock.h"
48 #include "namespace.h"
49
50 #ifndef PR_SET_MM
51 #define PR_SET_MM 35
52 #endif
53
54 #ifndef PR_SET_MM_ARG_START
55 #define PR_SET_MM_ARG_START 8
56 #endif
57
58 #ifndef PR_SET_MM_ARG_END
59 #define PR_SET_MM_ARG_END 9
60 #endif
61
62 #ifndef PR_SET_MM_ENV_START
63 #define PR_SET_MM_ENV_START 10
64 #endif
65
66 #ifndef PR_SET_MM_ENV_END
67 #define PR_SET_MM_ENV_END 11
68 #endif
69
70 lxc_log_define(lxc_utils, lxc);
71
72 static int _recursive_rmdir(char *dirname, dev_t pdev,
73 const char *exclude, int level, bool onedev)
74 {
75 struct dirent dirent, *direntp;
76 DIR *dir;
77 int ret, failed=0;
78 char pathname[MAXPATHLEN];
79 bool hadexclude = false;
80
81 dir = opendir(dirname);
82 if (!dir) {
83 ERROR("%s: failed to open %s", __func__, dirname);
84 return -1;
85 }
86
87 while (!readdir_r(dir, &dirent, &direntp)) {
88 struct stat mystat;
89 int rc;
90
91 if (!direntp)
92 break;
93
94 if (!strcmp(direntp->d_name, ".") ||
95 !strcmp(direntp->d_name, ".."))
96 continue;
97
98 rc = snprintf(pathname, MAXPATHLEN, "%s/%s", dirname, direntp->d_name);
99 if (rc < 0 || rc >= MAXPATHLEN) {
100 ERROR("pathname too long");
101 failed=1;
102 continue;
103 }
104
105 if (!level && exclude && !strcmp(direntp->d_name, exclude)) {
106 ret = rmdir(pathname);
107 if (ret < 0) {
108 switch(errno) {
109 case ENOTEMPTY:
110 INFO("Not deleting snapshot %s", pathname);
111 hadexclude = true;
112 break;
113 case ENOTDIR:
114 ret = unlink(pathname);
115 if (ret)
116 INFO("%s: failed to remove %s", __func__, pathname);
117 break;
118 default:
119 SYSERROR("%s: failed to rmdir %s", __func__, pathname);
120 failed = 1;
121 break;
122 }
123 }
124 continue;
125 }
126
127 ret = lstat(pathname, &mystat);
128 if (ret) {
129 ERROR("%s: failed to stat %s", __func__, pathname);
130 failed=1;
131 continue;
132 }
133 if (onedev && mystat.st_dev != pdev)
134 continue;
135 if (S_ISDIR(mystat.st_mode)) {
136 if (_recursive_rmdir(pathname, pdev, exclude, level+1, onedev) < 0)
137 failed=1;
138 } else {
139 if (unlink(pathname) < 0) {
140 SYSERROR("%s: failed to delete %s", __func__, pathname);
141 failed=1;
142 }
143 }
144 }
145
146 if (rmdir(dirname) < 0) {
147 if (!hadexclude) {
148 ERROR("%s: failed to delete %s", __func__, dirname);
149 failed=1;
150 }
151 }
152
153 ret = closedir(dir);
154 if (ret) {
155 ERROR("%s: failed to close directory %s", __func__, dirname);
156 failed=1;
157 }
158
159 return failed ? -1 : 0;
160 }
161
162 /* we have two different magic values for overlayfs, yay */
163 #define OVERLAYFS_SUPER_MAGIC 0x794c764f
164 #define OVERLAY_SUPER_MAGIC 0x794c7630
165 /*
166 * In overlayfs, st_dev is unreliable. so on overlayfs we don't do
167 * the lxc_rmdir_onedev()
168 */
169 static bool is_native_overlayfs(const char *path)
170 {
171 struct statfs sb;
172
173 if (statfs(path, &sb) < 0)
174 return false;
175 if (sb.f_type == OVERLAYFS_SUPER_MAGIC ||
176 sb.f_type == OVERLAY_SUPER_MAGIC)
177 return true;
178 return false;
179 }
180
181 /* returns 0 on success, -1 if there were any failures */
182 extern int lxc_rmdir_onedev(char *path, const char *exclude)
183 {
184 struct stat mystat;
185 bool onedev = true;
186
187 if (is_native_overlayfs(path)) {
188 onedev = false;
189 }
190
191 if (lstat(path, &mystat) < 0) {
192 ERROR("%s: failed to stat %s", __func__, path);
193 return -1;
194 }
195
196 return _recursive_rmdir(path, mystat.st_dev, exclude, 0, onedev);
197 }
198
199 static int mount_fs(const char *source, const char *target, const char *type)
200 {
201 /* the umount may fail */
202 if (umount(target))
203 WARN("failed to unmount %s : %s", target, strerror(errno));
204
205 if (mount(source, target, type, 0, NULL)) {
206 ERROR("failed to mount %s : %s", target, strerror(errno));
207 return -1;
208 }
209
210 DEBUG("'%s' mounted on '%s'", source, target);
211
212 return 0;
213 }
214
215 extern void lxc_setup_fs(void)
216 {
217 if (mount_fs("proc", "/proc", "proc"))
218 INFO("failed to remount proc");
219
220 /* if we can't mount /dev/shm, continue anyway */
221 if (mount_fs("shmfs", "/dev/shm", "tmpfs"))
222 INFO("failed to mount /dev/shm");
223
224 /* If we were able to mount /dev/shm, then /dev exists */
225 /* Sure, but it's read-only per config :) */
226 if (access("/dev/mqueue", F_OK) && mkdir("/dev/mqueue", 0666)) {
227 DEBUG("failed to create '/dev/mqueue'");
228 return;
229 }
230
231 /* continue even without posix message queue support */
232 if (mount_fs("mqueue", "/dev/mqueue", "mqueue"))
233 INFO("failed to mount /dev/mqueue");
234 }
235
236 /* borrowed from iproute2 */
237 extern int get_u16(unsigned short *val, const char *arg, int base)
238 {
239 unsigned long res;
240 char *ptr;
241
242 if (!arg || !*arg)
243 return -1;
244
245 errno = 0;
246 res = strtoul(arg, &ptr, base);
247 if (!ptr || ptr == arg || *ptr || res > 0xFFFF || errno != 0)
248 return -1;
249
250 *val = res;
251
252 return 0;
253 }
254
255 extern int mkdir_p(const char *dir, mode_t mode)
256 {
257 const char *tmp = dir;
258 const char *orig = dir;
259 char *makeme;
260
261 do {
262 dir = tmp + strspn(tmp, "/");
263 tmp = dir + strcspn(dir, "/");
264 makeme = strndup(orig, dir - orig);
265 if (*makeme) {
266 if (mkdir(makeme, mode) && errno != EEXIST) {
267 SYSERROR("failed to create directory '%s'", makeme);
268 free(makeme);
269 return -1;
270 }
271 }
272 free(makeme);
273 } while(tmp != dir);
274
275 return 0;
276 }
277
278 extern void remove_trailing_slashes(char *p)
279 {
280 int l = strlen(p);
281 while (--l >= 0 && (p[l] == '/' || p[l] == '\n'))
282 p[l] = '\0';
283 }
284
285 static char *copy_global_config_value(char *p)
286 {
287 int len = strlen(p);
288 char *retbuf;
289
290 if (len < 1)
291 return NULL;
292 if (p[len-1] == '\n') {
293 p[len-1] = '\0';
294 len--;
295 }
296 retbuf = malloc(len+1);
297 if (!retbuf)
298 return NULL;
299 strcpy(retbuf, p);
300 return retbuf;
301 }
302
303 #define DEFAULT_VG "lxc"
304 #define DEFAULT_THIN_POOL "lxc"
305 #define DEFAULT_ZFSROOT "lxc"
306
307 const char *lxc_global_config_value(const char *option_name)
308 {
309 static const char * const options[][2] = {
310 { "lxc.bdev.lvm.vg", DEFAULT_VG },
311 { "lxc.bdev.lvm.thin_pool", DEFAULT_THIN_POOL },
312 { "lxc.bdev.zfs.root", DEFAULT_ZFSROOT },
313 { "lxc.lxcpath", NULL },
314 { "lxc.default_config", NULL },
315 { "lxc.cgroup.pattern", NULL },
316 { "lxc.cgroup.use", NULL },
317 { NULL, NULL },
318 };
319
320 /* placed in the thread local storage pool for non-bionic targets */
321 #ifdef HAVE_TLS
322 static __thread const char *values[sizeof(options) / sizeof(options[0])] = { 0 };
323 #else
324 static const char *values[sizeof(options) / sizeof(options[0])] = { 0 };
325 #endif
326
327 /* user_config_path is freed as soon as it is used */
328 char *user_config_path = NULL;
329
330 /*
331 * The following variables are freed at bottom unconditionally.
332 * So NULL the value if it is to be returned to the caller
333 */
334 char *user_default_config_path = NULL;
335 char *user_lxc_path = NULL;
336 char *user_cgroup_pattern = NULL;
337
338 if (geteuid() > 0) {
339 const char *user_home = getenv("HOME");
340 if (!user_home)
341 user_home = "/";
342
343 user_config_path = malloc(sizeof(char) * (22 + strlen(user_home)));
344 user_default_config_path = malloc(sizeof(char) * (26 + strlen(user_home)));
345 user_lxc_path = malloc(sizeof(char) * (19 + strlen(user_home)));
346
347 sprintf(user_config_path, "%s/.config/lxc/lxc.conf", user_home);
348 sprintf(user_default_config_path, "%s/.config/lxc/default.conf", user_home);
349 sprintf(user_lxc_path, "%s/.local/share/lxc/", user_home);
350 user_cgroup_pattern = strdup("lxc/%n");
351 }
352 else {
353 user_config_path = strdup(LXC_GLOBAL_CONF);
354 user_default_config_path = strdup(LXC_DEFAULT_CONFIG);
355 user_lxc_path = strdup(LXCPATH);
356 user_cgroup_pattern = strdup(DEFAULT_CGROUP_PATTERN);
357 }
358
359 const char * const (*ptr)[2];
360 size_t i;
361 char buf[1024], *p, *p2;
362 FILE *fin = NULL;
363
364 for (i = 0, ptr = options; (*ptr)[0]; ptr++, i++) {
365 if (!strcmp(option_name, (*ptr)[0]))
366 break;
367 }
368 if (!(*ptr)[0]) {
369 free(user_config_path);
370 free(user_default_config_path);
371 free(user_lxc_path);
372 free(user_cgroup_pattern);
373 errno = EINVAL;
374 return NULL;
375 }
376
377 if (values[i]) {
378 free(user_config_path);
379 free(user_default_config_path);
380 free(user_lxc_path);
381 free(user_cgroup_pattern);
382 return values[i];
383 }
384
385 fin = fopen_cloexec(user_config_path, "r");
386 free(user_config_path);
387 if (fin) {
388 while (fgets(buf, 1024, fin)) {
389 if (buf[0] == '#')
390 continue;
391 p = strstr(buf, option_name);
392 if (!p)
393 continue;
394 /* see if there was just white space in front
395 * of the option name
396 */
397 for (p2 = buf; p2 < p; p2++) {
398 if (*p2 != ' ' && *p2 != '\t')
399 break;
400 }
401 if (p2 < p)
402 continue;
403 p = strchr(p, '=');
404 if (!p)
405 continue;
406 /* see if there was just white space after
407 * the option name
408 */
409 for (p2 += strlen(option_name); p2 < p; p2++) {
410 if (*p2 != ' ' && *p2 != '\t')
411 break;
412 }
413 if (p2 < p)
414 continue;
415 p++;
416 while (*p && (*p == ' ' || *p == '\t')) p++;
417 if (!*p)
418 continue;
419
420 if (strcmp(option_name, "lxc.lxcpath") == 0) {
421 free(user_lxc_path);
422 user_lxc_path = copy_global_config_value(p);
423 remove_trailing_slashes(user_lxc_path);
424 values[i] = user_lxc_path;
425 user_lxc_path = NULL;
426 goto out;
427 }
428
429 values[i] = copy_global_config_value(p);
430 goto out;
431 }
432 }
433 /* could not find value, use default */
434 if (strcmp(option_name, "lxc.lxcpath") == 0) {
435 remove_trailing_slashes(user_lxc_path);
436 values[i] = user_lxc_path;
437 user_lxc_path = NULL;
438 }
439 else if (strcmp(option_name, "lxc.default_config") == 0) {
440 values[i] = user_default_config_path;
441 user_default_config_path = NULL;
442 }
443 else if (strcmp(option_name, "lxc.cgroup.pattern") == 0) {
444 values[i] = user_cgroup_pattern;
445 user_cgroup_pattern = NULL;
446 }
447 else
448 values[i] = (*ptr)[1];
449
450 /* special case: if default value is NULL,
451 * and there is no config, don't view that
452 * as an error... */
453 if (!values[i])
454 errno = 0;
455
456 out:
457 if (fin)
458 fclose(fin);
459
460 free(user_cgroup_pattern);
461 free(user_default_config_path);
462 free(user_lxc_path);
463
464 return values[i];
465 }
466
467 char *get_rundir()
468 {
469 char *rundir;
470 const char *homedir;
471
472 if (geteuid() == 0) {
473 rundir = strdup(RUNTIME_PATH);
474 return rundir;
475 }
476
477 rundir = getenv("XDG_RUNTIME_DIR");
478 if (rundir) {
479 rundir = strdup(rundir);
480 return rundir;
481 }
482
483 INFO("XDG_RUNTIME_DIR isn't set in the environment.");
484 homedir = getenv("HOME");
485 if (!homedir) {
486 ERROR("HOME isn't set in the environment.");
487 return NULL;
488 }
489
490 rundir = malloc(sizeof(char) * (17 + strlen(homedir)));
491 sprintf(rundir, "%s/.cache/lxc/run/", homedir);
492
493 return rundir;
494 }
495
496 int wait_for_pid(pid_t pid)
497 {
498 int status, ret;
499
500 again:
501 ret = waitpid(pid, &status, 0);
502 if (ret == -1) {
503 if (errno == EINTR)
504 goto again;
505 return -1;
506 }
507 if (ret != pid)
508 goto again;
509 if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
510 return -1;
511 return 0;
512 }
513
514 int lxc_wait_for_pid_status(pid_t pid)
515 {
516 int status, ret;
517
518 again:
519 ret = waitpid(pid, &status, 0);
520 if (ret == -1) {
521 if (errno == EINTR)
522 goto again;
523 return -1;
524 }
525 if (ret != pid)
526 goto again;
527 return status;
528 }
529
530 ssize_t lxc_write_nointr(int fd, const void* buf, size_t count)
531 {
532 ssize_t ret;
533 again:
534 ret = write(fd, buf, count);
535 if (ret < 0 && errno == EINTR)
536 goto again;
537 return ret;
538 }
539
540 ssize_t lxc_read_nointr(int fd, void* buf, size_t count)
541 {
542 ssize_t ret;
543 again:
544 ret = read(fd, buf, count);
545 if (ret < 0 && errno == EINTR)
546 goto again;
547 return ret;
548 }
549
550 ssize_t lxc_read_nointr_expect(int fd, void* buf, size_t count, const void* expected_buf)
551 {
552 ssize_t ret;
553 ret = lxc_read_nointr(fd, buf, count);
554 if (ret <= 0)
555 return ret;
556 if ((size_t)ret != count)
557 return -1;
558 if (expected_buf && memcmp(buf, expected_buf, count) != 0) {
559 errno = EINVAL;
560 return -1;
561 }
562 return ret;
563 }
564
565 #if HAVE_LIBGNUTLS
566 #include <gnutls/gnutls.h>
567 #include <gnutls/crypto.h>
568
569 __attribute__((constructor))
570 static void gnutls_lxc_init(void)
571 {
572 gnutls_global_init();
573 }
574
575 int sha1sum_file(char *fnam, unsigned char *digest)
576 {
577 char *buf;
578 int ret;
579 FILE *f;
580 long flen;
581
582 if (!fnam)
583 return -1;
584 f = fopen_cloexec(fnam, "r");
585 if (!f) {
586 SYSERROR("Error opening template");
587 return -1;
588 }
589 if (fseek(f, 0, SEEK_END) < 0) {
590 SYSERROR("Error seeking to end of template");
591 fclose(f);
592 return -1;
593 }
594 if ((flen = ftell(f)) < 0) {
595 SYSERROR("Error telling size of template");
596 fclose(f);
597 return -1;
598 }
599 if (fseek(f, 0, SEEK_SET) < 0) {
600 SYSERROR("Error seeking to start of template");
601 fclose(f);
602 return -1;
603 }
604 if ((buf = malloc(flen+1)) == NULL) {
605 SYSERROR("Out of memory");
606 fclose(f);
607 return -1;
608 }
609 if (fread(buf, 1, flen, f) != flen) {
610 SYSERROR("Failure reading template");
611 free(buf);
612 fclose(f);
613 return -1;
614 }
615 if (fclose(f) < 0) {
616 SYSERROR("Failre closing template");
617 free(buf);
618 return -1;
619 }
620 buf[flen] = '\0';
621 ret = gnutls_hash_fast(GNUTLS_DIG_SHA1, buf, flen, (void *)digest);
622 free(buf);
623 return ret;
624 }
625 #endif
626
627 char** lxc_va_arg_list_to_argv(va_list ap, size_t skip, int do_strdup)
628 {
629 va_list ap2;
630 size_t count = 1 + skip;
631 char **result;
632
633 /* first determine size of argument list, we don't want to reallocate
634 * constantly...
635 */
636 va_copy(ap2, ap);
637 while (1) {
638 char* arg = va_arg(ap2, char*);
639 if (!arg)
640 break;
641 count++;
642 }
643 va_end(ap2);
644
645 result = calloc(count, sizeof(char*));
646 if (!result)
647 return NULL;
648 count = skip;
649 while (1) {
650 char* arg = va_arg(ap, char*);
651 if (!arg)
652 break;
653 arg = do_strdup ? strdup(arg) : arg;
654 if (!arg)
655 goto oom;
656 result[count++] = arg;
657 }
658
659 /* calloc has already set last element to NULL*/
660 return result;
661
662 oom:
663 free(result);
664 return NULL;
665 }
666
667 const char** lxc_va_arg_list_to_argv_const(va_list ap, size_t skip)
668 {
669 return (const char**)lxc_va_arg_list_to_argv(ap, skip, 0);
670 }
671
672 FILE *fopen_cloexec(const char *path, const char *mode)
673 {
674 int open_mode = 0;
675 int step = 0;
676 int fd;
677 int saved_errno = 0;
678 FILE *ret;
679
680 if (!strncmp(mode, "r+", 2)) {
681 open_mode = O_RDWR;
682 step = 2;
683 } else if (!strncmp(mode, "r", 1)) {
684 open_mode = O_RDONLY;
685 step = 1;
686 } else if (!strncmp(mode, "w+", 2)) {
687 open_mode = O_RDWR | O_TRUNC | O_CREAT;
688 step = 2;
689 } else if (!strncmp(mode, "w", 1)) {
690 open_mode = O_WRONLY | O_TRUNC | O_CREAT;
691 step = 1;
692 } else if (!strncmp(mode, "a+", 2)) {
693 open_mode = O_RDWR | O_CREAT | O_APPEND;
694 step = 2;
695 } else if (!strncmp(mode, "a", 1)) {
696 open_mode = O_WRONLY | O_CREAT | O_APPEND;
697 step = 1;
698 }
699 for (; mode[step]; step++)
700 if (mode[step] == 'x')
701 open_mode |= O_EXCL;
702 open_mode |= O_CLOEXEC;
703
704 fd = open(path, open_mode, 0666);
705 if (fd < 0)
706 return NULL;
707
708 ret = fdopen(fd, mode);
709 saved_errno = errno;
710 if (!ret)
711 close(fd);
712 errno = saved_errno;
713 return ret;
714 }
715
716 extern struct lxc_popen_FILE *lxc_popen(const char *command)
717 {
718 struct lxc_popen_FILE *fp = NULL;
719 int parent_end = -1, child_end = -1;
720 int pipe_fds[2];
721 pid_t child_pid;
722
723 int r = pipe2(pipe_fds, O_CLOEXEC);
724
725 if (r < 0) {
726 ERROR("pipe2 failure");
727 return NULL;
728 }
729
730 parent_end = pipe_fds[0];
731 child_end = pipe_fds[1];
732
733 child_pid = fork();
734
735 if (child_pid == 0) {
736 /* child */
737 int child_std_end = STDOUT_FILENO;
738
739 if (child_end != child_std_end) {
740 /* dup2() doesn't dup close-on-exec flag */
741 dup2(child_end, child_std_end);
742
743 /* it's safe not to close child_end here
744 * as it's marked close-on-exec anyway
745 */
746 } else {
747 /*
748 * The descriptor is already the one we will use.
749 * But it must not be marked close-on-exec.
750 * Undo the effects.
751 */
752 if (fcntl(child_end, F_SETFD, 0) != 0) {
753 SYSERROR("Failed to remove FD_CLOEXEC from fd.");
754 exit(127);
755 }
756 }
757
758 /*
759 * Unblock signals.
760 * This is the main/only reason
761 * why we do our lousy popen() emulation.
762 */
763 {
764 sigset_t mask;
765 sigfillset(&mask);
766 sigprocmask(SIG_UNBLOCK, &mask, NULL);
767 }
768
769 execl("/bin/sh", "sh", "-c", command, (char *) NULL);
770 exit(127);
771 }
772
773 /* parent */
774
775 close(child_end);
776 child_end = -1;
777
778 if (child_pid < 0) {
779 ERROR("fork failure");
780 goto error;
781 }
782
783 fp = calloc(1, sizeof(*fp));
784 if (!fp) {
785 ERROR("failed to allocate memory");
786 goto error;
787 }
788
789 fp->f = fdopen(parent_end, "r");
790 if (!fp->f) {
791 ERROR("fdopen failure");
792 goto error;
793 }
794
795 fp->child_pid = child_pid;
796
797 return fp;
798
799 error:
800
801 if (fp) {
802 if (fp->f) {
803 fclose(fp->f);
804 parent_end = -1; /* so we do not close it second time */
805 }
806
807 free(fp);
808 }
809
810 if (parent_end != -1)
811 close(parent_end);
812
813 return NULL;
814 }
815
816 extern int lxc_pclose(struct lxc_popen_FILE *fp)
817 {
818 FILE *f = NULL;
819 pid_t child_pid = 0;
820 int wstatus = 0;
821 pid_t wait_pid;
822
823 if (fp) {
824 f = fp->f;
825 child_pid = fp->child_pid;
826 /* free memory (we still need to close file stream) */
827 free(fp);
828 fp = NULL;
829 }
830
831 if (!f || fclose(f)) {
832 ERROR("fclose failure");
833 return -1;
834 }
835
836 do {
837 wait_pid = waitpid(child_pid, &wstatus, 0);
838 } while (wait_pid == -1 && errno == EINTR);
839
840 if (wait_pid == -1) {
841 ERROR("waitpid failure");
842 return -1;
843 }
844
845 return wstatus;
846 }
847
848 char *lxc_string_replace(const char *needle, const char *replacement, const char *haystack)
849 {
850 ssize_t len = -1, saved_len = -1;
851 char *result = NULL;
852 size_t replacement_len = strlen(replacement);
853 size_t needle_len = strlen(needle);
854
855 /* should be executed exactly twice */
856 while (len == -1 || result == NULL) {
857 char *p;
858 char *last_p;
859 ssize_t part_len;
860
861 if (len != -1) {
862 result = calloc(1, len + 1);
863 if (!result)
864 return NULL;
865 saved_len = len;
866 }
867
868 len = 0;
869
870 for (last_p = (char *)haystack, p = strstr(last_p, needle); p; last_p = p, p = strstr(last_p, needle)) {
871 part_len = (ssize_t)(p - last_p);
872 if (result && part_len > 0)
873 memcpy(&result[len], last_p, part_len);
874 len += part_len;
875 if (result && replacement_len > 0)
876 memcpy(&result[len], replacement, replacement_len);
877 len += replacement_len;
878 p += needle_len;
879 }
880 part_len = strlen(last_p);
881 if (result && part_len > 0)
882 memcpy(&result[len], last_p, part_len);
883 len += part_len;
884 }
885
886 /* make sure we did the same thing twice,
887 * once for calculating length, the other
888 * time for copying data */
889 assert(saved_len == len);
890 /* make sure we didn't overwrite any buffer,
891 * due to calloc the string should be 0-terminated */
892 assert(result[len] == '\0');
893
894 return result;
895 }
896
897 bool lxc_string_in_array(const char *needle, const char **haystack)
898 {
899 for (; haystack && *haystack; haystack++)
900 if (!strcmp(needle, *haystack))
901 return true;
902 return false;
903 }
904
905 char *lxc_string_join(const char *sep, const char **parts, bool use_as_prefix)
906 {
907 char *result;
908 char **p;
909 size_t sep_len = strlen(sep);
910 size_t result_len = use_as_prefix * sep_len;
911
912 /* calculate new string length */
913 for (p = (char **)parts; *p; p++)
914 result_len += (p > (char **)parts) * sep_len + strlen(*p);
915
916 result = calloc(result_len + 1, 1);
917 if (!result)
918 return NULL;
919
920 if (use_as_prefix)
921 strcpy(result, sep);
922 for (p = (char **)parts; *p; p++) {
923 if (p > (char **)parts)
924 strcat(result, sep);
925 strcat(result, *p);
926 }
927
928 return result;
929 }
930
931 char **lxc_normalize_path(const char *path)
932 {
933 char **components;
934 char **p;
935 size_t components_len = 0;
936 size_t pos = 0;
937
938 components = lxc_string_split(path, '/');
939 if (!components)
940 return NULL;
941 for (p = components; *p; p++)
942 components_len++;
943
944 /* resolve '.' and '..' */
945 for (pos = 0; pos < components_len; ) {
946 if (!strcmp(components[pos], ".") || (!strcmp(components[pos], "..") && pos == 0)) {
947 /* eat this element */
948 free(components[pos]);
949 memmove(&components[pos], &components[pos+1], sizeof(char *) * (components_len - pos));
950 components_len--;
951 } else if (!strcmp(components[pos], "..")) {
952 /* eat this and the previous element */
953 free(components[pos - 1]);
954 free(components[pos]);
955 memmove(&components[pos-1], &components[pos+1], sizeof(char *) * (components_len - pos));
956 components_len -= 2;
957 pos--;
958 } else {
959 pos++;
960 }
961 }
962
963 return components;
964 }
965
966 char *lxc_append_paths(const char *first, const char *second)
967 {
968 size_t len = strlen(first) + strlen(second) + 1;
969 const char *pattern = "%s%s";
970 char *result = NULL;
971
972 if (second[0] != '/') {
973 len += 1;
974 pattern = "%s/%s";
975 }
976
977 result = calloc(1, len);
978 if (!result)
979 return NULL;
980
981 snprintf(result, len, pattern, first, second);
982 return result;
983 }
984
985 bool lxc_string_in_list(const char *needle, const char *haystack, char _sep)
986 {
987 char *token, *str, *saveptr = NULL;
988 char sep[2] = { _sep, '\0' };
989
990 if (!haystack || !needle)
991 return 0;
992
993 str = alloca(strlen(haystack)+1);
994 strcpy(str, haystack);
995 for (; (token = strtok_r(str, sep, &saveptr)); str = NULL) {
996 if (strcmp(needle, token) == 0)
997 return 1;
998 }
999
1000 return 0;
1001 }
1002
1003 char **lxc_string_split(const char *string, char _sep)
1004 {
1005 char *token, *str, *saveptr = NULL;
1006 char sep[2] = { _sep, '\0' };
1007 char **result = NULL;
1008 size_t result_capacity = 0;
1009 size_t result_count = 0;
1010 int r, saved_errno;
1011
1012 if (!string)
1013 return calloc(1, sizeof(char *));
1014
1015 str = alloca(strlen(string)+1);
1016 strcpy(str, string);
1017 for (; (token = strtok_r(str, sep, &saveptr)); str = NULL) {
1018 r = lxc_grow_array((void ***)&result, &result_capacity, result_count + 1, 16);
1019 if (r < 0)
1020 goto error_out;
1021 result[result_count] = strdup(token);
1022 if (!result[result_count])
1023 goto error_out;
1024 result_count++;
1025 }
1026
1027 /* if we allocated too much, reduce it */
1028 return realloc(result, (result_count + 1) * sizeof(char *));
1029 error_out:
1030 saved_errno = errno;
1031 lxc_free_array((void **)result, free);
1032 errno = saved_errno;
1033 return NULL;
1034 }
1035
1036 char **lxc_string_split_and_trim(const char *string, char _sep)
1037 {
1038 char *token, *str, *saveptr = NULL;
1039 char sep[2] = { _sep, '\0' };
1040 char **result = NULL;
1041 size_t result_capacity = 0;
1042 size_t result_count = 0;
1043 int r, saved_errno;
1044 size_t i = 0;
1045
1046 if (!string)
1047 return calloc(1, sizeof(char *));
1048
1049 str = alloca(strlen(string)+1);
1050 strcpy(str, string);
1051 for (; (token = strtok_r(str, sep, &saveptr)); str = NULL) {
1052 while (token[0] == ' ' || token[0] == '\t')
1053 token++;
1054 i = strlen(token);
1055 while (i > 0 && (token[i - 1] == ' ' || token[i - 1] == '\t')) {
1056 token[i - 1] = '\0';
1057 i--;
1058 }
1059 r = lxc_grow_array((void ***)&result, &result_capacity, result_count + 1, 16);
1060 if (r < 0)
1061 goto error_out;
1062 result[result_count] = strdup(token);
1063 if (!result[result_count])
1064 goto error_out;
1065 result_count++;
1066 }
1067
1068 /* if we allocated too much, reduce it */
1069 return realloc(result, (result_count + 1) * sizeof(char *));
1070 error_out:
1071 saved_errno = errno;
1072 lxc_free_array((void **)result, free);
1073 errno = saved_errno;
1074 return NULL;
1075 }
1076
1077 void lxc_free_array(void **array, lxc_free_fn element_free_fn)
1078 {
1079 void **p;
1080 for (p = array; p && *p; p++)
1081 element_free_fn(*p);
1082 free((void*)array);
1083 }
1084
1085 int lxc_grow_array(void ***array, size_t* capacity, size_t new_size, size_t capacity_increment)
1086 {
1087 size_t new_capacity;
1088 void **new_array;
1089
1090 /* first time around, catch some trivial mistakes of the user
1091 * only initializing one of these */
1092 if (!*array || !*capacity) {
1093 *array = NULL;
1094 *capacity = 0;
1095 }
1096
1097 new_capacity = *capacity;
1098 while (new_size + 1 > new_capacity)
1099 new_capacity += capacity_increment;
1100 if (new_capacity != *capacity) {
1101 /* we have to reallocate */
1102 new_array = realloc(*array, new_capacity * sizeof(void *));
1103 if (!new_array)
1104 return -1;
1105 memset(&new_array[*capacity], 0, (new_capacity - (*capacity)) * sizeof(void *));
1106 *array = new_array;
1107 *capacity = new_capacity;
1108 }
1109
1110 /* array has sufficient elements */
1111 return 0;
1112 }
1113
1114 size_t lxc_array_len(void **array)
1115 {
1116 void **p;
1117 size_t result = 0;
1118
1119 for (p = array; p && *p; p++)
1120 result++;
1121
1122 return result;
1123 }
1124
1125 int lxc_write_to_file(const char *filename, const void* buf, size_t count, bool add_newline)
1126 {
1127 int fd, saved_errno;
1128 ssize_t ret;
1129
1130 fd = open(filename, O_WRONLY | O_TRUNC | O_CREAT | O_CLOEXEC, 0666);
1131 if (fd < 0)
1132 return -1;
1133 ret = lxc_write_nointr(fd, buf, count);
1134 if (ret < 0)
1135 goto out_error;
1136 if ((size_t)ret != count)
1137 goto out_error;
1138 if (add_newline) {
1139 ret = lxc_write_nointr(fd, "\n", 1);
1140 if (ret != 1)
1141 goto out_error;
1142 }
1143 close(fd);
1144 return 0;
1145
1146 out_error:
1147 saved_errno = errno;
1148 close(fd);
1149 errno = saved_errno;
1150 return -1;
1151 }
1152
1153 int lxc_read_from_file(const char *filename, void* buf, size_t count)
1154 {
1155 int fd = -1, saved_errno;
1156 ssize_t ret;
1157
1158 fd = open(filename, O_RDONLY | O_CLOEXEC);
1159 if (fd < 0)
1160 return -1;
1161
1162 if (!buf || !count) {
1163 char buf2[100];
1164 size_t count2 = 0;
1165 while ((ret = read(fd, buf2, 100)) > 0)
1166 count2 += ret;
1167 if (ret >= 0)
1168 ret = count2;
1169 } else {
1170 memset(buf, 0, count);
1171 ret = read(fd, buf, count);
1172 }
1173
1174 if (ret < 0)
1175 ERROR("read %s: %s", filename, strerror(errno));
1176
1177 saved_errno = errno;
1178 close(fd);
1179 errno = saved_errno;
1180 return ret;
1181 }
1182
1183 void **lxc_append_null_to_array(void **array, size_t count)
1184 {
1185 void **temp;
1186
1187 /* Append NULL to the array */
1188 if (count) {
1189 temp = realloc(array, (count + 1) * sizeof(*array));
1190 if (!temp) {
1191 int i;
1192 for (i = 0; i < count; i++)
1193 free(array[i]);
1194 free(array);
1195 return NULL;
1196 }
1197 array = temp;
1198 array[count] = NULL;
1199 }
1200 return array;
1201 }
1202
1203 int randseed(bool srand_it)
1204 {
1205 /*
1206 srand pre-seed function based on /dev/urandom
1207 */
1208 unsigned int seed=time(NULL)+getpid();
1209
1210 FILE *f;
1211 f = fopen("/dev/urandom", "r");
1212 if (f) {
1213 int ret = fread(&seed, sizeof(seed), 1, f);
1214 if (ret != 1)
1215 DEBUG("unable to fread /dev/urandom, %s, fallback to time+pid rand seed", strerror(errno));
1216 fclose(f);
1217 }
1218
1219 if (srand_it)
1220 srand(seed);
1221
1222 return seed;
1223 }
1224
1225 uid_t get_ns_uid(uid_t orig)
1226 {
1227 char *line = NULL;
1228 size_t sz = 0;
1229 uid_t nsid, hostid, range;
1230 FILE *f = fopen("/proc/self/uid_map", "r");
1231 if (!f)
1232 return 0;
1233
1234 while (getline(&line, &sz, f) != -1) {
1235 if (sscanf(line, "%u %u %u", &nsid, &hostid, &range) != 3)
1236 continue;
1237 if (hostid <= orig && hostid + range > orig) {
1238 nsid += orig - hostid;
1239 goto found;
1240 }
1241 }
1242
1243 nsid = 0;
1244 found:
1245 fclose(f);
1246 free(line);
1247 return nsid;
1248 }
1249
1250 bool dir_exists(const char *path)
1251 {
1252 struct stat sb;
1253 int ret;
1254
1255 ret = stat(path, &sb);
1256 if (ret < 0)
1257 // could be something other than eexist, just say no
1258 return false;
1259 return S_ISDIR(sb.st_mode);
1260 }
1261
1262 /* Note we don't use SHA-1 here as we don't want to depend on HAVE_GNUTLS.
1263 * FNV has good anti collision properties and we're not worried
1264 * about pre-image resistance or one-way-ness, we're just trying to make
1265 * the name unique in the 108 bytes of space we have.
1266 */
1267 uint64_t fnv_64a_buf(void *buf, size_t len, uint64_t hval)
1268 {
1269 unsigned char *bp;
1270
1271 for(bp = buf; bp < (unsigned char *)buf + len; bp++)
1272 {
1273 /* xor the bottom with the current octet */
1274 hval ^= (uint64_t)*bp;
1275
1276 /* gcc optimised:
1277 * multiply by the 64 bit FNV magic prime mod 2^64
1278 */
1279 hval += (hval << 1) + (hval << 4) + (hval << 5) +
1280 (hval << 7) + (hval << 8) + (hval << 40);
1281 }
1282
1283 return hval;
1284 }
1285
1286 /*
1287 * Detect whether / is mounted MS_SHARED. The only way I know of to
1288 * check that is through /proc/self/mountinfo.
1289 * I'm only checking for /. If the container rootfs or mount location
1290 * is MS_SHARED, but not '/', then you're out of luck - figuring that
1291 * out would be too much work to be worth it.
1292 */
1293 #define LINELEN 4096
1294 int detect_shared_rootfs(void)
1295 {
1296 char buf[LINELEN], *p;
1297 FILE *f;
1298 int i;
1299 char *p2;
1300
1301 f = fopen("/proc/self/mountinfo", "r");
1302 if (!f)
1303 return 0;
1304 while (fgets(buf, LINELEN, f)) {
1305 for (p = buf, i=0; p && i < 4; i++)
1306 p = strchr(p+1, ' ');
1307 if (!p)
1308 continue;
1309 p2 = strchr(p+1, ' ');
1310 if (!p2)
1311 continue;
1312 *p2 = '\0';
1313 if (strcmp(p+1, "/") == 0) {
1314 // this is '/'. is it shared?
1315 p = strchr(p2+1, ' ');
1316 if (p && strstr(p, "shared:")) {
1317 fclose(f);
1318 return 1;
1319 }
1320 }
1321 }
1322 fclose(f);
1323 return 0;
1324 }
1325
1326 bool switch_to_ns(pid_t pid, const char *ns) {
1327 int fd, ret;
1328 char nspath[MAXPATHLEN];
1329
1330 /* Switch to new ns */
1331 ret = snprintf(nspath, MAXPATHLEN, "/proc/%d/ns/%s", pid, ns);
1332 if (ret < 0 || ret >= MAXPATHLEN)
1333 return false;
1334
1335 fd = open(nspath, O_RDONLY);
1336 if (fd < 0) {
1337 SYSERROR("failed to open %s", nspath);
1338 return false;
1339 }
1340
1341 ret = setns(fd, 0);
1342 if (ret) {
1343 SYSERROR("failed to set process %d to %s of %d.", pid, ns, fd);
1344 close(fd);
1345 return false;
1346 }
1347 close(fd);
1348 return true;
1349 }
1350
1351 /*
1352 * looking at fs/proc_namespace.c, it appears we can
1353 * actually expect the rootfs entry to very specifically contain
1354 * " - rootfs rootfs "
1355 * IIUC, so long as we've chrooted so that rootfs is not our root,
1356 * the rootfs entry should always be skipped in mountinfo contents.
1357 */
1358 int detect_ramfs_rootfs(void)
1359 {
1360 char buf[LINELEN], *p;
1361 FILE *f;
1362 int i;
1363 char *p2;
1364
1365 f = fopen("/proc/self/mountinfo", "r");
1366 if (!f)
1367 return 0;
1368 while (fgets(buf, LINELEN, f)) {
1369 for (p = buf, i=0; p && i < 4; i++)
1370 p = strchr(p+1, ' ');
1371 if (!p)
1372 continue;
1373 p2 = strchr(p+1, ' ');
1374 if (!p2)
1375 continue;
1376 *p2 = '\0';
1377 if (strcmp(p+1, "/") == 0) {
1378 // this is '/'. is it the ramfs?
1379 p = strchr(p2+1, '-');
1380 if (p && strncmp(p, "- rootfs rootfs ", 16) == 0) {
1381 fclose(f);
1382 return 1;
1383 }
1384 }
1385 }
1386 fclose(f);
1387 return 0;
1388 }
1389
1390 char *on_path(char *cmd, const char *rootfs) {
1391 char *path = NULL;
1392 char *entry = NULL;
1393 char *saveptr = NULL;
1394 char cmdpath[MAXPATHLEN];
1395 int ret;
1396
1397 path = getenv("PATH");
1398 if (!path)
1399 return NULL;
1400
1401 path = strdup(path);
1402 if (!path)
1403 return NULL;
1404
1405 entry = strtok_r(path, ":", &saveptr);
1406 while (entry) {
1407 if (rootfs)
1408 ret = snprintf(cmdpath, MAXPATHLEN, "%s/%s/%s", rootfs, entry, cmd);
1409 else
1410 ret = snprintf(cmdpath, MAXPATHLEN, "%s/%s", entry, cmd);
1411
1412 if (ret < 0 || ret >= MAXPATHLEN)
1413 goto next_loop;
1414
1415 if (access(cmdpath, X_OK) == 0) {
1416 free(path);
1417 return strdup(cmdpath);
1418 }
1419
1420 next_loop:
1421 entry = strtok_r(NULL, ":", &saveptr);
1422 }
1423
1424 free(path);
1425 return NULL;
1426 }
1427
1428 bool file_exists(const char *f)
1429 {
1430 struct stat statbuf;
1431
1432 return stat(f, &statbuf) == 0;
1433 }
1434
1435 /* historically lxc-init has been under /usr/lib/lxc and under
1436 * /usr/lib/$ARCH/lxc. It now lives as $prefix/sbin/init.lxc.
1437 */
1438 char *choose_init(const char *rootfs)
1439 {
1440 char *retv = NULL;
1441 const char *empty = "",
1442 *tmp;
1443 int ret, env_set = 0;
1444 struct stat mystat;
1445
1446 if (!getenv("PATH")) {
1447 if (setenv("PATH", "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", 0))
1448 SYSERROR("Failed to setenv");
1449 env_set = 1;
1450 }
1451
1452 retv = on_path("init.lxc", rootfs);
1453
1454 if (env_set) {
1455 if (unsetenv("PATH"))
1456 SYSERROR("Failed to unsetenv");
1457 }
1458
1459 if (retv)
1460 return retv;
1461
1462 retv = malloc(PATH_MAX);
1463 if (!retv)
1464 return NULL;
1465
1466 if (rootfs)
1467 tmp = rootfs;
1468 else
1469 tmp = empty;
1470
1471 ret = snprintf(retv, PATH_MAX, "%s/%s/%s", tmp, SBINDIR, "/init.lxc");
1472 if (ret < 0 || ret >= PATH_MAX) {
1473 ERROR("pathname too long");
1474 goto out1;
1475 }
1476
1477 ret = stat(retv, &mystat);
1478 if (ret == 0)
1479 return retv;
1480
1481 ret = snprintf(retv, PATH_MAX, "%s/%s/%s", tmp, LXCINITDIR, "/lxc/lxc-init");
1482 if (ret < 0 || ret >= PATH_MAX) {
1483 ERROR("pathname too long");
1484 goto out1;
1485 }
1486
1487 ret = stat(retv, &mystat);
1488 if (ret == 0)
1489 return retv;
1490
1491 ret = snprintf(retv, PATH_MAX, "%s/usr/lib/lxc/lxc-init", tmp);
1492 if (ret < 0 || ret >= PATH_MAX) {
1493 ERROR("pathname too long");
1494 goto out1;
1495 }
1496 ret = stat(retv, &mystat);
1497 if (ret == 0)
1498 return retv;
1499
1500 ret = snprintf(retv, PATH_MAX, "%s/sbin/lxc-init", tmp);
1501 if (ret < 0 || ret >= PATH_MAX) {
1502 ERROR("pathname too long");
1503 goto out1;
1504 }
1505 ret = stat(retv, &mystat);
1506 if (ret == 0)
1507 return retv;
1508
1509 /*
1510 * Last resort, look for the statically compiled init.lxc which we
1511 * hopefully bind-mounted in.
1512 * If we are called during container setup, and we get to this point,
1513 * then the init.lxc.static from the host will need to be bind-mounted
1514 * in. So we return NULL here to indicate that.
1515 */
1516 if (rootfs)
1517 goto out1;
1518
1519 ret = snprintf(retv, PATH_MAX, "/init.lxc.static");
1520 if (ret < 0 || ret >= PATH_MAX) {
1521 WARN("Nonsense - name /lxc.init.static too long");
1522 goto out1;
1523 }
1524 ret = stat(retv, &mystat);
1525 if (ret == 0)
1526 return retv;
1527
1528 out1:
1529 free(retv);
1530 return NULL;
1531 }
1532
1533 int print_to_file(const char *file, const char *content)
1534 {
1535 FILE *f;
1536 int ret = 0;
1537
1538 f = fopen(file, "w");
1539 if (!f)
1540 return -1;
1541 if (fprintf(f, "%s", content) != strlen(content))
1542 ret = -1;
1543 fclose(f);
1544 return ret;
1545 }
1546
1547 int is_dir(const char *path)
1548 {
1549 struct stat statbuf;
1550 int ret = stat(path, &statbuf);
1551 if (ret == 0 && S_ISDIR(statbuf.st_mode))
1552 return 1;
1553 return 0;
1554 }
1555
1556 /*
1557 * Given the '-t' template option to lxc-create, figure out what to
1558 * do. If the template is a full executable path, use that. If it
1559 * is something like 'sshd', then return $templatepath/lxc-sshd.
1560 * On success return the template, on error return NULL.
1561 */
1562 char *get_template_path(const char *t)
1563 {
1564 int ret, len;
1565 char *tpath;
1566
1567 if (t[0] == '/' && access(t, X_OK) == 0) {
1568 tpath = strdup(t);
1569 return tpath;
1570 }
1571
1572 len = strlen(LXCTEMPLATEDIR) + strlen(t) + strlen("/lxc-") + 1;
1573 tpath = malloc(len);
1574 if (!tpath)
1575 return NULL;
1576 ret = snprintf(tpath, len, "%s/lxc-%s", LXCTEMPLATEDIR, t);
1577 if (ret < 0 || ret >= len) {
1578 free(tpath);
1579 return NULL;
1580 }
1581 if (access(tpath, X_OK) < 0) {
1582 SYSERROR("bad template: %s", t);
1583 free(tpath);
1584 return NULL;
1585 }
1586
1587 return tpath;
1588 }
1589
1590 /*
1591 * Sets the process title to the specified title. Note:
1592 * 1. this function requires root to succeed
1593 * 2. it clears /proc/self/environ
1594 * 3. it may not succed (e.g. if title is longer than /proc/self/environ +
1595 * the original title)
1596 */
1597 int setproctitle(char *title)
1598 {
1599 char buf[2048], *tmp;
1600 FILE *f;
1601 int i, len, ret = 0;
1602 unsigned long arg_start, arg_end, env_start, env_end;
1603
1604 f = fopen_cloexec("/proc/self/stat", "r");
1605 if (!f) {
1606 return -1;
1607 }
1608
1609 tmp = fgets(buf, sizeof(buf), f);
1610 fclose(f);
1611 if (!tmp) {
1612 return -1;
1613 }
1614
1615 /* Skip the first 47 fields, column 48-51 are ARG_START and
1616 * ARG_END. */
1617 tmp = strchr(buf, ' ');
1618 for (i = 0; i < 46; i++) {
1619 if (!tmp)
1620 return -1;
1621 tmp = strchr(tmp+1, ' ');
1622 }
1623
1624 if (!tmp)
1625 return -1;
1626
1627 i = sscanf(tmp, "%lu %lu %lu %lu", &arg_start, &arg_end, &env_start, &env_end);
1628 if (i != 4) {
1629 return -1;
1630 }
1631
1632 /* Include the null byte here, because in the calculations below we
1633 * want to have room for it. */
1634 len = strlen(title) + 1;
1635
1636 /* We're truncating the environment, so we should use at most the
1637 * length of the argument + environment for the title. */
1638 if (len > env_end - arg_start) {
1639 arg_end = env_end;
1640 len = env_end - arg_start;
1641 } else {
1642 /* Only truncate the environment if we're actually going to
1643 * overwrite part of it. */
1644 if (len >= arg_end - arg_start) {
1645 env_start = env_end;
1646 }
1647 arg_end = arg_start + len;
1648 }
1649
1650 strcpy((char*)arg_start, title);
1651
1652 ret |= prctl(PR_SET_MM, PR_SET_MM_ARG_START, (long)arg_start, 0, 0);
1653 ret |= prctl(PR_SET_MM, PR_SET_MM_ARG_END, (long)arg_end, 0, 0);
1654 ret |= prctl(PR_SET_MM, PR_SET_MM_ENV_START, (long)env_start, 0, 0);
1655 ret |= prctl(PR_SET_MM, PR_SET_MM_ENV_END, (long)env_end, 0, 0);
1656
1657 return ret;
1658 }