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