afm-urun: Fix infinite loop on start status
[src/app-framework-main.git] / src / afm-urun.c
index 8c21b2a..7f8ad16 100644 (file)
@@ -1,5 +1,5 @@
 /*
- Copyright (C) 2015-2018 IoT.bzh
+ Copyright (C) 2015-2020 IoT.bzh
 
  author: José Bollo <jose.bollo@iot.bzh>
 
@@ -146,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;
 }
 
 /*
@@ -268,7 +271,7 @@ 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);
@@ -326,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;
@@ -342,7 +345,7 @@ 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);
@@ -393,7 +396,7 @@ 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);
@@ -403,7 +406,7 @@ struct json_object *afm_urun_state(struct afm_udb *db, int runid, int uid)
                         && 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;
                        }
@@ -437,6 +440,10 @@ int afm_urun_search_runid(struct afm_udb *db, const char *id, int uid)
                pid = -1;
        } else {
                pid = systemd_unit_pid_of_dpath(isuser, udpath);
+               if (pid == 0) {
+                       errno = ESRCH;
+                       pid = -1;
+               }
        }
        return pid;
 }