1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
10 #include "conf-parser.h"
11 #include "load-fragment.h"
14 static int config_parse_deps(
23 NameDependency d
= PTR_TO_UINT(data
);
24 Name
*name
= userdata
;
33 FOREACH_WORD(w
, &l
, rvalue
, state
) {
38 if (!(t
= strndup(w
, l
)))
41 r
= manager_load_name(name
->meta
.manager
, t
, &other
);
47 if ((r
= name_add_dependency(name
, d
, other
)) < 0)
54 static int config_parse_names(
64 Name
*name
= userdata
;
74 FOREACH_WORD(w
, &l
, rvalue
, state
) {
79 if (!(t
= strndup(w
, l
)))
82 other
= manager_get_name(name
->meta
.manager
, t
);
88 if (other
->meta
.load_state
!= NAME_STUB
) {
93 if ((r
= name_merge(name
, other
)) < 0) {
102 if (!(*set
= set_new(trivial_hash_func
, trivial_compare_func
))) {
107 if ((r
= set_put(*set
, t
)) < 0) {
121 static int config_parse_listen(
122 const char *filename
,
141 if (!(p
= new0(SocketPort
, 1)))
144 if (streq(lvalue
, "ListenFIFO")) {
145 p
->type
= SOCKET_FIFO
;
147 if (!(p
->path
= strdup(rvalue
))) {
152 p
->type
= SOCKET_SOCKET
;
154 if ((r
= socket_address_parse(&p
->address
, rvalue
)) < 0) {
155 log_error("[%s:%u] Failed to parse address value: %s", filename
, line
, rvalue
);
160 if (streq(lvalue
, "ListenStream"))
161 p
->address
.type
= SOCK_STREAM
;
162 else if (streq(lvalue
, "ListenDatagram"))
163 p
->address
.type
= SOCK_DGRAM
;
165 assert(streq(lvalue
, "ListenSequentialPacket"));
166 p
->address
.type
= SOCK_SEQPACKET
;
169 if (socket_address_family(&p
->address
) != AF_LOCAL
&& p
->address
.type
== SOCK_SEQPACKET
) {
171 return -EPROTONOSUPPORT
;
176 LIST_PREPEND(SocketPort
, port
, s
->ports
, p
);
181 static int config_parse_socket_bind(
182 const char *filename
,
200 if ((r
= parse_boolean(rvalue
)) < 0) {
201 log_error("[%s:%u] Failed to parse bind IPv6 only value: %s", filename
, line
, rvalue
);
205 s
->bind_ipv6_only
= r
? SOCKET_ADDRESS_IPV6_ONLY
: SOCKET_ADDRESS_BOTH
;
210 static int config_parse_nice(
211 const char *filename
,
219 int *i
= data
, priority
, r
;
226 if ((r
= safe_atoi(rvalue
, &priority
)) < 0) {
227 log_error("[%s:%u] Failed to parse nice priority: %s", filename
, line
, rvalue
);
231 if (priority
< PRIO_MIN
|| priority
>= PRIO_MAX
) {
232 log_error("[%s:%u] Nice priority out of range: %s", filename
, line
, rvalue
);
240 static int config_parse_oom_adjust(
241 const char *filename
,
249 int *i
= data
, oa
, r
;
256 if ((r
= safe_atoi(rvalue
, &oa
)) < 0) {
257 log_error("[%s:%u] Failed to parse OOM adjust value: %s", filename
, line
, rvalue
);
261 if (oa
< OOM_DISABLE
|| oa
> OOM_ADJUST_MAX
) {
262 log_error("[%s:%u] OOM adjust value out of range: %s", filename
, line
, rvalue
);
270 static int config_parse_umask(
271 const char *filename
,
289 l
= strtol(rvalue
, &x
, 8);
290 if (!x
|| *x
|| errno
) {
291 log_error("[%s:%u] Failed to parse umask value: %s", filename
, line
, rvalue
);
292 return errno
? -errno
: -EINVAL
;
295 if (l
< 0000 || l
> 0777) {
296 log_error("[%s:%u] umask value out of range: %s", filename
, line
, rvalue
);
304 static int config_parse_exec(
305 const char *filename
,
313 ExecCommand
**e
= data
, *ee
, *nce
= NULL
;
326 FOREACH_WORD_QUOTED(w
, l
, rvalue
, state
)
329 if (!(n
= new(char*, k
+1)))
333 FOREACH_WORD_QUOTED(w
, l
, rvalue
, state
)
334 if (!(n
[k
++] = strndup(w
, l
)))
339 if (!n
[0] || n
[0][0] != '/') {
340 log_error("[%s:%u] Invalid executable path in command line: %s", filename
, line
, rvalue
);
345 if (!(nce
= new0(ExecCommand
, 1)))
349 if (!(nce
->path
= strdup(n
[0])))
353 /* It's kinda important that we keep the order here */
354 LIST_FIND_TAIL(ExecCommand
, command
, *e
, ee
);
355 LIST_INSERT_AFTER(ExecCommand
, command
, *e
, ee
, nce
);
371 static int config_parse_usec(
372 const char *filename
,
381 unsigned long long u
;
389 if ((r
= safe_atollu(rvalue
, &u
)) < 0) {
390 log_error("[%s:%u] Failed to parse time value: %s", filename
, line
, rvalue
);
394 /* We actually assume the user configures seconds. Later on we
395 * might choose to support suffixes for time values, to
396 * configure bigger or smaller units */
398 *usec
= u
* USEC_PER_SEC
;
403 static int config_parse_service_type(
404 const char *filename
,
419 if (streq(rvalue
, "forking"))
420 s
->type
= SERVICE_FORKING
;
421 else if (streq(rvalue
, "simple"))
422 s
->type
= SERVICE_SIMPLE
;
424 log_error("[%s:%u] Failed to parse service type: %s", filename
, line
, rvalue
);
431 static int config_parse_service_restart(
432 const char *filename
,
447 if (streq(rvalue
, "once"))
448 s
->restart
= SERVICE_ONCE
;
449 else if (streq(rvalue
, "on-success"))
450 s
->type
= SERVICE_RESTART_ON_SUCCESS
;
451 else if (streq(rvalue
, "always"))
452 s
->type
= SERVICE_RESTART_ALWAYS
;
454 log_error("[%s:%u] Failed to parse service type: %s", filename
, line
, rvalue
);
461 int name_load_fragment(Name
*n
) {
463 static const char* const section_table
[_NAME_TYPE_MAX
] = {
464 [NAME_SERVICE
] = "Service",
465 [NAME_TIMER
] = "Timer",
466 [NAME_SOCKET
] = "Socket",
467 [NAME_MILESTONE
] = "Milestone",
468 [NAME_DEVICE
] = "Device",
469 [NAME_MOUNT
] = "Mount",
470 [NAME_AUTOMOUNT
] = "Automount",
471 [NAME_SNAPSHOT
] = "Snapshot"
474 #define EXEC_CONTEXT_CONFIG_ITEMS(context, section) \
475 { "Directory", config_parse_path, &(context).directory, section }, \
476 { "User", config_parse_string, &(context).user, section }, \
477 { "Group", config_parse_string, &(context).group, section }, \
478 { "SupplementaryGroups", config_parse_strv, &(context).supplementary_groups, section }, \
479 { "Nice", config_parse_nice, &(context).nice, section }, \
480 { "OOMAdjust", config_parse_oom_adjust, &(context).oom_adjust, section }, \
481 { "UMask", config_parse_umask, &(context).umask, section }, \
482 { "Environment", config_parse_strv, &(context).environment, section }
484 const ConfigItem items
[] = {
485 { "Names", config_parse_names
, &n
->meta
.names
, "Meta" },
486 { "Description", config_parse_string
, &n
->meta
.description
, "Meta" },
487 { "Requires", config_parse_deps
, UINT_TO_PTR(NAME_REQUIRES
), "Meta" },
488 { "SoftRequires", config_parse_deps
, UINT_TO_PTR(NAME_SOFT_REQUIRES
), "Meta" },
489 { "Wants", config_parse_deps
, UINT_TO_PTR(NAME_WANTS
), "Meta" },
490 { "Requisite", config_parse_deps
, UINT_TO_PTR(NAME_REQUISITE
), "Meta" },
491 { "SoftRequisite", config_parse_deps
, UINT_TO_PTR(NAME_SOFT_REQUISITE
), "Meta" },
492 { "Conflicts", config_parse_deps
, UINT_TO_PTR(NAME_CONFLICTS
), "Meta" },
493 { "Before", config_parse_deps
, UINT_TO_PTR(NAME_BEFORE
), "Meta" },
494 { "After", config_parse_deps
, UINT_TO_PTR(NAME_AFTER
), "Meta" },
496 { "PIDFile", config_parse_path
, &n
->service
.pid_file
, "Service" },
497 { "ExecStartPre", config_parse_exec
, &n
->service
.exec_command
[SERVICE_EXEC_START_PRE
], "Service" },
498 { "ExecStart", config_parse_exec
, &n
->service
.exec_command
[SERVICE_EXEC_START
], "Service" },
499 { "ExecStartPost", config_parse_exec
, &n
->service
.exec_command
[SERVICE_EXEC_START_POST
], "Service" },
500 { "ExecReload", config_parse_exec
, &n
->service
.exec_command
[SERVICE_EXEC_RELOAD
], "Service" },
501 { "ExecStop", config_parse_exec
, &n
->service
.exec_command
[SERVICE_EXEC_STOP
], "Service" },
502 { "ExecStopPost", config_parse_exec
, &n
->service
.exec_command
[SERVICE_EXEC_STOP_POST
], "Service" },
503 { "RestartSec", config_parse_usec
, &n
->service
.restart_usec
, "Service" },
504 { "TimeoutSec", config_parse_usec
, &n
->service
.timeout_usec
, "Service" },
505 { "Type", config_parse_service_type
, &n
->service
, "Service" },
506 { "Restart", config_parse_service_restart
, &n
->service
, "Service" },
507 EXEC_CONTEXT_CONFIG_ITEMS(n
->service
.exec_context
, "Service"),
509 { "ListenStream", config_parse_listen
, &n
->socket
, "Socket" },
510 { "ListenDatagram", config_parse_listen
, &n
->socket
, "Socket" },
511 { "ListenSequentialPacket", config_parse_listen
, &n
->socket
, "Socket" },
512 { "ListenFIFO", config_parse_listen
, &n
->socket
, "Socket" },
513 { "BindIPv6Only", config_parse_socket_bind
, &n
->socket
, "Socket" },
514 { "Backlog", config_parse_unsigned
, &n
->socket
.backlog
, "Socket" },
515 { "ExecStartPre", config_parse_exec
, &n
->service
.exec_command
[SOCKET_EXEC_START_PRE
], "Socket" },
516 { "ExecStartPost", config_parse_exec
, &n
->service
.exec_command
[SOCKET_EXEC_START_POST
], "Socket" },
517 { "ExecStopPre", config_parse_exec
, &n
->service
.exec_command
[SOCKET_EXEC_STOP_PRE
], "Socket" },
518 { "ExecStopPost", config_parse_exec
, &n
->service
.exec_command
[SOCKET_EXEC_STOP_POST
], "Socket" },
519 EXEC_CONTEXT_CONFIG_ITEMS(n
->socket
.exec_context
, "Socket"),
521 EXEC_CONTEXT_CONFIG_ITEMS(n
->automount
.exec_context
, "Automount"),
523 { NULL
, NULL
, NULL
, NULL
}
526 #undef EXEC_CONTEXT_CONFIG_ITEMS
530 const char *sections
[3];
534 assert(n
->meta
.load_state
== NAME_STUB
);
536 sections
[0] = "Meta";
537 sections
[1] = section_table
[n
->meta
.type
];
540 SET_FOREACH(t
, n
->meta
.names
, i
) {
542 /* Try to find a name we can load this with */
543 if ((r
= config_parse(t
, sections
, items
, n
)) == -ENOENT
)
546 /* Yay, we succeeded! Now let's call this our identifier */