Managing processes (high-level API task.h)

The API is defined in the header file sysroot-*-kos/include/kos/task.h from the KasperskyOS SDK.

The API lets you create, start, and terminate processes, and statically create IPC channels and transfer handles.

Information about API functions is provided in the table below.

The libkos library also provides a low-level API for process management. The low-level API should be used only if the high-level API does not have sufficient capabilities.

Creating a process

To create a process, call one of the following functions:

With the exception of KosTaskCreate(), these functions receive the parameters of the created process via the input parameter params. This same input parameter is available in the KosTaskSetup() function, which lets you define the parameters of a process that was created by calling the KosTaskCreate() function. Use the params parameter to pass the structure containing the following fields:

When setting process parameters via the input parameter params of the KosTaskSetup(), KosTaskInit*(), and KosTaskLaunch() functions, you must take into account the following details:

When the KosTaskInit() or KosTaskLaunch() function is called, one of the following values is used as the process name and executable file name:

Likewise, these values are applied as the process name and/or executable file path if you call the KosTaskInitEx() or KosTaskInitFromSegEx() function with the RTL_NULL value in the name parameter and/or the path parameter. These values are also applied as the process name if you call the KosTaskInitFromSeg() function with the RTL_NULL value in the name parameter.

The KosTaskCreate() and KosTaskInit*() functions use the outTask output parameter to pass the address of the object describing the child process. This object is a structure that is created in the memory of the parent process and the child process. The developer of a solution does not need to perform operations with the fields of this structure. However, the pointer to this structure must be used as the process ID when calling API functions. A child process can get the address of the object describing it by calling the KosTaskGetSelf() function.

If statically created IPC channels are used to access endpoints provided by a server process, the object describing this server process must be linked to the structures containing information about endpoints from the formal specification of the solution component. This is necessary so that client processes can receive information about endpoints provided by the server process when creating a static IPC channel. To link an object describing a child server process to structures containing information about endpoints from the formal specification of the solution component, this information must be passed through the componentTree field of the params parameter when calling the KosTaskCreate(), KosTaskInit*(), and KosTaskLaunch() functions. A server process that is already running can link the object describing it to structures containing information about endpoints from the formal specification of the solution component by calling the KosTaskSetComponentTree() function. This is required if the running server process does not have a parent process.

When the KosTaskInitEx(), KosTaskInit() or KosTaskLaunch() function is called, the ELF image from the defined executable file in ROMFS is loaded into the memory of the created process. If the ELF image contains the symbol table .symtab and string table .strtab, they are loaded into process memory. Using these tables, the kernel receives the names of functions for generating stack backtrace data (call stack information).

To get information about the ELF image loaded into process memory, call the KosTaskGetElfSegs() function. This function is applicable only to those processes that were created with the specified KOS_TASK_FLAG_DUMPABLE flag. When using this function, the symbol table .symtab and string table .strtab are considered to be loadable ELF image segments along with segments of the LOAD type.

In contrast to the KosTaskInit*() and KosTaskLaunch() functions, the KosTaskCreate() function creates an "empty" process, which is a process in whose memory no program ELF image has been loaded. To load ELF image segments into process memory, call the KosTaskLoadSegments() function. To use the KosTaskLoadSegments() function and the KosTaskInitFromSegEx() function, MDL buffers containing loadable ELF image segments must be created in advance. Loadable ELF image segments must be defined via the segs parameter. In addition, the symbol table .symtab and the string table .strtab are also considered to be loadable segments along with segments of the LOAD type. You must use the entry and relocBase parameters to define the program entry point and the ELF image load offset, respectively. The program entry point is the sum of the address specified in the e_entry field of the ELF image header and the ELF image load offset. (For an ELF file of the DYN type, the ELF image load offset must be a random value for ASLR support.) To get the ELF image load offset, you can call the KnTaskVmReserveForElf() function from the task_api.h API, which reserves the virtual memory region for loading the ELF image. If the ELF file has the DYN type, the KnTaskVmReserveForElf() function uses the relocBase output parameter to pass a random offset, and it passes a zero offset for an ELF file of a different type. (The KosTaskInitEx(), KosTaskInit(), and KosTaskLaunch() functions also set a random offset for an ELF file of the DYN type and a zero offset for an ELF file of a different type.) Data passed to the KosTaskLoadSegments() and KosTaskInitFromSegEx() functions with the count, segs, and entry parameters, and parameters related to loading the symbol table .symtab and the string table .strtab are prepared by the KnElfCreateVmSegEx() function from the elf_api.h API.

