utils: make detect_ramfs_rootfs() return bool

Signed-off-by: Christian Brauner <christian.brauner@canonical.com>
This commit is contained in:
Christian Brauner 2016-09-06 13:49:13 +02:00
parent 92281f0275
commit fa454c8e3b
2 changed files with 17 additions and 13 deletions

View File

@ -1158,36 +1158,40 @@ bool switch_to_ns(pid_t pid, const char *ns) {
* IIUC, so long as we've chrooted so that rootfs is not our root, * IIUC, so long as we've chrooted so that rootfs is not our root,
* the rootfs entry should always be skipped in mountinfo contents. * the rootfs entry should always be skipped in mountinfo contents.
*/ */
int detect_ramfs_rootfs(void) bool detect_ramfs_rootfs(void)
{ {
char buf[LINELEN], *p;
FILE *f; FILE *f;
char *p, *p2;
char *line = NULL;
size_t len = 0;
int i; int i;
char *p2;
f = fopen("/proc/self/mountinfo", "r"); f = fopen("/proc/self/mountinfo", "r");
if (!f) if (!f)
return 0; return false;
while (fgets(buf, LINELEN, f)) {
for (p = buf, i=0; p && i < 4; i++) while (getline(&line, &len, f) != -1) {
p = strchr(p+1, ' '); for (p = line, i = 0; p && i < 4; i++)
p = strchr(p + 1, ' ');
if (!p) if (!p)
continue; continue;
p2 = strchr(p+1, ' '); p2 = strchr(p + 1, ' ');
if (!p2) if (!p2)
continue; continue;
*p2 = '\0'; *p2 = '\0';
if (strcmp(p+1, "/") == 0) { if (strcmp(p + 1, "/") == 0) {
// this is '/'. is it the ramfs? // this is '/'. is it the ramfs?
p = strchr(p2+1, '-'); p = strchr(p2 + 1, '-');
if (p && strncmp(p, "- rootfs rootfs ", 16) == 0) { if (p && strncmp(p, "- rootfs rootfs ", 16) == 0) {
free(line);
fclose(f); fclose(f);
return 1; return true;
} }
} }
} }
free(line);
fclose(f); fclose(f);
return 0; return false;
} }
char *on_path(char *cmd, const char *rootfs) { char *on_path(char *cmd, const char *rootfs) {

View File

@ -293,7 +293,7 @@ extern bool dir_exists(const char *path);
uint64_t fnv_64a_buf(void *buf, size_t len, uint64_t hval); uint64_t fnv_64a_buf(void *buf, size_t len, uint64_t hval);
int detect_shared_rootfs(void); int detect_shared_rootfs(void);
int detect_ramfs_rootfs(void); bool detect_ramfs_rootfs(void);
char *on_path(char *cmd, const char *rootfs); char *on_path(char *cmd, const char *rootfs);
bool file_exists(const char *f); bool file_exists(const char *f);
bool cgns_supported(void); bool cgns_supported(void);