]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
c6878637 | 2 | |
11c3a366 TA |
3 | #include <errno.h> |
4 | #include <fcntl.h> | |
5 | #include <stdbool.h> | |
6 | #include <stddef.h> | |
7 | #include <sys/stat.h> | |
8 | #include <sys/statfs.h> | |
9 | #include <unistd.h> | |
10 | ||
265e9be7 | 11 | #include "alloc-util.h" |
d9e2daaf | 12 | #include "btrfs-util.h" |
f0bef277 | 13 | #include "cgroup-util.h" |
8fb3f009 | 14 | #include "dirent-util.h" |
3ffd4af2 | 15 | #include "fd-util.h" |
93cc7779 TA |
16 | #include "log.h" |
17 | #include "macro.h" | |
049af8ad | 18 | #include "mountpoint-util.h" |
07630cea | 19 | #include "path-util.h" |
3ffd4af2 | 20 | #include "rm-rf.h" |
8fcde012 | 21 | #include "stat-util.h" |
07630cea | 22 | #include "string-util.h" |
c6878637 | 23 | |
f0bef277 EV |
24 | static bool is_physical_fs(const struct statfs *sfs) { |
25 | return !is_temporary_fs(sfs) && !is_cgroup_fs(sfs); | |
26 | } | |
27 | ||
c6878637 LP |
28 | int rm_rf_children(int fd, RemoveFlags flags, struct stat *root_dev) { |
29 | _cleanup_closedir_ DIR *d = NULL; | |
8fb3f009 | 30 | struct dirent *de; |
c6878637 | 31 | int ret = 0, r; |
f0bef277 | 32 | struct statfs sfs; |
c6878637 LP |
33 | |
34 | assert(fd >= 0); | |
35 | ||
c0ba6b92 LP |
36 | /* This returns the first error we run into, but nevertheless tries to go on. This closes the passed |
37 | * fd, in all cases, including on failure.. */ | |
c6878637 LP |
38 | |
39 | if (!(flags & REMOVE_PHYSICAL)) { | |
40 | ||
f0bef277 | 41 | r = fstatfs(fd, &sfs); |
c6878637 LP |
42 | if (r < 0) { |
43 | safe_close(fd); | |
f0bef277 | 44 | return -errno; |
c6878637 LP |
45 | } |
46 | ||
f0bef277 | 47 | if (is_physical_fs(&sfs)) { |
265e9be7 ZJS |
48 | /* We refuse to clean physical file systems with this call, |
49 | * unless explicitly requested. This is extra paranoia just | |
50 | * to be sure we never ever remove non-state data. */ | |
51 | _cleanup_free_ char *path = NULL; | |
52 | ||
53 | (void) fd_get_path(fd, &path); | |
54 | log_error("Attempted to remove disk file system under \"%s\", and we can't allow that.", | |
55 | strna(path)); | |
c6878637 | 56 | |
c6878637 LP |
57 | safe_close(fd); |
58 | return -EPERM; | |
59 | } | |
60 | } | |
61 | ||
62 | d = fdopendir(fd); | |
63 | if (!d) { | |
64 | safe_close(fd); | |
65 | return errno == ENOENT ? 0 : -errno; | |
66 | } | |
67 | ||
8fb3f009 | 68 | FOREACH_DIRENT_ALL(de, d, return -errno) { |
c6878637 LP |
69 | bool is_dir; |
70 | struct stat st; | |
71 | ||
49bfc877 | 72 | if (dot_or_dot_dot(de->d_name)) |
c6878637 LP |
73 | continue; |
74 | ||
9e9b663a LP |
75 | if (de->d_type == DT_UNKNOWN || |
76 | (de->d_type == DT_DIR && (root_dev || (flags & REMOVE_SUBVOLUME)))) { | |
c6878637 LP |
77 | if (fstatat(fd, de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) { |
78 | if (ret == 0 && errno != ENOENT) | |
79 | ret = -errno; | |
80 | continue; | |
81 | } | |
82 | ||
83 | is_dir = S_ISDIR(st.st_mode); | |
84 | } else | |
85 | is_dir = de->d_type == DT_DIR; | |
86 | ||
87 | if (is_dir) { | |
88 | int subdir_fd; | |
89 | ||
f25afeb6 | 90 | /* if root_dev is set, remove subdirectories only if device is same */ |
c6878637 LP |
91 | if (root_dev && st.st_dev != root_dev->st_dev) |
92 | continue; | |
93 | ||
94 | subdir_fd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME); | |
95 | if (subdir_fd < 0) { | |
96 | if (ret == 0 && errno != ENOENT) | |
97 | ret = -errno; | |
98 | continue; | |
99 | } | |
100 | ||
f25afeb6 | 101 | /* Stop at mount points */ |
5d409034 | 102 | r = fd_is_mount_point(fd, de->d_name, 0); |
f25afeb6 LP |
103 | if (r < 0) { |
104 | if (ret == 0 && r != -ENOENT) | |
105 | ret = r; | |
106 | ||
107 | safe_close(subdir_fd); | |
108 | continue; | |
109 | } | |
110 | if (r) { | |
111 | safe_close(subdir_fd); | |
112 | continue; | |
113 | } | |
114 | ||
9e9b663a | 115 | if ((flags & REMOVE_SUBVOLUME) && st.st_ino == 256) { |
9e9b663a LP |
116 | |
117 | /* This could be a subvolume, try to remove it */ | |
118 | ||
5bcd08db | 119 | r = btrfs_subvol_remove_fd(fd, de->d_name, BTRFS_REMOVE_RECURSIVE|BTRFS_REMOVE_QUOTA); |
d9e2daaf | 120 | if (r < 0) { |
4c701096 | 121 | if (!IN_SET(r, -ENOTTY, -EINVAL)) { |
9e9b663a | 122 | if (ret == 0) |
d9e2daaf | 123 | ret = r; |
9e9b663a LP |
124 | |
125 | safe_close(subdir_fd); | |
126 | continue; | |
127 | } | |
128 | ||
d9e2daaf LP |
129 | /* ENOTTY, then it wasn't a |
130 | * btrfs subvolume, continue | |
131 | * below. */ | |
9e9b663a LP |
132 | } else { |
133 | /* It was a subvolume, continue. */ | |
134 | safe_close(subdir_fd); | |
135 | continue; | |
136 | } | |
137 | } | |
138 | ||
c6878637 LP |
139 | /* We pass REMOVE_PHYSICAL here, to avoid |
140 | * doing the fstatfs() to check the file | |
141 | * system type again for each directory */ | |
142 | r = rm_rf_children(subdir_fd, flags | REMOVE_PHYSICAL, root_dev); | |
143 | if (r < 0 && ret == 0) | |
144 | ret = r; | |
145 | ||
146 | if (unlinkat(fd, de->d_name, AT_REMOVEDIR) < 0) { | |
147 | if (ret == 0 && errno != ENOENT) | |
148 | ret = -errno; | |
149 | } | |
150 | ||
151 | } else if (!(flags & REMOVE_ONLY_DIRECTORIES)) { | |
152 | ||
153 | if (unlinkat(fd, de->d_name, 0) < 0) { | |
154 | if (ret == 0 && errno != ENOENT) | |
155 | ret = -errno; | |
156 | } | |
157 | } | |
158 | } | |
8fb3f009 | 159 | return ret; |
c6878637 LP |
160 | } |
161 | ||
162 | int rm_rf(const char *path, RemoveFlags flags) { | |
163 | int fd, r; | |
164 | struct statfs s; | |
165 | ||
166 | assert(path); | |
167 | ||
c2f64c07 LP |
168 | /* For now, don't support dropping subvols when also only dropping directories, since we can't do |
169 | * this race-freely. */ | |
170 | if (FLAGS_SET(flags, REMOVE_ONLY_DIRECTORIES|REMOVE_SUBVOLUME)) | |
171 | return -EINVAL; | |
172 | ||
c6878637 LP |
173 | /* We refuse to clean the root file system with this |
174 | * call. This is extra paranoia to never cause a really | |
175 | * seriously broken system. */ | |
baaa35ad ZJS |
176 | if (path_equal_or_files_same(path, "/", AT_SYMLINK_NOFOLLOW)) |
177 | return log_error_errno(SYNTHETIC_ERRNO(EPERM), | |
178 | "Attempted to remove entire root file system (\"%s\"), and we can't allow that.", | |
179 | path); | |
c6878637 | 180 | |
d94a24ca | 181 | if (FLAGS_SET(flags, REMOVE_SUBVOLUME | REMOVE_ROOT | REMOVE_PHYSICAL)) { |
d9e2daaf | 182 | /* Try to remove as subvolume first */ |
5bcd08db | 183 | r = btrfs_subvol_remove(path, BTRFS_REMOVE_RECURSIVE|BTRFS_REMOVE_QUOTA); |
d9e2daaf LP |
184 | if (r >= 0) |
185 | return r; | |
186 | ||
4c701096 | 187 | if (!IN_SET(r, -ENOTTY, -EINVAL, -ENOTDIR)) |
d9e2daaf LP |
188 | return r; |
189 | ||
190 | /* Not btrfs or not a subvolume */ | |
191 | } | |
192 | ||
c6878637 LP |
193 | fd = open(path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME); |
194 | if (fd < 0) { | |
ec2ce0c5 | 195 | if (!IN_SET(errno, ENOTDIR, ELOOP)) |
c6878637 LP |
196 | return -errno; |
197 | ||
198 | if (!(flags & REMOVE_PHYSICAL)) { | |
199 | if (statfs(path, &s) < 0) | |
200 | return -errno; | |
201 | ||
baaa35ad ZJS |
202 | if (is_physical_fs(&s)) |
203 | return log_error_errno(SYNTHETIC_ERRNO(EPERM), | |
204 | "Attempted to remove files from a disk file system under \"%s\", refusing.", | |
205 | path); | |
c6878637 LP |
206 | } |
207 | ||
208 | if ((flags & REMOVE_ROOT) && !(flags & REMOVE_ONLY_DIRECTORIES)) | |
209 | if (unlink(path) < 0 && errno != ENOENT) | |
210 | return -errno; | |
211 | ||
212 | return 0; | |
213 | } | |
214 | ||
215 | r = rm_rf_children(fd, flags, NULL); | |
216 | ||
217 | if (flags & REMOVE_ROOT) { | |
d9e2daaf LP |
218 | if (rmdir(path) < 0) { |
219 | if (r == 0 && errno != ENOENT) | |
c6878637 LP |
220 | r = -errno; |
221 | } | |
222 | } | |
223 | ||
224 | return r; | |
225 | } |