afm-user-daemon: prepare to launch with mode
authorJosé Bollo <jose.bollo@iot.bzh>
Sat, 13 Feb 2016 11:51:29 +0000 (12:51 +0100)
committerJosé Bollo <jose.bollo@iot.bzh>
Sat, 13 Feb 2016 14:25:04 +0000 (15:25 +0100)
Change-Id: I0372eab2496c2fdb12144d68c0f041f5f1fd360e
Signed-off-by: José Bollo <jose.bollo@iot.bzh>
src/CMakeLists.txt
src/afm-launch-mode.c [new file with mode: 0644]
src/afm-launch-mode.h [new file with mode: 0644]
src/afm-launch.c
src/afm-launch.h
src/afm-run.c
src/afm-run.h
src/afm-user-daemon.c

index 0b055e8..809d586 100644 (file)
@@ -101,6 +101,7 @@ add_library(secwrp
 add_library(afm
        afm-db.c
        afm-launch.c
+       afm-launch-mode.c
        afm-run.c
        )
 
diff --git a/src/afm-launch-mode.c b/src/afm-launch-mode.c
new file mode 100644 (file)
index 0000000..99add62
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ Copyright 2015 IoT.bzh
+
+ author: José Bollo <jose.bollo@iot.bzh>
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+     http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+*/
+
+#include <string.h>
+
+#include "afm-launch-mode.h"
+
+static const char s_mode_local[] = "local";
+static const char s_mode_remote[] = "remote";
+
+enum afm_launch_mode launch_mode_of_string(const char *s)
+{
+       if (s) {
+               if (!strcmp(s, s_mode_local))
+                       return mode_local;
+               if (!strcmp(s, s_mode_remote))
+                       return mode_remote;
+       }
+       return invalid_launch_mode;
+}
+
+const char *name_of_launch_mode(enum afm_launch_mode m)
+{
+       switch (m) {
+       case mode_local:  return s_mode_local;
+       case mode_remote: return s_mode_remote;
+       default:          return "(INVALID LAUNCH MODE)";
+       }
+}
diff --git a/src/afm-launch-mode.h b/src/afm-launch-mode.h
new file mode 100644 (file)
index 0000000..8eee8eb
--- /dev/null
@@ -0,0 +1,31 @@
+/*
+ Copyright 2015 IoT.bzh
+
+ author: José Bollo <jose.bollo@iot.bzh>
+
+ Licensed under the Apache License, Version 2.0 (the "License");
+ you may not use this file except in compliance with the License.
+ You may obtain a copy of the License at
+
+     http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+*/
+
+enum afm_launch_mode {
+       invalid_launch_mode = 0,
+       mode_local   = 1,
+       mode_remote  = 2
+};
+
+#define default_launch_mode  mode_local
+
+#define launch_mode_is_valid(x)  ((x)==mode_local || (x)==mode_remote)
+
+enum afm_launch_mode launch_mode_of_string(const char *s);
+const char *name_of_launch_mode(enum afm_launch_mode m);
+
index 47f3f8b..216e6df 100644 (file)
@@ -32,6 +32,7 @@
 extern char **environ;
 
 #include "verbose.h"
+#include "afm-launch-mode.h"
 #include "afm-launch.h"
 #include "secmgr-wrap.h"
 
@@ -590,7 +591,7 @@ int afm_launch_initialize()
        return rc;
 }
 
