Ejemplo n.º 1
0
int client_test()
{
	int i, status;
	int count = 0;
	int normal = 0;
	pid_t pid[NUM_THREADS], wpid;
	unsigned long addr, len;
	unsigned long addr_to_map, addr_to_brk;
	void *ret[NUM_THREADS];
	cat_t ar = create_category(CAT_S);
        cat_t aw = create_category(CAT_I);       
        label_t L1 = {ar, aw};
        label_t L2 = {ar};
	own_t O = {};
	label_t L_self, L_test;
	own_t O_self;
	
	addr = (unsigned long)ab_calloc(256, 4, L1);
	printf("child A malloc: %lx\n", addr);
	//printf("addr = %lx\n", *(unsigned long *)addr);
	//*(unsigned long *)addr = 0x12345678;

	// test code for ab_realloc()
	addr = (unsigned long)ab_malloc(11*4096, L2);
	*(unsigned long *)addr = 0xdeadbeef;
	printf("child A malloc: %lx, %lx\n", addr, *(unsigned long *)addr);
	addr = (unsigned long)ab_realloc((void *)addr, 12*4096);
	printf("child A realloc: %lx, %lx\n", addr, *(unsigned long *)addr);

	// test code for get_ownership()
/*	get_ownership(O_self);
	print_label(O_self);
*/
	// test code for pthread mutex and condition variable
	//mutex = (pthread_mutex_t *)ab_malloc(sizeof(pthread_mutex_t), L1);
	pthread_mutexattr_t attr;
	Pthread_mutexattr_init(&attr);
	Pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);
	Pthread_mutex_init(mutex, &attr);

	cond = (pthread_cond_t *)ab_malloc(sizeof(pthread_cond_t), L1);
	pthread_condattr_t condattr;
	Pthread_condattr_init(&condattr);
	Pthread_condattr_setpshared(&condattr, PTHREAD_PROCESS_SHARED);
	Pthread_cond_init(cond, &condattr);

	// test code for ab_pthread_create() and ab_pthread_join()
	pthread_t tid[NUM_THREADS];
	struct child_arg tdata[NUM_THREADS];
	
	for (i = 0; i < NUM_THREADS; ++i) {
		if (i == 0) {
			tdata[i]._addr = addr;
			tdata[i].addr_to_map = (void *)(mutex);
			tdata[i].i = i;
			ab_pthread_create(&tid[i], NULL, child_func, &tdata[i], L1, O);
		}
		if (i == 1) {
			tdata[i].i = i;
			ab_pthread_create(&tid[i], NULL, child_func, &tdata[i], L2, O);
		}
		if (i == 2) {
			tdata[i].i = i;
			ab_pthread_create(&tid[i], NULL, child_func, &tdata[i], (label_t){}, (own_t){});
		}
		if (i == 3) {
			tdata[i].i = i;
			ab_pthread_create(&tid[i], NULL, child_func, &tdata[i], (label_t){}, O);
		}
	}
	//wait some time for all the child to start
	//sleep(10);
	//printf("child A: addr = %lx\n", *(unsigned long *)addr);
	//*(unsigned long *)addr = 0xdeadbeef;
	// test code for pthread mutex
	AB_DBG("child A: mutex = %p\n", mutex);
	AB_DBG("child A: ");
	debug_mutex(mutex);
	Pthread_mutex_lock(&mmutex);
	//Pthread_cond_wait(cond, mutex);
	AB_DBG("child A: ");
	debug_mutex(mutex);
	printf("child A: doing somthing\n");
	printf("child A: global_uninit=%lx\n", global_uninit);
	printf("child A: global_init=%lx\n", global_init);
	printf("child A: &global_uninit=%p\n", &global_uninit);
	printf("child A: &global_init=%p\n", &global_init);
	global_init = 0xdead0000;
	global_uninit = 0xdead0000;
	printf("child A: updated global_uninit=%lx\n", global_uninit);
	printf("child A: updated global_init=%lx\n", global_init);
	sleep(10);	
	Pthread_mutex_unlock(&mmutex);	
	AB_DBG("child A: ");
	debug_mutex(mutex);
	// test code for ablib_brk()
//	addr = (unsigned long)ablib_sbrk(pid[0], 0);
//	printf("child 0 sbrk: %lx\n", addr);
	
	// test code for ab_malloc()
/*	addr = (unsigned long)ab_malloc(41000, L2);
	printf("child A malloc: %lx\n", addr);
	//ab_free((void *)addr);
	addr = (unsigned long)ab_malloc(11*1024*4, L2);
	printf("child A malloc: %lx\n", addr);
*/
	// test code for ab_malloc() and ab_free() exhaustively
/*	unsigned long addr_list[100];
	for (i = 0; i < 100; i = i + 1) {
		addr_list[i] = (unsigned long)ab_malloc(i*1000, L2);
		printf("child A malloc: %lx, size = %d\n", addr_list[i], i*1000);
	}
	for (i = 0; i < 100; i = i + 1) {
		ab_free((void *)addr_list[i]);
		printf("child A free: %lx\n", addr_list[i]);
	}
	for (i = 0; i < 100; i = i + 1) {
		addr_list[i] = (unsigned long)ab_malloc(i*1000, L2);
		printf("child A malloc: %lx, size = %d\n", addr_list[i], i*1000);
	}
*/
	// test code for get_mem_label()
