/*
- Copyright 2015, 2016, 2017 IoT.bzh
+ Copyright (C) 2015-2020 IoT.bzh
author: José Bollo <jose.bollo@iot.bzh>
/**************** 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;
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));
/* get dpath of userid */
nun = alloca((size_t)(arodot - uname) + strlen(userid) + strlen(arodot) + 1);
stpcpy(stpcpy(stpncpy(nun, uname, (size_t)(arodot - uname)), userid), arodot);
- dp = systemd_unit_dpath_by_name(*isuser, uname, 1);
+ dp = systemd_unit_dpath_by_name(*isuser, nun, 1);
if (dp == NULL) {
- ERROR("Can't load unit of name %s for %s: %m", uname, uscope);
+ ERROR("Can't load unit of name %s for %s: %m", nun, uscope);
goto error;
}
/* record the dpath */
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;
if (rc < 0) {
j_read_string_at(appli, "unit-scope", &uscope);
j_read_string_at(appli, "unit-name", &uname);
- ERROR("can't start %s unit %s", uscope, uname);
+ ERROR("can't start %s unit %s for uid %d", uscope, uname, uid);
goto error;
}
if (state == NULL) {
j_read_string_at(appli, "unit-scope", &uscope);
j_read_string_at(appli, "unit-name", &uname);
- ERROR("can't wait %s unit %s: %m", uscope, uname);
+ ERROR("can't wait %s unit %s for uid %d: %m", uscope, uname, uid);
goto error;
}
if (state != SysD_State_Active) {
j_read_string_at(appli, "unit-scope", &uscope);
j_read_string_at(appli, "unit-name", &uname);
- ERROR("start error %s unit %s: %s", uscope, uname, state);
+ ERROR("start error %s unit %s for uid %d: %s", uscope, uname, uid, state);
goto error;
}
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: %m", uscope, uname);
+ ERROR("can't getpid of %s unit %s for uid %d: %m", uscope, uname, uid);
goto error;
}
-
+
return rc;
error:
*
* 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;
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);
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;
}
WARNING("searched runid %d of dpath %s isn't an applications", runid, dpath);
end:
json_object_put(apps);
- free(dpath);
+ free(dpath);
}
return result;
}
+/*
+ * Search the runid, if any, of the application of 'id' for the user 'uid'.
+ * Returns the pid (a positive not null number) or -1 in case of error.
+ */
+int afm_urun_search_runid(struct afm_udb *db, const char *id, int uid)
+{
+ int isuser, pid;
+ const char *udpath;
+ struct json_object *appli;
+
+ appli = afm_udb_get_application_private(db, id, uid);
+ if (!appli) {
+ NOTICE("Unknown appid %s", id);
+ errno = ENOENT;
+ pid = -1;
+ } 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;
+}
+