1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
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.
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.
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/>.
22 #include <linux/oom.h>
29 #include <sys/prctl.h>
30 #include <sys/mount.h>
34 #include <sys/resource.h>
38 #include "conf-parser.h"
39 #include "load-fragment.h"
42 #include "securebits.h"
44 #include "unit-name.h"
45 #include "bus-errors.h"
47 #ifndef HAVE_SYSV_COMPAT
48 int config_parse_warn_compat(
58 log_debug("[%s:%u] Support for option %s= has been disabled at compile time and is ignored", filename, line, lvalue);
63 int config_parse_unit_deps(
73 UnitDependency d = ltype;
83 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
91 k = unit_name_printf(u, t);
97 r = unit_add_dependency_by_name(u, d, k, NULL, true);
99 log_error("[%s:%u] Failed to add dependency on %s, ignoring: %s", filename, line, k, strerror(-r));
107 int config_parse_unit_names(
108 const char *filename,
127 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
131 if (!(t = strndup(w, l)))
134 k = unit_name_printf(u, t);
140 r = unit_merge_by_name(u, k);
143 log_error("Failed to add name %s, ignoring: %s", k, strerror(-r));
154 int config_parse_unit_string_printf(
155 const char *filename,
174 if (!(k = unit_full_printf(u, rvalue)))
188 int config_parse_unit_strv_printf(
189 const char *filename,
207 k = unit_full_printf(u, rvalue);
211 r = config_parse_strv(filename, line, section, lvalue, ltype, k, data, userdata);
217 int config_parse_unit_path_printf(
218 const char *filename,
237 if (!(k = unit_full_printf(u, rvalue)))
240 if (!path_is_absolute(k)) {
241 log_error("[%s:%u] Not an absolute path: %s", filename, line, k);
246 path_kill_slashes(k);
254 int config_parse_socket_listen(
255 const char *filename,
264 SocketPort *p, *tail;
274 if (!(p = new0(SocketPort, 1)))
277 if (streq(lvalue, "ListenFIFO")) {
278 p->type = SOCKET_FIFO;
280 if (!(p->path = unit_full_printf(UNIT(s), rvalue))) {
285 path_kill_slashes(p->path);
287 } else if (streq(lvalue, "ListenSpecial")) {
288 p->type = SOCKET_SPECIAL;
290 if (!(p->path = unit_full_printf(UNIT(s), rvalue))) {
295 path_kill_slashes(p->path);
297 } else if (streq(lvalue, "ListenMessageQueue")) {
299 p->type = SOCKET_MQUEUE;
301 if (!(p->path = unit_full_printf(UNIT(s), rvalue))) {
306 path_kill_slashes(p->path);
308 } else if (streq(lvalue, "ListenNetlink")) {
312 p->type = SOCKET_SOCKET;
313 k = unit_full_printf(UNIT(s), rvalue);
314 r = socket_address_parse_netlink(&p->address, k);
318 log_error("[%s:%u] Failed to parse address value, ignoring: %s", filename, line, rvalue);
327 p->type = SOCKET_SOCKET;
328 k = unit_full_printf(UNIT(s), rvalue);
329 r = socket_address_parse(&p->address, k);
333 log_error("[%s:%u] Failed to parse address value, ignoring: %s", filename, line, rvalue);
338 if (streq(lvalue, "ListenStream"))
339 p->address.type = SOCK_STREAM;
340 else if (streq(lvalue, "ListenDatagram"))
341 p->address.type = SOCK_DGRAM;
343 assert(streq(lvalue, "ListenSequentialPacket"));
344 p->address.type = SOCK_SEQPACKET;
347 if (socket_address_family(&p->address) != AF_LOCAL && p->address.type == SOCK_SEQPACKET) {
348 log_error("[%s:%u] Address family not supported, ignoring: %s", filename, line, rvalue);
357 LIST_FIND_TAIL(SocketPort, port, s->ports, tail);
358 LIST_INSERT_AFTER(SocketPort, port, s->ports, tail, p);
360 LIST_PREPEND(SocketPort, port, s->ports, p);
365 int config_parse_socket_bind(
366 const char *filename,
376 SocketAddressBindIPv6Only b;
385 if ((b = socket_address_bind_ipv6_only_from_string(rvalue)) < 0) {
388 if ((r = parse_boolean(rvalue)) < 0) {
389 log_error("[%s:%u] Failed to parse bind IPv6 only value, ignoring: %s", filename, line, rvalue);
393 s->bind_ipv6_only = r ? SOCKET_ADDRESS_IPV6_ONLY : SOCKET_ADDRESS_BOTH;
395 s->bind_ipv6_only = b;
400 int config_parse_exec_nice(
401 const char *filename,
410 ExecContext *c = data;
418 if (safe_atoi(rvalue, &priority) < 0) {
419 log_error("[%s:%u] Failed to parse nice priority, ignoring: %s. ", filename, line, rvalue);
423 if (priority < PRIO_MIN || priority >= PRIO_MAX) {
424 log_error("[%s:%u] Nice priority out of range, ignoring: %s", filename, line, rvalue);
434 int config_parse_exec_oom_score_adjust(
435 const char *filename,
444 ExecContext *c = data;
452 if (safe_atoi(rvalue, &oa) < 0) {
453 log_error("[%s:%u] Failed to parse the OOM score adjust value, ignoring: %s", filename, line, rvalue);
457 if (oa < OOM_SCORE_ADJ_MIN || oa > OOM_SCORE_ADJ_MAX) {
458 log_error("[%s:%u] OOM score adjust value out of range, ignoring: %s", filename, line, rvalue);
462 c->oom_score_adjust = oa;
463 c->oom_score_adjust_set = true;
468 int config_parse_exec(
469 const char *filename,
478 ExecCommand **e = data, *nce;
487 /* We accept an absolute path as first argument, or
488 * alternatively an absolute prefixed with @ to allow
489 * overriding of argv[0]. */
497 bool honour_argv0 = false, ignore = false;
503 rvalue += strspn(rvalue, WHITESPACE);
508 if (rvalue[0] == '-') {
513 if (rvalue[0] == '@') {
518 if (*rvalue != '/') {
519 log_error("[%s:%u] Invalid executable path in command line, ignoring: %s", filename, line, rvalue);
524 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
525 if (strncmp(w, ";", MAX(l, 1U)) == 0)
531 if (!(n = new(char*, k + !honour_argv0)))
535 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
536 if (strncmp(w, ";", MAX(l, 1U)) == 0)
539 if (honour_argv0 && w == rvalue) {
541 if (!(path = cunescape_length(w, l)))
544 if (!(n[k++] = cunescape_length(w, l)))
552 log_error("[%s:%u] Invalid command line, ignoring: %s", filename, line, rvalue);
559 if (!(path = strdup(n[0])))
562 assert(path_is_absolute(path));
564 if (!(nce = new0(ExecCommand, 1)))
569 nce->ignore = ignore;
571 path_kill_slashes(nce->path);
573 exec_command_append_list(e, nce);
589 DEFINE_CONFIG_PARSE_ENUM(config_parse_service_type, service_type, ServiceType, "Failed to parse service type");
590 DEFINE_CONFIG_PARSE_ENUM(config_parse_service_restart, service_restart, ServiceRestart, "Failed to parse service restart specifier");
592 int config_parse_socket_bindtodevice(
593 const char *filename,
610 if (rvalue[0] && !streq(rvalue, "*")) {
611 if (!(n = strdup(rvalue)))
616 free(s->bind_to_device);
617 s->bind_to_device = n;
622 DEFINE_CONFIG_PARSE_ENUM(config_parse_output, exec_output, ExecOutput, "Failed to parse output specifier");
623 DEFINE_CONFIG_PARSE_ENUM(config_parse_input, exec_input, ExecInput, "Failed to parse input specifier");
625 int config_parse_facility(
626 const char *filename,
643 if ((x = log_facility_unshifted_from_string(rvalue)) < 0) {
644 log_error("[%s:%u] Failed to parse log facility, ignoring: %s", filename, line, rvalue);
648 *o = (x << 3) | LOG_PRI(*o);
653 int config_parse_level(
654 const char *filename,
671 if ((x = log_level_from_string(rvalue)) < 0) {
672 log_error("[%s:%u] Failed to parse log level, ignoring: %s", filename, line, rvalue);
676 *o = (*o & LOG_FACMASK) | x;
680 int config_parse_exec_io_class(
681 const char *filename,
690 ExecContext *c = data;
698 if ((x = ioprio_class_from_string(rvalue)) < 0) {
699 log_error("[%s:%u] Failed to parse IO scheduling class, ignoring: %s", filename, line, rvalue);
703 c->ioprio = IOPRIO_PRIO_VALUE(x, IOPRIO_PRIO_DATA(c->ioprio));
704 c->ioprio_set = true;
709 int config_parse_exec_io_priority(
710 const char *filename,
719 ExecContext *c = data;
727 if (safe_atoi(rvalue, &i) < 0 || i < 0 || i >= IOPRIO_BE_NR) {
728 log_error("[%s:%u] Failed to parse io priority, ignoring: %s", filename, line, rvalue);
732 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c->ioprio), i);
733 c->ioprio_set = true;
738 int config_parse_exec_cpu_sched_policy(
739 const char *filename,
749 ExecContext *c = data;
757 if ((x = sched_policy_from_string(rvalue)) < 0) {
758 log_error("[%s:%u] Failed to parse CPU scheduling policy, ignoring: %s", filename, line, rvalue);
762 c->cpu_sched_policy = x;
763 c->cpu_sched_set = true;
768 int config_parse_exec_cpu_sched_prio(
769 const char *filename,
778 ExecContext *c = data;
786 /* On Linux RR/FIFO have the same range */
787 if (safe_atoi(rvalue, &i) < 0 || i < sched_get_priority_min(SCHED_RR) || i > sched_get_priority_max(SCHED_RR)) {
788 log_error("[%s:%u] Failed to parse CPU scheduling priority, ignoring: %s", filename, line, rvalue);
792 c->cpu_sched_priority = i;
793 c->cpu_sched_set = true;
798 int config_parse_exec_cpu_affinity(
799 const char *filename,
808 ExecContext *c = data;
818 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
823 if (!(t = strndup(w, l)))
826 r = safe_atou(t, &cpu);
830 if (!(c->cpuset = cpu_set_malloc(&c->cpuset_ncpus)))
833 if (r < 0 || cpu >= c->cpuset_ncpus) {
834 log_error("[%s:%u] Failed to parse CPU affinity, ignoring: %s", filename, line, rvalue);
838 CPU_SET_S(cpu, CPU_ALLOC_SIZE(c->cpuset_ncpus), c->cpuset);
844 int config_parse_exec_capabilities(
845 const char *filename,
854 ExecContext *c = data;
862 if (!(cap = cap_from_text(rvalue))) {
866 log_error("[%s:%u] Failed to parse capabilities, ignoring: %s", filename, line, rvalue);
871 cap_free(c->capabilities);
872 c->capabilities = cap;
877 int config_parse_exec_secure_bits(
878 const char *filename,
887 ExecContext *c = data;
897 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
898 if (first_word(w, "keep-caps"))
899 c->secure_bits |= SECURE_KEEP_CAPS;
900 else if (first_word(w, "keep-caps-locked"))
901 c->secure_bits |= SECURE_KEEP_CAPS_LOCKED;
902 else if (first_word(w, "no-setuid-fixup"))
903 c->secure_bits |= SECURE_NO_SETUID_FIXUP;
904 else if (first_word(w, "no-setuid-fixup-locked"))
905 c->secure_bits |= SECURE_NO_SETUID_FIXUP_LOCKED;
906 else if (first_word(w, "noroot"))
907 c->secure_bits |= SECURE_NOROOT;
908 else if (first_word(w, "noroot-locked"))
909 c->secure_bits |= SECURE_NOROOT_LOCKED;
911 log_error("[%s:%u] Failed to parse secure bits, ignoring: %s", filename, line, rvalue);
919 int config_parse_exec_bounding_set(
920 const char *filename,
929 ExecContext *c = data;
941 if (rvalue[0] == '~') {
946 /* Note that we store this inverted internally, since the
947 * kernel wants it like this. But we actually expose it
948 * non-inverted everywhere to have a fully normalized
951 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
956 if (!(t = strndup(w, l)))
959 r = cap_from_name(t, &cap);
963 log_error("[%s:%u] Failed to parse capability bounding set, ignoring: %s", filename, line, rvalue);
967 sum |= ((uint64_t) 1ULL) << (uint64_t) cap;
971 c->capability_bounding_set_drop |= sum;
973 c->capability_bounding_set_drop |= ~sum;
978 int config_parse_exec_timer_slack_nsec(
979 const char *filename,
988 ExecContext *c = data;
996 if (safe_atolu(rvalue, &u) < 0) {
997 log_error("[%s:%u] Failed to parse time slack value, ignoring: %s", filename, line, rvalue);
1001 c->timer_slack_nsec = u;
1006 int config_parse_limit(
1007 const char *filename,
1009 const char *section,
1016 struct rlimit **rl = data;
1017 unsigned long long u;
1026 if (streq(rvalue, "infinity"))
1027 u = (unsigned long long) RLIM_INFINITY;
1028 else if (safe_atollu(rvalue, &u) < 0) {
1029 log_error("[%s:%u] Failed to parse resource value, ignoring: %s", filename, line, rvalue);
1034 if (!(*rl = new(struct rlimit, 1)))
1037 (*rl)->rlim_cur = (*rl)->rlim_max = (rlim_t) u;
1041 int config_parse_unit_cgroup(
1042 const char *filename,
1044 const char *section,
1056 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
1064 k = unit_full_printf(u, t);
1076 r = unit_add_cgroup_from_text(u, t);
1080 log_error("[%s:%u] Failed to parse cgroup value, ignoring: %s", filename, line, rvalue);
1088 #ifdef HAVE_SYSV_COMPAT
1089 int config_parse_sysv_priority(
1090 const char *filename,
1092 const char *section,
1099 int *priority = data;
1107 if (safe_atoi(rvalue, &i) < 0 || i < 0) {
1108 log_error("[%s:%u] Failed to parse SysV start priority, ignoring: %s", filename, line, rvalue);
1112 *priority = (int) i;
1117 int config_parse_fsck_passno(
1118 const char *filename,
1120 const char *section,
1135 if (safe_atoi(rvalue, &i) || i < 0) {
1136 log_error("[%s:%u] Failed to parse fsck pass number, ignoring: %s", filename, line, rvalue);
1144 DEFINE_CONFIG_PARSE_ENUM(config_parse_kill_mode, kill_mode, KillMode, "Failed to parse kill mode");
1146 int config_parse_kill_signal(
1147 const char *filename,
1149 const char *section,
1164 if ((r = signal_from_string_try_harder(rvalue)) <= 0) {
1165 log_error("[%s:%u] Failed to parse kill signal, ignoring: %s", filename, line, rvalue);
1173 int config_parse_exec_mount_flags(
1174 const char *filename,
1176 const char *section,
1183 ExecContext *c = data;
1187 unsigned long flags = 0;
1194 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
1195 if (strncmp(w, "shared", MAX(l, 6U)) == 0)
1197 else if (strncmp(w, "slave", MAX(l, 5U)) == 0)
1199 else if (strncmp(w, "private", MAX(l, 7U)) == 0)
1200 flags |= MS_PRIVATE;
1202 log_error("[%s:%u] Failed to parse mount flags, ignoring: %s", filename, line, rvalue);
1207 c->mount_flags = flags;
1211 int config_parse_timer(
1212 const char *filename,
1214 const char *section,
1231 if ((b = timer_base_from_string(lvalue)) < 0) {
1232 log_error("[%s:%u] Failed to parse timer base, ignoring: %s", filename, line, lvalue);
1236 if (parse_usec(rvalue, &u) < 0) {
1237 log_error("[%s:%u] Failed to parse timer value, ignoring: %s", filename, line, rvalue);
1241 if (!(v = new0(TimerValue, 1)))
1247 LIST_PREPEND(TimerValue, value, t->values, v);
1252 int config_parse_timer_unit(
1253 const char *filename,
1255 const char *section,
1272 dbus_error_init(&error);
1274 if (endswith(rvalue, ".timer")) {
1275 log_error("[%s:%u] Unit cannot be of type timer, ignoring: %s", filename, line, rvalue);
1279 r = manager_load_unit(t->meta.manager, rvalue, NULL, NULL, &u);
1281 log_error("[%s:%u] Failed to load unit %s, ignoring: %s", filename, line, rvalue, bus_error(&error, r));
1282 dbus_error_free(&error);
1286 unit_ref_set(&t->unit, u);
1291 int config_parse_path_spec(
1292 const char *filename,
1294 const char *section,
1310 if ((b = path_type_from_string(lvalue)) < 0) {
1311 log_error("[%s:%u] Failed to parse path type, ignoring: %s", filename, line, lvalue);
1315 if (!path_is_absolute(rvalue)) {
1316 log_error("[%s:%u] Path is not absolute, ignoring: %s", filename, line, rvalue);
1320 if (!(s = new0(PathSpec, 1)))
1323 if (!(s->path = strdup(rvalue))) {
1328 path_kill_slashes(s->path);
1333 LIST_PREPEND(PathSpec, spec, p->specs, s);
1338 int config_parse_path_unit(
1339 const char *filename,
1341 const char *section,
1358 dbus_error_init(&error);
1360 if (endswith(rvalue, ".path")) {
1361 log_error("[%s:%u] Unit cannot be of type path, ignoring: %s", filename, line, rvalue);
1365 if ((r = manager_load_unit(t->meta.manager, rvalue, NULL, &error, &u)) < 0) {
1366 log_error("[%s:%u] Failed to load unit %s, ignoring: %s", filename, line, rvalue, bus_error(&error, r));
1367 dbus_error_free(&error);
1371 unit_ref_set(&t->unit, u);
1376 int config_parse_socket_service(
1377 const char *filename,
1379 const char *section,
1395 dbus_error_init(&error);
1397 if (!endswith(rvalue, ".service")) {
1398 log_error("[%s:%u] Unit must be of type service, ignoring: %s", filename, line, rvalue);
1402 if ((r = manager_load_unit(s->meta.manager, rvalue, NULL, &error, (Unit**) &s->service)) < 0) {
1403 log_error("[%s:%u] Failed to load unit %s, ignoring: %s", filename, line, rvalue, bus_error(&error, r));
1404 dbus_error_free(&error);
1411 int config_parse_service_sockets(
1412 const char *filename,
1414 const char *section,
1431 FOREACH_WORD_QUOTED(w, l, rvalue, state) {
1438 k = unit_name_printf(UNIT(s), t);
1444 if (!endswith(k, ".socket")) {
1445 log_error("[%s:%u] Unit must be of type socket, ignoring: %s", filename, line, rvalue);
1450 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_WANTS, UNIT_AFTER, k, NULL, true);
1452 log_error("[%s:%u] Failed to add dependency on %s, ignoring: %s", filename, line, k, strerror(-r));
1454 r = unit_add_dependency_by_name(UNIT(s), UNIT_TRIGGERED_BY, k, NULL, true);
1464 int config_parse_unit_env_file(
1465 const char *filename,
1467 const char *section,
1474 char ***env = data, **k;
1483 s = unit_full_printf(u, rvalue);
1487 if (!path_is_absolute(s[0] == '-' ? s + 1 : s)) {
1488 log_error("[%s:%u] Path '%s' is not absolute, ignoring.", filename, line, s);
1493 k = strv_append(*env, s);
1504 int config_parse_ip_tos(
1505 const char *filename,
1507 const char *section,
1514 int *ip_tos = data, x;
1521 if ((x = ip_tos_from_string(rvalue)) < 0)
1522 if (safe_atoi(rvalue, &x) < 0) {
1523 log_error("[%s:%u] Failed to parse IP TOS value, ignoring: %s", filename, line, rvalue);
1531 int config_parse_unit_condition_path(
1532 const char *filename,
1534 const char *section,
1541 ConditionType cond = ltype;
1543 bool trigger, negate;
1551 trigger = rvalue[0] == '|';
1555 negate = rvalue[0] == '!';
1559 if (!path_is_absolute(rvalue)) {
1560 log_error("[%s:%u] Path in condition not absolute, ignoring: %s", filename, line, rvalue);
1564 c = condition_new(cond, rvalue, trigger, negate);
1568 LIST_PREPEND(Condition, conditions, u->meta.conditions, c);
1572 int config_parse_unit_condition_string(
1573 const char *filename,
1575 const char *section,
1582 ConditionType cond = ltype;
1584 bool trigger, negate;
1592 if ((trigger = rvalue[0] == '|'))
1595 if ((negate = rvalue[0] == '!'))
1598 if (!(c = condition_new(cond, rvalue, trigger, negate)))
1601 LIST_PREPEND(Condition, conditions, u->meta.conditions, c);
1605 int config_parse_unit_condition_null(
1606 const char *filename,
1608 const char *section,
1617 bool trigger, negate;
1625 if ((trigger = rvalue[0] == '|'))
1628 if ((negate = rvalue[0] == '!'))
1631 if ((b = parse_boolean(rvalue)) < 0) {
1632 log_error("[%s:%u] Failed to parse boolean value in condition, ignoring: %s", filename, line, rvalue);
1639 if (!(c = condition_new(CONDITION_NULL, NULL, trigger, negate)))
1642 LIST_PREPEND(Condition, conditions, u->meta.conditions, c);
1646 DEFINE_CONFIG_PARSE_ENUM(config_parse_notify_access, notify_access, NotifyAccess, "Failed to parse notify access specifier");
1648 int config_parse_unit_cgroup_attr(
1649 const char *filename,
1651 const char *section,
1667 l = strv_split_quoted(rvalue);
1671 if (strv_length(l) != 2) {
1672 log_error("[%s:%u] Failed to parse cgroup attribute value, ignoring: %s", filename, line, rvalue);
1677 r = unit_add_cgroup_attribute(u, NULL, l[0], l[1], NULL);
1681 log_error("[%s:%u] Failed to add cgroup attribute value, ignoring: %s", filename, line, rvalue);
1688 int config_parse_unit_cpu_shares(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata) {
1699 if (safe_atolu(rvalue, &ul) < 0 || ul < 1) {
1700 log_error("[%s:%u] Failed to parse CPU shares value, ignoring: %s", filename, line, rvalue);
1704 if (asprintf(&t, "%lu", ul) < 0)
1707 r = unit_add_cgroup_attribute(u, "cpu", "cpu.shares", t, NULL);
1711 log_error("[%s:%u] Failed to add cgroup attribute value, ignoring: %s", filename, line, rvalue);
1718 int config_parse_unit_memory_limit(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata) {
1729 if (parse_bytes(rvalue, &sz) < 0 || sz <= 0) {
1730 log_error("[%s:%u] Failed to parse memory limit value, ignoring: %s", filename, line, rvalue);
1734 if (asprintf(&t, "%llu", (unsigned long long) sz) < 0)
1737 r = unit_add_cgroup_attribute(u,
1739 streq(lvalue, "MemorySoftLimit") ? "memory.soft_limit_in_bytes" : "memory.limit_in_bytes",
1744 log_error("[%s:%u] Failed to add cgroup attribute value, ignoring: %s", filename, line, rvalue);
1751 static int device_map(const char *controller, const char *name, const char *value, char **ret) {
1759 l = strv_split_quoted(value);
1763 assert(strv_length(l) >= 1);
1765 if (streq(l[0], "*")) {
1767 if (asprintf(ret, "a *:*%s%s",
1768 isempty(l[1]) ? "" : " ", strempty(l[1])) < 0) {
1776 if (stat(l[0], &st) < 0) {
1777 log_warning("Couldn't stat device %s", l[0]);
1782 if (!S_ISCHR(st.st_mode) && !S_ISBLK(st.st_mode)) {
1783 log_warning("%s is not a device.", l[0]);
1788 if (asprintf(ret, "%c %u:%u%s%s",
1789 S_ISCHR(st.st_mode) ? 'c' : 'b',
1790 major(st.st_rdev), minor(st.st_rdev),
1791 isempty(l[1]) ? "" : " ", strempty(l[1])) < 0) {
1802 int config_parse_unit_device_allow(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata) {
1813 l = strv_split_quoted(rvalue);
1818 if (k < 1 || k > 2) {
1819 log_error("[%s:%u] Failed to parse device value, ignoring: %s", filename, line, rvalue);
1824 if (!streq(l[0], "*") && !path_startswith(l[0], "/dev")) {
1825 log_error("[%s:%u] Device node path not absolute, ignoring: %s", filename, line, rvalue);
1830 if (!isempty(l[1]) && !in_charset(l[1], "rwm")) {
1831 log_error("[%s:%u] Device access string invalid, ignoring: %s", filename, line, rvalue);
1837 r = unit_add_cgroup_attribute(u, "devices",
1838 streq(lvalue, "DeviceAllow") ? "devices.allow" : "devices.deny",
1839 rvalue, device_map);
1842 log_error("[%s:%u] Failed to add cgroup attribute value, ignoring: %s", filename, line, rvalue);
1849 static int blkio_map(const char *controller, const char *name, const char *value, char **ret) {
1859 l = strv_split_quoted(value);
1863 assert(strv_length(l) == 2);
1865 if (stat(l[0], &st) < 0) {
1866 log_warning("Couldn't stat device %s", l[0]);
1871 if (S_ISBLK(st.st_mode))
1873 else if (major(st.st_dev) != 0) {
1874 /* If this is not a device node then find the block
1875 * device this file is stored on */
1878 /* If this is a partition, try to get the originating
1880 block_get_whole_disk(d, &d);
1882 log_warning("%s is not a block device and file system block device cannot be determined or is not local.", l[0]);
1887 if (asprintf(ret, "%u:%u %s", major(d), minor(d), l[1]) < 0) {
1896 int config_parse_unit_blkio_weight(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata) {
1900 const char *device = NULL, *weight;
1909 l = strv_split_quoted(rvalue);
1914 if (k < 1 || k > 2) {
1915 log_error("[%s:%u] Failed to parse weight value, ignoring: %s", filename, line, rvalue);
1927 if (device && !path_is_absolute(device)) {
1928 log_error("[%s:%u] Failed to parse block device node value, ignoring: %s", filename, line, rvalue);
1933 if (safe_atolu(weight, &ul) < 0 || ul < 10 || ul > 1000) {
1934 log_error("[%s:%u] Failed to parse block IO weight value, ignoring: %s", filename, line, rvalue);
1940 r = asprintf(&t, "%s %lu", device, ul);
1942 r = asprintf(&t, "%lu", ul);
1949 r = unit_add_cgroup_attribute(u, "blkio", "blkio.weight_device", t, blkio_map);
1951 r = unit_add_cgroup_attribute(u, "blkio", "blkio.weight", t, NULL);
1955 log_error("[%s:%u] Failed to add cgroup attribute value, ignoring: %s", filename, line, rvalue);
1962 int config_parse_unit_blkio_bandwidth(const char *filename, unsigned line, const char *section, const char *lvalue, int ltype, const char *rvalue, void *data, void *userdata) {
1974 l = strv_split_quoted(rvalue);
1980 log_error("[%s:%u] Failed to parse bandwidth value, ignoring: %s", filename, line, rvalue);
1985 if (!path_is_absolute(l[0])) {
1986 log_error("[%s:%u] Failed to parse block device node value, ignoring: %s", filename, line, rvalue);
1991 if (parse_bytes(l[1], &bytes) < 0 || bytes <= 0) {
1992 log_error("[%s:%u] Failed to parse block IO bandwith value, ignoring: %s", filename, line, rvalue);
1997 r = asprintf(&t, "%s %llu", l[0], (unsigned long long) bytes);
2003 r = unit_add_cgroup_attribute(u, "blkio",
2004 streq(lvalue, "BlockIOReadBandwidth") ? "blkio.read_bps_device" : "blkio.write_bps_device",
2009 log_error("[%s:%u] Failed to add cgroup attribute value, ignoring: %s", filename, line, rvalue);
2017 #define FOLLOW_MAX 8
2019 static int open_follow(char **filename, FILE **_f, Set *names, char **_final) {
2030 /* This will update the filename pointer if the loaded file is
2031 * reached by a symlink. The old string will be freed. */
2034 char *target, *name;
2036 if (c++ >= FOLLOW_MAX)
2039 path_kill_slashes(*filename);
2041 /* Add the file name we are currently looking at to
2042 * the names of this unit, but only if it is a valid
2044 name = file_name_from_path(*filename);
2046 if (unit_name_is_valid(name, true)) {
2048 id = set_get(names, name);
2054 r = set_put(names, id);
2062 /* Try to open the file name, but don't if its a symlink */
2063 if ((fd = open(*filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW)) >= 0)
2069 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
2070 if ((r = readlink_and_make_absolute(*filename, &target)) < 0)
2077 if (!(f = fdopen(fd, "re"))) {
2079 close_nointr_nofail(fd);
2088 static int merge_by_names(Unit **u, Set *names, const char *id) {
2096 /* Let's try to add in all symlink names we found */
2097 while ((k = set_steal_first(names))) {
2099 /* First try to merge in the other name into our
2101 if ((r = unit_merge_by_name(*u, k)) < 0) {
2104 /* Hmm, we couldn't merge the other unit into
2105 * ours? Then let's try it the other way
2108 other = manager_get_unit((*u)->meta.manager, k);
2112 if ((r = unit_merge(other, *u)) >= 0) {
2114 return merge_by_names(u, names, NULL);
2121 unit_choose_id(*u, id);
2129 static int load_from_path(Unit *u, const char *path) {
2133 char *filename = NULL, *id = NULL;
2140 symlink_names = set_new(string_hash_func, string_compare_func);
2144 if (path_is_absolute(path)) {
2146 if (!(filename = strdup(path))) {
2151 if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) {
2162 STRV_FOREACH(p, u->meta.manager->lookup_paths.unit_path) {
2164 /* Instead of opening the path right away, we manually
2165 * follow all symlinks and add their name to our unit
2166 * name set while doing so */
2167 if (!(filename = path_make_absolute(path, *p))) {
2172 if (u->meta.manager->unit_path_cache &&
2173 !set_get(u->meta.manager->unit_path_cache, filename))
2176 r = open_follow(&filename, &f, symlink_names, &id);
2187 /* Empty the symlink names for the next run */
2188 while ((sn = set_steal_first(symlink_names)))
2199 /* Hmm, no suitable file found? */
2205 if ((r = merge_by_names(&merged, symlink_names, id)) < 0)
2209 u->meta.load_state = UNIT_MERGED;
2215 if (fstat(fileno(f), &st) < 0) {
2220 if (null_or_empty(&st))
2221 u->meta.load_state = UNIT_MASKED;
2223 /* Now, parse the file contents */
2224 r = config_parse(filename, f, UNIT_VTABLE(u)->sections, config_item_perf_lookup, (void*) load_fragment_gperf_lookup, false, u);
2228 u->meta.load_state = UNIT_LOADED;
2231 free(u->meta.fragment_path);
2232 u->meta.fragment_path = filename;
2235 u->meta.fragment_mtime = timespec_load(&st.st_mtim);
2240 set_free_free(symlink_names);
2249 int unit_load_fragment(Unit *u) {
2255 assert(u->meta.load_state == UNIT_STUB);
2258 /* First, try to find the unit under its id. We always look
2259 * for unit files in the default directories, to make it easy
2260 * to override things by placing things in /etc/systemd/system */
2261 if ((r = load_from_path(u, u->meta.id)) < 0)
2264 /* Try to find an alias we can load this with */
2265 if (u->meta.load_state == UNIT_STUB)
2266 SET_FOREACH(t, u->meta.names, i) {
2268 if (t == u->meta.id)
2271 if ((r = load_from_path(u, t)) < 0)
2274 if (u->meta.load_state != UNIT_STUB)
2278 /* And now, try looking for it under the suggested (originally linked) path */
2279 if (u->meta.load_state == UNIT_STUB && u->meta.fragment_path) {
2281 if ((r = load_from_path(u, u->meta.fragment_path)) < 0)
2284 if (u->meta.load_state == UNIT_STUB) {
2285 /* Hmm, this didn't work? Then let's get rid
2286 * of the fragment path stored for us, so that
2287 * we don't point to an invalid location. */
2288 free(u->meta.fragment_path);
2289 u->meta.fragment_path = NULL;
2293 /* Look for a template */
2294 if (u->meta.load_state == UNIT_STUB && u->meta.instance) {
2297 if (!(k = unit_name_template(u->meta.id)))
2300 r = load_from_path(u, k);
2306 if (u->meta.load_state == UNIT_STUB)
2307 SET_FOREACH(t, u->meta.names, i) {
2309 if (t == u->meta.id)
2312 if (!(k = unit_name_template(t)))
2315 r = load_from_path(u, k);
2321 if (u->meta.load_state != UNIT_STUB)
2329 void unit_dump_config_items(FILE *f) {
2330 static const struct {
2331 const ConfigParserCallback callback;
2334 { config_parse_int, "INTEGER" },
2335 { config_parse_unsigned, "UNSIGNED" },
2336 { config_parse_size, "SIZE" },
2337 { config_parse_bool, "BOOLEAN" },
2338 { config_parse_string, "STRING" },
2339 { config_parse_path, "PATH" },
2340 { config_parse_unit_path_printf, "PATH" },
2341 { config_parse_strv, "STRING [...]" },
2342 { config_parse_exec_nice, "NICE" },
2343 { config_parse_exec_oom_score_adjust, "OOMSCOREADJUST" },
2344 { config_parse_exec_io_class, "IOCLASS" },
2345 { config_parse_exec_io_priority, "IOPRIORITY" },
2346 { config_parse_exec_cpu_sched_policy, "CPUSCHEDPOLICY" },
2347 { config_parse_exec_cpu_sched_prio, "CPUSCHEDPRIO" },
2348 { config_parse_exec_cpu_affinity, "CPUAFFINITY" },
2349 { config_parse_mode, "MODE" },
2350 { config_parse_unit_env_file, "FILE" },
2351 { config_parse_output, "OUTPUT" },
2352 { config_parse_input, "INPUT" },
2353 { config_parse_facility, "FACILITY" },
2354 { config_parse_level, "LEVEL" },
2355 { config_parse_exec_capabilities, "CAPABILITIES" },
2356 { config_parse_exec_secure_bits, "SECUREBITS" },
2357 { config_parse_exec_bounding_set, "BOUNDINGSET" },
2358 { config_parse_exec_timer_slack_nsec, "TIMERSLACK" },
2359 { config_parse_limit, "LIMIT" },
2360 { config_parse_unit_cgroup, "CGROUP [...]" },
2361 { config_parse_unit_deps, "UNIT [...]" },
2362 { config_parse_unit_names, "UNIT [...]" },
2363 { config_parse_exec, "PATH [ARGUMENT [...]]" },
2364 { config_parse_service_type, "SERVICETYPE" },
2365 { config_parse_service_restart, "SERVICERESTART" },
2366 #ifdef HAVE_SYSV_COMPAT
2367 { config_parse_sysv_priority, "SYSVPRIORITY" },
2369 { config_parse_warn_compat, "NOTSUPPORTED" },
2371 { config_parse_kill_mode, "KILLMODE" },
2372 { config_parse_kill_signal, "SIGNAL" },
2373 { config_parse_socket_listen, "SOCKET [...]" },
2374 { config_parse_socket_bind, "SOCKETBIND" },
2375 { config_parse_socket_bindtodevice, "NETWORKINTERFACE" },
2376 { config_parse_usec, "SECONDS" },
2377 { config_parse_path_strv, "PATH [...]" },
2378 { config_parse_exec_mount_flags, "MOUNTFLAG [...]" },
2379 { config_parse_unit_string_printf, "STRING" },
2380 { config_parse_timer, "TIMER" },
2381 { config_parse_timer_unit, "NAME" },
2382 { config_parse_path_spec, "PATH" },
2383 { config_parse_path_unit, "UNIT" },
2384 { config_parse_notify_access, "ACCESS" },
2385 { config_parse_ip_tos, "TOS" },
2386 { config_parse_unit_condition_path, "CONDITION" },
2387 { config_parse_unit_condition_string, "CONDITION" },
2388 { config_parse_unit_condition_null, "CONDITION" },
2391 const char *prev = NULL;
2396 NULSTR_FOREACH(i, load_fragment_gperf_nulstr) {
2397 const char *rvalue = "OTHER", *lvalue;
2401 const ConfigPerfItem *p;
2403 assert_se(p = load_fragment_gperf_lookup(i, strlen(i)));
2405 dot = strchr(i, '.');
2406 lvalue = dot ? dot + 1 : i;
2410 if (!prev || strncmp(prev, i, prefix_len+1) != 0) {
2414 fprintf(f, "[%.*s]\n", (int) prefix_len, i);
2417 for (j = 0; j < ELEMENTSOF(table); j++)
2418 if (p->parse == table[j].callback) {
2419 rvalue = table[j].rvalue;
2423 fprintf(f, "%s=%s\n", lvalue, rvalue);