static int mod_init(void) { LM_DBG("initializing\n"); if (register_mi_mod(exports.name, mi_cmds) != 0) { LM_ERR("failed to register MI commands\n"); return -1; } if (domain_init_rpc() != 0) { LM_ERR("failed to register RPC commands\n"); return -1; } if (domain_reg_myself !=0 ) { if (register_check_self_func(domain_check_self) <0 ) { LM_ERR("failed to register check self function\n"); return -1; } } /* Bind database */ if (domain_db_bind(&db_url)) { LM_DBG("Usign db_url [%.*s]\n", db_url.len, db_url.s); LM_ERR("no database module found. Have you configure thed \"db_url\" modparam properly?\n"); return -1; } /* Check table versions */ if (domain_db_init(&db_url) < 0) { LM_ERR("unable to open database connection\n"); return -1; } if (domain_db_ver(&domain_table, DOMAIN_TABLE_VERSION) < 0) { LM_ERR("error during check of domain table version\n"); domain_db_close(); goto error; } if (domain_db_ver(&domain_attrs_table, DOMAIN_ATTRS_TABLE_VERSION) < 0) { LM_ERR("error during check of domain_attrs table version\n"); domain_db_close(); goto error; } domain_db_close(); /* Initializing hash tables and hash table variable */ hash_table = (struct domain_list ***)shm_malloc (sizeof(struct domain_list *)); hash_table_1 = (struct domain_list **)shm_malloc (sizeof(struct domain_list *) * (DOM_HASH_SIZE + 1)); hash_table_2 = (struct domain_list **)shm_malloc (sizeof(struct domain_list *) * (DOM_HASH_SIZE + 1)); if ((hash_table == 0) || (hash_table_1 == 0) || (hash_table_2 == 0)) { LM_ERR("no memory for hash table\n"); goto error; } memset(hash_table_1, 0, sizeof(struct domain_list *) * (DOM_HASH_SIZE + 1)); memset(hash_table_2, 0, sizeof(struct domain_list *) * (DOM_HASH_SIZE + 1)); *hash_table = hash_table_1; /* Allocate and initialize locks */ reload_lock = lock_alloc(); if (reload_lock == NULL) { LM_ERR("cannot allocate reload_lock\n"); goto error; } if (lock_init(reload_lock) == NULL) { LM_ERR("cannot init reload_lock\n"); goto error; } /* First reload */ lock_get(reload_lock); if (reload_tables() == -1) { lock_release(reload_lock); LM_CRIT("domain reload failed\n"); goto error; } lock_release(reload_lock); return 0; error: destroy(); return -1; }
static int mod_init(void) { int i; LM_DBG("Initializing\n"); init_db_url( db_url , 0 /*cannot be null*/); domain_table.len = strlen(domain_table.s); domain_col.len = strlen(domain_col.s); domain_attrs_col.len = strlen(domain_attrs_col.s); /* Check if database module has been loaded */ if (domain_db_bind(&db_url) < 0) return -1; /* Check if cache needs to be loaded from domain table */ if (db_mode != 0) { if (domain_db_init(&db_url)<0) return -1; /* Check table version */ if (domain_db_ver(&domain_table, TABLE_VERSION) < 0) { LM_ERR("error during check of domain table version\n"); goto error; } /* Initializing hash tables and hash table variable */ hash_table_1 = (struct domain_list **)shm_malloc (sizeof(struct domain_list *) * DOM_HASH_SIZE); if (hash_table_1 == 0) { LM_ERR("No memory for hash table\n"); goto error; } hash_table_2 = (struct domain_list **)shm_malloc (sizeof(struct domain_list *) * DOM_HASH_SIZE); if (hash_table_2 == 0) { LM_ERR("No memory for hash table\n"); goto error; } for (i = 0; i < DOM_HASH_SIZE; i++) { hash_table_1[i] = hash_table_2[i] = (struct domain_list *)0; } hash_table = (struct domain_list ***)shm_malloc (sizeof(struct domain_list *)); *hash_table = hash_table_1; if (reload_domain_table() == -1) { LM_ERR("Domain table reload failed\n"); goto error; } domain_db_close(); } /* register the alias check function to core */ if (register_alias_fct(is_domain_alias)!=0) { LM_ERR("failed to register the alias check function\n"); goto error; } return 0; error: domain_db_close(); return -1; }
static int mod_init(void) { int i, ver; DBG("domain - initializing\n"); db_url.len = strlen(db_url.s); domain_table.len = strlen(domain_table.s); domain_col.len = strlen(domain_col.s); /* Check if database module has been loaded */ if (domain_db_bind(db_url.s)<0) return -1; /* Check if cache needs to be loaded from domain table */ if (db_mode != 0) { if (domain_db_init(db_url.s)<0) return -1; /* Check table version */ ver = domain_db_ver(&domain_table); if (ver < 0) { LOG(L_ERR, "ERROR: domain:mod_init(): " "error while querying table version\n"); domain_db_close(); return -1; } else if (ver < TABLE_VERSION) { LOG(L_ERR, "ERROR: domain:mod_init(): invalid table" " version (use ser_mysql.sh reinstall)\n"); domain_db_close(); return -1; } /* Initialize fifo interface */ (void)init_domain_fifo(); if (init_domain_unixsock() < 0) { LOG(L_ERR, "ERROR: domain:mod_init(): error while initializing" " unix socket interface\n"); domain_db_close(); return -1; } /* Initializing hash tables and hash table variable */ hash_table_1 = (struct domain_list **)shm_malloc(sizeof(struct domain_list *) * HASH_SIZE); if (hash_table_1 == 0) { LOG(L_ERR, "ERROR: domain: mod_init(): " "No memory for hash table\n"); } hash_table_2 = (struct domain_list **)shm_malloc(sizeof(struct domain_list *) * HASH_SIZE); if (hash_table_2 == 0) { LOG(L_ERR, "ERROR: domain: mod_init():" " No memory for hash table\n"); } for (i = 0; i < HASH_SIZE; i++) { hash_table_1[i] = hash_table_2[i] = (struct domain_list *)0; } hash_table = (struct domain_list ***)shm_malloc(sizeof(struct domain_list *)); *hash_table = hash_table_1; if (reload_domain_table() == -1) { LOG(L_CRIT, "ERROR: domain:mod_init():" " Domain table reload failed\n"); return -1; } domain_db_close(); } return 0; }
/* * Reload domain table to new hash table and when done, make new hash table * current one. */ int reload_tables ( void ) { db_key_t cols[4]; db1_res_t* res = NULL; db_row_t* row; struct domain_list **new_hash_table; int i; short type; str did, domain, name, value; int_str val; /* Choose new hash table and free its old contents */ if (*hash_table == hash_table_1) { hash_table_free(hash_table_2); new_hash_table = hash_table_2; } else { hash_table_free(hash_table_1); new_hash_table = hash_table_1; } cols[0] = &did_col; cols[1] = &name_col; cols[2] = &type_col; cols[3] = &value_col; if (domain_db_init(&d_db_url) < 0) { LM_ERR("unable to open database connection\n"); return -1; } if (domain_dbf.use_table(db_handle, &domain_attrs_table) < 0) { LM_ERR("error while trying to use domain_attrs table\n"); goto err; } if (domain_dbf.query(db_handle, NULL, 0, NULL, cols, 0, 4, 0, &res) < 0) { LM_ERR("error while querying database\n"); goto err; } row = RES_ROWS(res); LM_DBG("number of rows in domain_attrs table: %d\n", RES_ROW_N(res)); for (i = 0; i < RES_ROW_N(res); i++) { row = RES_ROWS(res) + i; if ((VAL_NULL(ROW_VALUES(row)) == 1) || (VAL_TYPE(ROW_VALUES(row)) != DB1_STRING)) { LM_ERR("did at row <%u> is null or not string\n", i); goto err; } did.s = (char *)VAL_STRING(ROW_VALUES(row)); did.len = strlen(did.s); if (did.len == 0) { LM_ERR("did at row <%u> is empty string\n", i); goto err; } if ((VAL_NULL(ROW_VALUES(row) + 1) == 1) || (VAL_TYPE(ROW_VALUES(row) + 1) != DB1_STRING)) { LM_ERR("name at row <%u> is null or not string\n", i); goto err; } name.s = (char *)VAL_STRING(ROW_VALUES(row) + 1); name.len = strlen(name.s); if (name.len == 0) { LM_ERR("name at row <%u> is empty string\n", i); goto err; } if ((VAL_NULL(ROW_VALUES(row) + 2) == 1) || ((VAL_TYPE(ROW_VALUES(row) + 2) != DB1_INT) && (VAL_TYPE(ROW_VALUES(row) + 2) != DB1_BIGINT))) { LM_ERR("type at row <%u> is null or not int\n", i); goto err; } if(VAL_TYPE(ROW_VALUES(row) + 2) == DB1_BIGINT) { type = (int)VAL_BIGINT(ROW_VALUES(row) + 2); } else { type = (int)VAL_INT(ROW_VALUES(row) + 2); } if ((type != 0) && (type != 2)) { LM_ERR("unknown type <%d> at row <%u>\n", type, i); goto err; } if ((VAL_NULL(ROW_VALUES(row) + 3) == 1) || (VAL_TYPE(ROW_VALUES(row) + 3) != DB1_STRING)) { LM_ERR("value at row <%u> is null or not string\n", i); goto err; } value.s = (char *)VAL_STRING(ROW_VALUES(row) + 3); value.len = strlen(value.s); if (type == 0) { if (str2sint(&value, &(val.n)) == -1) { LM_ERR("value at row <%u> is invalid int\n", i); goto err; } } else { val.s = value; } if (type == 0) LM_DBG("inserting <did/name/type/value> = <%s/%s/%d/%d> into attribute list\n", did.s, name.s, type, val.n); else LM_DBG("inserting <did/name/type/value> = <%s/%s/%d/%s> into attribute list\n", did.s, name.s, type, val.s.s); if (hash_table_attr_install(new_hash_table, &did, &name, type, &val) == -1) { LM_ERR("could not install attribute into hash table\n"); goto err; } } domain_dbf.free_result(db_handle, res); res = NULL; cols[0] = &domain_col; cols[1] = &did_col; if (domain_dbf.use_table(db_handle, &domain_table) < 0) { LM_ERR("error while trying to use domain table\n"); goto err; } if (domain_dbf.query(db_handle, NULL, 0, NULL, cols, 0, 2, 0, &res) < 0) { LM_ERR("error while querying database\n"); goto err; } row = RES_ROWS(res); LM_DBG("number of rows in domain table: %d\n", RES_ROW_N(res)); for (i = 0; i < RES_ROW_N(res); i++) { row = RES_ROWS(res) + i; if ((VAL_NULL(ROW_VALUES(row)) == 1) || (VAL_TYPE(ROW_VALUES(row)) != DB1_STRING)) { LM_ERR("domain at row <%u> is null or not string\n", i); goto err; } domain.s = (char *)VAL_STRING(ROW_VALUES(row)); domain.len = strlen(domain.s); if (domain.len == 0) { LM_ERR("domain at row <%u> is empty string\n", i); goto err; } if ((VAL_NULL(ROW_VALUES(row) + 1) != 1) && (VAL_TYPE(ROW_VALUES(row) + 1) != DB1_STRING)) { LM_ERR("did at row <%u> is not null or string\n", i); goto err; } if (VAL_NULL(ROW_VALUES(row) + 1) == 1) { did.s = domain.s; did.len = domain.len; } else { did.s = (char *)VAL_STRING(ROW_VALUES(row) + 1); did.len = strlen(did.s); if (did.len == 0) { LM_ERR("did at row <%u> is empty string\n", i); goto err; } } LM_DBG("inserting <did/domain> = <%s/%s> into hash table\n", did.s, domain.s); if (hash_table_install(new_hash_table, &did, &domain) == -1) { LM_ERR("could not install domain into hash table\n"); domain_dbf.free_result(db_handle, res); goto err; } } domain_dbf.free_result(db_handle, res); res = NULL; *hash_table = new_hash_table; domain_db_close(); return 1; err: domain_dbf.free_result(db_handle, res); res = NULL; domain_db_close(); return -1; }