Ejemplo n.º 1
0
int knh_InputStream_getc(Ctx *ctx, knh_InputStream_t *in)
{
	int ch;
	if(knh_InputStream_isFILE(in)) {
		ch = SP(in)->dspi->fgetc(ctx, DP(in)->fd);
	}
	else {
		while(1) {
			if(DP(in)->bufpos < DP(in)->bufend) {
				ch = (knh_uchar_t)DP(in)->buf[DP(in)->bufpos];
				DP(in)->bufpos++;
				break;
			}
			DP(in)->bufpos = 0;
			DP(in)->bufend = SP(in)->dspi->fread(ctx, DP(in)->fd, DP(in)->buf, DP(in)->bufsiz);
			if(DP(in)->bufend == 0) return EOF;
		}
	}
	{ /* statstics */
		DP(in)->size++;
		if(ch == '\n') {
			if(DP(in)->prev != '\r') {
				DP(in)->line++;
			}
		}
		else if(ch == '\r') {
			DP(in)->line++;
		}
		else if(ch == EOF) {
			knh_InputStream_close(ctx, in);
		}
	}
	return ch;
}
Ejemplo n.º 2
0
static void knh_Actor_mainLoop(CTX ctx, knh_sfp_t *sfp, knh_Actor_t *a)
{
	const char *name = DP(a)->actor_name;
	const char *path = DP(a)->path;
	int port = DP(a)->port;
	fprintf(stderr, "======== <<< Actor Information >>> ========\n");
	fprintf(stderr, "name : %s\n", name);
	fprintf(stderr, "path : %s\n", path);
	fprintf(stderr, "port : %d\n", port);
	fprintf(stderr, "===========================================\n");
	//int prev_pid = 0;
	int status;
	knh_io_t fd = knh_ServerSocket_open(ctx, sfp, port, 3);
	while (true) {
		knh_io_t sd = knh_ServerSocket_accept(ctx, sfp, fd);
		int pid = fork();
		//fprintf(stderr, "==================\n");
		//fprintf(stderr, "pid = [%d]\n", pid);
		if (pid == 0) {
			//child
			//fprintf(stderr, "child\n");
			knh_InputStream_t *ins = knh_Socket_getInputStream(ctx, sd);
			knh_class_t cid = knh_getcid(ctx, new_bytes("ConnectionObject"));
			const knh_ClassTBL_t *ct = ClassTBL(cid);
			knh_Object_t *o = (knh_Object_t *)knh_InputStream_readObject(ctx, ins, ct);
			//fprintf(stderr, "mtd_name = %s\n", c->mtd_name);
			//knh_Object_t *msg = c->msg;
			knh_Actor_readMessage(ctx, a, o);
			knh_InputStream_close(ctx, ins);
			exit(0);
		} else if (pid < 0) {
			fprintf(stderr, "fork error\n");
		} else {
			//fprintf(stderr, "PARENT: pid = [%d]\n", pid);
			waitpid(pid, &status, 0);
			//prev_pid = pid;
		}
		close(sd);
	}
}
Ejemplo n.º 3
0
/* added by Wakamori */
void loadPolicy(CTX ctx)
{
	if (enforce_security == 0) return;
	// load $konoha.home.path/policy
	knh_setProperty(ctx, new_String(ctx, "role"), (dynamic *)new_String(ctx, role));
	CWB_t cwbbuf, *cwb = CWB_open0(ctx, &cwbbuf);
	kString *s = knh_getPropertyNULL(ctx, STEXT("konoha.home.path"));
	CWB_write(ctx, cwb, S_tobytes(s));
	CWB_write(ctx, cwb, STEXT("/policy"));
	kInputStream *is = new_InputStream(ctx, NULL, new_Path(ctx, CWB_newString0(ctx, cwb)));

	if (is == NULL) {
		DBG_P("policy file not found. All @Restricted annotated method is rescricted");
	}
	else {
		/*
		if (enforce_security == 0) {
			enforce_security = 1;
			knh_memcpy(role, "Default", 7);
			role[7] = '\0';
		}
		*/
		// parse policy file written in JSON
		// it must be refactored in the future
		kDictMap *dmap = ctx->share->securityDictMap;
		kString *line = knh_InputStream_readLine(ctx, is);
		while (IS_NOTNULL(line)) {
			//fprintf(stderr, "line=%s\n", S_totext(line));
			if (S_equals(line, STEXT("[")) || S_equals(line, STEXT("]"))) {
				/* ignore */
			} else {
				kString *key = NULL;
				kArray *a = new_Array(ctx, CLASS_String, 0);
				const char *idx = NULL;
				char *p = strstr(S_totext(line), "\"name\": \"");
				if (p != NULL) {
					p += 9; // == strlen("\"name\": \"")
					idx = strchr((const char *)p, '"');
					if (idx != NULL) {
						p[idx - p] = '\0';
						//fprintf(stderr, "name: %s\n", p);
						//knh_DictMap_set(ctx, dmap, new_String(ctx, "name"), new_String(ctx, p));
						key = new_String(ctx, p);
						p = (char *)idx + 1;
					}
				}
				p = strstr((const char *)p, "\"permission\": [");
				if (p != NULL) {
					p += 16; // == strlen("\"permission\": \[\"")
					idx = strchr((const char *)p, '"');
					while (idx != NULL) {
						p[idx - p] = '\0';
						if (strstr((const char *)p, ", ") == NULL) {
							//fprintf(stderr, "permission: %s\n", p);
							knh_Array_add(ctx, a, new_String(ctx, p));
						}
						p = (char *)idx + 1;
						idx = strchr((const char *)p, '"');
					}
				}
				if (key != NULL) {
					knh_DictMap_set(ctx, dmap, key, a);
				}
			}
			line = knh_InputStream_readLine(ctx, is);
		}
		knh_InputStream_close(ctx, is);
	}
}