X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fwgtpkg-unit.c;h=e2813baf15f21cdaaeff314e4f7f73d6d6d8d751;hb=550ca0ae2bb9138c85fda941b67d4db1da1609ed;hp=9e6a1e23ac57049b63c24a546c018f88679733f2;hpb=cdd05aa3e66a876dba01f04173d817b9e5e039a0;p=src%2Fapp-framework-main.git diff --git a/src/wgtpkg-unit.c b/src/wgtpkg-unit.c index 9e6a1e2..e2813ba 100644 --- a/src/wgtpkg-unit.c +++ b/src/wgtpkg-unit.c @@ -35,13 +35,10 @@ #include "wgtpkg-mustach.h" #include "utils-json.h" #include "wgt-json.h" +#include "utils-systemd.h" #include "wgtpkg-unit.h" -#if !defined(SYSTEMD_UNITS_ROOT) -# define SYSTEMD_UNITS_ROOT "/usr/local/lib/systemd" -#endif - #if 0 #include #else @@ -119,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; @@ -143,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; @@ -158,7 +156,6 @@ static size_t pack(char *text, char purge) *write++ = *begin++; } } - cont = nextcont; } *write = 0; return (size_t)(write - text); @@ -388,79 +385,97 @@ static int check_unit_desc(const struct unitdesc *desc, int tells) static int get_unit_path(char *path, size_t pathlen, const struct unitdesc *desc) { - int rc; + int rc = systemd_get_unit_path( + path, pathlen, desc->scope == unitscope_user, + desc->name, desc->type == unittype_socket ? "socket" : "service"); - rc = snprintf(path, pathlen, "%s/%s/%s.%s", - SYSTEMD_UNITS_ROOT, - desc->scope == unitscope_system ? "system" : "user", - desc->name, - desc->type == unittype_socket ? "socket" : "service"); + if (rc < 0) + ERROR("can't get the unit path for %s", desc->name); - if (rc >= 0 && (size_t)rc >= pathlen) { - ERROR("can't set the unit path"); - errno = EINVAL; - rc = -1; - } return rc; } static int get_wants_path(char *path, size_t pathlen, const struct unitdesc *desc) { - int rc; + int rc = systemd_get_wants_path( + path, pathlen, desc->scope == unitscope_user, desc->wanted_by, + desc->name, desc->type == unittype_socket ? "socket" : "service"); - rc = snprintf(path, pathlen, "%s/%s/%s.wants/%s.%s", - SYSTEMD_UNITS_ROOT, - desc->scope == unitscope_system ? "system" : "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); - if (rc >= 0 && (size_t)rc >= pathlen) { - ERROR("can't set the wants path"); - errno = EINVAL; - rc = -1; - } return rc; } static int get_wants_target(char *path, size_t pathlen, const struct unitdesc *desc) { - int rc; + int rc = systemd_get_wants_target( + path, pathlen, + desc->name, desc->type == unittype_socket ? "socket" : "service"); - rc = snprintf(path, pathlen, "../%s.%s", - desc->name, - desc->type == unittype_socket ? "socket" : "service"); + if (rc < 0) + ERROR("can't get the wants target for %s", desc->name); - if (rc >= 0 && (size_t)rc >= pathlen) { - ERROR("can't set the wants target"); - errno = EINVAL; - rc = -1; - } 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++) { - rc = check_unit_desc(&descs[i], 0); - if (rc == 0) { - rc = get_unit_path(path, sizeof path, &descs[i]); - if (rc >= 0) { - rc = unlink(path); + 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); - rc = rc < 0 ? rc : rc2; } } + if (rc2 < 0 && rc == 0) + rc = rc2; } - return 0; + 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) @@ -489,12 +504,16 @@ static int do_install_units(void *closure, const struct unitdesc descs[], unsign i++; } } - if (rc < 0) { - do_uninstall_units(closure, descs, i); - return rc; - } + 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)