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