]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/quotacheck.c
util: return exit status in wait_for_terminate_and_warn()
[thirdparty/systemd.git] / src / 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 General Public License as published by
10 the Free Software Foundation; either version 2 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 General Public License for more details.
17
18 You should have received a copy of the GNU 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
30 static bool arg_skip = false;
31 static bool arg_force = false;
32
33 static int parse_proc_cmdline(void) {
34 char *line, *w, *state;
35 int r;
36 size_t l;
37
38 if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
39 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
40 return 0;
41 }
42
43 FOREACH_WORD_QUOTED(w, l, line, state) {
44
45 if (strneq(w, "quotacheck.mode=auto", l))
46 arg_force = arg_skip = false;
47 else if (strneq(w, "quotacheck.mode=force", l))
48 arg_force = true;
49 else if (strneq(w, "quotacheck.mode=skip", l))
50 arg_skip = true;
51 else if (startswith(w, "quotacheck.mode"))
52 log_warning("Invalid quotacheck.mode= parameter. Ignoring.");
53 #if defined(TARGET_FEDORA) || defined(TARGET_MANDRIVA)
54 else if (strneq(w, "forcequotacheck", l))
55 arg_force = true;
56 #endif
57 }
58
59 free(line);
60 return 0;
61 }
62
63 static void test_files(void) {
64 #if defined(TARGET_FEDORA) || defined(TARGET_MANDRIVA)
65 /* This exists only on Fedora or Mandriva */
66 if (access("/forcequotacheck", F_OK) >= 0)
67 arg_force = true;
68 #endif
69 }
70
71 int main(int argc, char *argv[]) {
72 static const char * const cmdline[] = {
73 "/sbin/quotacheck",
74 "-anug",
75 NULL
76 };
77
78 int r = EXIT_FAILURE;
79 pid_t pid;
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_SYSLOG_OR_KMSG);
87 log_parse_environment();
88 log_open();
89
90 parse_proc_cmdline();
91 test_files();
92
93 if (!arg_force) {
94 if (arg_skip)
95 return 0;
96
97 if (access("/dev/.run/systemd/quotacheck", F_OK) < 0)
98 return 0;
99 }
100
101 if ((pid = fork()) < 0) {
102 log_error("fork(): %m");
103 goto finish;
104 } else if (pid == 0) {
105 /* Child */
106 execv(cmdline[0], (char**) cmdline);
107 _exit(1); /* Operational error */
108 }
109
110 r = wait_for_terminate_and_warn("quotacheck", pid) == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
111
112 finish:
113 return r;
114 }