The KosTaskInitFromSeg() function is a simplified version of the KosTaskInitFromSegEx() function and does not let you load the symbol table .symtab, string table .strtab into process memory, and does not let you define the ELF image load offset (instead, it sets a null offset).

The KosTaskLaunch() function creates and immediately starts a process without the capability to statically create IPC channels.

The KosTaskSetArgs() and KosTaskSetEnv() functions define the program startup parameters and environment variables, respectively. These functions are intended for use by the libkos library.

The KosTaskAddEndpoints() function registers the endpoints provided by the process. This function is intended for use by the libkos library.

Statically created IPC channels

Before starting processes, you can create IPC channels between them. You can create multiple IPC channels with different names between one client process and one server process. You can create IPC channels with the same name between one server process and multiple client processes.

To create an IPC channel with a name matching the name of a server process class, call the KosTaskConnect() function.

To create an IPC channel with a defined name, call the KosTaskConnectToService() function.

To use the created IPC channel, you must get the IPC handle on the client side and on the server process side by calling the SLStaticResolveHandle() function from the API sl-static.h. This function uses the channelName parameter to receive the name of the IPC channel.

Transferring handles to a child process

A parent process can transfer handles to a child process that is not yet running. (General information about transferring handles is provided in the Transferring handles section.)

To pass a handle to a child process, call the KosTaskTransferResource() function while specifying the handle of the resource transfer context object, the permissions mask, and the conditional name of the descendant of the transferred handle in addition to the other parameters.

To find the descendant of a handle transferred by a parent process, call the KosTaskLookupResource() function from the child process while specifying the conditional name of the descendant of the handle that was defined by the parent process when calling the KosTaskTransferResource() function.

Starting a process

To create and immediately start a process without statically creating IPC channels, call the KosTaskLaunch() function.

To start an already created process for which you can create the necessary IPC channels before starting this process, call the KosTaskRunEx() or KosTaskRun() function.

Use the fsBackend parameter of the KosTaskRunEx() function to specify whether to use the kernel or the system program FSUsr to support the ROMFS file system for the started process by default (for details, see Using the ROMFS file system (low-level API fs_api.h)). In a process started by a call of the KosTaskRun() or KosTaskLaunch() function, the default support of the ROMFS file system is the same as the default support for the parent process when the KosTaskRun() or KosTaskLaunch() is called. To get information about how the ROMFS file system is supported for a process, call the KosTaskGetSelfFSBackend() function from this process. (The KosTaskSetSelfFSBackend() function writes information regarding how the ROMFS file system is supported for the calling process to the object describing the process. This function is intended for use by the libkos library.)

The parent process can put the ROMFS file system in the user space and mount it so that it can then be used by the child process by default. To do so, the parent process calls the KnFsChangeBackend() function from the fs_api.h API to indicate that the ROMFS file system for the process is supported by the FSUsr program, and it calls the KnFsChange() function from the fs_api.h API to mount the required ROMFS image. Then the parent process starts the child process by calling the KosTaskRun(), KosTaskRunEx() or KosTaskLaunch() function. (When calling the KosTaskRunEx() function, you must explicitly specify that the ROMFS file system for the child process will be supported by the FSUsr program.) As a result, by default the child process will use the ROMFS file system that is put into the user space by the parent process.

