]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/quotacheck/quotacheck.c
build-sys: use #if Y instead of #ifdef Y everywhere
[thirdparty/systemd.git] / src / quotacheck / quotacheck.c
1 /***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
15
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18 ***/
19
20 #include <errno.h>
21 #include <stdbool.h>
22 #include <stdio.h>
23 #include <sys/prctl.h>
24 #include <unistd.h>
25
26 #include "proc-cmdline.h"
27 #include "process-util.h"
28 #include "signal-util.h"
29 #include "string-util.h"
30 #include "util.h"
31
32 static bool arg_skip = false;
33 static bool arg_force = false;
34
35 static int parse_proc_cmdline_item(const char *key, const char *value, void *data) {
36
37 if (streq(key, "quotacheck.mode")) {
38
39 if (proc_cmdline_value_missing(key, value))
40 return 0;
41
42 if (streq(value, "auto"))
43 arg_force = arg_skip = false;
44 else if (streq(value, "force"))
45 arg_force = true;
46 else if (streq(value, "skip"))
47 arg_skip = true;
48 else
49 log_warning("Invalid quotacheck.mode= parameter '%s'. Ignoring.", value);
50 }
51
52 #if HAVE_SYSV_COMPAT
53 else if (streq(key, "forcequotacheck") && !value) {
54 log_warning("Please use 'quotacheck.mode=force' rather than 'forcequotacheck' on the kernel command line.");
55 arg_force = true;
56 }
57 #endif
58
59 return 0;
60 }
61
62 static void test_files(void) {
63
64 #if HAVE_SYSV_COMPAT
65 if (access("/forcequotacheck", F_OK) >= 0) {
66 log_error("Please pass 'quotacheck.mode=force' on the kernel command line rather than creating /forcequotacheck on the root file system.");
67 arg_force = true;
68 }
69 #endif
70 }
71
72 int main(int argc, char *argv[]) {
73
74 static const char * const cmdline[] = {
75 QUOTACHECK,
76 "-anug",
77 NULL
78 };
79
80 pid_t pid;
81 int r;
82
83 if (argc > 1) {
84 log_error("This program takes no arguments.");
85 return EXIT_FAILURE;
86 }
87
88 log_set_target(LOG_TARGET_AUTO);
89 log_parse_environment();
90 log_open();
91
92 umask(0022);
93
94 r = proc_cmdline_parse(parse_proc_cmdline_item, NULL, 0);
95 if (r < 0)
96 log_warning_errno(r, "Failed to parse kernel command line, ignoring: %m");
97
98 test_files();
99
100 if (!arg_force) {
101 if (arg_skip)
102 return EXIT_SUCCESS;
103
104 if (access("/run/systemd/quotacheck", F_OK) < 0)
105 return EXIT_SUCCESS;
106 }
107
108 pid = fork();
109 if (pid < 0) {
110 r = log_error_errno(errno, "fork(): %m");
111 goto finish;
112 }
113 if (pid == 0) {
114
115 /* Child */
116
117 (void) reset_all_signal_handlers();
118 (void) reset_signal_mask();
119 assert_se(prctl(PR_SET_PDEATHSIG, SIGTERM) == 0);
120
121 execv(cmdline[0], (char**) cmdline);
122 _exit(1); /* Operational error */
123 }
124
125 r = wait_for_terminate_and_warn("quotacheck", pid, true);
126
127 finish:
128 return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
129 }