From 6008a3ec4d1c0ed5df338b7c5f0775585ab51cfb Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Bollo?= Date: Tue, 3 Dec 2019 15:02:57 +0100 Subject: [PATCH] Enforce numeric application IDs MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This change allows to index applications numerically. This can be used for various purposes. One of it is to compute the HTTP port. Bug-AGL: SPEC-2968 Change-Id: I74531781f3a39d5d4b09eeb907f57f36822e38f0 Signed-off-by: José Bollo --- conf/unit/macros.inc | 2 + conf/unit/service.inc | 2 +- src/tests/test-unit/test-unit.c | 7 ++-- src/wgtpkg-install.c | 93 +++++++++++++++++++++-------------------- src/wgtpkg-uninstall.c | 3 +- src/wgtpkg-unit.c | 26 +++++++----- src/wgtpkg-unit.h | 3 +- 7 files changed, 75 insertions(+), 61 deletions(-) diff --git a/conf/unit/macros.inc b/conf/unit/macros.inc index ca72b95..f21dee5 100644 --- a/conf/unit/macros.inc +++ b/conf/unit/macros.inc @@ -44,6 +44,7 @@ define( `IF_NOT_VALUE', `MUSTACH_IF_NOT(value=$1)') -- use TARGET={{:id}}--{{:ver}}--{{:#target}} -- instead of TARGET={{idaver}}{{^#target=main}}@{{:#target}}{{/#target=main}} -------------------------------------------------------------------------------- +define( `AFID', `{{:#metatarget.afid}}') define( `SMACKLABEL', `User::App::{{:id}}') define( `TARGET', `{{idaver}}{{^#target=main}}@{{:#target}}{{/#target=main}}') define( `NEWTARGET', `{{:id}}--{{:ver}}--{{:#target}}') @@ -74,6 +75,7 @@ define( `PLATFORM_API_PATH_LINK', `PLATFORM_API_PATH/link') define( `USER_API_PATH', `USER_RUN_DIR/apis') define( `USER_API_PATH_WS', `USER_API_PATH/ws') define( `USER_API_PATH_LINK', `USER_API_PATH/link') + -------------------------------------------------------------------------------- -- AGL_DEVEL SPECIFIC PARTS -------------------------------------------------------------------------------- diff --git a/conf/unit/service.inc b/conf/unit/service.inc index 64e6e5f..0036cae 100644 --- a/conf/unit/service.inc +++ b/conf/unit/service.inc @@ -28,7 +28,7 @@ X-AFM-height={{height}} {{#icon}} X-AFM-icon={{:#metadata.install-dir}}/{{:src}} {{/icon}} -X-AFM--ID={{:id}} +X-AFM--ID=AFID X-AFM--target-name={{:#target}} X-AFM--content={{content.src}} X-AFM--type={{content.type}} diff --git a/src/tests/test-unit/test-unit.c b/src/tests/test-unit/test-unit.c index 590907d..a879a12 100644 --- a/src/tests/test-unit/test-unit.c +++ b/src/tests/test-unit/test-unit.c @@ -65,9 +65,9 @@ puts(json_object_to_json_string_ext(desc->desc, JSON_C_TO_STRING_PRETTY)); return 0; } -static int port() +static int new_afid() { - static int r = 10000; + static int r = 1; return r++; } @@ -79,7 +79,8 @@ int main(int ac, char **av) conf.installdir = "INSTALL-DIR"; conf.icondir = "ICONS-DIR"; - conf.port = port; + conf.new_afid = new_afid; + conf.base_http_ports = 20000; rc = unit_generator_open_template(*++av); if (rc < 0) error("can't read template %s: %m",*av); diff --git a/src/wgtpkg-install.c b/src/wgtpkg-install.c index 0122eda..9c148c7 100644 --- a/src/wgtpkg-install.c +++ b/src/wgtpkg-install.c @@ -53,19 +53,21 @@ static const char* exec_type_strings[] = { }; static const char key_afm_prefix[] = "X-AFM-"; -static const char key_http_port[] = "http-port"; +static const char key_afid[] = "-ID"; -#define HTTP_PORT_MIN 31000 -#define HTTP_PORT_MAX 32759 -#define HTTP_PORT_IS_VALID(port) (HTTP_PORT_MIN <= (port) && (port) <= HTTP_PORT_MAX) -#define HTTP_PORT_COUNT (HTTP_PORT_MAX - HTTP_PORT_MIN + 1) -#define HTTP_PORT_ACNT ((HTTP_PORT_COUNT + 31) >> 5) -#define HTTP_PORT_ASFT(port) (((port) - HTTP_PORT_MIN) & 31) -#define HTTP_PORT_AIDX(port) (((port) - HTTP_PORT_MIN) >> 5) -#define HTTP_PORT_TEST(array,port) ((((array)[HTTP_PORT_AIDX(port)]) >> HTTP_PORT_ASFT(port)) & 1) -#define HTTP_PORT_SET(array,port) (((array)[HTTP_PORT_AIDX(port)]) |= (((uint32_t)1) << HTTP_PORT_ASFT(port))) +#define HTTP_PORT_BASE 30000 -static uint32_t *port_bits = NULL; +#define AFID_MIN 1 +#define AFID_MAX 1999 +#define AFID_IS_VALID(afid) (AFID_MIN <= (afid) && (afid) <= AFID_MAX) +#define AFID_COUNT (AFID_MAX - AFID_MIN + 1) +#define AFID_ACNT ((AFID_COUNT + 31) >> 5) +#define AFID_ASFT(afid) (((afid) - AFID_MIN) & 31) +#define AFID_AIDX(afid) (((afid) - AFID_MIN) >> 5) +#define AFID_TEST(array,afid) ((((array)[AFID_AIDX(afid)]) >> AFID_ASFT(afid)) & 1) +#define AFID_SET(array,afid) (((array)[AFID_AIDX(afid)]) |= (((uint32_t)1) << AFID_ASFT(afid))) + +static uint32_t *afids_array = NULL; static const char *default_permissions[] = { "urn:AGL:token:valid" @@ -103,7 +105,7 @@ static void normalize_unit_file(char *content) *write = c; } -static int get_port_cb(void *closure, const char *name, const char *path, int isuser) +static int get_afid_cb(void *closure, const char *name, const char *path, int isuser) { char *iter; char *content; @@ -124,15 +126,15 @@ static int get_port_cb(void *closure, const char *name, const char *path, int is iter += sizeof key_afm_prefix - 1; if (*iter == '-') iter++; - if (!strncmp(iter, key_http_port, sizeof key_http_port - 1)) { - iter += sizeof key_http_port - 1; + if (!strncmp(iter, key_afid, sizeof key_afid - 1)) { + iter += sizeof key_afid - 1; while(*iter && *iter != '=' && *iter != '\n') iter++; if (*iter == '=') { while(*++iter == ' '); p = atoi(iter); - if (HTTP_PORT_IS_VALID(p)) - HTTP_PORT_SET((uint32_t*)closure, p); + if (AFID_IS_VALID(p)) + AFID_SET((uint32_t*)closure, p); } } iter = strstr(iter, key_afm_prefix); @@ -141,53 +143,53 @@ static int get_port_cb(void *closure, const char *name, const char *path, int is return 0; } -static int update_portbits(uint32_t *portbits) +static int update_afids(uint32_t *afids) { int rc; - memset(portbits, 0, HTTP_PORT_ACNT * sizeof(uint32_t)); - rc = systemd_unit_list(0, get_port_cb, portbits); + memset(afids, 0, AFID_ACNT * sizeof(uint32_t)); + rc = systemd_unit_list(0, get_afid_cb, afids); if (rc >= 0) - rc = systemd_unit_list(1, get_port_cb, portbits); + rc = systemd_unit_list(1, get_afid_cb, afids); if (rc < 0) - ERROR("troubles while updating ports"); + ERROR("troubles while updating afids"); return rc; } -static int first_free_port(uint32_t *portbits) +static int first_free_afid(uint32_t *afids) { - int port; - - port = HTTP_PORT_MIN; - while (port <= HTTP_PORT_MAX && !~portbits[HTTP_PORT_AIDX(port)]) - port += 32; - while (port <= HTTP_PORT_MAX && HTTP_PORT_TEST(portbits, port)) - port++; - if (port > HTTP_PORT_MAX) { - ERROR("Can't compute a valid port"); + int afid; + + afid = AFID_MIN; + while (afid <= AFID_MAX && !~afids[AFID_AIDX(afid)]) + afid += 32; + while (afid <= AFID_MAX && AFID_TEST(afids, afid)) + afid++; + if (afid > AFID_MAX) { + ERROR("Can't compute a valid afid"); errno = EADDRNOTAVAIL; - port = -1; + afid = -1; } - return port; + return afid; } -static int get_port() +static int get_new_afid() { - int port; + int afid; - /* ensure existing port bitmap */ - if (port_bits == NULL) { - port_bits = malloc(HTTP_PORT_ACNT * sizeof(uint32_t)); - if (port_bits == NULL || update_portbits(port_bits) < 0) + /* ensure existing afid bitmap */ + if (afids_array == NULL) { + afids_array = malloc(AFID_ACNT * sizeof(uint32_t)); + if (afids_array == NULL || update_afids(afids_array) < 0) return -1; } - /* allocates the port */ - port = first_free_port(port_bits); - if (port >= 0) - HTTP_PORT_SET(port_bits, port); + /* allocates the afid */ + afid = first_free_afid(afids_array); + if (afid >= 0) + AFID_SET(afids_array, afid); - return port; + return afid; } static int check_defined(const void *data, const char *name) @@ -612,7 +614,8 @@ struct wgt_info *install_widget(const char *wgtfile, const char *root, int force uconf.installdir = installdir; uconf.icondir = FWK_ICON_DIR; - uconf.port = get_port; + uconf.new_afid = get_new_afid; + uconf.base_http_ports = HTTP_PORT_BASE; if (unit_install(ifo, &uconf)) goto error4; diff --git a/src/wgtpkg-uninstall.c b/src/wgtpkg-uninstall.c index aa3faac..f435838 100644 --- a/src/wgtpkg-uninstall.c +++ b/src/wgtpkg-uninstall.c @@ -72,7 +72,8 @@ int uninstall_widget(const char *idaver, const char *root) } uconf.installdir = path; uconf.icondir = FWK_ICON_DIR; - uconf.port = 0; + uconf.new_afid = 0; + uconf.base_http_ports = 0; unit_uninstall(ifo, &uconf); wgt_info_unref(ifo); diff --git a/src/wgtpkg-unit.c b/src/wgtpkg-unit.c index ab0c933..e01faa9 100644 --- a/src/wgtpkg-unit.c +++ b/src/wgtpkg-unit.c @@ -375,22 +375,28 @@ int unit_generator_open_template(const char *filename) static int add_metadata(struct json_object *jdesc, const struct unitconf *conf) { struct json_object *targets, *targ; - char portstr[30]; - int port, i, n; + 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->port) - strcpy(portstr, "0"); - else { - port = conf->port ? conf->port() : 0; - if (port < 0) - return port; - sprintf(portstr, "%d", port); + if (!conf->new_afid) { + afid = 0; + port = 0; + } else { + afid = conf->new_afid(); + if (afid < 0) + return afid; + port = conf->base_http_ports + afid; } - if (!j_add_string_m(targ, "#metatarget.http-port", portstr)) + 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; } } diff --git a/src/wgtpkg-unit.h b/src/wgtpkg-unit.h index 796b277..a9c2709 100644 --- a/src/wgtpkg-unit.h +++ b/src/wgtpkg-unit.h @@ -47,7 +47,8 @@ struct unitdesc { struct unitconf { const char *installdir; const char *icondir; - int (*port)(); + int (*new_afid)(); + int base_http_ports; }; struct generatedesc { -- 2.16.6