If the parent process does not need to terminate the child process or wait for its termination, the object describing the child process must be deleted and the reference counter for it must be reset by using the KosTaskPut() function after the child process is started. The KosTaskLaunch() function calls the KosTaskPut() function after the child process is started.

Terminating a process

The API terminates and waits for termination of child processes.

To terminate a child process, call the KosTaskStop() or KosTaskStopAndWait() function.

To wait for a child process to terminate on its own initiative, call the KosTaskWait() function.

To ensure that the kernel object describing a child process is deleted after this process is terminated, its handle in the parent process must be closed before or after termination of the child process. The handle of the child process is closed when the object describing the child process is deleted from the memory of the parent process. To delete an object describing a child process, reset the reference counter for this object by using the KosTaskPut() function. The KosTaskLaunch() function calls the KosTaskPut() function after the child process is started.

For details about terminating processes, see Managing processes (low-level API task_api.h).

Getting a process handle

To get the handle of a process, call the KosTaskGetHandle() function.

The handle of a process can be transferred to another process via IPC.

The handle of a calling process that was received by calling the KosTaskGetHandle() function cannot be closed. If closed, this handle may be used to identify other resources, which will lead to errors. Descendants of this handle can be closed without a risk of similar errors occurring.

Getting the startup parameters and environment variables of a program

The KosTaskGetArgsList() function lets you get the startup parameters of a program. The KosTaskFreeArgsList() function lets you delete the array of addresses of program startup parameters that were obtained by calling the KosTaskGetArgsList() function.

The KosTaskGetEnvList() function lets you get the environment variables of a program. The KosTaskFreeEnvList() function lets you delete the array of addresses of program environment variables that were obtained by calling the KosTaskGetEnvList() function.

All these functions are intended for use by the libc library.

Information about API functions

task.h functions

Function

Information about the function

KosTaskCreate()

Purpose

Creates a process.

Parameters

  • [in] name – pointer to the process name.
  • [in] eiid – pointer to the process class name.
  • [in] path – pointer to the executable file path. This function does not use the specified path to query the executable file but instead only saves it as metadata of the created process.
  • [in] flags – flags defining the parameters for creating the process.
  • [out] outTask – pointer to the address of the object describing the process.

Returned values

If successful, the function returns rcOk, otherwise it returns an error code.

KosTaskSetup()

Purpose

Sets the parameters of a process.

Parameters

  • [in] task – pointer to the object that describes the process.
  • [in] params – pointer to the structure containing process parameters.

Returned values

If successful, the function returns rcOk, otherwise it returns an error code.

KosTaskLoadSegments()

Purpose

Loads ELF image segments into process memory.

Parameters

  • [in] task – pointer to the object that describes the process.
  • [in] count – number of loaded ELF image segments.
  • [in] relocBase – ELF image load offset (in bytes).
  • [in] entry – address of the program entry point.
  • [in,out] segs is a pointer to the array of structures describing loaded ELF image segments. The type of structure is defined in the header file sysroot-*-kos/include/coresrv/vmm/vmm_types.h from the KasperskyOS SDK. If the loadAddr field of this structure is set equal to 0, the load address of the segment will be selected automatically and written to the loadAddr field.
  • [in,optional] symTabIndex is the index of loaded the ELF image segment containing the symbol table .symtab. Enumeration starts with zero. The parameter value is ignored if at least one of the symTabSize and strTabSize parameters has a zero value.
  • [in,optional] symTabSize – size of the symbol table .symtab, or 0 if you do not need to load this table. If this parameter has a zero value, the strTabSize parameter must also have a zero value.
  • [in,optional] strTabIndex is the index of the loaded ELF image segment containing the string table .strtab. Enumeration starts with zero. The parameter value is ignored if at least one of the symTabSize and strTabSize parameters has a zero value.
  • [in,optional] strTabSize – size of the string table .strtab (in bytes), or 0 if you do not need to load this table. If this parameter has a zero value, the symTabSize parameter must also have a zero value.

Returned values

If successful, the function returns rcOk, otherwise it returns an error code.

KosTaskInitEx()

