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>
24 #include "alloc-util.h"
25 #include "coredump-vacuum.h"
26 #include "dirent-util.h"
30 #include "string-util.h"
31 #include "time-util.h"
32 #include "user-util.h"
35 #define DEFAULT_MAX_USE_LOWER (uint64_t) (1ULL*1024ULL*1024ULL) /* 1 MiB */
36 #define DEFAULT_MAX_USE_UPPER (uint64_t) (4ULL*1024ULL*1024ULL*1024ULL) /* 4 GiB */
37 #define DEFAULT_KEEP_FREE_UPPER (uint64_t) (4ULL*1024ULL*1024ULL*1024ULL) /* 4 GiB */
38 #define DEFAULT_KEEP_FREE (uint64_t) (1024ULL*1024ULL) /* 1 MB */
40 struct vacuum_candidate
{
46 static void vacuum_candidate_free(struct vacuum_candidate
*c
) {
54 DEFINE_TRIVIAL_CLEANUP_FUNC(struct vacuum_candidate
*, vacuum_candidate_free
);
56 static void vacuum_candidate_hasmap_free(Hashmap
*h
) {
57 struct vacuum_candidate
*c
;
59 while ((c
= hashmap_steal_first(h
)))
60 vacuum_candidate_free(c
);
65 DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap
*, vacuum_candidate_hasmap_free
);
67 static int uid_from_file_name(const char *filename
, uid_t
*uid
) {
68 const char *p
, *e
, *u
;
70 p
= startswith(filename
, "core.");
74 /* Skip the comm field */
86 return parse_uid(u
, uid
);
89 static bool vacuum_necessary(int fd
, uint64_t sum
, uint64_t keep_free
, uint64_t max_use
) {
90 uint64_t fs_size
= 0, fs_free
= (uint64_t) -1;
95 if (fstatvfs(fd
, &sv
) >= 0) {
96 fs_size
= sv
.f_frsize
* sv
.f_blocks
;
97 fs_free
= sv
.f_frsize
* sv
.f_bfree
;
100 if (max_use
== (uint64_t) -1) {
103 max_use
= PAGE_ALIGN(fs_size
/ 10); /* 10% */
105 if (max_use
> DEFAULT_MAX_USE_UPPER
)
106 max_use
= DEFAULT_MAX_USE_UPPER
;
108 if (max_use
< DEFAULT_MAX_USE_LOWER
)
109 max_use
= DEFAULT_MAX_USE_LOWER
;
111 max_use
= DEFAULT_MAX_USE_LOWER
;
113 max_use
= PAGE_ALIGN(max_use
);
115 if (max_use
> 0 && sum
> max_use
)
118 if (keep_free
== (uint64_t) -1) {
121 keep_free
= PAGE_ALIGN((fs_size
* 3) / 20); /* 15% */
123 if (keep_free
> DEFAULT_KEEP_FREE_UPPER
)
124 keep_free
= DEFAULT_KEEP_FREE_UPPER
;
126 keep_free
= DEFAULT_KEEP_FREE
;
128 keep_free
= PAGE_ALIGN(keep_free
);
130 if (keep_free
> 0 && fs_free
< keep_free
)
136 int coredump_vacuum(int exclude_fd
, uint64_t keep_free
, uint64_t max_use
) {
137 _cleanup_closedir_
DIR *d
= NULL
;
138 struct stat exclude_st
;
141 if (keep_free
== 0 && max_use
== 0)
144 if (exclude_fd
>= 0) {
145 if (fstat(exclude_fd
, &exclude_st
) < 0)
146 return log_error_errno(errno
, "Failed to fstat(): %m");
149 /* This algorithm will keep deleting the oldest file of the
150 * user with the most coredumps until we are back in the size
151 * limits. Note that vacuuming for journal files is different,
152 * because we rely on rate-limiting of the messages there,
153 * to avoid being flooded. */
155 d
= opendir("/var/lib/systemd/coredump");
160 log_error_errno(errno
, "Can't open coredump directory: %m");
165 _cleanup_(vacuum_candidate_hasmap_freep
) Hashmap
*h
= NULL
;
166 struct vacuum_candidate
*worst
= NULL
;
172 FOREACH_DIRENT(de
, d
, goto fail
) {
173 struct vacuum_candidate
*c
;
178 r
= uid_from_file_name(de
->d_name
, &uid
);
182 if (fstatat(dirfd(d
), de
->d_name
, &st
, AT_NO_AUTOMOUNT
|AT_SYMLINK_NOFOLLOW
) < 0) {
186 log_warning("Failed to stat /var/lib/systemd/coredump/%s", de
->d_name
);
190 if (!S_ISREG(st
.st_mode
))
193 if (exclude_fd
>= 0 &&
194 exclude_st
.st_dev
== st
.st_dev
&&
195 exclude_st
.st_ino
== st
.st_ino
)
198 r
= hashmap_ensure_allocated(&h
, NULL
);
202 t
= timespec_load(&st
.st_mtim
);
204 c
= hashmap_get(h
, UINT32_TO_PTR(uid
));
207 if (t
< c
->oldest_mtime
) {
210 n
= strdup(de
->d_name
);
214 free(c
->oldest_file
);
220 _cleanup_(vacuum_candidate_freep
) struct vacuum_candidate
*n
= NULL
;
222 n
= new0(struct vacuum_candidate
, 1);
226 n
->oldest_file
= strdup(de
->d_name
);
232 r
= hashmap_put(h
, UINT32_TO_PTR(uid
), n
);
243 worst
->n_files
< c
->n_files
||
244 (worst
->n_files
== c
->n_files
&& c
->oldest_mtime
< worst
->oldest_mtime
))
247 sum
+= st
.st_blocks
* 512;
253 r
= vacuum_necessary(dirfd(d
), sum
, keep_free
, max_use
);
257 if (unlinkat(dirfd(d
), worst
->oldest_file
, 0) < 0) {
262 log_error_errno(errno
, "Failed to remove file %s: %m", worst
->oldest_file
);
265 log_info("Removed old coredump %s.", worst
->oldest_file
);
271 log_error_errno(errno
, "Failed to read directory: %m");