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