]> git.proxmox.com Git - mirror_lxc.git/blob - src/lxc/mount_utils.c
cgroups: use zalloc
[mirror_lxc.git] / src / lxc / mount_utils.c
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2
3 #ifndef _GNU_SOURCE
4 #define _GNU_SOURCE 1
5 #endif
6 #include <fcntl.h>
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <sys/mount.h>
10 #include <sys/stat.h>
11 #include <sys/types.h>
12
13 #include "log.h"
14 #include "macro.h"
15 #include "memory_utils.h"
16 #include "mount_utils.h"
17 #include "syscall_numbers.h"
18 #include "syscall_wrappers.h"
19
20 lxc_log_define(mount_utils, lxc);
21
22 int mnt_attributes_new(unsigned int old_flags, unsigned int *new_flags)
23 {
24 unsigned int flags = 0;
25
26 if (old_flags & MS_RDONLY) {
27 flags |= MOUNT_ATTR_RDONLY;
28 old_flags &= ~MS_RDONLY;
29 }
30
31 if (old_flags & MS_NOSUID) {
32 flags |= MOUNT_ATTR_NOSUID;
33 old_flags &= ~MS_NOSUID;
34 }
35
36 if (old_flags & MS_NODEV) {
37 flags |= MOUNT_ATTR_NODEV;
38 old_flags &= ~MS_NODEV;
39 }
40
41 if (old_flags & MS_NOEXEC) {
42 flags |= MOUNT_ATTR_NOEXEC;
43 old_flags &= ~MS_NOEXEC;
44 }
45
46 if (old_flags & MS_RELATIME) {
47 flags |= MOUNT_ATTR_RELATIME;
48 old_flags &= ~MS_RELATIME;
49 }
50
51 if (old_flags & MS_NOATIME) {
52 flags |= MOUNT_ATTR_NOATIME;
53 old_flags &= ~MS_NOATIME;
54 }
55
56 if (old_flags & MS_STRICTATIME) {
57 flags |= MOUNT_ATTR_STRICTATIME;
58 old_flags &= ~MS_STRICTATIME;
59 }
60
61 if (old_flags & MS_NODIRATIME) {
62 flags |= MOUNT_ATTR_NODIRATIME;
63 old_flags &= ~MS_NODIRATIME;
64 }
65
66 *new_flags |= flags;
67 return old_flags;
68 }
69
70 int mnt_attributes_old(unsigned int new_flags, unsigned int *old_flags)
71 {
72 unsigned int flags = 0;
73
74 if (new_flags & MOUNT_ATTR_RDONLY) {
75 flags |= MS_RDONLY;
76 new_flags &= ~MOUNT_ATTR_RDONLY;
77 }
78
79 if (new_flags & MOUNT_ATTR_NOSUID) {
80 flags |= MS_NOSUID;
81 new_flags &= ~MOUNT_ATTR_NOSUID;
82 }
83
84 if (new_flags & MS_NODEV) {
85 flags |= MOUNT_ATTR_NODEV;
86 new_flags &= ~MS_NODEV;
87 }
88
89 if (new_flags & MOUNT_ATTR_NOEXEC) {
90 flags |= MS_NOEXEC;
91 new_flags &= ~MOUNT_ATTR_NOEXEC;
92 }
93
94 if (new_flags & MS_RELATIME) {
95 flags |= MS_RELATIME;
96 new_flags &= ~MOUNT_ATTR_RELATIME;
97 }
98
99 if (new_flags & MS_NOATIME) {
100 flags |= MS_NOATIME;
101 new_flags &= ~MOUNT_ATTR_NOATIME;
102 }
103
104 if (new_flags & MS_STRICTATIME) {
105 flags |= MS_STRICTATIME;
106 new_flags &= ~MOUNT_ATTR_STRICTATIME;
107 }
108
109 if (new_flags & MS_NODIRATIME) {
110 flags |= MS_NODIRATIME;
111 new_flags &= ~MOUNT_ATTR_NODIRATIME;
112 }
113
114 *old_flags |= flags;
115 return new_flags;
116 }
117
118 int mount_filesystem(const char *fs_name, const char *path, unsigned int attr_flags)
119 {
120 __do_close int fsfd = -EBADF;
121 unsigned int old_flags = 0;
122
123 fsfd = fsopen(fs_name, FSOPEN_CLOEXEC);
124 if (fsfd >= 0) {
125 __do_close int mfd = -EBADF;
126
127 if (fsconfig(fsfd, FSCONFIG_CMD_CREATE, NULL, NULL, 0))
128 return -1;
129
130 mfd = fsmount(fsfd, FSMOUNT_CLOEXEC, attr_flags);
131 if (mfd < 0)
132 return -1;
133
134 return move_mount(mfd, "", AT_FDCWD, path, MOVE_MOUNT_F_EMPTY_PATH);
135 }
136
137 TRACE("Falling back to old mount api");
138 mnt_attributes_old(attr_flags, &old_flags);
139 return mount("none", path, fs_name, old_flags, NULL);
140 }