From a46043a9b0387bcf8fb6dcb34d5984489644c090 Mon Sep 17 00:00:00 2001 From: José Bollo Date: Mon, 8 Feb 2016 10:17:20 +0100 Subject: removes unused smack field MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I3388fa366c44c96d2ebf37ba57ed1b3acce82869 Signed-off-by: José Bollo --- include/local-def.h | 1 - 1 file changed, 1 deletion(-) (limited to 'include') diff --git a/include/local-def.h b/include/local-def.h index 6d219406..0c48d678 100644 --- a/include/local-def.h +++ b/include/local-def.h @@ -170,7 +170,6 @@ typedef struct { char *console; // console device name (can be a file or a tty) int localhostOnly; int httpdPort; - char *smack; // smack label char *ldpaths; // list of plugins directories char *rootdir; // base dir for httpd file download char *rootbase; // Angular HTML5 base URL -- cgit 1.2.3-korg From 5de05afde9b308b5a7e77e595e3d827f1ef33a43 Mon Sep 17 00:00:00 2001 From: José Bollo Date: Mon, 8 Feb 2016 10:29:45 +0100 Subject: typo: minor improvement MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Iaf16a80d3fcb8887260f351448c68fd91c9387fe Signed-off-by: José Bollo --- include/local-def.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'include') diff --git a/include/local-def.h b/include/local-def.h index 0c48d678..c0070888 100644 --- a/include/local-def.h +++ b/include/local-def.h @@ -1,5 +1,5 @@ /* - alsajson-gw -- provide a REST/HTTP interface to ALSA-Mixer + local-def.h -- provide a REST/HTTP interface Copyright (C) 2015, Fulup Ar Foll @@ -88,7 +88,7 @@ typedef enum {AFB_PLUGIN_JSON=123456789, AFB_PLUGIN_JSCRIPT=987654321, AFB_PLU typedef enum { AFB_FALSE, AFB_TRUE, AFB_FATAL, AFB_FAIL, AFB_WARNING, AFB_EMPTY, AFB_SUCCESS, AFB_DONE, AFB_UNAUTH} AFB_error; extern char *ERROR_LABEL[]; -#define ERROR_LABEL_DEF {"false", "true","fatal", "fail", "warning", "empty", "success"} +#define ERROR_LABEL_DEF {"false", "true", "fatal", "fail", "warning", "empty", "success"} #define BANNER "Application Framework BinderApplication Framework " #define JSON_CONTENT "application/json" -- cgit 1.2.3-korg From 1329992e8d922a55d5b55039ba8c700e7801dab5 Mon Sep 17 00:00:00 2001 From: José Bollo Date: Mon, 8 Feb 2016 10:37:01 +0100 Subject: avoid use of keyword "private" MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I7efe1ce2e6a49b6211298e509ae0b21573d6776a Signed-off-by: José Bollo --- include/local-def.h | 4 ++-- src/rest-api.c | 28 ++++++++++++++-------------- 2 files changed, 16 insertions(+), 16 deletions(-) (limited to 'include') diff --git a/include/local-def.h b/include/local-def.h index c0070888..f7ddeeb4 100644 --- a/include/local-def.h +++ b/include/local-def.h @@ -130,7 +130,7 @@ typedef struct { int uid; // post uid for debug AFB_PostType type; // JSON or FORM AFB_apiCB completeCB; // callback when post is completed - char *private; // use internally to keep track or partial buffer + char *privatebuf; // use internally to keep track or partial buffer struct MHD_PostProcessor *pp; // iterator handle } AFB_PostHandle; @@ -206,7 +206,7 @@ typedef struct { AFB_sessionE session; AFB_apiCB callback; char *info; - AFB_privateApi *private; + AFB_privateApi *privateapi; } AFB_restapi; // Plugin definition diff --git a/src/rest-api.c b/src/rest-api.c index eae1d63c..9f16a8fd 100644 --- a/src/rest-api.c +++ b/src/rest-api.c @@ -45,7 +45,7 @@ PUBLIC void endPostRequest(AFB_PostHandle *postHandle) { if (postHandle->type == AFB_POST_FORM) { if (verbose) fprintf(stderr, "End PostForm Request UID=%d\n", postHandle->uid); } - if (postHandle->private) free(postHandle->private); + if (postHandle->privatebuf) free(postHandle->privatebuf); free(postHandle); } @@ -280,7 +280,7 @@ STATIC int doPostIterate (void *cls, enum MHD_ValueKind kind, const char *key, // retrieve API request from Post iterator handle AFB_PostHandle *postHandle = (AFB_PostHandle*)cls; - AFB_request *request = (AFB_request*)postHandle->private; + AFB_request *request = (AFB_request*)postHandle->privatebuf; AFB_PostRequest postRequest; fprintf (stderr, "postHandle key=%s filename=%s len=%d mime=%s\n", key, filename, size, mimetype); @@ -404,7 +404,7 @@ PUBLIC int doRestApi(struct MHD_Connection *connection, AFB_session *session, co request = createRequest (connection, session, url); if (request->jresp != NULL) goto ProcessApiCall; postHandle->type = AFB_POST_FORM; - postHandle->private= (void*)request; + postHandle->privatebuf = (void*)request; postHandle->pp = MHD_create_post_processor (connection, MAX_POST_SIZE, &doPostIterate, postHandle); if (NULL == postHandle->pp) { @@ -429,7 +429,7 @@ PUBLIC int doRestApi(struct MHD_Connection *connection, AFB_session *session, co // Size is OK, let's allocate a buffer to hold post data postHandle->type = AFB_POST_JSON; - postHandle->private = malloc(contentlen + 1); // allocate memory for full POST data + 1 for '\0' enf of string + postHandle->privatebuf = malloc(contentlen + 1); // allocate memory for full POST data + 1 for '\0' enf of string // if (verbose) fprintf(stderr, "Create PostJson[uid=%d] Size=%d\n", postHandle->uid, contentlen); return MHD_YES; @@ -454,7 +454,7 @@ PUBLIC int doRestApi(struct MHD_Connection *connection, AFB_session *session, co // Process JsonPost request when buffer is completed let's call API if (postHandle->type == AFB_POST_JSON) { // if (verbose) fprintf(stderr, "Updating PostJson[uid=%d]\n", postHandle->uid); - memcpy(&postHandle->private[postHandle->len], upload_data, *upload_data_size); + memcpy(&postHandle->privatebuf[postHandle->len], upload_data, *upload_data_size); postHandle->len = postHandle->len + *upload_data_size; } @@ -490,8 +490,8 @@ PUBLIC int doRestApi(struct MHD_Connection *connection, AFB_session *session, co } // Before processing data, make sure buffer string is properly ended - postHandle->private[postHandle->len] = '\0'; - postRequest.data = postHandle->private; + postHandle->privatebuf[postHandle->len] = '\0'; + postRequest.data = postHandle->privatebuf; request->post = &postRequest; // if (verbose) fprintf(stderr, "Close Post[%d] Buffer=%s\n", postHandle->uid, request->post->data); @@ -562,15 +562,15 @@ STATIC AFB_plugin ** RegisterJsonPlugins(AFB_plugin **plugins) { // Prebuild each API jtype to boost API json response for (jdx = 0; plugins[idx]->apis[jdx].name != NULL; jdx++) { - AFB_privateApi *private = malloc (sizeof (AFB_privateApi)); - if (plugins[idx]->apis[jdx].private != NULL) { + AFB_privateApi *privateapi = malloc (sizeof (AFB_privateApi)); + if (plugins[idx]->apis[jdx].privateapi != NULL) { fprintf (stderr, "WARNING: plugin=%s api=%s private handle should be NULL=0x%x\n" - ,plugins[idx]->prefix,plugins[idx]->apis[jdx].name, plugins[idx]->apis[jdx].private); + ,plugins[idx]->prefix,plugins[idx]->apis[jdx].name, plugins[idx]->apis[jdx].privateapi); } - private->len = strlen (plugins[idx]->apis[jdx].name); - private->jtype=json_object_new_string(plugins[idx]->apis[jdx].name); - json_object_get(private->jtype); // increase reference count to make it permanent - plugins[idx]->apis[jdx].private = private; + privateapi->len = strlen (plugins[idx]->apis[jdx].name); + privateapi->jtype=json_object_new_string(plugins[idx]->apis[jdx].name); + json_object_get(privateapi->jtype); // increase reference count to make it permanent + plugins[idx]->apis[jdx].privateapi = privateapi; } } } -- cgit 1.2.3-korg From 0890cefa1108aa409d6e6b3cf2f7517b3a6cc5f9 Mon Sep 17 00:00:00 2001 From: José Bollo Date: Tue, 9 Feb 2016 10:23:41 +0100 Subject: Adds a new option: mode MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The option mode can have 3 values: local, remote, global. It indicates wether the daemon serves: - loopback (local) - external interface (remote) - both (global) Change-Id: Ibd6c25da692e607b8e0ce793da11cca88db050a2 Signed-off-by: José Bollo --- include/local-def.h | 2 ++ src/main.c | 76 +++++++++++++++++++++++++++++++++-------------------- 2 files changed, 49 insertions(+), 29 deletions(-) (limited to 'include') diff --git a/include/local-def.h b/include/local-def.h index f7ddeeb4..bf353e94 100644 --- a/include/local-def.h +++ b/include/local-def.h @@ -108,6 +108,7 @@ typedef struct { } AFB_errorT; typedef enum {AFB_POST_NONE=0, AFB_POST_JSON, AFB_POST_FORM, AFB_POST_EMPTY} AFB_PostType; +typedef enum {AFB_MODE_LOCAL=0, AFB_MODE_REMOTE, AFB_MODE_GLOBAL} AFB_Mode; // Post Upload File Handle typedef struct { @@ -184,6 +185,7 @@ typedef struct { int cntxTimeout; // Client Session Context timeout int pluginCount; // loaded plugins count AFB_aliasdir *aliasdir; // alias mapping for icons,apps,... + AFB_Mode mode; // mode of listening } AFB_config; typedef struct { diff --git a/src/main.c b/src/main.c index b17435ec..533776a1 100644 --- a/src/main.c +++ b/src/main.c @@ -50,34 +50,36 @@ static sigjmp_buf exitPoint; // context save for set/longjmp // Define command line option - #define SET_VERBOSE 101 - #define SET_BACKGROUND 105 - #define SET_FORGROUND 106 - #define KILL_PREV_EXIT 107 - #define KILL_PREV_REST 108 - #define SET_FAKE_MOD 109 - - #define SET_TCP_PORT 120 - #define SET_ROOT_DIR 121 - #define SET_ROOT_BASE 122 - #define SET_ROOT_API 123 - #define SET_ROOT_ALIAS 124 - - #define SET_CACHE_TO 130 - #define SET_USERID 131 - #define SET_PID_FILE 132 - #define SET_SESSION_DIR 133 - #define SET_CONFIG_FILE 134 - #define SET_CONFIG_SAVE 135 - #define SET_CONFIG_EXIT 138 - - #define SET_AUTH_TOKEN 141 - #define SET_LDPATH 142 - #define SET_APITIMEOUT 143 - #define SET_CNTXTIMEOUT 144 - - #define DISPLAY_VERSION 150 - #define DISPLAY_HELP 151 +#define SET_VERBOSE 101 +#define SET_BACKGROUND 105 +#define SET_FORGROUND 106 +#define KILL_PREV_EXIT 107 +#define KILL_PREV_REST 108 +#define SET_FAKE_MOD 109 + +#define SET_TCP_PORT 120 +#define SET_ROOT_DIR 121 +#define SET_ROOT_BASE 122 +#define SET_ROOT_API 123 +#define SET_ROOT_ALIAS 124 + +#define SET_CACHE_TO 130 +#define SET_USERID 131 +#define SET_PID_FILE 132 +#define SET_SESSION_DIR 133 +#define SET_CONFIG_FILE 134 +#define SET_CONFIG_SAVE 135 +#define SET_CONFIG_EXIT 138 + +#define SET_AUTH_TOKEN 141 +#define SET_LDPATH 142 +#define SET_APITIMEOUT 143 +#define SET_CNTXTIMEOUT 144 + +#define DISPLAY_VERSION 150 +#define DISPLAY_HELP 151 + +#define SET_MODE 160 // Supported option @@ -111,6 +113,8 @@ static AFB_options cliOptions [] = { {DISPLAY_VERSION ,0,"version" , "Display version and copyright"}, {DISPLAY_HELP ,0,"help" , "Display this help"}, + + {SET_MODE ,1,"mode" , "set the mode: either local, remote or global"}, {0, 0, 0} }; @@ -413,6 +417,15 @@ int main(int argc, char *argv[]) { session->killPrevious = 2; break; + case SET_MODE: + if (optarg == 0) goto needValueForOption; + /* TODO setting directly session->config isn't the expected path but... see configLoadFile, configStoreFile (JOBOL) */ + if (!strcmp(optarg, "local")) session->config->mode = AFB_MODE_LOCAL; + else if (!strcmp(optarg, "remote")) session->config->mode = AFB_MODE_REMOTE; + else if (!strcmp(optarg, "global")) session->config->mode = AFB_MODE_GLOBAL; + else goto badMode; + break; + case DISPLAY_VERSION: if (optarg != 0) goto noValueForOption; printVersion(); @@ -440,7 +453,7 @@ int main(int argc, char *argv[]) { if ((session->background == 0) && (session->foreground == 0)) session->foreground=1; // open syslog if ever needed - openlog("AGB-log", 0, LOG_DAEMON); + openlog("AFB-log", 0, LOG_DAEMON); // -------------- Try to kill any previous process if asked --------------------- if (session->killPrevious) { @@ -615,6 +628,11 @@ notAnInteger: ,gnuOptions[optionIndex].name, gnuOptions[optionIndex].name); exit (-1); +badMode: + fprintf (stderr,"\nERR:AFB-daemon option [--%s] only accepts local, global or remote.\n\n" + ,gnuOptions[optionIndex].name); + exit (-1); + exitOnSignal: fprintf (stderr,"\n%s INF:AFB-daemon pid=%d received exit signal (Hopefully crtl-C or --kill-previous !!!)\n\n" ,configTime(), getpid()); -- cgit 1.2.3-korg From 3a6db27dec6fee245708074c301b42e1cbedd716 Mon Sep 17 00:00:00 2001 From: José Bollo Date: Sun, 14 Feb 2016 15:16:48 +0100 Subject: adds mode management MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I73240e346a06924c314961871e789c69e279cb48 Signed-off-by: José Bollo --- include/local-def.h | 2 +- plugins/afm-main-plugin/afm-main-plugin.c | 46 +++++++++++++++++++++++++++++-- src/config.c | 3 +- src/main.c | 7 ++--- 4 files changed, 49 insertions(+), 9 deletions(-) (limited to 'include') diff --git a/include/local-def.h b/include/local-def.h index bf353e94..7ad4e705 100644 --- a/include/local-def.h +++ b/include/local-def.h @@ -184,8 +184,8 @@ typedef struct { int apiTimeout; int cntxTimeout; // Client Session Context timeout int pluginCount; // loaded plugins count - AFB_aliasdir *aliasdir; // alias mapping for icons,apps,... AFB_Mode mode; // mode of listening + AFB_aliasdir *aliasdir; // alias mapping for icons,apps,... } AFB_config; typedef struct { diff --git a/plugins/afm-main-plugin/afm-main-plugin.c b/plugins/afm-main-plugin/afm-main-plugin.c index fa184bf8..3f9de713 100644 --- a/plugins/afm-main-plugin/afm-main-plugin.c +++ b/plugins/afm-main-plugin/afm-main-plugin.c @@ -16,6 +16,8 @@ * along with this program. If not, see . */ +#define _GNU_SOURCE /* See feature_test_macros(7) */ +#include #include "local-def.h" @@ -33,6 +35,10 @@ static char _runners_[] = "runners"; static char _state_[] = "state"; static char _install_[] = "install"; static char _uninstall_[] = "uninstall"; +static const char _mode_[] = "mode"; +static const char _local_[] = "local"; +static const char _remote_[]= "remote"; +static const char _uri_[] = "uri"; static struct jbus *jbus; @@ -117,9 +123,43 @@ static struct json_object *call_void__runnables(AFB_request *request, AFB_PostIt return embed(request, _runnables_, call_void(request, item)); } -static struct json_object *call_appid__runid(AFB_request *request, AFB_PostItem *item) +static struct json_object *call_start(AFB_request *request, AFB_PostItem *item) { - return embed(request, _runid_, call_appid(request, item)); + struct json_object *resp; + const char *id, *mode; + char *query; + int rc; + + /* get the id */ + id = getQueryValue(request, _id_); + if (id == NULL) { + request->errcode = MHD_HTTP_BAD_REQUEST; + return NULL; + } + /* get the mode */ + mode = getQueryValue(request, _mode_); + if (mode == NULL) { + mode = request->config->mode == AFB_MODE_REMOTE ? _remote_ : _local_; + } + + /* create the query */ + rc = asprintf(&query, "{\"id\":\"%s\",\"mode\":\"%s\"}", id, mode); + if (rc < 0) { + request->errcode = MHD_HTTP_INTERNAL_SERVER_ERROR; + return NULL; + } + + /* calls the service */ + resp = jbus_call_sj_sync(jbus, _start_, query); + if (verbose) + fprintf(stderr, "(afm-main-plugin) call_start: %s -> %s\n", query, resp ? json_object_to_json_string(resp) : "NULL"); + free(query); + + /* embed if needed */ + if (json_object_get_type(resp) == json_type_string) + resp = embed(request, _runid_, resp); + request->errcode = resp ? MHD_HTTP_OK : MHD_HTTP_FAILED_DEPENDENCY; + return resp; } static struct json_object *call_void__runners(AFB_request *request, AFB_PostItem *item) @@ -157,7 +197,7 @@ static AFB_restapi plug_apis[] = { {_runnables_, AFB_SESSION_CHECK, (AFB_apiCB)call_void__runnables, "Get list of runnable applications"}, {_detail_ , AFB_SESSION_CHECK, (AFB_apiCB)call_appid, "Get the details for one application"}, - {_start_ , AFB_SESSION_CHECK, (AFB_apiCB)call_appid__runid, "Start an application"}, + {_start_ , AFB_SESSION_CHECK, (AFB_apiCB)call_start, "Start an application"}, {_terminate_, AFB_SESSION_CHECK, (AFB_apiCB)call_runid, "Terminate a running application"}, {_stop_ , AFB_SESSION_CHECK, (AFB_apiCB)call_runid, "Stop (pause) a running application"}, {_continue_ , AFB_SESSION_CHECK, (AFB_apiCB)call_runid, "Continue (resume) a stopped application"}, diff --git a/src/config.c b/src/config.c index bd03b6ff..a64e131f 100644 --- a/src/config.c +++ b/src/config.c @@ -61,7 +61,8 @@ PUBLIC AFB_error configLoadFile (AFB_session * session, AFB_config *cliconfig) { // TBD integrate alias-dir array with config-file session->config->aliasdir = cliconfig->aliasdir; - + session->config->mode = cliconfig->mode; + // default HTTP port if (cliconfig->httpdPort == 0) session->config->httpdPort=1234; else session->config->httpdPort=cliconfig->httpdPort; diff --git a/src/main.c b/src/main.c index 533776a1..57660edf 100644 --- a/src/main.c +++ b/src/main.c @@ -419,10 +419,9 @@ int main(int argc, char *argv[]) { case SET_MODE: if (optarg == 0) goto needValueForOption; - /* TODO setting directly session->config isn't the expected path but... see configLoadFile, configStoreFile (JOBOL) */ - if (!strcmp(optarg, "local")) session->config->mode = AFB_MODE_LOCAL; - else if (!strcmp(optarg, "remote")) session->config->mode = AFB_MODE_REMOTE; - else if (!strcmp(optarg, "global")) session->config->mode = AFB_MODE_GLOBAL; + if (!strcmp(optarg, "local")) cliconfig.mode = AFB_MODE_LOCAL; + else if (!strcmp(optarg, "remote")) cliconfig.mode = AFB_MODE_REMOTE; + else if (!strcmp(optarg, "global")) cliconfig.mode = AFB_MODE_GLOBAL; else goto badMode; break; -- cgit 1.2.3-korg From 1edf05100332c85ee28abc0754fcb825648157c4 Mon Sep 17 00:00:00 2001 From: José Bollo Date: Mon, 15 Feb 2016 11:46:37 +0100 Subject: add option for signaling readyness MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I22e64d41f51683c6c15df77b546e3cc98f427eec Signed-off-by: José Bollo --- include/local-def.h | 1 + src/main.c | 13 +++++++++++++ 2 files changed, 14 insertions(+) (limited to 'include') diff --git a/include/local-def.h b/include/local-def.h index 7ad4e705..01f10626 100644 --- a/include/local-def.h +++ b/include/local-def.h @@ -265,6 +265,7 @@ typedef struct { void *httpd; // anonymous structure for httpd handler int fakemod; // respond to GET/POST request without interacting with sndboard int forceexit; // when autoconfig from script force exit before starting server + int readyfd; // a #fd to signal when ready to serve AFB_plugin **plugins; // pointer to REST/API plugins magic_t magic; // Mime type file magic lib sigjmp_buf restartCkpt; // context save for restart set/longjmp diff --git a/src/main.c b/src/main.c index 57660edf..78d7f79e 100644 --- a/src/main.c +++ b/src/main.c @@ -80,6 +80,7 @@ static sigjmp_buf exitPoint; // context save for set/longjmp #define DISPLAY_HELP 151 #define SET_MODE 160 +#define SET_READYFD 161 // Supported option @@ -115,6 +116,7 @@ static AFB_options cliOptions [] = { {DISPLAY_HELP ,0,"help" , "Display this help"}, {SET_MODE ,1,"mode" , "set the mode: either local, remote or global"}, + {SET_READYFD ,1,"readyfd" , "set the #fd to signal when ready"}, {0, 0, 0} }; @@ -242,6 +244,12 @@ static void listenLoop (AFB_session *session) { err = httpdStart (session); if (err != AFB_SUCCESS) return; + if (session->readyfd != 0) { + static const char readystr[] = "READY=1"; + write(session->readyfd, readystr, sizeof(readystr) - 1); + close(session->readyfd); + } + // infinite loop httpdLoop(session); @@ -425,6 +433,11 @@ int main(int argc, char *argv[]) { else goto badMode; break; + case SET_READYFD: + if (optarg == 0) goto needValueForOption; + if (!sscanf (optarg, "%u", &session->readyfd)) goto notAnInteger; + break; + case DISPLAY_VERSION: if (optarg != 0) goto noValueForOption; printVersion(); -- cgit 1.2.3-korg