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