chiark / gitweb /
time-util: when formatting usec_t as raw integers use PRIu64
[elogind.git] / src / basic / rm-rf.c
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
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
28 //#include "btrfs-util.h"
29 #include "cgroup-util.h"
30 #include "fd-util.h"
31 #include "log.h"
32 #include "macro.h"
33 #include "mount-util.h"
34 #include "path-util.h"
35 #include "rm-rf.h"
36 #include "stat-util.h"
37 #include "string-util.h"
38
39 static bool is_physical_fs(const struct statfs *sfs) {
40         return !is_temporary_fs(sfs) && !is_cgroup_fs(sfs);
41 }
42
43 int rm_rf_children(int fd, RemoveFlags flags, struct stat *root_dev) {
44         _cleanup_closedir_ DIR *d = NULL;
45         struct dirent *de;
46         int ret = 0, r;
47         struct statfs sfs;
48
49         assert(fd >= 0);
50
51         /* This returns the first error we run into, but nevertheless
52          * tries to go on. This closes the passed fd. */
53
54         if (!(flags & REMOVE_PHYSICAL)) {
55
56                 r = fstatfs(fd, &sfs);
57                 if (r < 0) {
58                         safe_close(fd);
59                         return -errno;
60                 }
61
62                 if (is_physical_fs(&sfs)) {
63                         /* We refuse to clean physical file systems
64                          * with this call, unless explicitly
65                          * requested. This is extra paranoia just to
66                          * be sure we never ever remove non-state
67                          * data */
68
69                         log_error("Attempted to remove disk file system, and we can't allow that.");
70                         safe_close(fd);
71                         return -EPERM;
72                 }
73         }
74
75         d = fdopendir(fd);
76         if (!d) {
77                 safe_close(fd);
78                 return errno == ENOENT ? 0 : -errno;
79         }
80
81         FOREACH_DIRENT_ALL(de, d, return -errno) {
82                 bool is_dir;
83                 struct stat st;
84
85                 if (dot_or_dot_dot(de->d_name))
86                         continue;
87
88                 if (de->d_type == DT_UNKNOWN ||
89                     (de->d_type == DT_DIR && (root_dev || (flags & REMOVE_SUBVOLUME)))) {
90                         if (fstatat(fd, de->d_name, &st, AT_SYMLINK_NOFOLLOW) < 0) {
91                                 if (ret == 0 && errno != ENOENT)
92                                         ret = -errno;
93                                 continue;
94                         }
95
96                         is_dir = S_ISDIR(st.st_mode);
97                 } else
98                         is_dir = de->d_type == DT_DIR;
99
100                 if (is_dir) {
101                         int subdir_fd;
102
103                         /* if root_dev is set, remove subdirectories only if device is same */
104                         if (root_dev && st.st_dev != root_dev->st_dev)
105                                 continue;
106
107                         subdir_fd = openat(fd, de->d_name, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME);
108                         if (subdir_fd < 0) {
109                                 if (ret == 0 && errno != ENOENT)
110                                         ret = -errno;
111                                 continue;
112                         }
113
114                         /* Stop at mount points */
115                         r = fd_is_mount_point(fd, de->d_name, 0);
116                         if (r < 0) {
117                                 if (ret == 0 && r != -ENOENT)
118                                         ret = r;
119
120                                 safe_close(subdir_fd);
121                                 continue;
122                         }
123                         if (r) {
124                                 safe_close(subdir_fd);
125                                 continue;
126                         }
127
128 #if 0 /// elogind does not support BTRFS this directly
129                         if ((flags & REMOVE_SUBVOLUME) && st.st_ino == 256) {
130
131                                 /* This could be a subvolume, try to remove it */
132
133                                 r = btrfs_subvol_remove_fd(fd, de->d_name, BTRFS_REMOVE_RECURSIVE|BTRFS_REMOVE_QUOTA);
134                                 if (r < 0) {
135                                         if (r != -ENOTTY && r != -EINVAL) {
136                                                 if (ret == 0)
137                                                         ret = r;
138
139                                                 safe_close(subdir_fd);
140                                                 continue;
141                                         }
142
143                                         /* ENOTTY, then it wasn't a
144                                          * btrfs subvolume, continue
145                                          * below. */
146                                 } else {
147                                         /* It was a subvolume, continue. */
148                                         safe_close(subdir_fd);
149                                         continue;
150                                 }
151                         }
152 #endif // 0
153
154                         /* We pass REMOVE_PHYSICAL here, to avoid
155                          * doing the fstatfs() to check the file
156                          * system type again for each directory */
157                         r = rm_rf_children(subdir_fd, flags | REMOVE_PHYSICAL, root_dev);
158                         if (r < 0 && ret == 0)
159                                 ret = r;
160
161                         if (unlinkat(fd, de->d_name, AT_REMOVEDIR) < 0) {
162                                 if (ret == 0 && errno != ENOENT)
163                                         ret = -errno;
164                         }
165
166                 } else if (!(flags & REMOVE_ONLY_DIRECTORIES)) {
167
168                         if (unlinkat(fd, de->d_name, 0) < 0) {
169                                 if (ret == 0 && errno != ENOENT)
170                                         ret = -errno;
171                         }
172                 }
173         }
174         return ret;
175 }
176
177 int rm_rf(const char *path, RemoveFlags flags) {
178         int fd, r;
179         struct statfs s;
180
181         assert(path);
182
183         /* We refuse to clean the root file system with this
184          * call. This is extra paranoia to never cause a really
185          * seriously broken system. */
186         if (path_equal(path, "/")) {
187                 log_error("Attempted to remove entire root file system, and we can't allow that.");
188                 return -EPERM;
189         }
190
191 #if 0 /// elogind does not support BTRFS this directly
192         if ((flags & (REMOVE_SUBVOLUME|REMOVE_ROOT|REMOVE_PHYSICAL)) == (REMOVE_SUBVOLUME|REMOVE_ROOT|REMOVE_PHYSICAL)) {
193                 /* Try to remove as subvolume first */
194                 r = btrfs_subvol_remove(path, BTRFS_REMOVE_RECURSIVE|BTRFS_REMOVE_QUOTA);
195                 if (r >= 0)
196                         return r;
197
198                 if (r != -ENOTTY && r != -EINVAL && r != -ENOTDIR)
199                         return r;
200
201                 /* Not btrfs or not a subvolume */
202         }
203 #endif // 0
204
205         fd = open(path, O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC|O_NOFOLLOW|O_NOATIME);
206         if (fd < 0) {
207
208                 if (errno != ENOTDIR && errno != ELOOP)
209                         return -errno;
210
211                 if (!(flags & REMOVE_PHYSICAL)) {
212                         if (statfs(path, &s) < 0)
213                                 return -errno;
214
215                         if (is_physical_fs(&s)) {
216                                 log_error("Attempted to remove disk file system, and we can't allow that.");
217                                 return -EPERM;
218                         }
219                 }
220
221                 if ((flags & REMOVE_ROOT) && !(flags & REMOVE_ONLY_DIRECTORIES))
222                         if (unlink(path) < 0 && errno != ENOENT)
223                                 return -errno;
224
225                 return 0;
226         }
227
228         r = rm_rf_children(fd, flags, NULL);
229
230         if (flags & REMOVE_ROOT) {
231                 if (rmdir(path) < 0) {
232                         if (r == 0 && errno != ENOENT)
233                                 r = -errno;
234                 }
235         }
236
237         return r;
238 }