monitoring: improvements
[src/app-framework-binder.git] / src / afb-api-so.c
index a22e444..55e474c 100644 (file)
@@ -16,7 +16,6 @@
  */
 
 #define _GNU_SOURCE
-#define NO_BINDING_VERBOSE_MACRO
 
 #include <stdio.h>
 #include <dlfcn.h>
@@ -30,7 +29,7 @@
 #include "afb-api-so-v2.h"
 #include "verbose.h"
 
-static int load_binding(const char *path, int force)
+static int load_binding(const char *path, int force, struct afb_apiset *apiset)
 {
        int rc;
        void *handle;
@@ -46,26 +45,29 @@ static int load_binding(const char *path, int force)
                goto error;
        }
 
-       /* retrieves the register function */
-       rc = afb_api_so_v2_add(path, handle);
+       /* try the version 2 */
+       rc = afb_api_so_v2_add(path, handle, apiset);
        if (rc < 0) {
                /* error when loading a valid v2 binding */
                goto error2;
        }
-       rc = afb_api_so_v1_add(path, handle);
+       if (rc)
+               return 0; /* yes version 2 */
+
+       /* try the version 1 */
+       rc = afb_api_so_v1_add(path, handle, apiset);
        if (rc < 0) {
                /* error when loading a valid v1 binding */
                goto error2;
        }
-       if (rc == 0) {
-               /* not a v1 binding */
-               if (force)
-                       ERROR("binding [%s] is not an AFB binding", path);
-               else
-                       INFO("binding [%s] is not an AFB binding", path);
-               goto error2;
-       }
-       return 0;
+       if (rc)
+               return 0; /* yes version 1 */
+
+       /* not a valid binding */
+       if (force)
+               ERROR("binding [%s] is not an AFB binding", path);
+       else
+               INFO("binding [%s] is not an AFB binding", path);
 
 error2:
        dlclose(handle);
@@ -74,16 +76,17 @@ error:
 }
 
 
-int afb_api_so_add_binding(const char *path)
+int afb_api_so_add_binding(const char *path, struct afb_apiset *apiset)
 {
-       return load_binding(path, 1);
+       return load_binding(path, 1, apiset);
 }
 
-static int adddirs(char path[PATH_MAX], size_t end)
+static int adddirs(char path[PATH_MAX], size_t end, struct afb_apiset *apiset, int failstops)
 {
        DIR *dir;
        struct dirent *dent;
        size_t len;
+       int rc = 0;
 
        /* open the DIR now */
        dir = opendir(path);
@@ -119,21 +122,24 @@ static int adddirs(char path[PATH_MAX], size_t end)
                                        continue;
                        }
                        memcpy(&path[end], dent->d_name, len+1);
-                       adddirs(path, end+len);;
+                       rc = adddirs(path, end+len, apiset, failstops);
                } else if (dent->d_type == DT_REG) {
                        /* case of files */
                        if (memcmp(&dent->d_name[len - 3], ".so", 4))
                                continue;
                        memcpy(&path[end], dent->d_name, len+1);
-                       if (load_binding(path, 0) < 0)
-                               return -1;
+                       rc = load_binding(path, 0, apiset);
+               }
+               if (rc < 0 && failstops) {
+                       closedir(dir);
+                       return rc;
                }
        }
        closedir(dir);
        return 0;
 }
 
-int afb_api_so_add_directory(const char *path)
+int afb_api_so_add_directory(const char *path, struct afb_apiset *apiset, int failstops)
 {
        size_t length;
        char buffer[PATH_MAX];
@@ -145,10 +151,10 @@ int afb_api_so_add_directory(const char *path)
        }
 
        memcpy(buffer, path, length + 1);
-       return adddirs(buffer, length);
+       return adddirs(buffer, length, apiset, failstops);
 }
 
-int afb_api_so_add_path(const char *path)
+int afb_api_so_add_path(const char *path, struct afb_apiset *apiset, int failstops)
 {
        struct stat st;
        int rc;
@@ -157,26 +163,38 @@ int afb_api_so_add_path(const char *path)
        if (rc < 0)
                ERROR("Invalid binding path [%s]: %m", path);
        else if (S_ISDIR(st.st_mode))
-               rc = afb_api_so_add_directory(path);
+               rc = afb_api_so_add_directory(path, apiset, failstops);
        else if (strstr(path, ".so"))
-               rc = load_binding(path, 0);
+               rc = load_binding(path, 0, apiset);
        else
                INFO("not a binding [%s], skipped", path);
        return rc;
 }
 
-int afb_api_so_add_pathset(const char *pathset)
+int afb_api_so_add_pathset(const char *pathset, struct afb_apiset *apiset, int failstops)
 {
        static char sep[] = ":";
        char *ps, *p;
+       int rc;
 
        ps = strdupa(pathset);
        for (;;) {
                p = strsep(&ps, sep);
                if (!p)
                        return 0;
-               if (afb_api_so_add_path(p) < 0)
-                       return -1;
+               rc = afb_api_so_add_path(p, apiset, failstops);
+               if (rc < 0)
+                       return rc;
        }
 }
 
+int afb_api_so_add_pathset_fails(const char *pathset, struct afb_apiset *apiset)
+{
+       return afb_api_so_add_pathset(pathset, apiset, 1);
+}
+
+int afb_api_so_add_pathset_nofails(const char *pathset, struct afb_apiset *apiset)
+{
+       return afb_api_so_add_pathset(pathset, apiset, 0);
+}
+