/*	addr = (unsigned long)ab_malloc(4, L2);
	printf("child A malloc: %lx\n", addr);
	print_label(L2);
	get_mem_label((void *)addr, L_test);
	print_label(L_test);
*/

	// test code for get_label() & get_mem_label();
/*	get_label(L_self);
	print_label(L_self);
	addr = (unsigned long)ab_malloc(4, L_self);
	printf("child A malloc: %lx\n", addr);

	get_mem_label((void *)addr, L_test);
	print_label(L_test);
*/	

	
/*	while(1) {
		wpid = waitpid(-1, &status, 0);
		if(wpid <= 0)
			break;
		count++;
		if(WIFEXITED(status)) {
			printf("child %lu exited normally. count = %d\n", 
			       (unsigned long)wpid, count);
			normal++;
		}
		if(WIFSIGNALED(status)) {
			printf("child %lu terminated by a signal. count = %d\n",
			       (unsigned long)wpid, count);
		}
		else
			printf("wait pid returns with status %d\n", status);
	}
*/	
	// test code for ab_pthread_create() and ab_pthread_join()
	for (i = 0; i < NUM_THREADS; ++i) {
		AB_DBG("main: tid[%d] = %d\n", i, (int)tid[i]);
		if (ab_pthread_join(tid[i], NULL) == 0) {
			normal++;
		}
	}
	
	if (normal == NUM_THREADS) {
		printf("mapping test successful.\n");
		return 0;
	}


test_failed:
	printf("test failed!!\n");
	return -1;			

}
Ejemplo n.º 2
0
int
main (int argc, char **argv)
{
        data_t data = { 0, };
        int retval = EXIT_SUCCESS, i = 0, cat_result = 0;

        openlog (argv[0], LOG_NOWAIT | LOG_PID, LOG_DAEMON);
        if (argc < 2) {
                syslog (LOG_EMERG, "argc is less than 2, that's very wrong");
                retval = EXIT_FAILURE;
                goto exit;
        }
        if (is_selinux_enabled () != 1) {
                syslog (LOG_WARNING, "SELinux is disabled. sVirt will do nothing.");
                goto exit;
        }
        /*  not really parsing parameters, just going on position  */
        data.domid = atoi (argv [1]);
        syslog (LOG_INFO, "domain id: %d", data.domid);

        data.xsh = xs_daemon_open();
        if (data.xsh == NULL) {
                syslog (LOG_CRIT, "ERROR connecting to XenStore. Halting");
                retval = EXIT_FAILURE;
                goto exit;
        }
        /*  get files that we need to relabele  */
        data.files = get_writable_files (data.xsh, data.domid);
        if (data.files == NULL) {
                syslog (LOG_CRIT, "ERROR getting files. Halting");
                retval = EXIT_FAILURE;
                goto exit_session;
        }
        for (i = 0; data.files [i] != NULL; ++i)
                syslog (LOG_INFO, "got file: %s", data.files [i]);
        /*  get category for our domid and save it to xenstore  */
        cat_result = create_category (data.xsh);
        if (cat_result < 0) {
                syslog (LOG_CRIT, "ERROR generating unique category. Halting");
                retval = EXIT_FAILURE;
                goto exit_files;
        }
        data.category = cat_result;
        syslog (LOG_INFO, "got unique mcs: %d", data.category);
        if (set_domid_category (data.xsh, data.domid, data.category) == false) {
                syslog (LOG_CRIT, "ERROR setting category. Halting");
                retval = EXIT_FAILURE;
                goto exit_files;
        }
        /*  SELinux stuff  */
        /*  get SELinux default contexts  */
        if (get_default_contexts (&data) != 0) {
                syslog (LOG_CRIT, "ERROR getting default contexts. Halting");
                retval = EXIT_FAILURE;
                goto exit_files;
        }
        /*  label files  */
        if (file_con_fixup (&data) != 0) {
                syslog (LOG_CRIT,
                        "ERROR setting contexts for VM device files. Halting");
                retval = EXIT_FAILURE;
                goto exit_files;
        }
        /*  Set Execution Context  */
        if (set_exec_context (&data) != true) {
                syslog (LOG_CRIT,
                        "ERROR setting context to %s for qemu execution: %s. Halting",
                        strerror (errno));
                retval = EXIT_FAILURE;
                goto exit_files;
        }
        syslog (LOG_INFO, "Successfully set set MCS label %d for domid %d",
                data.category, data.domid);
exit_files:
        if (data.files != NULL) {
                for (i = 0; data.files [i] != NULL; ++i)
                        free (data.files [i]);
                free (data.files);
        }
exit_session:
        if (data.xsh != NULL) 
                xs_daemon_close (data.xsh);
exit:
        closelog ();
        /*  execute the real qemu if no previous errors prevent it  */
        if (retval != EXIT_FAILURE)
                exec_cmd (argv);
        exit (retval);
}