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] || n[0][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);
455 static char *build_path(const char *path, const char *filename) {
462 /* This removes the last component of path and appends
463 * filename, unless the latter is absolute anyway or the
466 if (filename[0] == '/')
467 return strdup(filename);
469 if (!(e = strrchr(path, '/')))
470 return strdup(filename);
472 k = strlen(filename);
473 if (!(r = new(char, e-path+1+k+1)))
476 memcpy(r, path, e-path+1);
477 memcpy(r+(e-path)+1, filename, k+1);
482 static int open_follow(const char **filename, FILE **_f, Set *names) {
496 for (c = 0; c < FOLLOW_MAX; c++) {
497 char *target, *k, *name;
499 /* Add the file name we are currently looking at to
500 * the names of this unit */
501 name = file_name_from_path(fn);
502 if (!set_get(names, name)) {
504 if (!(name = strdup(name))) {
509 if ((r = set_put(names, name)) < 0) {
517 /* Try to open the file name, but don' if its a symlink */
518 fd = open(fn, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW);
519 if (fd >= 0 || errno != ELOOP)
522 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
523 if ((r = readlink_malloc(fn, &target)) < 0)
526 k = build_path(fn, target);
538 if (c >= FOLLOW_MAX) {
548 if (!(f = fdopen(fd, "r"))) {
550 assert(close_nointr(fd) == 0);
563 int unit_load_fragment(Unit *u) {
565 static const char* const section_table[_UNIT_TYPE_MAX] = {
566 [UNIT_SERVICE] = "Service",
567 [UNIT_TIMER] = "Timer",
568 [UNIT_SOCKET] = "Socket",
569 [UNIT_TARGET] = "Target",
570 [UNIT_DEVICE] = "Device",
571 [UNIT_MOUNT] = "Mount",
572 [UNIT_AUTOMOUNT] = "Automount",
573 [UNIT_SNAPSHOT] = "Snapshot"
576 #define EXEC_CONTEXT_CONFIG_ITEMS(context, section) \
577 { "Directory", config_parse_path, &(context).directory, section }, \
578 { "User", config_parse_string, &(context).user, section }, \
579 { "Group", config_parse_string, &(context).group, section }, \
580 { "SupplementaryGroups", config_parse_strv, &(context).supplementary_groups, section }, \
581 { "Nice", config_parse_nice, &(context).nice, section }, \
582 { "OOMAdjust", config_parse_oom_adjust, &(context).oom_adjust, section }, \
583 { "UMask", config_parse_umask, &(context).umask, section }, \
584 { "Environment", config_parse_strv, &(context).environment, section }
586 const ConfigItem items[] = {
587 { "Names", config_parse_names, u, "Meta" },
588 { "Description", config_parse_string, &u->meta.description, "Meta" },
589 { "Requires", config_parse_deps, UINT_TO_PTR(UNIT_REQUIRES), "Meta" },
590 { "SoftRequires", config_parse_deps, UINT_TO_PTR(UNIT_SOFT_REQUIRES), "Meta" },
591 { "Wants", config_parse_deps, UINT_TO_PTR(UNIT_WANTS), "Meta" },
592 { "Requisite", config_parse_deps, UINT_TO_PTR(UNIT_REQUISITE), "Meta" },
593 { "SoftRequisite", config_parse_deps, UINT_TO_PTR(UNIT_SOFT_REQUISITE), "Meta" },
594 { "Conflicts", config_parse_deps, UINT_TO_PTR(UNIT_CONFLICTS), "Meta" },
595 { "Before", config_parse_deps, UINT_TO_PTR(UNIT_BEFORE), "Meta" },
596 { "After", config_parse_deps, UINT_TO_PTR(UNIT_AFTER), "Meta" },
598 { "PIDFile", config_parse_path, &u->service.pid_file, "Service" },
599 { "ExecStartPre", config_parse_exec, &u->service.exec_command[SERVICE_EXEC_START_PRE], "Service" },
600 { "ExecStart", config_parse_exec, &u->service.exec_command[SERVICE_EXEC_START], "Service" },
601 { "ExecStartPost", config_parse_exec, &u->service.exec_command[SERVICE_EXEC_START_POST], "Service" },
602 { "ExecReload", config_parse_exec, &u->service.exec_command[SERVICE_EXEC_RELOAD], "Service" },
603 { "ExecStop", config_parse_exec, &u->service.exec_command[SERVICE_EXEC_STOP], "Service" },
604 { "ExecStopPost", config_parse_exec, &u->service.exec_command[SERVICE_EXEC_STOP_POST], "Service" },
605 { "RestartSec", config_parse_usec, &u->service.restart_usec, "Service" },
606 { "TimeoutSec", config_parse_usec, &u->service.timeout_usec, "Service" },
607 { "Type", config_parse_service_type, &u->service, "Service" },
608 { "Restart", config_parse_service_restart, &u->service, "Service" },
609 EXEC_CONTEXT_CONFIG_ITEMS(u->service.exec_context, "Service"),
611 { "ListenStream", config_parse_listen, &u->socket, "Socket" },
612 { "ListenDatagram", config_parse_listen, &u->socket, "Socket" },
613 { "ListenSequentialPacket", config_parse_listen, &u->socket, "Socket" },
614 { "ListenFIFO", config_parse_listen, &u->socket, "Socket" },
615 { "BindIPv6Only", config_parse_socket_bind, &u->socket, "Socket" },
616 { "Backlog", config_parse_unsigned, &u->socket.backlog, "Socket" },
617 { "ExecStartPre", config_parse_exec, &u->service.exec_command[SOCKET_EXEC_START_PRE], "Socket" },
618 { "ExecStartPost", config_parse_exec, &u->service.exec_command[SOCKET_EXEC_START_POST], "Socket" },
619 { "ExecStopPre", config_parse_exec, &u->service.exec_command[SOCKET_EXEC_STOP_PRE], "Socket" },
620 { "ExecStopPost", config_parse_exec, &u->service.exec_command[SOCKET_EXEC_STOP_POST], "Socket" },
621 EXEC_CONTEXT_CONFIG_ITEMS(u->socket.exec_context, "Socket"),
623 EXEC_CONTEXT_CONFIG_ITEMS(u->automount.exec_context, "Automount"),
625 { NULL, NULL, NULL, NULL }
628 #undef EXEC_CONTEXT_CONFIG_ITEMS
632 const char *sections[3];
637 assert(u->meta.load_state == UNIT_STUB);
639 sections[0] = "Meta";
640 sections[1] = section_table[u->meta.type];
643 if (!(symlink_names = set_new(string_hash_func, string_compare_func)))
646 /* Try to find a name we can load this with */
647 SET_FOREACH(t, u->meta.names, i) {
651 /* Clear the symlink name set first */
652 while ((k = set_steal_first(symlink_names)))
655 /* Instead of opening the path right away, we manually
656 * follow all symlinks and add their name to our unit
657 * name set while doing so */
659 if ((r = open_follow((const char**) &fn, &f, symlink_names)) < 0) {
666 /* Now, parse the file contents */
667 r = config_parse(fn, f, sections, items, u);
673 /* Let's try to add in all symlink names we found */
674 while ((k = set_steal_first(symlink_names)))
675 if ((r = unit_add_name(u, k)) < 0)
678 /* Yay, we succeeded! Now let's call this our identifier */
687 while ((k = set_steal_first(symlink_names)))
690 set_free(symlink_names);