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