chiark / gitweb /
first attempt at proper service/socket logic
[elogind.git] / load-fragment.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 #include <assert.h>
4 #include <errno.h>
5 #include <string.h>
6 #include <linux/oom.h>
7
8 #include "name.h"
9 #include "strv.h"
10 #include "conf-parser.h"
11 #include "load-fragment.h"
12 #include "log.h"
13
14 static int config_parse_deps(
15                 const char *filename,
16                 unsigned line,
17                 const char *section,
18                 const char *lvalue,
19                 const char *rvalue,
20                 void *data,
21                 void *userdata) {
22
23         Set **set = data;
24         Name *name = userdata;
25         char *w;
26         size_t l;
27         char *state;
28
29         assert(filename);
30         assert(lvalue);
31         assert(rvalue);
32         assert(data);
33
34         FOREACH_WORD(w, &l, rvalue, state) {
35                 char *t;
36                 int r;
37                 Name *other;
38
39                 if (!(t = strndup(w, l)))
40                         return -ENOMEM;
41
42                 r = manager_load_name(name->meta.manager, t, &other);
43                 free(t);
44
45                 if (r < 0)
46                         return r;
47
48                 if ((r = set_ensure_allocated(set, trivial_hash_func, trivial_compare_func)) < 0)
49                         return r;
50
51                 if ((r = set_put(*set, other)) < 0)
52                         return r;
53         }
54
55         return 0;
56 }
57
58 static int config_parse_names(
59                 const char *filename,
60                 unsigned line,
61                 const char *section,
62                 const char *lvalue,
63                 const char *rvalue,
64                 void *data,
65                 void *userdata) {
66
67         Set **set = data;
68         Name *name = userdata;
69         char *w;
70         size_t l;
71         char *state;
72
73         assert(filename);
74         assert(lvalue);
75         assert(rvalue);
76         assert(data);
77
78         FOREACH_WORD(w, &l, rvalue, state) {
79                 char *t;
80                 int r;
81                 Name *other;
82
83                 if (!(t = strndup(w, l)))
84                         return -ENOMEM;
85
86                 other = manager_get_name(name->meta.manager, t);
87
88                 if (other) {
89
90                         if (other != name) {
91
92                                 if (other->meta.load_state != NAME_STUB) {
93                                         free(t);
94                                         return -EEXIST;
95                                 }
96
97                                 if ((r = name_merge(name, other)) < 0) {
98                                         free(t);
99                                         return r;
100                                 }
101                         }
102
103                 } else {
104
105                         if (!*set)
106                                 if (!(*set = set_new(trivial_hash_func, trivial_compare_func))) {
107                                         free(t);
108                                         return -ENOMEM;
109                                 }
110
111                         if ((r = set_put(*set, t)) < 0) {
112                                 free(t);
113                                 return r;
114                         }
115
116                         t = NULL;
117                 }
118
119                 free(t);
120         }
121
122         return 0;
123 }
124
125 static int config_parse_listen(
126                 const char *filename,
127                 unsigned line,
128                 const char *section,
129                 const char *lvalue,
130                 const char *rvalue,
131                 void *data,
132                 void *userdata) {
133
134         int r;
135         SocketPort *p;
136         Socket *s;
137
138         assert(filename);
139         assert(lvalue);
140         assert(rvalue);
141         assert(data);
142
143         s = (Socket*) data;
144
145         if (!(p = new0(SocketPort, 1)))
146                 return -ENOMEM;
147
148         if (streq(lvalue, "ListenFIFO")) {
149                 p->type = SOCKET_FIFO;
150
151                 if (!(p->path = strdup(rvalue))) {
152                         free(p);
153                         return -ENOMEM;
154                 }
155         } else {
156                 p->type = SOCKET_SOCKET;
157
158                 if ((r = socket_address_parse(&p->address, rvalue)) < 0) {
159                         log_error("[%s:%u] Failed to parse address value: %s", filename, line, rvalue);
160                         free(p);
161                         return r;
162                 }
163
164                 if (streq(lvalue, "ListenStream"))
165                         p->address.type = SOCK_STREAM;
166                 else if (streq(lvalue, "ListenDatagram"))
167                         p->address.type = SOCK_DGRAM;
168                 else {
169                         assert(streq(lvalue, "ListenSequentialPacket"));
170                         p->address.type = SOCK_SEQPACKET;
171                 }
172
173                 if (socket_address_family(&p->address) != AF_LOCAL && p->address.type == SOCK_SEQPACKET) {
174                         free(p);
175                         return -EPROTONOSUPPORT;
176                 }
177         }
178
179         p->fd = -1;
180         LIST_PREPEND(SocketPort, port, s->ports, p);
181
182         return 0;
183 }
184
185 static int config_parse_socket_bind(
186                 const char *filename,
187                 unsigned line,
188                 const char *section,
189                 const char *lvalue,
190                 const char *rvalue,
191                 void *data,
192                 void *userdata) {
193
194         int r;
195         Socket *s;
196
197         assert(filename);
198         assert(lvalue);
199         assert(rvalue);
200         assert(data);
201
202         s = (Socket*) data;
203
204         if ((r = parse_boolean(rvalue)) < 0) {
205                 log_error("[%s:%u] Failed to parse bind IPv6 only value: %s", filename, line, rvalue);
206                 return r;
207         }
208
209         s->bind_ipv6_only = r ? SOCKET_ADDRESS_IPV6_ONLY : SOCKET_ADDRESS_BOTH;
210
211         return 0;
212 }
213
214 static int config_parse_nice(
215                 const char *filename,
216                 unsigned line,
217                 const char *section,
218                 const char *lvalue,
219                 const char *rvalue,
220                 void *data,
221                 void *userdata) {
222
223         int *i = data, priority, r;
224
225         assert(filename);
226         assert(lvalue);
227         assert(rvalue);
228         assert(data);
229
230         if ((r = safe_atoi(rvalue, &priority)) < 0) {
231                 log_error("[%s:%u] Failed to parse nice priority: %s", filename, line, rvalue);
232                 return r;
233         }
234
235         if (priority < PRIO_MIN || priority >= PRIO_MAX) {
236                 log_error("[%s:%u] Nice priority out of range: %s", filename, line, rvalue);
237                 return -ERANGE;
238         }
239
240         *i = priority;
241         return 0;
242 }
243
244 static int config_parse_oom_adjust(
245                 const char *filename,
246                 unsigned line,
247                 const char *section,
248                 const char *lvalue,
249                 const char *rvalue,
250                 void *data,
251                 void *userdata) {
252
253         int *i = data, oa, r;
254
255         assert(filename);
256         assert(lvalue);
257         assert(rvalue);
258         assert(data);
259
260         if ((r = safe_atoi(rvalue, &oa)) < 0) {
261                 log_error("[%s:%u] Failed to parse OOM adjust value: %s", filename, line, rvalue);
262                 return r;
263         }
264
265         if (oa < OOM_DISABLE || oa > OOM_ADJUST_MAX) {
266                 log_error("[%s:%u] OOM adjust value out of range: %s", filename, line, rvalue);
267                 return -ERANGE;
268         }
269
270         *i = oa;
271         return 0;
272 }
273
274 static int config_parse_umask(
275                 const char *filename,
276                 unsigned line,
277                 const char *section,
278                 const char *lvalue,
279                 const char *rvalue,
280                 void *data,
281                 void *userdata) {
282
283         mode_t *m = data;
284         long l;
285         char *x = NULL;
286
287         assert(filename);
288         assert(lvalue);
289         assert(rvalue);
290         assert(data);
291
292         errno = 0;
293         l = strtol(rvalue, &x, 8);
294         if (!x || *x || errno) {
295                 log_error("[%s:%u] Failed to parse umask value: %s", filename, line, rvalue);
296                 return errno ? -errno : -EINVAL;
297         }
298
299         if (l < 0000 || l > 0777) {
300                 log_error("[%s:%u] umask value out of range: %s", filename, line, rvalue);
301                 return -ERANGE;
302         }
303
304         *m = (mode_t) l;
305         return 0;
306 }
307
308 static int config_parse_exec(
309                 const char *filename,
310                 unsigned line,
311                 const char *section,
312                 const char *lvalue,
313                 const char *rvalue,
314                 void *data,
315                 void *userdata) {
316
317         ExecCommand **e = data, *ee, *nce = NULL;
318         char **n;
319         char *w;
320         unsigned k;
321         size_t l;
322         char *state;
323
324         assert(filename);
325         assert(lvalue);
326         assert(rvalue);
327         assert(data);
328
329         k = 0;
330         FOREACH_WORD_QUOTED(w, l, rvalue, state)
331                 k++;
332
333         if (!(n = new(char*, k+1)))
334                 return -ENOMEM;
335
336         FOREACH_WORD_QUOTED(w, l, rvalue, state)
337                 if (!(n[k++] = strndup(w, l)))
338                         goto fail;
339
340         n[k] = NULL;
341
342         if (!n[0] || n[0][0] != '/') {
343                 log_error("[%s:%u] Invalid executable path in command line: %s", filename, line, rvalue);
344                 strv_free(n);
345                 return -EINVAL;
346         }
347
348         if (!(nce = new0(ExecCommand, 1)))
349                 goto fail;
350
351         nce->argv = n;
352         if (!(nce->path = strdup(n[0])))
353                 goto fail;
354
355         if (*e) {
356                 /* It's kinda important that we keep the order here */
357                 LIST_FIND_TAIL(ExecCommand, command, *e, ee);
358                 LIST_INSERT_AFTER(ExecCommand, command, *e, ee, nce);
359         } else
360                 *e = nce;
361
362         return 0;
363
364 fail:
365         for (; k > 0; k--)
366                 free(n[k-1]);
367         free(n);
368
369         free(nce);
370
371         return -ENOMEM;
372 }
373
374 static int config_parse_usec(
375                 const char *filename,
376                 unsigned line,
377                 const char *section,
378                 const char *lvalue,
379                 const char *rvalue,
380                 void *data,
381                 void *userdata) {
382
383         usec_t *usec = data;
384         unsigned long long u;
385         int r;
386
387         assert(filename);
388         assert(lvalue);
389         assert(rvalue);
390         assert(data);
391
392         if ((r = safe_atollu(rvalue, &u)) < 0) {
393                 log_error("[%s:%u] Failed to parse time value: %s", filename, line, rvalue);
394                 return r;
395         }
396
397         /* We actually assume the user configures seconds. Later on we
398          * might choose to support suffixes for time values, to
399          * configure bigger or smaller units */
400
401         *usec = u * USEC_PER_SEC;
402
403         return 0;
404 }
405
406 static int config_parse_service_type(
407                 const char *filename,
408                 unsigned line,
409                 const char *section,
410                 const char *lvalue,
411                 const char *rvalue,
412                 void *data,
413                 void *userdata) {
414
415         Service *s = data;
416
417         assert(filename);
418         assert(lvalue);
419         assert(rvalue);
420         assert(data);
421
422         if (streq(rvalue, "forking"))
423                 s->type = SERVICE_FORKING;
424         else if (streq(rvalue, "simple"))
425                 s->type = SERVICE_SIMPLE;
426         else {
427                 log_error("[%s:%u] Failed to parse service type: %s", filename, line, rvalue);
428                 return -EBADMSG;
429         }
430
431         return 0;
432 }
433
434 static int config_parse_service_restart(
435                 const char *filename,
436                 unsigned line,
437                 const char *section,
438                 const char *lvalue,
439                 const char *rvalue,
440                 void *data,
441                 void *userdata) {
442
443         Service *s = data;
444
445         assert(filename);
446         assert(lvalue);
447         assert(rvalue);
448         assert(data);
449
450         if (streq(rvalue, "once"))
451                 s->restart = SERVICE_ONCE;
452         else if (streq(rvalue, "on-success"))
453                 s->type = SERVICE_RESTART_ON_SUCCESS;
454         else if (streq(rvalue, "always"))
455                 s->type = SERVICE_RESTART_ALWAYS;
456         else {
457                 log_error("[%s:%u] Failed to parse service type: %s", filename, line, rvalue);
458                 return -EBADMSG;
459         }
460
461         return 0;
462 }
463
464 int name_load_fragment(Name *n) {
465
466         static const char* const section_table[_NAME_TYPE_MAX] = {
467                 [NAME_SERVICE]   = "Service",
468                 [NAME_TIMER]     = "Timer",
469                 [NAME_SOCKET]    = "Socket",
470                 [NAME_MILESTONE] = "Milestone",
471                 [NAME_DEVICE]    = "Device",
472                 [NAME_MOUNT]     = "Mount",
473                 [NAME_AUTOMOUNT] = "Automount",
474                 [NAME_SNAPSHOT]  = "Snapshot"
475         };
476
477 #define EXEC_CONTEXT_CONFIG_ITEMS(context, section) \
478                 { "Directory",              config_parse_path,            &(context).directory,                              section   }, \
479                 { "User",                   config_parse_string,          &(context).user,                                   section   }, \
480                 { "Group",                  config_parse_string,          &(context).group,                                  section   }, \
481                 { "SupplementaryGroups",    config_parse_strv,            &(context).supplementary_groups,                   section   }, \
482                 { "Nice",                   config_parse_nice,            &(context).nice,                                   section   }, \
483                 { "OOMAdjust",              config_parse_oom_adjust,      &(context).oom_adjust,                             section   }, \
484                 { "UMask",                  config_parse_umask,           &(context).umask,                                  section   }, \
485                 { "Environment",            config_parse_strv,            &(context).environment,                            section   }
486
487         const ConfigItem items[] = {
488                 { "Names",                  config_parse_names,           &n->meta.names,                                    "Meta"    },
489                 { "Description",            config_parse_string,          &n->meta.description,                              "Meta"    },
490                 { "Requires",               config_parse_deps,            n->meta.dependencies+NAME_REQUIRES,                "Meta"    },
491                 { "SoftRequires",           config_parse_deps,            n->meta.dependencies+NAME_SOFT_REQUIRES,           "Meta"    },
492                 { "Wants",                  config_parse_deps,            n->meta.dependencies+NAME_WANTS,                   "Meta"    },
493                 { "Requisite",              config_parse_deps,            n->meta.dependencies+NAME_REQUISITE,               "Meta"    },
494                 { "SoftRequisite",          config_parse_deps,            n->meta.dependencies+NAME_SOFT_REQUISITE,          "Meta"    },
495                 { "Conflicts",              config_parse_deps,            n->meta.dependencies+NAME_CONFLICTS,               "Meta"    },
496                 { "Before",                 config_parse_deps,            n->meta.dependencies+NAME_BEFORE,                  "Meta"    },
497                 { "After",                  config_parse_deps,            n->meta.dependencies+NAME_AFTER,                   "Meta"    },
498
499                 { "PIDFile",                config_parse_path,            &n->service.pid_file,                              "Service" },
500                 { "ExecStartPre",           config_parse_exec,            &n->service.exec_command[SERVICE_EXEC_START_PRE],  "Service" },
501                 { "ExecStart",              config_parse_exec,            &n->service.exec_command[SERVICE_EXEC_START],      "Service" },
502                 { "ExecStartPost",          config_parse_exec,            &n->service.exec_command[SERVICE_EXEC_START_POST], "Service" },
503                 { "ExecReload",             config_parse_exec,            &n->service.exec_command[SERVICE_EXEC_RELOAD],     "Service" },
504                 { "ExecStop",               config_parse_exec,            &n->service.exec_command[SERVICE_EXEC_STOP],       "Service" },
505                 { "ExecStopPost",           config_parse_exec,            &n->service.exec_command[SERVICE_EXEC_STOP_POST],  "Service" },
506                 { "RestartSec",             config_parse_usec,            &n->service.restart_usec,                          "Service" },
507                 { "TimeoutSec",             config_parse_usec,            &n->service.timeout_usec,                          "Service" },
508                 { "Type",                   config_parse_service_type,    &n->service,                                       "Service" },
509                 { "Restart",                config_parse_service_restart, &n->service,                                       "Service" },
510                 EXEC_CONTEXT_CONFIG_ITEMS(n->service.exec_context, "Service"),
511
512                 { "ListenStream",           config_parse_listen,          &n->socket,                                        "Socket"  },
513                 { "ListenDatagram",         config_parse_listen,          &n->socket,                                        "Socket"  },
514                 { "ListenSequentialPacket", config_parse_listen,          &n->socket,                                        "Socket"  },
515                 { "ListenFIFO",             config_parse_listen,          &n->socket,                                        "Socket"  },
516                 { "BindIPv6Only",           config_parse_socket_bind,     &n->socket,                                        "Socket"  },
517                 { "Backlog",                config_parse_unsigned,        &n->socket.backlog,                                "Socket"  },
518                 { "ExecStartPre",           config_parse_exec,            &n->service.exec_command[SOCKET_EXEC_START_PRE],   "Socket"  },
519                 { "ExecStartPost",          config_parse_exec,            &n->service.exec_command[SOCKET_EXEC_START_POST],  "Socket"  },
520                 { "ExecStopPre",            config_parse_exec,            &n->service.exec_command[SOCKET_EXEC_STOP_PRE],    "Socket"  },
521                 { "ExecStopPost",           config_parse_exec,            &n->service.exec_command[SOCKET_EXEC_STOP_POST],   "Socket"  },
522                 EXEC_CONTEXT_CONFIG_ITEMS(n->socket.exec_context, "Socket"),
523
524                 EXEC_CONTEXT_CONFIG_ITEMS(n->automount.exec_context, "Automount"),
525
526                 { NULL, NULL, NULL, NULL }
527         };
528
529 #undef EXEC_CONTEXT_CONFIG_ITEMS
530
531         char *t;
532         int r;
533         const char *sections[3];
534         Iterator i;
535
536         assert(n);
537         assert(n->meta.load_state == NAME_STUB);
538
539         sections[0] = "Meta";
540         sections[1] = section_table[n->meta.type];
541         sections[2] = NULL;
542
543         SET_FOREACH(t, n->meta.names, i) {
544
545                 /* Try to find a name we can load this with */
546                 if ((r = config_parse(t, sections, items, n)) == -ENOENT)
547                         continue;
548
549                 /* Yay, we succeeded! Now let's call this our identifier */
550                 if (r == 0)
551                         n->meta.id = t;
552
553                 return r;
554         }
555
556         return -ENOENT;
557 }