2 * Copyright (C) 2015 "IoT.bzh"
3 * Author "Fulup Ar Foll"
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 * Contain all generic part to handle REST/API
20 * https://www.gnu.org/software/libmicrohttpd/tutorial.html [search 'largepost.c']
23 #include "../include/local-def.h"
30 #define AFB_MSG_JTYPE "AJB_reply"
34 static json_object *afbJsonType;
37 // Because of POST call multiple time requestApi we need to free POST handle here
38 // Note this method is called from http-svc just before closing session
39 PUBLIC void endPostRequest(AFB_PostHandle *postHandle) {
41 if (postHandle->type == AFB_POST_JSON) {
42 // if (verbose) fprintf(stderr, "End PostJson Request UID=%d\n", postHandle->uid);
45 if (postHandle->type == AFB_POST_FORM) {
46 if (verbose) fprintf(stderr, "End PostForm Request UID=%d\n", postHandle->uid);
48 if (postHandle->privatebuf) free(postHandle->privatebuf);
52 // Check of apiurl is declare in this plugin and call it
53 STATIC AFB_error callPluginApi(AFB_request *request, int plugidx, void *context) {
54 json_object *jresp, *jcall, *jreqt;
56 AFB_clientCtx *clientCtx;
57 AFB_plugin *plugin = request->plugins[plugidx];
58 int signals[]= {SIGALRM, SIGSEGV, SIGFPE, 0};
60 /*---------------------------------------------------------------
61 | Signal handler defined inside CallPluginApi to access Request
62 +---------------------------------------------------------------- */
63 void pluginError (int signum) {
67 // unlock signal to allow a new signal to come
68 sigemptyset (&sigset);
69 sigaddset (&sigset, signum);
70 sigprocmask (SIG_UNBLOCK, &sigset, 0);
72 fprintf (stderr, "Oops:%s Plugin Api Timeout timeout\n", configTime());
73 longjmp (request->checkPluginCall, signum);
77 // If a plugin hold this urlpath call its callback
78 for (idx = 0; plugin->apis[idx].callback != NULL; idx++) {
79 if (!strcmp(plugin->apis[idx].name, request->api)) {
81 // Request was found and at least partially executed
82 jreqt = json_object_new_object();
83 json_object_get (afbJsonType); // increate jsontype reference count
84 json_object_object_add (jreqt, "jtype", afbJsonType);
86 // prepare an object to store calling values
87 jcall=json_object_new_object();
88 json_object_object_add(jcall, "prefix", json_object_new_string (plugin->prefix));
89 json_object_object_add(jcall, "api" , json_object_new_string (plugin->apis[idx].name));
91 // save context before calling the API
92 status = setjmp (request->checkPluginCall);
95 // Plugin aborted somewhere during its execution
96 json_object_object_add(jcall, "status", json_object_new_string ("abort"));
97 json_object_object_add(jcall, "info" , json_object_new_string ("Plugin broke during execution"));
98 json_object_object_add(jreqt, "request", jcall);
102 // If timeout protection==0 we are in debug and we do not apply signal protection
103 if (request->config->apiTimeout > 0) {
104 for (sig=0; signals[sig] != 0; sig++) {
105 if (signal (signals[sig], pluginError) == SIG_ERR) {
106 request->errcode = MHD_HTTP_UNPROCESSABLE_ENTITY;
107 json_object_object_add(jcall, "status", json_object_new_string ("fail"));
108 json_object_object_add(jcall, "info", json_object_new_string ("Setting Timeout Handler Failed"));
109 json_object_object_add(jreqt, "request", jcall);
113 // Trigger a timer to protect from unacceptable long time execution
114 alarm (request->config->apiTimeout);
117 // Out of SessionNone every call get a client context session
118 if (AFB_SESSION_NONE != plugin->apis[idx].session) {
120 // add client context to request
121 clientCtx = ctxClientGet(request, plugidx);
122 if (clientCtx == NULL) {
123 request->errcode=MHD_HTTP_INSUFFICIENT_STORAGE;
124 json_object_object_add(jcall, "status", json_object_new_string ("fail"));
125 json_object_object_add(jcall, "info", json_object_new_string ("Client Session Context Full !!!"));
126 json_object_object_add(jreqt, "request", jcall);
130 if (verbose) fprintf(stderr, "Plugin=[%s] Api=[%s] Middleware=[%d] Client=[0x%x] Uuid=[%s] Token=[%s]\n"
131 , request->prefix, request->api, plugin->apis[idx].session, clientCtx, clientCtx->uuid, clientCtx->token);
133 switch(plugin->apis[idx].session) {
135 case AFB_SESSION_CREATE:
136 if (clientCtx->token[0] != '\0') {
137 request->errcode=MHD_HTTP_UNAUTHORIZED;
138 json_object_object_add(jcall, "status", json_object_new_string ("exist"));
139 json_object_object_add(jcall, "info", json_object_new_string ("AFB_SESSION_CREATE Session already exist"));
140 json_object_object_add(jreqt, "request", jcall);
144 if (AFB_SUCCESS != ctxTokenCreate (clientCtx, request)) {
145 request->errcode=MHD_HTTP_UNAUTHORIZED;
146 json_object_object_add(jcall, "status", json_object_new_string ("fail"));
147 json_object_object_add(jcall, "info", json_object_new_string ("AFB_SESSION_CREATE Invalid Initial Token"));
148 json_object_object_add(jreqt, "request", jcall);
151 json_object_object_add(jcall, "uuid", json_object_new_string (clientCtx->uuid));
152 json_object_object_add(jcall, "token", json_object_new_string (clientCtx->token));
153 json_object_object_add(jcall, "timeout", json_object_new_int (request->config->cntxTimeout));
158 case AFB_SESSION_RENEW:
159 if (AFB_SUCCESS != ctxTokenRefresh (clientCtx, request)) {
160 request->errcode=MHD_HTTP_UNAUTHORIZED;
161 json_object_object_add(jcall, "status", json_object_new_string ("fail"));
162 json_object_object_add(jcall, "info", json_object_new_string ("AFB_SESSION_REFRESH Broken Exchange Token Chain"));
163 json_object_object_add(jreqt, "request", jcall);
166 json_object_object_add(jcall, "uuid", json_object_new_string (clientCtx->uuid));
167 json_object_object_add(jcall, "token", json_object_new_string (clientCtx->token));
168 json_object_object_add(jcall, "timeout", json_object_new_int (request->config->cntxTimeout));
172 case AFB_SESSION_CLOSE:
173 if (AFB_SUCCESS != ctxTokenCheck (clientCtx, request)) {
174 request->errcode=MHD_HTTP_UNAUTHORIZED;
175 json_object_object_add(jcall, "status", json_object_new_string ("empty"));
176 json_object_object_add(jcall, "info", json_object_new_string ("AFB_SESSION_CLOSE Not a Valid Access Token"));
177 json_object_object_add(jreqt, "request", jcall);
180 json_object_object_add(jcall, "uuid", json_object_new_string (clientCtx->uuid));
184 case AFB_SESSION_CHECK:
186 // default action is check
187 if (AFB_SUCCESS != ctxTokenCheck (clientCtx, request)) {
188 request->errcode=MHD_HTTP_UNAUTHORIZED;
189 json_object_object_add(jcall, "status", json_object_new_string ("fail"));
190 json_object_object_add(jcall, "info", json_object_new_string ("AFB_SESSION_CHECK Invalid Active Token"));
191 json_object_object_add(jreqt, "request", jcall);
198 // Effectively CALL PLUGIN API with a subset of the context
199 jresp = plugin->apis[idx].callback(request, context);
201 // prefix response with request object;
202 request->jresp = jreqt;
204 // Store context in case it was updated by plugins
205 if (request->context != NULL) clientCtx->contexts[plugidx] = request->context;
207 // handle intermediary Post Iterates out of band
208 if ((jresp == NULL) && (request->errcode == MHD_HTTP_OK)) return (AFB_SUCCESS);
210 // Session close is done after the API call so API can still use session in closing API
211 if (AFB_SESSION_CLOSE == plugin->apis[idx].session) ctxTokenReset (clientCtx, request);
213 // API should return NULL of a valid Json Object
215 json_object_object_add(jcall, "status", json_object_new_string ("null"));
216 json_object_object_add(request->jresp, "request", jcall);
217 request->errcode = MHD_HTTP_NO_RESPONSE;
220 json_object_object_add(jcall, "status", json_object_new_string ("processed"));
221 json_object_object_add(request->jresp, "request", jcall);
222 json_object_object_add(request->jresp, "response", jresp);
224 // cancel timeout and plugin signal handle before next call
225 if (request->config->apiTimeout > 0) {
227 for (sig=0; signals[sig] != 0; sig++) {
228 signal (signals[sig], SIG_DFL);
238 STATIC AFB_error findAndCallApi (AFB_request *request, void *context) {
242 if (!request->api || !request->prefix) return (AFB_FAIL);
244 // Search for a plugin with this urlpath
245 for (idx = 0; request->plugins[idx] != NULL; idx++) {
246 if (!strcmp(request->plugins[idx]->prefix, request->prefix)) {
247 status =callPluginApi(request, idx, context);
251 // No plugin was found
252 if (request->plugins[idx] == NULL) {
253 request->jresp = jsonNewMessage(AFB_FATAL, "No Plugin=[%s] Url=%s", request->prefix, request->url);
257 // plugin callback did not return a valid Json Object
258 if (status == AFB_FAIL) {
259 request->jresp = jsonNewMessage(AFB_FATAL, "No API=[%s] for Plugin=[%s] url=[%s]", request->api, request->prefix, request->url);
263 // Everything look OK
267 request->errcode = MHD_HTTP_UNPROCESSABLE_ENTITY;
271 // This CB is call for every item with a form post it reformat iterator values
272 // and callback Plugin API for each Item within PostForm.
273 STATIC int doPostIterate (void *cls, enum MHD_ValueKind kind, const char *key,
274 const char *filename, const char *mimetype,
275 const char *encoding, const char *data, uint64_t offset,
281 // retrieve API request from Post iterator handle
282 AFB_PostHandle *postHandle = (AFB_PostHandle*)cls;
283 AFB_request *request = (AFB_request*)postHandle->privatebuf;
284 AFB_PostRequest postRequest;
287 fprintf (stderr, "postHandle key=%s filename=%s len=%d mime=%s\n", key, filename, size, mimetype);
289 // Create and Item value for Plugin API
292 item.filename = filename;
293 item.mimetype = mimetype;
294 item.encoding = encoding;
297 item.offset = offset;
299 // Reformat Request to make it somehow similar to GET/PostJson case
300 postRequest.data= (char*) postHandle;
301 postRequest.len = size;
302 postRequest.type= AFB_POST_FORM;;
303 request->post = &postRequest;
305 // effectively call plugin API
306 status = findAndCallApi (request, &item);
307 // when returning no processing of postform stop
308 if (status != AFB_SUCCESS) return MHD_NO;
310 // let's allow iterator to move to next item
314 STATIC void freeRequest (AFB_request *request) {
316 free (request->prefix);
321 STATIC AFB_request *createRequest (struct MHD_Connection *connection, AFB_session *session, const char* url) {
323 AFB_request *request;
326 // Start with a clean request
327 request = calloc (1, sizeof (AFB_request));
328 char *urlcpy1, *urlcpy2;
329 char *baseapi, *baseurl;
331 // Extract plugin urlpath from request and make two copy because strsep overload copy
332 urlcpy1 = urlcpy2 = strdup(url);
333 baseurl = strsep(&urlcpy2, "/");
334 if (baseurl == NULL) {
335 request->jresp = jsonNewMessage(AFB_FATAL, "Invalid API call url=[%s]", url);
336 request->errcode = MHD_HTTP_BAD_REQUEST;
340 // let's compute URL and call API
341 baseapi = strsep(&urlcpy2, "/");
342 if (baseapi == NULL) {
343 request->jresp = jsonNewMessage(AFB_FATAL, "Invalid API call plugin=[%s] url=[%s]", baseurl, url);
344 request->errcode = MHD_HTTP_BAD_REQUEST;
348 // build request structure
349 request->connection = connection;
350 request->config = session->config;
352 request->prefix = strdup (baseurl);
353 request->api = strdup (baseapi);
354 request->plugins= session->plugins;
355 // note request->handle is fed with request->context in ctxClientGet
362 // process rest API query
363 PUBLIC int doRestApi(struct MHD_Connection *connection, AFB_session *session, const char* url, const char *method
364 , const char *upload_data, size_t *upload_data_size, void **con_cls) {
366 static int postcount = 0; // static counter to debug POST protocol
367 json_object *errMessage;
369 struct MHD_Response *webResponse;
370 const char *serialized;
371 AFB_request *request;
372 AFB_PostHandle *postHandle;
373 AFB_PostRequest postRequest;
376 // fprintf (stderr, "doRestAPI method=%s posthandle=0x%x\n", method, con_cls);
378 // if post data may come in multiple calls
379 if (0 == strcmp(method, MHD_HTTP_METHOD_POST)) {
380 const char *encoding, *param;
382 postHandle = *con_cls;
384 // This is the initial post event let's create form post structure POST data come in multiple events
385 if (postHandle == NULL) {
387 // allocate application POST processor handle to zero
388 postHandle = calloc(1, sizeof (AFB_PostHandle));
389 postHandle->uid = postcount++; // build a UID for DEBUG
390 *con_cls = postHandle; // update context with posthandle
392 // Let make sure we have the right encoding and a valid length
393 encoding = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_CONTENT_TYPE);
395 // We are facing an empty post let's process it as a get
396 if (encoding == NULL) {
397 postHandle->type = AFB_POST_EMPTY;
401 // Form post is handle through a PostProcessor and call API once per form key
402 if (strcasestr(encoding, FORM_CONTENT) != NULL) {
403 if (verbose) fprintf(stderr, "Create doPostIterate[uid=%d posthandle=0x%x]\n", postHandle->uid, postHandle);
405 request = createRequest (connection, session, url);
406 if (request->jresp != NULL) goto ProcessApiCall;
407 postHandle->type = AFB_POST_FORM;
408 postHandle->privatebuf = (void*)request;
409 postHandle->pp = MHD_create_post_processor (connection, MAX_POST_SIZE, &doPostIterate, postHandle);
411 if (NULL == postHandle->pp) {
412 fprintf(stderr,"OOPS: Internal error fail to allocate MHD_create_post_processor\n");
419 // POST json is store into a buffer and present in one piece to API
420 if (strcasestr(encoding, JSON_CONTENT) != NULL) {
422 param = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_CONTENT_LENGTH);
423 if (param) sscanf(param, "%i", &contentlen);
425 // Because PostJson are build in RAM size is constrained
426 if (contentlen > MAX_POST_SIZE) {
427 errMessage = jsonNewMessage(AFB_FATAL, "Post Date to big %d > %d", contentlen, MAX_POST_SIZE);
431 // Size is OK, let's allocate a buffer to hold post data
432 postHandle->type = AFB_POST_JSON;
433 postHandle->privatebuf = malloc(contentlen + 1); // allocate memory for full POST data + 1 for '\0' enf of string
435 // if (verbose) fprintf(stderr, "Create PostJson[uid=%d] Size=%d\n", postHandle->uid, contentlen);
439 // We only support Json and Form Post format
440 errMessage = jsonNewMessage(AFB_FATAL, "Post Date wrong type encoding=%s != %s", encoding, JSON_CONTENT);
445 // This time we receive partial/all Post data. Note that even if we get all POST data. We should nevertheless
446 // return MHD_YES and not process the request directly. Otherwise Libmicrohttpd is unhappy and fails with
447 // 'Internal application error, closing connection'.
448 if (*upload_data_size) {
450 if (postHandle->type == AFB_POST_FORM) {
451 // if (verbose) fprintf(stderr, "Processing PostForm[uid=%d]\n", postHandle->uid);
452 MHD_post_process (postHandle->pp, upload_data, *upload_data_size);
455 // Process JsonPost request when buffer is completed let's call API
456 if (postHandle->type == AFB_POST_JSON) {
457 // if (verbose) fprintf(stderr, "Updating PostJson[uid=%d]\n", postHandle->uid);
458 memcpy(&postHandle->privatebuf[postHandle->len], upload_data, *upload_data_size);
459 postHandle->len = postHandle->len + *upload_data_size;
462 *upload_data_size = 0;
465 } else { // we have finish with Post reception let's finish the work
467 // Create a request structure to finalise the request
468 request= createRequest (connection, session, url);
469 if (request->jresp != NULL) {
470 errMessage = request->jresp;
473 postRequest.type = postHandle->type;
475 // Postform add application context handle to request
476 if (postHandle->type == AFB_POST_FORM) {
477 postRequest.data = (char*) postHandle;
478 request->post = &postRequest;
481 if (postHandle->type == AFB_POST_JSON) {
482 // if (verbose) fprintf(stderr, "Processing PostJson[uid=%d]\n", postHandle->uid);
484 param = MHD_lookup_connection_value(connection, MHD_HEADER_KIND, MHD_HTTP_HEADER_CONTENT_LENGTH);
485 if (param) sscanf(param, "%i", &contentlen);
487 // At this level we're may verify that we got everything and process DATA
488 if (postHandle->len != contentlen) {
489 errMessage = jsonNewMessage(AFB_FATAL, "Post Data Incomplete UID=%d Len %d != %d", postHandle->uid, contentlen, postHandle->len);
493 // Before processing data, make sure buffer string is properly ended
494 postHandle->privatebuf[postHandle->len] = '\0';
495 postRequest.data = postHandle->privatebuf;
496 request->post = &postRequest;
498 // if (verbose) fprintf(stderr, "Close Post[%d] Buffer=%s\n", postHandle->uid, request->post->data);
502 // this is a get we only need a request
503 request= createRequest (connection, session, url);
507 // Request is ready let's call API without any extra handle
508 status = findAndCallApi (request, NULL);
510 serialized = json_object_to_json_string(request->jresp);
511 webResponse = MHD_create_response_from_buffer(strlen(serialized), (void*) serialized, MHD_RESPMEM_MUST_COPY);
513 // client did not pass token on URI let's use cookies
514 if ((!request->restfull) && (request->context != NULL)) {
516 snprintf (cookie, sizeof (cookie), "%s=%s;path=%s;max-age=%d", COOKIE_NAME, request->uuid, request->config->rootapi,request->config->cntxTimeout);
517 MHD_add_response_header (webResponse, MHD_HTTP_HEADER_SET_COOKIE, cookie);
520 // if requested add an error status
521 if (request->errcode != 0) ret=MHD_queue_response (connection, request->errcode, webResponse);
522 else MHD_queue_response(connection, MHD_HTTP_OK, webResponse);
524 MHD_destroy_response(webResponse);
525 json_object_put(request->jresp); // decrease reference rqtcount to free the json object
526 freeRequest (request);
530 freeRequest (request);
531 serialized = json_object_to_json_string(errMessage);
532 webResponse = MHD_create_response_from_buffer(strlen(serialized), (void*) serialized, MHD_RESPMEM_MUST_COPY);
533 MHD_queue_response(connection, MHD_HTTP_BAD_REQUEST, webResponse);
534 MHD_destroy_response(webResponse);
535 json_object_put(errMessage); // decrease reference rqtcount to free the json object
540 // Loop on plugins. Check that they have the right type, prepare a JSON object with prefix
541 STATIC AFB_plugin ** RegisterJsonPlugins(AFB_plugin **plugins) {
544 for (idx = 0; plugins[idx] != NULL; idx++) {
545 if (plugins[idx]->type != AFB_PLUGIN_JSON) {
546 fprintf(stderr, "ERROR: AFSV plugin[%d] invalid type=%d != %d\n", idx, AFB_PLUGIN_JSON, plugins[idx]->type);
548 // some sanity controls
549 if ((plugins[idx]->prefix == NULL) || (plugins[idx]->info == NULL) || (plugins[idx]->apis == NULL)) {
550 if (plugins[idx]->prefix == NULL) plugins[idx]->prefix = "No URL prefix for APIs";
551 if (plugins[idx]->info == NULL) plugins[idx]->info = "No Info describing plugin APIs";
552 fprintf(stderr, "ERROR: plugin[%d] invalid prefix=%s info=%s", idx, plugins[idx]->prefix, plugins[idx]->info);
556 if (verbose) fprintf(stderr, "Loading plugin[%d] prefix=[%s] info=%s\n", idx, plugins[idx]->prefix, plugins[idx]->info);
558 // Prebuild plugin jtype to boost API response
559 plugins[idx]->jtype = json_object_new_string(plugins[idx]->prefix);
560 json_object_get(plugins[idx]->jtype); // increase reference count to make it permanent
561 plugins[idx]->prefixlen = strlen(plugins[idx]->prefix);
564 // Prebuild each API jtype to boost API json response
565 for (jdx = 0; plugins[idx]->apis[jdx].name != NULL; jdx++) {
566 AFB_privateApi *privateapi = malloc (sizeof (AFB_privateApi));
567 if (plugins[idx]->apis[jdx].privateapi != NULL) {
568 fprintf (stderr, "WARNING: plugin=%s api=%s private handle should be NULL=0x%x\n"
569 ,plugins[idx]->prefix,plugins[idx]->apis[jdx].name, plugins[idx]->apis[jdx].privateapi);
571 privateapi->len = strlen (plugins[idx]->apis[jdx].name);
572 privateapi->jtype=json_object_new_string(plugins[idx]->apis[jdx].name);
573 json_object_get(privateapi->jtype); // increase reference count to make it permanent
574 plugins[idx]->apis[jdx].privateapi = privateapi;
581 STATIC void scanDirectory(char *dirpath, int dirfd, AFB_plugin **plugins, int *count) {
584 struct dirent pluginDir, *result;
585 AFB_plugin* (*pluginRegisterFct)(void);
586 char pluginPath[255];
588 // Open Directory to scan over it
589 dir = fdopendir (dirfd);
591 fprintf(stderr, "ERROR in scanning directory\n");
594 if (verbose) fprintf (stderr, "Scanning dir=[%s] for plugins\n", dirpath);
597 readdir_r(dir, &pluginDir, &result);
598 if (result == NULL) break;
600 // Loop on any contained directory
601 if ((pluginDir.d_type == DT_DIR) && (pluginDir.d_name[0] != '.')) {
602 int fd = openat (dirfd, pluginDir.d_name, O_DIRECTORY);
604 strncpy (newpath, dirpath, sizeof(newpath));
605 strncat (newpath, "/", sizeof(newpath));
606 strncat (newpath, pluginDir.d_name, sizeof(newpath));
608 scanDirectory (newpath, fd, plugins, count);
613 // This is a file but not a plugin let's move to next directory element
614 if (!strstr (pluginDir.d_name, ".so")) continue;
616 // This is a loadable library let's check if it's a plugin
617 snprintf (pluginPath, sizeof(pluginPath), "%s/%s", dirpath, pluginDir.d_name);
618 libso = dlopen (pluginPath, RTLD_NOW | RTLD_LOCAL);
620 // Load fail we ignore this .so file
622 fprintf(stderr, "[%s] is not loadable, continuing...\n", pluginDir.d_name);
626 pluginRegisterFct = dlsym (libso, "pluginRegister");
628 if (!pluginRegisterFct) {
629 fprintf(stderr, "[%s] is not an AFB plugin, continuing...\n", pluginDir.d_name);
633 // if max plugin is reached let's stop searching
634 if (*count == AFB_MAX_PLUGINS) {
635 fprintf(stderr, "[%s] is not loaded [Max Count=%d reached]\n", *count);
639 if (verbose) fprintf(stderr, "[%s] is a valid AFB plugin, loading pos[%d]\n", pluginDir.d_name, *count);
640 plugins[*count] = pluginRegisterFct();
641 if (!plugins[*count]) {
642 if (verbose) fprintf(stderr, "ERROR: plugin [%s] register function failed. continuing...\n", pluginDir.d_name);
650 void initPlugins(AFB_session *session) {
651 AFB_plugin **plugins;
653 afbJsonType = json_object_new_string (AFB_MSG_JTYPE);
658 /* pre-allocate for AFB_MAX_PLUGINS plugins, we will downsize later */
659 plugins = (AFB_plugin **) malloc (AFB_MAX_PLUGINS *sizeof(AFB_plugin*));
661 // Loop on every directory passed in --plugins=xxx
662 while (dirpath = strsep(&session->config->ldpaths, ":")) {
663 // Ignore any directory we fail to open
664 if ((dirfd = open(dirpath, O_DIRECTORY)) <= 0) {
665 fprintf(stderr, "Invalid directory path=[%s]\n", dirpath);
668 scanDirectory (dirpath, dirfd, plugins, &count);
673 fprintf(stderr, "No plugins found, afb-daemon is unlikely to work in this configuration, exiting...\n");
677 // downsize structure to effective number of loaded plugins
678 plugins = (AFB_plugin **)realloc (plugins, (count+1)*sizeof(AFB_plugin*));
679 plugins[count] = NULL;
681 // complete plugins and save them within current sessions
682 session->plugins = RegisterJsonPlugins(plugins);
683 session->config->pluginCount = count;