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
;
482 int config_parse_output(
483 const char *filename
,
491 ExecOutput
*o
= data
;
498 if (streq(rvalue
, "syslog"))
500 else if (streq(rvalue
, "null"))
502 else if (streq(rvalue
, "syslog"))
504 else if (streq(rvalue
, "kernel"))
507 log_error("[%s:%u] Failed to parse log output: %s", filename
, line
, rvalue
);
514 int config_parse_facility(
515 const char *filename
,
523 static const char * const table
[LOG_NFACILITIES
] = {
524 [LOG_FAC(LOG_KERN
)] = "kern",
525 [LOG_FAC(LOG_USER
)] = "user",
526 [LOG_FAC(LOG_MAIL
)] = "mail",
527 [LOG_FAC(LOG_DAEMON
)] = "daemon",
528 [LOG_FAC(LOG_AUTH
)] = "auth",
529 [LOG_FAC(LOG_SYSLOG
)] = "syslog",
530 [LOG_FAC(LOG_LPR
)] = "lpr",
531 [LOG_FAC(LOG_NEWS
)] = "news",
532 [LOG_FAC(LOG_UUCP
)] = "uucp",
533 [LOG_FAC(LOG_CRON
)] = "cron",
534 [LOG_FAC(LOG_AUTHPRIV
)] = "authpriv",
535 [LOG_FAC(LOG_FTP
)] = "ftp",
536 [LOG_FAC(LOG_LOCAL0
)] = "local0",
537 [LOG_FAC(LOG_LOCAL1
)] = "local1",
538 [LOG_FAC(LOG_LOCAL2
)] = "local2",
539 [LOG_FAC(LOG_LOCAL3
)] = "local3",
540 [LOG_FAC(LOG_LOCAL4
)] = "local4",
541 [LOG_FAC(LOG_LOCAL5
)] = "local5",
542 [LOG_FAC(LOG_LOCAL6
)] = "local6",
543 [LOG_FAC(LOG_LOCAL7
)] = "local7"
546 ExecOutput
*o
= data
;
554 for (i
= 0; i
< (int) ELEMENTSOF(table
); i
++)
555 if (streq(rvalue
, table
[i
])) {
556 *o
= LOG_MAKEPRI(i
, LOG_PRI(*o
));
560 if (i
>= (int) ELEMENTSOF(table
)) {
562 /* Second try, let's see if this is a number. */
563 if (safe_atoi(rvalue
, &i
) >= 0 &&
565 i
< (int) ELEMENTSOF(table
))
566 *o
= LOG_MAKEPRI(i
, LOG_PRI(*o
));
568 log_error("[%s:%u] Failed to parse log output: %s", filename
, line
, rvalue
);
576 int config_parse_level(
577 const char *filename
,
585 static const char * const table
[LOG_DEBUG
+1] = {
586 [LOG_EMERG
] = "emerg",
587 [LOG_ALERT
] = "alert",
590 [LOG_WARNING
] = "warning",
591 [LOG_NOTICE
] = "notice",
593 [LOG_DEBUG
] = "debug"
596 ExecOutput
*o
= data
;
604 for (i
= 0; i
< (int) ELEMENTSOF(table
); i
++)
605 if (streq(rvalue
, table
[i
])) {
606 *o
= LOG_MAKEPRI(LOG_FAC(*o
), i
);
610 if (i
>= LOG_NFACILITIES
) {
612 /* Second try, let's see if this is a number. */
613 if (safe_atoi(rvalue
, &i
) >= 0 &&
615 i
< (int) ELEMENTSOF(table
))
616 *o
= LOG_MAKEPRI(LOG_FAC(*o
), i
);
618 log_error("[%s:%u] Failed to parse log output: %s", filename
, line
, rvalue
);
628 static int open_follow(char **filename
, FILE **_f
, Set
*names
, char **_id
) {
639 /* This will update the filename pointer if the loaded file is
640 * reached by a symlink. The old string will be freed. */
643 char *target
, *k
, *name
;
645 if (c
++ >= FOLLOW_MAX
)
648 /* Add the file name we are currently looking at to
649 * the names of this unit */
650 name
= file_name_from_path(*filename
);
651 if (!(id
= set_get(names
, name
))) {
653 if (!(id
= strdup(name
)))
656 if ((r
= set_put(names
, id
)) < 0) {
662 /* Try to open the file name, but don't if its a symlink */
663 if ((fd
= open(*filename
, O_RDONLY
|O_CLOEXEC
|O_NOCTTY
|O_NOFOLLOW
)) >= 0)
669 /* Hmm, so this is a symlink. Let's read the name, and follow it manually */
670 if ((r
= readlink_malloc(*filename
, &target
)) < 0)
673 k
= file_in_same_dir(*filename
, target
);
683 if (!(f
= fdopen(fd
, "r"))) {
685 assert(close_nointr(fd
) == 0);
694 static int load_from_path(Unit
*u
, const char *path
) {
696 static const char* const section_table
[_UNIT_TYPE_MAX
] = {
697 [UNIT_SERVICE
] = "Service",
698 [UNIT_TIMER
] = "Timer",
699 [UNIT_SOCKET
] = "Socket",
700 [UNIT_TARGET
] = "Target",
701 [UNIT_DEVICE
] = "Device",
702 [UNIT_MOUNT
] = "Mount",
703 [UNIT_AUTOMOUNT
] = "Automount",
704 [UNIT_SNAPSHOT
] = "Snapshot"
707 #define EXEC_CONTEXT_CONFIG_ITEMS(context, section) \
708 { "Directory", config_parse_path, &(context).directory, section }, \
709 { "User", config_parse_string, &(context).user, section }, \
710 { "Group", config_parse_string, &(context).group, section }, \
711 { "SupplementaryGroups", config_parse_strv, &(context).supplementary_groups, section }, \
712 { "Nice", config_parse_nice, &(context).nice, section }, \
713 { "OOMAdjust", config_parse_oom_adjust, &(context).oom_adjust, section }, \
714 { "UMask", config_parse_umask, &(context).umask, section }, \
715 { "Environment", config_parse_strv, &(context).environment, section }, \
716 { "Output", config_parse_output, &(context).output, section }, \
717 { "SyslogIdentifier", config_parse_string, &(context).syslog_identifier, section }, \
718 { "SyslogFacility", config_parse_facility, &(context).syslog_priority, section }, \
719 { "SyslogLevel", config_parse_level, &(context).syslog_priority, section }
721 const ConfigItem items
[] = {
722 { "Names", config_parse_names
, u
, "Meta" },
723 { "Description", config_parse_string
, &u
->meta
.description
, "Meta" },
724 { "Requires", config_parse_deps
, UINT_TO_PTR(UNIT_REQUIRES
), "Meta" },
725 { "SoftRequires", config_parse_deps
, UINT_TO_PTR(UNIT_SOFT_REQUIRES
), "Meta" },
726 { "Wants", config_parse_deps
, UINT_TO_PTR(UNIT_WANTS
), "Meta" },
727 { "Requisite", config_parse_deps
, UINT_TO_PTR(UNIT_REQUISITE
), "Meta" },
728 { "SoftRequisite", config_parse_deps
, UINT_TO_PTR(UNIT_SOFT_REQUISITE
), "Meta" },
729 { "Conflicts", config_parse_deps
, UINT_TO_PTR(UNIT_CONFLICTS
), "Meta" },
730 { "Before", config_parse_deps
, UINT_TO_PTR(UNIT_BEFORE
), "Meta" },
731 { "After", config_parse_deps
, UINT_TO_PTR(UNIT_AFTER
), "Meta" },
733 { "PIDFile", config_parse_path
, &u
->service
.pid_file
, "Service" },
734 { "ExecStartPre", config_parse_exec
, u
->service
.exec_command
+SERVICE_EXEC_START_PRE
, "Service" },
735 { "ExecStart", config_parse_exec
, u
->service
.exec_command
+SERVICE_EXEC_START
, "Service" },
736 { "ExecStartPost", config_parse_exec
, u
->service
.exec_command
+SERVICE_EXEC_START_POST
, "Service" },
737 { "ExecReload", config_parse_exec
, u
->service
.exec_command
+SERVICE_EXEC_RELOAD
, "Service" },
738 { "ExecStop", config_parse_exec
, u
->service
.exec_command
+SERVICE_EXEC_STOP
, "Service" },
739 { "ExecStopPost", config_parse_exec
, u
->service
.exec_command
+SERVICE_EXEC_STOP_POST
, "Service" },
740 { "RestartSec", config_parse_usec
, &u
->service
.restart_usec
, "Service" },
741 { "TimeoutSec", config_parse_usec
, &u
->service
.timeout_usec
, "Service" },
742 { "Type", config_parse_service_type
, &u
->service
, "Service" },
743 { "Restart", config_parse_service_restart
, &u
->service
, "Service" },
744 EXEC_CONTEXT_CONFIG_ITEMS(u
->service
.exec_context
, "Service"),
746 { "ListenStream", config_parse_listen
, &u
->socket
, "Socket" },
747 { "ListenDatagram", config_parse_listen
, &u
->socket
, "Socket" },
748 { "ListenSequentialPacket", config_parse_listen
, &u
->socket
, "Socket" },
749 { "ListenFIFO", config_parse_listen
, &u
->socket
, "Socket" },
750 { "BindIPv6Only", config_parse_socket_bind
, &u
->socket
, "Socket" },
751 { "Backlog", config_parse_unsigned
, &u
->socket
.backlog
, "Socket" },
752 { "BindToDevice", config_parse_bindtodevice
, &u
->socket
, "Socket" },
753 { "ExecStartPre", config_parse_exec
, u
->socket
.exec_command
+SOCKET_EXEC_START_PRE
, "Socket" },
754 { "ExecStartPost", config_parse_exec
, u
->socket
.exec_command
+SOCKET_EXEC_START_POST
, "Socket" },
755 { "ExecStopPre", config_parse_exec
, u
->socket
.exec_command
+SOCKET_EXEC_STOP_PRE
, "Socket" },
756 { "ExecStopPost", config_parse_exec
, u
->socket
.exec_command
+SOCKET_EXEC_STOP_POST
, "Socket" },
757 EXEC_CONTEXT_CONFIG_ITEMS(u
->socket
.exec_context
, "Socket"),
759 EXEC_CONTEXT_CONFIG_ITEMS(u
->automount
.exec_context
, "Automount"),
761 { NULL
, NULL
, NULL
, NULL
}
764 #undef EXEC_CONTEXT_CONFIG_ITEMS
766 const char *sections
[3];
773 sections
[0] = "Meta";
774 sections
[1] = section_table
[u
->meta
.type
];
777 if (!(symlink_names
= set_new(string_hash_func
, string_compare_func
)))
780 /* Instead of opening the path right away, we manually
781 * follow all symlinks and add their name to our unit
782 * name set while doing so */
783 if (!(filename
= path_make_absolute(path
, unit_path()))) {
788 if ((r
= open_follow(&filename
, &f
, symlink_names
, &id
)) < 0) {
790 r
= 0; /* returning 0 means: no suitable config file found */
795 /* Now, parse the file contents */
796 r
= config_parse(filename
, f
, sections
, items
, u
);
800 /* Let's try to add in all symlink names we found */
801 while ((k
= set_steal_first(symlink_names
))) {
802 if ((r
= unit_add_name(u
, k
)) < 0)
806 assert_se(u
->meta
.id
= set_get(u
->meta
.names
, k
));
811 free(u
->meta
.load_path
);
812 u
->meta
.load_path
= filename
;
815 r
= 1; /* returning 1 means: suitable config file found and loaded */
818 while ((k
= set_steal_first(symlink_names
)))
820 set_free(symlink_names
);
826 int unit_load_fragment(Unit
*u
) {
831 assert(u
->meta
.load_state
== UNIT_STUB
);
833 if (u
->meta
.load_path
)
834 r
= load_from_path(u
, u
->meta
.load_path
);
839 /* Try to find a name we can load this with */
840 SET_FOREACH(t
, u
->meta
.names
, i
)
841 if ((r
= load_from_path(u
, t
)) != 0)
845 if (u
->meta
.type
== UNIT_SOCKET
)
846 c
= &u
->socket
.exec_context
;
847 else if (u
->meta
.type
== UNIT_SERVICE
)
848 c
= &u
->service
.exec_context
;
853 (c
->output
== EXEC_KERNEL
|| c
->output
== EXEC_SYSLOG
)) {
856 /* If syslog or kernel logging is requested, make sure
857 * our own logging daemon is run first. */
859 if ((k
= unit_add_dependency(u
, UNIT_AFTER
, u
->meta
.manager
->special_units
[SPECIAL_LOGGER_SOCKET
])) < 0)
862 if ((k
= unit_add_dependency(u
, UNIT_REQUIRES
, u
->meta
.manager
->special_units
[SPECIAL_LOGGER_SOCKET
])) < 0)