| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "bitfield.h" |
| 4 |
#include "errno-util.h" |
| 5 |
#include "fd-util.h" |
| 6 |
#include "format-util.h" |
| 7 |
#include "log.h" |
| 8 |
#include "main-func.h" |
| 9 |
#include "quota-util.h" |
| 10 |
#include "strv.h" |
| 11 |
#include "user-util.h" |
| 12 |
#include "userdb.h" |
| 13 |
|
| 14 |
static int show_quota(uid_t uid, const char *path) { |
| 15 |
int r; |
| 16 |
|
| 17 |
_cleanup_close_ int fd = open(path, O_DIRECTORY|O_CLOEXEC); |
| 18 |
if (fd < 0) |
| 19 |
return log_error_errno(errno, "Failed to open '%s': %m", path); |
| 20 |
|
| 21 |
struct dqblk req; |
| 22 |
r = quotactl_fd_with_fallback(fd, QCMD_FIXED(Q_GETQUOTA, USRQUOTA), uid, &req); |
| 23 |
if (r == -ESRCH) { |
| 24 |
log_info_errno(r, "No quota set on %s for UID "UID_FMT": %m", path, uid); |
| 25 |
return 0; |
| 26 |
} |
| 27 |
if (ERRNO_IS_NEG_NOT_SUPPORTED(r)) |
| 28 |
return log_warning_errno(r, "No UID quota support on %s: %m", path); |
| 29 |
if (ERRNO_IS_NEG_PRIVILEGE(r)) |
| 30 |
return log_error_errno(r, "Lacking privileges to query UID quota on %s: %m", path); |
| 31 |
if (r < 0) |
| 32 |
return log_error_errno(r, "Failed to query disk quota on %s for UID "UID_FMT": %m", path, uid); |
| 33 |
|
| 34 |
printf("** Quota on %s for UID "UID_FMT" **\n" |
| 35 |
"block hardlimit: %"PRIu64"\n" |
| 36 |
"block softlimit: %"PRIu64"\n" |
| 37 |
"blocks current: %"PRIu64"\n" |
| 38 |
"inodes hardlimit: %"PRIu64"\n" |
| 39 |
"inodes softlimit: %"PRIu64"\n" |
| 40 |
"inodes current: %"PRIu64"\n" |
| 41 |
"excess block time: %"PRIu64"\n" |
| 42 |
"excess inode time: %"PRIu64"\n" |
| 43 |
"validity mask: 0x%"PRIx32, |
| 44 |
path, uid, |
| 45 |
req.dqb_bhardlimit, |
| 46 |
req.dqb_bsoftlimit, |
| 47 |
req.dqb_curspace, |
| 48 |
req.dqb_ihardlimit, |
| 49 |
req.dqb_isoftlimit, |
| 50 |
req.dqb_curinodes, |
| 51 |
req.dqb_btime, |
| 52 |
req.dqb_itime, |
| 53 |
req.dqb_valid); |
| 54 |
|
| 55 |
const char* fields[] = {"BLIMITS", "SPACE", "INODES", "BTIME", "ITIME"}; |
| 56 |
bool first = true; |
| 57 |
for (size_t i = 0; i < ELEMENTSOF(fields); i++) |
| 58 |
if (BIT_SET(req.dqb_valid, i)) { |
| 59 |
printf("%c%s", first ? ' ' : '|', fields[i]); |
| 60 |
first = false; |
| 61 |
} |
| 62 |
printf("%s\n", first ? "(none)" : ""); |
| 63 |
|
| 64 |
return 0; |
| 65 |
} |
| 66 |
|
| 67 |
static int run(int argc, char **argv) { |
| 68 |
int r; |
| 69 |
|
| 70 |
if (argc < 2) |
| 71 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 72 |
"This program requires at least one argument\n" |
| 73 |
"syntax: test-display-quota USER PATH…"); |
| 74 |
|
| 75 |
const char *user = argv[1]; |
| 76 |
_cleanup_(user_record_unrefp) UserRecord *ur = NULL; |
| 77 |
r = userdb_by_name(user, /* match= */ NULL, USERDB_PARSE_NUMERIC|USERDB_SUPPRESS_SHADOW, &ur); |
| 78 |
if (r < 0) |
| 79 |
return log_error_errno(r, "Failed to resolve user '%s': %m", user); |
| 80 |
|
| 81 |
if (!uid_is_valid(ur->uid)) |
| 82 |
return log_error_errno(SYNTHETIC_ERRNO(ENOMSG), "User '%s' lacks UID.", ur->user_name); |
| 83 |
|
| 84 |
r = 0; |
| 85 |
STRV_FOREACH(path, strv_skip(argv, 2)) |
| 86 |
RET_GATHER(r, show_quota(ur->uid, *path)); |
| 87 |
|
| 88 |
return r; |
| 89 |
} |
| 90 |
|
| 91 |
DEFINE_MAIN_FUNCTION(run); |