coredump-vacuum.c revision 59f448cf15f94bc5ebfd5b254de6f2441d02fbec
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
/***
This file is part of systemd.
Copyright 2014 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include "util.h"
#include "time-util.h"
#include "hashmap.h"
#include "macro.h"
#include "coredump-vacuum.h"
struct vacuum_candidate {
unsigned n_files;
char *oldest_file;
};
static void vacuum_candidate_free(struct vacuum_candidate *c) {
if (!c)
return;
free(c->oldest_file);
free(c);
}
static void vacuum_candidate_hasmap_free(Hashmap *h) {
struct vacuum_candidate *c;
while ((c = hashmap_steal_first(h)))
hashmap_free(h);
}
const char *p, *e, *u;
if (!p)
return -EINVAL;
/* Skip the comm field */
p = strchr(p, '.');
if (!p)
return -EINVAL;
p++;
/* Find end up UID */
e = strchr(p, '.');
if (!e)
return -EINVAL;
u = strndupa(p, e-p);
}
}
if (fs_size > 0) {
if (max_use > DEFAULT_MAX_USE_UPPER)
if (max_use < DEFAULT_MAX_USE_LOWER)
} else
} else
return true;
if (fs_size > 0) {
if (keep_free > DEFAULT_KEEP_FREE_UPPER)
} else
} else
return true;
return false;
}
struct stat exclude_st;
int r;
return 0;
if (exclude_fd >= 0) {
}
/* This algorithm will keep deleting the oldest file of the
* user with the most coredumps until we are back in the size
* limits. Note that vacuuming for journal files is different,
* because we rely on rate-limiting of the messages there,
* to avoid being flooded. */
if (!d) {
return 0;
return -errno;
}
for (;;) {
rewinddir(d);
struct vacuum_candidate *c;
usec_t t;
if (r < 0)
continue;
continue;
continue;
}
continue;
if (exclude_fd >= 0 &&
continue;
r = hashmap_ensure_allocated(&h, NULL);
if (r < 0)
return log_oom();
if (c) {
if (t < c->oldest_mtime) {
char *n;
if (!n)
return log_oom();
free(c->oldest_file);
c->oldest_file = n;
c->oldest_mtime = t;
}
} else {
if (!n)
return log_oom();
if (!n->oldest_file)
return log_oom();
n->oldest_mtime = t;
if (r < 0)
return log_oom();
c = n;
n = NULL;
}
c->n_files++;
if (!worst ||
worst = c;
}
if (!worst)
break;
if (r <= 0)
return r;
continue;
return -errno;
} else
}
return 0;
fail:
return -errno;
}