chiark / gitweb /
service: use uppercase SYSV prefix to make it easier to recognize
[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 ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
71                 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
72                 return false;
73         }
74
75         equal = !!strchr(parameter, '=');
76         pl = strlen(parameter);
77
78         FOREACH_WORD_QUOTED(w, l, line, state) {
79
80                 free(word);
81                 if (!(word = strndup(w, l)))
82                         break;
83
84                 if (equal) {
85                         if (streq(word, parameter)) {
86                                 found = true;
87                                 break;
88                         }
89                 } else {
90                         if (startswith(word, parameter) && (word[pl] == '=' || word[pl] == 0)) {
91                                 found = true;
92                                 break;
93                         }
94                 }
95
96         }
97
98         free(word);
99         free(line);
100
101         return found;
102 }
103
104 static bool test_virtualization(const char *parameter) {
105         int r, b;
106         const char *id;
107
108         assert(parameter);
109
110         if ((r = detect_virtualization(&id)) < 0) {
111                 log_warning("Failed to detect virtualization, ignoring: %s", strerror(-r));
112                 return false;
113         }
114
115         b = parse_boolean(parameter);
116
117         if (r > 0 && b > 0)
118                 return true;
119
120         if (r == 0 && b == 0)
121                 return true;
122
123         return streq(parameter, id);
124 }
125
126 bool condition_test(Condition *c) {
127         assert(c);
128
129         switch(c->type) {
130
131         case CONDITION_PATH_EXISTS:
132                 return (access(c->parameter, F_OK) >= 0) == !c->negate;
133
134         case CONDITION_DIRECTORY_NOT_EMPTY: {
135                 int k;
136
137                 k = dir_is_empty(c->parameter);
138                 return !(k == -ENOENT || k > 0) == !c->negate;
139         }
140
141         case CONDITION_KERNEL_COMMAND_LINE:
142                 return test_kernel_command_line(c->parameter) == !c->negate;
143
144         case CONDITION_VIRTUALIZATION:
145                 return test_virtualization(c->parameter) == !c->negate;
146
147         case CONDITION_NULL:
148                 return !c->negate;
149
150         default:
151                 assert_not_reached("Invalid condition type.");
152         }
153 }
154
155 bool condition_test_list(Condition *first) {
156         Condition *c;
157         int triggered = -1;
158
159         /* If the condition list is empty, then it is true */
160         if (!first)
161                 return true;
162
163         /* Otherwise, if all of the non-trigger conditions apply and
164          * if any of the trigger conditions apply (unless there are
165          * none) we return true */
166         LIST_FOREACH(conditions, c, first) {
167                 bool b;
168
169                 b = condition_test(c);
170
171                 if (!c->trigger && !b)
172                         return false;
173
174                 if (c->trigger && triggered <= 0)
175                         triggered = b;
176         }
177
178         return triggered != 0;
179 }
180
181 void condition_dump(Condition *c, FILE *f, const char *prefix) {
182         assert(c);
183         assert(f);
184
185         if (!prefix)
186                 prefix = "";
187
188         fprintf(f,
189                 "%s%s: %s%s%s\n",
190                 prefix,
191                 condition_type_to_string(c->type),
192                 c->trigger ? "|" : "",
193                 c->negate ? "!" : "",
194                 c->parameter);
195 }
196
197 void condition_dump_list(Condition *first, FILE *f, const char *prefix) {
198         Condition *c;
199
200         LIST_FOREACH(conditions, c, first)
201                 condition_dump(c, f, prefix);
202 }
203
204 static const char* const condition_type_table[_CONDITION_TYPE_MAX] = {
205         [CONDITION_KERNEL_COMMAND_LINE] = "ConditionKernelCommandLine",
206         [CONDITION_PATH_EXISTS] = "ConditionPathExists",
207         [CONDITION_NULL] = "ConditionNull"
208 };
209
210 DEFINE_STRING_TABLE_LOOKUP(condition_type, ConditionType);