-int afm_launch(struct afm_launch_desc *desc, pid_t children[2])
+int afm_launch(struct afm_launch_desc *desc, pid_t children[2], char **uri)
 {
        char datadir[PATH_MAX];
        int ikl, rc;
@@ -600,6 +601,7 @@ int afm_launch(struct afm_launch_desc *desc, pid_t children[2])
 
        /* should be init */
        assert(groupid != 0);
+       assert(launch_mode_is_valid(desc->mode));
 
        /* init */
        children[0] = 0;
index d7fcde2..b246559 100644 (file)
@@ -27,7 +27,8 @@ struct afm_launch_desc {
        const char **plugins;
        int width;
        int height;
+       enum afm_launch_mode mode;
 };
 
 int afm_launch_initialize();
-int afm_launch(struct afm_launch_desc *desc, pid_t children[2]);
+int afm_launch(struct afm_launch_desc *desc, pid_t children[2], char **uri);
index 4e002e0..c414ceb 100644 (file)
@@ -32,8 +32,9 @@
 #include "verbose.h"
 #include "utils-dir.h"
 #include "utils-json.h"
-#include "afm-run.h"
+#include "afm-launch-mode.h"
 #include "afm-launch.h"
+#include "afm-run.h"
 
 enum appstate {
        as_starting,
@@ -239,10 +240,12 @@ static void on_sigchld(int signum, siginfo_t *info, void *uctxt)
 
 /**************** handle afm_launch_desc *********************/
 
-static int fill_launch_desc(struct json_object *appli, struct afm_launch_desc *desc)
+static int fill_launch_desc(struct json_object *appli, enum afm_launch_mode mode, struct afm_launch_desc *desc)
 {
        json_object *pub;
 
+       assert(launch_mode_is_valid(mode));
+
        /* main items */
        if(!j_read_object_at(appli, "public", &pub)
        || !j_read_string_at(appli, "path", &desc->path)
@@ -265,20 +268,23 @@ static int fill_launch_desc(struct json_object *appli, struct afm_launch_desc *d
 
        /* finaly */
        desc->home = homeappdir;
+       desc->mode = mode;
        return 0;
 };
 
 /**************** API handling ************************/
 
-int afm_run_start(struct json_object *appli)
+int afm_run_start(struct json_object *appli, enum afm_launch_mode mode, char **uri)
 {
        static struct apprun *runner;
        struct afm_launch_desc desc;
        int rc;
        sigset_t saved, blocked;
 
+       assert(launch_mode_is_valid(mode));
+
        /* prepare to launch */
-       rc = fill_launch_desc(appli, &desc);
+       rc = fill_launch_desc(appli, mode, &desc);
        if (rc)
                return rc;
        runner = createrunner(appli);
@@ -291,7 +297,7 @@ int afm_run_start(struct json_object *appli)
        sigprocmask(SIG_BLOCK, &blocked, &saved);
 
        /* launch now */
-       rc = afm_launch(&desc, runner->pids);
+       rc = afm_launch(&desc, runner->pids, uri);
        if (rc < 0) {
                /* fork failed */
                sigprocmask(SIG_SETMASK, &saved, NULL);
index 4ad0de1..88b6625 100644 (file)
@@ -16,7 +16,7 @@
  limitations under the License.
 */
 
-extern int afm_run_start(struct json_object *appli);
+extern int afm_run_start(struct json_object *appli, enum afm_launch_mode mode, char **uri);
 extern int afm_run_terminate(int runid);
 extern int afm_run_stop(int runid);
 extern int afm_run_continue(int runid);
index 8f8f381..09ec0b5 100644 (file)
 #include <stdio.h>
 #include <time.h>
 #include <getopt.h>
+#include <string.h>
 
 #include <json.h>
 
 #include "verbose.h"
 #include "utils-jbus.h"
+#include "utils-json.h"
 #include "afm.h"
 #include "afm-db.h"
+#include "afm-launch-mode.h"
 #include "afm-run.h"
 
 static const char appname[] = "afm-user-daemon";
@@ -113,30 +116,64 @@ static void on_detail(struct jreq *jreq, struct json_object *obj)
 
 static void on_start(struct jreq *jreq, struct json_object *obj)
 {
-       const char *appid;
-       struct json_object *appli;
+       const char *appid, *modestr;
+       char *uri;
+       struct json_object *appli, *resp;
        int runid;
        char runidstr[20];
-
-       appid = getappid(obj);
-       INFO("method start called for %s", appid);
-       if (appid == NULL)
+       enum afm_launch_mode mode;
+
+       /* get the parameters */
+       mode = invalid_launch_mode;
+       if (j_read_string(obj, &appid)) {
+               mode = default_launch_mode;
+       } else if (j_read_string_at(obj, "id", &appid)) {
+               if (j_read_string_at(obj, "mode", &modestr)) {
+                       mode = launch_mode_of_string(modestr);
+               } else {
+                       mode = default_launch_mode;
+               }
+       }
+       if (!launch_mode_is_valid(mode)) {
                jbus_reply_error_s(jreq, error_bad_request);
+               return;
+       }
+
+       /* get the application */
+       INFO("method start called for %s mode=%s", appid, mode);
+       appli = afm_db_get_application(afdb, appid);
+       if (appli == NULL) {
+               jbus_reply_error_s(jreq, error_not_found);
+               return;
+       }
+
+       /* launch the application */
+       uri = NULL;
+       runid = afm_run_start(appli, mode, &uri);
+       if (runid <= 0) {
+               jbus_reply_error_s(jreq, error_cant_start);
+               free(uri);
+               return;
+       }
+
+       if (uri == NULL) {
+               /* returns only the runid */
+               snprintf(runidstr, sizeof runidstr, "%d", runid);
+               runidstr[sizeof runidstr - 1] = 0;
+               jbus_reply_s(jreq, runidstr);
+               return;
+       }
+
+       /* returns the runid and its uri */
+       resp = json_object_new_object();
+       if (resp != NULL && j_add_integer(resp, "runid", runid) && j_add_string(resp, "uri", uri))
+               jbus_reply_j(jreq, resp);
        else {
-               appli = afm_db_get_application(afdb, appid);
-               if (appli == NULL)
-                       jbus_reply_error_s(jreq, error_not_found);
-               else {
-                       runid = afm_run_start(appli);
-                       if (runid <= 0)
-                               jbus_reply_error_s(jreq, error_cant_start);
-                       else {
-                               snprintf(runidstr, sizeof runidstr, "%d", runid);
-                               runidstr[sizeof runidstr - 1] = 0;
-                               jbus_reply_s(jreq, runidstr);
-                       }
-               }
+               afm_run_stop(runid);
+               jbus_reply_error_s(jreq, error_cant_start);
        }
+       json_object_put(resp);
+       free(uri);
 }
 
 static void on_stop(struct jreq *jreq, struct json_object *obj)