]>
Commit | Line | Data |
---|---|---|
bf5f9c24 MT |
1 | /*############################################################################# |
2 | # # | |
3 | # Pakfire - The IPFire package management system # | |
4 | # Copyright (C) 2022 Pakfire development team # | |
5 | # # | |
6 | # This program is free software: you can redistribute it and/or modify # | |
7 | # it under the terms of the GNU General Public License as published by # | |
8 | # the Free Software Foundation, either version 3 of the License, or # | |
9 | # (at your option) any later version. # | |
10 | # # | |
11 | # This program is distributed in the hope that it will be useful, # | |
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # | |
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | |
14 | # GNU General Public License for more details. # | |
15 | # # | |
16 | # You should have received a copy of the GNU General Public License # | |
17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. # | |
18 | # # | |
19 | #############################################################################*/ | |
20 | ||
21 | #include <errno.h> | |
22 | #include <linux/limits.h> | |
bf5f9c24 MT |
23 | #include <stddef.h> |
24 | #include <sys/mount.h> | |
25 | #include <sys/stat.h> | |
f71c82d8 | 26 | #include <sys/sysmacros.h> |
bf5f9c24 MT |
27 | #include <sys/types.h> |
28 | ||
660120b6 | 29 | #include <pakfire/arch.h> |
bf5f9c24 MT |
30 | #include <pakfire/logging.h> |
31 | #include <pakfire/pakfire.h> | |
bc0b24a7 | 32 | #include <pakfire/parse.h> |
729827f7 | 33 | #include <pakfire/path.h> |
bf5f9c24 | 34 | #include <pakfire/mount.h> |
d973a13d | 35 | #include <pakfire/string.h> |
bf5f9c24 MT |
36 | #include <pakfire/util.h> |
37 | ||
38 | static const struct pakfire_mountpoint { | |
b2ccb363 | 39 | pakfire_mntns_t ns; |
bf5f9c24 MT |
40 | const char* source; |
41 | const char* target; | |
42 | const char* fstype; | |
43 | int flags; | |
44 | const char* options; | |
bf5f9c24 | 45 | } mountpoints[] = { |
9ab7dd21 | 46 | // Mount a new instance of /proc |
b2ccb363 MT |
47 | { |
48 | PAKFIRE_MNTNS_INNER|PAKFIRE_MNTNS_OUTER, | |
49 | "pakfire_proc", | |
50 | "proc", | |
51 | "proc", | |
52 | MS_NOSUID|MS_NOEXEC|MS_NODEV, | |
53 | NULL, | |
54 | }, | |
bf5f9c24 | 55 | |
0d9e0714 MT |
56 | /* |
57 | XXX it is kind of problematic to mount /proc twice as a process inside the | |
58 | jail can umount /proc and will then see the host's /proc. | |
59 | */ | |
60 | ||
9ab7dd21 | 61 | // Make /proc/sys read-only (except /proc/sys/net) |
b2ccb363 MT |
62 | { |
63 | PAKFIRE_MNTNS_INNER, | |
64 | "/proc/sys", | |
65 | "proc/sys", | |
66 | "bind", | |
67 | MS_BIND|MS_REC, | |
68 | NULL, | |
69 | }, | |
70 | { | |
71 | PAKFIRE_MNTNS_INNER, | |
72 | "/proc/sys/net", | |
73 | "proc/sys/net", | |
74 | "bind", | |
75 | MS_BIND|MS_REC, | |
76 | NULL, | |
77 | }, | |
78 | { | |
79 | PAKFIRE_MNTNS_INNER, | |
80 | "/proc/sys", | |
81 | "proc/sys", | |
82 | "bind", | |
83 | MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, | |
84 | NULL, | |
85 | }, | |
bf5f9c24 | 86 | |
9ab7dd21 | 87 | // Deny write access to /proc/sysrq-trigger (can be used to restart the host) |
b2ccb363 MT |
88 | { |
89 | PAKFIRE_MNTNS_INNER, | |
90 | "/proc/sysrq-trigger", | |
91 | "proc/sysrq-trigger", | |
92 | "bind", | |
93 | MS_BIND|MS_REC, | |
94 | NULL, | |
95 | }, | |
96 | { | |
97 | PAKFIRE_MNTNS_INNER, | |
98 | "/proc/sysrq-trigger", | |
99 | "proc/sysrq-trigger", | |
100 | "bind", | |
101 | MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, | |
102 | NULL, | |
103 | }, | |
9ab7dd21 MT |
104 | |
105 | // Make /proc/irq read-only | |
b2ccb363 MT |
106 | { |
107 | PAKFIRE_MNTNS_INNER, | |
108 | "/proc/irq", | |
109 | "proc/irq", | |
110 | "bind", | |
111 | MS_BIND|MS_REC, | |
112 | NULL, | |
113 | }, | |
114 | { | |
115 | PAKFIRE_MNTNS_INNER, | |
116 | "/proc/irq", | |
117 | "proc/irq", | |
118 | "bind", | |
119 | MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, | |
120 | NULL, | |
121 | }, | |
9ab7dd21 MT |
122 | |
123 | // Make /proc/bus read-only | |
b2ccb363 MT |
124 | { |
125 | PAKFIRE_MNTNS_INNER, | |
126 | "/proc/bus", | |
127 | "proc/bus", | |
128 | "bind", | |
129 | MS_BIND|MS_REC, | |
130 | NULL, | |
131 | }, | |
132 | { | |
133 | PAKFIRE_MNTNS_INNER, | |
134 | "/proc/bus", | |
135 | "proc/bus", | |
136 | "bind", | |
137 | MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, | |
138 | NULL, | |
139 | }, | |
9ab7dd21 MT |
140 | |
141 | // Bind-Mount /sys ready-only | |
b2ccb363 MT |
142 | { |
143 | PAKFIRE_MNTNS_OUTER, | |
144 | "/sys", | |
145 | "sys", | |
146 | "bind", | |
147 | MS_BIND|MS_REC, | |
148 | NULL, | |
149 | }, | |
150 | { | |
151 | PAKFIRE_MNTNS_OUTER, | |
152 | "/sys", | |
153 | "sys", | |
154 | "bind", | |
155 | MS_BIND|MS_RDONLY|MS_NOSUID|MS_NOEXEC|MS_NODEV|MS_REMOUNT, | |
156 | NULL, | |
157 | }, | |
bf5f9c24 MT |
158 | |
159 | // Create a new /dev | |
b2ccb363 MT |
160 | { |
161 | PAKFIRE_MNTNS_OUTER, | |
162 | "pakfire_dev", | |
163 | "dev", | |
164 | "tmpfs", | |
165 | MS_NOSUID|MS_NOEXEC, | |
166 | "mode=0755,size=4m,nr_inodes=64k", | |
167 | }, | |
168 | { | |
169 | PAKFIRE_MNTNS_OUTER, | |
170 | "pakfire_dev_pts", | |
171 | "dev/pts", | |
172 | "devpts", | |
173 | MS_NOSUID|MS_NOEXEC, | |
174 | "newinstance,ptmxmode=0666,mode=620", | |
175 | }, | |
cc752ada | 176 | |
99affb62 | 177 | // Create a new /dev/shm |
b2ccb363 MT |
178 | { |
179 | PAKFIRE_MNTNS_OUTER, | |
180 | "pakfire_dev_shm", | |
181 | "dev/shm", | |
182 | "tmpfs", | |
183 | MS_NOSUID|MS_NODEV|MS_STRICTATIME, | |
184 | "mode=1777,size=1024m", | |
185 | }, | |
99affb62 | 186 | |
cc752ada | 187 | // Mount /dev/mqueue |
b2ccb363 MT |
188 | { |
189 | PAKFIRE_MNTNS_INNER, | |
190 | "mqueue", | |
191 | "dev/mqueue", | |
192 | "mqueue", | |
193 | MS_NOSUID|MS_NOEXEC|MS_NODEV, | |
194 | NULL, | |
195 | }, | |
bf5f9c24 MT |
196 | |
197 | // Create a new /run | |
b2ccb363 MT |
198 | { |
199 | PAKFIRE_MNTNS_OUTER, | |
200 | "pakfire_run", | |
201 | "run", | |
202 | "tmpfs", | |
203 | MS_NOSUID|MS_NOEXEC|MS_NODEV, | |
204 | "mode=755,size=256m,nr_inodes=1k", | |
205 | }, | |
b1a6d98c MT |
206 | |
207 | // Create a new /tmp | |
b2ccb363 MT |
208 | { |
209 | PAKFIRE_MNTNS_OUTER, | |
210 | "pakfire_tmp", | |
211 | "tmp", | |
212 | "tmpfs", | |
213 | MS_NOSUID|MS_NODEV|MS_STRICTATIME, | |
214 | "mode=1777,size=4096m", | |
215 | }, | |
bf5f9c24 | 216 | |
bf5f9c24 | 217 | // The end |
b2ccb363 | 218 | {}, |
bf5f9c24 MT |
219 | }; |
220 | ||
f71c82d8 MT |
221 | static const struct pakfire_devnode { |
222 | const char* path; | |
223 | int major; | |
224 | int minor; | |
225 | mode_t mode; | |
282b732a | 226 | int flags; |
f71c82d8 | 227 | } devnodes[] = { |
282b732a MT |
228 | { "/dev/null", 1, 3, S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH, 0 }, |
229 | { "/dev/zero", 1, 5, S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH, 0 }, | |
230 | { "/dev/full", 1, 7, S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH, 0 }, | |
231 | { "/dev/random", 1, 8, S_IFCHR|S_IRUSR|S_IRGRP|S_IROTH, 0 }, | |
232 | { "/dev/urandom", 1, 9, S_IFCHR|S_IRUSR|S_IRGRP|S_IROTH, 0 }, | |
233 | { "/dev/kmsg", 1, 11, S_IFCHR|S_IRUSR|S_IRGRP|S_IROTH, 0 }, | |
234 | { "/dev/tty", 5, 0, S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH, 0 }, | |
282b732a MT |
235 | { "/dev/rtc0", 252, 0, S_IFCHR|S_IRUSR|S_IWUSR, 0 }, |
236 | ||
237 | // Loop Devices | |
238 | { "/dev/loop-control", 10, 237, S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP, PAKFIRE_MOUNT_LOOP_DEVICES }, | |
239 | { "/dev/loop0", 7, 0, S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP, PAKFIRE_MOUNT_LOOP_DEVICES }, | |
240 | { "/dev/loop1", 7, 1, S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP, PAKFIRE_MOUNT_LOOP_DEVICES }, | |
241 | { "/dev/loop2", 7, 2, S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP, PAKFIRE_MOUNT_LOOP_DEVICES }, | |
242 | { "/dev/loop3", 7, 3, S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP, PAKFIRE_MOUNT_LOOP_DEVICES }, | |
243 | { "/dev/loop4", 7, 4, S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP, PAKFIRE_MOUNT_LOOP_DEVICES }, | |
244 | { "/dev/loop5", 7, 5, S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP, PAKFIRE_MOUNT_LOOP_DEVICES }, | |
245 | { "/dev/loop6", 7, 6, S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP, PAKFIRE_MOUNT_LOOP_DEVICES }, | |
246 | { "/dev/loop7", 7, 7, S_IFCHR|S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP, PAKFIRE_MOUNT_LOOP_DEVICES }, | |
247 | ||
f71c82d8 MT |
248 | { NULL }, |
249 | }; | |
dc01afb6 | 250 | |
f71c82d8 MT |
251 | static const struct pakfire_symlink { |
252 | const char* target; | |
253 | const char* path; | |
254 | } symlinks[] = { | |
0675c514 | 255 | { "/dev/pts/ptmx", "/dev/ptmx", }, |
f71c82d8 MT |
256 | { "/proc/self/fd", "/dev/fd", }, |
257 | { "/proc/self/fd/0", "/dev/stdin" }, | |
258 | { "/proc/self/fd/1", "/dev/stdout" }, | |
259 | { "/proc/self/fd/2", "/dev/stderr" }, | |
260 | { "/proc/kcore", "/dev/core" }, | |
261 | { NULL }, | |
262 | }; | |
dc01afb6 | 263 | |
719e2e3e MT |
264 | int pakfire_mount_change_propagation(struct pakfire_ctx* ctx, const char* path, int propagation) { |
265 | CTX_DEBUG(ctx, "Changing mount propagation on %s\n", path); | |
9f558f7d MT |
266 | |
267 | int r = mount(NULL, path, NULL, propagation|MS_REC, NULL); | |
268 | if (r) | |
719e2e3e | 269 | CTX_ERROR(ctx, "Failed to change mount propagation on %s: %m\n", path); |
9f558f7d MT |
270 | |
271 | return r; | |
272 | } | |
273 | ||
14df7388 MT |
274 | static int pakfire_mount_is_mountpoint(struct pakfire* pakfire, const char* path) { |
275 | // XXX THIS STILL NEEDS TO BE IMPLEMENTED | |
276 | return 1; | |
277 | } | |
278 | ||
279 | int pakfire_mount_make_mounpoint(struct pakfire* pakfire, const char* path) { | |
280 | int r; | |
281 | ||
282 | // Check if path already is a mountpoint | |
283 | r = pakfire_mount_is_mountpoint(pakfire, path); | |
284 | switch (r) { | |
285 | // Already is a mountpoint | |
286 | case 0: | |
287 | return 0; | |
288 | ||
289 | // Is not a mountpoint | |
290 | case 1: | |
291 | break; | |
292 | ||
293 | default: | |
294 | ERROR(pakfire, "Could not determine whether %s is a mountpoint: %m\n", path); | |
295 | return r; | |
296 | } | |
297 | ||
298 | // Bind-mount to self | |
299 | r = mount(path, path, NULL, MS_BIND|MS_REC, NULL); | |
300 | if (r) { | |
301 | ERROR(pakfire, "Could not make %s a mountpoint: %m\n", path); | |
302 | return r; | |
303 | } | |
304 | ||
305 | return 0; | |
306 | } | |
307 | ||
163851bc | 308 | static int pakfire_mount(struct pakfire* pakfire, const char* source, const char* target, |
bf5f9c24 MT |
309 | const char* fstype, unsigned long mflags, const void* data) { |
310 | const char* options = (const char*)data; | |
bf5f9c24 MT |
311 | |
312 | // Check for some basic inputs | |
313 | if (!source || !target) { | |
314 | errno = EINVAL; | |
315 | return 1; | |
316 | } | |
317 | ||
7f970e6e | 318 | DEBUG(pakfire, "Mounting %s from %s (%s - %s)\n", target, source, fstype, options); |
bf5f9c24 | 319 | |
7f970e6e MT |
320 | // Perform mount() |
321 | int r = mount(source, target, fstype, mflags, data); | |
bf5f9c24 | 322 | if (r) { |
7f970e6e | 323 | ERROR(pakfire, "Could not mount %s: %m\n", target); |
bf5f9c24 MT |
324 | } |
325 | ||
bf5f9c24 MT |
326 | return r; |
327 | } | |
328 | ||
bc0b24a7 MT |
329 | static int __pakfire_mount_list(char* line, size_t length, void* data) { |
330 | struct pakfire_ctx* ctx = data; | |
bf5f9c24 | 331 | |
bc0b24a7 MT |
332 | // Send the line to the logger |
333 | CTX_DEBUG(ctx, " %.*s", (int)length, line); | |
334 | ||
335 | return 0; | |
336 | } | |
337 | ||
338 | int pakfire_mount_list(struct pakfire_ctx* ctx) { | |
339 | CTX_DEBUG(ctx, "Mountpoints:\n"); | |
340 | ||
341 | return pakfire_parse_file("/proc/self/mounts", __pakfire_mount_list, ctx); | |
f71c82d8 MT |
342 | } |
343 | ||
b2ccb363 | 344 | int pakfire_populate_dev(struct pakfire* pakfire, int flags) { |
f71c82d8 MT |
345 | char path[PATH_MAX]; |
346 | ||
347 | // Create device nodes | |
348 | for (const struct pakfire_devnode* devnode = devnodes; devnode->path; devnode++) { | |
349 | DEBUG(pakfire, "Creating device node %s\n", devnode->path); | |
350 | ||
282b732a MT |
351 | // Check if flags match |
352 | if (devnode->flags && !(flags & devnode->flags)) | |
353 | continue; | |
354 | ||
77e26129 MT |
355 | int r = pakfire_path(pakfire, path, "%s", devnode->path); |
356 | if (r) | |
357 | return r; | |
f71c82d8 MT |
358 | |
359 | dev_t dev = makedev(devnode->major, devnode->minor); | |
360 | ||
361 | r = mknod(path, devnode->mode, dev); | |
8f1003a3 MT |
362 | |
363 | // Continue if mknod was successful | |
364 | if (r == 0) | |
365 | continue; | |
366 | ||
367 | // If we could not create the device node because of permission issues, | |
368 | // it might be likely that we are running in a user namespace where creating | |
369 | // device nodes is not permitted. Try bind-mounting them. | |
370 | if (errno == EPERM) | |
371 | goto MOUNT; | |
372 | ||
373 | // Otherwise log an error and end | |
374 | ERROR(pakfire, "Could not create %s: %m\n", devnode->path); | |
375 | return r; | |
376 | ||
377 | MOUNT: | |
378 | // Create an empty file | |
379 | r = pakfire_touch(path, 0444); | |
f71c82d8 | 380 | if (r) { |
8f1003a3 | 381 | ERROR(pakfire, "Could not create %s: %m\n", path); |
f71c82d8 MT |
382 | return r; |
383 | } | |
8f1003a3 MT |
384 | |
385 | // Create a bind-mount over the file | |
64e3b4ff | 386 | r = pakfire_mount(pakfire, devnode->path, path, "bind", MS_BIND, NULL); |
8f1003a3 MT |
387 | if (r) |
388 | return r; | |
f71c82d8 MT |
389 | } |
390 | ||
391 | // Create symlinks | |
392 | for (const struct pakfire_symlink* s = symlinks; s->target; s++) { | |
393 | DEBUG(pakfire, "Creating symlink %s -> %s\n", s->path, s->target); | |
394 | ||
77e26129 MT |
395 | int r = pakfire_path(pakfire, path, "%s", s->path); |
396 | if (r) | |
397 | return r; | |
f71c82d8 MT |
398 | |
399 | r = symlink(s->target, path); | |
400 | if (r) { | |
401 | ERROR(pakfire, "Could not create symlink %s: %m\n", s->path); | |
402 | return r; | |
403 | } | |
404 | } | |
405 | ||
406 | return 0; | |
bf5f9c24 MT |
407 | } |
408 | ||
b2ccb363 | 409 | int pakfire_mount_interpreter(struct pakfire* pakfire) { |
660120b6 MT |
410 | char target[PATH_MAX]; |
411 | ||
412 | // Fetch the target architecture | |
652f2a99 | 413 | const char* arch = pakfire_get_effective_arch(pakfire); |
660120b6 MT |
414 | |
415 | // Can we emulate this architecture? | |
416 | char* interpreter = pakfire_arch_find_interpreter(arch); | |
417 | ||
418 | // No interpreter required | |
419 | if (!interpreter) | |
420 | return 0; | |
421 | ||
422 | DEBUG(pakfire, "Mounting interpreter %s for %s\n", interpreter, arch); | |
423 | ||
424 | // Where to mount this? | |
77e26129 MT |
425 | int r = pakfire_path(pakfire, target, "%s", interpreter); |
426 | if (r) | |
660120b6 MT |
427 | return r; |
428 | ||
429 | // Create directory | |
520ce66c | 430 | r = pakfire_mkparentdir(target, 0755); |
660120b6 MT |
431 | if (r) |
432 | return r; | |
433 | ||
434 | // Create an empty file | |
435 | FILE* f = fopen(target, "w"); | |
436 | if (!f) | |
437 | return 1; | |
438 | fclose(f); | |
439 | ||
440 | r = pakfire_mount(pakfire, interpreter, target, NULL, MS_BIND|MS_RDONLY, NULL); | |
441 | if (r) | |
442 | ERROR(pakfire, "Could not mount interpreter %s to %s: %m\n", interpreter, target); | |
443 | ||
444 | return r; | |
445 | } | |
446 | ||
b2ccb363 | 447 | int pakfire_mount_all(struct pakfire* pakfire, pakfire_mntns_t ns, int flags) { |
bf5f9c24 | 448 | char target[PATH_MAX]; |
bf5f9c24 MT |
449 | int r; |
450 | ||
b2ccb363 MT |
451 | const char* root = "/"; |
452 | ||
bf5f9c24 | 453 | // Fetch Pakfire's root directory |
b2ccb363 MT |
454 | if (ns == PAKFIRE_MNTNS_OUTER) |
455 | root = pakfire_get_path(pakfire); | |
bf5f9c24 | 456 | |
bf5f9c24 | 457 | for (const struct pakfire_mountpoint* mp = mountpoints; mp->source; mp++) { |
b2ccb363 MT |
458 | if (!(mp->ns & ns)) |
459 | continue; | |
460 | ||
bf5f9c24 | 461 | // Figure out where to mount |
819232d6 | 462 | r = pakfire_path_append(target, root, mp->target); |
56796f84 | 463 | if (r) |
bf5f9c24 MT |
464 | return r; |
465 | ||
a685a503 MT |
466 | // Create target if it doesn't exist |
467 | if (!pakfire_path_exists(target)) { | |
468 | r = pakfire_mkdir(target, 0755); | |
469 | if (r) { | |
470 | ERROR(pakfire, "Could not create %s: %m\n", target); | |
471 | return r; | |
472 | } | |
473 | } | |
bf5f9c24 | 474 | |
bf5f9c24 | 475 | // Perform mount() |
f71c82d8 | 476 | r = pakfire_mount(pakfire, mp->source, target, mp->fstype, mp->flags, mp->options); |
a685a503 | 477 | if (r) |
bf5f9c24 | 478 | return r; |
bf5f9c24 MT |
479 | } |
480 | ||
bf5f9c24 MT |
481 | return 0; |
482 | } | |
483 | ||
06d741c6 MT |
484 | int pakfire_make_ramdisk(struct pakfire* pakfire, char* path, const char* args) { |
485 | int r; | |
486 | ||
487 | // Create a new temporary directory | |
488 | char* p = pakfire_mkdtemp(path); | |
489 | if (!p) | |
490 | return -errno; | |
491 | ||
492 | // Mount the ramdisk | |
493 | r = pakfire_mount(pakfire, "pakfire_ramdisk", p, "tmpfs", 0, args); | |
494 | if (r) { | |
495 | ERROR_ERRNO(pakfire, r, "Could not mount ramdisk at %s (%s): %m\n", p, args); | |
496 | return r; | |
497 | } | |
498 | ||
499 | DEBUG(pakfire, "Ramdisk mounted at %s (%s)\n", p, args); | |
500 | ||
501 | return 0; | |
502 | } | |
503 | ||
061223f7 | 504 | int pakfire_bind(struct pakfire* pakfire, const char* src, const char* dst, int flags) { |
163851bc MT |
505 | struct stat st; |
506 | char mountpoint[PATH_MAX]; | |
507 | ||
508 | if (!dst) | |
509 | dst = src; | |
510 | ||
77e26129 MT |
511 | int r = pakfire_path(pakfire, mountpoint, "%s", dst); |
512 | if (r) | |
513 | return r; | |
163851bc MT |
514 | |
515 | DEBUG(pakfire, "Bind-mounting %s to %s\n", src, mountpoint); | |
516 | ||
517 | r = stat(src, &st); | |
518 | if (r < 0) { | |
519 | ERROR(pakfire, "Could not stat %s: %m\n", src); | |
520 | return 1; | |
521 | } | |
522 | ||
523 | // Make sure the mountpoint exists | |
524 | switch (st.st_mode & S_IFMT) { | |
525 | case S_IFDIR: | |
520ce66c | 526 | r = pakfire_mkdir(mountpoint, st.st_mode); |
163851bc MT |
527 | if (r && errno != EEXIST) |
528 | return r; | |
529 | break; | |
530 | ||
531 | case S_IFREG: | |
532 | case S_IFLNK: | |
533 | // Make parent directory | |
520ce66c | 534 | r = pakfire_mkparentdir(mountpoint, 0755); |
163851bc MT |
535 | if (r) |
536 | return r; | |
537 | ||
538 | // Create a file | |
539 | FILE* f = fopen(mountpoint, "w"); | |
540 | if (!f) | |
541 | return 1; | |
542 | fclose(f); | |
543 | break; | |
544 | ||
545 | default: | |
546 | errno = ENOTSUP; | |
547 | return 1; | |
548 | } | |
549 | ||
976fbbc8 MT |
550 | // The Linux kernel seems to be quite funny when trying to bind-mount something |
551 | // as read-only and requires us to mount the source first, and then remount it | |
552 | // again using MS_RDONLY. | |
553 | if (flags & MS_RDONLY) { | |
b16b8753 | 554 | r = pakfire_mount(pakfire, src, mountpoint, "bind", MS_BIND|MS_REC, NULL); |
976fbbc8 MT |
555 | if (r) |
556 | return r; | |
557 | ||
558 | // Add the remount flag | |
559 | flags |= MS_REMOUNT; | |
560 | } | |
561 | ||
163851bc | 562 | // Perform mount |
b16b8753 | 563 | return pakfire_mount(pakfire, src, mountpoint, "bind", flags|MS_BIND|MS_REC, NULL); |
163851bc | 564 | } |