소스 검색

uid_avp_db: clang-format for coherent indentation and coding style

Victor Seva 2 년 전
부모
커밋
c9ef2603b5
4개의 변경된 파일347개의 추가작업 그리고 301개의 파일을 삭제
  1. 187 146
      src/modules/uid_avp_db/extra_attrs.c
  2. 7 7
      src/modules/uid_avp_db/extra_attrs.h
  3. 152 147
      src/modules/uid_avp_db/uid_avp_db.c
  4. 1 1
      src/modules/uid_avp_db/uid_avp_db.h

+ 187 - 146
src/modules/uid_avp_db/extra_attrs.c

@@ -9,22 +9,41 @@
 #include "../../core/script_cb.h"
 #include "../../core/hashes.h"
 
-#define set_str_val(f,s)	(f).v.lstr=(s); \
+#define set_str_val(f, s) \
+	(f).v.lstr = (s);     \
 	(f).flags = 0;
 
-#define set_str_val_ex(f,s)	if ((s).len) { (f).v.lstr=(s); (f).flags = 0; }  \
-	else (f).flags|=DB_NULL; 
+#define set_str_val_ex(f, s) \
+	if((s).len) {            \
+		(f).v.lstr = (s);    \
+		(f).flags = 0;       \
+	} else                   \
+		(f).flags |= DB_NULL;
 
-#define set_int_val(f,t)	(f).v.int4=t;\
-	(f).flags=0; 
-
-#define get_str_val(rvi,dst)	do{if(!(rvi.flags&DB_NULL)){dst=rvi.v.lstr;} else dst.len = 0;}while(0)
-#define get_int_val(rvi,dst)	do{if(!(rvi.flags&DB_NULL)){dst=rvi.v.int4;} else dst = 0;}while(0)
+#define set_int_val(f, t) \
+	(f).v.int4 = t;       \
+	(f).flags = 0;
 
