Beispiel #1
0
/**
 * destroy function of module
 */
static void destroy(void)
{
	int i;
#ifdef XJ_EXTRA_DEBUG
	DBG("XJAB: Unloading module ...\n");
#endif
	if(pipes)
	{ // close the pipes
		for(i = 0; i < nrw; i++)
		{
			if(pipes[i])
			{
				close(pipes[i][0]);
				close(pipes[i][1]);
			}
			pkg_free(pipes[i]);
		}
		pkg_free(pipes);
	}

	if (ctx) db_ctx_free(ctx);
	ctx = NULL;
	
	xj_wlist_free(jwl);
	DBG("XJAB: Unloaded ...\n");
}
Beispiel #2
0
/* destroy the DB connection */
static void perm_destroy_db(void)
{
	if (db_conn) {
		db_disconnect(db_conn);
		db_ctx_free(db_conn);
		db_conn = NULL;
	}
}
Beispiel #3
0
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);
	}
}
Beispiel #4
0
/* returns -2..error happend in the past, do not try connect again, -1 .. error, 0..OK */
static int connect_db() {
	if (db_pid != getpid()) {
		db_pid = getpid();
		db_cntx = db_ctx(MODULE_NAME);
		if (db_cntx == NULL) {
			ERR(MODULE_NAME": Error while initializing database layer\n");
			return -1;
		}	
		if (db_add_db(db_cntx, db_url) < 0) {
			ERR(MODULE_NAME": Error adding database '%s'\n", db_url);
			db_ctx_free(db_cntx);
			db_cntx = NULL;
			return -1;
		}		
		if (db_connect(db_cntx) < 0) {
			ERR(MODULE_NAME": Error connecting database '%s'\n", db_url);
			db_ctx_free(db_cntx);
			db_cntx = NULL;
			return -1;
		}
	}
	if (!db_cntx) return -2; /* database has not been connected */
	return 0;
}
Beispiel #5
0
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;
	}
}
Beispiel #6
0
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;
}
Beispiel #7
0
static int init_db(struct dbops_action* p)
{
	db_fld_t* matches = NULL, *result = NULL, *values = NULL;
	int type, i;

	DEBUG(MODULE_NAME": init_db: query: %s(%d)\n", p->query_name, p->query_no);
	if (p->db_url == NULL) {
		ERR(MODULE_NAME": No database URL specified\n");
		return -1;
	}
	p->ctx = db_ctx(MODULE_NAME);
	if (p->ctx == NULL) {
		ERR(MODULE_NAME": Error while initializing database layer\n");
		return -1;
	}
	
	if (db_add_db(p->ctx, p->db_url) < 0) return -1;
	if (db_connect(p->ctx) < 0) return -1;

	if (p->is_raw_query) {
		type = DB_SQL;
		if (build_params(&matches, p) < 0) return -1;
	}
	else {
		switch(p->operation) {
		case INSERT_OPS:
		case REPLACE_OPS:
			type = DB_PUT;
			if (build_params(&values, p) < 0) return -1;
			break;

		case UPDATE_OPS:
			type = DB_UPD;
			if (build_match(&matches, p) < 0) return -1;
			if (build_params(&values, p) < 0) {
				if (matches) pkg_free(matches);
				return -1;
			}
			break;

		case DELETE_OPS:
			type = DB_DEL;

			if (build_match(&matches, p) < 0) return -1;
			break;

		case OPEN_QUERY_OPS:
			type = DB_GET;
			if (build_match(&matches, p) < 0) return -1;
			if (build_result(&result, p) < 0) {
				if (matches) pkg_free(matches);
				return -1;
			}
			break;
		default:
			BUG("Unknown operation %d\n", p->operation);
			return -1;
		}
	}

	p->cmd = db_cmd(type, p->ctx, p->table.s, result, matches, values);
	if (p->cmd == NULL) {
		ERR(MODULE_NAME": init_db: query: %s(%d), error while compiling database query\n", p->query_name, p->query_no);
		if (values) pkg_free(values);
		if (matches) pkg_free(matches);
		if (result) pkg_free(result);
		db_disconnect(p->ctx);
		db_ctx_free(p->ctx);
		return -1;
	}
	if (values) pkg_free(values);
	if (matches) pkg_free(matches);
	if (result) pkg_free(result);

	for (i=0; i<p->extra_ops_count; i++) {
		char *end;
		DEBUG(MODULE_NAME": init_db: query_no: %s(%d), setopt('%s', %i, '%s'\n", p->query_name, p->query_no, p->extra_ops[i].name, p->extra_ops[i].type, p->extra_ops[i].value);
		switch (p->extra_ops[i].type) {
			case DB_NONE: 
				/* set null ?? */
				break;			
			case DB_DATETIME: {
				time_t v;
				v = strtol(p->extra_ops[i].value, &end, 10);
				if (db_setopt(p->cmd, p->extra_ops[i].name, v) < 0) return -1;
				break;
			}
			case DB_INT: {
				int v;
				v = strtol(p->extra_ops[i].value, &end, 10);
				if (db_setopt(p->cmd, p->extra_ops[i].name, v) < 0) return -1;
				break;
			}
			case DB_FLOAT: {
				float v;
				#ifdef  __USE_ISOC99
				v = strtof(p->extra_ops[i].value, &end);
				#else
				v = strtod(p->extra_ops[i].value, &end);
				#endif
				if (db_setopt(p->cmd, p->extra_ops[i].name, v) < 0) return -1;
				break;
			}
			case DB_DOUBLE: {
				double v;
				v = strtod(p->extra_ops[i].value, &end);
				if (db_setopt(p->cmd, p->extra_ops[i].name, v) < 0) return -1;
				break;
			}
			case DB_CSTR:
				if (db_setopt(p->cmd, p->extra_ops[i].name, p->extra_ops[i].value) < 0) return -1;
				break;
		default:
				BUG("Unknown extra_op type: %d\n", p->extra_ops[i].type);
				return -1;
		}
	}
	return 0;
}
Beispiel #8
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;
}
Beispiel #9
0
/*
 * 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;
}