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