1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2014 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/statvfs.h>
25 #include "time-util.h"
29 #include "coredump-vacuum.h"
31 #define DEFAULT_MAX_USE_LOWER (off_t) (1ULL*1024ULL*1024ULL) /* 1 MiB */
32 #define DEFAULT_MAX_USE_UPPER (off_t) (4ULL*1024ULL*1024ULL*1024ULL) /* 4 GiB */
33 #define DEFAULT_KEEP_FREE_UPPER (off_t) (4ULL*1024ULL*1024ULL*1024ULL) /* 4 GiB */
34 #define DEFAULT_KEEP_FREE (off_t) (1024ULL*1024ULL) /* 1 MB */
36 struct vacuum_candidate {
42 static void vacuum_candidate_free(struct vacuum_candidate *c) {
50 DEFINE_TRIVIAL_CLEANUP_FUNC(struct vacuum_candidate*, vacuum_candidate_free);
52 static void vacuum_candidate_hasmap_free(Hashmap *h) {
53 struct vacuum_candidate *c;
55 while ((c = hashmap_steal_first(h)))
56 vacuum_candidate_free(c);
61 DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap*, vacuum_candidate_hasmap_free);
63 static int uid_from_file_name(const char *filename, uid_t *uid) {
64 const char *p, *e, *u;
66 p = startswith(filename, "core.");
70 /* Skip the comm field */
82 return parse_uid(u, uid);
85 static bool vacuum_necessary(int fd, off_t sum, off_t keep_free, off_t max_use) {
86 off_t fs_size = 0, fs_free = (off_t) -1;
91 if (fstatvfs(fd, &sv) >= 0) {
92 fs_size = sv.f_frsize * sv.f_blocks;
93 fs_free = sv.f_frsize * sv.f_bfree;
96 if (max_use == (off_t) -1) {
99 max_use = PAGE_ALIGN(fs_size / 10); /* 10% */
101 if (max_use > DEFAULT_MAX_USE_UPPER)
102 max_use = DEFAULT_MAX_USE_UPPER;
104 if (max_use < DEFAULT_MAX_USE_LOWER)
105 max_use = DEFAULT_MAX_USE_LOWER;
108 max_use = DEFAULT_MAX_USE_LOWER;
110 max_use = PAGE_ALIGN(max_use);
112 if (max_use > 0 && sum > max_use)
115 if (keep_free == (off_t) -1) {
118 keep_free = PAGE_ALIGN((fs_size * 3) / 20); /* 15% */
120 if (keep_free > DEFAULT_KEEP_FREE_UPPER)
121 keep_free = DEFAULT_KEEP_FREE_UPPER;
123 keep_free = DEFAULT_KEEP_FREE;
125 keep_free = PAGE_ALIGN(keep_free);
127 if (keep_free > 0 && fs_free < keep_free)
133 int coredump_vacuum(int exclude_fd, off_t keep_free, off_t max_use) {
134 _cleanup_closedir_ DIR *d = NULL;
135 struct stat exclude_st;
138 if (keep_free <= 0 && max_use <= 0)
141 if (exclude_fd >= 0) {
142 if (fstat(exclude_fd, &exclude_st) < 0) {
143 log_error("Failed to fstat(): %m");
148 /* This algorithm will keep deleting the oldest file of the
149 * user with the most coredumps until we are back in the size
150 * limits. Note that vacuuming for journal files is different,
151 * because we rely on rate-limiting of the messages there,
152 * to avoid being flooded. */
154 d = opendir("/var/lib/systemd/coredump");
159 log_error("Can't open coredump directory: %m");
164 _cleanup_(vacuum_candidate_hasmap_freep) Hashmap *h = NULL;
165 struct vacuum_candidate *worst = NULL;
171 FOREACH_DIRENT(de, d, goto fail) {
172 struct vacuum_candidate *c;
177 r = uid_from_file_name(de->d_name, &uid);
181 if (fstatat(dirfd(d), de->d_name, &st, AT_NO_AUTOMOUNT|AT_SYMLINK_NOFOLLOW) < 0) {
185 log_warning("Failed to stat /var/lib/systemd/coredump/%s", de->d_name);
189 if (!S_ISREG(st.st_mode))
192 if (exclude_fd >= 0 &&
193 exclude_st.st_dev == st.st_dev &&
194 exclude_st.st_ino == st.st_ino)
197 r = hashmap_ensure_allocated(&h, NULL, NULL);
201 t = timespec_load(&st.st_mtim);
203 c = hashmap_get(h, UINT32_TO_PTR(uid));
206 if (t < c->oldest_mtime) {
209 n = strdup(de->d_name);
213 free(c->oldest_file);
219 _cleanup_(vacuum_candidate_freep) struct vacuum_candidate *n = NULL;
221 n = new0(struct vacuum_candidate, 1);
225 n->oldest_file = strdup(de->d_name);
231 r = hashmap_put(h, UINT32_TO_PTR(uid), n);
242 worst->n_files < c->n_files ||
243 (worst->n_files == c->n_files && c->oldest_mtime < worst->oldest_mtime))
246 sum += st.st_blocks * 512;
252 r = vacuum_necessary(dirfd(d), sum, keep_free, max_use);
256 if (unlinkat(dirfd(d), worst->oldest_file, 0) < 0) {
261 log_error("Failed to remove file %s: %m", worst->oldest_file);
264 log_info("Removed old coredump %s.", worst->oldest_file);
270 log_error("Failed to read directory: %m");