-typedef struct _registered_table_t {
+#define get_str_val(rvi, dst)        \
+	do {                             \
+		if(!(rvi.flags & DB_NULL)) { \
+			dst = rvi.v.lstr;        \
+		} else                       \
+			dst.len = 0;             \
+	} while(0)
+#define get_int_val(rvi, dst)        \
+	do {                             \
+		if(!(rvi.flags & DB_NULL)) { \
+			dst = rvi.v.int4;        \
+		} else                       \
+			dst = 0;                 \
+	} while(0)
+
+typedef struct _registered_table_t
+{
 	char *id;
 	char *table_name;
-	
+
 	/* column names */
 	char *key_column;
 	char *name_column;
@@ -53,8 +72,9 @@ static registered_table_t *tables = NULL;
 registered_table_t *find_registered_table(const char *id)
 {
 	registered_table_t *t = tables;
-	while (t) {
-		if (strcmp(t->id, id) == 0) return t;
+	while(t) {
+		if(strcmp(t->id, id) == 0)
+			return t;
 		t = t->next;
 	}
 	return NULL;
@@ -62,7 +82,11 @@ registered_table_t *find_registered_table(const char *id)
 
 char *get_token(char *s, str *name, str *value)
 {
-	enum { reading_name, reading_value } state = reading_name;
+	enum
+	{
+		reading_name,
+		reading_value
+	} state = reading_name;
 	/* returns 'token' which has the form name[=value][,]
 	 * replaces separators ,= by binary 0 to allow char* strings */
 
@@ -71,28 +95,29 @@ char *get_token(char *s, str *name, str *value)
 	value->s = NULL;
 	value->len = 0;
 
-	while (*s) {
-		switch (state) {
-			case reading_name: 
-				switch (*s) {
+	while(*s) {
+		switch(state) {
+			case reading_name:
+				switch(*s) {
 					case '=':
-					case ':': 
+					case ':':
 						state = reading_value;
 						value->s = s + 1;
 						*s = 0; /* replace separator */
 						break;
-					case ',': 
+					case ',':
 						*s = 0; /* replace separator */
 						return s + 1;
-					default: name->len++;
+					default:
+						name->len++;
 				}
 				break;
 			case reading_value:
-				if (*s == ',') {
+				if(*s == ',') {
 					*s = 0; /* replace separator */
 					return s + 1;
-				}
-				else value->len++;
+				} else
+					value->len++;
 				break;
 		}
 		s++;
@@ -104,10 +129,13 @@ static int cmp_s(str *a, str *b)
 {
 	int i;
 	/* Warning: none string can be NULL! */
-	if (a->len != b->len) return -1;
-	if (!a->len) return 0; /* equal - empty */
-	for (i = 0; i < a->len; i++) 
-		if (a->s[i] != b->s[i]) return 1;
+	if(a->len != b->len)
+		return -1;
+	if(!a->len)
+		return 0; /* equal - empty */
+	for(i = 0; i < a->len; i++)
+		if(a->s[i] != b->s[i])
+			return 1;
 	return 0;
 }
 
@@ -118,13 +146,13 @@ static inline void cpy(char *dst, str *s)
 }
 
 /* adds new 'extra attribute group' (it adds new table for it) */
-int declare_attr_group(modparam_t type, char* _param)
+int declare_attr_group(modparam_t type, char *_param)
 {
 	registered_table_t *rt;
 	str name, value;
 	str param;
 	char *p;
-	
+
 	static str table = STR_STATIC_INIT("table");
 	static str flag = STR_STATIC_INIT("flag");
 	static str id = STR_STATIC_INIT("id");
@@ -133,20 +161,20 @@ int declare_attr_group(modparam_t type, char* _param)
 	static str value_column = STR_STATIC_INIT("value_column");
 	static str type_column = STR_STATIC_INIT("type_column");
 	static str flags_column = STR_STATIC_INIT("flags_column");
-	
-	if (!(type & PARAM_STR)) {
+
+	if(!(type & PARAM_STR)) {
 		ERR("Invalid parameter type\n");
 		return -1;
 	}
-	if (!_param) {
+	if(!_param) {
 		ERR("invalid parameter value\n");
 		return -1;
 	}
-	param = *((str*)_param);
-    DBG("group def: %.*s\n", param.len, param.s);
-	
+	param = *((str *)_param);
+	DBG("group def: %.*s\n", param.len, param.s);
+
 	rt = pkg_malloc(param.len + sizeof(*rt) + 1);
-	if (!rt) {
+	if(!rt) {
 		PKG_MEM_ERROR;
 		return -1;
 	}
@@ -164,17 +192,25 @@ int declare_attr_group(modparam_t type, char* _param)
 	p = rt->buf;
 	do {
 		p = get_token(p, &name, &value);
-		if (cmp_s(&name, &table) == 0) rt->table_name = value.s;
-		else if (cmp_s(&name, &flag) == 0) rt->flag_name = value.s;
-		else if (cmp_s(&name, &id) == 0) rt->id = value.s;
-		else if (cmp_s(&name, &key_column) == 0) rt->key_column = value.s;
-		else if (cmp_s(&name, &name_column) == 0) rt->name_column = value.s;
-		else if (cmp_s(&name, &type_column) == 0) rt->type_column = value.s;
-		else if (cmp_s(&name, &value_column) == 0) rt->value_column = value.s;
-		else if (cmp_s(&name, &flags_column) == 0) rt->flags_column = value.s;
-	} while (p);
-	
-	if ((!rt->id) || (!rt->flag_name)) {
+		if(cmp_s(&name, &table) == 0)
+			rt->table_name = value.s;
+		else if(cmp_s(&name, &flag) == 0)
+			rt->flag_name = value.s;
+		else if(cmp_s(&name, &id) == 0)
+			rt->id = value.s;
+		else if(cmp_s(&name, &key_column) == 0)
+			rt->key_column = value.s;
+		else if(cmp_s(&name, &name_column) == 0)
+			rt->name_column = value.s;
+		else if(cmp_s(&name, &type_column) == 0)
+			rt->type_column = value.s;
+		else if(cmp_s(&name, &value_column) == 0)
+			rt->value_column = value.s;
+		else if(cmp_s(&name, &flags_column) == 0)
+			rt->flags_column = value.s;
+	} while(p);
+
+	if((!rt->id) || (!rt->flag_name)) {
 		ERR("at least attribute group ID and flags must be given\n");
 		pkg_free(rt);
 		return -1;
@@ -182,12 +218,12 @@ int declare_attr_group(modparam_t type, char* _param)
 	/* insert new element into registered tables */
 
 	rt->flag = register_avpflag(rt->flag_name);
-	if (!rt->flag) {
+	if(!rt->flag) {
 		ERR("can't register AVP flag: %s\n", rt->flag_name);
 		pkg_free(rt);
 		return -1;
 	}
-	
+
 	/* append to the beggining - it doesn't depend on the order */
 	rt->next = tables;
 	tables = rt;
@@ -199,57 +235,56 @@ int declare_attr_group(modparam_t type, char* _param)
  * Variable default_res holds columns which can used by read_attrs. */
 static int init_queries(db_ctx_t *ctx, registered_table_t *t)
 {
-	db_fld_t match[] = {
-		{ .name = t->key_column, .type = DB_STR, .op = DB_EQ },
-		{ .name = NULL }
-	};
+	db_fld_t match[] = {{.name = t->key_column, .type = DB_STR, .op = DB_EQ},
+			{.name = NULL}};
 	db_fld_t query_res[] = {
-		/* Warning: be careful here - the query must have the same result
+			/* Warning: be careful here - the query must have the same result
 		 * as query for user/uri AVPs to be readable by read_attrs */
-		{ .name = t->name_column, .type = DB_STR, .op = DB_EQ },
-		{ .name = t->type_column, .type = DB_INT, .op = DB_EQ },
-		{ .name = t->value_column, .type = DB_STR, .op = DB_EQ },
-		{ .name = t->flags_column, .type = DB_BITMAP, .op = DB_EQ },
-		{ .name = NULL }
-	};
+			{.name = t->name_column, .type = DB_STR, .op = DB_EQ},
+			{.name = t->type_column, .type = DB_INT, .op = DB_EQ},
+			{.name = t->value_column, .type = DB_STR, .op = DB_EQ},
+			{.name = t->flags_column, .type = DB_BITMAP, .op = DB_EQ},
+			{.name = NULL}};
 	db_fld_t add_values[] = {
-		{ .name = t->key_column, .type = DB_STR, .op = DB_EQ },
-		{ .name = t->name_column, .type = DB_STR, .op = DB_EQ },
-		{ .name = t->type_column, .type = DB_INT, .op = DB_EQ },
-		{ .name = t->value_column, .type = DB_STR, .op = DB_EQ },
-		{ .name = t->flags_column, .type = DB_BITMAP, .op = DB_EQ },
-		{ .name = NULL }
-	};
+			{.name = t->key_column, .type = DB_STR, .op = DB_EQ},
+			{.name = t->name_column, .type = DB_STR, .op = DB_EQ},
+			{.name = t->type_column, .type = DB_INT, .op = DB_EQ},
+			{.name = t->value_column, .type = DB_STR, .op = DB_EQ},
+			{.name = t->flags_column, .type = DB_BITMAP, .op = DB_EQ},
+			{.name = NULL}};
 
 	t->query = db_cmd(DB_GET, ctx, t->table_name, query_res, match, NULL);
 	t->remove = db_cmd(DB_DEL, ctx, t->table_name, NULL, match, NULL);
 	t->add = db_cmd(DB_PUT, ctx, t->table_name, NULL, NULL, add_values);
 
-	if (t->query && t->remove && t->add) return 0;
-	else return -1; /* not all queries were initialized */
+	if(t->query && t->remove && t->add)
+		return 0;
+	else
+		return -1; /* not all queries were initialized */
 }
 
 int init_extra_avp_queries(db_ctx_t *ctx)
 {
 	registered_table_t *t = tables;
-	while (t) {
-		if (init_queries(ctx, t) < 0)  return -1;
+	while(t) {
+		if(init_queries(ctx, t) < 0)
+			return -1;
 		t = t->next;
 	}
 	return 0;
 }
 
-static void get_avp_value_ex(avp_t *avp, str *dst, int *type) {
+static void get_avp_value_ex(avp_t *avp, str *dst, int *type)
+{
 	avp_value_t val;
 
 	/* Warning! it uses static buffer from int2str !!! */
 
 	get_avp_val(avp, &val);
-	if (avp->flags & AVP_VAL_STR) {
+	if(avp->flags & AVP_VAL_STR) {
 		*dst = val.s;
 		*type = AVP_VAL_STR;
-	}
-	else { /* probably (!) number */
+	} else { /* probably (!) number */
 		dst->s = int2str(val.n, &dst->len);
 		*type = 0;
 	}
@@ -258,24 +293,28 @@ static void get_avp_value_ex(avp_t *avp, str *dst, int *type) {
 /** Saves attribute into DB with given ID.  The ID must not be NULL. The
  * use_table must be called outside of this function (from interface
  * functions) */
-static inline int save_avp(registered_table_t *t, avp_t *avp, str *id) /* id MUST NOT be NULL */
+static inline int save_avp(
+		registered_table_t *t, avp_t *avp, str *id) /* id MUST NOT be NULL */
 {
 	str *s, v;
 	int type;
 	static str empty = STR_STATIC_INIT("");
-	
+
 	set_str_val(t->add->vals[0], *id);
-	
+
 	s = get_avp_name(avp);
-	if (!s) s = &empty;
+	if(!s)
+		s = &empty;
 	set_str_val(t->add->vals[1], *s);
-	
+
 	get_avp_value_ex(avp, &v, &type);
 	set_int_val(t->add->vals[2], type);
 	set_str_val(t->add->vals[3], v);
-	set_int_val(t->add->vals[4], avp->flags & (AVP_CLASS_ALL | AVP_TRACK_ALL | AVP_NAME_STR | AVP_VAL_STR));
-	
-	if (db_exec(NULL, t->add) < 0) {
+	set_int_val(t->add->vals[4], avp->flags
+										 & (AVP_CLASS_ALL | AVP_TRACK_ALL
+												 | AVP_NAME_STR | AVP_VAL_STR));
+
+	if(db_exec(NULL, t->add) < 0) {
 		ERR("Can't insert record into DB\n");
 		return -1;
 	}
@@ -288,21 +327,23 @@ static int read_avps(db_res_t *res, avp_flags_t flag) /* id must not be NULL */
 	db_rec_t *row;
 
 	row = db_first(res);
-	while (row) {
+	while(row) {
 		int flags = 0;
 		int type = 0;
 		str value = STR_NULL;
 		avp_value_t val;
 		avp_name_t name;
-		
+
 		get_str_val(row->fld[0], name.s);
 		get_int_val(row->fld[1], type);
 		get_str_val(row->fld[2], value);
 		get_int_val(row->fld[3], flags);
 
-		if (flags & SRDB_LOAD_SER) {
-			if (type == AVP_VAL_STR) val.s = value;
-			else str2int(&value, (unsigned int *)&val.n); /* FIXME */
+		if(flags & SRDB_LOAD_SER) {
+			if(type == AVP_VAL_STR)
+				val.s = value;
+			else
+				str2int(&value, (unsigned int *)&val.n); /* FIXME */
 
 			flags |= flag;
 
@@ -319,7 +360,7 @@ static int read_avps(db_res_t *res, avp_flags_t flag) /* id must not be NULL */
 static inline int remove_all_avps(registered_table_t *t, str *id)
 {
 	set_str_val(t->remove->match[0], *id);
-	if (db_exec(NULL, t->remove) < 0) {
+	if(db_exec(NULL, t->remove) < 0) {
 		ERR("can't remove attrs\n");
 		return -1;
 	}
@@ -328,24 +369,24 @@ static inline int remove_all_avps(registered_table_t *t, str *id)
 
 /* ----- interface functions ----- */
 
-int load_extra_attrs(struct sip_msg* msg, char* _table, char* _id)
+int load_extra_attrs(struct sip_msg *msg, char *_table, char *_id)
 {
 	registered_table_t *t;
 	db_res_t *res = NULL;
 	str id;
 
 	t = (registered_table_t *)_table;
-	if ((!t) || (get_str_fparam(&id, msg, (fparam_t*)_id) < 0)) {
+	if((!t) || (get_str_fparam(&id, msg, (fparam_t *)_id) < 0)) {
 		ERR("invalid parameter value\n");
 		return -1;
 	}
-	
+
 	set_str_val(t->query->match[0], id);
-	if (db_exec(&res, t->query) < 0) {
+	if(db_exec(&res, t->query) < 0) {
 		ERR("DB query failed\n");
 		return -1;
 	}
-	if (res) {
+	if(res) {
 		read_avps(res, t->flag);
 		db_res_free(res);
 	}
@@ -353,14 +394,14 @@ int load_extra_attrs(struct sip_msg* msg, char* _table, char* _id)
 	return 1;
 }
 
-int remove_extra_attrs(struct sip_msg* msg, char *_table, char* _id)
+int remove_extra_attrs(struct sip_msg *msg, char *_table, char *_id)
 {
 	str id;
 	registered_table_t *t;
 
 	t = (registered_table_t *)_table;
 
-	if ((!t) || (get_str_fparam(&id, msg, (fparam_t*)_id) < 0)) {
+	if((!t) || (get_str_fparam(&id, msg, (fparam_t *)_id) < 0)) {
 		ERR("invalid parameter value\n");
 		return -1;
 	}
@@ -369,23 +410,19 @@ int remove_extra_attrs(struct sip_msg* msg, char *_table, char* _id)
 	return 1;
 }
 
-int save_extra_attrs(struct sip_msg* msg, char* _table, char *_id)
+int save_extra_attrs(struct sip_msg *msg, char *_table, char *_id)
 {
 	str id;
 	int i;
 	struct usr_avp *avp;
-	static unsigned short lists[] = {
-		AVP_CLASS_USER | AVP_TRACK_FROM, 
-		AVP_CLASS_USER | AVP_TRACK_TO, 
-		AVP_CLASS_URI | AVP_TRACK_FROM, 
-		AVP_CLASS_URI | AVP_TRACK_TO, 
-		0
-	};
+	static unsigned short lists[] = {AVP_CLASS_USER | AVP_TRACK_FROM,
+			AVP_CLASS_USER | AVP_TRACK_TO, AVP_CLASS_URI | AVP_TRACK_FROM,
+			AVP_CLASS_URI | AVP_TRACK_TO, 0};
 	registered_table_t *t;
 
 	t = (registered_table_t *)_table;
 
-	if ((!t) || (get_str_fparam(&id, msg, (fparam_t*)_id) < 0)) {
+	if((!t) || (get_str_fparam(&id, msg, (fparam_t *)_id) < 0)) {
 		ERR("invalid parameter value\n");
 		return -1;
 	}
@@ -393,49 +430,54 @@ int save_extra_attrs(struct sip_msg* msg, char* _table, char *_id)
 	/* delete all attrs under given id */
 	remove_all_avps(t, &id);
 
-	/* save all attrs flagged with flag under id */	
-	for (i = 0; lists[i]; i++) {
-		for (avp = get_avp_list(lists[i]); avp; avp = avp->next) {
-			if ((avp->flags & t->flag) != 0) save_avp(t, avp, &id);
+	/* save all attrs flagged with flag under id */
+	for(i = 0; lists[i]; i++) {
+		for(avp = get_avp_list(lists[i]); avp; avp = avp->next) {
+			if((avp->flags & t->flag) != 0)
+				save_avp(t, avp, &id);
 		}
 	}
 	return 1;
 }
 
-int extra_attrs_fixup(void** param, int param_no)
+int extra_attrs_fixup(void **param, int param_no)
 {
 	registered_table_t *t;
 
-	switch (param_no) {
+	switch(param_no) {
 		case 1: /* try to find registered table, error if not found */
-				t = find_registered_table(*param);
-				if (!t) {
-					ERR("can't find attribute group with id: %s\n", (char*)*param);
-					return -1;
-				}
-				*param = (void*)t;
-				break;
-		case 2: return fixup_var_str_2(param, param_no);
+			t = find_registered_table(*param);
+			if(!t) {
+				ERR("can't find attribute group with id: %s\n", (char *)*param);
+				return -1;
+			}
+			*param = (void *)t;
+			break;
+		case 2:
+			return fixup_var_str_2(param, param_no);
 	}
 	return 0;
 }
 
-/******* locking *******/ 
+/******* locking *******/
 
-#define LOCK_CNT	32
+#define LOCK_CNT 32
 
 gen_lock_t *locks = NULL; /* set of mutexes allocated in shared memory */
-int lock_counters[LOCK_CNT]; /* set of counters (each proces has its own counters) */
+int lock_counters
+		[LOCK_CNT]; /* set of counters (each proces has its own counters) */
 
-static int avpdb_post_script_cb(struct sip_msg *msg, unsigned int flags, void *param) {
+static int avpdb_post_script_cb(
+		struct sip_msg *msg, unsigned int flags, void *param)
+{
 	int i;
 
-	for (i=0; i<LOCK_CNT; i++) {
-		if (lock_counters[i] > 0) {
-			if (auto_unlock) {
+	for(i = 0; i < LOCK_CNT; i++) {
+		if(lock_counters[i] > 0) {
+			if(auto_unlock) {
 				DEBUG("post script auto unlock extra attrs <%d>\n", i);
 				lock_release(&locks[i]);
-				lock_counters[i]=0;
+				lock_counters[i] = 0;
 			} else {
 				BUG("script writer didn't unlock extra attrs !!!\n");
 				return 1;
@@ -451,7 +493,8 @@ int init_extra_avp_locks()
 	registered_table_t *t = tables;
 
 	if(register_script_cb(avpdb_post_script_cb,
-			REQUEST_CB | ONREPLY_CB | POST_SCRIPT_CB, 0)<0) {
+			   REQUEST_CB | ONREPLY_CB | POST_SCRIPT_CB, 0)
+			< 0) {
 		LM_ERR("failed to register script callbacks\n");
 		return -1;
 	}
@@ -460,18 +503,19 @@ int init_extra_avp_locks()
 	memset(lock_counters, 0, sizeof(lock_counters));
 
 	locks = shm_malloc(sizeof(gen_lock_t) * LOCK_CNT);
-	if (!locks) {
+	if(!locks) {
 		SHM_MEM_ERROR;
 		return -1;
 	}
-	for (i = 0; i < LOCK_CNT; i++) {
+	for(i = 0; i < LOCK_CNT; i++) {
 		lock_init(&locks[i]);
 	}
 
 	/* initializes mutexes for extra AVPs */
 	i = 0;
-	while (t) {
-		t->group_mutex_idx = get_hash1_raw(t->table_name, strlen(t->table_name)) % LOCK_CNT;
+	while(t) {
+		t->group_mutex_idx =
+				get_hash1_raw(t->table_name, strlen(t->table_name)) % LOCK_CNT;
 		t = t->next;
 	}
 
@@ -484,14 +528,14 @@ static inline int find_mutex(registered_table_t *t, str *id)
 	return ((t->group_mutex_idx + get_hash1_raw(id->s, id->len)) % LOCK_CNT);
 }
 
-int lock_extra_attrs(struct sip_msg* msg, char *_table, char* _id)
+int lock_extra_attrs(struct sip_msg *msg, char *_table, char *_id)
 {
 	str id;
 	registered_table_t *t;
 	int mutex_idx;
 
 	t = (registered_table_t *)_table;
-	if ((!t) || (get_str_fparam(&id, msg, (fparam_t*)_id) < 0)) {
+	if((!t) || (get_str_fparam(&id, msg, (fparam_t *)_id) < 0)) {
 		ERR("invalid parameter value\n");
 		return -1;
 	}
@@ -499,11 +543,10 @@ int lock_extra_attrs(struct sip_msg* msg, char *_table, char* _id)
 	/* find right mutex according to id/table */
 	mutex_idx = find_mutex(t, &id);
 
-	if (lock_counters[mutex_idx] > 0) {
+	if(lock_counters[mutex_idx] > 0) {
 		/* mutex is already locked by this process */
 		lock_counters[mutex_idx]++;
-	}
-	else {
+	} else {
 		/* the mutex was not locked => lock it and set counter */
 		lock_get(&locks[mutex_idx]);
 		lock_counters[mutex_idx] = 1;
@@ -512,14 +555,14 @@ int lock_extra_attrs(struct sip_msg* msg, char *_table, char* _id)
 	return 1;
 }
 
-int unlock_extra_attrs(struct sip_msg* msg, char *_table, char* _id)
+int unlock_extra_attrs(struct sip_msg *msg, char *_table, char *_id)
 {
 	str id;
 	registered_table_t *t;
 	int mutex_idx;
 
 	t = (registered_table_t *)_table;
-	if ((!t) || (get_str_fparam(&id, msg, (fparam_t*)_id) < 0)) {
+	if((!t) || (get_str_fparam(&id, msg, (fparam_t *)_id) < 0)) {
 		ERR("invalid parameter value\n");
 		return -1;
 	}
@@ -527,19 +570,17 @@ int unlock_extra_attrs(struct sip_msg* msg, char *_table, char* _id)
 	/* find right mutex according to id/table */
 	mutex_idx = find_mutex(t, &id);
 
-	if (lock_counters[mutex_idx] > 1) {
+	if(lock_counters[mutex_idx] > 1) {
 		/* mutex is locked more times by this process */
 		lock_counters[mutex_idx]--;
-	}
-	else if (lock_counters[mutex_idx] == 1) {
+	} else if(lock_counters[mutex_idx] == 1) {
 		/* the mutex is locked once => unlock it and reset counter */
 		lock_release(&locks[mutex_idx]);
 		lock_counters[mutex_idx] = 0;
-	}
-	else {
-		BUG("trying to unlock without lock group=\"%s\" id=\"%.*s\"\n", t->id, id.len, id.s);
+	} else {
+		BUG("trying to unlock without lock group=\"%s\" id=\"%.*s\"\n", t->id,
+				id.len, id.s);
 	}
 
 	return 1;
 }
-

+ 7 - 7
src/modules/uid_avp_db/extra_attrs.h

@@ -6,18 +6,18 @@
 #include "../../lib/srdb2/db.h"
 #include "../../core/sr_module.h"
 
-int declare_attr_group(modparam_t type, char* param);
+int declare_attr_group(modparam_t type, char *param);
 
 int init_extra_avp_queries(db_ctx_t *ctx);
 int init_extra_avp_locks();
 
-int load_extra_attrs(struct sip_msg* msg, char *_table, char* _id);
-int save_extra_attrs(struct sip_msg* msg, char* _table, char *_id);
-int remove_extra_attrs(struct sip_msg* msg, char *_table, char* _id);
+int load_extra_attrs(struct sip_msg *msg, char *_table, char *_id);
+int save_extra_attrs(struct sip_msg *msg, char *_table, char *_id);
+int remove_extra_attrs(struct sip_msg *msg, char *_table, char *_id);
 
-int extra_attrs_fixup(void** param, int param_no);
+int extra_attrs_fixup(void **param, int param_no);
 
-int lock_extra_attrs(struct sip_msg* msg, char *_table, char* _id);
-int unlock_extra_attrs(struct sip_msg* msg, char *_table, char* _id);
+int lock_extra_attrs(struct sip_msg *msg, char *_table, char *_id);
+int unlock_extra_attrs(struct sip_msg *msg, char *_table, char *_id);
 
 #endif

+ 152 - 147
src/modules/uid_avp_db/uid_avp_db.c

@@ -43,27 +43,27 @@
 
 MODULE_VERSION
 
-static char* db_url           = DEFAULT_RODB_URL;    /* Database URL */
-static char* user_attrs_table = "uid_user_attrs";
-static char* uri_attrs_table  = "uid_uri_attrs";
-static char* uid_column       = "uid";
-static char* username_column  = "username";
-static char* did_column       = "did";
-static char* name_column      = "name";
-static char* type_column      = "type";
-static char* val_column       = "value";
-static char* flags_column     = "flags";
-static char* scheme_column    = "scheme";
-int   auto_unlock      = 0;
-
-db_ctx_t* ctx = 0;
+static char *db_url = DEFAULT_RODB_URL; /* Database URL */
+static char *user_attrs_table = "uid_user_attrs";
+static char *uri_attrs_table = "uid_uri_attrs";
+static char *uid_column = "uid";
+static char *username_column = "username";
+static char *did_column = "did";
+static char *name_column = "name";
+static char *type_column = "type";
+static char *val_column = "value";
+static char *flags_column = "flags";
+static char *scheme_column = "scheme";
+int auto_unlock = 0;
+
+db_ctx_t *ctx = 0;
 db_cmd_t *load_user_attrs_cmd = NULL;
 db_cmd_t *load_uri_attrs_cmd = NULL;
 
 static int mod_init(void);
 static int child_init(int);
-static int load_attrs(struct sip_msg* msg, char* s1, char* s2);
-static int attrs_fixup(void** param, int param_no);
+static int load_attrs(struct sip_msg *msg, char *s1, char *s2);
+static int attrs_fixup(void **param, int param_no);
 
 
 static domain_get_did_t dm_get_did = NULL;
@@ -71,60 +71,55 @@ static domain_get_did_t dm_get_did = NULL;
 /*
  * Exported functions
  */
-static cmd_export_t cmds[] = {
-	{"load_attrs", load_attrs, 2, attrs_fixup, 0,
-		REQUEST_ROUTE | FAILURE_ROUTE},
-	/* functions for loading/storing flagged attributes into DB */
-	{"load_extra_attrs", load_extra_attrs, 2, extra_attrs_fixup, 0,
-		REQUEST_ROUTE | ONREPLY_ROUTE | FAILURE_ROUTE},
-	{"save_extra_attrs", save_extra_attrs, 2, extra_attrs_fixup, 0,
-		REQUEST_ROUTE | ONREPLY_ROUTE | FAILURE_ROUTE},
-	{"remove_extra_attrs", remove_extra_attrs, 2, extra_attrs_fixup, 0,
-		REQUEST_ROUTE | ONREPLY_ROUTE | FAILURE_ROUTE},
-
-	/* locking attrs - needed for proper work! */
-	{"lock_extra_attrs", lock_extra_attrs, 2, extra_attrs_fixup, 0,
-		REQUEST_ROUTE | ONREPLY_ROUTE | FAILURE_ROUTE},
-	{"unlock_extra_attrs", unlock_extra_attrs, 2, extra_attrs_fixup, 0,
-		REQUEST_ROUTE | ONREPLY_ROUTE | FAILURE_ROUTE},
-
-	{0, 0, 0, 0, 0, 0}
-};
+static cmd_export_t cmds[] = {{"load_attrs", load_attrs, 2, attrs_fixup, 0,
+									  REQUEST_ROUTE | FAILURE_ROUTE},
+		/* functions for loading/storing flagged attributes into DB */
+		{"load_extra_attrs", load_extra_attrs, 2, extra_attrs_fixup, 0,
+				REQUEST_ROUTE | ONREPLY_ROUTE | FAILURE_ROUTE},
+		{"save_extra_attrs", save_extra_attrs, 2, extra_attrs_fixup, 0,
+				REQUEST_ROUTE | ONREPLY_ROUTE | FAILURE_ROUTE},
+		{"remove_extra_attrs", remove_extra_attrs, 2, extra_attrs_fixup, 0,
+				REQUEST_ROUTE | ONREPLY_ROUTE | FAILURE_ROUTE},
+
+		/* locking attrs - needed for proper work! */
+		{"lock_extra_attrs", lock_extra_attrs, 2, extra_attrs_fixup, 0,
+				REQUEST_ROUTE | ONREPLY_ROUTE | FAILURE_ROUTE},
+		{"unlock_extra_attrs", unlock_extra_attrs, 2, extra_attrs_fixup, 0,
+				REQUEST_ROUTE | ONREPLY_ROUTE | FAILURE_ROUTE},
+
+		{0, 0, 0, 0, 0, 0}};
 
 
 /*
  * Exported parameters
  */
-static param_export_t params[] = {
-    {"db_url",           PARAM_STRING, &db_url          },
-    {"user_attrs_table", PARAM_STRING, &user_attrs_table},
-    {"uri_attrs_table",  PARAM_STRING, &uri_attrs_table },
-    {"uid_column",       PARAM_STRING, &uid_column      },
-    {"username_column",  PARAM_STRING, &username_column },
-    {"did_column",       PARAM_STRING, &did_column      },
-    {"name_column",      PARAM_STRING, &name_column     },
-    {"type_column",      PARAM_STRING, &type_column     },
-    {"value_column",     PARAM_STRING, &val_column      },
-    {"flags_column",     PARAM_STRING, &flags_column    },
-    {"scheme_column",    PARAM_STRING, &scheme_column   },
-
-    {"attr_group", PARAM_STR | PARAM_USE_FUNC, (void*)declare_attr_group },
-    {"auto_unlock_extra_attrs", PARAM_INT, &auto_unlock },
-    {0, 0, 0}
-};
+static param_export_t params[] = {{"db_url", PARAM_STRING, &db_url},
+		{"user_attrs_table", PARAM_STRING, &user_attrs_table},
+		{"uri_attrs_table", PARAM_STRING, &uri_attrs_table},
+		{"uid_column", PARAM_STRING, &uid_column},
+		{"username_column", PARAM_STRING, &username_column},
+		{"did_column", PARAM_STRING, &did_column},
+		{"name_column", PARAM_STRING, &name_column},
+		{"type_column", PARAM_STRING, &type_column},
+		{"value_column", PARAM_STRING, &val_column},
+		{"flags_column", PARAM_STRING, &flags_column},
+		{"scheme_column", PARAM_STRING, &scheme_column},
+
+		{"attr_group", PARAM_STR | PARAM_USE_FUNC, (void *)declare_attr_group},
+		{"auto_unlock_extra_attrs", PARAM_INT, &auto_unlock}, {0, 0, 0}};
 
 
 struct module_exports exports = {
-    "uid_avp_db",/* module name */
-    DEFAULT_DLFLAGS, /* dlopen flags */
-    cmds,        /* exported commands */
-    params,      /* exported parameters */
-    0,           /* exported RPC methods */
-    0,           /* exported pseudo-variables */
-    0,           /* response function*/
-    mod_init,    /* module init function */
-    child_init,  /* per-child init function */
-    0            /* destroy function */
+		"uid_avp_db",	 /* module name */
+		DEFAULT_DLFLAGS, /* dlopen flags */
+		cmds,			 /* exported commands */
+		params,			 /* exported parameters */
+		0,				 /* exported RPC methods */
+		0,				 /* exported pseudo-variables */
+		0,				 /* response function*/
+		mod_init,		 /* module init function */
+		child_init,		 /* per-child init function */
+		0				 /* destroy function */
 };
 
 
@@ -136,79 +131,85 @@ static int mod_init(void)
 
 static int child_init(int rank)
 {
-	db_fld_t res_cols[] = {
-		{.name = name_column, .type = DB_STR},
-		{.name = type_column, .type = DB_INT},
-		{.name = val_column, .type = DB_STR},
-		{.name = flags_column, .type = DB_BITMAP},
-		{.name = NULL}
-	};
+	db_fld_t res_cols[] = {{.name = name_column, .type = DB_STR},
+			{.name = type_column, .type = DB_INT},
+			{.name = val_column, .type = DB_STR},
+			{.name = flags_column, .type = DB_BITMAP}, {.name = NULL}};
 	db_fld_t match_uri[] = {
-		{.name = username_column, .type = DB_STR, .op = DB_EQ},
-		{.name = did_column, .type = DB_STR, .op = DB_EQ},
-		{.name = scheme_column, .type = DB_STR, .op = DB_EQ},
-		{.name = NULL}
-	};
+			{.name = username_column, .type = DB_STR, .op = DB_EQ},
+			{.name = did_column, .type = DB_STR, .op = DB_EQ},
+			{.name = scheme_column, .type = DB_STR, .op = DB_EQ},
+			{.name = NULL}};
 	db_fld_t match_user[] = {
-		{.name = uid_column, .type = DB_STR, .op = DB_EQ},
-		{.name = NULL}
-	};
+			{.name = uid_column, .type = DB_STR, .op = DB_EQ}, {.name = NULL}};
 
-	if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN)
+	if(rank == PROC_INIT || rank == PROC_MAIN || rank == PROC_TCP_MAIN)
 		return 0; /* do nothing for the main process */
-	
+
 	ctx = db_ctx("avp_db");
-	if (!ctx) goto err;
-	if (db_add_db(ctx, db_url) < 0) goto err;
-	if (db_connect(ctx) < 0) goto err;
-	
-	load_uri_attrs_cmd = db_cmd(DB_GET, ctx, uri_attrs_table, res_cols, match_uri, NULL);
-	if (!load_uri_attrs_cmd) goto err;
+	if(!ctx)
+		goto err;
+	if(db_add_db(ctx, db_url) < 0)
+		goto err;
+	if(db_connect(ctx) < 0)
+		goto err;
+
+	load_uri_attrs_cmd =
+			db_cmd(DB_GET, ctx, uri_attrs_table, res_cols, match_uri, NULL);
+	if(!load_uri_attrs_cmd)
+		goto err;
 
-	load_user_attrs_cmd = db_cmd(DB_GET, ctx, user_attrs_table, res_cols, match_user, NULL);
-	if (!load_user_attrs_cmd) goto err;
+	load_user_attrs_cmd =
+			db_cmd(DB_GET, ctx, user_attrs_table, res_cols, match_user, NULL);
+	if(!load_user_attrs_cmd)
+		goto err;
 
-	if (init_extra_avp_queries(ctx) < 0) goto err;
+	if(init_extra_avp_queries(ctx) < 0)
+		goto err;
 
-    return 0;
+	return 0;
 
 err:
-	if (load_uri_attrs_cmd) db_cmd_free(load_uri_attrs_cmd);
-	if (load_user_attrs_cmd) db_cmd_free(load_user_attrs_cmd);
+	if(load_uri_attrs_cmd)
+		db_cmd_free(load_uri_attrs_cmd);
+	if(load_user_attrs_cmd)
+		db_cmd_free(load_user_attrs_cmd);
 
-	if (ctx) db_ctx_free(ctx);
+	if(ctx)
+		db_ctx_free(ctx);
 
 	ERR("Error while initializing database layer\n");
 	return -1;
 }
 
-#define IS_DB_NULL(f)	(f.flags & DB_NULL)
+#define IS_DB_NULL(f) (f.flags & DB_NULL)
 
 static void read_attrs(db_res_t *res, unsigned long flags)
 {
-    int_str name, v;
-    str avp_val;
-    int type, n, found;
-	db_rec_t* row;
+	int_str name, v;
+	str avp_val;
+	int type, n, found;
+	db_rec_t *row;
 
 	n = 0;
 	found = 0;
 	/* AVP names from DB are always strings */
 	flags |= AVP_NAME_STR;
-	if (res) row = db_first(res);
-	else row = NULL;
-	while (row) {
+	if(res)
+		row = db_first(res);
+	else
+		row = NULL;
+	while(row) {
 		found++;
 
-		if (IS_DB_NULL(row->fld[0]) || 
-				IS_DB_NULL(row->fld[1]) ||
-				IS_DB_NULL(row->fld[3])) {
+		if(IS_DB_NULL(row->fld[0]) || IS_DB_NULL(row->fld[1])
+				|| IS_DB_NULL(row->fld[3])) {
 			ERR("Skipping row containing NULL entries\n");
 			row = db_next(res);
 			continue;
 		}
 
-		if ((row->fld[3].v.int4 & SRDB_LOAD_SER) == 0) {
+		if((row->fld[3].v.int4 & SRDB_LOAD_SER) == 0) {
 			row = db_next(res);
 			continue;
 		}
@@ -221,57 +222,58 @@ static void read_attrs(db_res_t *res, unsigned long flags)
 		type = row->fld[1].v.int4;
 
 		/* Test for NULL value */
-		if (IS_DB_NULL(row->fld[2])) {
+		if(IS_DB_NULL(row->fld[2])) {
 			avp_val.s = 0;
 			avp_val.len = 0;
 		} else {
 			avp_val = row->fld[2].v.lstr;
 		}
 
-		if (type == AVP_VAL_STR) {
+		if(type == AVP_VAL_STR) {
 			/* String AVP */
 			v.s = avp_val;
 			flags |= AVP_VAL_STR;
 		} else {
 			/* Integer AVP */
-			str2int(&avp_val, (unsigned*)&v.n);
+			str2int(&avp_val, (unsigned *)&v.n);
 			/* reset val_str as the value could be an integer */
 			flags &= ~AVP_VAL_STR;
 		}
 
-		if (add_avp(flags, name, v) < 0) {
+		if(add_avp(flags, name, v) < 0) {
 			ERR("Error while adding user attribute %.*s, skipping\n",
 					name.s.len, ZSW(name.s.s));
 		}
 		row = db_next(res);
-
 	}
 	DBG("avp_db:load_attrs: %d attributes found, %d loaded\n", found, n);
 }
 
-static int load_uri_attrs(struct sip_msg* msg, unsigned long flags, fparam_t* fp)
+static int load_uri_attrs(
+		struct sip_msg *msg, unsigned long flags, fparam_t *fp)
 {
-    db_res_t* res;
-    str uri;
-    struct sip_uri puri;
+	db_res_t *res;
+	str uri;
+	struct sip_uri puri;
 	static str default_did = STR_STATIC_INIT(DEFAULT_DID);
 
-	if (get_str_fparam(&uri, msg, (fparam_t*)fp) != 0) {
+	if(get_str_fparam(&uri, msg, (fparam_t *)fp) != 0) {
 		DBG("Unable to get URI from load_uri_attrs parameters\n");
 		return -1;
 	}
 
-	if (parse_uri(uri.s, uri.len, &puri) < 0) {
+	if(parse_uri(uri.s, uri.len, &puri) < 0) {
 		ERR("Error while parsing URI '%.*s'\n", uri.len, uri.s);
 		return -1;
 	}
 
-    load_uri_attrs_cmd->match[0].v.lstr = puri.user;
+	load_uri_attrs_cmd->match[0].v.lstr = puri.user;
 
-	if (puri.host.len) {
+	if(puri.host.len) {
 		/* domain name is present */
-		if (dm_get_did(&load_uri_attrs_cmd->match[1].v.lstr, &puri.host) < 0) {
-			DBG("Cannot lookup DID for domain %.*s, using default value\n", puri.host.len, ZSW(puri.host.s));
+		if(dm_get_did(&load_uri_attrs_cmd->match[1].v.lstr, &puri.host) < 0) {
+			DBG("Cannot lookup DID for domain %.*s, using default value\n",
+					puri.host.len, ZSW(puri.host.s));
 			load_uri_attrs_cmd->match[1].v.lstr = default_did;
 		}
 	} else {
@@ -280,14 +282,14 @@ static int load_uri_attrs(struct sip_msg* msg, unsigned long flags, fparam_t* fp
 		load_uri_attrs_cmd->match[1].v.lstr = default_did;
 	}
 
-    uri_type_to_str(puri.type, &(load_uri_attrs_cmd->match[2].v.lstr));
+	uri_type_to_str(puri.type, &(load_uri_attrs_cmd->match[2].v.lstr));
 
-	if (db_exec(&res, load_uri_attrs_cmd) < 0) {
+	if(db_exec(&res, load_uri_attrs_cmd) < 0) {
 		ERR("Error while querying database\n");
 		return -1;
-    }
-    
-	if (res) {
+	}
+
+	if(res) {
 		read_attrs(res, flags);
 		db_res_free(res);
 	}
@@ -295,21 +297,24 @@ static int load_uri_attrs(struct sip_msg* msg, unsigned long flags, fparam_t* fp
 }
 
 
-static int load_user_attrs(struct sip_msg* msg, unsigned long flags, fparam_t* fp)
+static int load_user_attrs(
+		struct sip_msg *msg, unsigned long flags, fparam_t *fp)
 {
-    db_res_t* res;
+	db_res_t *res;
 
-	if (get_str_fparam(&load_user_attrs_cmd->match[0].v.lstr, msg, (fparam_t*)fp) < 0) {
+	if(get_str_fparam(
+			   &load_user_attrs_cmd->match[0].v.lstr, msg, (fparam_t *)fp)
+			< 0) {
 		DBG("Unable to get UID from load_user_attrs parameter\n");
 		return -1;
 	}
 
-	if (db_exec(&res, load_user_attrs_cmd) < 0) {
+	if(db_exec(&res, load_user_attrs_cmd) < 0) {
 		ERR("Error while querying database\n");
 		return -1;
-    }
-    
-	if (res) {
+	}
+
+	if(res) {
 		read_attrs(res, flags);
 		db_res_free(res);
 	}
@@ -320,30 +325,30 @@ static int load_user_attrs(struct sip_msg* msg, unsigned long flags, fparam_t* f
 /*
  * Load user attributes
  */
-static int load_attrs(struct sip_msg* msg, char* fl, char* fp)
+static int load_attrs(struct sip_msg *msg, char *fl, char *fp)
 {
 	unsigned long flags;
 
 	flags = (unsigned long)fl;
 
-	if (flags & AVP_CLASS_URI) {
-		return load_uri_attrs(msg, flags, (fparam_t*)fp);
+	if(flags & AVP_CLASS_URI) {
+		return load_uri_attrs(msg, flags, (fparam_t *)fp);
 	} else {
-		return load_user_attrs(msg, flags, (fparam_t*)fp);
+		return load_user_attrs(msg, flags, (fparam_t *)fp);
 	}
 }
 
 
-static int attrs_fixup(void** param, int param_no)
+static int attrs_fixup(void **param, int param_no)
 {
-    unsigned long flags;
-    char* s;
-    
-	if (param_no == 1) {
+	unsigned long flags;
+	char *s;
+
+	if(param_no == 1) {
 		/* Determine the track and class of attributes to be loaded */
-		s = (char*)*param;
+		s = (char *)*param;
 		flags = 0;
-		if (*s != '$' || (strlen(s) != 3)) {
+		if(*s != '$' || (strlen(s) != 3)) {
 			ERR("Invalid parameter value, $xy expected\n");
 			return -1;
 		}
@@ -381,18 +386,18 @@ static int attrs_fixup(void** param, int param_no)
 				return -1;
 		}
 
-		if ((flags & AVP_CLASS_URI) && !dm_get_did) {
+		if((flags & AVP_CLASS_URI) && !dm_get_did) {
 			dm_get_did = (domain_get_did_t)find_export("get_did", 0, 0);
-			if (!dm_get_did) {
+			if(!dm_get_did) {
 				ERR("Domain module required but not found\n");
 				return -1;
 			}
 		}
 
 		pkg_free(*param);
-		*param = (void*)flags;
-	} else if (param_no == 2) {
+		*param = (void *)flags;
+	} else if(param_no == 2) {
 		return fixup_var_str_12(param, 2);
 	}
-    return 0;
+	return 0;
 }

+ 1 - 1
src/modules/uid_avp_db/uid_avp_db.h

@@ -29,6 +29,6 @@
 #define UID_AVP_DB_H
 #include "../../lib/srdb2/db.h"
 
-extern db_ctx_t* ctx;
+extern db_ctx_t *ctx;
 extern int auto_unlock;
 #endif