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
) {
39 if (!(t
= strndup(w
, l
)))
42 r
= unit_add_dependency_by_name(u
, d
, t
);
52 static int config_parse_names(
71 FOREACH_WORD(w
, &l
, rvalue
, state
) {
76 if (!(t
= strndup(w
, l
)))
79 other
= manager_get_unit(u
->meta
.manager
, t
);
85 if (other
->meta
.load_state
!= UNIT_STUB
) {
90 if ((r
= unit_merge(u
, other
)) < 0) {
97 if ((r
= unit_add_name(u
, t
)) < 0) {
109 static int config_parse_listen(
110 const char *filename
,
129 if (!(p
= new0(SocketPort
, 1)))
132 if (streq(lvalue
, "ListenFIFO")) {
133 p
->type
= SOCKET_FIFO
;
135 if (!(p
->path
= strdup(rvalue
))) {
140 p
->type
= SOCKET_SOCKET
;
142 if ((r
= socket_address_parse(&p
->address
, rvalue
)) < 0) {
143 log_error("[%s:%u] Failed to parse address value: %s", filename
, line
, rvalue
);
148 if (streq(lvalue
, "ListenStream"))
149 p
->address
.type
= SOCK_STREAM
;
150 else if (streq(lvalue
, "ListenDatagram"))
151 p
->address
.type
= SOCK_DGRAM
;
153 assert(streq(lvalue
, "ListenSequentialPacket"));
154 p
->address
.type
= SOCK_SEQPACKET
;
157 if (socket_address_family(&p
->address
) != AF_LOCAL
&& p
->address
.type
== SOCK_SEQPACKET
) {
159 return -EPROTONOSUPPORT
;
164 LIST_PREPEND(SocketPort
, port
, s
->ports
, p
);
169 static int config_parse_socket_bind(
170 const char *filename
,
188 if ((r
= parse_boolean(rvalue
)) < 0) {
189 log_error("[%s:%u] Failed to parse bind IPv6 only value: %s", filename
, line
, rvalue
);
193 s
->bind_ipv6_only
= r
? SOCKET_ADDRESS_IPV6_ONLY
: SOCKET_ADDRESS_BOTH
;
198 static int config_parse_nice(
199 const char *filename
,
207 ExecContext
*c
= data
;
215 if ((r
= safe_atoi(rvalue
, &priority
)) < 0) {
216 log_error("[%s:%u] Failed to parse nice priority: %s", filename
, line
, rvalue
);
220 if (priority
< PRIO_MIN
|| priority
>= PRIO_MAX
) {
221 log_error("[%s:%u] Nice priority out of range: %s", filename
, line
, rvalue
);
231 static int config_parse_oom_adjust(
232 const char *filename
,
240 ExecContext
*c
= data
;
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
);
259 c
->oom_adjust_set
= true;
264 static int config_parse_umask(
265 const char *filename
,
283 l
= strtol(rvalue
, &x
, 8);
284 if (!x
|| *x
|| errno
) {
285 log_error("[%s:%u] Failed to parse umask value: %s", filename
, line
, rvalue
);
286 return errno
? -errno
: -EINVAL
;
289 if (l
< 0000 || l
> 0777) {
290 log_error("[%s:%u] umask value out of range: %s", filename
, line
, rvalue
);
298 static int config_parse_exec(
299 const char *filename
,
307 ExecCommand
**e
= data
, *ee
, *nce
= NULL
;
320 FOREACH_WORD_QUOTED(w
, l
, rvalue
, state
)
323 if (!(n
= new(char*, k
+1)))
327 FOREACH_WORD_QUOTED(w
, l
, rvalue
, state
)
328 if (!(n
[k
++] = strndup(w
, l
)))
333 if (!n
[0] || !path_is_absolute(n
[0])) {
334 log_error("[%s:%u] Invalid executable path in command line: %s", filename
, line
, rvalue
);
339 if (!(nce
= new0(ExecCommand
, 1)))
343 if (!(nce
->path
= strdup(n
[0])))
347 /* It's kinda important that we keep the order here */
348 LIST_FIND_TAIL(ExecCommand
, command
, *e
, ee
);
349 LIST_INSERT_AFTER(ExecCommand
, command
, *e
, ee
, nce
);
365 static int config_parse_usec(
366 const char *filename
,
375 unsigned long long u
;
383 if ((r
= safe_atollu(rvalue
, &u
)) < 0) {
384 log_error("[%s:%u] Failed to parse time value: %s", filename
, line
, rvalue
);
388 /* We actually assume the user configures seconds. Later on we
389 * might choose to support suffixes for time values, to
390 * configure bigger or smaller units */
392 *usec
= u
* USEC_PER_SEC
;
397 static int config_parse_service_type(
398 const char *filename
,
413 if (streq(rvalue
, "forking"))
414 s
->type
= SERVICE_FORKING
;
415 else if (streq(rvalue
, "simple"))
416 s
->type
= SERVICE_SIMPLE
;
418 log_error("[%s:%u] Failed to parse service type: %s", filename
, line
, rvalue
);
425 static int config_parse_service_restart(
426 const char *filename
,
441 if (streq(rvalue
, "once"))
442 s
->restart
= SERVICE_ONCE
;
443 else if (streq(rvalue
, "on-success"))
444 s
->type
= SERVICE_RESTART_ON_SUCCESS
;
445 else if (streq(rvalue
, "always"))
446 s
->type
= SERVICE_RESTART_ALWAYS
;
448 log_error("[%s:%u] Failed to parse service type: %s", filename
, line
, rvalue
);
455 int config_parse_bindtodevice(
456 const char *filename
,
472 if (rvalue
[0] && !streq(rvalue
, "*")) {
473 if (!(n
= strdup(rvalue
)))
478 free(s
->bind_to_device
);
479 s
->bind_to_device
= n
;
484 int config_parse_output(
485 const char *filename
,
493 ExecOutput
*o
= data
;
500 if (streq(rvalue
, "syslog"))
502 else if (streq(rvalue
, "null"))
504 else if (streq(rvalue
, "syslog"))
506 else if (streq(rvalue
, "kernel"))
509 log_error("[%s:%u] Failed to parse log output: %s", filename
, line
, rvalue
);
516 int config_parse_facility(
517 const char *filename
,
525 static const char * const table
[LOG_NFACILITIES
] = {
526 [LOG_FAC(LOG_KERN
)] = "kern",
527 [LOG_FAC(LOG_USER
)] = "user",
528 [LOG_FAC(LOG_MAIL
)] = "mail",
529 [LOG_FAC(LOG_DAEMON
)] = "daemon",
530 [LOG_FAC(LOG_AUTH
)] = "auth",
531 [LOG_FAC(LOG_SYSLOG
)] = "syslog",
532 [LOG_FAC(LOG_LPR
)] = "lpr",
533 [LOG_FAC(LOG_NEWS
)] = "news",
534 [LOG_FAC(LOG_UUCP
)] = "uucp",
535 [LOG_FAC(LOG_CRON
)] = "cron",
536 [LOG_FAC(LOG_AUTHPRIV
)] = "authpriv",
537 [LOG_FAC(LOG_FTP
)] = "ftp",
538 [LOG_FAC(LOG_LOCAL0
)] = "local0",
539 [LOG_FAC(LOG_LOCAL1
)] = "local1",
540 [LOG_FAC(LOG_LOCAL2
)] = "local2",
541 [LOG_FAC(LOG_LOCAL3
)] = "local3",
542 [LOG_FAC(LOG_LOCAL4
)] = "local4",
543 [LOG_FAC(LOG_LOCAL5
)] = "local5",
544 [LOG_FAC(LOG_LOCAL6
)] = "local6",
545 [LOG_FAC(LOG_LOCAL7
)] = "local7"
548 ExecOutput
*o
= data
;
556 for (i
= 0; i
< (int) ELEMENTSOF(table
); i
++)
557 if (streq(rvalue
, table
[i
])) {
558 *o
= LOG_MAKEPRI(i
, LOG_PRI(*o
));
562 if (i
>= (int) ELEMENTSOF(table
)) {
564 /* Second try, let's see if this is a number. */
565 if (safe_atoi(rvalue
, &i
) >= 0 &&
567 i
< (int) ELEMENTSOF(table
))
568 *o
= LOG_MAKEPRI(i
, LOG_PRI(*o
));
570 log_error("[%s:%u] Failed to parse log output: %s", filename
, line
, rvalue
);
578 int config_parse_level(
579 const char *filename
,
587 static const char * const table
[LOG_DEBUG
+1] = {
588 [LOG_EMERG
] = "emerg",
589 [LOG_ALERT
] = "alert",
592 [LOG_WARNING
] = "warning",
593 [LOG_NOTICE
] = "notice",
595 [LOG_DEBUG
] = "debug"
598 ExecOutput
*o
= data
;
606 for (i
= 0; i
< (int) ELEMENTSOF(table
); i
++)
607 if (streq(rvalue
, table
[i
])) {
608 *o
= LOG_MAKEPRI(LOG_FAC(*o
), i
);
612 if (i
>= LOG_NFACILITIES
) {
614 /* Second try, let's see if this is a number. */
615 if (safe_atoi(rvalue
, &i
) >= 0 &&
617 i
< (int) ELEMENTSOF(table
))
618 *o
= LOG_MAKEPRI(LOG_FAC(*o
), i
);
620 log_error("[%s:%u] Failed to parse log output: %s", filename
, line
, rvalue
);
630 static int open_follow(char **filename
, FILE **_f
, Set
*names
, char **_id
) {
641 /* This will update the filename pointer if the loaded file is
642 * reached by a symlink. The old string will be freed. */
645 char *target
, *k
, *name
;
647 if (c
++ >= FOLLOW_MAX
)
650 path_kill_slashes(*filename
);
652 /* Add the file name we are currently looking at to
653 * the names of this unit */
654 name
= file_name_from_path(*filename
);
655 if (!(id
= set_get(names
, name
))) {
657 if (!(id
= strdup(name
)))
660 if ((r
= set_put(names
, id
)) < 0) {
666 /* Try to open the file name, but don't if its a symlink */
667 if ((fd
= open(*filename
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
)) >= 0)
673 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
674 if ((r
= readlink_malloc(*filename
, &target
)) < 0)
677 k
= file_in_same_dir(*filename
, target
);
687 if (!(f
= fdopen(fd
, "r"))) {
689 assert(close_nointr(fd
) == 0);
698 static int load_from_path(Unit
*u
, const char *path
) {
700 static const char* const section_table
[_UNIT_TYPE_MAX
] = {
701 [UNIT_SERVICE
] = "Service",
702 [UNIT_TIMER
] = "Timer",
703 [UNIT_SOCKET
] = "Socket",
704 [UNIT_TARGET
] = "Target",
705 [UNIT_DEVICE
] = "Device",
706 [UNIT_MOUNT
] = "Mount",
707 [UNIT_AUTOMOUNT
] = "Automount",
708 [UNIT_SNAPSHOT
] = "Snapshot"
711 #define EXEC_CONTEXT_CONFIG_ITEMS(context, section) \
712 { "Directory", config_parse_path, &(context).directory, section }, \
713 { "User", config_parse_string, &(context).user, section }, \
714 { "Group", config_parse_string, &(context).group, section }, \
715 { "SupplementaryGroups", config_parse_strv, &(context).supplementary_groups, section }, \
716 { "Nice", config_parse_nice, &(context), section }, \
717 { "OOMAdjust", config_parse_oom_adjust, &(context), section }, \
718 { "UMask", config_parse_umask, &(context).umask, section }, \
719 { "Environment", config_parse_strv, &(context).environment, section }, \
720 { "Output", config_parse_output, &(context).output, section }, \
721 { "SyslogIdentifier", config_parse_string, &(context).syslog_identifier, section }, \
722 { "SyslogFacility", config_parse_facility, &(context).syslog_priority, section }, \
723 { "SyslogLevel", config_parse_level, &(context).syslog_priority, section }
725 const ConfigItem items
[] = {
726 { "Names", config_parse_names
, u
, "Meta" },
727 { "Description", config_parse_string
, &u
->meta
.description
, "Meta" },
728 { "Requires", config_parse_deps
, UINT_TO_PTR(UNIT_REQUIRES
), "Meta" },
729 { "SoftRequires", config_parse_deps
, UINT_TO_PTR(UNIT_SOFT_REQUIRES
), "Meta" },
730 { "Wants", config_parse_deps
, UINT_TO_PTR(UNIT_WANTS
), "Meta" },
731 { "Requisite", config_parse_deps
, UINT_TO_PTR(UNIT_REQUISITE
), "Meta" },
732 { "SoftRequisite", config_parse_deps
, UINT_TO_PTR(UNIT_SOFT_REQUISITE
), "Meta" },
733 { "Conflicts", config_parse_deps
, UINT_TO_PTR(UNIT_CONFLICTS
), "Meta" },
734 { "Before", config_parse_deps
, UINT_TO_PTR(UNIT_BEFORE
), "Meta" },
735 { "After", config_parse_deps
, UINT_TO_PTR(UNIT_AFTER
), "Meta" },
737 { "PIDFile", config_parse_path
, &u
->service
.pid_file
, "Service" },
738 { "ExecStartPre", config_parse_exec
, u
->service
.exec_command
+SERVICE_EXEC_START_PRE
, "Service" },
739 { "ExecStart", config_parse_exec
, u
->service
.exec_command
+SERVICE_EXEC_START
, "Service" },
740 { "ExecStartPost", config_parse_exec
, u
->service
.exec_command
+SERVICE_EXEC_START_POST
, "Service" },
741 { "ExecReload", config_parse_exec
, u
->service
.exec_command
+SERVICE_EXEC_RELOAD
, "Service" },
742 { "ExecStop", config_parse_exec
, u
->service
.exec_command
+SERVICE_EXEC_STOP
, "Service" },
743 { "ExecStopPost", config_parse_exec
, u
->service
.exec_command
+SERVICE_EXEC_STOP_POST
, "Service" },
744 { "RestartSec", config_parse_usec
, &u
->service
.restart_usec
, "Service" },
745 { "TimeoutSec", config_parse_usec
, &u
->service
.timeout_usec
, "Service" },
746 { "Type", config_parse_service_type
, &u
->service
, "Service" },
747 { "Restart", config_parse_service_restart
, &u
->service
, "Service" },
748 EXEC_CONTEXT_CONFIG_ITEMS(u
->service
.exec_context
, "Service"),
750 { "ListenStream", config_parse_listen
, &u
->socket
, "Socket" },
751 { "ListenDatagram", config_parse_listen
, &u
->socket
, "Socket" },
752 { "ListenSequentialPacket", config_parse_listen
, &u
->socket
, "Socket" },
753 { "ListenFIFO", config_parse_listen
, &u
->socket
, "Socket" },
754 { "BindIPv6Only", config_parse_socket_bind
, &u
->socket
, "Socket" },
755 { "Backlog", config_parse_unsigned
, &u
->socket
.backlog
, "Socket" },
756 { "BindToDevice", config_parse_bindtodevice
, &u
->socket
, "Socket" },
757 { "ExecStartPre", config_parse_exec
, u
->socket
.exec_command
+SOCKET_EXEC_START_PRE
, "Socket" },
758 { "ExecStartPost", config_parse_exec
, u
->socket
.exec_command
+SOCKET_EXEC_START_POST
, "Socket" },
759 { "ExecStopPre", config_parse_exec
, u
->socket
.exec_command
+SOCKET_EXEC_STOP_PRE
, "Socket" },
760 { "ExecStopPost", config_parse_exec
, u
->socket
.exec_command
+SOCKET_EXEC_STOP_POST
, "Socket" },
761 EXEC_CONTEXT_CONFIG_ITEMS(u
->socket
.exec_context
, "Socket"),
763 EXEC_CONTEXT_CONFIG_ITEMS(u
->automount
.exec_context
, "Automount"),
765 { NULL
, NULL
, NULL
, NULL
}
768 #undef EXEC_CONTEXT_CONFIG_ITEMS
770 const char *sections
[3];
777 sections
[0] = "Meta";
778 sections
[1] = section_table
[u
->meta
.type
];
781 if (!(symlink_names
= set_new(string_hash_func
, string_compare_func
)))
784 /* Instead of opening the path right away, we manually
785 * follow all symlinks and add their name to our unit
786 * name set while doing so */
787 if (!(filename
= path_make_absolute(path
, unit_path()))) {
792 if ((r
= open_follow(&filename
, &f
, symlink_names
, &id
)) < 0) {
794 r
= 0; /* returning 0 means: no suitable config file found */
799 /* Now, parse the file contents */
800 r
= config_parse(filename
, f
, sections
, items
, u
);
804 /* Let's try to add in all symlink names we found */
805 while ((k
= set_steal_first(symlink_names
))) {
806 if ((r
= unit_add_name(u
, k
)) < 0)
811 unit_choose_id(u
, id
);
816 free(u
->meta
.load_path
);
817 u
->meta
.load_path
= filename
;
820 r
= 1; /* returning 1 means: suitable config file found and loaded */
823 while ((k
= set_steal_first(symlink_names
)))
825 set_free(symlink_names
);
831 int unit_load_fragment(Unit
*u
) {
836 assert(u
->meta
.load_state
== UNIT_STUB
);
838 if (u
->meta
.load_path
)
839 r
= load_from_path(u
, u
->meta
.load_path
);
844 /* Try to find a name we can load this with */
845 SET_FOREACH(t
, u
->meta
.names
, i
)
846 if ((r
= load_from_path(u
, t
)) != 0)
850 if (u
->meta
.type
== UNIT_SOCKET
)
851 c
= &u
->socket
.exec_context
;
852 else if (u
->meta
.type
== UNIT_SERVICE
)
853 c
= &u
->service
.exec_context
;
858 (c
->output
== EXEC_KERNEL
|| c
->output
== EXEC_SYSLOG
)) {
861 /* If syslog or kernel logging is requested, make sure
862 * our own logging daemon is run first. */
864 if ((k
= unit_add_dependency_by_name(u
, UNIT_AFTER
, SPECIAL_LOGGER_SOCKET
)) < 0)
867 if ((k
= unit_add_dependency_by_name(u
, UNIT_REQUIRES
, SPECIAL_LOGGER_SOCKET
)) < 0)