lib/util: consolidate module loading into common code
[samba.git] / source4 / param / util.c
index b1a7a571e65ea8c3230d25f9fa2597de4935724c..472096f60d295ccdd08b071a7165de82d09406db 100644 (file)
@@ -100,6 +100,62 @@ char *lpcfg_lock_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
        return fname;
 }
 
+/**
+ A useful function for returning a path in the Samba state directory.
+**/
+char *lpcfg_state_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
+                      const char *name)
+{
+       char *fname, *dname;
+       if (name == NULL) {
+               return NULL;
+       }
+       if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
+               return talloc_strdup(mem_ctx, name);
+       }
+
+       dname = talloc_strdup(mem_ctx, lpcfg_statedir(lp_ctx));
+       trim_string(dname,"","/");
+
+       if (!directory_exist(dname)) {
+               mkdir(dname,0755);
+       }
+
+       fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
+
+       talloc_free(dname);
+
+       return fname;
+}
+
+/**
+ A useful function for returning a path in the Samba cache directory.
+**/
+char *lpcfg_cache_path(TALLOC_CTX* mem_ctx, struct loadparm_context *lp_ctx,
+                      const char *name)
+{
+       char *fname, *dname;
+       if (name == NULL) {
+               return NULL;
+       }
+       if (name[0] == 0 || name[0] == '/' || strstr(name, ":/")) {
+               return talloc_strdup(mem_ctx, name);
+       }
+
+       dname = talloc_strdup(mem_ctx, lpcfg_cachedir(lp_ctx));
+       trim_string(dname,"","/");
+
+       if (!directory_exist(dname)) {
+               mkdir(dname,0755);
+       }
+
+       fname = talloc_asprintf(mem_ctx, "%s/%s", dname, name);
+
+       talloc_free(dname);
+
+       return fname;
+}
+
 /**
  * @brief Returns an absolute path to a file in the directory containing the current config file
  *
@@ -180,110 +236,6 @@ char *smbd_tmp_path(TALLOC_CTX *mem_ctx,
        return fname;
 }
 
-/**
- * Obtain the init function from a shared library file
- */
-init_module_fn load_module(TALLOC_CTX *mem_ctx, const char *path)
-{
-       void *handle;
-       void *init_fn;
-
-       handle = dlopen(path, RTLD_NOW);
-       if (handle == NULL) {
-               DEBUG(0, ("Unable to open %s: %s\n", path, dlerror()));
-               return NULL;
-       }
-
-       init_fn = dlsym(handle, SAMBA_INIT_MODULE);
-
-       if (init_fn == NULL) {
-               DEBUG(0, ("Unable to find %s() in %s: %s\n", 
-                         SAMBA_INIT_MODULE, path, dlerror()));
-               DEBUG(1, ("Loading module '%s' failed\n", path));
-               dlclose(handle);
-               return NULL;
-       }
-
-       return (init_module_fn)init_fn;
-}
-
-/**
- * Obtain list of init functions from the modules in the specified
- * directory
- */
-init_module_fn *load_modules(TALLOC_CTX *mem_ctx, const char *path)
-{
-       DIR *dir;
-       struct dirent *entry;
-       char *filename;
-       int success = 0;
-       init_module_fn *ret = talloc_array(mem_ctx, init_module_fn, 2);
-
-       ret[0] = NULL;
-       
-       dir = opendir(path);
-       if (dir == NULL) {
-               talloc_free(ret);
-               return NULL;
-       }
-
-       while((entry = readdir(dir))) {
-               if (ISDOT(entry->d_name) || ISDOTDOT(entry->d_name))
-                       continue;
-
-               filename = talloc_asprintf(mem_ctx, "%s/%s", path, entry->d_name);
-
-               ret[success] = load_module(mem_ctx, filename);
-               if (ret[success]) {
-                       ret = talloc_realloc(mem_ctx, ret, init_module_fn, success+2);
-                       success++;
-                       ret[success] = NULL;
-               }
-
-               talloc_free(filename);
-       }
-
-       closedir(dir);
-
-       return ret;
-}
-
-/**
- * Run the specified init functions.
- *
- * @return true if all functions ran successfully, false otherwise
- */
-bool run_init_functions(init_module_fn *fns)
-{
-       int i;
-       bool ret = true;
-       
-       if (fns == NULL)
-               return true;
-       
-       for (i = 0; fns[i]; i++) { ret &= (bool)NT_STATUS_IS_OK(fns[i]()); }
-
-       return ret;
-}
-
-/**
- * Load the initialization functions from DSO files for a specific subsystem.
- *
- * Will return an array of function pointers to initialization functions
- */
-
-init_module_fn *load_samba_modules(TALLOC_CTX *mem_ctx, const char *subsystem)
-{
-       char *path = modules_path(mem_ctx, subsystem);
-       init_module_fn *ret;
-
-       ret = load_modules(mem_ctx, path);
-
-       talloc_free(path);
-
-       return ret;
-}
-
 const char *lpcfg_imessaging_path(TALLOC_CTX *mem_ctx,
                                       struct loadparm_context *lp_ctx)
 {
@@ -295,9 +247,9 @@ struct smb_iconv_handle *smb_iconv_handle_reinit_lp(TALLOC_CTX *mem_ctx,
                                                              struct smb_iconv_handle *old_ic)
 {
        return smb_iconv_handle_reinit(mem_ctx, lpcfg_dos_charset(lp_ctx),
-                                           lpcfg_unix_charset(lp_ctx),
-                                           lpcfg_parm_bool(lp_ctx, NULL, "iconv", "native", true),
-                                           old_ic);
+                                      lpcfg_unix_charset(lp_ctx),
+                                      true,
+                                      old_ic);
 }