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