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