Browse code

- core: load_module - compatibility with K mod paths

- preservation of SER and Kamailio module loading styles
- loadmodule accepts "modname.so"

loadpath "searchpath"
loadmodule "modpath"

- when modpath is modname or modname.so, search module at:
- searchpath/modname.so
- searchpath/modname/modname.so
- if modpath does not start with '/', search module at:
- modpath
- searchpath/modpath

Daniel-Constantin Mierla authored on 29/05/2009 10:08:05
Showing 1 changed files
... ...
@@ -260,7 +260,7 @@ int load_module(char* mod_path)
260 260
 	unsigned* mod_if_ver;
261 261
 	struct sr_module* t;
262 262
 	struct stat stat_buf;
263
-	char* modname;
263
+	str modname;
264 264
 	char* mdir;
265 265
 	char* nxt_mdir;
266 266
 	char* path;
... ...
@@ -269,71 +269,114 @@ int load_module(char* mod_path)
269 269
 	int dlflags;
270 270
 	int new_dlflags;
271 271
 	int retries;
272
+	int path_type;
272 273
 
273 274
 #ifndef RTLD_NOW
274 275
 /* for openbsd */
275 276
 #define RTLD_NOW DL_LAZY
276 277
 #endif
277 278
 	path=mod_path;
278
-	if (!strchr(path, '/') && !strchr(path, '.')) {
279
+	path_type = 0;
280
+	modname.s = path;
281
+	modname.len = strlen(mod_path);
282
+	if(modname.len>3 && strcmp(modname.s+modname.len-3, ".so")==0) {
283
+		path_type = 1;
284
+		modname.len -= 3;
285
+	}
286
+	if (!strchr(path, '/'))
287
+		path_type |= 2;
288
+	if((path_type&2) || path[0] != '/') {
279 289
 		/* module name was given, we try to construct the path */
280
-		modname = path;
281 290
 		mdir=mods_dir; /* search path */
282 291
 		do{
283 292
 			nxt_mdir=strchr(mdir, ':');
284 293
 			if (nxt_mdir) mdir_len=(int)(nxt_mdir-mdir);
285 294
 			else mdir_len=strlen(mdir);
286 295
 			
287
-			/* try path <MODS_DIR>/<modname>.so */
288
-			path = (char*)pkg_malloc(mdir_len + 1 /* "/" */ +
289
-									strlen(modname) + 3 /* ".so" */ + 1);
290
-			if (path==0) goto error;
291
-			memcpy(path, mdir, mdir_len);
292
-			len = mdir_len;
293
-			if (len != 0 && path[len - 1] != '/'){
294
-				path[len]='/';
295
-				len++;
296
-			}
297
-			path[len]=0;
298
-			strcat(path, modname);
299
-			strcat(path, ".so");
300
-
301
-			if (stat(path, &stat_buf) == -1) {
302
-				DBG("load_module: module file not found <%s>\n", path);
303
-				pkg_free(path);
304
-
305
-				/* try path <MODS_DIR>/<modname>/<modname>.so */
306
-				path = (char*)pkg_malloc(
307
-					mdir_len + 1 /* "/" */ +
308
-					strlen(modname) + 1 /* "/" */ +
309
-					strlen(modname) + 3 /* ".so" */ + 1);
296
+			if(path_type&2) {
297
+				/* try path <MODS_DIR>/<modname>.so */
298
+				path = (char*)pkg_malloc(mdir_len + 1 /* "/" */ +
299
+									modname.len + 3 /* ".so" */ + 1);
310 300
 				if (path==0) goto error;
311 301
 				memcpy(path, mdir, mdir_len);
312 302
 				len = mdir_len;
313
-				if (len != 0 && path[len - 1] != '/') {
303
+				if (len != 0 && path[len - 1] != '/'){
314 304
 					path[len]='/';
315 305
 					len++;
316 306
 				}
317 307
 				path[len]=0;
318
-				strcat(path, modname);
319
-				strcat(path, "/");
320
-				strcat(path, modname);
321
-				strcat(path, ".so");
308
+				strcat(path, modname.s);
309
+				if(!(path_type&1))
310
+					strcat(path, ".so");
322 311
 
323 312
 				if (stat(path, &stat_buf) == -1) {
324 313
 					DBG("load_module: module file not found <%s>\n", path);
325 314
 					pkg_free(path);
326
-					path=0;
315
+
316
+					/* try path <MODS_DIR>/<modname>/<modname>.so */
317
+					path = (char*)pkg_malloc(
318
+						mdir_len + 1 /* "/" */ +
319
+						modname.len + 1 /* "/" */ +
320
+						modname.len + 3 /* ".so" */ + 1);
321
+					if (path==0) goto error;
322
+					memcpy(path, mdir, mdir_len);
323
+					len = mdir_len;
324
+					if (len != 0 && path[len - 1] != '/') {
325
+						path[len]='/';
326
+						len++;
327
+					}
328
+					path[len]=0;
329
+					strncat(path, modname.s, modname.len);
330
+					strcat(path, "/");
331
+					strcat(path, modname.s);
332
+					if(!(path_type&1))
333
+						strcat(path, ".so");
334
+
335
+					if (stat(path, &stat_buf) == -1) {
336
+						DBG("load_module: module file not found <%s>\n", path);
337
+						pkg_free(path);
338
+						path=0;
339
+					}
340
+				}
341
+			} else {
342
+				/* try mod_path - S compat */
343
+				if(path==mod_path) {
344
+					if (stat(path, &stat_buf) == -1) {
345
+						DBG("load_module: module file not found <%s>\n", path);
346
+						path=0;
347
+					}
348
+				}
349
+				if(path==0) {
350
+					/* try path <MODS_DIR>/mod_path - K compat */
351
+					path = (char*)pkg_malloc(mdir_len + 1 /* "/" */ +
352
+									strlen(mod_path) + 1);
353
+					if (path==0) goto error;
354
+					memcpy(path, mdir, mdir_len);
355
+					len = mdir_len;
356
+					if (len != 0 && path[len - 1] != '/'){
357
+						path[len]='/';
358
+						len++;
359
+					}
360
+					path[len]=0;
361
+					strcat(path, mod_path);
362
+
363
+					if (stat(path, &stat_buf) == -1) {
364
+						DBG("load_module: module file not found <%s>\n", path);
365
+						pkg_free(path);
366
+						path=0;
367
+					}
327 368
 				}
328 369
 			}
329 370
 			mdir=nxt_mdir?nxt_mdir+1:0;
330 371
 		}while(path==0 && mdir);
331 372
 		if (path==0){
332
-			LOG(L_ERR, "ERROR: load_module: could not find module <%s> in"
333
-						" <%s>\n", modname, mods_dir);
373
+			LOG(L_ERR, "ERROR: load_module: could not find module <%.*s> in"
374
+						" <%s>\n", modname.len, modname.s, mods_dir);
334 375
 			goto error;
335 376
 		}
336 377
 	}
378
+	DBG("load_module: trying to load <%s>\n", path);
379
+
337 380
 	retries=2;
338 381
 	dlflags=RTLD_NOW;
339 382
 reload: