Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.44 KB | None | 0 0
  1. #include "module_loader.h"
  2.  
  3. /*
  4.  * Loads all of the modules in the provided directory.
  5.  * Returns 1 on success or zero on failure.
  6.  */
  7. int
  8. load_modules(char *module_dir)
  9. {
  10.   DIR *dir;
  11.   struct dirent *ent;
  12.   void *handle;
  13.   int (*fn)();
  14.   char *error;
  15.   int len;
  16.   int load_result;
  17.   char *mod_loc;
  18.  
  19.   dir = opendir(module_dir);
  20.   if (dir == NULL) {
  21.     perror("load_modules() failed to open directory");
  22.     exit(EXIT_FAILURE);
  23.   }
  24.  
  25.   /*
  26.    * If the filename contains .so then we attempt
  27.    * to load it as a plugin.
  28.    */
  29.   while ((ent = readdir(dir)) != NULL) {
  30.     char *c = strstr(ent->d_name, ".so");
  31.     if (c != NULL) {
  32.       len = strlen(module_dir) + strlen(ent->d_name) + 1;
  33.       mod_loc = (char *) malloc(len);
  34.       strcat(mod_loc, module_dir);
  35.       strcat(mod_loc, ent->d_name);
  36.  
  37.       handle = dlopen(mod_loc, RTLD_NOW);
  38.       free(mod_loc);
  39.       if (!handle) {
  40.         fprintf(stderr, "Failed open: %s. Could not load module %s\n",
  41.                 dlerror(), ent->d_name);
  42.         return 0;
  43.       }
  44.  
  45.       fn = dlsym(handle, "module_init");
  46.       if ((error = dlerror()) != NULL) {
  47.         fprintf(stderr, "Failed symbol search: %s. Could not load module %s\n",
  48.                 error, ent->d_name);
  49.         return 0;
  50.       }
  51.  
  52.       load_result = (*fn)();
  53.       if (!load_result) {
  54.         printf("Failed to load module %s\n", ent->d_name);
  55.         return 0;
  56.       }
  57.  
  58.       printf("Loaded module: %s\n", ent->d_name);
  59.       dlclose(handle); /* Cleanup, temporary only */
  60.     }
  61.   }
  62.   closedir(dir);
  63.   return 1;
  64. }
  65.  
  66. int
  67. unload_modules()
  68. {
  69.   return 1;
  70. }
  71.  
  72. int
  73. load_module(char *module_dir, char *module_name)
  74. {
  75.   void *handle;
  76.   int (*fn)();
  77.   int load_result;
  78.   char *error;
  79.   char full_module_name[100];
  80.  
  81.   memset(full_module_name, '\0', 100);
  82.   strcat(full_module_name, module_dir);
  83.   strcat(full_module_name, module_name);
  84.   strcat(full_module_name, ".so");
  85.  
  86.   handle = dlopen(full_module_name, RTLD_NOW);
  87.   if (!handle) {
  88.     fprintf(stderr, "1. %s\n", dlerror());
  89.     return 0;
  90.   }
  91.  
  92.   fn = dlsym(handle, "module_init");
  93.   if ((error = dlerror()) != NULL) {
  94.     fprintf(stderr, "2. %s\n", error);
  95.     return 0;
  96.   }
  97.  
  98.   load_result = (*fn)();
  99.   if (!load_result) {
  100.     fprintf(stderr, "Failed to load module %s\n", module_name);
  101.     return 0;
  102.   }
  103.  
  104.   dlclose(handle); /* Cleanup, temporary only */
  105.   return 1;
  106. }
  107.  
  108. int
  109. unload_module(char *module_name)
  110. {
  111.   return 1;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement