int do_test_userland(const char *test_parm) { (void) test_parm; char testdata[5]; int res; int32_t code; strcpy(testdata, "abcd"); SHOW_INFO0( 0, "port_create()"); uland_port = port_create(1, "__regress_test_port"); test_check_ge(uland_port,0); SHOW_INFO0( 0, "port_write()"); res = port_write(uland_port, 1, &testdata, sizeof(testdata)); test_check_eq(res,0); testdata[0] = 0; SHOW_INFO0( 0, "port_read()"); res = port_read_etc(uland_port, &code, &testdata, sizeof(testdata), PORT_FLAG_TIMEOUT, 1000000); test_check_eq(res,5); test_check_eq(code,0xAA); test_check_eq( strcmp(testdata, "abcd"), 0); SHOW_INFO0( 0, "port_read() - wait for userland to finish"); res = port_read_etc(uland_port, &code, &testdata, sizeof(testdata), PORT_FLAG_TIMEOUT, 1000000); test_check_ge(res,0); test_check_eq(code,0x55); SHOW_INFO0( 0, "close port"); res = port_close(uland_port); test_check_ne(res,0); #if 0 SHOW_INFO0( 0, "delete port"); res = port_delete(test_p2); test_check_eq(res,0); #endif SHOW_INFO0( 0, "end test"); return 0; }
ssize_t user_port_read_etc(port_id uport, int32 *umsg_code, void *umsg_buffer, size_t ubuffer_size, uint32 uflags, bigtime_t utimeout) { ssize_t res; int32 msg_code; int rc; if (umsg_code == NULL) return ERR_INVALID_ARGS; if (umsg_buffer == NULL) return ERR_INVALID_ARGS; if(is_kernel_address(umsg_code)) return ERR_VM_BAD_USER_MEMORY; if(is_kernel_address(umsg_buffer)) return ERR_VM_BAD_USER_MEMORY; res = port_read_etc(uport, &msg_code, umsg_buffer, ubuffer_size, uflags | PORT_FLAG_USE_USER_MEMCPY | SEM_FLAG_INTERRUPTABLE, utimeout); rc = user_memcpy(umsg_code, &msg_code, sizeof(int32)); if(rc < 0) return rc; return res; }
ssize_t port_read(port_id port, int32 *msg_code, void *msg_buffer, size_t buffer_size) { return port_read_etc(port, msg_code, msg_buffer, buffer_size, 0, 0); }
void port_test() { char testdata[5]; thread_id t; int res; int32 dummy; int32 dummy2; strcpy(testdata, "abcd"); dprintf("porttest: port_create()\n"); test_p1 = port_create(1, "test port #1"); test_p2 = port_create(10, "test port #2"); test_p3 = port_create(1024, "test port #3"); test_p4 = port_create(1024, "test port #4"); dprintf("porttest: port_find()\n"); dprintf("'test port #1' has id %d (should be %d)\n", port_find("test port #1"), test_p1); dprintf("porttest: port_write() on 1, 2 and 3\n"); port_write(test_p1, 1, &testdata, sizeof(testdata)); port_write(test_p2, 666, &testdata, sizeof(testdata)); port_write(test_p3, 999, &testdata, sizeof(testdata)); dprintf("porttest: port_count(test_p1) = %d\n", port_count(test_p1)); dprintf("porttest: port_write() on 1 with timeout of 1 sec (blocks 1 sec)\n"); port_write_etc(test_p1, 1, &testdata, sizeof(testdata), PORT_FLAG_TIMEOUT, 1000000); dprintf("porttest: port_write() on 2 with timeout of 1 sec (wont block)\n"); res = port_write_etc(test_p2, 777, &testdata, sizeof(testdata), PORT_FLAG_TIMEOUT, 1000000); dprintf("porttest: res=%d, %s\n", res, res == 0 ? "ok" : "BAD"); dprintf("porttest: port_read() on empty port 4 with timeout of 1 sec (blocks 1 sec)\n"); res = port_read_etc(test_p4, &dummy, &dummy2, sizeof(dummy2), PORT_FLAG_TIMEOUT, 1000000); dprintf("porttest: res=%d, %s\n", res, res == ERR_PORT_TIMED_OUT ? "ok" : "BAD"); dprintf("porttest: spawning thread for port 1\n"); t = thread_create_kernel_thread("port_test", port_test_thread_func, NULL); // resume thread thread_resume_thread(t); dprintf("porttest: write\n"); port_write(test_p1, 1, &testdata, sizeof(testdata)); // now we can write more (no blocking) dprintf("porttest: write #2\n"); port_write(test_p1, 2, &testdata, sizeof(testdata)); dprintf("porttest: write #3\n"); port_write(test_p1, 3, &testdata, sizeof(testdata)); dprintf("porttest: waiting on spawned thread\n"); thread_wait_on_thread(t, NULL); dprintf("porttest: close p1\n"); port_close(test_p2); dprintf("porttest: attempt write p1 after close\n"); res = port_write(test_p2, 4, &testdata, sizeof(testdata)); dprintf("porttest: port_write ret %d\n", res); dprintf("porttest: testing delete p2\n"); port_delete(test_p2); dprintf("porttest: end test main thread\n"); }