X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fwgtpkg-unit.c;h=e2813baf15f21cdaaeff314e4f7f73d6d6d8d751;hb=550ca0ae2bb9138c85fda941b67d4db1da1609ed;hp=26cf324571c19db9bbacfde054bed517402a85dd;hpb=14d4c3fe7e351c12f1cc4ec6246a23ae94dca7da;p=src%2Fapp-framework-main.git diff --git a/src/wgtpkg-unit.c b/src/wgtpkg-unit.c index 26cf324..e2813ba 100644 --- a/src/wgtpkg-unit.c +++ b/src/wgtpkg-unit.c @@ -25,11 +25,18 @@ #include #include #include +#include + +#include #include "verbose.h" #include "utils-file.h" #include "wgtpkg-mustach.h" +#include "utils-json.h" +#include "wgt-json.h" +#include "utils-systemd.h" + #include "wgtpkg-unit.h" #if 0 @@ -109,12 +116,13 @@ static size_t pack(char *text, char purge) char cont; /* flag telling whether the line continues the previous one */ char nextcont; /* flag telling whether the line will continues the next one */ - cont = 0; + nextcont = 0; c = *(write = read = text); /* iteration over lines */ while (c) { /* computes emit, nextcont, emit and start for the current line */ + cont = nextcont; emit = nextcont = 0; start = NULL; begin = read; @@ -133,7 +141,7 @@ static size_t pack(char *text, char purge) if (c) c = *++read; /* emit the line if not empty */ - if (emit) { + if (emit || (cont && !nextcont)) { /* removes the blanks on the left of not continuing lines */ if (!cont && start) begin = start; @@ -148,7 +156,6 @@ static size_t pack(char *text, char purge) *write++ = *begin++; } } - cont = nextcont; } *write = 0; return (size_t)(write - text); @@ -183,8 +190,8 @@ static char *offset(char *text, const char *pattern, char **args) */ static int process_one_unit(char *spec, struct unitdesc *desc) { - char *nsoc, *nsrv, *name; - int isuser, issystem, issock, isserv; + char *nsoc, *nsrv, *name, *wanted; + int isuser, issystem, issock, isserv, iswanted; size_t len; /* finds the configuration directive of the unit */ @@ -192,6 +199,7 @@ static int process_one_unit(char *spec, struct unitdesc *desc) issystem = !!offset(spec, "%systemd-unit system\n", NULL); issock = !!offset(spec, "%systemd-unit socket ", &nsoc); isserv = !!offset(spec, "%systemd-unit service ", &nsrv); + iswanted = !!offset(spec, "%systemd-unit wanted-by ", &wanted); /* check the unit scope */ if ((isuser + issystem) == 1) { @@ -218,6 +226,15 @@ static int process_one_unit(char *spec, struct unitdesc *desc) desc->name_length = 0; } + if (iswanted) { + len = (size_t)(strchrnul(wanted, '\n') - wanted); + desc->wanted_by = strndup(wanted, len); + desc->wanted_by_length = len; + } else { + desc->wanted_by = NULL; + desc->wanted_by_length = 0; + } + desc->content = spec; desc->content_length = pack(spec, '%'); @@ -282,8 +299,10 @@ static int process_all_units(char *corpus, int (*process)(void *closure, const s rc = process(closure, descs, n); /* cleanup and frees */ - while(n) + while(n) { free((char *)(descs[--n].name)); + free((char *)(descs[n].wanted_by)); + } free(descs); return rc; @@ -299,7 +318,7 @@ void unit_generator_off() } /* - * Initialises the unit generator with 'filename'. + * 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) @@ -345,3 +364,206 @@ int unit_generator_process(struct json_object *jdesc, int (*process)(void *closu return rc; } +/**************** SPECIALIZED PART *****************************/ + +static int check_unit_desc(const struct unitdesc *desc, int tells) +{ + if (desc->scope != unitscope_unknown && desc->type != unittype_unknown && desc->name != NULL) + return 0; + + if (tells) { + if (desc->scope == unitscope_unknown) + ERROR("unit of unknown scope"); + if (desc->type == unittype_unknown) + ERROR("unit of unknown type"); + if (desc->name == NULL) + ERROR("unit of unknown name"); + } + errno = EINVAL; + return -1; +} + +static int get_unit_path(char *path, size_t pathlen, const struct unitdesc *desc) +{ + int rc = systemd_get_unit_path( + path, pathlen, desc->scope == unitscope_user, + desc->name, desc->type == unittype_socket ? "socket" : "service"); + + if (rc < 0) + ERROR("can't get the unit path for %s", desc->name); + + return rc; +} + +static int get_wants_path(char *path, size_t pathlen, const struct unitdesc *desc) +{ + int rc = systemd_get_wants_path( + path, pathlen, desc->scope == unitscope_user, desc->wanted_by, + desc->name, desc->type == unittype_socket ? "socket" : "service"); + + if (rc < 0) + ERROR("can't get the wants path for %s and %s", desc->name, desc->wanted_by); + + return rc; +} + +static int get_wants_target(char *path, size_t pathlen, const struct unitdesc *desc) +{ + int rc = systemd_get_wants_target( + path, pathlen, + desc->name, desc->type == unittype_socket ? "socket" : "service"); + + if (rc < 0) + ERROR("can't get the wants target for %s", desc->name); + + return rc; +} + +static int do_send_reload(const struct unitdesc descs[], unsigned count) +{ + unsigned i; + int reloadsys, reloadusr; + + reloadsys = reloadusr = 0; + for (i = 0 ; i < count ; i++) { + if (descs[i].wanted_by != NULL) { + switch (descs[i].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 unitdesc descs[], unsigned count) +{ + int rc, rc2; + unsigned i; + char path[PATH_MAX]; + + rc = 0; + for (i = 0 ; i < count ; i++) { + rc2 = check_unit_desc(&descs[i], 0); + if (rc2 == 0) { + rc2 = get_unit_path(path, sizeof path, &descs[i]); + if (rc2 >= 0) { + rc2 = unlink(path); + } + if (rc2 < 0 && rc == 0) + rc = rc2; + if (descs[i].wanted_by != NULL) { + rc2 = get_wants_path(path, sizeof path, &descs[i]); + if (rc2 >= 0) + rc2 = unlink(path); + } + } + if (rc2 < 0 && rc == 0) + rc = rc2; + } + rc2 = do_send_reload(descs, count); + if (rc2 < 0 && rc == 0) + rc = rc2; + return rc; +} + +static int do_install_units(void *closure, const struct unitdesc descs[], unsigned count) +{ + int rc; + unsigned i; + char path[PATH_MAX + 1], target[PATH_MAX + 1]; + + i = 0; + while (i < count) { + rc = check_unit_desc(&descs[i], 1); + if (!rc) { + rc = get_unit_path(path, sizeof path, &descs[i]); + if (rc >= 0) { + rc = putfile(path, descs[i].content, descs[i].content_length); + if (descs[i].wanted_by != NULL) { + rc = get_wants_path(path, sizeof path, &descs[i]); + if (rc >= 0) { + rc = get_wants_target(target, sizeof target, &descs[i]); + if (rc >= 0) { + unlink(path); /* TODO? check? */ + rc = symlink(target, path); + } + } + } + i++; + } + } + if (rc < 0) + goto error; + } + rc = do_send_reload(descs, count); + if (rc < 0) + goto error; + return 0; +error: + do_uninstall_units(closure, descs, i); + return rc; +} + +static int add_metadata(struct json_object *jdesc, const char *installdir, const char *icondir, int port) +{ + char portstr[30]; + + sprintf(portstr, "%d", port); + return j_add_many_strings_m(jdesc, + "#metadata.install-dir", installdir, + "#metadata.app-data-dir", "%h/app-data", + "#metadata.icons-dir", icondir, + "#metadata.http-port", portstr, + NULL) ? 0 : -1; +} + +static int do_install_uninstall( + struct wgt_info *ifo, + const char *installdir, + const char *icondir, + int port, + int (*doer)(void *, const struct unitdesc[], unsigned) +) +{ + int rc; + struct json_object *jdesc; + + jdesc = wgt_info_to_json(ifo); + if (!jdesc) + rc = -1; + else { + rc = add_metadata(jdesc, installdir, icondir, port); + if (rc) + ERROR("can't set the metadata. %m"); + else { + rc = unit_generator_process(jdesc, doer, NULL); + if (rc) + ERROR("can't install units, error %d", rc); + } + json_object_put(jdesc); + } + return rc; +} + +int unit_install(struct wgt_info *ifo, const char *installdir, const char *icondir, int port) +{ + return do_install_uninstall(ifo, installdir, icondir, port, do_install_units); +} + +int unit_uninstall(struct wgt_info *ifo) +{ + return do_install_uninstall(ifo, "", "", 0, do_uninstall_units); +} +