From bd910c758e234e165f6a3239c14e77f53d1fc480 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Bollo?= Date: Mon, 18 Dec 2017 12:26:41 +0100 Subject: [PATCH] afm-binding: Allow use of appid instead of runid MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This facility avoid the double call 'ps', 'kill' and allow a single call to 'kill' with the application id. Bug-AGL: SPEC-1189 Change-Id: I71861c06847e855b05dc8294ab1ea6785f555416 Signed-off-by: José Bollo --- src/afm-binding.c | 29 +++++++++++++++++++++++------ src/afm-urun.c | 23 +++++++++++++++++++++++ src/afm-urun.h | 1 + 3 files changed, 47 insertions(+), 6 deletions(-) diff --git a/src/afm-binding.c b/src/afm-binding.c index 51316e7..7fe9f3d 100644 --- a/src/afm-binding.c +++ b/src/afm-binding.c @@ -130,17 +130,34 @@ static int onappid(struct afb_req req, const char *method, const char **appid) static int onrunid(struct afb_req req, const char *method, int *runid) { struct json_object *json; + const char *appid; + /* get the paramaters of the request */ json = afb_req_json(req); - if (wrap_json_unpack(json, "i", runid) - && wrap_json_unpack(json, "{si}", _runid_, runid)) { - INFO("bad request method %s: %s", method, - json_object_to_json_string(json)); - bad_request(req); + + /* get the runid if any */ + if (!wrap_json_unpack(json, "i", runid) + || !wrap_json_unpack(json, "{si}", _runid_, runid)) { + INFO("method %s called for %d", method, *runid); + return 1; + } + + /* get the appid if any */ + if (!onappid(req, method, &appid)) + return 0; + + /* search the runid of the appid */ + *runid = afm_urun_search_runid(afudb, appid, afb_req_get_uid(req)); + if (*runid < 0) { + /* nothing appropriate */ + INFO("method %s can't get runid for %s: %m", method, + appid); + not_found(req); return 0; } - INFO("method %s called for %d", method, *runid); + /* found */ + INFO("method %s called for %s -> %d", method, appid, *runid); return 1; } diff --git a/src/afm-urun.c b/src/afm-urun.c index d242df8..36f40b2 100644 --- a/src/afm-urun.c +++ b/src/afm-urun.c @@ -424,3 +424,26 @@ end: 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, 0, uid) < 0) { + pid = -1; + } else { + pid = systemd_unit_pid_of_dpath(isuser, udpath); + } + return pid; +} + diff --git a/src/afm-urun.h b/src/afm-urun.h index 178edf9..3914aec 100644 --- a/src/afm-urun.h +++ b/src/afm-urun.h @@ -25,4 +25,5 @@ extern int afm_urun_pause(int runid, int uid); extern int afm_urun_resume(int runid, int uid); extern struct json_object *afm_urun_list(struct afm_udb *db, int uid); extern struct json_object *afm_urun_state(struct afm_udb *db, int runid, int uid); +extern int afm_urun_search_runid(struct afm_udb *db, const char *id, int uid); -- 2.16.6