2 Copyright 2016, 2017 IoT.bzh
4 author: José Bollo <jose.bollo@iot.bzh>
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
23 #include <sys/types.h>
30 #include <json-c/json.h>
33 #include "utils-file.h"
35 #include "wgtpkg-mustach.h"
36 #include "utils-json.h"
38 #include "utils-systemd.h"
40 #include "wgtpkg-unit.h"
45 #define isblank(c) ((c)==' '||(c)=='\t')
48 /* the template for all units */
49 static char *template;
52 * Search for the 'pattern' in 'text'.
53 * Returns 1 if 'text' matches the 'pattern' or else returns 0.
54 * When returning 1 and 'after' isn't NULL, the pointer to the
55 * first character after the pettern in 'text' is stored in 'after'.
56 * The characters '\n' and ' ' have a special mening in the search:
57 * * '\n': matches any space or tabs (including none) followed
58 * either by '\n' or '\0' (end of the string)
59 * * ' ': matches any space or tabs but at least one.
61 static int matches(const char *pattern, char *text, char **after)
99 * Pack a null terminated 'text' by removing empty lines,
100 * lines made of blanks and terminated with \, lines
101 * starting with the 'purge' character (can be null).
102 * Lines made of the 'purge' character followed with
103 * "nl" exactly (without quotes ") are replaced with
106 * Returns the size after packing (offset of the ending null).
108 static size_t pack(char *text, char purge)
110 char *read; /* read iterator */
111 char *write; /* write iterator */
112 char *begin; /* begin the copied text of the line */
113 char *start; /* first character of the line that isn't blanck */
114 char c; /* currently scanned character (pointed by read) */
115 char emit; /* flag telling whether line is to be copied */
116 char cont; /* flag telling whether the line continues the previous one */
117 char nextcont; /* flag telling whether the line will continues the next one */
120 c = *(write = read = text);
122 /* iteration over lines */
124 /* computes emit, nextcont, emit and start for the current line */
129 while (c && c != '\n') {
131 if (c == '\\' && read[1] == '\n')
143 /* emit the line if not empty */
144 if (emit || (cont && !nextcont)) {
145 /* removes the blanks on the left of not continuing lines */
148 /* check if purge applies */
149 if (purge && *begin == purge) {
150 /* yes, insert new line if requested */
151 if (!strncmp(begin+1, "nl\n",3))
154 /* copies the line */
155 while (begin != read)
161 return (size_t)(write - text);
165 * Searchs the first character of the next line
166 * of the 'text' and returns its address
167 * Returns NULL if there is no next line.
169 static inline char *nextline(char *text)
171 char *result = strchr(text, '\n');
172 return result + !!result;
176 * Search in 'text' the offset of a line beginning with the 'pattern'
177 * Returns NULL if not found or the address of the line contning the pattern
178 * If args isn't NULL and the pattern is found, the pointed pattern is
179 * updated with the address of the character following the found pattern.
181 static char *offset(char *text, const char *pattern, char **args)
183 while (text && !matches(pattern, text, args))
184 text = nextline(text);
191 static int process_one_unit(char *spec, struct unitdesc *desc)
193 char *nsoc, *nsrv, *name, *wanted;
194 int isuser, issystem, issock, isserv, iswanted;
197 /* finds the configuration directive of the unit */
198 isuser = !!offset(spec, "%systemd-unit user\n", NULL);
199 issystem = !!offset(spec, "%systemd-unit system\n", NULL);
200 issock = !!offset(spec, "%systemd-unit socket ", &nsoc);
201 isserv = !!offset(spec, "%systemd-unit service ", &nsrv);
202 iswanted = !!offset(spec, "%systemd-unit wanted-by ", &wanted);
204 /* check the unit scope */
205 if ((isuser + issystem) == 1) {
206 desc->scope = isuser ? unitscope_user : unitscope_system;
208 desc->scope = unitscope_unknown;
211 /* check the unit type */
212 if ((issock + isserv) == 1) {
214 desc->type = unittype_socket;
217 desc->type = unittype_service;
220 len = (size_t)(strchrnul(name, '\n') - name);
221 desc->name = strndup(name, len);
222 desc->name_length = desc->name ? len : 0;
224 desc->type = unittype_unknown;
226 desc->name_length = 0;
230 len = (size_t)(strchrnul(wanted, '\n') - wanted);
231 desc->wanted_by = strndup(wanted, len);
232 desc->wanted_by_length = len;
234 desc->wanted_by = NULL;
235 desc->wanted_by_length = 0;
238 desc->content = spec;
239 desc->content_length = pack(spec, '%');
245 * Processes all the units of the 'corpus'.
246 * Each unit of the corpus is separated and packed and its
247 * charactistics are stored in a descriptor.
248 * At the end if no error was found, calls the function 'process'
249 * with its given 'closure' and the array descripbing the units.
250 * Return 0 in case of success or a negative value in case of error.
252 static int process_all_units(char *corpus, const struct unitconf *conf, int (*process)(void *closure, const struct generatedesc *desc), void *closure)
256 struct unitdesc *units, *u;
257 struct generatedesc gdesc;
263 /* while there is a unit in the corpus */
264 while(offset(corpus, "%begin systemd-unit\n", &beg)) {
266 /* get the end of the unit */
267 end = offset(beg, "%end systemd-unit\n", &corpus);
269 /* unterminated unit !! */
270 ERROR("unterminated unit description!! %s", beg);
274 /* separate the unit from the corpus */
277 /* allocates a descriptor for the unit */
278 u = realloc(units, ((unsigned)n + 1) * sizeof *units);
282 /* creates the unit description */
285 memset(u, 0, sizeof *u);
286 rc2 = process_one_unit(beg, u);
291 /* records the error if there is an error */
298 /* call the function that processes the units */
299 if (rc == 0 && process) {
303 rc = process(closure, &gdesc);
306 /* cleanup and frees */
308 free((char *)(units[--n].name));
309 free((char *)(units[n].wanted_by));
317 * Clear the unit generator
319 void unit_generator_off()
326 * Initialises the unit generator with the content of the file of path 'filename'.
327 * Returns 0 in case of success or a negative number in case of error.
329 int unit_generator_on(const char *filename)
335 unit_generator_off();
336 rc = getfile(filename ? : FWK_UNIT_CONF, &template, NULL);
338 size = pack(template, ';');
339 tmp = realloc(template, 1 + size);
346 static int add_metadata(struct json_object *jdesc, const struct unitconf *conf)
350 sprintf(portstr, "%d", conf->port);
351 return j_add_many_strings_m(jdesc,
352 "#metadata.install-dir", conf->installdir,
353 "#metadata.app-data-dir", "%h/app-data",
354 "#metadata.icons-dir", conf->icondir,
355 "#metadata.http-port", portstr,
360 * Applies the object 'jdesc' augmented of meta data coming
361 * from 'conf' to the current unit generator.
362 * The current unit generator will be set to the default one if not unit
363 * was previously set using the function 'unit_generator_on'.
364 * The callback function 'process' is then called with the
365 * unit descriptors array and the expected closure.
366 * Return what returned process in case of success or a negative
369 int unit_generator_process(struct json_object *jdesc, const struct unitconf *conf, int (*process)(void *closure, const struct generatedesc *desc), void *closure)
375 rc = add_metadata(jdesc, conf);
377 ERROR("can't set the metadata. %m");
379 rc = template ? 0 : unit_generator_on(NULL);
382 rc = apply_mustach(template, jdesc, &instance, &size);
384 rc = process_all_units(instance, conf, process, closure);
391 /**************** SPECIALIZED PART *****************************/
393 static int check_unit_desc(const struct unitdesc *desc, int tells)
395 if (desc->scope != unitscope_unknown && desc->type != unittype_unknown && desc->name != NULL)
399 if (desc->scope == unitscope_unknown)
400 ERROR("unit of unknown scope");
401 if (desc->type == unittype_unknown)
402 ERROR("unit of unknown type");
403 if (desc->name == NULL)
404 ERROR("unit of unknown name");
410 static int get_unit_path(char *path, size_t pathlen, const struct unitdesc *desc)
412 int rc = systemd_get_unit_path(
413 path, pathlen, desc->scope == unitscope_user,
414 desc->name, desc->type == unittype_socket ? "socket" : "service");
417 ERROR("can't get the unit path for %s", desc->name);
422 static int get_wants_path(char *path, size_t pathlen, const struct unitdesc *desc)
424 int rc = systemd_get_wants_path(
425 path, pathlen, desc->scope == unitscope_user, desc->wanted_by,
426 desc->name, desc->type == unittype_socket ? "socket" : "service");
429 ERROR("can't get the wants path for %s and %s", desc->name, desc->wanted_by);
434 static int get_wants_target(char *path, size_t pathlen, const struct unitdesc *desc)
436 int rc = systemd_get_wants_target(
438 desc->name, desc->type == unittype_socket ? "socket" : "service");
441 ERROR("can't get the wants target for %s", desc->name);
446 static int do_send_reload(const struct generatedesc *desc)
449 int reloadsys, reloadusr;
450 const struct unitdesc *u;
452 reloadsys = reloadusr = 0;
453 for (i = 0 ; i < desc->nunits ; i++) {
455 if (u->wanted_by != NULL) {
460 case unitscope_system:
470 reloadusr = systemd_daemon_reload(1);
472 reloadsys = systemd_daemon_reload(0);
473 return reloadsys ? : reloadusr ? : 0;
476 static int do_uninstall_units(void *closure, const struct generatedesc *desc)
481 const struct unitdesc *u;
484 for (i = 0 ; i < desc->nunits ; i++) {
486 rc2 = check_unit_desc(u, 0);
488 rc2 = get_unit_path(path, sizeof path, u);
492 if (rc2 < 0 && rc == 0)
494 if (u->wanted_by != NULL) {
495 rc2 = get_wants_path(path, sizeof path, u);
500 if (rc2 < 0 && rc == 0)
503 rc2 = do_send_reload(desc);
504 if (rc2 < 0 && rc == 0)
509 static int do_install_units(void *closure, const struct generatedesc *desc)
513 char path[PATH_MAX + 1], target[PATH_MAX + 1];
514 const struct unitdesc *u;
517 while (i < desc->nunits) {
519 rc = check_unit_desc(u, 1);
521 rc = get_unit_path(path, sizeof path, u);
523 rc = putfile(path, u->content, u->content_length);
524 if (rc >= 0 && u->wanted_by != NULL) {
525 rc = get_wants_path(path, sizeof path, u);
527 rc = get_wants_target(target, sizeof target, u);
529 unlink(path); /* TODO? check? */
530 rc = symlink(target, path);
540 rc = do_send_reload(desc);
546 do_uninstall_units(closure, desc);
551 static int do_install_uninstall(
552 struct wgt_info *ifo,
553 const struct unitconf *conf,
554 int (*doer)(void *, const struct generatedesc *)
558 struct json_object *jdesc;
560 jdesc = wgt_info_to_json(ifo);
564 rc = unit_generator_process(jdesc, conf, doer, NULL);
565 json_object_put(jdesc);
570 int unit_install(struct wgt_info *ifo, const struct unitconf *conf)
572 return do_install_uninstall(ifo, conf, do_install_units);
575 int unit_uninstall(struct wgt_info *ifo, const struct unitconf *conf)
577 return do_install_uninstall(ifo, conf, do_uninstall_units);