]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/condition.c
unit: introduce ConditionDirectoryNotEmpty=
[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 if ((r = read_one_line_file("/proc/cmdline", &line)) < 0) {
68 log_warning("Failed to read /proc/cmdline, ignoring: %s", strerror(-r));
69 return false;
70 }
71
72 equal = !!strchr(parameter, '=');
73 pl = strlen(parameter);
74
75 FOREACH_WORD_QUOTED(w, l, line, state) {
76
77 free(word);
78 if (!(word = strndup(w, l)))
79 break;
80
81 if (equal) {
82 if (streq(word, parameter)) {
83 found = true;
84 break;
85 }
86 } else {
87 if (startswith(word, parameter) && (word[pl] == '=' || word[pl] == 0)) {
88 found = true;
89 break;
90 }
91 }
92
93 }
94
95 free(word);
96 free(line);
97
98 return found;
99 }
100
101 bool condition_test(Condition *c) {
102 assert(c);
103
104 switch(c->type) {
105
106 case CONDITION_PATH_EXISTS:
107 return (access(c->parameter, F_OK) >= 0) == !c->negate;
108
109 case CONDITION_DIRECTORY_NOT_EMPTY: {
110 int k;
111
112 k = dir_is_empty(c->parameter);
113 return !(k == -ENOENT || k > 0) == !c->negate;
114 }
115
116 case CONDITION_KERNEL_COMMAND_LINE:
117 return !!test_kernel_command_line(c->parameter) == !c->negate;
118
119 case CONDITION_NULL:
120 return !c->negate;
121
122 default:
123 assert_not_reached("Invalid condition type.");
124 }
125 }
126
127 bool condition_test_list(Condition *first) {
128 Condition *c;
129
130 /* If the condition list is empty, then it is true */
131 if (!first)
132 return true;
133
134 /* Otherwise, if any of the conditions apply we return true */
135 LIST_FOREACH(conditions, c, first)
136 if (condition_test(c))
137 return true;
138
139 return false;
140 }
141
142 void condition_dump(Condition *c, FILE *f, const char *prefix) {
143 assert(c);
144 assert(f);
145
146 if (!prefix)
147 prefix = "";
148
149 fprintf(f,
150 "%s%s: %s%s\n",
151 prefix,
152 condition_type_to_string(c->type),
153 c->negate ? "!" : "",
154 c->parameter);
155 }
156
157 void condition_dump_list(Condition *first, FILE *f, const char *prefix) {
158 Condition *c;
159
160 LIST_FOREACH(conditions, c, first)
161 condition_dump(c, f, prefix);
162 }
163
164 static const char* const condition_type_table[_CONDITION_TYPE_MAX] = {
165 [CONDITION_KERNEL_COMMAND_LINE] = "ConditionKernelCommandLine",
166 [CONDITION_PATH_EXISTS] = "ConditionPathExists",
167 [CONDITION_NULL] = "ConditionNull"
168 };
169
170 DEFINE_STRING_TABLE_LOOKUP(condition_type, ConditionType);