Purpose

Creates a process.

Parameters

  • [in] params – pointer to the structure containing process parameters.
  • [in,optional] name – pointer to the process name, or RTL_NULL to use the process class name, received via the params parameter, as the process name.
  • [in,optional] path – pointer to the name of the executable file in ROMFS, or RTL_NULL to use the process class name, received via the params parameter, as the executable file name.
  • [out] outTask – pointer to the address of the object describing the process.

Returned values

If successful, the function returns rcOk, otherwise it returns an error code.

KosTaskInit()

Purpose

Creates a process.

Parameters

  • [in] params – pointer to the structure containing process parameters.
  • [out] outTask – pointer to the address of the object describing the process.

Returned values

If successful, the function returns rcOk, otherwise it returns an error code.

KosTaskInitFromSegEx()

Purpose

Creates a process.

Parameters

  • [in] params – pointer to the structure containing process parameters.
  • [in,optional] name – pointer to the process name, or RTL_NULL to use the process class name, received via the params parameter, as the process name.
  • [in,optional] path – pointer to the executable file path or RTL_NULL to use the process class name, received via the params parameter, as the executable file path. This function does not use the specified path to query the executable file but instead only saves it as metadata of the created process.
  • [in] count – number of loaded ELF image segments.
  • [in] relocBase – ELF image load offset (in bytes).
  • [in] entry – address of the program entry point.
  • [in,out] segs is a pointer to the array of structures describing loaded ELF image segments. The type of structure is defined in the header file sysroot-*-kos/include/coresrv/vmm/vmm_types.h from the KasperskyOS SDK. If the loadAddr field of this structure is set equal to 0, the load address of the segment will be selected automatically and written to the loadAddr field.
  • [in,optional] symTabIndex is the index of loaded the ELF image segment containing the symbol table .symtab. Enumeration starts with zero. The parameter value is ignored if at least one of the symTabSize and strTabSize parameters has a zero value.
  • [in,optional] symTabSize – size of the symbol table .symtab, or 0 if you do not need to load this table. If this parameter has a zero value, the strTabSize parameter must also have a zero value.
  • [in,optional] strTabIndex is the index of the loaded ELF image segment containing the string table .strtab. Enumeration starts with zero. The parameter value is ignored if at least one of the symTabSize and strTabSize parameters has a zero value.
  • [in,optional] strTabSize – size of the string table .strtab (in bytes), or 0 if you do not need to load this table. If this parameter has a zero value, the symTabSize parameter must also have a zero value.
  • [out] outTask – pointer to the address of the object describing the process.

Returned values

If successful, the function returns rcOk, otherwise it returns an error code.

KosTaskInitFromSeg()

Purpose

Creates a process.

Parameters

  • [in] params – pointer to the structure containing process parameters.
  • [in,optional] name – pointer to the process name, or RTL_NULL to use the process class name, received via the params parameter, as the process name.
  • [in] count – number of loaded ELF image segments.
  • [in] entry – address of the program entry point.
  • [in,out] segs is a pointer to the array of structures describing loaded ELF image segments. The type of structure is defined in the header file sysroot-*-kos/include/coresrv/vmm/vmm_types.h from the KasperskyOS SDK. If the loadAddr field of this structure is set equal to 0, the load address of the segment will be selected automatically and written to the loadAddr field.
  • [out] outTask – pointer to the address of the object describing the process.

Returned values

If successful, the function returns rcOk, otherwise it returns an error code.

KosTaskGetElfSegs()

Purpose

Gets information about the ELF image loaded in process memory.

Parameters

  • [in] task – pointer to the object that describes the process.
  • [out] segsBuf is a pointer to the array used to save structures describing loaded ELF image segments. The type of structure is defined in the header file sysroot-*-kos/include/coresrv/vmm/vmm_types.h from the KasperskyOS SDK.
  • [in] segsBufSize is the size (in bytes) of the buffer used to store the array of structures that describe the loaded ELF image segments.
  • [out] segsCount – pointer to the number of loaded ELF image segments.
  • [out] symTabIndex is a pointer to the index of the loaded ELF image segment containing the symbol table .symtab.
  • [out] symTabSize – pointer to the size of the symbol table .symtab (in bytes).
  • [out] strTabIndex is a pointer to the index of the loaded ELF image segment containing the string table .strtab.
  • [out] strTabSize – pointer to the size of the string table .strtab (in bytes).

