1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
12 #include "conf-parser.h"
13 #include "load-fragment.h"
17 static int config_parse_deps(
26 UnitDependency d = PTR_TO_UINT(data);
36 FOREACH_WORD(w, &l, rvalue, state) {
40 if (!(t = strndup(w, l)))
43 r = unit_add_dependency_by_name(u, d, t);
53 static int config_parse_names(
72 FOREACH_WORD(w, &l, rvalue, state) {
77 if (!(t = strndup(w, l)))
80 other = manager_get_unit(u->meta.manager, t);
86 if (other->meta.load_state != UNIT_STUB) {
91 if ((r = unit_merge(u, other)) < 0) {
98 if ((r = unit_add_name(u, t)) < 0) {
110 static int config_parse_listen(
111 const char *filename,
130 if (!(p = new0(SocketPort, 1)))
133 if (streq(lvalue, "ListenFIFO")) {
134 p->type = SOCKET_FIFO;
136 if (!(p->path = strdup(rvalue))) {
141 p->type = SOCKET_SOCKET;
143 if ((r = socket_address_parse(&p->address, rvalue)) < 0) {
144 log_error("[%s:%u] Failed to parse address value: %s", filename, line, rvalue);
149 if (streq(lvalue, "ListenStream"))
150 p->address.type = SOCK_STREAM;
151 else if (streq(lvalue, "ListenDatagram"))
152 p->address.type = SOCK_DGRAM;
154 assert(streq(lvalue, "ListenSequentialPacket"));
155 p->address.type = SOCK_SEQPACKET;
158 if (socket_address_family(&p->address) != AF_LOCAL && p->address.type == SOCK_SEQPACKET) {
160 return -EPROTONOSUPPORT;
165 LIST_PREPEND(SocketPort, port, s->ports, p);
170 static int config_parse_socket_bind(
171 const char *filename,
189 if ((r = parse_boolean(rvalue)) < 0) {
190 log_error("[%s:%u] Failed to parse bind IPv6 only value: %s", filename, line, rvalue);
194 s->bind_ipv6_only = r ? SOCKET_ADDRESS_IPV6_ONLY : SOCKET_ADDRESS_BOTH;
199 static int config_parse_nice(
200 const char *filename,
208 ExecContext *c = data;
216 if ((r = safe_atoi(rvalue, &priority)) < 0) {
217 log_error("[%s:%u] Failed to parse nice priority: %s", filename, line, rvalue);
221 if (priority < PRIO_MIN || priority >= PRIO_MAX) {
222 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 ExecContext *c = data;
249 if ((r = safe_atoi(rvalue, &oa)) < 0) {
250 log_error("[%s:%u] Failed to parse OOM adjust value: %s", filename, line, rvalue);
254 if (oa < OOM_DISABLE || oa > OOM_ADJUST_MAX) {
255 log_error("[%s:%u] OOM adjust value out of range: %s", filename, line, rvalue);
260 c->oom_adjust_set = true;
265 static int config_parse_umask(
266 const char *filename,
284 l = strtol(rvalue, &x, 8);
285 if (!x || *x || errno) {
286 log_error("[%s:%u] Failed to parse umask value: %s", filename, line, rvalue);
287 return errno ? -errno : -EINVAL;
290 if (l < 0000 || l > 0777) {
291 log_error("[%s:%u] umask value out of range: %s", filename, line, rvalue);
299 static int config_parse_exec(
300 const char *filename,
308 ExecCommand **e = data, *ee, *nce = NULL;
321 FOREACH_WORD_QUOTED(w, l, rvalue, state)
324 if (!(n = new(char*, k+1)))
328 FOREACH_WORD_QUOTED(w, l, rvalue, state)
329 if (!(n[k++] = strndup(w, l)))
334 if (!n[0] || !path_is_absolute(n[0])) {
335 log_error("[%s:%u] Invalid executable path in command line: %s", filename, line, rvalue);
340 if (!(nce = new0(ExecCommand, 1)))
344 if (!(nce->path = strdup(n[0])))
348 /* It's kinda important that we keep the order here */
349 LIST_FIND_TAIL(ExecCommand, command, *e, ee);
350 LIST_INSERT_AFTER(ExecCommand, command, *e, ee, nce);
366 static int config_parse_usec(
367 const char *filename,
376 unsigned long long u;
384 if ((r = safe_atollu(rvalue, &u)) < 0) {
385 log_error("[%s:%u] Failed to parse time value: %s", filename, line, rvalue);
389 /* We actually assume the user configures seconds. Later on we
390 * might choose to support suffixes for time values, to
391 * configure bigger or smaller units */
393 *usec = u * USEC_PER_SEC;
398 static int config_parse_service_type(
399 const char *filename,
414 if (streq(rvalue, "forking"))
415 s->type = SERVICE_FORKING;
416 else if (streq(rvalue, "simple"))
417 s->type = SERVICE_SIMPLE;
419 log_error("[%s:%u] Failed to parse service type: %s", filename, line, rvalue);
426 static int config_parse_service_restart(
427 const char *filename,
442 if (streq(rvalue, "once"))
443 s->restart = SERVICE_ONCE;
444 else if (streq(rvalue, "on-success"))
445 s->type = SERVICE_RESTART_ON_SUCCESS;
446 else if (streq(rvalue, "always"))
447 s->type = SERVICE_RESTART_ALWAYS;
449 log_error("[%s:%u] Failed to parse service type: %s", filename, line, rvalue);
456 int config_parse_bindtodevice(
457 const char *filename,
473 if (rvalue[0] && !streq(rvalue, "*")) {
474 if (!(n = strdup(rvalue)))
479 free(s->bind_to_device);
480 s->bind_to_device = n;
485 int config_parse_output(
486 const char *filename,
494 ExecOutput *o = data;
501 if (streq(rvalue, "syslog"))
503 else if (streq(rvalue, "null"))
505 else if (streq(rvalue, "syslog"))
507 else if (streq(rvalue, "kernel"))
510 log_error("[%s:%u] Failed to parse log output: %s", filename, line, rvalue);
517 int config_parse_facility(
518 const char *filename,
526 static const char * const table[LOG_NFACILITIES] = {
527 [LOG_FAC(LOG_KERN)] = "kern",
528 [LOG_FAC(LOG_USER)] = "user",
529 [LOG_FAC(LOG_MAIL)] = "mail",
530 [LOG_FAC(LOG_DAEMON)] = "daemon",
531 [LOG_FAC(LOG_AUTH)] = "auth",
532 [LOG_FAC(LOG_SYSLOG)] = "syslog",
533 [LOG_FAC(LOG_LPR)] = "lpr",
534 [LOG_FAC(LOG_NEWS)] = "news",
535 [LOG_FAC(LOG_UUCP)] = "uucp",
536 [LOG_FAC(LOG_CRON)] = "cron",
537 [LOG_FAC(LOG_AUTHPRIV)] = "authpriv",
538 [LOG_FAC(LOG_FTP)] = "ftp",
539 [LOG_FAC(LOG_LOCAL0)] = "local0",
540 [LOG_FAC(LOG_LOCAL1)] = "local1",
541 [LOG_FAC(LOG_LOCAL2)] = "local2",
542 [LOG_FAC(LOG_LOCAL3)] = "local3",
543 [LOG_FAC(LOG_LOCAL4)] = "local4",
544 [LOG_FAC(LOG_LOCAL5)] = "local5",
545 [LOG_FAC(LOG_LOCAL6)] = "local6",
546 [LOG_FAC(LOG_LOCAL7)] = "local7"
549 ExecOutput *o = data;
557 for (i = 0; i < (int) ELEMENTSOF(table); i++)
558 if (streq(rvalue, table[i])) {
559 *o = LOG_MAKEPRI(i, LOG_PRI(*o));
563 if (i >= (int) ELEMENTSOF(table)) {
565 /* Second try, let's see if this is a number. */
566 if (safe_atoi(rvalue, &i) >= 0 &&
568 i < (int) ELEMENTSOF(table))
569 *o = LOG_MAKEPRI(i, LOG_PRI(*o));
571 log_error("[%s:%u] Failed to parse log output: %s", filename, line, rvalue);
579 int config_parse_level(
580 const char *filename,
588 static const char * const table[LOG_DEBUG+1] = {
589 [LOG_EMERG] = "emerg",
590 [LOG_ALERT] = "alert",
593 [LOG_WARNING] = "warning",
594 [LOG_NOTICE] = "notice",
596 [LOG_DEBUG] = "debug"
599 ExecOutput *o = data;
607 for (i = 0; i < (int) ELEMENTSOF(table); i++)
608 if (streq(rvalue, table[i])) {
609 *o = LOG_MAKEPRI(LOG_FAC(*o), i);
613 if (i >= LOG_NFACILITIES) {
615 /* Second try, let's see if this is a number. */
616 if (safe_atoi(rvalue, &i) >= 0 &&
618 i < (int) ELEMENTSOF(table))
619 *o = LOG_MAKEPRI(LOG_FAC(*o), i);
621 log_error("[%s:%u] Failed to parse log output: %s", filename, line, rvalue);
629 int config_parse_io_class(
630 const char *filename,
638 static const char * const table[] = {
639 [IOPRIO_CLASS_NONE] = NULL,
640 [IOPRIO_CLASS_RT] = "realtime",
641 [IOPRIO_CLASS_BE] = "best-effort",
642 [IOPRIO_CLASS_IDLE] = "idle",
645 ExecContext *c = data;
653 for (i = 0; i < (int) ELEMENTSOF(table); i++) {
657 if (streq(rvalue, table[i])) {
658 c->ioprio = IOPRIO_PRIO_VALUE(i, IOPRIO_PRIO_DATA(c->ioprio));
663 if (i >= (int) ELEMENTSOF(table)) {
665 /* Second try, let's see if this is a number. */
666 if (safe_atoi(rvalue, &i) >= 0 &&
668 i < (int) ELEMENTSOF(table) &&
670 c->ioprio = IOPRIO_PRIO_VALUE(i, IOPRIO_PRIO_DATA(c->ioprio));
672 log_error("[%s:%u] Failed to parse io priority: %s", filename, line, rvalue);
677 c->ioprio_set = true;
682 int config_parse_io_priority(
683 const char *filename,
691 ExecContext *c = data;
699 if (safe_atoi(rvalue, &i) >= 0 &&
702 c->ioprio = IOPRIO_PRIO_VALUE(IOPRIO_PRIO_CLASS(c->ioprio), i);
704 log_error("[%s:%u] Failed to parse io priority: %s", filename, line, rvalue);
708 c->ioprio_set = true;
715 static int open_follow(char **filename, FILE **_f, Set *names, char **_id) {
726 /* This will update the filename pointer if the loaded file is
727 * reached by a symlink. The old string will be freed. */
730 char *target, *k, *name;
732 if (c++ >= FOLLOW_MAX)
735 path_kill_slashes(*filename);
737 /* Add the file name we are currently looking at to
738 * the names of this unit */
739 name = file_name_from_path(*filename);
740 if (!(id = set_get(names, name))) {
742 if (!(id = strdup(name)))
745 if ((r = set_put(names, id)) < 0) {
751 /* Try to open the file name, but don't if its a symlink */
752 if ((fd = open(*filename, O_RDONLY|O_CLOEXEC|O_NOCTTY|O_NOFOLLOW)) >= 0)
758 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
759 if ((r = readlink_malloc(*filename, &target)) < 0)
762 k = file_in_same_dir(*filename, target);
772 if (!(f = fdopen(fd, "r"))) {
774 assert(close_nointr(fd) == 0);
783 static int load_from_path(Unit *u, const char *path) {
785 static const char* const section_table[_UNIT_TYPE_MAX] = {
786 [UNIT_SERVICE] = "Service",
787 [UNIT_TIMER] = "Timer",
788 [UNIT_SOCKET] = "Socket",
789 [UNIT_TARGET] = "Target",
790 [UNIT_DEVICE] = "Device",
791 [UNIT_MOUNT] = "Mount",
792 [UNIT_AUTOMOUNT] = "Automount",
793 [UNIT_SNAPSHOT] = "Snapshot"
796 #define EXEC_CONTEXT_CONFIG_ITEMS(context, section) \
797 { "WorkingDirectory", config_parse_path, &(context).working_directory, section }, \
798 { "RootDirectory", config_parse_path, &(context).root_directory, section }, \
799 { "User", config_parse_string, &(context).user, section }, \
800 { "Group", config_parse_string, &(context).group, section }, \
801 { "SupplementaryGroups", config_parse_strv, &(context).supplementary_groups, section }, \
802 { "Nice", config_parse_nice, &(context), section }, \
803 { "OOMAdjust", config_parse_oom_adjust, &(context), section }, \
804 { "IOPriority", config_parse_io_priority, &(context), section }, \
805 { "IOSchedulingClass", config_parse_io_class, &(context), section }, \
806 { "UMask", config_parse_umask, &(context).umask, section }, \
807 { "Environment", config_parse_strv, &(context).environment, section }, \
808 { "Output", config_parse_output, &(context).output, section }, \
809 { "SyslogIdentifier", config_parse_string, &(context).syslog_identifier, section }, \
810 { "SyslogFacility", config_parse_facility, &(context).syslog_priority, section }, \
811 { "SyslogLevel", config_parse_level, &(context).syslog_priority, section }
813 const ConfigItem items[] = {
814 { "Names", config_parse_names, u, "Meta" },
815 { "Description", config_parse_string, &u->meta.description, "Meta" },
816 { "Requires", config_parse_deps, UINT_TO_PTR(UNIT_REQUIRES), "Meta" },
817 { "SoftRequires", config_parse_deps, UINT_TO_PTR(UNIT_SOFT_REQUIRES), "Meta" },
818 { "Wants", config_parse_deps, UINT_TO_PTR(UNIT_WANTS), "Meta" },
819 { "Requisite", config_parse_deps, UINT_TO_PTR(UNIT_REQUISITE), "Meta" },
820 { "SoftRequisite", config_parse_deps, UINT_TO_PTR(UNIT_SOFT_REQUISITE), "Meta" },
821 { "Conflicts", config_parse_deps, UINT_TO_PTR(UNIT_CONFLICTS), "Meta" },
822 { "Before", config_parse_deps, UINT_TO_PTR(UNIT_BEFORE), "Meta" },
823 { "After", config_parse_deps, UINT_TO_PTR(UNIT_AFTER), "Meta" },
824 { "RecursiveStop", config_parse_bool, &u->meta.recursive_stop, "Meta" },
825 { "StopWhenUnneeded", config_parse_bool, &u->meta.stop_when_unneeded, "Meta" },
827 { "PIDFile", config_parse_path, &u->service.pid_file, "Service" },
828 { "ExecStartPre", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START_PRE, "Service" },
829 { "ExecStart", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START, "Service" },
830 { "ExecStartPost", config_parse_exec, u->service.exec_command+SERVICE_EXEC_START_POST, "Service" },
831 { "ExecReload", config_parse_exec, u->service.exec_command+SERVICE_EXEC_RELOAD, "Service" },
832 { "ExecStop", config_parse_exec, u->service.exec_command+SERVICE_EXEC_STOP, "Service" },
833 { "ExecStopPost", config_parse_exec, u->service.exec_command+SERVICE_EXEC_STOP_POST, "Service" },
834 { "RestartSec", config_parse_usec, &u->service.restart_usec, "Service" },
835 { "TimeoutSec", config_parse_usec, &u->service.timeout_usec, "Service" },
836 { "Type", config_parse_service_type, &u->service, "Service" },
837 { "Restart", config_parse_service_restart, &u->service, "Service" },
838 EXEC_CONTEXT_CONFIG_ITEMS(u->service.exec_context, "Service"),
840 { "ListenStream", config_parse_listen, &u->socket, "Socket" },
841 { "ListenDatagram", config_parse_listen, &u->socket, "Socket" },
842 { "ListenSequentialPacket", config_parse_listen, &u->socket, "Socket" },
843 { "ListenFIFO", config_parse_listen, &u->socket, "Socket" },
844 { "BindIPv6Only", config_parse_socket_bind, &u->socket, "Socket" },
845 { "Backlog", config_parse_unsigned, &u->socket.backlog, "Socket" },
846 { "BindToDevice", config_parse_bindtodevice, &u->socket, "Socket" },
847 { "ExecStartPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_PRE, "Socket" },
848 { "ExecStartPost", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_START_POST, "Socket" },
849 { "ExecStopPre", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_STOP_PRE, "Socket" },
850 { "ExecStopPost", config_parse_exec, u->socket.exec_command+SOCKET_EXEC_STOP_POST, "Socket" },
851 EXEC_CONTEXT_CONFIG_ITEMS(u->socket.exec_context, "Socket"),
853 EXEC_CONTEXT_CONFIG_ITEMS(u->automount.exec_context, "Automount"),
855 { NULL, NULL, NULL, NULL }
858 #undef EXEC_CONTEXT_CONFIG_ITEMS
860 const char *sections[3];
867 sections[0] = "Meta";
868 sections[1] = section_table[u->meta.type];
871 if (!(symlink_names = set_new(string_hash_func, string_compare_func)))
874 /* Instead of opening the path right away, we manually
875 * follow all symlinks and add their name to our unit
876 * name set while doing so */
877 if (!(filename = path_make_absolute(path, unit_path()))) {
882 if ((r = open_follow(&filename, &f, symlink_names, &id)) < 0) {
884 r = 0; /* returning 0 means: no suitable config file found */
889 /* Now, parse the file contents */
890 r = config_parse(filename, f, sections, items, u);
894 /* Let's try to add in all symlink names we found */
895 while ((k = set_steal_first(symlink_names))) {
896 if ((r = unit_add_name(u, k)) < 0)
901 unit_choose_id(u, id);
906 free(u->meta.load_path);
907 u->meta.load_path = filename;
910 r = 1; /* returning 1 means: suitable config file found and loaded */
913 while ((k = set_steal_first(symlink_names)))
915 set_free(symlink_names);
921 int unit_load_fragment(Unit *u) {
926 assert(u->meta.load_state == UNIT_STUB);
928 if (u->meta.load_path)
929 r = load_from_path(u, u->meta.load_path);
934 /* Try to find a name we can load this with */
935 SET_FOREACH(t, u->meta.names, i)
936 if ((r = load_from_path(u, t)) != 0)
940 if (u->meta.type == UNIT_SOCKET)
941 c = &u->socket.exec_context;
942 else if (u->meta.type == UNIT_SERVICE)
943 c = &u->service.exec_context;
948 (c->output == EXEC_KERNEL || c->output == EXEC_SYSLOG)) {
951 /* If syslog or kernel logging is requested, make sure
952 * our own logging daemon is run first. */
954 if ((k = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_LOGGER_SOCKET)) < 0)
957 if ((k = unit_add_dependency_by_name(u, UNIT_REQUIRES, SPECIAL_LOGGER_SOCKET)) < 0)