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 ExecContext *c = data;
219 if ((r = safe_atoi(rvalue, &priority)) < 0) {
220 log_error("[%s:%u] Failed to parse nice priority: %s", filename, line, rvalue);
224 if (priority < PRIO_MIN || priority >= PRIO_MAX) {
225 log_error("[%s:%u] Nice priority out of range: %s", filename, line, rvalue);
235 static int config_parse_oom_adjust(
236 const char *filename,
244 ExecContext *c = data;
252 if ((r = safe_atoi(rvalue, &oa)) < 0) {
253 log_error("[%s:%u] Failed to parse OOM adjust value: %s", filename, line, rvalue);
257 if (oa < OOM_DISABLE || oa > OOM_ADJUST_MAX) {
258 log_error("[%s:%u] OOM adjust value out of range: %s", filename, line, rvalue);
263 c->oom_adjust_set = true;
268 static int config_parse_umask(
269 const char *filename,
287 l = strtol(rvalue, &x, 8);
288 if (!x || *x || errno) {
289 log_error("[%s:%u] Failed to parse umask value: %s", filename, line, rvalue);
290 return errno ? -errno : -EINVAL;
293 if (l < 0000 || l > 0777) {
294 log_error("[%s:%u] umask value out of range: %s", filename, line, rvalue);
302 static int config_parse_exec(
303 const char *filename,
311 ExecCommand **e = data, *ee, *nce = NULL;
324 FOREACH_WORD_QUOTED(w, l, rvalue, state)
327 if (!(n = new(char*, k+1)))
331 FOREACH_WORD_QUOTED(w, l, rvalue, state)
332 if (!(n[k++] = strndup(w, l)))
337 if (!n[0] || !path_is_absolute(n[0])) {
338 log_error("[%s:%u] Invalid executable path in command line: %s", filename, line, rvalue);
343 if (!(nce = new0(ExecCommand, 1)))
347 if (!(nce->path = strdup(n[0])))
351 /* It's kinda important that we keep the order here */
352 LIST_FIND_TAIL(ExecCommand, command, *e, ee);
353 LIST_INSERT_AFTER(ExecCommand, command, *e, ee, nce);
369 static int config_parse_usec(
370 const char *filename,
379 unsigned long long u;
387 if ((r = safe_atollu(rvalue, &u)) < 0) {
388 log_error("[%s:%u] Failed to parse time value: %s", filename, line, rvalue);
392 /* We actually assume the user configures seconds. Later on we
393 * might choose to support suffixes for time values, to
394 * configure bigger or smaller units */
396 *usec = u * USEC_PER_SEC;
401 static int config_parse_service_type(
402 const char *filename,
417 if (streq(rvalue, "forking"))
418 s->type = SERVICE_FORKING;
419 else if (streq(rvalue, "simple"))
420 s->type = SERVICE_SIMPLE;
422 log_error("[%s:%u] Failed to parse service type: %s", filename, line, rvalue);
429 static int config_parse_service_restart(
430 const char *filename,
445 if (streq(rvalue, "once"))
446 s->restart = SERVICE_ONCE;
447 else if (streq(rvalue, "on-success"))
448 s->type = SERVICE_RESTART_ON_SUCCESS;
449 else if (streq(rvalue, "always"))
450 s->type = SERVICE_RESTART_ALWAYS;
452 log_error("[%s:%u] Failed to parse service type: %s", filename, line, rvalue);
459 int config_parse_bindtodevice(
460 const char *filename,
476 if (rvalue[0] && !streq(rvalue, "*")) {
477 if (!(n = strdup(rvalue)))
482 free(s->bind_to_device);
483 s->bind_to_device = n;
488 int config_parse_output(
489 const char *filename,
497 ExecOutput *o = data;
504 if (streq(rvalue, "syslog"))
506 else if (streq(rvalue, "null"))
508 else if (streq(rvalue, "syslog"))
510 else if (streq(rvalue, "kernel"))
513 log_error("[%s:%u] Failed to parse log output: %s", filename, line, rvalue);
520 int config_parse_facility(
521 const char *filename,
529 static const char * const table[LOG_NFACILITIES] = {
530 [LOG_FAC(LOG_KERN)] = "kern",
531 [LOG_FAC(LOG_USER)] = "user",
532 [LOG_FAC(LOG_MAIL)] = "mail",
533 [LOG_FAC(LOG_DAEMON)] = "daemon",
534 [LOG_FAC(LOG_AUTH)] = "auth",
535 [LOG_FAC(LOG_SYSLOG)] = "syslog",
536 [LOG_FAC(LOG_LPR)] = "lpr",
537 [LOG_FAC(LOG_NEWS)] = "news",
538 [LOG_FAC(LOG_UUCP)] = "uucp",
539 [LOG_FAC(LOG_CRON)] = "cron",
540 [LOG_FAC(LOG_AUTHPRIV)] = "authpriv",
541 [LOG_FAC(LOG_FTP)] = "ftp",
542 [LOG_FAC(LOG_LOCAL0)] = "local0",
543 [LOG_FAC(LOG_LOCAL1)] = "local1",
544 [LOG_FAC(LOG_LOCAL2)] = "local2",
545 [LOG_FAC(LOG_LOCAL3)] = "local3",
546 [LOG_FAC(LOG_LOCAL4)] = "local4",
547 [LOG_FAC(LOG_LOCAL5)] = "local5",
548 [LOG_FAC(LOG_LOCAL6)] = "local6",
549 [LOG_FAC(LOG_LOCAL7)] = "local7"
552 ExecOutput *o = data;
560 for (i = 0; i < (int) ELEMENTSOF(table); i++)
561 if (streq(rvalue, table[i])) {
562 *o = LOG_MAKEPRI(i, LOG_PRI(*o));
566 if (i >= (int) ELEMENTSOF(table)) {
568 /* Second try, let's see if this is a number. */
569 if (safe_atoi(rvalue, &i) >= 0 &&
571 i < (int) ELEMENTSOF(table))
572 *o = LOG_MAKEPRI(i, LOG_PRI(*o));
574 log_error("[%s:%u] Failed to parse log output: %s", filename, line, rvalue);
582 int config_parse_level(
583 const char *filename,
591 static const char * const table[LOG_DEBUG+1] = {
592 [LOG_EMERG] = "emerg",
593 [LOG_ALERT] = "alert",
596 [LOG_WARNING] = "warning",
597 [LOG_NOTICE] = "notice",
599 [LOG_DEBUG] = "debug"
602 ExecOutput *o = data;
610 for (i = 0; i < (int) ELEMENTSOF(table); i++)
611 if (streq(rvalue, table[i])) {
612 *o = LOG_MAKEPRI(LOG_FAC(*o), i);
616 if (i >= LOG_NFACILITIES) {
618 /* Second try, let's see if this is a number. */
619 if (safe_atoi(rvalue, &i) >= 0 &&
621 i < (int) ELEMENTSOF(table))
622 *o = LOG_MAKEPRI(LOG_FAC(*o), i);
624 log_error("[%s:%u] Failed to parse log output: %s", filename, line, rvalue);
634 static int open_follow(char **filename, FILE **_f, Set *names, char **_id) {
645 /* This will update the filename pointer if the loaded file is
646 * reached by a symlink. The old string will be freed. */
649 char *target, *k, *name;
651 if (c++ >= FOLLOW_MAX)
654 /* Add the file name we are currently looking at to
655 * the names of this unit */
656 name = file_name_from_path(*filename);
657 if (!(id = set_get(names, name))) {
659 if (!(id = strdup(name)))
662 if ((r = set_put(names, id)) < 0) {
668 /* Try to open the file name, but don't if its a symlink */
669 if ((fd = open(*filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW)) >= 0)
675 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
676 if ((r = readlink_malloc(*filename, &target)) < 0)
679 k = file_in_same_dir(*filename, target);
689 if (!(f = fdopen(fd, "r"))) {
691 assert(close_nointr(fd) == 0);
700 static int load_from_path(Unit *u, const char *path) {
702 static const char* const section_table[_UNIT_TYPE_MAX] = {
703 [UNIT_SERVICE] = "Service",
704 [UNIT_TIMER] = "Timer",
705 [UNIT_SOCKET] = "Socket",
706 [UNIT_TARGET] = "Target",
707 [UNIT_DEVICE] = "Device",
708 [UNIT_MOUNT] = "Mount",
709 [UNIT_AUTOMOUNT] = "Automount",
710 [UNIT_SNAPSHOT] = "Snapshot"
713 #define EXEC_CONTEXT_CONFIG_ITEMS(context, section) \
714 { "Directory", config_parse_path, &(context).directory, section }, \
715 { "User", config_parse_string, &(context).user, section }, \
716 { "Group", config_parse_string, &(context).group, section }, \
717 { "SupplementaryGroups", config_parse_strv, &(context).supplementary_groups, section }, \
718 { "Nice", config_parse_nice, &(context), section }, \
719 { "OOMAdjust", config_parse_oom_adjust, &(context), section }, \
720 { "UMask", config_parse_umask, &(context).umask, section }, \
721 { "Environment", config_parse_strv, &(context).environment, section }, \
722 { "Output", config_parse_output, &(context).output, section }, \
723 { "SyslogIdentifier", config_parse_string, &(context).syslog_identifier, section }, \
724 { "SyslogFacility", config_parse_facility, &(context).syslog_priority, section }, \
725 { "SyslogLevel", config_parse_level, &(context).syslog_priority, section }
727 const ConfigItem items[] = {
728 { "Names", config_parse_names, u, "Meta" },
729 { "Description", config_parse_string, &u->meta.description, "Meta" },
730 { "Requires", config_parse_deps, UINT_TO_PTR(UNIT_REQUIRES), "Meta" },
731 { "SoftRequires", config_parse_deps, UINT_TO_PTR(UNIT_SOFT_REQUIRES), "Meta" },
732 { "Wants", config_parse_deps, UINT_TO_PTR(UNIT_WANTS), "Meta" },
733 { "Requisite", config_parse_deps, UINT_TO_PTR(UNIT_REQUISITE), "Meta" },
734 { "SoftRequisite", config_parse_deps, UINT_TO_PTR(UNIT_SOFT_REQUISITE), "Meta" },
735 { "Conflicts", config_parse_deps, UINT_TO_PTR(UNIT_CONFLICTS), "Meta" },
736 { "Before", config_parse_deps, UINT_TO_PTR(UNIT_BEFORE), "Meta" },
737 { "After", config_parse_deps, UINT_TO_PTR(UNIT_AFTER), "Meta" },
739 { "PIDFile", config_parse_path, &u->service.pid_file, "Service" },
740 { "ExecStartPre", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START_PRE, "Service" },
741 { "ExecStart", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START, "Service" },
742 { "ExecStartPost", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START_POST, "Service" },
743 { "ExecReload", config_parse_exec, u->service.exec_command+SERVICE_EXEC_RELOAD, "Service" },
744 { "ExecStop", config_parse_exec, u->service.exec_command+SERVICE_EXEC_STOP, "Service" },
745 { "ExecStopPost", config_parse_exec, u->service.exec_command+SERVICE_EXEC_STOP_POST, "Service" },
746 { "RestartSec", config_parse_usec, &u->service.restart_usec, "Service" },
747 { "TimeoutSec", config_parse_usec, &u->service.timeout_usec, "Service" },
748 { "Type", config_parse_service_type, &u->service, "Service" },
749 { "Restart", config_parse_service_restart, &u->service, "Service" },
750 EXEC_CONTEXT_CONFIG_ITEMS(u->service.exec_context, "Service"),
752 { "ListenStream", config_parse_listen, &u->socket, "Socket" },
753 { "ListenDatagram", config_parse_listen, &u->socket, "Socket" },
754 { "ListenSequentialPacket", config_parse_listen, &u->socket, "Socket" },
755 { "ListenFIFO", config_parse_listen, &u->socket, "Socket" },
756 { "BindIPv6Only", config_parse_socket_bind, &u->socket, "Socket" },
757 { "Backlog", config_parse_unsigned, &u->socket.backlog, "Socket" },
758 { "BindToDevice", config_parse_bindtodevice, &u->socket, "Socket" },
759 { "ExecStartPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_PRE, "Socket" },
760 { "ExecStartPost", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_POST, "Socket" },
761 { "ExecStopPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_STOP_PRE, "Socket" },
762 { "ExecStopPost", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_STOP_POST, "Socket" },
763 EXEC_CONTEXT_CONFIG_ITEMS(u->socket.exec_context, "Socket"),
765 EXEC_CONTEXT_CONFIG_ITEMS(u->automount.exec_context, "Automount"),
767 { NULL, NULL, NULL, NULL }
770 #undef EXEC_CONTEXT_CONFIG_ITEMS
772 const char *sections[3];
779 sections[0] = "Meta";
780 sections[1] = section_table[u->meta.type];
783 if (!(symlink_names = set_new(string_hash_func, string_compare_func)))
786 /* Instead of opening the path right away, we manually
787 * follow all symlinks and add their name to our unit
788 * name set while doing so */
789 if (!(filename = path_make_absolute(path, unit_path()))) {
794 if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) {
796 r = 0; /* returning 0 means: no suitable config file found */
801 /* Now, parse the file contents */
802 r = config_parse(filename, f, sections, items, u);
806 /* Let's try to add in all symlink names we found */
807 while ((k = set_steal_first(symlink_names))) {
808 if ((r = unit_add_name(u, k)) < 0)
812 assert_se(u->meta.id = set_get(u->meta.names, k));
817 free(u->meta.load_path);
818 u->meta.load_path = filename;
821 r = 1; /* returning 1 means: suitable config file found and loaded */
824 while ((k = set_steal_first(symlink_names)))
826 set_free(symlink_names);
832 int unit_load_fragment(Unit *u) {
837 assert(u->meta.load_state == UNIT_STUB);
839 if (u->meta.load_path)
840 r = load_from_path(u, u->meta.load_path);
845 /* Try to find a name we can load this with */
846 SET_FOREACH(t, u->meta.names, i)
847 if ((r = load_from_path(u, t)) != 0)
851 if (u->meta.type == UNIT_SOCKET)
852 c = &u->socket.exec_context;
853 else if (u->meta.type == UNIT_SERVICE)
854 c = &u->service.exec_context;
859 (c->output == EXEC_KERNEL || c->output == EXEC_SYSLOG)) {
862 /* If syslog or kernel logging is requested, make sure
863 * our own logging daemon is run first. */
865 if ((k = unit_add_dependency(u, UNIT_AFTER, u->meta.manager->special_units[SPECIAL_LOGGER_SOCKET])) < 0)
868 if ((k = unit_add_dependency(u, UNIT_REQUIRES, u->meta.manager->special_units[SPECIAL_LOGGER_SOCKET])) < 0)