]>
Commit | Line | Data |
---|---|---|
c6878637 LP |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2015 Lennart Poettering | |
5 | ||
6 | systemd is free software; you can redistribute it and/or modify it | |
7 | under the terms of the GNU Lesser General Public License as published by | |
8 | the Free Software Foundation; either version 2.1 of the License, or | |
9 | (at your option) any later version. | |
10 | ||
11 | systemd is distributed in the hope that it will be useful, but | |
12 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
14 | Lesser General Public License for more details. | |
15 | ||
16 | You should have received a copy of the GNU Lesser General Public License | |
17 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
18 | ***/ | |
19 | ||
11c3a366 TA |
20 | #include <errno.h> |
21 | #include <fcntl.h> | |
22 | #include <stdbool.h> | |
23 | #include <stddef.h> | |
24 | #include <sys/stat.h> | |
25 | #include <sys/statfs.h> | |
26 | #include <unistd.h> | |
27 | ||
d9e2daaf | 28 | #include "btrfs-util.h" |
f0bef277 | 29 | #include "cgroup-util.h" |
8fb3f009 | 30 | #include "dirent-util.h" |
3ffd4af2 | 31 | #include "fd-util.h" |
93cc7779 TA |
32 | #include "log.h" |
33 | #include "macro.h" | |
4349cd7c | 34 | #include "mount-util.h" |
07630cea | 35 | #include "path-util.h" |
3ffd4af2 | 36 | #include "rm-rf.h" |
8fcde012 | 37 | #include "stat-util.h" |
07630cea | 38 | #include "string-util.h" |
c6878637 | 39 | |
f0bef277 EV |
40 | static bool is_physical_fs(const struct statfs *sfs) { |
41 | return !is_temporary_fs(sfs) && !is_cgroup_fs(sfs); | |
42 | } | |
43 | ||
c6878637 LP |
44 | int rm_rf_children(int fd, RemoveFlags flags, struct stat *root_dev) { |
45 | _cleanup_closedir_ DIR *d = NULL; | |
8fb3f009 | 46 | struct dirent *de; |
c6878637 | 47 | int ret = 0, r; |
f0bef277 | 48 | struct statfs sfs; |
c6878637 LP |
49 | |
50 | assert(fd >= 0); | |
51 | ||
52 | /* This returns the first error we run into, but nevertheless | |
53 | * tries to go on. This closes the passed fd. */ | |
54 | ||
55 | if (!(flags & REMOVE_PHYSICAL)) { | |
56 | ||
f0bef277 | 57 | r = fstatfs(fd, &sfs); |
c6878637 LP |
58 | if (r < 0) { |
59 | safe_close(fd); | |
f0bef277 | 60 | return -errno; |
c6878637 LP |
61 | } |
62 | ||
f0bef277 | 63 | if (is_physical_fs(&sfs)) { |
c6878637 LP |
64 | /* We refuse to clean physical file systems |
65 | * with this call, unless explicitly | |
66 | * requested. This is extra paranoia just to | |
67 | * be sure we never ever remove non-state | |
68 | * data */ | |
69 | ||
70 | log_error("Attempted to remove disk file system, and we can't allow that."); | |
71 | safe_close(fd); | |
72 | return -EPERM; | |
73 | } | |
74 | } | |
75 | ||
76 | d = fdopendir(fd); | |
77 | if (!d) { | |
78 | safe_close(fd); | |
79 | return errno == ENOENT ? 0 : -errno; | |
80 | } | |
81 | ||
8fb3f009 | 82 | FOREACH_DIRENT_ALL(de, d, return -errno) { |
c6878637 LP |
83 | bool is_dir; |
84 | struct stat st; | |
85 | ||
49bfc877 | 86 | if (dot_or_dot_dot(de->d_name)) |
c6878637 LP |
87 | continue; |
88 | ||
9e9b663a LP |
89 | if (de->d_type == DT_UNKNOWN || |
90 | (de->d_type == DT_DIR && (root_dev || (flags & REMOVE_SUBVOLUME)))) { | |
c6878637 LP |
91 | if (fstatat(fd, de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) { |
92 | if (ret == 0 && errno != ENOENT) | |
93 | ret = -errno; | |
94 | continue; | |
95 | } | |
96 | ||
97 | is_dir = S_ISDIR(st.st_mode); | |
98 | } else | |
99 | is_dir = de->d_type == DT_DIR; | |
100 | ||
101 | if (is_dir) { | |
102 | int subdir_fd; | |
103 | ||
f25afeb6 | 104 | /* if root_dev is set, remove subdirectories only if device is same */ |
c6878637 LP |
105 | if (root_dev && st.st_dev != root_dev->st_dev) |
106 | continue; | |
107 | ||
108 | subdir_fd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME); | |
109 | if (subdir_fd < 0) { | |
110 | if (ret == 0 && errno != ENOENT) | |
111 | ret = -errno; | |
112 | continue; | |
113 | } | |
114 | ||
f25afeb6 | 115 | /* Stop at mount points */ |
5d409034 | 116 | r = fd_is_mount_point(fd, de->d_name, 0); |
f25afeb6 LP |
117 | if (r < 0) { |
118 | if (ret == 0 && r != -ENOENT) | |
119 | ret = r; | |
120 | ||
121 | safe_close(subdir_fd); | |
122 | continue; | |
123 | } | |
124 | if (r) { | |
125 | safe_close(subdir_fd); | |
126 | continue; | |
127 | } | |
128 | ||
9e9b663a | 129 | if ((flags & REMOVE_SUBVOLUME) && st.st_ino == 256) { |
9e9b663a LP |
130 | |
131 | /* This could be a subvolume, try to remove it */ | |
132 | ||
5bcd08db | 133 | r = btrfs_subvol_remove_fd(fd, de->d_name, BTRFS_REMOVE_RECURSIVE|BTRFS_REMOVE_QUOTA); |
d9e2daaf LP |
134 | if (r < 0) { |
135 | if (r != -ENOTTY && r != -EINVAL) { | |
9e9b663a | 136 | if (ret == 0) |
d9e2daaf | 137 | ret = r; |
9e9b663a LP |
138 | |
139 | safe_close(subdir_fd); | |
140 | continue; | |
141 | } | |
142 | ||
d9e2daaf LP |
143 | /* ENOTTY, then it wasn't a |
144 | * btrfs subvolume, continue | |
145 | * below. */ | |
9e9b663a LP |
146 | } else { |
147 | /* It was a subvolume, continue. */ | |
148 | safe_close(subdir_fd); | |
149 | continue; | |
150 | } | |
151 | } | |
152 | ||
c6878637 LP |
153 | /* We pass REMOVE_PHYSICAL here, to avoid |
154 | * doing the fstatfs() to check the file | |
155 | * system type again for each directory */ | |
156 | r = rm_rf_children(subdir_fd, flags | REMOVE_PHYSICAL, root_dev); | |
157 | if (r < 0 && ret == 0) | |
158 | ret = r; | |
159 | ||
160 | if (unlinkat(fd, de->d_name, AT_REMOVEDIR) < 0) { | |
161 | if (ret == 0 && errno != ENOENT) | |
162 | ret = -errno; | |
163 | } | |
164 | ||
165 | } else if (!(flags & REMOVE_ONLY_DIRECTORIES)) { | |
166 | ||
167 | if (unlinkat(fd, de->d_name, 0) < 0) { | |
168 | if (ret == 0 && errno != ENOENT) | |
169 | ret = -errno; | |
170 | } | |
171 | } | |
172 | } | |
8fb3f009 | 173 | return ret; |
c6878637 LP |
174 | } |
175 | ||
176 | int rm_rf(const char *path, RemoveFlags flags) { | |
177 | int fd, r; | |
178 | struct statfs s; | |
179 | ||
180 | assert(path); | |
181 | ||
182 | /* We refuse to clean the root file system with this | |
183 | * call. This is extra paranoia to never cause a really | |
184 | * seriously broken system. */ | |
7cb53855 | 185 | if (path_equal_or_files_same(path, "/", AT_SYMLINK_NOFOLLOW)) { |
c6878637 LP |
186 | log_error("Attempted to remove entire root file system, and we can't allow that."); |
187 | return -EPERM; | |
188 | } | |
189 | ||
d9e2daaf LP |
190 | if ((flags & (REMOVE_SUBVOLUME|REMOVE_ROOT|REMOVE_PHYSICAL)) == (REMOVE_SUBVOLUME|REMOVE_ROOT|REMOVE_PHYSICAL)) { |
191 | /* Try to remove as subvolume first */ | |
5bcd08db | 192 | r = btrfs_subvol_remove(path, BTRFS_REMOVE_RECURSIVE|BTRFS_REMOVE_QUOTA); |
d9e2daaf LP |
193 | if (r >= 0) |
194 | return r; | |
195 | ||
636aabc2 | 196 | if (r != -ENOTTY && r != -EINVAL && r != -ENOTDIR) |
d9e2daaf LP |
197 | return r; |
198 | ||
199 | /* Not btrfs or not a subvolume */ | |
200 | } | |
201 | ||
c6878637 LP |
202 | fd = open(path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME); |
203 | if (fd < 0) { | |
204 | ||
205 | if (errno != ENOTDIR && errno != ELOOP) | |
206 | return -errno; | |
207 | ||
208 | if (!(flags & REMOVE_PHYSICAL)) { | |
209 | if (statfs(path, &s) < 0) | |
210 | return -errno; | |
211 | ||
f0bef277 | 212 | if (is_physical_fs(&s)) { |
c6878637 LP |
213 | log_error("Attempted to remove disk file system, and we can't allow that."); |
214 | return -EPERM; | |
215 | } | |
216 | } | |
217 | ||
218 | if ((flags & REMOVE_ROOT) && !(flags & REMOVE_ONLY_DIRECTORIES)) | |
219 | if (unlink(path) < 0 && errno != ENOENT) | |
220 | return -errno; | |
221 | ||
222 | return 0; | |
223 | } | |
224 | ||
225 | r = rm_rf_children(fd, flags, NULL); | |
226 | ||
227 | if (flags & REMOVE_ROOT) { | |
d9e2daaf LP |
228 | if (rmdir(path) < 0) { |
229 | if (r == 0 && errno != ENOENT) | |
c6878637 LP |
230 | r = -errno; |
231 | } | |
232 | } | |
233 | ||
234 | return r; | |
235 | } |