chiark / gitweb /
shutdown: accept minutes argument without '+'
[elogind.git] / src / condition.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 <stdlib.h>
23 #include <errno.h>
24 #include <string.h>
25 #include <unistd.h>
26
27 #ifdef HAVE_SELINUX
28 #include <selinux/selinux.h>
29 #endif
30
31 #include "util.h"
32 #include "condition.h"
33
34 Condition* condition_new(ConditionType type, const char *parameter, bool trigger, bool negate) {
35         Condition *c;
36
37         if (!(c = new0(Condition, 1)))
38                 return NULL;
39
40         c->type = type;
41         c->trigger = trigger;
42         c->negate = negate;
43
44         if (parameter)
45                 if (!(c->parameter = strdup(parameter))) {
46                         free(c);
47                         return NULL;
48                 }
49
50         return c;
51 }
52
53 void condition_free(Condition *c) {
54         assert(c);
55
56         free(c->parameter);
57         free(c);
58 }
59
60 void condition_free_list(Condition *first) {
61         Condition *c, *n;
62
63         LIST_FOREACH_SAFE(conditions, c, n, first)
64                 condition_free(c);
65 }
66
67 static bool test_kernel_command_line(const char *parameter) {
68         char *line, *w, *state, *word = NULL;
69         bool equal;
70         int r;
71         size_t l, pl;
72         bool found = false;
73
74         assert(parameter);
75
76         if (detect_virtualization(NULL) > 0)
77                 return false;
78
79         if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
80                 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
81                 return false;
82         }
83
84         equal = !!strchr(parameter, '=');
85         pl = strlen(parameter);
86
87         FOREACH_WORD_QUOTED(w, l, line, state) {
88
89                 free(word);
90                 if (!(word = strndup(w, l)))
91                         break;
92
93                 if (equal) {
94                         if (streq(word, parameter)) {
95                                 found = true;
96                                 break;
97                         }
98                 } else {
99                         if (startswith(word, parameter) && (word[pl] == '=' || word[pl] == 0)) {
100                                 found = true;
101                                 break;
102                         }
103                 }
104
105         }
106
107         free(word);
108         free(line);
109
110         return found;
111 }
112
113 static bool test_virtualization(const char *parameter) {
114         int r, b;
115         const char *id;
116
117         assert(parameter);
118
119         if ((r = detect_virtualization(&id)) < 0) {
120                 log_warning("Failed to detect virtualization, ignoring: %s", strerror(-r));
121                 return false;
122         }
123
124         b = parse_boolean(parameter);
125
126         if (r > 0 && b > 0)
127                 return true;
128
129         if (r == 0 && b == 0)
130                 return true;
131
132         return streq(parameter, id);
133 }
134
135 static bool test_security(const char *parameter) {
136 #ifdef HAVE_SELINUX
137         if (streq(parameter, "selinux"))
138                 return is_selinux_enabled() > 0;
139 #endif
140         return false;
141 }
142
143 bool condition_test(Condition *c) {
144         assert(c);
145
146         switch(c->type) {
147
148         case CONDITION_PATH_EXISTS:
149                 return (access(c->parameter, F_OK) >= 0) == !c->negate;
150
151         case CONDITION_PATH_IS_DIRECTORY: {
152                 struct stat st;
153
154                 if (lstat(c->parameter, &st) < 0)
155                         return !c->negate;
156                 return S_ISDIR(st.st_mode) == !c->negate;
157         }
158
159         case CONDITION_DIRECTORY_NOT_EMPTY: {
160                 int k;
161
162                 k = dir_is_empty(c->parameter);
163                 return !(k == -ENOENT || k > 0) == !c->negate;
164         }
165
166         case CONDITION_KERNEL_COMMAND_LINE:
167                 return test_kernel_command_line(c->parameter) == !c->negate;
168
169         case CONDITION_VIRTUALIZATION:
170                 return test_virtualization(c->parameter) == !c->negate;
171
172         case CONDITION_SECURITY:
173                 return test_security(c->parameter) == !c->negate;
174
175         case CONDITION_NULL:
176                 return !c->negate;
177
178         default:
179                 assert_not_reached("Invalid condition type.");
180         }
181 }
182
183 bool condition_test_list(Condition *first) {
184         Condition *c;
185         int triggered = -1;
186
187         /* If the condition list is empty, then it is true */
188         if (!first)
189                 return true;
190
191         /* Otherwise, if all of the non-trigger conditions apply and
192          * if any of the trigger conditions apply (unless there are
193          * none) we return true */
194         LIST_FOREACH(conditions, c, first) {
195                 bool b;
196
197                 b = condition_test(c);
198
199                 if (!c->trigger && !b)
200                         return false;
201
202                 if (c->trigger && triggered <= 0)
203                         triggered = b;
204         }
205
206         return triggered != 0;
207 }
208
209 void condition_dump(Condition *c, FILE *f, const char *prefix) {
210         assert(c);
211         assert(f);
212
213         if (!prefix)
214                 prefix = "";
215
216         fprintf(f,
217                 "%s\t%s: %s%s%s\n",
218                 prefix,
219                 condition_type_to_string(c->type),
220                 c->trigger ? "|" : "",
221                 c->negate ? "!" : "",
222                 c->parameter);
223 }
224
225 void condition_dump_list(Condition *first, FILE *f, const char *prefix) {
226         Condition *c;
227
228         LIST_FOREACH(conditions, c, first)
229                 condition_dump(c, f, prefix);
230 }
231
232 static const char* const condition_type_table[_CONDITION_TYPE_MAX] = {
233         [CONDITION_PATH_EXISTS] = "ConditionPathExists",
234         [CONDITION_PATH_IS_DIRECTORY] = "ConditionPathIsDirectory",
235         [CONDITION_DIRECTORY_NOT_EMPTY] = "ConditionDirectoryNotEmpty",
236         [CONDITION_KERNEL_COMMAND_LINE] = "ConditionKernelCommandLine",
237         [CONDITION_VIRTUALIZATION] = "ConditionVirtualization",
238         [CONDITION_SECURITY] = "ConditionSecurity",
239         [CONDITION_NULL] = "ConditionNull"
240 };
241
242 DEFINE_STRING_TABLE_LOOKUP(condition_type, ConditionType);