chiark / gitweb /
architecture: Add tilegx
[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_word(const char *w) {
35         if (streq(w, "quotacheck.mode=auto"))
36                 arg_force = arg_skip = false;
37         else if (streq(w, "quotacheck.mode=force"))
38                 arg_force = true;
39         else if (streq(w, "quotacheck.mode=skip"))
40                 arg_skip = true;
41         else if (startswith(w, "quotacheck"))
42                 log_warning("Invalid quotacheck parameter. Ignoring.");
43 #ifdef HAVE_SYSV_COMPAT
44         else if (streq(w, "forcequotacheck")) {
45                 log_error("Please use 'quotacheck.mode=force' rather than 'forcequotacheck' on the kernel command line.");
46                 arg_force = true;
47         }
48 #endif
49
50         return 0;
51 }
52
53 static void test_files(void) {
54 #ifdef HAVE_SYSV_COMPAT
55         if (access("/forcequotacheck", F_OK) >= 0) {
56                 log_error("Please pass 'quotacheck.mode=force' on the kernel command line rather than creating /forcequotacheck on the root file system.");
57                 arg_force = true;
58         }
59 #endif
60 }
61
62 int main(int argc, char *argv[]) {
63
64         static const char * const cmdline[] = {
65                 QUOTACHECK,
66                 "-anug",
67                 NULL
68         };
69
70         pid_t pid;
71
72         if (argc > 1) {
73                 log_error("This program takes no arguments.");
74                 return EXIT_FAILURE;
75         }
76
77         log_set_target(LOG_TARGET_AUTO);
78         log_parse_environment();
79         log_open();
80
81         umask(0022);
82
83         parse_proc_cmdline(parse_proc_cmdline_word);
84         test_files();
85
86         if (!arg_force) {
87                 if (arg_skip)
88                         return EXIT_SUCCESS;
89
90                 if (access("/run/systemd/quotacheck", F_OK) < 0)
91                         return EXIT_SUCCESS;
92         }
93
94         pid = fork();
95         if (pid < 0) {
96                 log_error("fork(): %m");
97                 return EXIT_FAILURE;
98         } else if (pid == 0) {
99                 /* Child */
100                 execv(cmdline[0], (char**) cmdline);
101                 _exit(1); /* Operational error */
102         }
103
104         return wait_for_terminate_and_warn("quotacheck", pid) >= 0 ? EXIT_SUCCESS : EXIT_FAILURE;
105 }