KasperskyOS API >
libkos library > Initializing IPC transport for interprocess communication and managing IPC request processing (transport-kos.h, transport-kos-dispatch.h)
Initializing IPC transport for interprocess communication and managing IPC request processing (transport-kos.h, transport-kos-dispatch.h)
APIs are defined in the header files transport-kos.h
and transport-kos-dispatch.h
from the KasperskyOS SDK that are located at the path sysroot-*-kos/include/coresrv/nk
.
API capabilities:
Information about API functions is provided in the tables below.
This section contains API usage examples. In these examples, programs acting as servers have the following formal specification:
FsDriver.edl
entity FsDriver
components {
operationsComp : Operations
}
Operations.cdl
component Operations
endpoints {
fileOperations : FileIface
}
FileIface.idl
package FileIface
interface {
Open(in array<UInt8, 1024> path);
Read(out sequence<UInt8, 2048> content);
}
Initializing IPC transport for interprocess communication
To initialize IPC transport for interaction with other processes, call the NkKosTransport_Init()
or NkKosTransportSync_Init()
function declared in the header file transport-kos.h
.
Example use of the NkKosTransport_Init()
function on the client side:
int main(int argc, const char *argv[])
{
NkKosTransport driver_transport;
struct FileIface_proxy file_operations_proxy;
struct FileIface_Open_req req;
struct FileIface_Open_res res;
Handle driver_handle;
rtl_uint32_t file_operations_riid;
if (KnCmConnect("FsDriver", "operationsComp.fileOperations", INFINITE_TIMEOUT,
&driver_handle, &file_operations_riid) == rcOk) {
NkKosTransport_Init(&driver_transport, driver_handle, NK_NULL, 0);
FileIface_proxy_init(&file_operations_proxy, &driver_transport.base,
(nk_iid_t) file_operations_riid);
}
...
strncpy(req.path, "/example/file/path", sizeof(req.path));
if (FileIface_Open(file_operations_proxy.base, &req, NULL,
&res, NULL) != NK_EOK) {
...
}
...
}
If a client needs to use several endpoints, the same number of proxy objects must be initialized. When initializing each proxy object, you need to specify the IPC transport that is associated through the client IPC handle with the relevant server. When initializing multiple proxy objects pertaining to the endpoints of one server, you can specify the same IPC transport that is associated with this server.
Example use of the NkKosTransport_Init()
function on the server side:
int main(int argc, const char *argv[])
{
...
NkKosTransport transport;
Handle handle;
char client[32];
char endpoint[32];
Retcode rc = KnCmListen(RTL_NULL, INFINITE_TIMEOUT, client, endpoint);
if (rc == rcOk)
rc = KnCmAccept(client, endpoint,
FsDriver_operationsComp_fileOperations_iid,
INVALID_HANDLE, &handle);
...
NkKosTransport_Init(&transport, handle, NK_NULL, 0);
...
do
{
...
rc = nk_transport_recv(&transport.base, ...);
if (rc == NK_EOK) {
rc = FsDriver_entity_dispatch(...);
if (rc == NK_EOK) {
rc = nk_transport_reply(&transport.base, ...);
}
}
}
while (rc == NK_EOK)
return EXIT_SUCCESS;
}
If a server processes IPC requests received through multiple IPC channels, the following special considerations should be taken into account:
- If a listener handle is associated with all IPC channels, IPC interaction with all clients can use the same IPC transport associated with this listener handle.
- If IPC channels are associated with different listener handles, IPC interaction with each group of clients corresponding to the same listener handle must use a separate IPC transport associated with this listener handle. In this case, IPC requests can be processed in parallel threads if you are using a thread-safe implementation of endpoint methods.
The NkKosTransportSync_Init()
function initializes IPC transport with support for interrupting the Call()
and Recv()
locking system calls. (For example, an interrupt of these calls may be required for correct termination of the process that is executing them.) To interrupt the Call()
and Recv()
system calls, use the API ipc_api.h.
The NkKosSetTransportTimeouts()
function declared in the header file transport-kos.h
defines the maximum lockout duration for Call()
and Recv()
system calls used for IPC transport.
Starting the IPC request processing loop
The IPC request processing loop on a server includes the following steps:
- Receive an IPC request.
- Process the IPC request.
- Send an IPC response.
Each step of this loop can be completed separately by sequentially calling the nk_transport_recv()
, dispatcher, and nk_transport_reply()
functions. (The nk_transport_recv()
and nk_transport_reply()
functions are declared in the header file sysroot-*-kos/include/nk/transport.h
from the KasperskyOS SDK.) You can also call the NkKosTransport_Dispatch()
or NkKosDoDispatch()
function in which this loop is completed in its entirety. (The NkKosTransport_Dispatch()
and NkKosDoDispatch()
functions are declared in the header files transport-kos.h
and transport-kos-dispatch.h
, respectively.) It is more convenient to use the NkKosDoDispatch()
function because it requires fewer preparatory operations (for example, you do not need to initialize IPC transport).
You can initialize the structure passed to the NkKosDoDispatch()
function through the info
parameter by using the macros defined in the header file transport-kos-dispatch.h
.
The NkKosTransport_Dispatch()
and NkKosDoDispatch()
functions can be called from parallel threads if you are using a thread-safe implementation of endpoint methods.
Example use of the NkKosDoDispatch()
function:
static nk_err_t Open_impl(...)
{
...
}
static nk_err_t Read_impl(...)
{
...
}
static struct FileIface *CreateFileOperations()
{
static const struct FileIface_ops ops = {
.Open = Open_impl,
.Read = Read_impl
};
static struct FileIface impl = {
.ops = &ops
};
return &impl;
}
int main(int argc, const char *argv[])
{
...
NkKosDispatchInfo info;
struct Operations_component component;
struct FsDriver_entity entity;
Handle handle = ServiceLocatorRegister("driver_connection", NULL, 0, &iid);
assert(handle != INVALID_HANDLE);
Operations_component_init(&component, CreateFileOperations());
FsDriver_entity_init(&entity, &component);
info = NK_TASK_DISPATCH_INFO_INITIALIZER(FsDriver, entity);
NkKosDoDispatch(handle, info);
return EXIT_SUCCESS;
}
Example use of the NkKosTransport_Dispatch()
function:
static nk_err_t Open_impl(...)
{
...
}
static nk_err_t Read_impl(...)
{
...
}
static struct FileIface *CreateFileOperations()
{
static const struct FileIface_ops ops = {
.Open = Open_impl,
.Read = Read_impl
};
static struct FileIface impl = {
.ops = &ops
};
return &impl;
}
int main(int argc, const char *argv[])
{
...
NkKosTransport transport;
struct Operations_component component;
struct FsDriver_entity entity;
union FsDriver_entity_req req;
union FsDriver_entity_res res;
char res_buffer[FsDriver_entity_res_arena_size];
struct nk_arena res_arena = NK_ARENA_INITIALIZER(res_buffer,
res_buffer + sizeof(res_buffer));
Handle handle = ServiceLocatorRegister("driver_connection", NULL, 0, &iid);
assert(handle != INVALID_HANDLE);
NkKosTransport_Init(&transport, handle, NK_NULL, 0);
Operations_component_init(&component, CreateFileOperations());
FsDriver_entity_init(&entity, &component);
NkKosTransport_Dispatch(&transport.base, FsDriver_entity_dispatch,
&entity, &req, sizeof(FsDriver_entity_req),
RTL_NULL, &res, &res_arena);
return EXIT_SUCCESS;
}
Copying data to the IPC message arena
To copy a string to the IPC message arena, call the NkKosCopyStringToArena()
function declared in the header file transport-kos.h
. This function reserves a segment of the arena and copies a string to this segment.
Example use of the NkKosCopyStringToArena()
function:
static nk_err_t Read_impl(struct FileIface *self,
const struct FileIface_Read_req *req,
const struct nk_arena* req_arena,
struct FileIface_Read_res* res,
struct nk_arena* res_arena)
{
if (NkKosCopyStringToArena(&res_arena, &res.content,
"CONTENT OF THE FILE") != rcOk) {
...
}
return NK_EOK;
}
Information about API functions
transport-kos.h functions
Function
|
Information about the function
|
NkKosTransport_Init()
|
Purpose
Initializes IPC transport.
Parameters
- [out]
transport – pointer to the structure containing the IPC transport parameters. - [in]
handle – client or server IPC handle. - [in]
view – parameter that must have the value NK_NULL . - [in]
size – parameter that must have the value 0 .
Returned values
N/A
|
NkKosTransportSync_Init()
|
Purpose
Initializes IPC transport with support for interrupting the Call() and/or Recv() system calls.
Parameters
- [out]
transport – pointer to the structure containing the IPC transport parameters. - [in]
handle – client or server IPC handle. - [in,optional]
callSyncHandle – handle of the IPC synchronization object for Call() system calls, or INVALID_HANDLE if an interrupt of Call() system calls is not required. - [in,optional]
recvSyncHandle – handle of the IPC synchronization object for Recv() system calls, or INVALID_HANDLE if an interrupt of Recv() system calls is not required.
Returned values
N/A
|
NkKosSetTransportTimeouts()
|
Purpose
Defines the maximum lockout duration for Call() and Recv() system calls used for IPC transport.
Parameters
- [out]
transport – pointer to the structure containing the IPC transport parameters. - [in]
recvTimeout – maximum lockout duration for Recv() system calls in milliseconds, or INFINITE_TIMEOUT to define an unlimited lockout duration. - [in]
callTimeout – maximum lockout duration for Call() system calls in milliseconds, or INFINITE_TIMEOUT to define an unlimited lockout duration.
Returned values
N/A
|
NkKosTransport_Dispatch()
|
Purpose
Starts the IPC request processing loop.
Parameters
- [in]
transport – pointer to the base field of the structure containing the IPC transport parameters. - [in]
dispatch – pointer to the dispatcher (dispatch method) from the transport code. The dispatcher is named <process class name>_entity_dispatch . - [in]
impl – pointer to the stub, which consists of a structure with the type <process class name>_entity from the transport code. The dispatcher uses this structure to get the pointers to functions implementing endpoint methods. - [out]
req – pointer to the union with the type <process class name>_entity_req from the transport code. This union is intended for storing the constant part of IPC requests for any methods of endpoints provided by the server. - [in]
req_size – maximum size (in bytes) of the constant part of IPC requests. It is defined as sizeof(<process class name>_entity_req) , where <process class name>_entity_req is the type from the transport code. - [in,out,optional]
req_arena – pointer to the IPC request arena descriptor, or RTL_NULL if an IPC request arena is not in use. The type of handle is defined in the header file sysroot-*-kos/include/nk/arena.h from the KasperskyOS SDK. - [out]
res – pointer to the union with the type <process class name>_entity_res from the transport code. This union is intended for storing the constant part of IPC responses for any methods of endpoints provided by the server. - [in,out,optional]
res_arena – pointer to the IPC response arena descriptor, or RTL_NULL if an IPC response arena is not in use. The type of handle is defined in the header file sysroot-*-kos/include/nk/arena.h from the KasperskyOS SDK.
Returned values
If unsuccessful, it returns an error code.
|
NkKosCopyStringToArena()
|
Purpose
Reserves a segment of the arena and copies a string to this segment.
Parameters
- [in,out]
arena – pointer to the arena descriptor. The type of handle is defined in the header file sysroot-*-kos/include/nk/arena.h from the KasperskyOS SDK. - [out]
field – pointer to the arena chunk descriptor where the string is copied. The type of handle is defined in the header file sysroot-*-kos/include/nk/types.h . - [in]
src – pointer to the string to be copied to the IPC message arena.
Returned values
If successful, the function returns rcOk , otherwise it returns an error code.
|
transport-kos-dispatch.h functions
Function
|
Information about the function
|
NkKosDoDispatch()
|
Purpose
Starts the IPC request processing loop.
Parameters
- [in]
h – server IPC handle. - [in]
info – pointer to the structure containing the data required by the function to use transport code (including the names of types, sizes of the constant part and IPC message arena).
Returned values
N/A
|
Article ID: libkos_ipc_transport_api, Last review: May 21, 2024