]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/quotacheck/quotacheck.c
basic: rename util.h to logarithm.h
[thirdparty/systemd.git] / src / quotacheck / quotacheck.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <errno.h>
4 #include <stdbool.h>
5 #include <stdio.h>
6 #include <sys/prctl.h>
7 #include <sys/stat.h>
8 #include <sys/types.h>
9 #include <unistd.h>
10
11 #include "main-func.h"
12 #include "proc-cmdline.h"
13 #include "process-util.h"
14 #include "signal-util.h"
15 #include "string-util.h"
16
17 static bool arg_skip = false;
18 static bool arg_force = false;
19
20 static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
21
22 if (streq(key, "quotacheck.mode")) {
23
24 if (proc_cmdline_value_missing(key, value))
25 return 0;
26
27 if (streq(value, "auto"))
28 arg_force = arg_skip = false;
29 else if (streq(value, "force"))
30 arg_force = true;
31 else if (streq(value, "skip"))
32 arg_skip = true;
33 else
34 log_warning("Invalid quotacheck.mode= parameter '%s'. Ignoring.", value);
35 }
36
37 #if HAVE_SYSV_COMPAT
38 else if (streq(key, "forcequotacheck") && !value) {
39 log_warning("Please use 'quotacheck.mode=force' rather than 'forcequotacheck' on the kernel command line.");
40 arg_force = true;
41 }
42 #endif
43
44 return 0;
45 }
46
47 static void test_files(void) {
48
49 #if HAVE_SYSV_COMPAT
50 if (access("/forcequotacheck", F_OK) >= 0) {
51 log_error("Please pass 'quotacheck.mode=force' on the kernel command line rather than creating /forcequotacheck on the root file system.");
52 arg_force = true;
53 }
54 #endif
55 }
56
57 static int run(int argc, char *argv[]) {
58 int r;
59
60 log_setup();
61
62 if (argc > 1)
63 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
64 "This program takes no arguments.");
65
66 umask(0022);
67
68 r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, 0);
69 if (r < 0)
70 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
71
72 test_files();
73
74 if (!arg_force) {
75 if (arg_skip)
76 return 0;
77
78 if (access("/run/systemd/quotacheck", F_OK) < 0)
79 return 0;
80 }
81
82 r = safe_fork("(quotacheck)", FORK_RESET_SIGNALS|FORK_DEATHSIG|FORK_RLIMIT_NOFILE_SAFE|FORK_WAIT|FORK_LOG, NULL);
83 if (r < 0)
84 return r;
85 if (r == 0) {
86 static const char * const cmdline[] = {
87 QUOTACHECK,
88 "-anug",
89 NULL
90 };
91
92 /* Child */
93
94 execv(cmdline[0], (char**) cmdline);
95 _exit(EXIT_FAILURE); /* Operational error */
96 }
97
98 return 0;
99 }
100
101 DEFINE_MAIN_FUNCTION(run);