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