Returned values

If successful, the function returns rcOk, otherwise it returns an error code.

KosTaskConnect()

Purpose

Creates an IPC channel.

Parameters

  • [in] cl – pointer to the object that describes the client process.
  • [in] sr – pointer to the object that describes the server process.

Returned values

If successful, the function returns rcOk, otherwise it returns an error code.

KosTaskConnectToService()

Purpose

Creates an IPC channel with the defined name.

Parameters

  • [in] cl – pointer to the object that describes the client process.
  • [in] sr – pointer to the object that describes the server process.
  • [in] name – pointer to the name of the IPC channel.

Returned values

If successful, the function returns rcOk, otherwise it returns an error code.

KosTaskSetArgs()

Purpose

Defines the program startup parameters.

Parameters

  • [in] task – pointer to the object that describes the process.
  • [in] args – pointer to an array of addresses of program startup parameters. The RTL_NULL value must be the last element of the array.

Returned values

If successful, the function returns rcOk, otherwise it returns an error code.

KosTaskGetArgsList()

Purpose

Gets the program startup parameters.

Parameters

  • [in] task – pointer to the object that describes the process.
  • [out] args – pointer to the address of the array of program startup parameter addresses, or RTL_NULL if these parameters are not available.

Returned values

If successful, the function returns rcOk, otherwise it returns an error code.

KosTaskFreeArgsList()

Purpose

Deletes the array of addresses of program startup parameters that was received by calling the KosTaskGetArgsList().

Parameters

  • [in] args – pointer to an array of addresses of program startup parameters.

Returned values

N/A

KosTaskSetEnv()

Purpose

Defines environment variables of a program.

Parameters

  • [in] task – pointer to the object that describes the process.
  • [in] envs – pointer to the array of addresses of program environment variables. The RTL_NULL value must be the last element of the array.

Returned values

If successful, the function returns rcOk, otherwise it returns an error code.

KosTaskGetEnvList()

Purpose

Gets the environment variables of a program.

Parameters

  • [in] task – pointer to the object that describes the process.
  • [out] envs – pointer to the address of the array of program environment variable addresses, or RTL_NULL if these variables are not available.

Returned values

If successful, the function returns rcOk, otherwise it returns an error code.

KosTaskFreeEnvList()

Purpose

Deletes the array of program environment variable addresses that was received by calling the KosTaskGetEnvList() function.

Parameters

  • [in] envs – pointer to the array of addresses of program environment variables.

Returned values

N/A

KosTaskRunEx()

Purpose

Resumes a process.

Parameters

  • [in] task – pointer to the object that describes the process.
  • [in] fsBackend – parameter that specifies whether the ROMFS file system for the started process is supported by the kernel or by the system program FSUsr by default.

Returned values

If successful, the function returns rcOk, otherwise it returns an error code.

KosTaskRun()

Purpose

Resumes a process.

Parameters

  • [in] task – pointer to the object that describes the process.

Returned values

If successful, the function returns rcOk, otherwise it returns an error code.

KosTaskStop()

Purpose

Terminates a process.

Parameters

  • [in] task – pointer to the object that describes the process.

Returned values

If successful, the function returns rcOk, otherwise it returns an error code.

KosTaskStopAndWait()

Purpose

Terminates a process and waits for the termination of this process.

Parameters

  • [in] task – pointer to the object that describes the process.
  • [in] timeout – timeout (in milliseconds), or INFINITE_TIMEOUT to define an unlimited timeout. The constant INFINITE_TIMEOUT is defined in the header file sysroot-*-kos/include/rtl/rtc.h from the KasperskyOS SDK.

