X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fafm-udb.c;h=80cc7158f1dc0418d673ea79a9270993cb0dbdd6;hb=f551ac4966543b360e3458a61615626d71f55a6e;hp=e199ae1e349e834633f3c95a457bdc6d6891f9e3;hpb=a91edb8202d3436e9d94a4f2b90c759f8833f770;p=src%2Fapp-framework-main.git diff --git a/src/afm-udb.c b/src/afm-udb.c index e199ae1..80cc715 100644 --- a/src/afm-udb.c +++ b/src/afm-udb.c @@ -17,6 +17,7 @@ */ #include +#include #include #include #include @@ -33,7 +34,6 @@ #include "afm-udb.h" - static const char x_afm_prefix[] = "X-AFM-"; static const char service_extension[] = ".service"; static const char key_unit_path[] = "-unit-path"; @@ -224,6 +224,8 @@ static int addunit( { struct json_object *priv, *pub, *id; const char *strid; + char *un = NULL; + size_t len; /* create the application structure */ priv = json_object_new_object(); @@ -234,12 +236,30 @@ static int addunit( if (!pub) goto error; + /* make the unit name */ + len = strlen(unitname); + assert(len >= (sizeof service_extension - 1)); + assert(!memcmp(&unitname[len - (sizeof service_extension - 1)], service_extension, sizeof service_extension)); + if (unitname[len - sizeof service_extension] == '@') { + char buffer[40]; + size_t l = (size_t)snprintf(buffer, sizeof buffer, "%d", (int)getuid()); + un = malloc(len + l + 1); + if (!un) + goto error; + memcpy(&un[0], unitname, len - (sizeof service_extension - 1)); + if (l) + memcpy(&un[len - (sizeof service_extension - 1)], buffer, l); + memcpy(&un[len - (sizeof service_extension - 1) + l], service_extension, sizeof service_extension); + } + /* adds the values */ if (add_fields_of_content(priv, pub, content, length) || add_field(priv, pub, key_unit_path, unitpath) - || add_field(priv, pub, key_unit_name, unitname) + || add_field(priv, pub, key_unit_name, un ? : unitname) || add_field(priv, pub, key_unit_scope, isuser ? scope_user : scope_system)) goto error; + free(un); + un = NULL; /* get the id */ if (!json_object_object_get_ex(pub, key_id, &id)) { @@ -258,11 +278,71 @@ static int addunit( return 0; error: + free(un); json_object_put(pub); json_object_put(priv); return -1; } +/* + * read a unit file + */ +static int read_unit_file(const char *path, char **content, size_t *length) +{ + int rc, st; + char c, *read, *write; + + /* read the file */ + rc = getfile(path, content, length); + if (rc >= 0) { + /* removes any comment and join lines */ + st = 0; + read = write = *content; + for (;;) { + do { c = *read++; } while (c == '\r'); + if (!c) + break; + switch (st) { + case 0: + if (c == ';' || c == '#') { + st = 3; /* removes lines starting with ; or # */ + break; + } + if (c == '\n') + break; /* removes empty lines */ +enter_state_1: + st = 1; + /*@fallthrough@*/ + case 1: + if (c == '\\') + st = 2; + else { + *write++ = c; + if (c == '\n') + st = 0; + } + break; + case 2: + if (c == '\n') + c = ' '; + else + *write++ = '\\'; + goto enter_state_1; + case 3: + if (c == '\n') + st = 0; + break; + } + } + if (st == 1) + *write++ = '\n'; + *write = 0; + *length = (size_t)(write - *content); + *content = realloc(*content, *length + 1); + } + return rc; +} + /* * called for each unit */ @@ -284,14 +364,16 @@ static int update_cb(void *closure, const char *name, const char *path, int isus return 0; /* reads the file */ - rc = getfile(path, &content, &length); + rc = read_unit_file(path, &content, &length); if (rc < 0) - return rc; + return 0; /* process the file */ rc = addunit(&updt->applications, isuser, path, name, content, length); + /* TODO: if (rc < 0) + ERROR("Ignored boggus unit %s (error: %m)", path); */ free(content); - return rc; + return 0; } /*