Lennart Poettering | 3efd419 | 2009-11-19 23:13:20 +0100 | [diff] [blame] | 1 | /*-*- Mode: C; c-basic-offset: 8 -*-*/ |
| 2 | |
Lennart Poettering | a7334b0 | 2010-02-03 13:03:47 +0100 | [diff] [blame] | 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 | |
Lennart Poettering | 87f0e41 | 2010-01-26 21:39:06 +0100 | [diff] [blame] | 22 | #include <linux/oom.h> |
Lennart Poettering | 3efd419 | 2009-11-19 23:13:20 +0100 | [diff] [blame] | 23 | #include <assert.h> |
| 24 | #include <errno.h> |
| 25 | #include <string.h> |
Lennart Poettering | 87f0e41 | 2010-01-26 21:39:06 +0100 | [diff] [blame] | 26 | #include <unistd.h> |
| 27 | #include <fcntl.h> |
Lennart Poettering | 94f0434 | 2010-01-30 01:55:42 +0100 | [diff] [blame] | 28 | #include <sched.h> |
| 29 | #include <sys/prctl.h> |
Lennart Poettering | 15ae422 | 2010-04-21 22:15:06 +0200 | [diff] [blame] | 30 | #include <sys/mount.h> |
Kay Sievers | 25e870b | 2010-04-24 05:05:01 +0200 | [diff] [blame] | 31 | #include <linux/fs.h> |
Lennart Poettering | 3efd419 | 2009-11-19 23:13:20 +0100 | [diff] [blame] | 32 | |
Lennart Poettering | 87f0e41 | 2010-01-26 21:39:06 +0100 | [diff] [blame] | 33 | #include "unit.h" |
Lennart Poettering | 3efd419 | 2009-11-19 23:13:20 +0100 | [diff] [blame] | 34 | #include "strv.h" |
| 35 | #include "conf-parser.h" |
| 36 | #include "load-fragment.h" |
Lennart Poettering | 16354ef | 2010-01-20 19:19:53 +0100 | [diff] [blame] | 37 | #include "log.h" |
Lennart Poettering | 9eba9da | 2010-01-29 20:46:22 +0100 | [diff] [blame] | 38 | #include "ioprio.h" |
Lennart Poettering | 94f0434 | 2010-01-30 01:55:42 +0100 | [diff] [blame] | 39 | #include "securebits.h" |
| 40 | #include "missing.h" |
Lennart Poettering | 9e2f7c1 | 2010-04-15 03:11:11 +0200 | [diff] [blame] | 41 | #include "unit-name.h" |
Lennart Poettering | 3efd419 | 2009-11-19 23:13:20 +0100 | [diff] [blame] | 42 | |
Lennart Poettering | ddb26e1 | 2010-06-18 06:06:24 +0200 | [diff] [blame] | 43 | #define COMMENTS "#;\n" |
| 44 | #define LINE_MAX 4096 |
| 45 | |
Lennart Poettering | 0d87eb4 | 2010-04-13 02:22:41 +0200 | [diff] [blame] | 46 | #define DEFINE_CONFIG_PARSE_ENUM(function,name,type,msg) \ |
| 47 | static int function( \ |
| 48 | const char *filename, \ |
| 49 | unsigned line, \ |
| 50 | const char *section, \ |
| 51 | const char *lvalue, \ |
| 52 | const char *rvalue, \ |
| 53 | void *data, \ |
| 54 | void *userdata) { \ |
| 55 | \ |
| 56 | type *i = data, x; \ |
| 57 | \ |
| 58 | assert(filename); \ |
| 59 | assert(lvalue); \ |
| 60 | assert(rvalue); \ |
| 61 | assert(data); \ |
| 62 | \ |
| 63 | if ((x = name##_from_string(rvalue)) < 0) { \ |
| 64 | log_error("[%s:%u] " msg ": %s", filename, line, rvalue); \ |
| 65 | return -EBADMSG; \ |
| 66 | } \ |
| 67 | \ |
| 68 | *i = x; \ |
| 69 | \ |
| 70 | return 0; \ |
| 71 | } |
| 72 | |
Lennart Poettering | 42f4e3c | 2010-01-19 02:56:37 +0100 | [diff] [blame] | 73 | static int config_parse_deps( |
Lennart Poettering | 3efd419 | 2009-11-19 23:13:20 +0100 | [diff] [blame] | 74 | const char *filename, |
| 75 | unsigned line, |
| 76 | const char *section, |
| 77 | const char *lvalue, |
| 78 | const char *rvalue, |
| 79 | void *data, |
| 80 | void *userdata) { |
| 81 | |
Lennart Poettering | 87f0e41 | 2010-01-26 21:39:06 +0100 | [diff] [blame] | 82 | UnitDependency d = PTR_TO_UINT(data); |
| 83 | Unit *u = userdata; |
Lennart Poettering | 3efd419 | 2009-11-19 23:13:20 +0100 | [diff] [blame] | 84 | char *w; |
| 85 | size_t l; |
| 86 | char *state; |
| 87 | |
| 88 | assert(filename); |
| 89 | assert(lvalue); |
| 90 | assert(rvalue); |
Lennart Poettering | 3efd419 | 2009-11-19 23:13:20 +0100 | [diff] [blame] | 91 | |
Lennart Poettering | f62c0e4 | 2010-02-14 01:07:36 +0100 | [diff] [blame] | 92 | FOREACH_WORD(w, l, rvalue, state) { |
Lennart Poettering | 9e2f7c1 | 2010-04-15 03:11:11 +0200 | [diff] [blame] | 93 | char *t, *k; |
Lennart Poettering | 3efd419 | 2009-11-19 23:13:20 +0100 | [diff] [blame] | 94 | int r; |
Lennart Poettering | 3efd419 | 2009-11-19 23:13:20 +0100 | [diff] [blame] | 95 | |
| 96 | if (!(t = strndup(w, l))) |
| 97 | return -ENOMEM; |
| 98 | |
Lennart Poettering | 9e2f7c1 | 2010-04-15 03:11:11 +0200 | [diff] [blame] | 99 | k = unit_name_printf(u, t); |
Lennart Poettering | 3efd419 | 2009-11-19 23:13:20 +0100 | [diff] [blame] | 100 | free(t); |
| 101 | |
Lennart Poettering | 9e2f7c1 | 2010-04-15 03:11:11 +0200 | [diff] [blame] | 102 | if (!k) |
| 103 | return -ENOMEM; |
| 104 | |
Lennart Poettering | 701cc38 | 2010-04-21 06:01:13 +0200 | [diff] [blame] | 105 | r = unit_add_dependency_by_name(u, d, k, NULL, true); |
Lennart Poettering | 9e2f7c1 | 2010-04-15 03:11:11 +0200 | [diff] [blame] | 106 | free(k); |
| 107 | |
Lennart Poettering | 3efd419 | 2009-11-19 23:13:20 +0100 | [diff] [blame] | 108 | if (r < 0) |
| 109 | return r; |
Lennart Poettering | 3efd419 | 2009-11-19 23:13:20 +0100 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | return 0; |
| 113 | } |
| 114 | |
Lennart Poettering | 42f4e3c | 2010-01-19 02:56:37 +0100 | [diff] [blame] | 115 | static int config_parse_names( |
Lennart Poettering | 87d1515 | 2010-01-18 23:50:13 +0100 | [diff] [blame] | 116 | const char *filename, |
| 117 | unsigned line, |
| 118 | const char *section, |
| 119 | const char *lvalue, |
| 120 | const char *rvalue, |
| 121 | void *data, |
| 122 | void *userdata) { |
| 123 | |
Lennart Poettering | 87f0e41 | 2010-01-26 21:39:06 +0100 | [diff] [blame] | 124 | Unit *u = userdata; |
Lennart Poettering | 87d1515 | 2010-01-18 23:50:13 +0100 | [diff] [blame] | 125 | char *w; |
| 126 | size_t l; |
| 127 | char *state; |
| 128 | |
| 129 | assert(filename); |
| 130 | assert(lvalue); |
| 131 | assert(rvalue); |
| 132 | assert(data); |
| 133 | |
Lennart Poettering | f62c0e4 | 2010-02-14 01:07:36 +0100 | [diff] [blame] | 134 | FOREACH_WORD(w, l, rvalue, state) { |
Lennart Poettering | 9e2f7c1 | 2010-04-15 03:11:11 +0200 | [diff] [blame] | 135 | char *t, *k; |
Lennart Poettering | 87d1515 | 2010-01-18 23:50:13 +0100 | [diff] [blame] | 136 | int r; |
Lennart Poettering | 87d1515 | 2010-01-18 23:50:13 +0100 | [diff] [blame] | 137 | |
| 138 | if (!(t = strndup(w, l))) |
| 139 | return -ENOMEM; |
| 140 | |
Lennart Poettering | 9e2f7c1 | 2010-04-15 03:11:11 +0200 | [diff] [blame] | 141 | k = unit_name_printf(u, t); |
Lennart Poettering | 87d1515 | 2010-01-18 23:50:13 +0100 | [diff] [blame] | 142 | free(t); |
Lennart Poettering | 23a177e | 2010-04-06 02:43:58 +0200 | [diff] [blame] | 143 | |
Lennart Poettering | 9e2f7c1 | 2010-04-15 03:11:11 +0200 | [diff] [blame] | 144 | if (!k) |
| 145 | return -ENOMEM; |
| 146 | |
| 147 | r = unit_merge_by_name(u, k); |
| 148 | free(k); |
| 149 | |
Lennart Poettering | 23a177e | 2010-04-06 02:43:58 +0200 | [diff] [blame] | 150 | if (r < 0) |
| 151 | return r; |
Lennart Poettering | 87d1515 | 2010-01-18 23:50:13 +0100 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
Lennart Poettering | f2d3769 | 2010-06-18 23:25:19 +0200 | [diff] [blame] | 157 | static int config_parse_string_printf( |
Lennart Poettering | 932921b | 2010-04-24 03:11:01 +0200 | [diff] [blame] | 158 | const char *filename, |
| 159 | unsigned line, |
| 160 | const char *section, |
| 161 | const char *lvalue, |
| 162 | const char *rvalue, |
| 163 | void *data, |
| 164 | void *userdata) { |
| 165 | |
| 166 | Unit *u = userdata; |
Lennart Poettering | f2d3769 | 2010-06-18 23:25:19 +0200 | [diff] [blame] | 167 | char **s = data; |
Lennart Poettering | 932921b | 2010-04-24 03:11:01 +0200 | [diff] [blame] | 168 | char *k; |
| 169 | |
| 170 | assert(filename); |
| 171 | assert(lvalue); |
| 172 | assert(rvalue); |
Lennart Poettering | f2d3769 | 2010-06-18 23:25:19 +0200 | [diff] [blame] | 173 | assert(s); |
| 174 | assert(u); |
Lennart Poettering | 932921b | 2010-04-24 03:11:01 +0200 | [diff] [blame] | 175 | |
| 176 | if (!(k = unit_full_printf(u, rvalue))) |
| 177 | return -ENOMEM; |
| 178 | |
Lennart Poettering | f2d3769 | 2010-06-18 23:25:19 +0200 | [diff] [blame] | 179 | free(*s); |
Lennart Poettering | 932921b | 2010-04-24 03:11:01 +0200 | [diff] [blame] | 180 | if (*k) |
Lennart Poettering | f2d3769 | 2010-06-18 23:25:19 +0200 | [diff] [blame] | 181 | *s = k; |
Lennart Poettering | 932921b | 2010-04-24 03:11:01 +0200 | [diff] [blame] | 182 | else { |
| 183 | free(k); |
Lennart Poettering | f2d3769 | 2010-06-18 23:25:19 +0200 | [diff] [blame] | 184 | *s = NULL; |
Lennart Poettering | 932921b | 2010-04-24 03:11:01 +0200 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | return 0; |
| 188 | } |
| 189 | |
Lennart Poettering | 42f4e3c | 2010-01-19 02:56:37 +0100 | [diff] [blame] | 190 | static int config_parse_listen( |
| 191 | const char *filename, |
| 192 | unsigned line, |
| 193 | const char *section, |
| 194 | const char *lvalue, |
| 195 | const char *rvalue, |
| 196 | void *data, |
| 197 | void *userdata) { |
| 198 | |
Lennart Poettering | 16354ef | 2010-01-20 19:19:53 +0100 | [diff] [blame] | 199 | int r; |
Lennart Poettering | 542563b | 2010-01-23 03:35:54 +0100 | [diff] [blame] | 200 | SocketPort *p; |
| 201 | Socket *s; |
Lennart Poettering | 16354ef | 2010-01-20 19:19:53 +0100 | [diff] [blame] | 202 | |
Lennart Poettering | 42f4e3c | 2010-01-19 02:56:37 +0100 | [diff] [blame] | 203 | assert(filename); |
| 204 | assert(lvalue); |
| 205 | assert(rvalue); |
| 206 | assert(data); |
| 207 | |
Lennart Poettering | 542563b | 2010-01-23 03:35:54 +0100 | [diff] [blame] | 208 | s = (Socket*) data; |
| 209 | |
| 210 | if (!(p = new0(SocketPort, 1))) |
| 211 | return -ENOMEM; |
| 212 | |
| 213 | if (streq(lvalue, "ListenFIFO")) { |
| 214 | p->type = SOCKET_FIFO; |
| 215 | |
| 216 | if (!(p->path = strdup(rvalue))) { |
| 217 | free(p); |
| 218 | return -ENOMEM; |
| 219 | } |
Lennart Poettering | 01f7847 | 2010-05-24 05:25:33 +0200 | [diff] [blame] | 220 | |
| 221 | path_kill_slashes(p->path); |
Lennart Poettering | 542563b | 2010-01-23 03:35:54 +0100 | [diff] [blame] | 222 | } else { |
| 223 | p->type = SOCKET_SOCKET; |
| 224 | |
| 225 | if ((r = socket_address_parse(&p->address, rvalue)) < 0) { |
| 226 | log_error("[%s:%u] Failed to parse address value: %s", filename, line, rvalue); |
| 227 | free(p); |
| 228 | return r; |
| 229 | } |
| 230 | |
| 231 | if (streq(lvalue, "ListenStream")) |
| 232 | p->address.type = SOCK_STREAM; |
| 233 | else if (streq(lvalue, "ListenDatagram")) |
| 234 | p->address.type = SOCK_DGRAM; |
| 235 | else { |
| 236 | assert(streq(lvalue, "ListenSequentialPacket")); |
| 237 | p->address.type = SOCK_SEQPACKET; |
| 238 | } |
| 239 | |
| 240 | if (socket_address_family(&p->address) != AF_LOCAL && p->address.type == SOCK_SEQPACKET) { |
| 241 | free(p); |
| 242 | return -EPROTONOSUPPORT; |
| 243 | } |
Lennart Poettering | 16354ef | 2010-01-20 19:19:53 +0100 | [diff] [blame] | 244 | } |
| 245 | |
Lennart Poettering | 542563b | 2010-01-23 03:35:54 +0100 | [diff] [blame] | 246 | p->fd = -1; |
Lennart Poettering | 034c6ed | 2010-01-26 04:18:44 +0100 | [diff] [blame] | 247 | LIST_PREPEND(SocketPort, port, s->ports, p); |
Lennart Poettering | 542563b | 2010-01-23 03:35:54 +0100 | [diff] [blame] | 248 | |
Lennart Poettering | 16354ef | 2010-01-20 19:19:53 +0100 | [diff] [blame] | 249 | return 0; |
Lennart Poettering | 42f4e3c | 2010-01-19 02:56:37 +0100 | [diff] [blame] | 250 | } |
| 251 | |
Lennart Poettering | 034c6ed | 2010-01-26 04:18:44 +0100 | [diff] [blame] | 252 | static int config_parse_socket_bind( |
Lennart Poettering | 42f4e3c | 2010-01-19 02:56:37 +0100 | [diff] [blame] | 253 | const char *filename, |
| 254 | unsigned line, |
| 255 | const char *section, |
| 256 | const char *lvalue, |
| 257 | const char *rvalue, |
| 258 | void *data, |
| 259 | void *userdata) { |
| 260 | |
Lennart Poettering | 542563b | 2010-01-23 03:35:54 +0100 | [diff] [blame] | 261 | Socket *s; |
Lennart Poettering | c0120d9 | 2010-05-21 23:41:25 +0200 | [diff] [blame] | 262 | SocketAddressBindIPv6Only b; |
Lennart Poettering | 42f4e3c | 2010-01-19 02:56:37 +0100 | [diff] [blame] | 263 | |
| 264 | assert(filename); |
| 265 | assert(lvalue); |
| 266 | assert(rvalue); |
| 267 | assert(data); |
| 268 | |
Lennart Poettering | 542563b | 2010-01-23 03:35:54 +0100 | [diff] [blame] | 269 | s = (Socket*) data; |
| 270 | |
Lennart Poettering | c0120d9 | 2010-05-21 23:41:25 +0200 | [diff] [blame] | 271 | if ((b = socket_address_bind_ipv6_only_from_string(rvalue)) < 0) { |
| 272 | int r; |
Lennart Poettering | 42f4e3c | 2010-01-19 02:56:37 +0100 | [diff] [blame] | 273 | |
Lennart Poettering | c0120d9 | 2010-05-21 23:41:25 +0200 | [diff] [blame] | 274 | if ((r = parse_boolean(rvalue)) < 0) { |
| 275 | log_error("[%s:%u] Failed to parse bind IPv6 only value: %s", filename, line, rvalue); |
| 276 | return -EBADMSG; |
| 277 | } |
| 278 | |
| 279 | s->bind_ipv6_only = r ? SOCKET_ADDRESS_IPV6_ONLY : SOCKET_ADDRESS_BOTH; |
| 280 | } else |
| 281 | s->bind_ipv6_only = b; |
Lennart Poettering | 542563b | 2010-01-23 03:35:54 +0100 | [diff] [blame] | 282 | |
Lennart Poettering | 42f4e3c | 2010-01-19 02:56:37 +0100 | [diff] [blame] | 283 | return 0; |
| 284 | } |
| 285 | |
Lennart Poettering | 034c6ed | 2010-01-26 04:18:44 +0100 | [diff] [blame] | 286 | static int config_parse_nice( |
| 287 | const char *filename, |
| 288 | unsigned line, |
| 289 | const char *section, |
| 290 | const char *lvalue, |
| 291 | const char *rvalue, |
| 292 | void *data, |
| 293 | void *userdata) { |
| 294 | |
Lennart Poettering | fb33a39 | 2010-01-28 02:53:56 +0100 | [diff] [blame] | 295 | ExecContext *c = data; |
| 296 | int priority, r; |
Lennart Poettering | 034c6ed | 2010-01-26 04:18:44 +0100 | [diff] [blame] | 297 | |
| 298 | assert(filename); |
| 299 | assert(lvalue); |
| 300 | assert(rvalue); |
| 301 | assert(data); |
| 302 | |
| 303 | if ((r = safe_atoi(rvalue, &priority)) < 0) { |
| 304 | log_error("[%s:%u] Failed to parse nice priority: %s", filename, line, rvalue); |
| 305 | return r; |
| 306 | } |
| 307 | |
| 308 | if (priority < PRIO_MIN || priority >= PRIO_MAX) { |
| 309 | log_error("[%s:%u] Nice priority out of range: %s", filename, line, rvalue); |
| 310 | return -ERANGE; |
| 311 | } |
| 312 | |
Lennart Poettering | fb33a39 | 2010-01-28 02:53:56 +0100 | [diff] [blame] | 313 | c->nice = priority; |
| 314 | c->nice_set = false; |
| 315 | |
Lennart Poettering | 034c6ed | 2010-01-26 04:18:44 +0100 | [diff] [blame] | 316 | return 0; |
| 317 | } |
| 318 | |
| 319 | static int config_parse_oom_adjust( |
| 320 | const char *filename, |
| 321 | unsigned line, |
| 322 | const char *section, |
| 323 | const char *lvalue, |
| 324 | const char *rvalue, |
| 325 | void *data, |
| 326 | void *userdata) { |
| 327 | |
Lennart Poettering | fb33a39 | 2010-01-28 02:53:56 +0100 | [diff] [blame] | 328 | ExecContext *c = data; |
| 329 | int oa, r; |
Lennart Poettering | 034c6ed | 2010-01-26 04:18:44 +0100 | [diff] [blame] | 330 | |
| 331 | assert(filename); |
| 332 | assert(lvalue); |
| 333 | assert(rvalue); |
| 334 | assert(data); |
| 335 | |
| 336 | if ((r = safe_atoi(rvalue, &oa)) < 0) { |
| 337 | log_error("[%s:%u] Failed to parse OOM adjust value: %s", filename, line, rvalue); |
| 338 | return r; |
| 339 | } |
| 340 | |
| 341 | if (oa < OOM_DISABLE || oa > OOM_ADJUST_MAX) { |
| 342 | log_error("[%s:%u] OOM adjust value out of range: %s", filename, line, rvalue); |
| 343 | return -ERANGE; |
| 344 | } |
| 345 | |
Lennart Poettering | fb33a39 | 2010-01-28 02:53:56 +0100 | [diff] [blame] | 346 | c->oom_adjust = oa; |
| 347 | c->oom_adjust_set = true; |
| 348 | |
Lennart Poettering | 034c6ed | 2010-01-26 04:18:44 +0100 | [diff] [blame] | 349 | return 0; |
| 350 | } |
| 351 | |
Lennart Poettering | b5a0699 | 2010-02-12 02:02:14 +0100 | [diff] [blame] | 352 | static int config_parse_mode( |
Lennart Poettering | 034c6ed | 2010-01-26 04:18:44 +0100 | [diff] [blame] | 353 | const char *filename, |
| 354 | unsigned line, |
| 355 | const char *section, |
| 356 | const char *lvalue, |
| 357 | const char *rvalue, |
| 358 | void *data, |
| 359 | void *userdata) { |
| 360 | |
| 361 | mode_t *m = data; |
| 362 | long l; |
| 363 | char *x = NULL; |
| 364 | |
| 365 | assert(filename); |
| 366 | assert(lvalue); |
| 367 | assert(rvalue); |
| 368 | assert(data); |
| 369 | |
| 370 | errno = 0; |
| 371 | l = strtol(rvalue, &x, 8); |
| 372 | if (!x || *x || errno) { |
Lennart Poettering | b5a0699 | 2010-02-12 02:02:14 +0100 | [diff] [blame] | 373 | log_error("[%s:%u] Failed to parse mode value: %s", filename, line, rvalue); |
Lennart Poettering | 034c6ed | 2010-01-26 04:18:44 +0100 | [diff] [blame] | 374 | return errno ? -errno : -EINVAL; |
| 375 | } |
| 376 | |
Lennart Poettering | b5a0699 | 2010-02-12 02:02:14 +0100 | [diff] [blame] | 377 | if (l < 0000 || l > 07777) { |
| 378 | log_error("[%s:%u] mode value out of range: %s", filename, line, rvalue); |
Lennart Poettering | 034c6ed | 2010-01-26 04:18:44 +0100 | [diff] [blame] | 379 | return -ERANGE; |
| 380 | } |
| 381 | |
| 382 | *m = (mode_t) l; |
| 383 | return 0; |
| 384 | } |
| 385 | |
| 386 | static int config_parse_exec( |
| 387 | const char *filename, |
| 388 | unsigned line, |
| 389 | const char *section, |
| 390 | const char *lvalue, |
| 391 | const char *rvalue, |
| 392 | void *data, |
| 393 | void *userdata) { |
| 394 | |
Lennart Poettering | a6a80b4 | 2010-02-14 01:05:55 +0100 | [diff] [blame] | 395 | ExecCommand **e = data, *nce = NULL; |
Lennart Poettering | 034c6ed | 2010-01-26 04:18:44 +0100 | [diff] [blame] | 396 | char **n; |
| 397 | char *w; |
| 398 | unsigned k; |
| 399 | size_t l; |
Lennart Poettering | 6c666e2 | 2010-05-19 21:51:53 +0200 | [diff] [blame] | 400 | char *state, *path = NULL; |
| 401 | bool honour_argv0, write_to_path; |
Lennart Poettering | 034c6ed | 2010-01-26 04:18:44 +0100 | [diff] [blame] | 402 | |
| 403 | assert(filename); |
| 404 | assert(lvalue); |
| 405 | assert(rvalue); |
| 406 | assert(data); |
| 407 | |
Lennart Poettering | 6c666e2 | 2010-05-19 21:51:53 +0200 | [diff] [blame] | 408 | /* We accept an absolute path as first argument, or |
| 409 | * alternatively an absolute prefixed with @ to allow |
| 410 | * overriding of argv[0]. */ |
| 411 | |
| 412 | honour_argv0 = rvalue[0] == '@'; |
| 413 | |
| 414 | if (rvalue[honour_argv0 ? 1 : 0] != '/') { |
| 415 | log_error("[%s:%u] Invalid executable path in command line: %s", filename, line, rvalue); |
| 416 | return -EINVAL; |
| 417 | } |
| 418 | |
Lennart Poettering | 034c6ed | 2010-01-26 04:18:44 +0100 | [diff] [blame] | 419 | k = 0; |
| 420 | FOREACH_WORD_QUOTED(w, l, rvalue, state) |
| 421 | k++; |
| 422 | |
Lennart Poettering | 6c666e2 | 2010-05-19 21:51:53 +0200 | [diff] [blame] | 423 | if (!(n = new(char*, k + (honour_argv0 ? 0 : 1)))) |
Lennart Poettering | 034c6ed | 2010-01-26 04:18:44 +0100 | [diff] [blame] | 424 | return -ENOMEM; |
| 425 | |
Lennart Poettering | 44d8db9 | 2010-01-26 07:02:51 +0100 | [diff] [blame] | 426 | k = 0; |
Lennart Poettering | 6c666e2 | 2010-05-19 21:51:53 +0200 | [diff] [blame] | 427 | write_to_path = honour_argv0; |
| 428 | FOREACH_WORD_QUOTED(w, l, rvalue, state) { |
| 429 | if (write_to_path) { |
| 430 | if (!(path = strndup(w+1, l-1))) |
| 431 | goto fail; |
| 432 | write_to_path = false; |
| 433 | } else { |
| 434 | if (!(n[k++] = strndup(w, l))) |
| 435 | goto fail; |
| 436 | } |
| 437 | } |
Lennart Poettering | 034c6ed | 2010-01-26 04:18:44 +0100 | [diff] [blame] | 438 | |
| 439 | n[k] = NULL; |
| 440 | |
Lennart Poettering | 6c666e2 | 2010-05-19 21:51:53 +0200 | [diff] [blame] | 441 | if (!n[0]) { |
| 442 | log_error("[%s:%u] Invalid command line: %s", filename, line, rvalue); |
Lennart Poettering | 034c6ed | 2010-01-26 04:18:44 +0100 | [diff] [blame] | 443 | strv_free(n); |
| 444 | return -EINVAL; |
| 445 | } |
| 446 | |
Lennart Poettering | 6c666e2 | 2010-05-19 21:51:53 +0200 | [diff] [blame] | 447 | if (!path) |
| 448 | if (!(path = strdup(n[0]))) |
| 449 | goto fail; |
| 450 | |
| 451 | assert(path_is_absolute(path)); |
| 452 | |
Lennart Poettering | 034c6ed | 2010-01-26 04:18:44 +0100 | [diff] [blame] | 453 | if (!(nce = new0(ExecCommand, 1))) |
| 454 | goto fail; |
| 455 | |
| 456 | nce->argv = n; |
Lennart Poettering | 6c666e2 | 2010-05-19 21:51:53 +0200 | [diff] [blame] | 457 | nce->path = path; |
Lennart Poettering | 034c6ed | 2010-01-26 04:18:44 +0100 | [diff] [blame] | 458 | |
Lennart Poettering | 01f7847 | 2010-05-24 05:25:33 +0200 | [diff] [blame] | 459 | path_kill_slashes(nce->path); |
| 460 | |
Lennart Poettering | a6a80b4 | 2010-02-14 01:05:55 +0100 | [diff] [blame] | 461 | exec_command_append_list(e, nce); |
Lennart Poettering | 034c6ed | 2010-01-26 04:18:44 +0100 | [diff] [blame] | 462 | |
| 463 | return 0; |
| 464 | |
| 465 | fail: |
Lennart Poettering | 6c666e2 | 2010-05-19 21:51:53 +0200 | [diff] [blame] | 466 | n[k] = NULL; |
| 467 | strv_free(n); |
| 468 | free(path); |
Lennart Poettering | 034c6ed | 2010-01-26 04:18:44 +0100 | [diff] [blame] | 469 | free(nce); |
| 470 | |
| 471 | return -ENOMEM; |
| 472 | } |
| 473 | |
| 474 | static int config_parse_usec( |
| 475 | const char *filename, |
| 476 | unsigned line, |
| 477 | const char *section, |
| 478 | const char *lvalue, |
| 479 | const char *rvalue, |
| 480 | void *data, |
| 481 | void *userdata) { |
| 482 | |
| 483 | usec_t *usec = data; |
Lennart Poettering | 034c6ed | 2010-01-26 04:18:44 +0100 | [diff] [blame] | 484 | int r; |
| 485 | |
| 486 | assert(filename); |
| 487 | assert(lvalue); |
| 488 | assert(rvalue); |
| 489 | assert(data); |
| 490 | |
Lennart Poettering | 24a6e4a | 2010-04-23 20:26:59 +0200 | [diff] [blame] | 491 | if ((r = parse_usec(rvalue, usec)) < 0) { |
Lennart Poettering | 034c6ed | 2010-01-26 04:18:44 +0100 | [diff] [blame] | 492 | log_error("[%s:%u] Failed to parse time value: %s", filename, line, rvalue); |
| 493 | return r; |
| 494 | } |
| 495 | |
Lennart Poettering | 034c6ed | 2010-01-26 04:18:44 +0100 | [diff] [blame] | 496 | return 0; |
| 497 | } |
| 498 | |
Lennart Poettering | 0d87eb4 | 2010-04-13 02:22:41 +0200 | [diff] [blame] | 499 | DEFINE_CONFIG_PARSE_ENUM(config_parse_service_type, service_type, ServiceType, "Failed to parse service type"); |
| 500 | DEFINE_CONFIG_PARSE_ENUM(config_parse_service_restart, service_restart, ServiceRestart, "Failed to parse service restart specifier"); |
Lennart Poettering | 034c6ed | 2010-01-26 04:18:44 +0100 | [diff] [blame] | 501 | |
Lennart Poettering | 47be870 | 2010-02-03 14:21:48 +0100 | [diff] [blame] | 502 | static int config_parse_bindtodevice( |
Lennart Poettering | acbb022 | 2010-01-27 04:31:52 +0100 | [diff] [blame] | 503 | const char *filename, |
| 504 | unsigned line, |
| 505 | const char *section, |
| 506 | const char *lvalue, |
| 507 | const char *rvalue, |
| 508 | void *data, |
| 509 | void *userdata) { |
| 510 | |
| 511 | Socket *s = data; |
| 512 | char *n; |
| 513 | |
| 514 | assert(filename); |
| 515 | assert(lvalue); |
| 516 | assert(rvalue); |
| 517 | assert(data); |
| 518 | |
| 519 | if (rvalue[0] && !streq(rvalue, "*")) { |
| 520 | if (!(n = strdup(rvalue))) |
| 521 | return -ENOMEM; |
| 522 | } else |
| 523 | n = NULL; |
| 524 | |
| 525 | free(s->bind_to_device); |
| 526 | s->bind_to_device = n; |
| 527 | |
| 528 | return 0; |
| 529 | } |
| 530 | |
Lennart Poettering | 0d87eb4 | 2010-04-13 02:22:41 +0200 | [diff] [blame] | 531 | DEFINE_CONFIG_PARSE_ENUM(config_parse_output, exec_output, ExecOutput, "Failed to parse output specifier"); |
| 532 | DEFINE_CONFIG_PARSE_ENUM(config_parse_input, exec_input, ExecInput, "Failed to parse input specifier"); |
Lennart Poettering | 071830f | 2010-01-28 02:06:20 +0100 | [diff] [blame] | 533 | |
Lennart Poettering | 47be870 | 2010-02-03 14:21:48 +0100 | [diff] [blame] | 534 | static int config_parse_facility( |
Lennart Poettering | 071830f | 2010-01-28 02:06:20 +0100 | [diff] [blame] | 535 | const char *filename, |
| 536 | unsigned line, |
| 537 | const char *section, |
| 538 | const char *lvalue, |
| 539 | const char *rvalue, |
| 540 | void *data, |
| 541 | void *userdata) { |
| 542 | |
Lennart Poettering | 071830f | 2010-01-28 02:06:20 +0100 | [diff] [blame] | 543 | |
Lennart Poettering | 94f0434 | 2010-01-30 01:55:42 +0100 | [diff] [blame] | 544 | int *o = data, x; |
Lennart Poettering | 071830f | 2010-01-28 02:06:20 +0100 | [diff] [blame] | 545 | |
| 546 | assert(filename); |
| 547 | assert(lvalue); |
| 548 | assert(rvalue); |
| 549 | assert(data); |
| 550 | |
Lennart Poettering | 0d87eb4 | 2010-04-13 02:22:41 +0200 | [diff] [blame] | 551 | if ((x = log_facility_from_string(rvalue)) < 0) { |
| 552 | log_error("[%s:%u] Failed to parse log facility: %s", filename, line, rvalue); |
| 553 | return -EBADMSG; |
| 554 | } |
Lennart Poettering | 94f0434 | 2010-01-30 01:55:42 +0100 | [diff] [blame] | 555 | |
| 556 | *o = LOG_MAKEPRI(x, LOG_PRI(*o)); |
Lennart Poettering | 071830f | 2010-01-28 02:06:20 +0100 | [diff] [blame] | 557 | |
| 558 | return 0; |
| 559 | } |
| 560 | |
Lennart Poettering | 47be870 | 2010-02-03 14:21:48 +0100 | [diff] [blame] | 561 | static int config_parse_level( |
Lennart Poettering | 071830f | 2010-01-28 02:06:20 +0100 | [diff] [blame] | 562 | const char *filename, |
| 563 | unsigned line, |
| 564 | const char *section, |
| 565 | const char *lvalue, |
| 566 | const char *rvalue, |
| 567 | void *data, |
| 568 | void *userdata) { |
| 569 | |
Lennart Poettering | 071830f | 2010-01-28 02:06:20 +0100 | [diff] [blame] | 570 | |
Lennart Poettering | 94f0434 | 2010-01-30 01:55:42 +0100 | [diff] [blame] | 571 | int *o = data, x; |
Lennart Poettering | 071830f | 2010-01-28 02:06:20 +0100 | [diff] [blame] | 572 | |
| 573 | assert(filename); |
| 574 | assert(lvalue); |
| 575 | assert(rvalue); |
| 576 | assert(data); |
| 577 | |
Lennart Poettering | 0d87eb4 | 2010-04-13 02:22:41 +0200 | [diff] [blame] | 578 | if ((x = log_level_from_string(rvalue)) < 0) { |
| 579 | log_error("[%s:%u] Failed to parse log level: %s", filename, line, rvalue); |
| 580 | return -EBADMSG; |
| 581 | } |
Lennart Poettering | 071830f | 2010-01-28 02:06:20 +0100 | [diff] [blame] | 582 | |
Lennart Poettering | 94f0434 | 2010-01-30 01:55:42 +0100 | [diff] [blame] | 583 | *o = LOG_MAKEPRI(LOG_FAC(*o), x); |
Lennart Poettering | 071830f | 2010-01-28 02:06:20 +0100 | [diff] [blame] | 584 | return 0; |
| 585 | } |
| 586 | |
Lennart Poettering | 47be870 | 2010-02-03 14:21:48 +0100 | [diff] [blame] | 587 | static int config_parse_io_class( |
Lennart Poettering | 9eba9da | 2010-01-29 20:46:22 +0100 | [diff] [blame] | 588 | const char *filename, |
| 589 | unsigned line, |
| 590 | const char *section, |
| 591 | const char *lvalue, |
| 592 | const char *rvalue, |
| 593 | void *data, |
| 594 | void *userdata) { |
| 595 | |
Lennart Poettering | 9eba9da | 2010-01-29 20:46:22 +0100 | [diff] [blame] | 596 | ExecContext *c = data; |
Lennart Poettering | 94f0434 | 2010-01-30 01:55:42 +0100 | [diff] [blame] | 597 | int x; |
Lennart Poettering | 9eba9da | 2010-01-29 20:46:22 +0100 | [diff] [blame] | 598 | |
| 599 | assert(filename); |
| 600 | assert(lvalue); |
| 601 | assert(rvalue); |
| 602 | assert(data); |
| 603 | |
Lennart Poettering | 0d87eb4 | 2010-04-13 02:22:41 +0200 | [diff] [blame] | 604 | if ((x = ioprio_class_from_string(rvalue)) < 0) { |
| 605 | log_error("[%s:%u] Failed to parse IO scheduling class: %s", filename, line, rvalue); |
| 606 | return -EBADMSG; |
| 607 | } |
Lennart Poettering | 9eba9da | 2010-01-29 20:46:22 +0100 | [diff] [blame] | 608 | |
Lennart Poettering | 94f0434 | 2010-01-30 01:55:42 +0100 | [diff] [blame] | 609 | c->ioprio = IOPRIO_PRIO_VALUE(x, IOPRIO_PRIO_DATA(c->ioprio)); |
Lennart Poettering | 9eba9da | 2010-01-29 20:46:22 +0100 | [diff] [blame] | 610 | c->ioprio_set = true; |
| 611 | |
| 612 | return 0; |
| 613 | } |
| 614 | |
Lennart Poettering | 47be870 | 2010-02-03 14:21:48 +0100 | [diff] [blame] | 615 | static int config_parse_io_priority( |
Lennart Poettering | 9eba9da | 2010-01-29 20:46:22 +0100 | [diff] [blame] | 616 | const char *filename, |
| 617 | unsigned line, |
| 618 | const char *section, |
| 619 | const char *lvalue, |
| 620 | const char *rvalue, |
| 621 | void *data, |
| 622 | void *userdata) { |
| 623 | |
| 624 | ExecContext *c = data; |
| 625 | int i; |
| 626 | |
| 627 | assert(filename); |
| 628 | assert(lvalue); |
| 629 | assert(rvalue); |
| 630 | assert(data); |
| 631 | |
Lennart Poettering | 94f0434 | 2010-01-30 01:55:42 +0100 | [diff] [blame] | 632 | if (safe_atoi(rvalue, &i) < 0 || i < 0 || i >= IOPRIO_BE_NR) { |
Lennart Poettering | 9eba9da | 2010-01-29 20:46:22 +0100 | [diff] [blame] | 633 | log_error("[%s:%u] Failed to parse io priority: %s", filename, line, rvalue); |
| 634 | return -EBADMSG; |
| 635 | } |
| 636 | |
Lennart Poettering | 94f0434 | 2010-01-30 01:55:42 +0100 | [diff] [blame] | 637 | c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c->ioprio), i); |
Lennart Poettering | 9eba9da | 2010-01-29 20:46:22 +0100 | [diff] [blame] | 638 | c->ioprio_set = true; |
| 639 | |
| 640 | return 0; |
| 641 | } |
| 642 | |
Lennart Poettering | 47be870 | 2010-02-03 14:21:48 +0100 | [diff] [blame] | 643 | static int config_parse_cpu_sched_policy( |
Lennart Poettering | 94f0434 | 2010-01-30 01:55:42 +0100 | [diff] [blame] | 644 | const char *filename, |
| 645 | unsigned line, |
| 646 | const char *section, |
| 647 | const char *lvalue, |
| 648 | const char *rvalue, |
| 649 | void *data, |
| 650 | void *userdata) { |
| 651 | |
| 652 | |
| 653 | ExecContext *c = data; |
| 654 | int x; |
| 655 | |
| 656 | assert(filename); |
| 657 | assert(lvalue); |
| 658 | assert(rvalue); |
| 659 | assert(data); |
| 660 | |
Lennart Poettering | 0d87eb4 | 2010-04-13 02:22:41 +0200 | [diff] [blame] | 661 | if ((x = sched_policy_from_string(rvalue)) < 0) { |
| 662 | log_error("[%s:%u] Failed to parse CPU scheduling policy: %s", filename, line, rvalue); |
| 663 | return -EBADMSG; |
| 664 | } |
Lennart Poettering | 94f0434 | 2010-01-30 01:55:42 +0100 | [diff] [blame] | 665 | |
| 666 | c->cpu_sched_policy = x; |
| 667 | c->cpu_sched_set = true; |
| 668 | |
| 669 | return 0; |
| 670 | } |
| 671 | |
Lennart Poettering | 47be870 | 2010-02-03 14:21:48 +0100 | [diff] [blame] | 672 | static int config_parse_cpu_sched_prio( |
Lennart Poettering | 94f0434 | 2010-01-30 01:55:42 +0100 | [diff] [blame] | 673 | const char *filename, |
| 674 | unsigned line, |
| 675 | const char *section, |
| 676 | const char *lvalue, |
| 677 | const char *rvalue, |
| 678 | void *data, |
| 679 | void *userdata) { |
| 680 | |
| 681 | ExecContext *c = data; |
| 682 | int i; |
| 683 | |
| 684 | assert(filename); |
| 685 | assert(lvalue); |
| 686 | assert(rvalue); |
| 687 | assert(data); |
| 688 | |
| 689 | /* On Linux RR/FIFO have the same range */ |
| 690 | if (safe_atoi(rvalue, &i) < 0 || i < sched_get_priority_min(SCHED_RR) || i > sched_get_priority_max(SCHED_RR)) { |
| 691 | log_error("[%s:%u] Failed to parse CPU scheduling priority: %s", filename, line, rvalue); |
| 692 | return -EBADMSG; |
| 693 | } |
| 694 | |
| 695 | c->cpu_sched_priority = i; |
| 696 | c->cpu_sched_set = true; |
| 697 | |
| 698 | return 0; |
| 699 | } |
| 700 | |
Lennart Poettering | 47be870 | 2010-02-03 14:21:48 +0100 | [diff] [blame] | 701 | static int config_parse_cpu_affinity( |
Lennart Poettering | 94f0434 | 2010-01-30 01:55:42 +0100 | [diff] [blame] | 702 | const char *filename, |
| 703 | unsigned line, |
| 704 | const char *section, |
| 705 | const char *lvalue, |
| 706 | const char *rvalue, |
| 707 | void *data, |
| 708 | void *userdata) { |
| 709 | |
| 710 | ExecContext *c = data; |
| 711 | char *w; |
| 712 | size_t l; |
| 713 | char *state; |
| 714 | |
| 715 | assert(filename); |
| 716 | assert(lvalue); |
| 717 | assert(rvalue); |
| 718 | assert(data); |
| 719 | |
Lennart Poettering | f62c0e4 | 2010-02-14 01:07:36 +0100 | [diff] [blame] | 720 | FOREACH_WORD(w, l, rvalue, state) { |
Lennart Poettering | 94f0434 | 2010-01-30 01:55:42 +0100 | [diff] [blame] | 721 | char *t; |
| 722 | int r; |
| 723 | unsigned cpu; |
| 724 | |
| 725 | if (!(t = strndup(w, l))) |
| 726 | return -ENOMEM; |
| 727 | |
Lennart Poettering | 82c121a | 2010-07-04 16:44:58 +0200 | [diff] [blame^] | 728 | if (!(c->cpuset)) |
| 729 | if (!(c->cpuset = cpu_set_malloc(&c->cpuset_ncpus))) |
| 730 | return -ENOMEM; |
| 731 | |
Lennart Poettering | 94f0434 | 2010-01-30 01:55:42 +0100 | [diff] [blame] | 732 | r = safe_atou(t, &cpu); |
| 733 | free(t); |
| 734 | |
Lennart Poettering | 82c121a | 2010-07-04 16:44:58 +0200 | [diff] [blame^] | 735 | if (r < 0 || cpu >= c->cpuset_ncpus) { |
Lennart Poettering | 94f0434 | 2010-01-30 01:55:42 +0100 | [diff] [blame] | 736 | log_error("[%s:%u] Failed to parse CPU affinity: %s", filename, line, rvalue); |
| 737 | return -EBADMSG; |
| 738 | } |
| 739 | |
Lennart Poettering | 82c121a | 2010-07-04 16:44:58 +0200 | [diff] [blame^] | 740 | CPU_SET_S(cpu, CPU_ALLOC_SIZE(c->cpuset_ncpus), c->cpuset); |
Lennart Poettering | 94f0434 | 2010-01-30 01:55:42 +0100 | [diff] [blame] | 741 | } |
| 742 | |
Lennart Poettering | 94f0434 | 2010-01-30 01:55:42 +0100 | [diff] [blame] | 743 | return 0; |
| 744 | } |
| 745 | |
Lennart Poettering | 47be870 | 2010-02-03 14:21:48 +0100 | [diff] [blame] | 746 | static int config_parse_capabilities( |
Lennart Poettering | 94f0434 | 2010-01-30 01:55:42 +0100 | [diff] [blame] | 747 | const char *filename, |
| 748 | unsigned line, |
| 749 | const char *section, |
| 750 | const char *lvalue, |
| 751 | const char *rvalue, |
| 752 | void *data, |
| 753 | void *userdata) { |
| 754 | |
| 755 | ExecContext *c = data; |
| 756 | cap_t cap; |
| 757 | |
| 758 | assert(filename); |
| 759 | assert(lvalue); |
| 760 | assert(rvalue); |
| 761 | assert(data); |
| 762 | |
| 763 | if (!(cap = cap_from_text(rvalue))) { |
| 764 | if (errno == ENOMEM) |
| 765 | return -ENOMEM; |
| 766 | |
| 767 | log_error("[%s:%u] Failed to parse capabilities: %s", filename, line, rvalue); |
| 768 | return -EBADMSG; |
| 769 | } |
| 770 | |
| 771 | if (c->capabilities) |
| 772 | cap_free(c->capabilities); |
| 773 | c->capabilities = cap; |
| 774 | |
| 775 | return 0; |
| 776 | } |
| 777 | |
Lennart Poettering | 47be870 | 2010-02-03 14:21:48 +0100 | [diff] [blame] | 778 | static int config_parse_secure_bits( |
Lennart Poettering | 94f0434 | 2010-01-30 01:55:42 +0100 | [diff] [blame] | 779 | const char *filename, |
| 780 | unsigned line, |
| 781 | const char *section, |
| 782 | const char *lvalue, |
| 783 | const char *rvalue, |
| 784 | void *data, |
| 785 | void *userdata) { |
| 786 | |
| 787 | ExecContext *c = data; |
| 788 | char *w; |
| 789 | size_t l; |
| 790 | char *state; |
| 791 | |
| 792 | assert(filename); |
| 793 | assert(lvalue); |
| 794 | assert(rvalue); |
| 795 | assert(data); |
| 796 | |
Lennart Poettering | f62c0e4 | 2010-02-14 01:07:36 +0100 | [diff] [blame] | 797 | FOREACH_WORD(w, l, rvalue, state) { |
Lennart Poettering | 94f0434 | 2010-01-30 01:55:42 +0100 | [diff] [blame] | 798 | if (first_word(w, "keep-caps")) |
| 799 | c->secure_bits |= SECURE_KEEP_CAPS; |
| 800 | else if (first_word(w, "keep-caps-locked")) |
| 801 | c->secure_bits |= SECURE_KEEP_CAPS_LOCKED; |
| 802 | else if (first_word(w, "no-setuid-fixup")) |
| 803 | c->secure_bits |= SECURE_NO_SETUID_FIXUP; |
| 804 | else if (first_word(w, "no-setuid-fixup-locked")) |
| 805 | c->secure_bits |= SECURE_NO_SETUID_FIXUP_LOCKED; |
| 806 | else if (first_word(w, "noroot")) |
| 807 | c->secure_bits |= SECURE_NOROOT; |
| 808 | else if (first_word(w, "noroot-locked")) |
| 809 | c->secure_bits |= SECURE_NOROOT_LOCKED; |
| 810 | else { |
| 811 | log_error("[%s:%u] Failed to parse secure bits: %s", filename, line, rvalue); |
| 812 | return -EBADMSG; |
| 813 | } |
| 814 | } |
| 815 | |
| 816 | return 0; |
| 817 | } |
| 818 | |
Lennart Poettering | 47be870 | 2010-02-03 14:21:48 +0100 | [diff] [blame] | 819 | static int config_parse_bounding_set( |
Lennart Poettering | 94f0434 | 2010-01-30 01:55:42 +0100 | [diff] [blame] | 820 | const char *filename, |
| 821 | unsigned line, |
| 822 | const char *section, |
| 823 | const char *lvalue, |
| 824 | const char *rvalue, |
| 825 | void *data, |
| 826 | void *userdata) { |
| 827 | |
| 828 | ExecContext *c = data; |
| 829 | char *w; |
| 830 | size_t l; |
| 831 | char *state; |
| 832 | |
| 833 | assert(filename); |
| 834 | assert(lvalue); |
| 835 | assert(rvalue); |
| 836 | assert(data); |
| 837 | |
Lennart Poettering | f62c0e4 | 2010-02-14 01:07:36 +0100 | [diff] [blame] | 838 | FOREACH_WORD(w, l, rvalue, state) { |
Lennart Poettering | 94f0434 | 2010-01-30 01:55:42 +0100 | [diff] [blame] | 839 | char *t; |
| 840 | int r; |
| 841 | cap_value_t cap; |
| 842 | |
| 843 | if (!(t = strndup(w, l))) |
| 844 | return -ENOMEM; |
| 845 | |
| 846 | r = cap_from_name(t, &cap); |
| 847 | free(t); |
| 848 | |
| 849 | if (r < 0) { |
| 850 | log_error("[%s:%u] Failed to parse capability bounding set: %s", filename, line, rvalue); |
| 851 | return -EBADMSG; |
| 852 | } |
| 853 | |
| 854 | c->capability_bounding_set_drop |= 1 << cap; |
| 855 | } |
| 856 | |
| 857 | return 0; |
| 858 | } |
| 859 | |
| 860 | static int config_parse_timer_slack_ns( |
| 861 | const char *filename, |
| 862 | unsigned line, |
| 863 | const char *section, |
| 864 | const char *lvalue, |
| 865 | const char *rvalue, |
| 866 | void *data, |
| 867 | void *userdata) { |
| 868 | |
| 869 | ExecContext *c = data; |
| 870 | unsigned long u; |
| 871 | int r; |
| 872 | |
| 873 | assert(filename); |
| 874 | assert(lvalue); |
| 875 | assert(rvalue); |
| 876 | assert(data); |
| 877 | |
| 878 | if ((r = safe_atolu(rvalue, &u)) < 0) { |
| 879 | log_error("[%s:%u] Failed to parse time slack value: %s", filename, line, rvalue); |
| 880 | return r; |
| 881 | } |
| 882 | |
| 883 | c->timer_slack_ns = u; |
| 884 | |
| 885 | return 0; |
| 886 | } |
| 887 | |
| 888 | static int config_parse_limit( |
| 889 | const char *filename, |
| 890 | unsigned line, |
| 891 | const char *section, |
| 892 | const char *lvalue, |
| 893 | const char *rvalue, |
| 894 | void *data, |
| 895 | void *userdata) { |
| 896 | |
| 897 | struct rlimit **rl = data; |
| 898 | unsigned long long u; |
| 899 | int r; |
| 900 | |
| 901 | assert(filename); |
| 902 | assert(lvalue); |
| 903 | assert(rvalue); |
| 904 | assert(data); |
| 905 | |
| 906 | if ((r = safe_atollu(rvalue, &u)) < 0) { |
| 907 | log_error("[%s:%u] Failed to parse resource value: %s", filename, line, rvalue); |
| 908 | return r; |
| 909 | } |
| 910 | |
| 911 | if (!*rl) |
| 912 | if (!(*rl = new(struct rlimit, 1))) |
| 913 | return -ENOMEM; |
| 914 | |
| 915 | (*rl)->rlim_cur = (*rl)->rlim_max = (rlim_t) u; |
| 916 | return 0; |
| 917 | } |
| 918 | |
Lennart Poettering | 8e27452 | 2010-03-31 16:29:55 +0200 | [diff] [blame] | 919 | static int config_parse_cgroup( |
| 920 | const char *filename, |
| 921 | unsigned line, |
| 922 | const char *section, |
| 923 | const char *lvalue, |
| 924 | const char *rvalue, |
| 925 | void *data, |
| 926 | void *userdata) { |
| 927 | |
| 928 | Unit *u = userdata; |
| 929 | char *w; |
| 930 | size_t l; |
| 931 | char *state; |
| 932 | |
| 933 | FOREACH_WORD(w, l, rvalue, state) { |
| 934 | char *t; |
| 935 | int r; |
| 936 | |
| 937 | if (!(t = strndup(w, l))) |
| 938 | return -ENOMEM; |
| 939 | |
| 940 | r = unit_add_cgroup_from_text(u, t); |
| 941 | free(t); |
| 942 | |
| 943 | if (r < 0) |
| 944 | return r; |
| 945 | } |
| 946 | |
| 947 | return 0; |
| 948 | } |
| 949 | |
Lennart Poettering | a9a1e00 | 2010-04-06 02:40:10 +0200 | [diff] [blame] | 950 | static int config_parse_sysv_priority( |
| 951 | const char *filename, |
| 952 | unsigned line, |
| 953 | const char *section, |
| 954 | const char *lvalue, |
| 955 | const char *rvalue, |
| 956 | void *data, |
| 957 | void *userdata) { |
| 958 | |
| 959 | int *priority = data; |
| 960 | int r, i; |
| 961 | |
| 962 | assert(filename); |
| 963 | assert(lvalue); |
| 964 | assert(rvalue); |
| 965 | assert(data); |
| 966 | |
| 967 | if ((r = safe_atoi(rvalue, &i)) < 0 || i < 0) { |
| 968 | log_error("[%s:%u] Failed to parse SysV start priority: %s", filename, line, rvalue); |
| 969 | return r; |
| 970 | } |
| 971 | |
| 972 | *priority = (int) i; |
| 973 | return 0; |
| 974 | } |
| 975 | |
Lennart Poettering | 0d87eb4 | 2010-04-13 02:22:41 +0200 | [diff] [blame] | 976 | DEFINE_CONFIG_PARSE_ENUM(config_parse_kill_mode, kill_mode, KillMode, "Failed to parse kill mode"); |
Lennart Poettering | 50159e6 | 2010-04-08 00:52:14 +0200 | [diff] [blame] | 977 | |
Lennart Poettering | 15ae422 | 2010-04-21 22:15:06 +0200 | [diff] [blame] | 978 | static int config_parse_mount_flags( |
| 979 | const char *filename, |
| 980 | unsigned line, |
| 981 | const char *section, |
| 982 | const char *lvalue, |
| 983 | const char *rvalue, |
| 984 | void *data, |
| 985 | void *userdata) { |
| 986 | |
| 987 | ExecContext *c = data; |
| 988 | char *w; |
| 989 | size_t l; |
| 990 | char *state; |
| 991 | unsigned long flags = 0; |
| 992 | |
| 993 | assert(filename); |
| 994 | assert(lvalue); |
| 995 | assert(rvalue); |
| 996 | assert(data); |
| 997 | |
| 998 | FOREACH_WORD(w, l, rvalue, state) { |
| 999 | if (strncmp(w, "shared", l) == 0) |
| 1000 | flags |= MS_SHARED; |
| 1001 | else if (strncmp(w, "slave", l) == 0) |
| 1002 | flags |= MS_SLAVE; |
| 1003 | else if (strncmp(w, "private", l) == 0) |
| 1004 | flags |= MS_PRIVATE; |
| 1005 | else { |
| 1006 | log_error("[%s:%u] Failed to parse mount flags: %s", filename, line, rvalue); |
| 1007 | return -EINVAL; |
| 1008 | } |
| 1009 | } |
| 1010 | |
| 1011 | c->mount_flags = flags; |
| 1012 | return 0; |
| 1013 | } |
| 1014 | |
Lennart Poettering | 871d7de | 2010-05-24 01:45:54 +0200 | [diff] [blame] | 1015 | static int config_parse_timer( |
| 1016 | const char *filename, |
| 1017 | unsigned line, |
| 1018 | const char *section, |
| 1019 | const char *lvalue, |
| 1020 | const char *rvalue, |
| 1021 | void *data, |
| 1022 | void *userdata) { |
| 1023 | |
| 1024 | Timer *t = data; |
| 1025 | usec_t u; |
| 1026 | int r; |
| 1027 | TimerValue *v; |
| 1028 | TimerBase b; |
| 1029 | |
| 1030 | assert(filename); |
| 1031 | assert(lvalue); |
| 1032 | assert(rvalue); |
| 1033 | assert(data); |
| 1034 | |
| 1035 | if ((b = timer_base_from_string(lvalue)) < 0) { |
| 1036 | log_error("[%s:%u] Failed to parse timer base: %s", filename, line, lvalue); |
| 1037 | return -EINVAL; |
| 1038 | } |
| 1039 | |
| 1040 | if ((r = parse_usec(rvalue, &u)) < 0) { |
| 1041 | log_error("[%s:%u] Failed to parse timer value: %s", filename, line, rvalue); |
| 1042 | return r; |
| 1043 | } |
| 1044 | |
| 1045 | if (!(v = new0(TimerValue, 1))) |
| 1046 | return -ENOMEM; |
| 1047 | |
| 1048 | v->base = b; |
| 1049 | v->value = u; |
| 1050 | |
| 1051 | LIST_PREPEND(TimerValue, value, t->values, v); |
| 1052 | |
| 1053 | return 0; |
| 1054 | } |
| 1055 | |
| 1056 | static int config_parse_timer_unit( |
| 1057 | const char *filename, |
| 1058 | unsigned line, |
| 1059 | const char *section, |
| 1060 | const char *lvalue, |
| 1061 | const char *rvalue, |
| 1062 | void *data, |
| 1063 | void *userdata) { |
| 1064 | |
| 1065 | Timer *t = data; |
| 1066 | int r; |
| 1067 | |
| 1068 | if (endswith(rvalue, ".timer")) { |
| 1069 | log_error("[%s:%u] Unit cannot be of type timer: %s", filename, line, rvalue); |
| 1070 | return -EINVAL; |
| 1071 | } |
| 1072 | |
| 1073 | if ((r = manager_load_unit(t->meta.manager, rvalue, NULL, &t->unit)) < 0) { |
| 1074 | log_error("[%s:%u] Failed to load unit: %s", filename, line, rvalue); |
| 1075 | return r; |
| 1076 | } |
| 1077 | |
| 1078 | return 0; |
| 1079 | } |
| 1080 | |
Lennart Poettering | 01f7847 | 2010-05-24 05:25:33 +0200 | [diff] [blame] | 1081 | static int config_parse_path_spec( |
| 1082 | const char *filename, |
| 1083 | unsigned line, |
| 1084 | const char *section, |
| 1085 | const char *lvalue, |
| 1086 | const char *rvalue, |
| 1087 | void *data, |
| 1088 | void *userdata) { |
| 1089 | |
| 1090 | Path *p = data; |
| 1091 | PathSpec *s; |
| 1092 | PathType b; |
| 1093 | |
| 1094 | assert(filename); |
| 1095 | assert(lvalue); |
| 1096 | assert(rvalue); |
| 1097 | assert(data); |
| 1098 | |
| 1099 | if ((b = path_type_from_string(lvalue)) < 0) { |
| 1100 | log_error("[%s:%u] Failed to parse path type: %s", filename, line, lvalue); |
| 1101 | return -EINVAL; |
| 1102 | } |
| 1103 | |
| 1104 | if (!path_is_absolute(rvalue)) { |
| 1105 | log_error("[%s:%u] Path is not absolute: %s", filename, line, rvalue); |
| 1106 | return -EINVAL; |
| 1107 | } |
| 1108 | |
| 1109 | if (!(s = new0(PathSpec, 1))) |
| 1110 | return -ENOMEM; |
| 1111 | |
| 1112 | if (!(s->path = strdup(rvalue))) { |
| 1113 | free(s); |
| 1114 | return -ENOMEM; |
| 1115 | } |
| 1116 | |
| 1117 | path_kill_slashes(s->path); |
| 1118 | |
| 1119 | s->type = b; |
| 1120 | s->inotify_fd = -1; |
| 1121 | |
| 1122 | LIST_PREPEND(PathSpec, spec, p->specs, s); |
| 1123 | |
| 1124 | return 0; |
| 1125 | } |
| 1126 | |
| 1127 | static int config_parse_path_unit( |
| 1128 | const char *filename, |
| 1129 | unsigned line, |
| 1130 | const char *section, |
| 1131 | const char *lvalue, |
| 1132 | const char *rvalue, |
| 1133 | void *data, |
| 1134 | void *userdata) { |
| 1135 | |
| 1136 | Path *t = data; |
| 1137 | int r; |
| 1138 | |
| 1139 | if (endswith(rvalue, ".path")) { |
| 1140 | log_error("[%s:%u] Unit cannot be of type path: %s", filename, line, rvalue); |
| 1141 | return -EINVAL; |
| 1142 | } |
| 1143 | |
| 1144 | if ((r = manager_load_unit(t->meta.manager, rvalue, NULL, &t->unit)) < 0) { |
| 1145 | log_error("[%s:%u] Failed to load unit: %s", filename, line, rvalue); |
| 1146 | return r; |
| 1147 | } |
| 1148 | |
| 1149 | return 0; |
| 1150 | } |
| 1151 | |
Lennart Poettering | ddb26e1 | 2010-06-18 06:06:24 +0200 | [diff] [blame] | 1152 | static int config_parse_env_file( |
| 1153 | const char *filename, |
| 1154 | unsigned line, |
| 1155 | const char *section, |
| 1156 | const char *lvalue, |
| 1157 | const char *rvalue, |
| 1158 | void *data, |
| 1159 | void *userdata) { |
| 1160 | |
| 1161 | FILE *f; |
| 1162 | int r; |
| 1163 | char ***env = data; |
| 1164 | |
| 1165 | assert(filename); |
| 1166 | assert(lvalue); |
| 1167 | assert(rvalue); |
| 1168 | assert(data); |
| 1169 | |
| 1170 | if (!(f = fopen(rvalue, "re"))) { |
| 1171 | log_error("[%s:%u] Failed to open environment file '%s': %m", filename, line, rvalue); |
| 1172 | return -errno; |
| 1173 | } |
| 1174 | |
| 1175 | while (!feof(f)) { |
| 1176 | char l[LINE_MAX], *p; |
| 1177 | char **t; |
| 1178 | |
| 1179 | if (!fgets(l, sizeof(l), f)) { |
| 1180 | if (feof(f)) |
| 1181 | break; |
| 1182 | |
| 1183 | r = -errno; |
| 1184 | log_error("[%s:%u] Failed to read environment file '%s': %m", filename, line, rvalue); |
| 1185 | goto finish; |
| 1186 | } |
| 1187 | |
| 1188 | p = strstrip(l); |
| 1189 | |
| 1190 | if (!*p) |
| 1191 | continue; |
| 1192 | |
| 1193 | if (strchr(COMMENTS, *p)) |
| 1194 | continue; |
| 1195 | |
| 1196 | t = strv_env_set(*env, p); |
| 1197 | strv_free(*env); |
| 1198 | *env = t; |
| 1199 | } |
| 1200 | |
| 1201 | r = 0; |
| 1202 | |
| 1203 | finish: |
| 1204 | if (f) |
| 1205 | fclose(f); |
| 1206 | |
| 1207 | return r; |
| 1208 | } |
| 1209 | |
Lennart Poettering | 4fd5948 | 2010-07-01 00:29:17 +0200 | [diff] [blame] | 1210 | static int config_parse_ip_tos( |
| 1211 | const char *filename, |
| 1212 | unsigned line, |
| 1213 | const char *section, |
| 1214 | const char *lvalue, |
| 1215 | const char *rvalue, |
| 1216 | void *data, |
| 1217 | void *userdata) { |
| 1218 | |
| 1219 | int *ip_tos = data, x; |
| 1220 | int r; |
| 1221 | |
| 1222 | assert(filename); |
| 1223 | assert(lvalue); |
| 1224 | assert(rvalue); |
| 1225 | assert(data); |
| 1226 | |
| 1227 | if ((x = ip_tos_from_string(rvalue)) < 0) |
| 1228 | if ((r = safe_atoi(rvalue, &x)) < 0) { |
| 1229 | log_error("[%s:%u] Failed to parse IP TOS value: %s", filename, line, rvalue); |
| 1230 | return r; |
| 1231 | } |
| 1232 | |
| 1233 | *ip_tos = x; |
| 1234 | return 0; |
| 1235 | } |
| 1236 | |
Lennart Poettering | c952c6e | 2010-06-18 23:12:48 +0200 | [diff] [blame] | 1237 | DEFINE_CONFIG_PARSE_ENUM(config_parse_notify_access, notify_access, NotifyAccess, "Failed to parse notify access specifier"); |
| 1238 | |
Lennart Poettering | 071830f | 2010-01-28 02:06:20 +0100 | [diff] [blame] | 1239 | #define FOLLOW_MAX 8 |
Lennart Poettering | 87f0e41 | 2010-01-26 21:39:06 +0100 | [diff] [blame] | 1240 | |
Lennart Poettering | 9e2f7c1 | 2010-04-15 03:11:11 +0200 | [diff] [blame] | 1241 | static int open_follow(char **filename, FILE **_f, Set *names, char **_final) { |
Lennart Poettering | 0301abf | 2010-01-27 00:15:56 +0100 | [diff] [blame] | 1242 | unsigned c = 0; |
Lennart Poettering | 87f0e41 | 2010-01-26 21:39:06 +0100 | [diff] [blame] | 1243 | int fd, r; |
| 1244 | FILE *f; |
Lennart Poettering | 0301abf | 2010-01-27 00:15:56 +0100 | [diff] [blame] | 1245 | char *id = NULL; |
Lennart Poettering | 87f0e41 | 2010-01-26 21:39:06 +0100 | [diff] [blame] | 1246 | |
| 1247 | assert(filename); |
| 1248 | assert(*filename); |
| 1249 | assert(_f); |
| 1250 | assert(names); |
| 1251 | |
Lennart Poettering | 0301abf | 2010-01-27 00:15:56 +0100 | [diff] [blame] | 1252 | /* This will update the filename pointer if the loaded file is |
| 1253 | * reached by a symlink. The old string will be freed. */ |
Lennart Poettering | 87f0e41 | 2010-01-26 21:39:06 +0100 | [diff] [blame] | 1254 | |
Lennart Poettering | 0301abf | 2010-01-27 00:15:56 +0100 | [diff] [blame] | 1255 | for (;;) { |
Lennart Poettering | 2c7108c | 2010-06-16 01:56:00 +0200 | [diff] [blame] | 1256 | char *target, *name; |
Lennart Poettering | 87f0e41 | 2010-01-26 21:39:06 +0100 | [diff] [blame] | 1257 | |
Lennart Poettering | 0301abf | 2010-01-27 00:15:56 +0100 | [diff] [blame] | 1258 | if (c++ >= FOLLOW_MAX) |
| 1259 | return -ELOOP; |
| 1260 | |
Lennart Poettering | b08d03f | 2010-01-29 02:07:41 +0100 | [diff] [blame] | 1261 | path_kill_slashes(*filename); |
| 1262 | |
Lennart Poettering | 87f0e41 | 2010-01-26 21:39:06 +0100 | [diff] [blame] | 1263 | /* Add the file name we are currently looking at to |
| 1264 | * the names of this unit */ |
Lennart Poettering | 0301abf | 2010-01-27 00:15:56 +0100 | [diff] [blame] | 1265 | name = file_name_from_path(*filename); |
| 1266 | if (!(id = set_get(names, name))) { |
Lennart Poettering | 87f0e41 | 2010-01-26 21:39:06 +0100 | [diff] [blame] | 1267 | |
Lennart Poettering | 0301abf | 2010-01-27 00:15:56 +0100 | [diff] [blame] | 1268 | if (!(id = strdup(name))) |
| 1269 | return -ENOMEM; |
| 1270 | |
| 1271 | if ((r = set_put(names, id)) < 0) { |
| 1272 | free(id); |
| 1273 | return r; |
Lennart Poettering | 87f0e41 | 2010-01-26 21:39:06 +0100 | [diff] [blame] | 1274 | } |
Lennart Poettering | 87f0e41 | 2010-01-26 21:39:06 +0100 | [diff] [blame] | 1275 | } |
| 1276 | |
Lennart Poettering | 0301abf | 2010-01-27 00:15:56 +0100 | [diff] [blame] | 1277 | /* Try to open the file name, but don't if its a symlink */ |
| 1278 | if ((fd = open(*filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW)) >= 0) |
Lennart Poettering | 87f0e41 | 2010-01-26 21:39:06 +0100 | [diff] [blame] | 1279 | break; |
| 1280 | |
Lennart Poettering | 0301abf | 2010-01-27 00:15:56 +0100 | [diff] [blame] | 1281 | if (errno != ELOOP) |
| 1282 | return -errno; |
Lennart Poettering | 87f0e41 | 2010-01-26 21:39:06 +0100 | [diff] [blame] | 1283 | |
Lennart Poettering | 0301abf | 2010-01-27 00:15:56 +0100 | [diff] [blame] | 1284 | /* Hmm, so this is a symlink. Let's read the name, and follow it manually */ |
Lennart Poettering | 2c7108c | 2010-06-16 01:56:00 +0200 | [diff] [blame] | 1285 | if ((r = readlink_and_make_absolute(*filename, &target)) < 0) |
Lennart Poettering | 0301abf | 2010-01-27 00:15:56 +0100 | [diff] [blame] | 1286 | return r; |
| 1287 | |
Lennart Poettering | 0301abf | 2010-01-27 00:15:56 +0100 | [diff] [blame] | 1288 | free(*filename); |
Lennart Poettering | 2c7108c | 2010-06-16 01:56:00 +0200 | [diff] [blame] | 1289 | *filename = target; |
Lennart Poettering | 87f0e41 | 2010-01-26 21:39:06 +0100 | [diff] [blame] | 1290 | } |
| 1291 | |
| 1292 | if (!(f = fdopen(fd, "r"))) { |
| 1293 | r = -errno; |
Lennart Poettering | 9e2f7c1 | 2010-04-15 03:11:11 +0200 | [diff] [blame] | 1294 | close_nointr_nofail(fd); |
Lennart Poettering | 0301abf | 2010-01-27 00:15:56 +0100 | [diff] [blame] | 1295 | return r; |
Lennart Poettering | 87f0e41 | 2010-01-26 21:39:06 +0100 | [diff] [blame] | 1296 | } |
| 1297 | |
| 1298 | *_f = f; |
Lennart Poettering | 9e2f7c1 | 2010-04-15 03:11:11 +0200 | [diff] [blame] | 1299 | *_final = id; |
Lennart Poettering | 0301abf | 2010-01-27 00:15:56 +0100 | [diff] [blame] | 1300 | return 0; |
Lennart Poettering | 87f0e41 | 2010-01-26 21:39:06 +0100 | [diff] [blame] | 1301 | } |
| 1302 | |
Lennart Poettering | 23a177e | 2010-04-06 02:43:58 +0200 | [diff] [blame] | 1303 | static int merge_by_names(Unit **u, Set *names, const char *id) { |
| 1304 | char *k; |
| 1305 | int r; |
| 1306 | |
| 1307 | assert(u); |
| 1308 | assert(*u); |
| 1309 | assert(names); |
| 1310 | |
| 1311 | /* Let's try to add in all symlink names we found */ |
| 1312 | while ((k = set_steal_first(names))) { |
| 1313 | |
| 1314 | /* First try to merge in the other name into our |
| 1315 | * unit */ |
| 1316 | if ((r = unit_merge_by_name(*u, k)) < 0) { |
| 1317 | Unit *other; |
| 1318 | |
| 1319 | /* Hmm, we couldn't merge the other unit into |
| 1320 | * ours? Then let's try it the other way |
| 1321 | * round */ |
| 1322 | |
| 1323 | other = manager_get_unit((*u)->meta.manager, k); |
| 1324 | free(k); |
| 1325 | |
| 1326 | if (other) |
| 1327 | if ((r = unit_merge(other, *u)) >= 0) { |
| 1328 | *u = other; |
| 1329 | return merge_by_names(u, names, NULL); |
| 1330 | } |
| 1331 | |
| 1332 | return r; |
| 1333 | } |
| 1334 | |
| 1335 | if (id == k) |
| 1336 | unit_choose_id(*u, id); |
| 1337 | |
| 1338 | free(k); |
| 1339 | } |
| 1340 | |
| 1341 | return 0; |
| 1342 | } |
| 1343 | |
Lennart Poettering | e537352 | 2010-04-10 17:53:17 +0200 | [diff] [blame] | 1344 | static void dump_items(FILE *f, const ConfigItem *items) { |
| 1345 | const ConfigItem *i; |
| 1346 | const char *prev_section = NULL; |
| 1347 | bool not_first = false; |
| 1348 | |
| 1349 | struct { |
| 1350 | ConfigParserCallback callback; |
| 1351 | const char *rvalue; |
| 1352 | } table[] = { |
| 1353 | { config_parse_int, "INTEGER" }, |
| 1354 | { config_parse_unsigned, "UNSIGNED" }, |
| 1355 | { config_parse_size, "SIZE" }, |
| 1356 | { config_parse_bool, "BOOLEAN" }, |
| 1357 | { config_parse_string, "STRING" }, |
| 1358 | { config_parse_path, "PATH" }, |
| 1359 | { config_parse_strv, "STRING [...]" }, |
| 1360 | { config_parse_nice, "NICE" }, |
| 1361 | { config_parse_oom_adjust, "OOMADJUST" }, |
| 1362 | { config_parse_io_class, "IOCLASS" }, |
| 1363 | { config_parse_io_priority, "IOPRIORITY" }, |
| 1364 | { config_parse_cpu_sched_policy, "CPUSCHEDPOLICY" }, |
| 1365 | { config_parse_cpu_sched_prio, "CPUSCHEDPRIO" }, |
| 1366 | { config_parse_cpu_affinity, "CPUAFFINITY" }, |
| 1367 | { config_parse_mode, "MODE" }, |
Lennart Poettering | ddb26e1 | 2010-06-18 06:06:24 +0200 | [diff] [blame] | 1368 | { config_parse_env_file, "FILE" }, |
Lennart Poettering | e537352 | 2010-04-10 17:53:17 +0200 | [diff] [blame] | 1369 | { config_parse_output, "OUTPUT" }, |
| 1370 | { config_parse_input, "INPUT" }, |
| 1371 | { config_parse_facility, "FACILITY" }, |
| 1372 | { config_parse_level, "LEVEL" }, |
| 1373 | { config_parse_capabilities, "CAPABILITIES" }, |
| 1374 | { config_parse_secure_bits, "SECUREBITS" }, |
| 1375 | { config_parse_bounding_set, "BOUNDINGSET" }, |
| 1376 | { config_parse_timer_slack_ns, "TIMERSLACK" }, |
| 1377 | { config_parse_limit, "LIMIT" }, |
| 1378 | { config_parse_cgroup, "CGROUP [...]" }, |
| 1379 | { config_parse_deps, "UNIT [...]" }, |
| 1380 | { config_parse_names, "UNIT [...]" }, |
| 1381 | { config_parse_exec, "PATH [ARGUMENT [...]]" }, |
| 1382 | { config_parse_service_type, "SERVICETYPE" }, |
| 1383 | { config_parse_service_restart, "SERVICERESTART" }, |
| 1384 | { config_parse_sysv_priority, "SYSVPRIORITY" }, |
| 1385 | { config_parse_kill_mode, "KILLMODE" }, |
| 1386 | { config_parse_listen, "SOCKET [...]" }, |
| 1387 | { config_parse_socket_bind, "SOCKETBIND" }, |
Lennart Poettering | 93ef5e8 | 2010-04-23 20:27:47 +0200 | [diff] [blame] | 1388 | { config_parse_bindtodevice, "NETWORKINTERFACE" }, |
| 1389 | { config_parse_usec, "SECONDS" }, |
| 1390 | { config_parse_path_strv, "PATH [...]" }, |
Lennart Poettering | 932921b | 2010-04-24 03:11:01 +0200 | [diff] [blame] | 1391 | { config_parse_mount_flags, "MOUNTFLAG [...]" }, |
Lennart Poettering | f2d3769 | 2010-06-18 23:25:19 +0200 | [diff] [blame] | 1392 | { config_parse_string_printf, "STRING" }, |
Lennart Poettering | 871d7de | 2010-05-24 01:45:54 +0200 | [diff] [blame] | 1393 | { config_parse_timer, "TIMER" }, |
| 1394 | { config_parse_timer_unit, "NAME" }, |
Lennart Poettering | c952c6e | 2010-06-18 23:12:48 +0200 | [diff] [blame] | 1395 | { config_parse_path_spec, "PATH" }, |
| 1396 | { config_parse_path_unit, "UNIT" }, |
Lennart Poettering | 8b03dae | 2010-07-01 16:34:26 +0200 | [diff] [blame] | 1397 | { config_parse_notify_access, "ACCESS" }, |
| 1398 | { config_parse_ip_tos, "TOS" }, |
Lennart Poettering | e537352 | 2010-04-10 17:53:17 +0200 | [diff] [blame] | 1399 | }; |
| 1400 | |
| 1401 | assert(f); |
| 1402 | assert(items); |
| 1403 | |
| 1404 | for (i = items; i->lvalue; i++) { |
| 1405 | unsigned j; |
| 1406 | const char *rvalue = "OTHER"; |
| 1407 | |
| 1408 | if (!streq_ptr(i->section, prev_section)) { |
| 1409 | if (!not_first) |
| 1410 | not_first = true; |
| 1411 | else |
| 1412 | fputc('\n', f); |
| 1413 | |
| 1414 | fprintf(f, "[%s]\n", i->section); |
| 1415 | prev_section = i->section; |
| 1416 | } |
| 1417 | |
| 1418 | for (j = 0; j < ELEMENTSOF(table); j++) |
| 1419 | if (i->parse == table[j].callback) { |
| 1420 | rvalue = table[j].rvalue; |
| 1421 | break; |
| 1422 | } |
| 1423 | |
| 1424 | fprintf(f, "%s=%s\n", i->lvalue, rvalue); |
| 1425 | } |
| 1426 | } |
| 1427 | |
| 1428 | static int load_from_path(Unit *u, const char *path) { |
Lennart Poettering | 87f0e41 | 2010-01-26 21:39:06 +0100 | [diff] [blame] | 1429 | |
| 1430 | static const char* const section_table[_UNIT_TYPE_MAX] = { |
| 1431 | [UNIT_SERVICE] = "Service", |
| 1432 | [UNIT_TIMER] = "Timer", |
| 1433 | [UNIT_SOCKET] = "Socket", |
| 1434 | [UNIT_TARGET] = "Target", |
| 1435 | [UNIT_DEVICE] = "Device", |
| 1436 | [UNIT_MOUNT] = "Mount", |
| 1437 | [UNIT_AUTOMOUNT] = "Automount", |
Maarten Lankhorst | 07b0b13 | 2010-05-09 18:44:11 +0200 | [diff] [blame] | 1438 | [UNIT_SNAPSHOT] = "Snapshot", |
Lennart Poettering | 01f7847 | 2010-05-24 05:25:33 +0200 | [diff] [blame] | 1439 | [UNIT_SWAP] = "Swap", |
| 1440 | [UNIT_PATH] = "Path" |
Lennart Poettering | 42f4e3c | 2010-01-19 02:56:37 +0100 | [diff] [blame] | 1441 | }; |
| 1442 | |
Lennart Poettering | 034c6ed | 2010-01-26 04:18:44 +0100 | [diff] [blame] | 1443 | #define EXEC_CONTEXT_CONFIG_ITEMS(context, section) \ |
Lennart Poettering | 9eba9da | 2010-01-29 20:46:22 +0100 | [diff] [blame] | 1444 | { "WorkingDirectory", config_parse_path, &(context).working_directory, section }, \ |
| 1445 | { "RootDirectory", config_parse_path, &(context).root_directory, section }, \ |
Lennart Poettering | f2d3769 | 2010-06-18 23:25:19 +0200 | [diff] [blame] | 1446 | { "User", config_parse_string_printf, &(context).user, section }, \ |
| 1447 | { "Group", config_parse_string_printf, &(context).group, section }, \ |
Lennart Poettering | 1c01f82 | 2010-01-27 02:16:11 +0100 | [diff] [blame] | 1448 | { "SupplementaryGroups", config_parse_strv, &(context).supplementary_groups, section }, \ |
Lennart Poettering | fb33a39 | 2010-01-28 02:53:56 +0100 | [diff] [blame] | 1449 | { "Nice", config_parse_nice, &(context), section }, \ |
| 1450 | { "OOMAdjust", config_parse_oom_adjust, &(context), section }, \ |
Lennart Poettering | 9eba9da | 2010-01-29 20:46:22 +0100 | [diff] [blame] | 1451 | { "IOSchedulingClass", config_parse_io_class, &(context), section }, \ |
Lennart Poettering | 94f0434 | 2010-01-30 01:55:42 +0100 | [diff] [blame] | 1452 | { "IOSchedulingPriority", config_parse_io_priority, &(context), section }, \ |
| 1453 | { "CPUSchedulingPolicy", config_parse_cpu_sched_policy,&(context), section }, \ |
| 1454 | { "CPUSchedulingPriority", config_parse_cpu_sched_prio, &(context), section }, \ |
Lennart Poettering | 38b4875 | 2010-02-02 12:50:04 +0100 | [diff] [blame] | 1455 | { "CPUSchedulingResetOnFork", config_parse_bool, &(context).cpu_sched_reset_on_fork, section }, \ |
Lennart Poettering | 94f0434 | 2010-01-30 01:55:42 +0100 | [diff] [blame] | 1456 | { "CPUAffinity", config_parse_cpu_affinity, &(context), section }, \ |
Lennart Poettering | b5a0699 | 2010-02-12 02:02:14 +0100 | [diff] [blame] | 1457 | { "UMask", config_parse_mode, &(context).umask, section }, \ |
Lennart Poettering | 071830f | 2010-01-28 02:06:20 +0100 | [diff] [blame] | 1458 | { "Environment", config_parse_strv, &(context).environment, section }, \ |
Lennart Poettering | ddb26e1 | 2010-06-18 06:06:24 +0200 | [diff] [blame] | 1459 | { "EnvironmentFile", config_parse_env_file, &(context).environment, section }, \ |
Lennart Poettering | 80876c2 | 2010-04-13 02:06:27 +0200 | [diff] [blame] | 1460 | { "StandardInput", config_parse_input, &(context).std_input, section }, \ |
| 1461 | { "StandardOutput", config_parse_output, &(context).std_output, section }, \ |
Lennart Poettering | a4ddf82 | 2010-06-03 03:37:12 +0200 | [diff] [blame] | 1462 | { "StandardError", config_parse_output, &(context).std_error, section }, \ |
Lennart Poettering | 80876c2 | 2010-04-13 02:06:27 +0200 | [diff] [blame] | 1463 | { "TTYPath", config_parse_path, &(context).tty_path, section }, \ |
Lennart Poettering | f2d3769 | 2010-06-18 23:25:19 +0200 | [diff] [blame] | 1464 | { "SyslogIdentifier", config_parse_string_printf, &(context).syslog_identifier, section }, \ |
Lennart Poettering | 071830f | 2010-01-28 02:06:20 +0100 | [diff] [blame] | 1465 | { "SyslogFacility", config_parse_facility, &(context).syslog_priority, section }, \ |
Lennart Poettering | 94f0434 | 2010-01-30 01:55:42 +0100 | [diff] [blame] | 1466 | { "SyslogLevel", config_parse_level, &(context).syslog_priority, section }, \ |
Lennart Poettering | 4f4a1db | 2010-05-16 01:46:35 +0200 | [diff] [blame] | 1467 | { "SyslogNoPrefix", config_parse_bool, &(context).syslog_no_prefix, section }, \ |
Lennart Poettering | 94f0434 | 2010-01-30 01:55:42 +0100 | [diff] [blame] | 1468 | { "Capabilities", config_parse_capabilities, &(context), section }, \ |
| 1469 | { "SecureBits", config_parse_secure_bits, &(context), section }, \ |
| 1470 | { "CapabilityBoundingSetDrop", config_parse_bounding_set, &(context), section }, \ |
| 1471 | { "TimerSlackNS", config_parse_timer_slack_ns, &(context), section }, \ |
| 1472 | { "LimitCPU", config_parse_limit, &(context).rlimit[RLIMIT_CPU], section }, \ |
| 1473 | { "LimitFSIZE", config_parse_limit, &(context).rlimit[RLIMIT_FSIZE], section }, \ |
| 1474 | { "LimitDATA", config_parse_limit, &(context).rlimit[RLIMIT_DATA], section }, \ |
| 1475 | { "LimitSTACK", config_parse_limit, &(context).rlimit[RLIMIT_STACK], section }, \ |
| 1476 | { "LimitCORE", config_parse_limit, &(context).rlimit[RLIMIT_CORE], section }, \ |
| 1477 | { "LimitRSS", config_parse_limit, &(context).rlimit[RLIMIT_RSS], section }, \ |
| 1478 | { "LimitNOFILE", config_parse_limit, &(context).rlimit[RLIMIT_NOFILE], section }, \ |
| 1479 | { "LimitAS", config_parse_limit, &(context).rlimit[RLIMIT_AS], section }, \ |
| 1480 | { "LimitNPROC", config_parse_limit, &(context).rlimit[RLIMIT_NPROC], section }, \ |
| 1481 | { "LimitMEMLOCK", config_parse_limit, &(context).rlimit[RLIMIT_MEMLOCK], section }, \ |
| 1482 | { "LimitLOCKS", config_parse_limit, &(context).rlimit[RLIMIT_LOCKS], section }, \ |
| 1483 | { "LimitSIGPENDING", config_parse_limit, &(context).rlimit[RLIMIT_SIGPENDING], section }, \ |
| 1484 | { "LimitMSGQUEUE", config_parse_limit, &(context).rlimit[RLIMIT_MSGQUEUE], section }, \ |
| 1485 | { "LimitNICE", config_parse_limit, &(context).rlimit[RLIMIT_NICE], section }, \ |
| 1486 | { "LimitRTPRIO", config_parse_limit, &(context).rlimit[RLIMIT_RTPRIO], section }, \ |
Lennart Poettering | 451a074 | 2010-02-12 02:00:18 +0100 | [diff] [blame] | 1487 | { "LimitRTTIME", config_parse_limit, &(context).rlimit[RLIMIT_RTTIME], section }, \ |
Lennart Poettering | 15ae422 | 2010-04-21 22:15:06 +0200 | [diff] [blame] | 1488 | { "ControlGroup", config_parse_cgroup, u, section }, \ |
| 1489 | { "ReadWriteDirectories", config_parse_path_strv, &(context).read_write_dirs, section }, \ |
| 1490 | { "ReadOnlyDirectories", config_parse_path_strv, &(context).read_only_dirs, section }, \ |
| 1491 | { "InaccessibleDirectories",config_parse_path_strv, &(context).inaccessible_dirs, section }, \ |
| 1492 | { "PrivateTmp", config_parse_bool, &(context).private_tmp, section }, \ |
Lennart Poettering | df1f0af | 2010-06-16 16:25:42 +0200 | [diff] [blame] | 1493 | { "MountFlags", config_parse_mount_flags, &(context), section }, \ |
Lennart Poettering | f2d3769 | 2010-06-18 23:25:19 +0200 | [diff] [blame] | 1494 | { "TCPWrapName", config_parse_string_printf, &(context).tcpwrap_name, section }, \ |
| 1495 | { "PAMName", config_parse_string_printf, &(context).pam_name, section } |
Lennart Poettering | 034c6ed | 2010-01-26 04:18:44 +0100 | [diff] [blame] | 1496 | |
Lennart Poettering | 3efd419 | 2009-11-19 23:13:20 +0100 | [diff] [blame] | 1497 | const ConfigItem items[] = { |
Lennart Poettering | 0947726 | 2010-04-15 23:20:17 +0200 | [diff] [blame] | 1498 | { "Names", config_parse_names, u, "Unit" }, |
Lennart Poettering | f2d3769 | 2010-06-18 23:25:19 +0200 | [diff] [blame] | 1499 | { "Description", config_parse_string_printf, &u->meta.description, "Unit" }, |
Lennart Poettering | 0947726 | 2010-04-15 23:20:17 +0200 | [diff] [blame] | 1500 | { "Requires", config_parse_deps, UINT_TO_PTR(UNIT_REQUIRES), "Unit" }, |
| 1501 | { "RequiresOverridable", config_parse_deps, UINT_TO_PTR(UNIT_REQUIRES_OVERRIDABLE), "Unit" }, |
| 1502 | { "Requisite", config_parse_deps, UINT_TO_PTR(UNIT_REQUISITE), "Unit" }, |
| 1503 | { "RequisiteOverridable", config_parse_deps, UINT_TO_PTR(UNIT_REQUISITE_OVERRIDABLE), "Unit" }, |
| 1504 | { "Wants", config_parse_deps, UINT_TO_PTR(UNIT_WANTS), "Unit" }, |
| 1505 | { "Conflicts", config_parse_deps, UINT_TO_PTR(UNIT_CONFLICTS), "Unit" }, |
| 1506 | { "Before", config_parse_deps, UINT_TO_PTR(UNIT_BEFORE), "Unit" }, |
| 1507 | { "After", config_parse_deps, UINT_TO_PTR(UNIT_AFTER), "Unit" }, |
| 1508 | { "RecursiveStop", config_parse_bool, &u->meta.recursive_stop, "Unit" }, |
| 1509 | { "StopWhenUnneeded", config_parse_bool, &u->meta.stop_when_unneeded, "Unit" }, |
Lennart Poettering | bc0f877 | 2010-05-21 23:41:54 +0200 | [diff] [blame] | 1510 | { "OnlyByDependency", config_parse_bool, &u->meta.only_by_dependency, "Unit" }, |
Lennart Poettering | a40eb73 | 2010-07-03 19:48:33 +0200 | [diff] [blame] | 1511 | { "DefaultDependencies", config_parse_bool, &u->meta.default_dependencies, "Unit" }, |
Lennart Poettering | 034c6ed | 2010-01-26 04:18:44 +0100 | [diff] [blame] | 1512 | |
Lennart Poettering | 1c01f82 | 2010-01-27 02:16:11 +0100 | [diff] [blame] | 1513 | { "PIDFile", config_parse_path, &u->service.pid_file, "Service" }, |
| 1514 | { "ExecStartPre", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START_PRE, "Service" }, |
| 1515 | { "ExecStart", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START, "Service" }, |
| 1516 | { "ExecStartPost", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START_POST, "Service" }, |
| 1517 | { "ExecReload", config_parse_exec, u->service.exec_command+SERVICE_EXEC_RELOAD, "Service" }, |
| 1518 | { "ExecStop", config_parse_exec, u->service.exec_command+SERVICE_EXEC_STOP, "Service" }, |
| 1519 | { "ExecStopPost", config_parse_exec, u->service.exec_command+SERVICE_EXEC_STOP_POST, "Service" }, |
| 1520 | { "RestartSec", config_parse_usec, &u->service.restart_usec, "Service" }, |
| 1521 | { "TimeoutSec", config_parse_usec, &u->service.timeout_usec, "Service" }, |
Lennart Poettering | 0d87eb4 | 2010-04-13 02:22:41 +0200 | [diff] [blame] | 1522 | { "Type", config_parse_service_type, &u->service.type, "Service" }, |
| 1523 | { "Restart", config_parse_service_restart, &u->service.restart, "Service" }, |
Lennart Poettering | 81a2b7c | 2010-02-14 22:43:08 +0100 | [diff] [blame] | 1524 | { "PermissionsStartOnly", config_parse_bool, &u->service.permissions_start_only, "Service" }, |
| 1525 | { "RootDirectoryStartOnly", config_parse_bool, &u->service.root_directory_start_only, "Service" }, |
Lennart Poettering | 8e27452 | 2010-03-31 16:29:55 +0200 | [diff] [blame] | 1526 | { "ValidNoProcess", config_parse_bool, &u->service.valid_no_process, "Service" }, |
Lennart Poettering | a9a1e00 | 2010-04-06 02:40:10 +0200 | [diff] [blame] | 1527 | { "SysVStartPriority", config_parse_sysv_priority, &u->service.sysv_start_priority, "Service" }, |
Lennart Poettering | 50159e6 | 2010-04-08 00:52:14 +0200 | [diff] [blame] | 1528 | { "KillMode", config_parse_kill_mode, &u->service.kill_mode, "Service" }, |
Lennart Poettering | b778d55 | 2010-04-10 17:48:41 +0200 | [diff] [blame] | 1529 | { "NonBlocking", config_parse_bool, &u->service.exec_context.non_blocking, "Service" }, |
Lennart Poettering | f2d3769 | 2010-06-18 23:25:19 +0200 | [diff] [blame] | 1530 | { "BusName", config_parse_string_printf, &u->service.bus_name, "Service" }, |
Lennart Poettering | c952c6e | 2010-06-18 23:12:48 +0200 | [diff] [blame] | 1531 | { "NotifyAccess", config_parse_notify_access, &u->service.notify_access, "Service" }, |
Lennart Poettering | 87f0e41 | 2010-01-26 21:39:06 +0100 | [diff] [blame] | 1532 | EXEC_CONTEXT_CONFIG_ITEMS(u->service.exec_context, "Service"), |
Lennart Poettering | 034c6ed | 2010-01-26 04:18:44 +0100 | [diff] [blame] | 1533 | |
Lennart Poettering | 1c01f82 | 2010-01-27 02:16:11 +0100 | [diff] [blame] | 1534 | { "ListenStream", config_parse_listen, &u->socket, "Socket" }, |
| 1535 | { "ListenDatagram", config_parse_listen, &u->socket, "Socket" }, |
| 1536 | { "ListenSequentialPacket", config_parse_listen, &u->socket, "Socket" }, |
| 1537 | { "ListenFIFO", config_parse_listen, &u->socket, "Socket" }, |
| 1538 | { "BindIPv6Only", config_parse_socket_bind, &u->socket, "Socket" }, |
| 1539 | { "Backlog", config_parse_unsigned, &u->socket.backlog, "Socket" }, |
Lennart Poettering | acbb022 | 2010-01-27 04:31:52 +0100 | [diff] [blame] | 1540 | { "BindToDevice", config_parse_bindtodevice, &u->socket, "Socket" }, |
Lennart Poettering | 1c01f82 | 2010-01-27 02:16:11 +0100 | [diff] [blame] | 1541 | { "ExecStartPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_PRE, "Socket" }, |
| 1542 | { "ExecStartPost", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_POST, "Socket" }, |
| 1543 | { "ExecStopPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_STOP_PRE, "Socket" }, |
| 1544 | { "ExecStopPost", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_STOP_POST, "Socket" }, |
Lennart Poettering | 108736d | 2010-04-10 17:50:54 +0200 | [diff] [blame] | 1545 | { "TimeoutSec", config_parse_usec, &u->socket.timeout_usec, "Socket" }, |
Lennart Poettering | b5a0699 | 2010-02-12 02:02:14 +0100 | [diff] [blame] | 1546 | { "DirectoryMode", config_parse_mode, &u->socket.directory_mode, "Socket" }, |
| 1547 | { "SocketMode", config_parse_mode, &u->socket.socket_mode, "Socket" }, |
Lennart Poettering | 50159e6 | 2010-04-08 00:52:14 +0200 | [diff] [blame] | 1548 | { "KillMode", config_parse_kill_mode, &u->socket.kill_mode, "Socket" }, |
Lennart Poettering | 4f2d528 | 2010-04-15 06:19:54 +0200 | [diff] [blame] | 1549 | { "Accept", config_parse_bool, &u->socket.accept, "Socket" }, |
Lennart Poettering | 6cf6bbc | 2010-06-19 04:25:28 +0200 | [diff] [blame] | 1550 | { "MaxConnections", config_parse_unsigned, &u->socket.max_connections, "Socket" }, |
Lennart Poettering | 4fd5948 | 2010-07-01 00:29:17 +0200 | [diff] [blame] | 1551 | { "KeepAlive", config_parse_bool, &u->socket.keep_alive, "Socket" }, |
| 1552 | { "Priority", config_parse_int, &u->socket.priority, "Socket" }, |
| 1553 | { "ReceiveBuffer", config_parse_size, &u->socket.receive_buffer, "Socket" }, |
| 1554 | { "SendBuffer", config_parse_size, &u->socket.send_buffer, "Socket" }, |
| 1555 | { "IPTOS", config_parse_ip_tos, &u->socket.ip_tos, "Socket" }, |
| 1556 | { "IPTTL", config_parse_int, &u->socket.ip_ttl, "Socket" }, |
| 1557 | { "Mark", config_parse_int, &u->socket.mark, "Socket" }, |
| 1558 | { "PipeSize", config_parse_size, &u->socket.pipe_size, "Socket" }, |
| 1559 | { "FreeBind", config_parse_bool, &u->socket.free_bind, "Socket" }, |
Lennart Poettering | 87f0e41 | 2010-01-26 21:39:06 +0100 | [diff] [blame] | 1560 | EXEC_CONTEXT_CONFIG_ITEMS(u->socket.exec_context, "Socket"), |
Lennart Poettering | 034c6ed | 2010-01-26 04:18:44 +0100 | [diff] [blame] | 1561 | |
Lennart Poettering | e537352 | 2010-04-10 17:53:17 +0200 | [diff] [blame] | 1562 | { "What", config_parse_string, &u->mount.parameters_fragment.what, "Mount" }, |
| 1563 | { "Where", config_parse_path, &u->mount.where, "Mount" }, |
| 1564 | { "Options", config_parse_string, &u->mount.parameters_fragment.options, "Mount" }, |
| 1565 | { "Type", config_parse_string, &u->mount.parameters_fragment.fstype, "Mount" }, |
| 1566 | { "TimeoutSec", config_parse_usec, &u->mount.timeout_usec, "Mount" }, |
| 1567 | { "KillMode", config_parse_kill_mode, &u->mount.kill_mode, "Mount" }, |
Lennart Poettering | 3e5235b | 2010-07-02 00:28:44 +0200 | [diff] [blame] | 1568 | { "DirectoryMode", config_parse_mode, &u->mount.directory_mode, "Mount" }, |
Lennart Poettering | e537352 | 2010-04-10 17:53:17 +0200 | [diff] [blame] | 1569 | EXEC_CONTEXT_CONFIG_ITEMS(u->mount.exec_context, "Mount"), |
Lennart Poettering | 034c6ed | 2010-01-26 04:18:44 +0100 | [diff] [blame] | 1570 | |
Lennart Poettering | 8d56758 | 2010-04-16 23:24:39 +0200 | [diff] [blame] | 1571 | { "Where", config_parse_path, &u->automount.where, "Automount" }, |
Lennart Poettering | 1cf18f2 | 2010-07-02 01:17:21 +0200 | [diff] [blame] | 1572 | { "DirectoryMode", config_parse_mode, &u->automount.directory_mode, "Automount" }, |
Lennart Poettering | 8d56758 | 2010-04-16 23:24:39 +0200 | [diff] [blame] | 1573 | |
Lennart Poettering | 01f7847 | 2010-05-24 05:25:33 +0200 | [diff] [blame] | 1574 | { "What", config_parse_path, &u->swap.parameters_fragment.what, "Swap" }, |
| 1575 | { "Priority", config_parse_int, &u->swap.parameters_fragment.priority, "Swap" }, |
Maarten Lankhorst | 07b0b13 | 2010-05-09 18:44:11 +0200 | [diff] [blame] | 1576 | |
Lennart Poettering | 01f7847 | 2010-05-24 05:25:33 +0200 | [diff] [blame] | 1577 | { "OnActive", config_parse_timer, &u->timer, "Timer" }, |
| 1578 | { "OnBoot", config_parse_timer, &u->timer, "Timer" }, |
| 1579 | { "OnStartup", config_parse_timer, &u->timer, "Timer" }, |
| 1580 | { "OnUnitActive", config_parse_timer, &u->timer, "Timer" }, |
| 1581 | { "OnUnitInactive", config_parse_timer, &u->timer, "Timer" }, |
| 1582 | { "Unit", config_parse_timer_unit, &u->timer, "Timer" }, |
| 1583 | |
| 1584 | { "PathExists", config_parse_path_spec, &u->path, "Path" }, |
| 1585 | { "PathChanged", config_parse_path_spec, &u->path, "Path" }, |
| 1586 | { "DirectoryNotEmpty", config_parse_path_spec, &u->path, "Path" }, |
| 1587 | { "Unit", config_parse_path_unit, &u->path, "Path" }, |
Lennart Poettering | 871d7de | 2010-05-24 01:45:54 +0200 | [diff] [blame] | 1588 | |
Lennart Poettering | 10e87ee | 2010-06-16 01:58:50 +0200 | [diff] [blame] | 1589 | /* The [Install] section is ignored here. */ |
| 1590 | { "Alias", NULL, NULL, "Install" }, |
| 1591 | { "WantedBy", NULL, NULL, "Install" }, |
| 1592 | { "Also", NULL, NULL, "Install" }, |
| 1593 | |
Lennart Poettering | 3efd419 | 2009-11-19 23:13:20 +0100 | [diff] [blame] | 1594 | { NULL, NULL, NULL, NULL } |
| 1595 | }; |
| 1596 | |
Lennart Poettering | 034c6ed | 2010-01-26 04:18:44 +0100 | [diff] [blame] | 1597 | #undef EXEC_CONTEXT_CONFIG_ITEMS |
Lennart Poettering | 42f4e3c | 2010-01-19 02:56:37 +0100 | [diff] [blame] | 1598 | |
Lennart Poettering | 10e87ee | 2010-06-16 01:58:50 +0200 | [diff] [blame] | 1599 | const char *sections[4]; |
Lennart Poettering | 0301abf | 2010-01-27 00:15:56 +0100 | [diff] [blame] | 1600 | int r; |
Lennart Poettering | 87f0e41 | 2010-01-26 21:39:06 +0100 | [diff] [blame] | 1601 | Set *symlink_names; |
Lennart Poettering | 23a177e | 2010-04-06 02:43:58 +0200 | [diff] [blame] | 1602 | FILE *f = NULL; |
| 1603 | char *filename = NULL, *id = NULL; |
| 1604 | Unit *merged; |
| 1605 | |
Lennart Poettering | e537352 | 2010-04-10 17:53:17 +0200 | [diff] [blame] | 1606 | if (!u) { |
| 1607 | /* Dirty dirty hack. */ |
| 1608 | dump_items((FILE*) path, items); |
| 1609 | return 0; |
| 1610 | } |
| 1611 | |
Lennart Poettering | 23a177e | 2010-04-06 02:43:58 +0200 | [diff] [blame] | 1612 | assert(u); |
Lennart Poettering | e537352 | 2010-04-10 17:53:17 +0200 | [diff] [blame] | 1613 | assert(path); |
Lennart Poettering | 3efd419 | 2009-11-19 23:13:20 +0100 | [diff] [blame] | 1614 | |
Lennart Poettering | 0947726 | 2010-04-15 23:20:17 +0200 | [diff] [blame] | 1615 | sections[0] = "Unit"; |
Lennart Poettering | 87f0e41 | 2010-01-26 21:39:06 +0100 | [diff] [blame] | 1616 | sections[1] = section_table[u->meta.type]; |
Lennart Poettering | 10e87ee | 2010-06-16 01:58:50 +0200 | [diff] [blame] | 1617 | sections[2] = "Install"; |
| 1618 | sections[3] = NULL; |
Lennart Poettering | 42f4e3c | 2010-01-19 02:56:37 +0100 | [diff] [blame] | 1619 | |
Lennart Poettering | 87f0e41 | 2010-01-26 21:39:06 +0100 | [diff] [blame] | 1620 | if (!(symlink_names = set_new(string_hash_func, string_compare_func))) |
| 1621 | return -ENOMEM; |
Lennart Poettering | 3efd419 | 2009-11-19 23:13:20 +0100 | [diff] [blame] | 1622 | |
Lennart Poettering | 036643a | 2010-02-13 01:07:02 +0100 | [diff] [blame] | 1623 | if (path_is_absolute(path)) { |
| 1624 | |
| 1625 | if (!(filename = strdup(path))) { |
| 1626 | r = -ENOMEM; |
| 1627 | goto finish; |
| 1628 | } |
| 1629 | |
| 1630 | if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) { |
| 1631 | free(filename); |
| 1632 | filename = NULL; |
| 1633 | |
| 1634 | if (r != -ENOENT) |
| 1635 | goto finish; |
| 1636 | } |
| 1637 | |
| 1638 | } else { |
| 1639 | char **p; |
| 1640 | |
Lennart Poettering | 84e3543 | 2010-06-15 14:45:15 +0200 | [diff] [blame] | 1641 | STRV_FOREACH(p, u->meta.manager->lookup_paths.unit_path) { |
Lennart Poettering | 036643a | 2010-02-13 01:07:02 +0100 | [diff] [blame] | 1642 | |
| 1643 | /* Instead of opening the path right away, we manually |
| 1644 | * follow all symlinks and add their name to our unit |
| 1645 | * name set while doing so */ |
| 1646 | if (!(filename = path_make_absolute(path, *p))) { |
| 1647 | r = -ENOMEM; |
| 1648 | goto finish; |
| 1649 | } |
| 1650 | |
| 1651 | if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) { |
| 1652 | char *sn; |
| 1653 | |
| 1654 | free(filename); |
| 1655 | filename = NULL; |
| 1656 | |
| 1657 | if (r != -ENOENT) |
| 1658 | goto finish; |
| 1659 | |
| 1660 | /* Empty the symlink names for the next run */ |
| 1661 | while ((sn = set_steal_first(symlink_names))) |
| 1662 | free(sn); |
| 1663 | |
| 1664 | continue; |
| 1665 | } |
| 1666 | |
| 1667 | break; |
| 1668 | } |
Lennart Poettering | 034c6ed | 2010-01-26 04:18:44 +0100 | [diff] [blame] | 1669 | } |
| 1670 | |
Lennart Poettering | 036643a | 2010-02-13 01:07:02 +0100 | [diff] [blame] | 1671 | if (!filename) { |
Lennart Poettering | 23a177e | 2010-04-06 02:43:58 +0200 | [diff] [blame] | 1672 | r = 0; |
| 1673 | goto finish; |
| 1674 | } |
| 1675 | |
| 1676 | merged = u; |
| 1677 | if ((r = merge_by_names(&merged, symlink_names, id)) < 0) |
| 1678 | goto finish; |
| 1679 | |
| 1680 | if (merged != u) { |
Lennart Poettering | e537352 | 2010-04-10 17:53:17 +0200 | [diff] [blame] | 1681 | u->meta.load_state = UNIT_MERGED; |
Lennart Poettering | 23a177e | 2010-04-06 02:43:58 +0200 | [diff] [blame] | 1682 | r = 0; |
Lennart Poettering | 0301abf | 2010-01-27 00:15:56 +0100 | [diff] [blame] | 1683 | goto finish; |
| 1684 | } |
| 1685 | |
| 1686 | /* Now, parse the file contents */ |
Lennart Poettering | 10e87ee | 2010-06-16 01:58:50 +0200 | [diff] [blame] | 1687 | if ((r = config_parse(filename, f, sections, items, false, u)) < 0) |
Lennart Poettering | 0301abf | 2010-01-27 00:15:56 +0100 | [diff] [blame] | 1688 | goto finish; |
| 1689 | |
Lennart Poettering | 6be1e7d | 2010-02-14 01:01:10 +0100 | [diff] [blame] | 1690 | free(u->meta.fragment_path); |
| 1691 | u->meta.fragment_path = filename; |
Lennart Poettering | 0301abf | 2010-01-27 00:15:56 +0100 | [diff] [blame] | 1692 | filename = NULL; |
| 1693 | |
Lennart Poettering | e537352 | 2010-04-10 17:53:17 +0200 | [diff] [blame] | 1694 | u->meta.load_state = UNIT_LOADED; |
Lennart Poettering | 23a177e | 2010-04-06 02:43:58 +0200 | [diff] [blame] | 1695 | r = 0; |
Lennart Poettering | 87f0e41 | 2010-01-26 21:39:06 +0100 | [diff] [blame] | 1696 | |
| 1697 | finish: |
Lennart Poettering | 53ec43c | 2010-06-15 02:45:26 +0200 | [diff] [blame] | 1698 | set_free_free(symlink_names); |
Lennart Poettering | 0301abf | 2010-01-27 00:15:56 +0100 | [diff] [blame] | 1699 | free(filename); |
| 1700 | |
Lennart Poettering | 23a177e | 2010-04-06 02:43:58 +0200 | [diff] [blame] | 1701 | if (f) |
| 1702 | fclose(f); |
| 1703 | |
Lennart Poettering | 0301abf | 2010-01-27 00:15:56 +0100 | [diff] [blame] | 1704 | return r; |
| 1705 | } |
| 1706 | |
Lennart Poettering | e537352 | 2010-04-10 17:53:17 +0200 | [diff] [blame] | 1707 | int unit_load_fragment(Unit *u) { |
Lennart Poettering | 23a177e | 2010-04-06 02:43:58 +0200 | [diff] [blame] | 1708 | int r; |
Lennart Poettering | 0301abf | 2010-01-27 00:15:56 +0100 | [diff] [blame] | 1709 | |
| 1710 | assert(u); |
Lennart Poettering | 0301abf | 2010-01-27 00:15:56 +0100 | [diff] [blame] | 1711 | |
Lennart Poettering | 23a177e | 2010-04-06 02:43:58 +0200 | [diff] [blame] | 1712 | if (u->meta.fragment_path) { |
| 1713 | |
Lennart Poettering | e537352 | 2010-04-10 17:53:17 +0200 | [diff] [blame] | 1714 | if ((r = load_from_path(u, u->meta.fragment_path)) < 0) |
Lennart Poettering | 23a177e | 2010-04-06 02:43:58 +0200 | [diff] [blame] | 1715 | return r; |
| 1716 | |
| 1717 | } else { |
Lennart Poettering | 0301abf | 2010-01-27 00:15:56 +0100 | [diff] [blame] | 1718 | Iterator i; |
Lennart Poettering | 890f434 | 2010-02-14 01:08:20 +0100 | [diff] [blame] | 1719 | const char *t; |
Lennart Poettering | 0301abf | 2010-01-27 00:15:56 +0100 | [diff] [blame] | 1720 | |
Lennart Poettering | 890f434 | 2010-02-14 01:08:20 +0100 | [diff] [blame] | 1721 | /* Try to find the unit under its id */ |
Lennart Poettering | 9e2f7c1 | 2010-04-15 03:11:11 +0200 | [diff] [blame] | 1722 | if ((r = load_from_path(u, u->meta.id)) < 0) |
| 1723 | return r; |
Lennart Poettering | 890f434 | 2010-02-14 01:08:20 +0100 | [diff] [blame] | 1724 | |
| 1725 | /* Try to find an alias we can load this with */ |
Lennart Poettering | e537352 | 2010-04-10 17:53:17 +0200 | [diff] [blame] | 1726 | if (u->meta.load_state == UNIT_STUB) |
Lennart Poettering | 23a177e | 2010-04-06 02:43:58 +0200 | [diff] [blame] | 1727 | SET_FOREACH(t, u->meta.names, i) { |
| 1728 | |
Lennart Poettering | 9e2f7c1 | 2010-04-15 03:11:11 +0200 | [diff] [blame] | 1729 | if (t == u->meta.id) |
Lennart Poettering | 23a177e | 2010-04-06 02:43:58 +0200 | [diff] [blame] | 1730 | continue; |
| 1731 | |
Lennart Poettering | e537352 | 2010-04-10 17:53:17 +0200 | [diff] [blame] | 1732 | if ((r = load_from_path(u, t)) < 0) |
Lennart Poettering | 23a177e | 2010-04-06 02:43:58 +0200 | [diff] [blame] | 1733 | return r; |
| 1734 | |
Lennart Poettering | e537352 | 2010-04-10 17:53:17 +0200 | [diff] [blame] | 1735 | if (u->meta.load_state != UNIT_STUB) |
Lennart Poettering | 890f434 | 2010-02-14 01:08:20 +0100 | [diff] [blame] | 1736 | break; |
Lennart Poettering | 23a177e | 2010-04-06 02:43:58 +0200 | [diff] [blame] | 1737 | } |
Lennart Poettering | 9e2f7c1 | 2010-04-15 03:11:11 +0200 | [diff] [blame] | 1738 | |
| 1739 | /* Now, follow the same logic, but look for a template */ |
| 1740 | if (u->meta.load_state == UNIT_STUB && u->meta.instance) { |
| 1741 | char *k; |
| 1742 | |
| 1743 | if (!(k = unit_name_template(u->meta.id))) |
| 1744 | return -ENOMEM; |
| 1745 | |
| 1746 | r = load_from_path(u, k); |
| 1747 | free(k); |
| 1748 | |
| 1749 | if (r < 0) |
| 1750 | return r; |
| 1751 | |
| 1752 | if (u->meta.load_state == UNIT_STUB) |
| 1753 | SET_FOREACH(t, u->meta.names, i) { |
| 1754 | |
| 1755 | if (t == u->meta.id) |
| 1756 | continue; |
| 1757 | |
| 1758 | if (!(k = unit_name_template(t))) |
| 1759 | return -ENOMEM; |
| 1760 | |
| 1761 | r = load_from_path(u, k); |
| 1762 | free(k); |
| 1763 | |
| 1764 | if (r < 0) |
| 1765 | return r; |
| 1766 | |
| 1767 | if (u->meta.load_state != UNIT_STUB) |
| 1768 | break; |
| 1769 | } |
| 1770 | } |
Lennart Poettering | 0301abf | 2010-01-27 00:15:56 +0100 | [diff] [blame] | 1771 | } |
Lennart Poettering | 87f0e41 | 2010-01-26 21:39:06 +0100 | [diff] [blame] | 1772 | |
Lennart Poettering | 23a177e | 2010-04-06 02:43:58 +0200 | [diff] [blame] | 1773 | return 0; |
Lennart Poettering | 3efd419 | 2009-11-19 23:13:20 +0100 | [diff] [blame] | 1774 | } |
Lennart Poettering | e537352 | 2010-04-10 17:53:17 +0200 | [diff] [blame] | 1775 | |
| 1776 | void unit_dump_config_items(FILE *f) { |
| 1777 | /* OK, this wins a prize for extreme ugliness. */ |
| 1778 | |
| 1779 | load_from_path(NULL, (const void*) f); |
| 1780 | } |