HelenOS sources
This source file includes following definitions.
- ipc_test_get_ro_area_size_srv
- ipc_test_get_rw_area_size_srv
- ipc_test_share_in_ro_srv
- ipc_test_share_in_rw_srv
- ipc_test_set_rw_buf_size_srv
- ipc_test_read_srv
- ipc_test_write_srv
- ipc_test_connection
- main
#include <as.h>
#include <async.h>
#include <errno.h>
#include <str_error.h>
#include <io/log.h>
#include <ipc/ipc_test.h>
#include <ipc/services.h>
#include <loc.h>
#include <mem.h>
#include <stdio.h>
#include <stdlib.h>
#include <task.h>
#define NAME "ipc-test"
static service_id_t svc_id;
enum {
max_rw_buf_size = 16384,
};
static const char *ro_data = "Hello, world!";
static char rw_data[] = "Hello, world!";
static void *rw_buf;
size_t rw_buf_size;
static void ipc_test_get_ro_area_size_srv(ipc_call_t *icall)
{
errno_t rc;
as_area_info_t info;
log_msg(LOG_DEFAULT, LVL_DEBUG, "ipc_test_get_ro_area_size_srv");
rc = as_area_get_info((void *)ro_data, &info);
if (rc != EOK) {
async_answer_0(icall, EIO);
log_msg(LOG_DEFAULT, LVL_ERROR, "as_area_get_info failed");
return;
}
async_answer_1(icall, EOK, info.size);
}
static void ipc_test_get_rw_area_size_srv(ipc_call_t *icall)
{
errno_t rc;
as_area_info_t info;
log_msg(LOG_DEFAULT, LVL_DEBUG, "ipc_test_get_rw_area_size_srv");
rc = as_area_get_info(rw_data, &info);
if (rc != EOK) {
async_answer_0(icall, EIO);
log_msg(LOG_DEFAULT, LVL_ERROR, "as_area_get_info failed");
return;
}
async_answer_1(icall, EOK, info.size);
}
static void ipc_test_share_in_ro_srv(ipc_call_t *icall)
{
ipc_call_t call;
errno_t rc;
size_t size;
as_area_info_t info;
log_msg(LOG_DEFAULT, LVL_DEBUG, "ipc_test_share_in_ro_srv");
if (!async_share_in_receive(&call, &size)) {
async_answer_0(icall, EINVAL);
log_msg(LOG_DEFAULT, LVL_ERROR, "share_in_receive failed");
return;
}
rc = as_area_get_info((void *)ro_data, &info);
if (rc != EOK) {
async_answer_0(icall, EINVAL);
log_msg(LOG_DEFAULT, LVL_ERROR, "as_area_get_info failed");
return;
}
if (size != info.size) {
log_msg(LOG_DEFAULT, LVL_ERROR, "size(%zu) != %zu",
size, info.size);
async_answer_0(icall, EINVAL);
return;
}
rc = async_share_in_finalize(&call, (void *) info.start_addr,
AS_AREA_READ);
if (rc != EOK) {
log_msg(LOG_DEFAULT, LVL_ERROR,
" - async_share_in_finalize failed");
async_answer_0(icall, EINVAL);
return;
}
async_answer_0(icall, EOK);
}
static void ipc_test_share_in_rw_srv(ipc_call_t *icall)
{
ipc_call_t call;
errno_t rc;
size_t size;
as_area_info_t info;
log_msg(LOG_DEFAULT, LVL_DEBUG, "ipc_test_share_in_ro_srv");
if (!async_share_in_receive(&call, &size)) {
async_answer_0(icall, EINVAL);
log_msg(LOG_DEFAULT, LVL_ERROR, "share_in_receive failed");
return;
}
rc = as_area_get_info(rw_data, &info);
if (rc != EOK) {
async_answer_0(icall, EINVAL);
log_msg(LOG_DEFAULT, LVL_ERROR, "as_area_get_info failed");
return;
}
if (size != info.size) {
log_msg(LOG_DEFAULT, LVL_ERROR, " size(%zu) != %zu",
size, info.size);
async_answer_0(icall, EINVAL);
return;
}
rc = async_share_in_finalize(&call, (void *) info.start_addr,
AS_AREA_READ | AS_AREA_WRITE);
if (rc != EOK) {
log_msg(LOG_DEFAULT, LVL_ERROR,
"async_share_in_finalize failed");
async_answer_0(icall, EINVAL);
return;
}
async_answer_0(icall, EOK);
}
static void ipc_test_set_rw_buf_size_srv(ipc_call_t *icall)
{
size_t size;
void *nbuf;
log_msg(LOG_DEFAULT, LVL_DEBUG, "ipc_test_set_rw_buf_size_srv");
size = ipc_get_arg1(icall);
if (size == 0) {
async_answer_0(icall, ERANGE);
log_msg(LOG_DEFAULT, LVL_ERROR,
"Requested read/write buffer size is zero.");
return;
}
if (size > max_rw_buf_size) {
async_answer_0(icall, ERANGE);
log_msg(LOG_DEFAULT, LVL_ERROR, "Requested read/write buffer "
"size > %u", max_rw_buf_size);
return;
}
nbuf = realloc(rw_buf, size);
if (nbuf == NULL) {
async_answer_0(icall, ENOMEM);
log_msg(LOG_DEFAULT, LVL_ERROR, "Out of memory.");
return;
}
rw_buf = nbuf;
rw_buf_size = size;
async_answer_0(icall, EOK);
}
static void ipc_test_read_srv(ipc_call_t *icall)
{
ipc_call_t call;
errno_t rc;
size_t size;
log_msg(LOG_DEFAULT, LVL_DEBUG, "ipc_test_read_srv");
if (!async_data_read_receive(&call, &size)) {
async_answer_0(icall, EREFUSED);
log_msg(LOG_DEFAULT, LVL_ERROR, "data_read_receive failed");
return;
}
if (size > rw_buf_size) {
async_answer_0(&call, EINVAL);
async_answer_0(icall, EINVAL);
log_msg(LOG_DEFAULT, LVL_ERROR, "Invalid read size.");
return;
}
rc = async_data_read_finalize(&call, rw_buf, size);
if (rc != EOK) {
log_msg(LOG_DEFAULT, LVL_ERROR,
"data_read_finalize failed");
async_answer_0(icall, EINVAL);
return;
}
async_answer_0(icall, EOK);
}
static void ipc_test_write_srv(ipc_call_t *icall)
{
ipc_call_t call;
errno_t rc;
size_t size;
log_msg(LOG_DEFAULT, LVL_DEBUG, "ipc_test_write_srv");
if (!async_data_write_receive(&call, &size)) {
async_answer_0(icall, EREFUSED);
log_msg(LOG_DEFAULT, LVL_ERROR, "data_write_receive failed");
return;
}
if (size > rw_buf_size) {
async_answer_0(&call, EINVAL);
async_answer_0(icall, EINVAL);
log_msg(LOG_DEFAULT, LVL_ERROR, "Invalid write size.");
return;
}
rc = async_data_write_finalize(&call, rw_buf, size);
if (rc != EOK) {
log_msg(LOG_DEFAULT, LVL_ERROR,
"data_write_finalize failed");
async_answer_0(icall, EINVAL);
return;
}
async_answer_0(icall, EOK);
}
static void ipc_test_connection(ipc_call_t *icall, void *arg)
{
async_accept_0(icall);
while (true) {
ipc_call_t call;
async_get_call(&call);
if (!ipc_get_imethod(&call)) {
async_answer_0(&call, EOK);
break;
}
switch (ipc_get_imethod(&call)) {
case IPC_TEST_PING:
async_answer_0(&call, EOK);
break;
case IPC_TEST_GET_RO_AREA_SIZE:
ipc_test_get_ro_area_size_srv(&call);
break;
case IPC_TEST_GET_RW_AREA_SIZE:
ipc_test_get_rw_area_size_srv(&call);
break;
case IPC_TEST_SHARE_IN_RO:
ipc_test_share_in_ro_srv(&call);
break;
case IPC_TEST_SHARE_IN_RW:
ipc_test_share_in_rw_srv(&call);
break;
case IPC_TEST_SET_RW_BUF_SIZE:
ipc_test_set_rw_buf_size_srv(&call);
break;
case IPC_TEST_READ:
ipc_test_read_srv(&call);
break;
case IPC_TEST_WRITE:
ipc_test_write_srv(&call);
break;
default:
async_answer_0(&call, ENOTSUP);
break;
}
}
}
int main(int argc, char *argv[])
{
errno_t rc;
loc_srv_t *srv;
printf("%s: IPC test service\n", NAME);
async_set_fallback_port_handler(ipc_test_connection, NULL);
rc = log_init(NAME);
if (rc != EOK) {
printf(NAME ": Failed to initialize log.\n");
return rc;
}
rc = loc_server_register(NAME, &srv);
if (rc != EOK) {
log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering server. (%s)\n",
str_error(rc));
return rc;
}
rc = loc_service_register(srv, SERVICE_NAME_IPC_TEST, &svc_id);
if (rc != EOK) {
loc_server_unregister(srv);
log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering service. (%s)\n",
str_error(rc));
return rc;
}
printf("%s: Accepting connections\n", NAME);
task_retval(0);
async_manager();
return 0;
}
HelenOS homepage, sources at GitHub