-static bool mkdir_p(const char *dir, mode_t mode)
-{
- const char *tmp = dir;
- const char *orig = dir;
- char *makeme;
-
- do {
- dir = tmp + strspn(tmp, "/");
- tmp = dir + strcspn(dir, "/");
- makeme = strndup(orig, dir - orig);
- if (!makeme)
- return false;
- if (mkdir(makeme, mode) && errno != EEXIST) {
- fprintf(stderr, "failed to create directory '%s': %s",
- makeme, strerror(errno));
- free(makeme);
- return false;
- }
- free(makeme);
- } while(tmp != dir);
-
- return true;
-}
-
-static bool umount_if_mounted(void)
-{
- if (umount2(basedir, MNT_DETACH) < 0 && errno != EINVAL) {
- fprintf(stderr, "failed to umount %s: %s\n", basedir,
- strerror(errno));
- return false;
- }
- return true;
-}
-
-static bool setup_cgfs_dir(void)
-{
- if (!mkdir_p(basedir, 0700)) {
- fprintf(stderr, "Failed to create lxcfs cgdir\n");
- return false;
- }
- if (!umount_if_mounted()) {
- fprintf(stderr, "Failed to clean up old lxcfs cgdir\n");
- return false;
- }
- if (mount("tmpfs", basedir, "tmpfs", 0, "size=100000,mode=700") < 0) {
- fprintf(stderr, "Failed to mount tmpfs for private controllers\n");
- return false;
- }
- return true;
-}
-
-static bool do_mount_cgroup(char *controller)
-{
- char *target;
- size_t len;
- int ret;
-
- len = strlen(basedir) + strlen(controller) + 2;
- target = alloca(len);
- ret = snprintf(target, len, "%s/%s", basedir, controller);
- if (ret < 0 || ret >= len)
- return false;
- if (mkdir(target, 0755) < 0 && errno != EEXIST)
- return false;
- if (mount(controller, target, "cgroup", 0, controller) < 0) {
- fprintf(stderr, "Failed mounting cgroup %s\n", controller);
- return false;
- }
- return true;
-}
-
-static bool do_mount_cgroups(void)
-{
- bool ret;
- FILE *f;
- char *line = NULL;
- size_t len = 0;
-
- if ((f = fopen("/proc/self/cgroup", "r")) == NULL) {
- fprintf(stderr, "Error opening /proc/self/cgroup: %s\n", strerror(errno));
- return false;
- }
-
- while (getline(&line, &len, f) != -1) {
- char *p, *p2;
-
- p = strchr(line, ':');
- if (!p)
- goto out;
- *(p++) = '\0';
-
- p2 = strrchr(p, ':');
- if (!p2)
- goto out;
- *p2 = '\0';
-
- if (!do_mount_cgroup(p))
- goto out;
- }
- ret = true;
-
-out:
- free(line);
- fclose(f);
- return ret;
-}
-
-static bool cgfs_setup_controllers(void)
-{
- if (!setup_cgfs_dir()) {
- return false;
- }
-
- if (!do_mount_cgroups()) {
- fprintf(stderr, "Failed to set up cgroup mounts\n");
- return false;
- }
-
- return true;
-}
-