1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
12 #include "conf-parser.h"
13 #include "load-fragment.h"
16 static int config_parse_deps(
25 UnitDependency d = PTR_TO_UINT(data);
35 FOREACH_WORD(w, &l, rvalue, state) {
40 if (!(t = strndup(w, l)))
43 r = manager_load_unit(u->meta.manager, t, &other);
49 if ((r = unit_add_dependency(u, d, other)) < 0)
56 static int config_parse_names(
75 FOREACH_WORD(w, &l, rvalue, state) {
80 if (!(t = strndup(w, l)))
83 other = manager_get_unit(u->meta.manager, t);
89 if (other->meta.load_state != UNIT_STUB) {
94 if ((r = unit_merge(u, other)) < 0) {
101 if ((r = unit_add_name(u, t)) < 0) {
113 static int config_parse_listen(
114 const char *filename,
133 if (!(p = new0(SocketPort, 1)))
136 if (streq(lvalue, "ListenFIFO")) {
137 p->type = SOCKET_FIFO;
139 if (!(p->path = strdup(rvalue))) {
144 p->type = SOCKET_SOCKET;
146 if ((r = socket_address_parse(&p->address, rvalue)) < 0) {
147 log_error("[%s:%u] Failed to parse address value: %s", filename, line, rvalue);
152 if (streq(lvalue, "ListenStream"))
153 p->address.type = SOCK_STREAM;
154 else if (streq(lvalue, "ListenDatagram"))
155 p->address.type = SOCK_DGRAM;
157 assert(streq(lvalue, "ListenSequentialPacket"));
158 p->address.type = SOCK_SEQPACKET;
161 if (socket_address_family(&p->address) != AF_LOCAL && p->address.type == SOCK_SEQPACKET) {
163 return -EPROTONOSUPPORT;
168 LIST_PREPEND(SocketPort, port, s->ports, p);
173 static int config_parse_socket_bind(
174 const char *filename,
192 if ((r = parse_boolean(rvalue)) < 0) {
193 log_error("[%s:%u] Failed to parse bind IPv6 only value: %s", filename, line, rvalue);
197 s->bind_ipv6_only = r ? SOCKET_ADDRESS_IPV6_ONLY : SOCKET_ADDRESS_BOTH;
202 static int config_parse_nice(
203 const char *filename,
211 int *i = data, priority, r;
218 if ((r = safe_atoi(rvalue, &priority)) < 0) {
219 log_error("[%s:%u] Failed to parse nice priority: %s", filename, line, rvalue);
223 if (priority < PRIO_MIN || priority >= PRIO_MAX) {
224 log_error("[%s:%u] Nice priority out of range: %s", filename, line, rvalue);
232 static int config_parse_oom_adjust(
233 const char *filename,
241 int *i = data, oa, r;
248 if ((r = safe_atoi(rvalue, &oa)) < 0) {
249 log_error("[%s:%u] Failed to parse OOM adjust value: %s", filename, line, rvalue);
253 if (oa < OOM_DISABLE || oa > OOM_ADJUST_MAX) {
254 log_error("[%s:%u] OOM adjust value out of range: %s", filename, line, rvalue);
262 static int config_parse_umask(
263 const char *filename,
281 l = strtol(rvalue, &x, 8);
282 if (!x || *x || errno) {
283 log_error("[%s:%u] Failed to parse umask value: %s", filename, line, rvalue);
284 return errno ? -errno : -EINVAL;
287 if (l < 0000 || l > 0777) {
288 log_error("[%s:%u] umask value out of range: %s", filename, line, rvalue);
296 static int config_parse_exec(
297 const char *filename,
305 ExecCommand **e = data, *ee, *nce = NULL;
318 FOREACH_WORD_QUOTED(w, l, rvalue, state)
321 if (!(n = new(char*, k+1)))
325 FOREACH_WORD_QUOTED(w, l, rvalue, state)
326 if (!(n[k++] = strndup(w, l)))
331 if (!n[0] || !path_is_absolute(n[0])) {
332 log_error("[%s:%u] Invalid executable path in command line: %s", filename, line, rvalue);
337 if (!(nce = new0(ExecCommand, 1)))
341 if (!(nce->path = strdup(n[0])))
345 /* It's kinda important that we keep the order here */
346 LIST_FIND_TAIL(ExecCommand, command, *e, ee);
347 LIST_INSERT_AFTER(ExecCommand, command, *e, ee, nce);
363 static int config_parse_usec(
364 const char *filename,
373 unsigned long long u;
381 if ((r = safe_atollu(rvalue, &u)) < 0) {
382 log_error("[%s:%u] Failed to parse time value: %s", filename, line, rvalue);
386 /* We actually assume the user configures seconds. Later on we
387 * might choose to support suffixes for time values, to
388 * configure bigger or smaller units */
390 *usec = u * USEC_PER_SEC;
395 static int config_parse_service_type(
396 const char *filename,
411 if (streq(rvalue, "forking"))
412 s->type = SERVICE_FORKING;
413 else if (streq(rvalue, "simple"))
414 s->type = SERVICE_SIMPLE;
416 log_error("[%s:%u] Failed to parse service type: %s", filename, line, rvalue);
423 static int config_parse_service_restart(
424 const char *filename,
439 if (streq(rvalue, "once"))
440 s->restart = SERVICE_ONCE;
441 else if (streq(rvalue, "on-success"))
442 s->type = SERVICE_RESTART_ON_SUCCESS;
443 else if (streq(rvalue, "always"))
444 s->type = SERVICE_RESTART_ALWAYS;
446 log_error("[%s:%u] Failed to parse service type: %s", filename, line, rvalue);
453 int config_parse_bindtodevice(
454 const char *filename,
470 if (rvalue[0] && !streq(rvalue, "*")) {
471 if (!(n = strdup(rvalue)))
476 free(s->bind_to_device);
477 s->bind_to_device = n;
484 static char *build_path(const char *path, const char *filename) {
491 /* This removes the last component of path and appends
492 * filename, unless the latter is absolute anyway or the
495 if (path_is_absolute(filename))
496 return strdup(filename);
498 if (!(e = strrchr(path, '/')))
499 return strdup(filename);
501 k = strlen(filename);
502 if (!(r = new(char, e-path+1+k+1)))
505 memcpy(r, path, e-path+1);
506 memcpy(r+(e-path)+1, filename, k+1);
511 static int open_follow(char **filename, FILE **_f, Set *names, char **_id) {
522 /* This will update the filename pointer if the loaded file is
523 * reached by a symlink. The old string will be freed. */
526 char *target, *k, *name;
528 if (c++ >= FOLLOW_MAX)
531 /* Add the file name we are currently looking at to
532 * the names of this unit */
533 name = file_name_from_path(*filename);
534 if (!(id = set_get(names, name))) {
536 if (!(id = strdup(name)))
539 if ((r = set_put(names, id)) < 0) {
545 /* Try to open the file name, but don't if its a symlink */
546 if ((fd = open(*filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW)) >= 0)
552 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
553 if ((r = readlink_malloc(*filename, &target)) < 0)
556 k = build_path(*filename, target);
566 if (!(f = fdopen(fd, "r"))) {
568 assert(close_nointr(fd) == 0);
577 static int load_from_path(Unit *u, const char *path) {
579 static const char* const section_table[_UNIT_TYPE_MAX] = {
580 [UNIT_SERVICE] = "Service",
581 [UNIT_TIMER] = "Timer",
582 [UNIT_SOCKET] = "Socket",
583 [UNIT_TARGET] = "Target",
584 [UNIT_DEVICE] = "Device",
585 [UNIT_MOUNT] = "Mount",
586 [UNIT_AUTOMOUNT] = "Automount",
587 [UNIT_SNAPSHOT] = "Snapshot"
590 #define EXEC_CONTEXT_CONFIG_ITEMS(context, section) \
591 { "Directory", config_parse_path, &(context).directory, section }, \
592 { "User", config_parse_string, &(context).user, section }, \
593 { "Group", config_parse_string, &(context).group, section }, \
594 { "SupplementaryGroups", config_parse_strv, &(context).supplementary_groups, section }, \
595 { "Nice", config_parse_nice, &(context).nice, section }, \
596 { "OOMAdjust", config_parse_oom_adjust, &(context).oom_adjust, section }, \
597 { "UMask", config_parse_umask, &(context).umask, section }, \
598 { "Environment", config_parse_strv, &(context).environment, section }
600 const ConfigItem items[] = {
601 { "Names", config_parse_names, u, "Meta" },
602 { "Description", config_parse_string, &u->meta.description, "Meta" },
603 { "Requires", config_parse_deps, UINT_TO_PTR(UNIT_REQUIRES), "Meta" },
604 { "SoftRequires", config_parse_deps, UINT_TO_PTR(UNIT_SOFT_REQUIRES), "Meta" },
605 { "Wants", config_parse_deps, UINT_TO_PTR(UNIT_WANTS), "Meta" },
606 { "Requisite", config_parse_deps, UINT_TO_PTR(UNIT_REQUISITE), "Meta" },
607 { "SoftRequisite", config_parse_deps, UINT_TO_PTR(UNIT_SOFT_REQUISITE), "Meta" },
608 { "Conflicts", config_parse_deps, UINT_TO_PTR(UNIT_CONFLICTS), "Meta" },
609 { "Before", config_parse_deps, UINT_TO_PTR(UNIT_BEFORE), "Meta" },
610 { "After", config_parse_deps, UINT_TO_PTR(UNIT_AFTER), "Meta" },
612 { "PIDFile", config_parse_path, &u->service.pid_file, "Service" },
613 { "ExecStartPre", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START_PRE, "Service" },
614 { "ExecStart", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START, "Service" },
615 { "ExecStartPost", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START_POST, "Service" },
616 { "ExecReload", config_parse_exec, u->service.exec_command+SERVICE_EXEC_RELOAD, "Service" },
617 { "ExecStop", config_parse_exec, u->service.exec_command+SERVICE_EXEC_STOP, "Service" },
618 { "ExecStopPost", config_parse_exec, u->service.exec_command+SERVICE_EXEC_STOP_POST, "Service" },
619 { "RestartSec", config_parse_usec, &u->service.restart_usec, "Service" },
620 { "TimeoutSec", config_parse_usec, &u->service.timeout_usec, "Service" },
621 { "Type", config_parse_service_type, &u->service, "Service" },
622 { "Restart", config_parse_service_restart, &u->service, "Service" },
623 EXEC_CONTEXT_CONFIG_ITEMS(u->service.exec_context, "Service"),
625 { "ListenStream", config_parse_listen, &u->socket, "Socket" },
626 { "ListenDatagram", config_parse_listen, &u->socket, "Socket" },
627 { "ListenSequentialPacket", config_parse_listen, &u->socket, "Socket" },
628 { "ListenFIFO", config_parse_listen, &u->socket, "Socket" },
629 { "BindIPv6Only", config_parse_socket_bind, &u->socket, "Socket" },
630 { "Backlog", config_parse_unsigned, &u->socket.backlog, "Socket" },
631 { "BindToDevice", config_parse_bindtodevice, &u->socket, "Socket" },
632 { "ExecStartPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_PRE, "Socket" },
633 { "ExecStartPost", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_POST, "Socket" },
634 { "ExecStopPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_STOP_PRE, "Socket" },
635 { "ExecStopPost", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_STOP_POST, "Socket" },
636 EXEC_CONTEXT_CONFIG_ITEMS(u->socket.exec_context, "Socket"),
638 EXEC_CONTEXT_CONFIG_ITEMS(u->automount.exec_context, "Automount"),
640 { NULL, NULL, NULL, NULL }
643 #undef EXEC_CONTEXT_CONFIG_ITEMS
645 const char *sections[3];
652 sections[0] = "Meta";
653 sections[1] = section_table[u->meta.type];
656 if (!(symlink_names = set_new(string_hash_func, string_compare_func)))
659 /* Instead of opening the path right away, we manually
660 * follow all symlinks and add their name to our unit
661 * name set while doing so */
662 if (!(filename = path_make_absolute(path, unit_path()))) {
667 if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) {
669 r = 0; /* returning 0 means: no suitable config file found */
674 /* Now, parse the file contents */
675 r = config_parse(filename, f, sections, items, u);
679 /* Let's try to add in all symlink names we found */
680 while ((k = set_steal_first(symlink_names))) {
681 if ((r = unit_add_name(u, k)) < 0)
685 assert_se(u->meta.id = set_get(u->meta.names, k));
690 free(u->meta.load_path);
691 u->meta.load_path = filename;
694 r = 1; /* returning 1 means: suitable config file found and loaded */
697 while ((k = set_steal_first(symlink_names)))
699 set_free(symlink_names);
705 int unit_load_fragment(Unit *u) {
709 assert(u->meta.load_state == UNIT_STUB);
711 if (u->meta.load_path)
712 r = load_from_path(u, u->meta.load_path);
717 /* Try to find a name we can load this with */
718 SET_FOREACH(t, u->meta.names, i)
719 if ((r = load_from_path(u, t)) != 0)