diff options
author | smitsohu <smitsohu@gmail.com> | 2018-10-26 22:47:48 +0200 |
---|---|---|
committer | smitsohu <smitsohu@gmail.com> | 2018-10-26 22:47:48 +0200 |
commit | 922c4a10abe52d9f2c1e1b1c932a43076822920f (patch) | |
tree | d22523938f1a78eb06d7713c12301c7a91c368b6 /src/firejail/mountinfo.c | |
parent | Merge pull request #2218 from smitsohu/mounts2 (diff) | |
download | firejail-922c4a10abe52d9f2c1e1b1c932a43076822920f.tar.gz firejail-922c4a10abe52d9f2c1e1b1c932a43076822920f.tar.zst firejail-922c4a10abe52d9f2c1e1b1c932a43076822920f.zip |
little tweaks, cosmetic changes
Diffstat (limited to 'src/firejail/mountinfo.c')
-rw-r--r-- | src/firejail/mountinfo.c | 40 |
1 files changed, 20 insertions, 20 deletions
diff --git a/src/firejail/mountinfo.c b/src/firejail/mountinfo.c index b7760ba67..b7e6c6fdd 100644 --- a/src/firejail/mountinfo.c +++ b/src/firejail/mountinfo.c | |||
@@ -67,7 +67,7 @@ static void unmangle_path(char *path) { | |||
67 | // Parse a line from /proc/self/mountinfo, | 67 | // Parse a line from /proc/self/mountinfo, |
68 | // the function does an exit(1) if anything goes wrong. | 68 | // the function does an exit(1) if anything goes wrong. |
69 | static void parse_line(char *line, MountData *output) { | 69 | static void parse_line(char *line, MountData *output) { |
70 | assert(line && *line); | 70 | assert(line && output); |
71 | memset(output, 0, sizeof(*output)); | 71 | memset(output, 0, sizeof(*output)); |
72 | // extract filesystem name, directory and filesystem types | 72 | // extract filesystem name, directory and filesystem types |
73 | // examples: | 73 | // examples: |
@@ -156,7 +156,7 @@ int get_mount_id(const char *path) { | |||
156 | EUID_ASSERT(); | 156 | EUID_ASSERT(); |
157 | int fd = open(path, O_PATH|O_CLOEXEC); | 157 | int fd = open(path, O_PATH|O_CLOEXEC); |
158 | if (fd == -1) | 158 | if (fd == -1) |
159 | return 0; | 159 | return -1; |
160 | 160 | ||
161 | char *fdinfo; | 161 | char *fdinfo; |
162 | if (asprintf(&fdinfo, "/proc/self/fdinfo/%d", fd) == -1) | 162 | if (asprintf(&fdinfo, "/proc/self/fdinfo/%d", fd) == -1) |
@@ -166,31 +166,31 @@ int get_mount_id(const char *path) { | |||
166 | EUID_USER(); | 166 | EUID_USER(); |
167 | if (!fp) | 167 | if (!fp) |
168 | goto errexit; | 168 | goto errexit; |
169 | // go to the last line | 169 | |
170 | // read the file | ||
170 | char buf[MAX_BUF]; | 171 | char buf[MAX_BUF]; |
171 | while (fgets(buf, MAX_BUF, fp)); | 172 | while (fgets(buf, MAX_BUF, fp)) { |
172 | fclose(fp); | 173 | if (strncmp(buf, "mnt_id:", 7) == 0) { |
173 | close(fd); | 174 | char *ptr = buf + 7; |
174 | // go to the mount id | 175 | while (*ptr != '\0' && (*ptr == ' ' || *ptr == '\t')) { |
175 | if (strncmp(buf, "mnt_id:", 7) != 0) | 176 | ptr++; |
176 | goto errexit; | 177 | } |
177 | char *ptr = buf + 7; | 178 | if (*ptr == '\0') |
178 | while (*ptr != '\0' && (*ptr == ' ' || *ptr == '\t')) { | 179 | goto errexit; |
179 | ptr++; | 180 | fclose(fp); |
181 | close(fd); | ||
182 | free(fdinfo); | ||
183 | return atoi(ptr); | ||
184 | } | ||
180 | } | 185 | } |
181 | if (*ptr == '\0') | ||
182 | goto errexit; | ||
183 | free(fdinfo); | ||
184 | |||
185 | return atoi(ptr); | ||
186 | 186 | ||
187 | errexit: | 187 | errexit: |
188 | fprintf(stderr, "Error: cannot read file in /proc/self/fdinfo\n"); | 188 | fprintf(stderr, "Error: cannot read %s\n", fdinfo); |
189 | exit(1); | 189 | exit(1); |
190 | } | 190 | } |
191 | 191 | ||
192 | // Return array with all paths that might need a remount. | 192 | // Return array with all paths that might need a remount. |
193 | char **get_all_mounts(const int mountid, const char *path) { | 193 | char **build_mount_array(const int mountid, const char *path) { |
194 | // open /proc/self/mountinfo | 194 | // open /proc/self/mountinfo |
195 | FILE *fp = fopen("/proc/self/mountinfo", "re"); | 195 | FILE *fp = fopen("/proc/self/mountinfo", "re"); |
196 | if (!fp) { | 196 | if (!fp) { |
@@ -244,7 +244,7 @@ char **get_all_mounts(const int mountid, const char *path) { | |||
244 | errExit("realloc"); | 244 | errExit("realloc"); |
245 | } | 245 | } |
246 | rv[cnt] = strdup(mdata.dir); | 246 | rv[cnt] = strdup(mdata.dir); |
247 | if (!rv[cnt]) | 247 | if (rv[cnt] == NULL) |
248 | errExit("strdup"); | 248 | errExit("strdup"); |
249 | cnt++; | 249 | cnt++; |
250 | } | 250 | } |