X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fwgtpkg-unit.c;h=e01faa9116f3fe505da716b856a2922c1e453bde;hb=6008a3ec4d1c0ed5df338b7c5f0775585ab51cfb;hp=3f2095c3ede454959819e5bec8c69972e78453db;hpb=17f9cdadca63005fe075d999e49154342fdd5086;p=src%2Fapp-framework-main.git diff --git a/src/wgtpkg-unit.c b/src/wgtpkg-unit.c index 3f2095c..e01faa9 100644 --- a/src/wgtpkg-unit.c +++ b/src/wgtpkg-unit.c @@ -1,5 +1,5 @@ /* - Copyright 2016, 2017 IoT.bzh + Copyright (C) 2016-2019 IoT.bzh author: José Bollo @@ -28,7 +28,7 @@ #include #include - + #include "verbose.h" #include "utils-file.h" @@ -38,6 +38,7 @@ #include "utils-systemd.h" #include "wgtpkg-unit.h" +#include "wgt-strings.h" #if 0 #include @@ -53,8 +54,8 @@ static char *template; * Returns 1 if 'text' matches the 'pattern' or else returns 0. * When returning 1 and 'after' isn't NULL, the pointer to the * first character after the pettern in 'text' is stored in 'after'. - * The characters '\n' and ' ' have a special mening in the search: - * * '\n': matches any space or tabs (including none) followed + * The characters '\n' and ' ' have a special meaning in the search: + * * '\n': matches any space or tabs (including none) followed * either by '\n' or '\0' (end of the string) * * ' ': matches any space or tabs but at least one. */ @@ -217,7 +218,7 @@ static int process_one_unit(char *spec, struct unitdesc *desc) desc->type = unittype_service; name = nsrv; } - len = (size_t)(strchrnul(name, '\n') - name); + len = strcspn(name, " \t\n"); desc->name = strndup(name, len); desc->name_length = desc->name ? len : 0; } else { @@ -227,7 +228,7 @@ static int process_one_unit(char *spec, struct unitdesc *desc) } if (iswanted) { - len = (size_t)(strchrnul(wanted, '\n') - wanted); + len = strcspn(wanted, " \t\n"); desc->wanted_by = strndup(wanted, len); desc->wanted_by_length = len; } else { @@ -249,43 +250,75 @@ static int process_one_unit(char *spec, struct unitdesc *desc) * with its given 'closure' and the array descripbing the units. * Return 0 in case of success or a negative value in case of error. */ -static int process_all_units(char *corpus, const struct unitconf *conf, int (*process)(void *closure, const struct generatedesc *desc), void *closure) +static int process_all_units(char *corpus, const struct unitconf *conf, int (*process)(void *closure, const struct generatedesc *desc), void *closure, struct json_object *jdesc) { - int n, rc, rc2; - char *beg, *end; - struct unitdesc *units, *u; + int rc, rc2; + char *beg, *end, *befbeg, *aftend; + struct unitdesc *u; struct generatedesc gdesc; - units = NULL; - n = 0; + gdesc.conf = conf; + gdesc.desc = jdesc; + gdesc.units = NULL; + gdesc.nunits = 0; rc = rc2 = 0; /* while there is a unit in the corpus */ - while(offset(corpus, "%begin systemd-unit\n", &beg)) { - - /* get the end of the unit */ - end = offset(beg, "%end systemd-unit\n", &corpus); + for(;;) { + befbeg = offset(corpus, "%begin ", &beg); + end = offset(corpus, "%end ", &aftend); + if (!befbeg) { + if (end) { + /* %end detected without %begin */ + ERROR("unexpected %%end at end"); + rc = rc ? :-EINVAL; + } + break; + } if (!end) { /* unterminated unit !! */ - ERROR("unterminated unit description!! %s", beg); + ERROR("unterminated unit description!!"); corpus = beg; rc2 = -EINVAL; + } else if (end < befbeg) { + /* sequence %end ... %begin detected !! */ + ERROR("unexpected %%end before %%begin"); + corpus = aftend; + rc2 = -EINVAL; } else { - /* separate the unit from the corpus */ - *end = 0; - - /* allocates a descriptor for the unit */ - u = realloc(units, ((unsigned)n + 1) * sizeof *units); - if (u == NULL) - rc2 = -ENOMEM; - else { - /* creates the unit description */ - units = u; - u = &u[n]; - memset(u, 0, sizeof *u); - rc2 = process_one_unit(beg, u); - if (rc2 >= 0) - n++; + befbeg = offset(beg, "%begin ", NULL); + if (befbeg && befbeg < end) { + /* sequence %begin ... %begin ... %end detected !! */ + ERROR("unexpected %%begin after %%begin"); + corpus = beg; + rc2 = -EINVAL; + } else { + *end = 0; + corpus = aftend; + if (matches("systemd-unit\n", beg, &beg)) { + if (!matches("systemd-unit\n", aftend, &corpus)) { + /* end doesnt match */ + ERROR("unmatched %%begin systemd-unit (matching end mismatch)"); + rc2 = -EINVAL; + } else { + /* allocates a descriptor for the unit */ + u = realloc((void*)gdesc.units, ((unsigned)gdesc.nunits + 1) * sizeof *gdesc.units); + if (u == NULL) + rc2 = -ENOMEM; + else { + /* creates the unit description */ + gdesc.units = u; + u = &u[gdesc.nunits]; + memset(u, 0, sizeof *u); + rc2 = process_one_unit(beg, u); + if (rc2 >= 0) + gdesc.nunits++; + } + } + } else { + ERROR("unexpected %%begin name"); + rc2 = -EINVAL; + } } } /* records the error if there is an error */ @@ -296,19 +329,15 @@ static int process_all_units(char *corpus, const struct unitconf *conf, int (*pr } /* call the function that processes the units */ - if (rc == 0 && process) { - gdesc.conf = conf; - gdesc.units = units; - gdesc.nunits = n; + if (rc == 0 && process) rc = process(closure, &gdesc); - } /* cleanup and frees */ - while(n) { - free((char *)(units[--n].name)); - free((char *)(units[n].wanted_by)); + while(gdesc.nunits) { + free((void*)(gdesc.units[--gdesc.nunits].name)); + free((void*)(gdesc.units[gdesc.nunits].wanted_by)); } - free(units); + free((void*)gdesc.units); return rc; } @@ -316,7 +345,7 @@ static int process_all_units(char *corpus, const struct unitconf *conf, int (*pr /* * Clear the unit generator */ -void unit_generator_off() +void unit_generator_close_template() { free(template); template = NULL; @@ -326,13 +355,13 @@ void unit_generator_off() * Initialises the unit generator with the content of the file of path 'filename'. * Returns 0 in case of success or a negative number in case of error. */ -int unit_generator_on(const char *filename) +int unit_generator_open_template(const char *filename) { size_t size; char *tmp; int rc; - unit_generator_off(); + unit_generator_close_template(); rc = getfile(filename ? : FWK_UNIT_CONF, &template, NULL); if (!rc) { size = pack(template, ';'); @@ -345,14 +374,36 @@ int unit_generator_on(const char *filename) static int add_metadata(struct json_object *jdesc, const struct unitconf *conf) { - char portstr[30]; + struct json_object *targets, *targ; + char portstr[30], afidstr[30]; + int port, afid, i, n; + + if (json_object_object_get_ex(jdesc, string_targets, &targets)) { + n = json_object_array_length(targets); + for (i = 0 ; i < n ; i++) { + targ = json_object_array_get_idx(targets, i); + if (!conf->new_afid) { + afid = 0; + port = 0; + } else { + afid = conf->new_afid(); + if (afid < 0) + return afid; + port = conf->base_http_ports + afid; + } + sprintf(afidstr, "%d", afid); + sprintf(portstr, "%d", port); + if (!j_add_many_strings_m(targ, + "#metatarget.http-port", portstr, + "#metatarget.afid", afidstr, + NULL)) + return -1; + } + } - sprintf(portstr, "%d", conf->port); return j_add_many_strings_m(jdesc, "#metadata.install-dir", conf->installdir, - "#metadata.app-data-dir", "%h/app-data", "#metadata.icons-dir", conf->icondir, - "#metadata.http-port", portstr, NULL) ? 0 : -1; } @@ -360,7 +411,7 @@ static int add_metadata(struct json_object *jdesc, const struct unitconf *conf) * Applies the object 'jdesc' augmented of meta data coming * from 'conf' to the current unit generator. * The current unit generator will be set to the default one if not unit - * was previously set using the function 'unit_generator_on'. + * was previously set using the function 'unit_generator_open_template'. * The callback function 'process' is then called with the * unit descriptors array and the expected closure. * Return what returned process in case of success or a negative @@ -376,12 +427,12 @@ int unit_generator_process(struct json_object *jdesc, const struct unitconf *con if (rc) ERROR("can't set the metadata. %m"); else { - rc = template ? 0 : unit_generator_on(NULL); + rc = template ? 0 : unit_generator_open_template(NULL); if (!rc) { instance = NULL; rc = apply_mustach(template, jdesc, &instance, &size); if (!rc) - rc = process_all_units(instance, conf, process, closure); + rc = process_all_units(instance, conf, process, closure, jdesc); free(instance); } } @@ -443,36 +494,6 @@ static int get_wants_target(char *path, size_t pathlen, const struct unitdesc *d return rc; } -static int do_send_reload(const struct generatedesc *desc) -{ - int i; - int reloadsys, reloadusr; - const struct unitdesc *u; - - reloadsys = reloadusr = 0; - for (i = 0 ; i < desc->nunits ; i++) { - u = &desc->units[i]; - if (u->wanted_by != NULL) { - switch (u->scope) { - case unitscope_user: - reloadusr = 1; - break; - case unitscope_system: - reloadsys = 1; - break; - default: - break; - } - } - } - - if (reloadusr) - reloadusr = systemd_daemon_reload(1); - if (reloadsys) - reloadsys = systemd_daemon_reload(0); - return reloadsys ? : reloadusr ? : 0; -} - static int do_uninstall_units(void *closure, const struct generatedesc *desc) { int rc, rc2; @@ -500,9 +521,6 @@ static int do_uninstall_units(void *closure, const struct generatedesc *desc) if (rc2 < 0 && rc == 0) rc = rc2; } - rc2 = do_send_reload(desc); - if (rc2 < 0 && rc == 0) - rc = rc2; return rc; } @@ -537,9 +555,6 @@ static int do_install_units(void *closure, const struct generatedesc *desc) if (rc < 0) goto error; } - rc = do_send_reload(desc); - if (rc < 0) - goto error; return 0; error: i = errno;