
ChildProcess C++ library
v1.0.7
Table of contents
- Overview
- Versions
- Library files
- ChildProcess class description
- Build and connect to your project
- Example
Overview
ChildProcess C++ library provides an interface to run external processes from your application in Linux. The library runs an external process (child process) and returns control to you. The library doesn’t show the child process output, it just runs. The library also provides a function to stop the child process (the process will be terminated automatically in the ChildProcess class destructor). The library doesn’t have third-party dependencies and is built with the C++17 standard.
ChildProcess is general-purpose software: it is not tied to any sector or application, and the developer of a program decides what the program does with it.
Versions
Table 1 - Library versions.
| Version | Release date | What’s new |
|---|---|---|
| 1.0.0 | 16.08.2023 | - First version |
| 1.0.1 | 16.01.2024 | - Examples updated. - Documentation updated. |
| 1.0.2 | 16.04.2024 | - Documentation updated. |
| 1.0.3 | 17.05.2024 | - Documentation updated. |
| 1.0.4 | 17.07.2024 | - CMake updated. |
| 1.0.5 | 04.12.2024 | - Fix process kill procedure. |
| 1.0.6 | 04.12.2024 | - Fix close method. |
| 1.0.7 | 15.10.2026 | - Child process is created with posix_spawnp: no code of the application runs in the child process. - run() method returns FALSE if the program can not be started, if the command or the params contain a null character or if the previous child process is still running. - close() method waits up to 1 second for the child process to exit after SIGTERM. - Copy and move of ChildProcess objects are deleted. - Compiler warnings and security hardening flags added to CMake files. - Documentation updated. |
Library files
The library is supplied only by source code. The user is given a set of files in the form of a CMake project (repository). The repository structure is shown below:
CMakeLists.txt ---------------- Main CMake file of the library.
src --------------------------- Folder with library source code.
CMakeLists.txt ------------ CMake file of the library.
ChildProcess.cpp ---------- C++ implementation file.
ChildProcess.h ------------ Library main header file.
ChildProcessVersion.h ----- Header file with library version.
ChildProcessVersion.h.in -- CMake service file to generate version header.
example ----------------------- Folder of example application.
CMakeLists.txt ------------ CMake file of example application.
main.cpp ------------------ Source C++ file of example application.
static ------------------------ Folder with images for the documentation.
ChildProcess class description
ChildProcess class declaration
ChildProcess class declared in ChildProcess.h file. An object controls one child process at a time. The object can not be copied or moved: only the object that created the child process terminates it. The class is not thread-safe: an object must not be used from several threads at the same time without external synchronization. Different objects can be used from different threads. Class declaration:
namespace rapidpixel
{
/// Child process class.
class ChildProcess
{
public:
/// Get library version.
static std::string getVersion();
/// Class constructor.
ChildProcess();
/// Class destructor. Terminates the child process if it is running.
~ChildProcess();
/// Copy and move are deleted.
ChildProcess(const ChildProcess&) = delete;
ChildProcess(ChildProcess&&) = delete;
ChildProcess& operator=(const ChildProcess&) = delete;
ChildProcess& operator=(ChildProcess&&) = delete;
/// Run a program in a child process.
bool run(std::string command, std::string params = "");
/// Terminate the child process.
void close();
};
}
getVersion method
The getVersion() method returns string of current version of ChildProcess class. Method declaration:
static std::string getVersion();
Method can be used without ChildProcess class instance:
std::cout << "ChildProcess class version: "
<< rapidpixel::ChildProcess::getVersion();
Console output:
ChildProcess class version: 1.0.7
run method
The run() method runs a program in a new process (child process) and returns without waiting for the program to finish. If the command does not contain a slash, the program is searched in the directories of the PATH environment variable. The program must be an executable file (a script must start with the interpreter line #!). The program gets the command as the first argument (argv[0]) and the params split at space characters as the next arguments: a sequence of spaces is one separator, quotes and backslashes have no special meaning and no shell is used. For example, run(“mkdir”, “-p dir1 dir2”) runs the program mkdir with the arguments mkdir, -p, dir1 and dir2. The child process inherits the environment, the standard input and the file descriptors that are not marked close-on-exec (open the files of your application with the O_CLOEXEC flag if the child process must not get them). The standard output and the standard error of the child process are redirected to /dev/null. An object controls one child process: if the child process created by the previous call is still running, call the close() method before the next run(). Method declaration:
bool run(std::string command, std::string params = "");
| Parameter | Value |
|---|---|
| command | Name or path of the program to run (for example: mkdir or /usr/bin/myProgram). Must not be empty and must not contain null characters. The library runs any program, so the caller must pass a trusted value. |
| params | Optional arguments of the program separated by spaces (for example: -l, newDirectory or myProgramInput.txt). Must not contain null characters. |
Returns: TRUE if the child process is created or FALSE if not: the command is empty, the command or the params contain a null character, the arguments are too long, the child process created by the previous call is still running, or the C library reports that the program can not be started (for example, it is not found or it is not executable). The C library may not report these errors (for example, under qemu-user or valgrind): then the method returns TRUE and the child process exits with code 127.
close method
The close() method terminates the child process created by the run() method. When the library runs a child process, the library remembers the PID of the child process. The method sends SIGTERM to the child process and waits up to 1 second for it to exit. If the child process is still running after that (for example, it ignores SIGTERM), the method sends SIGKILL and waits for it to exit. The exit status of the child process is collected, so no zombie process is left. If the child process can not be signalled (for example, it runs with another user ID), the method does not wait for it. The processes started by the child process do not get the signals. The method does nothing if there is no child process. The class destructor calls this method. Your application must not collect the exit status of the child process by itself (waitpid() for any child process, SIGCHLD set to SIG_IGN): the PID can be given to another process then, and this process would get the signals. Method declaration:
void close();
Build and connect to your project
Build stand alone
The repository does not contain sub-repositories and the library does not depend on other libraries. The library is supported only on Linux: on other systems CMake stops with an error. Typical commands to build the library and the example:
cd ChildProcess
mkdir build
cd build
cmake ..
make
CMake options
Table 2 - CMake options of the repository. In the options below ${PARENT} is the prefix defined by the top-level project (empty if ChildProcess is the top-level project).
| Option | Default | Description |
|---|---|---|
| ${PARENT}_CHILD_PROCESS | ON | Build the library. |
| ${PARENT}_CHILD_PROCESS_EXAMPLE | ON (stand alone), OFF (included in other project) | Build the example. |
| ${PARENT}_SUBMODULE_CACHE_OVERWRITE | ON | Forced rewriting of the options above in the CMake cache: while it is ON, the options get the default values even if other values are given on the command line. The top-level project sets it to OFF when it includes ChildProcess as a sub-repository. |
| CMAKE_BUILD_TYPE | Release (if not specified) | Build type. |
Connect to your project
If you want to connect ChildProcess library to your CMake project as source code you can do the following. For example, if your repository has structure:
CMakeLists.txt
src
CMakeLists.txt
yourLib.h
yourLib.cpp
Create folder 3rdparty in your repository and copy the folder of the library ChildProcess there (for example from the folder modules of the RapidPixel SDK). The new structure of your repository:
CMakeLists.txt
src
CMakeLists.txt
yourLib.h
yourLib.cpp
3rdparty
ChildProcess
Create a CMakeLists.txt file in the 3rdparty folder. CMakeLists.txt should contain:
cmake_minimum_required(VERSION 3.13)
################################################################################
## 3RD-PARTY
## dependencies for the project
################################################################################
project(3rdparty LANGUAGES CXX)
################################################################################
## SETTINGS
## basic 3rd-party settings before use
################################################################################
# To inherit the top-level architecture when the project is used as a submodule.
SET(PARENT ${PARENT}_YOUR_PROJECT_3RDPARTY)
# Disable self-overwriting of parameters inside included subdirectories.
SET(${PARENT}_SUBMODULE_CACHE_OVERWRITE OFF CACHE BOOL "" FORCE)
################################################################################
## CONFIGURATION
## 3rd-party submodules configuration
################################################################################
SET(${PARENT}_SUBMODULE_CHILD_PROCESS ON CACHE BOOL "" FORCE)
if (${PARENT}_SUBMODULE_CHILD_PROCESS)
SET(${PARENT}_CHILD_PROCESS ON CACHE BOOL "" FORCE)
SET(${PARENT}_CHILD_PROCESS_EXAMPLE OFF CACHE BOOL "" FORCE)
endif()
################################################################################
## INCLUDING SUBDIRECTORIES
## Adding subdirectories according to the 3rd-party configuration
################################################################################
if (${PARENT}_SUBMODULE_CHILD_PROCESS)
add_subdirectory(ChildProcess)
endif()
File 3rdparty/CMakeLists.txt adds the ChildProcess folder to your project and excludes the example application from compiling (by default, the example is excluded from compiling if ChildProcess is included as a sub-repository). The new structure of the repository:
CMakeLists.txt
src
CMakeLists.txt
yourLib.h
yourLib.cpp
3rdparty
CMakeLists.txt
ChildProcess
Next you need to include the 3rdparty folder in the main CMakeLists.txt file of your repository. Add this line at the end of your main CMakeLists.txt:
add_subdirectory(3rdparty)
Next you have to include ChildProcess library in your src/CMakeLists.txt file:
target_link_libraries(${PROJECT_NAME} ChildProcess)
Done!
Example
Example application reads a command and its arguments (one line each), runs the command in a child process and after 2 seconds terminates the child process.
#include <chrono>
#include <iostream>
#include <string>
#include <thread>
#include "ChildProcess.h"
int main()
{
std::cout << "ChildProcess v" << rapidpixel::ChildProcess::getVersion()
<< " example" << std::endl << std::endl;
// Read the command (one line, must not be empty).
std::string command{};
std::cout << "Enter command: ";
if (!std::getline(std::cin, command) || command.empty())
{
std::cout << "No command entered" << std::endl;
return -1;
}
// Read the arguments separated by spaces (one line, may be empty).
std::string arguments{};
std::cout << "Enter arguments: ";
if (!std::getline(std::cin, arguments))
{
arguments.clear();
}
// Run child process.
rapidpixel::ChildProcess process;
if (!process.run(command, arguments))
{
std::cout << "Can't run the command" << std::endl;
return -1;
}
// Wait 2 seconds for example.
std::this_thread::sleep_for(std::chrono::seconds(2));
// Close child process.
process.close();
return 0;
}