Esempio n. 1
0
File: data.c Progetto: Tayyib/uludag
int
db_put_script(int node_no, const char *app, const char *buffer, size_t size)
{
	struct databases db;
	char *key;
	int e, ret = -1;

	if (open_env(&db, APP_DB | MODEL_DB | CODE_DB)) goto out;

	key = make_key(node_no, app);
	if (!key) goto out;

	e = append_item(db.app, app, model_get_path(node_no));
	if (e) goto out;

	e = put_data(db.code, key, buffer, size);
	if (e) goto out;

	e = append_item(db.model, model_get_path(node_no), app);
	if (e) goto out;

	ret = 0;
out:
	close_env(&db);
	return ret;
}
Esempio n. 2
0
File: job.c Progetto: Tayyib/uludag
static int
do_event(const char *event, int node, const char *app, struct pack *p)
{
	struct timeval start, end;
	unsigned long msec;
	int e;
	char *code;
	size_t code_size;

	log_debug(LOG_JOB, "Event(%s,%s,%s)\n", event, model_get_path(node), app);

	bk_app = strdup(app);
	bk_node = node;

	csl_setup();

	if (0 != db_get_code(node, app, &code, &code_size)) {
		return -1;
	}

	gettimeofday(&start, NULL);
	e = csl_execute(code, code_size, event, p, NULL, NULL);
	gettimeofday(&end, NULL);

	msec = time_diff(&start, &end);
	if (msec > 60*1000) {
		log_info("Script %s (%s) took %d seconds for %s event.\n", bk_app, model_get_path(node), msec / 1000, event);
	} else {
		log_debug(LOG_PERF, "Script %s (%s) took %d seconds for %s event.\n", bk_app, model_get_path(node), msec / 1000, event);
	}

	return e;
}
Esempio n. 3
0
File: csl.c Progetto: Tayyib/uludag
static void
log_exception(void)
{
	PyObject *pType;
	PyObject *pValue;
	PyObject *pTrace;
	char *eStr;
	char *vStr = "";
	long lineno = 0;

	PyErr_Fetch(&pType, &pValue, &pTrace);
	if (!pType) {
		log_error("csl.c log_exception() called when there isn't an exception\n");
		return;
	}

	eStr = PyString_AsString(PyObject_Str(pType));

	if (pValue) {
		PyObject *tmp;
		tmp = PyObject_Str(pValue);
		if (tmp) vStr = PyString_AsString(tmp);
	}

	if (pTrace) {
		PyObject *tmp;
		tmp = PyObject_GetAttrString(pTrace, "tb_lineno");
		if (tmp) lineno = PyInt_AsLong(tmp);
	}

	log_error("Python Exception [%s] in (%s,%s,%ld): %s\n",
		eStr, model_get_path(bk_node), bk_app, lineno, vStr
	);
}
Esempio n. 4
0
File: job.c Progetto: Tayyib/uludag
static int
do_register(int node, const char *app, const char *fname)
{
	char *buf;
	char *code;
	size_t codelen;
	int e;

	log_debug(LOG_JOB, "Register(%s,%s,%s)\n", model_get_path(node), app, fname);

	csl_setup();

	buf = load_file(fname, NULL);
	if (!buf) {
		send_result(CMD_ERROR, "no file", 7);
		return -1;
	}

	e = csl_compile(buf, "test", &code, &codelen);
	if (e) {
		send_result(CMD_ERROR, "compile error", 13);
		return -1;
	}

	db_put_script(node, app, code, codelen);

	send_result(CMD_RESULT, "registered", 10);

	csl_cleanup();

	return 0;
}
Esempio n. 5
0
File: job.c Progetto: Tayyib/uludag
static int
do_execute(int node, const char *app)
{
	char *code;
	char *res;
	size_t code_size;
	size_t res_size;
	int e;

	log_debug(LOG_JOB, "Execute(%s,%s)\n", model_get_path(node), app);

	bk_app = (char *) app;

	csl_setup();

	if (0 != db_get_code(model_parent(node), app, &code, &code_size)) return -1;
	e = csl_execute(code, code_size, model_get_method(node), &res, &res_size);
	if (e) {
		send_result(CMD_ERROR, "err", 3);
	} else {
		send_result(CMD_RESULT, res, res_size);
	}
	free(res);

	csl_cleanup();

	return e;
}
Esempio n. 6
0
File: job.c Progetto: Tayyib/uludag
static int
do_call_package(int node, const char *app, struct pack *p)
{
	log_debug(LOG_JOB, "CallPackage(%s,%s)\n", model_get_path(node), app);

	do_execute(node, app, p);

	return 0;
}
Esempio n. 7
0
File: job.c Progetto: Tayyib/uludag
static int
do_call(int node)
{
	char *apps;

	log_debug(LOG_JOB, "Call(%s)\n", model_get_path(node));

	if (db_get_apps(model_parent(node), &apps) != 0) {
		send_result(CMD_ERROR, "no app", 6);
		exit(1);
	}

	if (strchr(apps, '/') == NULL) {
		// there is only one script
		do_execute(node, apps);
	} else {
		// multiple scripts, run concurrently
		char *t, *s;
		struct ProcChild *p;
		int cmd;
		int cnt = 0;
		size_t size;

		// FIXME: package count
		send_result(CMD_RESULT_START, NULL, 0);
		for (t = apps; t; t = s) {
			s = strchr(t, '/');
			if (s) {
				*s = '\0';
				++s;
			}
			bk_node = node;
			bk_app = t;
			p = proc_fork(exec_proc, "SubJob");
			if (p) {
				++cnt;
			} else {
				send_result(CMD_ERROR, "fork failed", 11);
			}
		}
		while(1) {
			struct ipc_data *ipc;
			proc_listen(&p, &cmd, &size, -1);
			if (cmd == CMD_FINISH) {
				--cnt;
				if (!cnt) break;
			} else {
				proc_recv(p, &ipc, size);
				proc_send(TO_PARENT, cmd, ipc, size);
			}
		}
		send_result(CMD_RESULT_END, NULL, 0);
	}

	return 0;
}
Esempio n. 8
0
File: data.c Progetto: Tayyib/uludag
static char *
make_profile_key(int method, const char *app, const char *inst_key, const char *inst_value)
{
	size_t len;
	const char *node;
	char *inst_sep = "";
	char *key;

// key format: Node / [App] / [instance = [ value ] ]

	if (inst_key)
		// instances belong to the class
		node = model_get_path(model_parent(method));
	else
		// globals belong to the method
		node = model_get_path(method);

	len = strlen(node) + 3;

	if (app)
		len += strlen(app);
	else
		app = "";

	if (inst_key) {
		if (!inst_value) inst_value = "";
		len += strlen(inst_key) + 1 + strlen(inst_value);
		inst_sep = "=";
	} else {
		inst_key = "";
		inst_value = "";
	}

	key = malloc(len);
	if (!key) return NULL;

	snprintf(key, len, "%s/%s/%s%s%s", node, app, inst_key, inst_sep, inst_value);

	return key;
}
Esempio n. 9
0
File: data.c Progetto: Tayyib/uludag
int
db_put_script(int node_no, const char *app, const char *buffer, size_t size)
{
	struct databases db;
	int e, ret = -1;

	if (open_env(&db, APP_DB | MODEL_DB)) goto out;

	e = append_item(db.app, app, model_get_path(node_no));
	if (e) goto out;

	e = append_item(db.model, model_get_path(node_no), app);
	if (e) goto out;

	ret = 0;
out:
	close_env(&db);
	if (ret == 0) {
		ret = db_save_code(node_no, app, buffer);
	}
	return ret;
}
Esempio n. 10
0
File: data.c Progetto: Tayyib/uludag
int
db_get_apps(int node_no, char **bufferp)
{
	struct databases db;
	int e, ret = -1;

	if (open_env(&db, MODEL_DB)) goto out;

	*bufferp = get_data(db.model, model_get_path(node_no), NULL, &e);
	if (e) goto out;

	ret = 0;
out:
	close_env(&db);
	return ret;
}
Esempio n. 11
0
File: data.c Progetto: Tayyib/uludag
static char *
make_key(int node_no, const char *app)
{
	const char *path;
	char *key;
	size_t size;

	path = model_get_path(node_no);
	size = strlen(path) + 1 + strlen(app) + 1;

	key = malloc(size);
	if (!key) return NULL;
	snprintf(key, size, "%s/%s", path, app);

	return key;
}
Esempio n. 12
0
static void
add_groups(iks *tag, int class_no, int level, struct acl_class *ac)
{
    iks *x;
    // global permissions
    for (x = iks_find(tag, "group"); x; x = iks_next_tag(x)) {
        if (iks_strcmp(iks_name(x), "group") == 0)
            add_group(x, level, ac);
    }
    // class permissions
    x = iks_find_with_attrib(tag, "class", "name", model_get_path(class_no));
    for (x = iks_find(x, "group"); x; x = iks_next_tag(x)) {
        if (iks_strcmp(iks_name(x), "group") == 0)
            add_group(x, level, ac);
    }
}
Esempio n. 13
0
File: job.c Progetto: Tayyib/uludag
static int
do_getlist(int node)
{
	char *apps;

	log_debug(LOG_JOB, "GetList(%s)\n", model_get_path(node));

	if (db_get_apps(node, &apps) != 0) {
		send_result(CMD_RESULT, NULL, 0);
	} else {
		char *t;
		for (t = apps; *t; t++) {
			if (*t == '/') *t = '\n';
		}
		send_result(CMD_RESULT, apps, 0);
	}
	return 0;
}
Esempio n. 14
0
static int
count_groups(iks *tag, int class_no)
{
    iks *x;
    unsigned int nr = 0;
    // global permissions
    for (x = iks_find(tag, "group"); x; x = iks_next_tag(x)) {
        if (iks_strcmp(iks_name(x), "group") == 0)
            ++nr;
    }
    // class permissions
    x = iks_find_with_attrib(tag, "class", "name", model_get_path(class_no));
    for (x = iks_find(x, "group"); x; x = iks_next_tag(x)) {
        if (iks_strcmp(iks_name(x), "group") == 0)
            ++nr;
    }
    return nr;
}
Esempio n. 15
0
File: data.c Progetto: Tayyib/uludag
static char *
make_code_key(int node_no, const char *app)
{
	const char *path;
	char *key;
	char *t;
	size_t size;

	path = model_get_path(node_no);
	size = strlen(cfg_data_dir) + 6 + strlen(path) + 1 + strlen(app) + 4;

	key = malloc(size);
	if (!key) return NULL;

	snprintf(key, size, "%s/code/%s_%s.py", cfg_data_dir, path, app);

	for (t = key + size - 5; *t != '/'; t--) {
		if (*t == '.') *t = '_';
	}

	return key;
}
Esempio n. 16
0
File: job.c Progetto: Tayyib/uludag
static int
do_call(int node, struct pack *pak)
{
	struct pack *p = NULL;
	char *apps;
	int ok = 0;

	log_debug(LOG_JOB, "Call(%s)\n", model_get_path(node));

	if (model_flags(node) & P_GLOBAL) {
		p = pack_dup(pak);
	}

	if (db_get_apps(model_parent(node), &apps) != 0) {
		send_result(CMD_NONE, "noapp", 5);
		// FIXME: ok diyecek betik yoksa profile kayıt etmeli mi acaba
		exit(1);
	}

	if (strchr(apps, '/') == NULL) {
		// there is only one script
		if (0 == do_execute(node, apps, pak))
			ok = 1;
	} else {
		// multiple scripts, run concurrently
		char *t, *s;
		struct ProcChild *p;
		int cmd;
		int cnt = 0;
		size_t size;

		// FIXME: package count
		send_result(CMD_RESULT_START, NULL, 0);
		for (t = apps; t; t = s) {
			s = strchr(t, '/');
			if (s) {
				*s = '\0';
				++s;
			}
			bk_node = node;
			bk_app = t;
			bk_pak = pak;
			p = proc_fork(exec_proc, "ComarSubJob");
			if (p) {
				++cnt;
			} else {
				send_result(CMD_ERROR, "fork failed", 11);
			}
		}
		while(1) {
			struct ipc_struct ipc;
			struct pack *pak;
			pak = pack_new(128);
			proc_listen(&p, &cmd, &size, -1);
			if (cmd == CMD_FINISH) {
				--cnt;
				if (!cnt) break;
			} else {
				if (cmd == CMD_RESULT) ok++;
				proc_get(p, &ipc, pak, size);
				proc_put(TO_PARENT, cmd, &ipc, pak);
			}
		}
		send_result(CMD_RESULT_END, NULL, 0);
	}

    if ((model_flags(node) & P_GLOBAL) && ok) {
		db_put_profile(node, NULL, p);
		pack_delete(p);
	}

	return 0;
}
Esempio n. 17
0
File: job.c Progetto: Tayyib/uludag
static int
do_execute(int node, const char *app, struct pack *pak)
{
	struct timeval start, end;
	unsigned long msec;
	struct pack *p = NULL;
	char *code;
	char *res;
	size_t code_size;
	size_t res_size;
	int e;

	log_debug(LOG_JOB, "Execute(%s,%s)\n", model_get_path(node), app);

	bk_app = strdup(app);
	bk_node = node;

	if (model_flags(node) & P_PACKAGE) {
		p = pack_dup(pak);
	}

	csl_setup();

	if (0 != db_get_code(model_parent(node), app, &code, &code_size)) {
		send_result(CMD_NONE, "noapp", 5);
		return -1;
	}

	gettimeofday(&start, NULL);
	e = csl_execute(code, code_size, model_get_method(node), pak, &res, &res_size);
	gettimeofday(&end, NULL);
	if (e) {
		if (e == CSL_NOFUNC)
			send_result(CMD_NONE, "nomethod", 8);
		else
			send_result(CMD_ERROR, "err", 3);
	} else {
		send_result(CMD_RESULT, res, res_size);
		free(res);
	}

	msec = time_diff(&start, &end);
	if (msec > 60*1000) {
		log_info("Script %s took %d seconds for %s call.\n", bk_app, msec / 1000, model_get_path(node));
	} else {
		log_debug(LOG_PERF, "Script %s took %d miliseconds for %s call.\n", bk_app, msec, model_get_path(node));
	}

	if (model_flags(node) & P_PACKAGE) {
		if (0 == e) {
			if (model_flags(node) & P_DELETE)
				db_del_profile(node, bk_app, p);
			else
				db_put_profile(node, bk_app, p);
		}
		pack_delete(p);
	}

	csl_cleanup();

	return e;
}