+static int add_metadata(struct json_object *jdesc, const struct unitconf *conf)
+{
+ struct json_object *targets, *targ;
+ char portstr[30];
+ int port, 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 (!j_add_string_m(targ, "#metatarget.http-port", portstr))
+ return -1;
+ }
+ }
+
+ return j_add_many_strings_m(jdesc,
+ "#metadata.install-dir", conf->installdir,
+ "#metadata.icons-dir", conf->icondir,
+ NULL) ? 0 : -1;
+}
+