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