2 Copyright (C) 2015-2018 IoT.bzh
4 author: José Bollo <jose.bollo@iot.bzh>
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
27 #include <sys/types.h>
29 #include <json-c/json.h>
31 #include "utils-json.h"
32 #include "utils-systemd.h"
33 #include "utils-file.h"
37 static const char x_afm_prefix[] = "X-AFM-";
38 static const char service_extension[] = ".service";
39 static const char key_unit_path[] = "-unit-path";
40 static const char key_unit_name[] = "-unit-name";
41 static const char key_unit_scope[] = "-unit-scope";
42 static const char scope_user[] = "user";
43 static const char scope_system[] = "system";
44 static const char key_id[] = "id";
46 #define x_afm_prefix_length (sizeof x_afm_prefix - 1)
47 #define service_extension_length (sizeof service_extension - 1)
50 * The structure afm_apps records the data about applications
51 * for several accesses.
54 struct json_object *prvarr; /* array of the private data of apps */
55 struct json_object *pubarr; /* array of the public data of apps */
56 struct json_object *pubobj; /* hash of application's publics */
57 struct json_object *prvobj; /* hash of application's privates */
61 * The structure afm_udb records the applications
62 * for a set of directories recorded as a linked list
65 struct afm_apps applications; /* the data about applications */
66 int refcount; /* count of references to the structure */
67 int system; /* is managing system units? */
68 int user; /* is managing user units? */
69 size_t prefixlen; /* length of the prefix */
70 char prefix[1]; /* filtering prefix */
74 * The structure afm_updt is internally used for updates
77 struct afm_udb *afudb;
78 struct afm_apps applications;
82 * The default language
84 static char *default_lang;
87 * Release the data of the afm_apps object 'apps'.
89 static void apps_put(struct afm_apps *apps)
91 json_object_put(apps->prvarr);
92 json_object_put(apps->pubarr);
93 json_object_put(apps->pubobj);
94 json_object_put(apps->prvobj);
98 * Append the field 'data' to the field 'name' of the 'object'.
99 * When a second append is done to one field, it is automatically
100 * transformed to an array.
101 * Return 0 in case of success or -1 in case of error.
103 static int append_field(
104 struct json_object *object,
106 struct json_object *data
109 struct json_object *item, *array;
111 if (!json_object_object_get_ex(object, name, &item))
112 json_object_object_add(object, name, data);
114 if (json_object_is_type(item, json_type_array))
117 array = json_object_new_array();
120 json_object_array_add(array, item);
121 json_object_object_add(object, name, array);
123 json_object_array_add(array, data);
127 json_object_put(data);
133 * Adds the field of 'name' and 'value' in 'priv' and also if possible in 'pub'
134 * Returns 0 on success or -1 on error.
136 static int add_field(
137 struct json_object *priv,
138 struct json_object *pub,
145 struct json_object *v;
147 /* try to adapt the value to its type */
149 ival = strtol(value, &end, 10);
150 if (*value && !*end && !errno) {
152 v = json_object_new_int64(ival);
155 v = json_object_new_string(value);
163 if (name[0] == '-') {
164 append_field(priv, &name[1], v);
166 append_field(priv, name, json_object_get(v));
167 append_field(pub, name, v);
173 * Adds the field of 'name' and 'value' in 'priv' and also if possible in 'pub'
174 * Returns 0 on success or -1 on error.
176 static int add_fields_of_content(
177 struct json_object *priv,
178 struct json_object *pub,
183 char *name, *value, *read, *write;
185 read = strstr(content, x_afm_prefix);
187 name = read + x_afm_prefix_length;
188 value = strchr(name, '=');
190 read = strstr(name, x_afm_prefix);
193 read = write = value;
194 while(*read && *read != '\n') {
199 case 'n': *write++ = '\n'; break;
200 case '\n': *write++ = ' '; break;
201 default: *write++ = '\\'; *write++ = *read; break;
206 read = strstr(read, x_afm_prefix);
208 if (add_field(priv, pub, name, value) < 0)
216 * Adds the application widget 'desc' of the directory 'path' to the
217 * afm_apps object 'apps'.
218 * Returns 0 in case of success.
219 * Returns -1 and set errno in case of error
222 struct afm_apps *apps,
224 const char *unitpath,
225 const char *unitname,
230 struct json_object *priv, *pub, *id;
234 /* create the application structure */
235 priv = json_object_new_object();
239 pub = json_object_new_object();
243 /* make the unit name */
244 len = strlen(unitname);
245 assert(len >= (sizeof service_extension - 1));
246 assert(!memcmp(&unitname[len - (sizeof service_extension - 1)], service_extension, sizeof service_extension));
248 /* adds the values */
249 if (add_fields_of_content(priv, pub, content, length)
250 || add_field(priv, pub, key_unit_path, unitpath)
251 || add_field(priv, pub, key_unit_name, unitname)
252 || add_field(priv, pub, key_unit_scope, isuser ? scope_user : scope_system))
256 if (!json_object_object_get_ex(pub, key_id, &id)) {
260 strid = json_object_get_string(id);
262 /* record the application structure */
263 json_object_get(pub);
264 json_object_array_add(apps->pubarr, pub);
265 json_object_object_add(apps->pubobj, strid, pub);
266 json_object_get(priv);
267 json_object_array_add(apps->prvarr, priv);
268 json_object_object_add(apps->prvobj, strid, priv);
272 json_object_put(pub);
273 json_object_put(priv);
280 static int read_unit_file(const char *path, char **content, size_t *length)
283 char c, *read, *write;
286 rc = getfile(path, content, length);
288 /* removes any comment and join continued lines */
290 read = write = *content;
292 do { c = *read++; } while (c == '\r');
297 /* state 0: begin of a line */
298 if (c == ';' || c == '#') {
299 st = 3; /* removes lines starting with ; or # */
303 break; /* removes empty lines */
308 /* state 1: emitting a normal line */
318 /* state 2: character after '\' */
325 /* state 3: inside a comment, wait its end */
334 *length = (size_t)(write - *content);
335 *content = realloc(*content, *length + 1);
341 * called for each unit
343 static int update_cb(void *closure, const char *name, const char *path, int isuser)
345 struct afm_updt *updt = closure;
350 /* prefix filtering */
351 length = updt->afudb->prefixlen;
352 if (length && strncmp(updt->afudb->prefix, name, length))
356 length = strlen(name);
357 if (length < service_extension_length || strcmp(service_extension, name + length - service_extension_length))
361 rc = read_unit_file(path, &content, &length);
365 /* process the file */
366 rc = addunit(&updt->applications, isuser, path, name, content, length);
368 ERROR("Ignored boggus unit %s (error: %m)", path); */
374 * Creates an afm_udb object and returns it with one reference added.
375 * Return NULL with errno = ENOMEM if memory exhausted.
377 struct afm_udb *afm_udb_create(int sys, int usr, const char *prefix)
380 struct afm_udb *afudb;
382 length = prefix ? strlen(prefix) : 0;
383 afudb = malloc(length + sizeof * afudb);
388 afudb->applications.prvarr = NULL;
389 afudb->applications.pubarr = NULL;
390 afudb->applications.pubobj = NULL;
391 afudb->applications.prvobj = NULL;
394 afudb->prefixlen = length;
396 memcpy(afudb->prefix, prefix, length);
397 afudb->prefix[length] = 0;
398 if (afm_udb_update(afudb) < 0) {
399 afm_udb_unref(afudb);
407 * Adds a reference to an existing afm_udb.
409 void afm_udb_addref(struct afm_udb *afudb)
416 * Removes a reference to an existing afm_udb object.
417 * Removes the objet if there no more reference to it.
419 void afm_udb_unref(struct afm_udb *afudb)
422 if (!--afudb->refcount) {
423 /* no more reference, clean the memory used by the object */
424 apps_put(&afudb->applications);
430 * Regenerate the list of applications of the afm_bd object 'afudb'.
431 * Returns 0 in case of success.
432 * Returns -1 and set errno in case of error
434 int afm_udb_update(struct afm_udb *afudb)
436 struct afm_updt updt;
440 afm_udb_addref(afudb);
443 /* create the result */
444 updt.applications.prvarr = json_object_new_array();
445 updt.applications.pubarr = json_object_new_array();
446 updt.applications.pubobj = json_object_new_object();
447 updt.applications.prvobj = json_object_new_object();
448 if (updt.applications.pubarr == NULL
449 || updt.applications.prvarr == NULL
450 || updt.applications.pubobj == NULL
451 || updt.applications.prvobj == NULL) {
458 if (systemd_unit_list(1, update_cb, &updt) < 0)
461 if (systemd_unit_list(0, update_cb, &updt) < 0)
464 /* commit the result */
465 tmp = afudb->applications;
466 afudb->applications = updt.applications;
468 afm_udb_addref(afudb);
472 apps_put(&updt.applications);
473 afm_udb_addref(afudb);
477 void afm_udb_set_default_lang(const char *lang)
479 char *oldval = default_lang;
480 default_lang = lang ? strdup(lang) : NULL;
485 * Get the list of the applications private data of the afm_udb object 'afudb'.
486 * The list is returned as a JSON-array that must be released using
488 * Returns NULL in case of error.
490 struct json_object *afm_udb_applications_private(struct afm_udb *afudb, int uid)
492 return json_object_get(afudb->applications.prvarr);
496 * Get the list of the applications public data of the afm_udb object 'afudb'.
497 * The list is returned as a JSON-array that must be released using
499 * Returns NULL in case of error.
501 struct json_object *afm_udb_applications_public(struct afm_udb *afudb, int uid, const char *lang)
503 return json_object_get(afudb->applications.pubarr);
507 * Get the private data of the applications of 'id' in the afm_udb object 'afudb'.
508 * It returns a JSON-object that must be released using 'json_object_put'.
509 * Returns NULL in case of error.
511 static struct json_object *get_no_case(struct json_object *object, const char *id, int uid, const char *lang)
513 struct json_object *result;
514 struct json_object_iter i;
516 /* search case sensitively */
517 if (json_object_object_get_ex(object, id, &result))
518 return json_object_get(result);
520 /* fallback to a case insensitive search */
521 json_object_object_foreachC(object, i) {
522 if (!strcasecmp(i.key, id))
523 return json_object_get(i.val);
529 * Get the private data of the applications of 'id' in the afm_udb object 'afudb'.
530 * It returns a JSON-object that must be released using 'json_object_put'.
531 * Returns NULL in case of error.
533 struct json_object *afm_udb_get_application_private(struct afm_udb *afudb, const char *id, int uid)
535 return get_no_case(afudb->applications.prvobj, id, uid, NULL);
539 * Get the public data of the applications of 'id' in the afm_udb object 'afudb'.
540 * It returns a JSON-object that must be released using 'json_object_put'.
541 * Returns NULL in case of error.
543 struct json_object *afm_udb_get_application_public(struct afm_udb *afudb,
544 const char *id, int uid, const char *lang)
546 return get_no_case(afudb->applications.pubobj, id, uid, lang);
551 #if defined(TESTAPPFWK)
555 struct afm_udb *afudb = afm_udb_create(1, 1, NULL);
556 printf("array = %s\n", json_object_to_json_string_ext(afudb->applications.pubarr, 3));
557 printf("pubobj = %s\n", json_object_to_json_string_ext(afudb->applications.pubobj, 3));
558 printf("prvobj = %s\n", json_object_to_json_string_ext(afudb->applications.prvobj, 3));