utils-systemd: Offer way to set systemd's buses
[src/app-framework-main.git] / src / afm-urun.c
index 48f8436..5649973 100644 (file)
@@ -36,6 +36,8 @@
 #include "afm-udb.h"
 #include "afm-urun.h"
 
+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)
@@ -51,7 +53,7 @@ static int get_basis(struct json_object *appli, int *isuser, const char **dpath,
        *isuser = strcmp(uscope, "system") != 0;
 
        /* get dpath */
-       if (!j_read_string_at(appli, "-unit-dpath-", dpath)) {
+       if (!j_read_string_at(appli, key_unit_d_path, dpath)) {
                if (!load) {
                        errno = ENOENT;
                        goto error;
@@ -65,12 +67,12 @@ static int get_basis(struct json_object *appli, int *isuser, const char **dpath,
                        ERROR("Can't load unit of name %s for %s: %m", uname, uscope);
                        goto error;
                }
-               if (!j_add_string(appli, "-unit-dpath-", dp)) {
+               if (!j_add_string(appli, key_unit_d_path, dp)) {
                        free(dp);
                        goto nomem;
                }
                free(dp);
-               j_read_string_at(appli, "-unit-dpath-", dpath);
+               j_read_string_at(appli, key_unit_d_path, dpath);
        }
 
        return 0;
@@ -93,7 +95,7 @@ static const char *wait_state_stable(int isuser, const char *dpath)
        for (;;) {
                state = systemd_unit_state_of_dpath(isuser, dpath);
                if (state == NULL || state == SysD_State_Active
-                || state == SysD_State_Failed || state == SysD_State_Inactive)
+                || state == SysD_State_Failed)
                        return state;
                /* TODO: sleep */
        }
@@ -238,6 +240,8 @@ static int not_yet_implemented(const char *what)
 int afm_urun_terminate(int runid)
 {
        int rc = systemd_unit_stop_pid(1 /* TODO: isuser? */, (unsigned)runid);
+       if (rc < 0)
+               rc = systemd_unit_stop_pid(0 /* TODO: isuser? */, (unsigned)runid);
        return rc < 0 ? rc : 0;
 }
 
@@ -290,10 +294,12 @@ struct json_object *afm_urun_list(struct afm_udb *db)
                        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);
-                               desc = mkstate(id, pid, pid, state);
-                               if (desc && json_object_array_add(result, desc) == -1) {
-                                       ERROR("can't add desc %s to result", json_object_get_string(desc));
-                                       json_object_put(desc);
+                               if (state == SysD_State_Active) {
+                                       desc = mkstate(id, pid, pid, state);
+                                       if (desc && json_object_array_add(result, desc) == -1) {
+                                               ERROR("can't add desc %s to result", json_object_get_string(desc));
+                                               json_object_put(desc);
+                                       }
                                }
                        }
                }
@@ -311,7 +317,7 @@ error:
  */
 struct json_object *afm_urun_state(struct afm_udb *db, int runid)
 {
-       int i, n, isuser, pid;
+       int i, n, isuser, pid, wasuser;
        char *dpath;
        const char *udpath;
        const char *id;
@@ -323,9 +329,10 @@ struct json_object *afm_urun_state(struct afm_udb *db, int runid)
        result = NULL;
 
        /* get the dpath */
-       dpath = systemd_unit_dpath_by_pid(1 /* TODO: isuser? */, (unsigned)runid);
+       dpath = systemd_unit_dpath_by_pid(wasuser = 1, (unsigned)runid);
+       if (!dpath)
+               dpath = systemd_unit_dpath_by_pid(wasuser = 0, (unsigned)runid);
        if (!dpath) {
-               result = NULL;
                errno = EINVAL;
                WARNING("searched runid %d not found", runid);
        } else {
@@ -340,11 +347,11 @@ struct json_object *afm_urun_state(struct afm_udb *db, int runid)
                         && j_read_string_at(appli, "id", &id)) {
                                pid = systemd_unit_pid_of_dpath(isuser, udpath);
                                state = systemd_unit_state_of_dpath(isuser, dpath);
-                               result = mkstate(id, runid, pid, state);
+                               if (state == SysD_State_Active)
+                                       result = mkstate(id, runid, pid, state);
                                goto end;
                        }
                }
-               result = NULL;
                errno = ENOENT;
                WARNING("searched runid %d of dpath %s isn't an applications", runid, dpath);
 end: