Skip to content

Commit ffaa514

Browse files
committed
module dependencies: Be able to easily specify N dependencies
1 parent 790a390 commit ffaa514

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

sr_module_deps.c

+41-2
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,25 @@ static struct sr_module_dep unsolved_deps;
4545
type == MOD_TYPE_AAA ? "aaa module" : \
4646
"module")
4747

48+
4849
module_dependency_t *alloc_module_dep(enum module_type mod_type, char *mod_name,
4950
enum dep_type dep_type)
5051
{
52+
return _alloc_module_dep(mod_type, mod_name, dep_type, MOD_TYPE_NULL);
53+
}
54+
55+
56+
module_dependency_t *_alloc_module_dep(enum module_type mod_type, char *mod_name,
57+
enum dep_type dep_type, ... /* , MOD_TYPE_NULL */)
58+
{
59+
va_list ap;
5160
module_dependency_t *md;
61+
int ndeps = 1;
5262

53-
/* also allocate a zeroed entry in the end */
63+
/* always keep a zeroed entry at the end */
5464
md = pkg_malloc(2 * sizeof *md);
5565
if (!md) {
56-
LM_ERR("out of pkg\n");
66+
LM_ERR("oom\n");
5767
return NULL;
5868
}
5969

@@ -62,9 +72,32 @@ module_dependency_t *alloc_module_dep(enum module_type mod_type, char *mod_name,
6272
md->mod_name = mod_name;
6373
md->type = dep_type;
6474

75+
va_start(ap, dep_type);
76+
77+
for (;;) {
78+
mod_type = va_arg(ap, enum module_type);
79+
if (mod_type == MOD_TYPE_NULL)
80+
break;
81+
82+
ndeps++;
83+
84+
md = pkg_realloc(md, (ndeps + 1) * sizeof *md);
85+
if (!md) {
86+
LM_ERR("oom\n");
87+
return NULL;
88+
}
89+
memset(&md[ndeps], 0, sizeof *md);
90+
91+
md[ndeps - 1].mod_type = mod_type;
92+
md[ndeps - 1].mod_name = va_arg(ap, char *);
93+
md[ndeps - 1].type = va_arg(ap, enum dep_type);
94+
}
95+
96+
va_end(ap);
6597
return md;
6698
}
6799

100+
68101
module_dependency_t *get_deps_sqldb_url(param_export_t *param)
69102
{
70103
char *db_url = *(char **)param->param_pointer;
@@ -78,6 +111,7 @@ module_dependency_t *get_deps_sqldb_url(param_export_t *param)
78111
return alloc_module_dep(MOD_TYPE_SQLDB, NULL, DEP_WARN);
79112
}
80113

114+
81115
module_dependency_t *get_deps_cachedb_url(param_export_t *param)
82116
{
83117
char *cdb_url = *(char **)param->param_pointer;
@@ -88,6 +122,7 @@ module_dependency_t *get_deps_cachedb_url(param_export_t *param)
88122
return alloc_module_dep(MOD_TYPE_CACHEDB, NULL, DEP_ABORT);
89123
}
90124

125+
91126
static int add_module_dependency(struct sr_module *mod, module_dependency_t *dep,
92127
char *script_param)
93128
{
@@ -125,6 +160,7 @@ static int add_module_dependency(struct sr_module *mod, module_dependency_t *dep
125160
return 0;
126161
}
127162

163+
128164
/*
129165
* register all OpenSIPS module dependencies of a single module parameter
130166
*/
@@ -178,6 +214,7 @@ int add_modparam_dependencies(struct sr_module *mod, param_export_t *param)
178214
return 0;
179215
}
180216

217+
181218
/*
182219
* register all OpenSIPS module dependencies of a single module
183220
*/
@@ -195,6 +232,7 @@ int add_module_dependencies(struct sr_module *mod)
195232
return 0;
196233
}
197234

235+
198236
int solve_module_dependencies(struct sr_module *modules)
199237
{
200238
struct sr_module_dep *md, *it;
@@ -317,6 +355,7 @@ int solve_module_dependencies(struct sr_module *modules)
317355
return 0;
318356
}
319357

358+
320359
/* after all modules are properly loaded, free all sr_module_dep structures */
321360
void free_module_dependencies(struct sr_module *modules)
322361
{

sr_module_deps.h

+10
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@
5656
* - it is up to the module writers to prevent such side effects
5757
*/
5858

59+
#include <stdarg.h>
60+
5961
#include "str.h"
6062

6163
#define MAX_MOD_DEPS 10
@@ -90,10 +92,18 @@ typedef struct modparam_dependency {
9092
struct module_dependency *(*get_deps_f)(param_export_t *param);
9193
} modparam_dependency_t;
9294

95+
9396
/* helps to avoid duplicate code when writing "get_deps_f" functions */
9497
module_dependency_t *alloc_module_dep(enum module_type mod_type, char *mod_name,
9598
enum dep_type dep_type);
9699

100+
101+
/* same as above, but with VLA, (3 * N + 1) arguments
102+
* and _must_ end with the special MOD_TYPE_NULL value */
103+
module_dependency_t *_alloc_module_dep(enum module_type mod_type, char *mod_name,
104+
enum dep_type dep_type, ... /* , MOD_TYPE_NULL */);
105+
106+
97107
/* commonly used modparam dependency functions */
98108

99109
/**

0 commit comments

Comments
 (0)