X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fafm-urun.c;h=7f8ad166b8330ad4d046f68afa78b7986df42e89;hb=e7ac328451fa3b3edfbd3658a2365b75d41c0698;hp=0249a6077dcc13fc0081b678eb4602ba645379ab;hpb=63c180a63657bc7dd55bab423ea0c2751c87310b;p=src%2Fapp-framework-main.git diff --git a/src/afm-urun.c b/src/afm-urun.c index 0249a60..7f8ad16 100644 --- a/src/afm-urun.c +++ b/src/afm-urun.c @@ -1,5 +1,5 @@ /* - Copyright (C) 2015-2018 IoT.bzh + Copyright (C) 2015-2020 IoT.bzh author: José Bollo @@ -40,7 +40,7 @@ static const char key_unit_d_path[] = "-unit-dpath-"; /**************** get appli basis *********************/ -static int get_basis(struct json_object *appli, int *isuser, const char **dpath, int load, int uid) +static int get_basis(struct json_object *appli, int *isuser, const char **dpath, int uid) { char userid[40]; char *dp, *arodot, *nun; @@ -76,12 +76,6 @@ static int get_basis(struct json_object *appli, int *isuser, const char **dpath, return 0; } - /* not here. load it? */ - if (!load) { - errno = ENOENT; - goto error; - } - /* get uname */ if (!j_read_string_at(appli, "unit-name", &uname)) { ERROR("'unit-name' missing in appli description %s", json_object_get_string(appli)); @@ -152,15 +146,18 @@ error: static const char *wait_state_stable(int isuser, const char *dpath) { - const char *state; + int trial, count; + const char *state = NULL; - for (;;) { + count = 10; + for (trial = 1 ; trial <= count ; trial++) { state = systemd_unit_state_of_dpath(isuser, dpath); if (state == NULL || state == SysD_State_Active || state == SysD_State_Failed) return state; - /* TODO: sleep */ + sleep(1); } + return state; } /* @@ -246,7 +243,7 @@ int afm_urun_once(struct json_object *appli, int uid) int rc, isuser; /* retrieve basis */ - rc = get_basis(appli, &isuser, &udpath, 1, uid); + rc = get_basis(appli, &isuser, &udpath, uid); if (rc < 0) goto error; @@ -274,13 +271,13 @@ int afm_urun_once(struct json_object *appli, int uid) } rc = systemd_unit_pid_of_dpath(isuser, udpath); - if (rc < 0) { + if (rc <= 0) { j_read_string_at(appli, "unit-scope", &uscope); j_read_string_at(appli, "unit-name", &uname); ERROR("can't getpid of %s unit %s for uid %d: %m", uscope, uname, uid); goto error; } - + return rc; error: @@ -332,7 +329,7 @@ int afm_urun_resume(int runid, int uid) * * Returns the list or NULL in case of error. */ -struct json_object *afm_urun_list(struct afm_udb *db, int uid) +struct json_object *afm_urun_list(struct afm_udb *db, int all, int uid) { int i, n, isuser, pid; const char *udpath; @@ -348,11 +345,11 @@ struct json_object *afm_urun_list(struct afm_udb *db, int uid) if (result == NULL) goto error; - apps = afm_udb_applications_private(db, uid); + apps = afm_udb_applications_private(db, all, uid); n = json_object_array_length(apps); for (i = 0 ; i < n ; i++) { appli = json_object_array_get_idx(apps, i); - if (appli && get_basis(appli, &isuser, &udpath, 0, uid) >= 0) { + if (appli && get_basis(appli, &isuser, &udpath, uid) >= 0) { pid = systemd_unit_pid_of_dpath(isuser, udpath); if (pid > 0 && j_read_string_at(appli, "id", &id)) { state = systemd_unit_state_of_dpath(isuser, udpath); @@ -399,17 +396,17 @@ struct json_object *afm_urun_state(struct afm_udb *db, int runid, int uid) WARNING("searched runid %d not found", runid); } else { /* search in the base */ - apps = afm_udb_applications_private(db, uid); + apps = afm_udb_applications_private(db, 1, uid); n = json_object_array_length(apps); for (i = 0 ; i < n ; i++) { appli = json_object_array_get_idx(apps, i); if (appli - && get_basis(appli, &isuser, &udpath, 0, uid) >= 0 + && get_basis(appli, &isuser, &udpath, uid) >= 0 && !strcmp(dpath, udpath) && j_read_string_at(appli, "id", &id)) { pid = systemd_unit_pid_of_dpath(isuser, udpath); state = systemd_unit_state_of_dpath(isuser, dpath); - if (state == SysD_State_Active) + if (pid > 0 && state == SysD_State_Active) result = mkstate(id, runid, pid, state); goto end; } @@ -418,7 +415,7 @@ struct json_object *afm_urun_state(struct afm_udb *db, int runid, int uid) WARNING("searched runid %d of dpath %s isn't an applications", runid, dpath); end: json_object_put(apps); - free(dpath); + free(dpath); } return result; @@ -439,10 +436,14 @@ int afm_urun_search_runid(struct afm_udb *db, const char *id, int uid) NOTICE("Unknown appid %s", id); errno = ENOENT; pid = -1; - } else if (get_basis(appli, &isuser, &udpath, 0, uid) < 0) { + } else if (get_basis(appli, &isuser, &udpath, uid) < 0) { pid = -1; } else { pid = systemd_unit_pid_of_dpath(isuser, udpath); + if (pid == 0) { + errno = ESRCH; + pid = -1; + } } return pid; }