Returned values

If successful, the function returns rcOk, otherwise it returns an error code.

Additional information

Non-blocking call if the timeout parameter is set to 0.

KosTaskWait()

Purpose

Waits for the termination of a process.

Parameters

  • [in] task – pointer to the object that describes the process.
  • [in] timeout – timeout (in milliseconds), or INFINITE_TIMEOUT to define an unlimited timeout. The constant INFINITE_TIMEOUT is defined in the header file sysroot-*-kos/include/rtl/rtc.h from the KasperskyOS SDK.
  • [out] retcode – pointer to the process exit code.

Returned values

If successful, the function returns rcOk, otherwise it returns an error code.

Additional information

Non-blocking call if the timeout parameter is set to 0.

KosTaskGetHandle()

Purpose

Gets the process handle.

Parameters

  • [in] task – pointer to the object that describes the process.

Returned values

Process handle.

KosTaskLaunch()

Purpose

Creates and starts a process.

Parameters

  • [in] params – pointer to the structure containing process parameters.

Returned values

If successful, the function returns rcOk, otherwise it returns an error code.

KosTaskGetSelf()

Purpose

Gets the address of an object that describes a calling process.

Parameters

N/A

Returned values

Pointer to the object describing the process, or RTL_NULL if this object was deleted.

Additional information

The function adds 1 to the reference counter for the object describing the process, therefore the KosTaskPut() function must be called after this function is called.

KosTaskPut()

Purpose

Subtracts 1 from the reference counter for the object describing the process.

Parameters

  • [in] task – pointer to the object that describes the process.

Returned values

N/A

KosTaskAddEndpoints()

Purpose

Registers endpoints.

Parameters

  • [in,out] task – pointer to the object that describes the process.
  • [in] endpoints – pointer to the array of structures describing endpoints. The type of structure is defined in the header file sysroot-*-kos/include/services/handle/if_connection.h from the KasperskyOS SDK.
  • [in] count – number of endpoints.

Returned values

If successful, the function returns rcOk, otherwise it returns an error code.

KosTaskSetSelfFSBackend()

Purpose

Writes information about how ROMFS file system support is provided for the calling process (by the kernel or by the FSUsr system program) to the object describing the process.

Parameters

  • [in] fsBackend – parameter that specifies whether the ROMFS file system is supported by the kernel or by the system program FSUsr.

Returned values

N/A

KosTaskGetSelfFSBackend()

Purpose

Gets information indicating whether the ROMFS file system for a calling process is supported by the kernel or by the system program FSUsr.

Parameters

N/A

Returned values

Value indicating whether the ROMFS file system is supported by the kernel or by the system program FSUsr.

KosTaskSetComponentTree()

Purpose

Creates structures containing information about endpoints from the formal specification of the solution component and links these structures to the object describing the process.

Parameters

  • [in] task – pointer to the object that describes the process.
  • [in] root – pointer to the structure containing information from the formal specification of the solution component. The type of structure is defined in the header file sysroot-*-kos/include/nk/types.h from the KasperskyOS SDK. This structure is an element of automatically generated transport code.

Returned values

If successful, the function returns rcOk, otherwise it returns an error code.

KosTaskTransferResource()

Purpose

Transfers a handle to a process that is not yet running.

Parameters

  • [in] task – pointer to the object that describes the process.
  • [in] srcHandle – transferred handle.
  • [in] srcBadge – handle of the resource transfer context object.
  • [in] dstRights – permissions mask of the descendant of the transferred handle.
  • [in] handleName – pointer to the name of the descendant of the transferred handle.

Returned values

If successful, the function returns rcOk, otherwise it returns an error code.

KosTaskLookupResource()

Purpose

Searches for a handle based on the name defined by the KosTaskTransferResource() function.

Parameters

  • [in] handleName – pointer to the handle name.

Returned values

If successful, it returns the handle. Otherwise, it returns INVALID_HANDLE.

Page top