chiark / gitweb /
dhcp: Add function to free DHCP client data
[elogind.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 <stdio.h>
23 #include <stdbool.h>
24 #include <string.h>
25 #include <errno.h>
26 #include <unistd.h>
27
28 #include "util.h"
29 #include "fileio.h"
30
31 static bool arg_skip = false;
32 static bool arg_force = false;
33
34 static int parse_proc_cmdline(void) {
35         _cleanup_free_ char *line = NULL;
36         char *w, *state;
37         size_t l;
38         int r;
39
40         r = proc_cmdline(&line);
41         if (r < 0)
42                 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
43         if (r <= 0)
44                 return 0;
45
46         FOREACH_WORD_QUOTED(w, l, line, state) {
47
48                 if (strneq(w, "quotacheck.mode=auto", l))
49                         arg_force = arg_skip = false;
50                 else if (strneq(w, "quotacheck.mode=force", l))
51                         arg_force = true;
52                 else if (strneq(w, "quotacheck.mode=skip", l))
53                         arg_skip = true;
54                 else if (startswith(w, "quotacheck"))
55                         log_warning("Invalid quotacheck parameter. Ignoring.");
56 #ifdef HAVE_SYSV_COMPAT
57                 else if (strneq(w, "forcequotacheck", l)) {
58                         log_error("Please use 'quotacheck.mode=force' rather than 'forcequotacheck' on the kernel command line.");
59                         arg_force = true;
60                 }
61 #endif
62         }
63         return 0;
64 }
65
66 static void test_files(void) {
67 #ifdef HAVE_SYSV_COMPAT
68         if (access("/forcequotacheck", F_OK) >= 0) {
69                 log_error("Please pass 'quotacheck.mode=force' on the kernel command line rather than creating /forcequotacheck on the root file system.");
70                 arg_force = true;
71         }
72 #endif
73 }
74
75 int main(int argc, char *argv[]) {
76
77         static const char * const cmdline[] = {
78                 QUOTACHECK,
79                 "-anug",
80                 NULL
81         };
82
83         pid_t pid;
84
85         if (argc > 1) {
86                 log_error("This program takes no arguments.");
87                 return EXIT_FAILURE;
88         }
89
90         log_set_target(LOG_TARGET_AUTO);
91         log_parse_environment();
92         log_open();
93
94         umask(0022);
95
96         parse_proc_cmdline();
97         test_files();
98
99         if (!arg_force) {
100                 if (arg_skip)
101                         return EXIT_SUCCESS;
102
103                 if (access("/run/systemd/quotacheck", F_OK) < 0)
104                         return EXIT_SUCCESS;
105         }
106
107         pid = fork();
108         if (pid < 0) {
109                 log_error("fork(): %m");
110                 return EXIT_FAILURE;
111         } else if (pid == 0) {
112                 /* Child */
113                 execv(cmdline[0], (char**) cmdline);
114                 _exit(1); /* Operational error */
115         }
116
117         return wait_for_terminate_and_warn("quotacheck", pid) >= 0 ? EXIT_SUCCESS : EXIT_FAILURE;
118 }