static void mod_destroy(void) { if (write_mc) db_cmd_free(write_mc); if (write_acc) db_cmd_free(write_acc); if (acc_db) { db_disconnect(acc_db); db_ctx_free(acc_db); } }
void cpl_db_close() { if (delete_user) db_cmd_free(delete_user); delete_user = NULL; if (write_script) db_cmd_free(write_script); write_script = NULL; if (get_script) db_cmd_free(get_script); get_script = NULL; if (ctx) { db_disconnect(ctx); db_ctx_free(ctx); ctx = NULL; } }
static int child_init(int rank) { if (rank==PROC_INIT || rank==PROC_MAIN || rank==PROC_TCP_MAIN) return 0; /* do nothing for the main process */ if (db_url.s) { acc_db = db_ctx("acc_db"); if (acc_db == NULL) { ERR("Error while initializing database layer\n"); return -1; } if (db_add_db(acc_db, db_url.s) < 0) goto error; if (db_connect(acc_db) < 0) goto error; write_acc = db_cmd(DB_PUT, acc_db, acc_table.s, NULL, NULL, fld); if (write_acc == NULL) { ERR("Error while compiling database query\n"); goto error; } write_mc = db_cmd(DB_PUT, acc_db, mc_table.s, NULL, NULL, fld); if (write_mc == NULL) { ERR("Error while compiling database query\n"); goto error; } return 0; } else { LOG(L_CRIT, "BUG:acc:child_init: null db url\n"); return -1; } error: if (write_acc) db_cmd_free(write_acc); write_acc = NULL; if (write_mc) db_cmd_free(write_mc); write_mc = NULL; if (acc_db) db_ctx_free(acc_db); acc_db = NULL; return -1; }
/* callback function called once per cfg_group identified by group_name */ static void on_declare(str *group_name, cfg_def_t *definition) { static db_cmd_t* cmd; db_res_t *res; cfg_def_t *def; int ret; str asterisk_s = STR_STATIC_INIT("*"); DBG(MODULE_NAME": on_declare('%.*s')\n", group_name->len, group_name->s); if (connect_db() < 0) return; for (def=definition; def->name; def++) { /* for each definition lookup config tables */ if (exec_transl(group_name, &cmd, &res) < 0) return; ret = find_cfg_var(group_name, def->name, res); db_res_free(res); db_cmd_free(cmd); if (ret > 0) continue; /* not found then try default '*' translations */ if (exec_transl(&asterisk_s, &cmd, &res) < 0) return; find_cfg_var(group_name, def->name, res); db_res_free(res); db_cmd_free(cmd); } }
static int exec_transl(str *group_name, db_cmd_t **cmd, db_res_t **res) { db_fld_t cols[5]; db_fld_t params[2]; memset(cols, 0, sizeof(cols)); cols[0].name = transl_cfg_table_fld; cols[0].type = DB_CSTR; cols[1].name = transl_cfg_table_group_name_field_fld; cols[1].type = DB_CSTR; cols[2].name = transl_cfg_table_name_field_fld; cols[2].type = DB_CSTR; cols[3].name = transl_cfg_table_value_field_fld; cols[3].type = DB_CSTR; memset(params, 0, sizeof(params)); params[0].name = transl_group_name_fld; params[0].type = DB_STR; params[0].op = DB_EQ; DBG(MODULE_NAME": exec_transl('%.*s', ...)\n", group_name->len, group_name->s); *cmd = db_cmd(DB_GET, db_cntx, transl_tbl, cols, params, NULL); if (!*cmd) { ERR(MODULE_NAME": Error preparing query '%s'\n", transl_tbl); return -1; } (*cmd)->match[0].flags &= ~DB_NULL; (*cmd)->match[0].v.lstr = *group_name; // FIXME: proprietary code! db_setopt(*cmd, "key", "pKey"); db_setopt(*cmd, "key_omit", 1); if (db_exec(res, *cmd) < 0) { ERR(MODULE_NAME": Error executing query '%s'\n", transl_tbl); db_cmd_free(*cmd); return -1; } return 0; }
/** * check if all SER2Jab workers are still alive * - if not, try to launch new ones */ void xjab_check_workers(int mpid) { int i, n, stat; //DBG("XJAB:%d:xjab_check_workers: time=%d\n", mpid, get_ticks()); if(!jwl || jwl->len <= 0) return; for(i=0; i < jwl->len; i++) { if(jwl->workers[i].pid > 0) { stat = 0; n = waitpid(jwl->workers[i].pid, &stat, WNOHANG); if(n == 0 || n!=jwl->workers[i].pid) continue; LOG(L_ERR,"XJAB:xjab_check_workers: worker[%d][pid=%d] has exited" " - status=%d err=%d errno=%d\n", i, jwl->workers[i].pid, stat, n, errno); xj_wlist_clean_jobs(jwl, i, 1); xj_wlist_set_pid(jwl, -1, i); } #ifdef XJ_EXTRA_DEBUG DBG("XJAB:%d:xjab_check_workers: create a new worker[%d]\n", mpid, i); #endif if ( (stat=fork())<0 ) { #ifdef XJ_EXTRA_DEBUG DBG("XJAB:xjab_check_workers: error - cannot launch new" " worker[%d]\n", i); #endif LOG(L_ERR, "XJAB:xjab_check_workers: error - worker[%d] lost" " forever \n", i); return; } if (stat == 0) { if(xj_wlist_set_pid(jwl, getpid(), i) < 0) { LOG(L_ERR, "XJAB:xjab_check_workers: error setting new" " worker's pid - w[%d]\n", i); return; } /* initialize the config framework * The child process was not registered under * the framework during mod_init, therefore the * late version needs to be called. (Miklos) */ if (cfg_late_child_init()) return; ctx = db_ctx("jabber"); if (ctx == NULL) goto dberror; if (db_add_db(ctx, db_url) < 0) goto dberror; if (db_connect(ctx) < 0) goto dberror; cmd = db_cmd(DB_GET, ctx, db_table, db_cols, db_params, NULL); if (!cmd) goto dberror; xj_worker_process(jwl,jaddress,jport,i, cmd); db_cmd_free(cmd); db_ctx_free(ctx); ctx = NULL; /* destroy the local config */ cfg_child_destroy(); exit(0); } } dberror: if (cmd) db_cmd_free(cmd); cmd = NULL; if (ctx) db_ctx_free(ctx); ctx = NULL; }
/* * Initialize children */ static int child_init(int rank) { int i, j, mpid, cpid; DBG("XJAB:child_init: initializing child <%d>\n", rank); /* Rank 0 is main process now - 1 is the first child (janakj) */ if(rank == 1) { #ifdef HAVE_IHTTP /** register iHTTP callbacks -- go forward in any case*/ ihb.reg_f("xjab", "XMPP Gateway", IH_MENU_YES, xjab_mod_info, NULL); ihb.reg_f("xjabc", "XMPP connections", IH_MENU_YES, xjab_connections, NULL); #endif if((mpid=fork())<0 ) { LOG(L_ERR, "XJAB:child_init:error - cannot launch worker's" " manager\n"); return -1; } if(mpid == 0) { /** launching the workers */ for(i=0;i<nrw;i++) { if ( (cpid=fork())<0 ) { LOG(L_ERR,"XJAB:child_init:error - cannot launch worker\n"); return -1; } if (cpid == 0) { for(j=0;j<nrw;j++) if(j!=i) close(pipes[j][0]); close(pipes[i][1]); if(xj_wlist_set_pid(jwl, getpid(), i) < 0) { LOG(L_ERR, "XJAB:child_init:error setting worker's" " pid\n"); return -1; } /* initialize the config framework */ if (cfg_child_init()) return -1; ctx = db_ctx("jabber"); if (ctx == NULL) goto dberror; if (db_add_db(ctx, db_url) < 0) goto dberror; if (db_connect(ctx) < 0) goto dberror; cmd = db_cmd(DB_GET, ctx, db_table, db_cols, db_params, NULL); if (!cmd) goto dberror; xj_worker_process(jwl,jaddress,jport,i, cmd); db_cmd_free(cmd); db_ctx_free(ctx); ctx = NULL; /* destroy the local config */ cfg_child_destroy(); exit(0); } } mpid = getpid(); /* initialize the config framework */ if (cfg_child_init()) return -1; while(1) { sleep(check_time); /* update the local config */ cfg_update(); xjab_check_workers(mpid); } } } //if(pipes) //{ // for(i=0;i<nrw;i++) // close(pipes[i][0]); //} return 0; dberror: if (cmd) db_cmd_free(cmd); cmd = NULL; if (ctx) db_ctx_free(ctx); ctx = NULL; return -1; }
/* module initialization function */ static int mod_init(void) { static str default_s = STR_STATIC_INIT("<default>"); db_cmd_t *cmd; db_res_t *res; db_rec_t *rec; db_fld_t cols[7]; DBG(MODULE_NAME": mod_init: initializing\n"); /* get default values from translation table */ if (connect_db() < 0) return E_CFG; DBG(MODULE_NAME": mod_init: getting default values from translation table\n"); if (exec_transl(&default_s, &cmd, &res) < 0) return E_CFG; rec = db_first(res); if (rec) { CSTRDUP(def_cfg_table, rec->fld[0]); CSTRDUP(def_cfg_table_group_name_field, rec->fld[1]); CSTRDUP(def_cfg_table_name_field, rec->fld[2]); CSTRDUP(def_cfg_table_value_field, rec->fld[3]); } // db_rec_free(rec); // ---> causes next db_cmd is aborted !!! db_res_free(res); db_cmd_free(cmd); DBG(MODULE_NAME": mod_init: default values: table='%s', group_name_field='%s', name_field='%s', value_field='%s'\n", def_cfg_table, def_cfg_table_group_name_field, def_cfg_table_name_field, def_cfg_table_value_field); /* get custom parameters from database */ DBG(MODULE_NAME": mod_init: getting custom parameters from '%s'\n", custom_tbl); memset(cols, 0, sizeof(cols)); cols[0].name = custom_tbl_group_name_fld; cols[0].type = DB_CSTR; cols[1].name = custom_tbl_name_fld; cols[1].type = DB_CSTR; cols[2].name = custom_tbl_value_type_fld; cols[2].type = DB_CSTR; cols[3].name = custom_tbl_min_value_fld; cols[3].type = DB_INT; cols[4].name = custom_tbl_max_value_fld; cols[4].type = DB_INT; cols[5].name = custom_tbl_decription_fld; cols[5].type = DB_CSTR; cmd = db_cmd(DB_GET, db_cntx, custom_tbl, cols, NULL, NULL); if (!cmd) { ERR(MODULE_NAME": Error preparing query '%s'\n", custom_tbl); return E_CFG; } if (db_exec(&res, cmd) < 0) { ERR(MODULE_NAME": Error executing query '%s'\n", custom_tbl); db_cmd_free(cmd); return E_CFG; } rec = db_first(res); while (rec) { DBG(MODULE_NAME": custom parameter '%s.%s' type:%s\n", rec->fld[0].v.cstr, rec->fld[1].v.cstr, rec->fld[2].v.cstr); if (((rec->fld[0].flags & DB_NULL) || strlen(rec->fld[0].v.cstr) == 0) || ((rec->fld[1].flags & DB_NULL) || strlen(rec->fld[1].v.cstr) == 0) || ((rec->fld[2].flags & DB_NULL) || strlen(rec->fld[2].v.cstr) == 0)) { ERR(MODULE_NAME": empty group_name,name or type value in table '%s'\n", custom_tbl); return E_CFG; } switch (rec->fld[2].v.cstr[0]) { case 'i': case 'I': if (cfg_declare_int(rec->fld[0].v.cstr, rec->fld[1].v.cstr, 0, rec->fld[3].v.int4, rec->fld[4].v.int4, rec->fld[5].v.cstr) < 0) { ERR(MODULE_NAME": Error declaring cfg int '%s.%s'\n", rec->fld[0].v.cstr, rec->fld[1].v.cstr); return E_CFG; } break; case 's': case 'S': if (cfg_declare_str(rec->fld[0].v.cstr, rec->fld[1].v.cstr, "", rec->fld[5].v.cstr) < 0) { ERR(MODULE_NAME": Error declaring cfg str '%s.%s'\n", rec->fld[0].v.cstr, rec->fld[1].v.cstr); return E_CFG; } break; default: ERR(MODULE_NAME": bad custom value type '%s'\n", rec->fld[2].v.cstr); return E_CFG; } rec = db_next(res); } db_res_free(res); db_cmd_free(cmd); /* register into config framework */ DBG(MODULE_NAME": mod_init: registering cfg callback\n"); if (cfg_register_ctx(&cfg_ctx, on_declare) < 0) { ERR(MODULE_NAME": failed to register cfg context\n"); return -1; } return 0; }
/* translate name using translation table, returns 0..not found, 1..success, -1..error */ static int find_cfg_var(str *group_name, char *def_name, db_res_t *transl_res) { db_rec_t *transl_rec; int ret = -1; DBG(MODULE_NAME": find_cfg_var('%.*s', '%s', ...)\n", group_name->len, group_name->s, def_name); transl_rec = db_first(transl_res); /* iterate through each candidate where cfg def may be found */ while (transl_rec) { static db_cmd_t* cmd; db_rec_t *rec; db_res_t *res; db_fld_t params[3], cols[2]; memset(cols, 0, sizeof(cols)); cols[0].name = GETCSTR(transl_rec->fld[3], def_cfg_table_value_field); cols[0].type = DB_NONE; memset(params, 0, sizeof(params)); params[0].name = GETCSTR(transl_rec->fld[1], def_cfg_table_group_name_field); params[0].type = DB_STR; params[0].op = DB_EQ; params[1].name = GETCSTR(transl_rec->fld[2], def_cfg_table_name_field); params[1].type = DB_CSTR; params[1].op = DB_EQ; DBG(MODULE_NAME": exec_transl: looking in '%s'\n", GETCSTR(transl_rec->fld[0], def_cfg_table)); cmd = db_cmd(DB_GET, db_cntx, GETCSTR(transl_rec->fld[0], def_cfg_table), cols, params, NULL); if (!cmd) { ERR(MODULE_NAME": Error preparing query '%s'\n", transl_tbl); return -1; } cmd->match[0].flags &= ~DB_NULL; cmd->match[0].v.lstr = *group_name; cmd->match[1].flags &= ~DB_NULL; cmd->match[1].v.cstr = def_name; // FIXME: proprietary code! db_setopt(cmd, "key", "bySerGroup"); db_setopt(cmd, "key_omit", 0); if (db_exec(&res, cmd) < 0) { ERR(MODULE_NAME": Error executing query '%s'\n", transl_tbl); db_cmd_free(cmd); return -1; } rec = db_first(res); if (rec) { /* var found in config table */ str def_name_s; def_name_s.s = def_name; def_name_s.len = strlen(def_name); DBG(MODULE_NAME": exec_transl: found record, type:%d\n", rec->fld[0].type); /* read and set cfg var */ switch (rec->fld[0].type) { case DB_STR: if (cfg_set_now(cfg_ctx, group_name, NULL /* group id */, &def_name_s, &rec->fld[0].v.lstr, CFG_VAR_STR) < 0) goto err; break; case DB_CSTR: if (cfg_set_now_string(cfg_ctx, group_name, NULL /* group id */, &def_name_s, rec->fld[0].v.cstr) < 0) goto err; break; case DB_INT: if (cfg_set_now_int(cfg_ctx, group_name, NULL /* group id */, &def_name_s, rec->fld[0].v.int4) < 0) goto err; break; default: ERR(MODULE_NAME": unexpected field type (%d), table:'%s', field:'%s'\n", rec->fld[0].type, GETCSTR(transl_rec->fld[0], def_cfg_table), GETCSTR(transl_rec->fld[3], def_cfg_table_value_field) ); goto err; } ret = 1; err: db_res_free(res); db_cmd_free(cmd); return ret; } db_res_free(res); db_cmd_free(cmd); transl_rec = db_next(transl_res); } return 0; }