return -errno;
}
- r = copy_bytes(STDIN_FILENO, fd);
- if (r < 0) {
+ r = copy_bytes(STDIN_FILENO, fd, arg_process_size_max);
+ if (r == -E2BIG) {
+ log_error("Coredump of %s (%s) is larger than configured processing limit, refusing.", argv[ARG_PID], argv[ARG_COMM]);
+ goto fail;
+ } else if (IN_SET(r, -EDQUOT, -ENOSPC)) {
+ log_error("Not enough disk space for coredump of %s (%s), refusing.", argv[ARG_PID], argv[ARG_COMM]);
+ goto fail;
+ } else if (r < 0) {
log_error("Failed to dump coredump to file: %s", strerror(-r));
goto fail;
}
return -errno;
}
- r = copy_bytes(fd, output ? fileno(output) : STDOUT_FILENO);
+ r = copy_bytes(fd, output ? fileno(output) : STDOUT_FILENO, (off_t) -1);
if (r < 0) {
log_error("Failed to stream coredump: %s", strerror(-r));
return r;
#include "util.h"
#include "copy.h"
-int copy_bytes(int fdf, int fdt) {
+int copy_bytes(int fdf, int fdt, off_t max_bytes) {
assert(fdf >= 0);
assert(fdt >= 0);
for (;;) {
char buf[PIPE_BUF];
ssize_t n, k;
+ size_t m;
- n = read(fdf, buf, sizeof(buf));
+ m = sizeof(buf);
+
+ if (max_bytes != (off_t) -1) {
+
+ if (max_bytes <= 0)
+ return -E2BIG;
+
+ if ((off_t) m > max_bytes)
+ m = (size_t) max_bytes;
+ }
+
+ n = read(fdf, buf, m);
if (n < 0)
return -errno;
if (n == 0)
return k;
if (k != n)
return errno ? -errno : -EIO;
+
+ if (max_bytes != (off_t) -1) {
+ assert(max_bytes >= n);
+ max_bytes -= n;
+ }
}
return 0;
if (fdt < 0)
return -errno;
- r = copy_bytes(fdf, fdt);
+ r = copy_bytes(fdf, fdt, (off_t) -1);
if (r < 0) {
unlinkat(dt, to, 0);
return r;
if (fdt < 0)
return -errno;
- r = copy_bytes(fdf, fdt);
+ r = copy_bytes(fdf, fdt, (off_t) -1);
if (r < 0) {
unlink(to);
return r;
int copy_file(const char *from, const char *to, int flags, mode_t mode);
int copy_tree(const char *from, const char *to, bool merge);
-int copy_bytes(int fdf, int fdt);
+int copy_bytes(int fdf, int fdt, off_t max_bytes);
if (dst < 0)
return dst;
- r = copy_bytes(src, dst);
+ r = copy_bytes(src, dst, (off_t) -1);
if (r < 0)
goto fail;