1 /* SPDX-License-Identifier: LGPL-2.1+ */
3 This file is part of systemd.
5 Copyright 2014 Lennart Poettering
7 systemd is free software; you can redistribute it and/or modify it
8 under the terms of the GNU Lesser General Public License as published by
9 the Free Software Foundation; either version 2.1 of the License, or
10 (at your option) any later version.
12 systemd is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
17 You should have received a copy of the GNU Lesser General Public License
18 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 #include <sys/statvfs.h>
23 #include "alloc-util.h"
24 #include "coredump-vacuum.h"
25 #include "dirent-util.h"
29 #include "string-util.h"
30 #include "time-util.h"
31 #include "user-util.h"
34 #define DEFAULT_MAX_USE_LOWER (uint64_t) (1ULL*1024ULL*1024ULL) /* 1 MiB */
35 #define DEFAULT_MAX_USE_UPPER (uint64_t) (4ULL*1024ULL*1024ULL*1024ULL) /* 4 GiB */
36 #define DEFAULT_KEEP_FREE_UPPER (uint64_t) (4ULL*1024ULL*1024ULL*1024ULL) /* 4 GiB */
37 #define DEFAULT_KEEP_FREE (uint64_t) (1024ULL*1024ULL) /* 1 MB */
39 struct vacuum_candidate
{
45 static void vacuum_candidate_free(struct vacuum_candidate
*c
) {
53 DEFINE_TRIVIAL_CLEANUP_FUNC(struct vacuum_candidate
*, vacuum_candidate_free
);
55 static void vacuum_candidate_hasmap_free(Hashmap
*h
) {
56 struct vacuum_candidate
*c
;
58 while ((c
= hashmap_steal_first(h
)))
59 vacuum_candidate_free(c
);
64 DEFINE_TRIVIAL_CLEANUP_FUNC(Hashmap
*, vacuum_candidate_hasmap_free
);
66 static int uid_from_file_name(const char *filename
, uid_t
*uid
) {
67 const char *p
, *e
, *u
;
69 p
= startswith(filename
, "core.");
73 /* Skip the comm field */
85 return parse_uid(u
, uid
);
88 static bool vacuum_necessary(int fd
, uint64_t sum
, uint64_t keep_free
, uint64_t max_use
) {
89 uint64_t fs_size
= 0, fs_free
= (uint64_t) -1;
94 if (fstatvfs(fd
, &sv
) >= 0) {
95 fs_size
= sv
.f_frsize
* sv
.f_blocks
;
96 fs_free
= sv
.f_frsize
* sv
.f_bfree
;
99 if (max_use
== (uint64_t) -1) {
102 max_use
= PAGE_ALIGN(fs_size
/ 10); /* 10% */
104 if (max_use
> DEFAULT_MAX_USE_UPPER
)
105 max_use
= DEFAULT_MAX_USE_UPPER
;
107 if (max_use
< DEFAULT_MAX_USE_LOWER
)
108 max_use
= DEFAULT_MAX_USE_LOWER
;
110 max_use
= DEFAULT_MAX_USE_LOWER
;
112 max_use
= PAGE_ALIGN(max_use
);
114 if (max_use
> 0 && sum
> max_use
)
117 if (keep_free
== (uint64_t) -1) {
120 keep_free
= PAGE_ALIGN((fs_size
* 3) / 20); /* 15% */
122 if (keep_free
> DEFAULT_KEEP_FREE_UPPER
)
123 keep_free
= DEFAULT_KEEP_FREE_UPPER
;
125 keep_free
= DEFAULT_KEEP_FREE
;
127 keep_free
= PAGE_ALIGN(keep_free
);
129 if (keep_free
> 0 && fs_free
< keep_free
)
135 int coredump_vacuum(int exclude_fd
, uint64_t keep_free
, uint64_t max_use
) {
136 _cleanup_closedir_
DIR *d
= NULL
;
137 struct stat exclude_st
;
140 if (keep_free
== 0 && max_use
== 0)
143 if (exclude_fd
>= 0) {
144 if (fstat(exclude_fd
, &exclude_st
) < 0)
145 return log_error_errno(errno
, "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 return log_error_errno(errno
, "Can't open coredump directory: %m");
163 _cleanup_(vacuum_candidate_hasmap_freep
) Hashmap
*h
= NULL
;
164 struct vacuum_candidate
*worst
= NULL
;
170 FOREACH_DIRENT(de
, d
, goto fail
) {
171 struct vacuum_candidate
*c
;
176 r
= uid_from_file_name(de
->d_name
, &uid
);
180 if (fstatat(dirfd(d
), de
->d_name
, &st
, AT_NO_AUTOMOUNT
|AT_SYMLINK_NOFOLLOW
) < 0) {
184 log_warning_errno(errno
, "Failed to stat /var/lib/systemd/coredump/%s: %m", de
->d_name
);
188 if (!S_ISREG(st
.st_mode
))
191 if (exclude_fd
>= 0 &&
192 exclude_st
.st_dev
== st
.st_dev
&&
193 exclude_st
.st_ino
== st
.st_ino
)
196 r
= hashmap_ensure_allocated(&h
, NULL
);
200 t
= timespec_load(&st
.st_mtim
);
202 c
= hashmap_get(h
, UID_TO_PTR(uid
));
205 if (t
< c
->oldest_mtime
) {
208 n
= strdup(de
->d_name
);
212 free(c
->oldest_file
);
218 _cleanup_(vacuum_candidate_freep
) struct vacuum_candidate
*n
= NULL
;
220 n
= new0(struct vacuum_candidate
, 1);
224 n
->oldest_file
= strdup(de
->d_name
);
230 r
= hashmap_put(h
, UID_TO_PTR(uid
), n
);
241 worst
->n_files
< c
->n_files
||
242 (worst
->n_files
== c
->n_files
&& c
->oldest_mtime
< worst
->oldest_mtime
))
245 sum
+= st
.st_blocks
* 512;
251 r
= vacuum_necessary(dirfd(d
), sum
, keep_free
, max_use
);
255 if (unlinkat(dirfd(d
), worst
->oldest_file
, 0) < 0) {
260 return log_error_errno(errno
, "Failed to remove file %s: %m", worst
->oldest_file
);
262 log_info("Removed old coredump %s.", worst
->oldest_file
);
268 return log_error_errno(errno
, "Failed to read directory: %m");