$darkmode
Elektra 0.11.0
|
Source for the pluginprocess library. More...
#include "kdbpluginprocess.h"
#include <kdberrors.h>
#include <kdbinvoke.h>
#include <kdblogger.h>
#include <kdbprivate.h>
#include <errno.h>
#include <limits.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
Functions | |
void | elektraPluginProcessStart (Plugin *handle, ElektraPluginProcess *pp) |
Start the child process' command loop. More... | |
int | elektraPluginProcessSend (const ElektraPluginProcess *pp, pluginprocess_t command, KeySet *originalKeySet, Key *key) |
Call a plugin's function in a child process. More... | |
int | elektraPluginProcessIsParent (const ElektraPluginProcess *pp) |
Check if a given plugin process is the parent or the child process. More... | |
ElektraPluginProcess * | elektraPluginProcessInit (Key *errorKey) |
Initialize a plugin to be executed in its own process. More... | |
int | elektraPluginProcessOpen (ElektraPluginProcess *pp, Key *errorKey) |
Call a plugin's open function in a child process. More... | |
ElektraPluginProcessCloseResult | elektraPluginProcessClose (ElektraPluginProcess *pp, Key *errorKey) |
Cleanup a plugin process. More... | |
void | elektraPluginProcessSetData (ElektraPluginProcess *pp, void *data) |
Store a pointer to any plugin related data that is being executed inside an own process. More... | |
void * | elektraPluginProcessGetData (const ElektraPluginProcess *pp) |
Get a pointer to any plugin related data stored before. More... | |
Source for the pluginprocess library.
Executes plugins in a separate process via fork and uses a simple communication protocol based on the dump plugin via named pipes.
The communication protocol works as follows, where Child and Parent stand for the child and the parent process: 1) Four pipes are created to handle the communication in a reliable way
ElektraPluginProcessCloseResult elektraPluginProcessClose | ( | ElektraPluginProcess * | pp, |
Key * | errorKey | ||
) |
Cleanup a plugin process.
This will decrease the internal counter how often open/close has been called, closing opened pipes when the counter reaches 0. This will not delete the plugin data associated with the handle as it may contain other data out of the scope of this library, so this has to be done manually like
Note that pp might be null here if the initialization failed!
pp | the data structure containing the plugin's process information |
1 | if the data structure got cleaned up |
0 | if the data structure is still used |
void* elektraPluginProcessGetData | ( | const ElektraPluginProcess * | pp | ) |
Get a pointer to any plugin related data stored before.
If elektraPluginProcessSetData was not called earlier, NULL will be returned.
pp | the data structure containing the plugin's process information |
a | pointer to the data |
ElektraPluginProcess* elektraPluginProcessInit | ( | Key * | errorKey | ) |
Initialize a plugin to be executed in its own process.
This will prepare all the required resources and then fork the current process. After the initialization the child process will typically call the command loop while the parent starts to send commands to it. Also the resulting process information has to be stored for the plugin. In order to allow users to handle custom plugin data this will not automatically call elektraPluginSetData.
Typically called in a plugin's open function like (assuming no custom plugin data):
handle | the plugin's handle |
errorKey | a key where error messages will be set |
NULL | if the initialization failed |
a | pointer to the information |
int elektraPluginProcessIsParent | ( | const ElektraPluginProcess * | pp | ) |
Check if a given plugin process is the parent or the child process.
pp | the data structure containing the plugin's process information |
0 | if it's the child process |
the | child process' pid otherwise |
int elektraPluginProcessOpen | ( | ElektraPluginProcess * | pp, |
Key * | errorKey | ||
) |
Call a plugin's open function in a child process.
This will increase the internal counter how often open/close has been called, so the opened pipes and forks will not be closed too early.
pp | the data structure containing the plugin's process information |
errorKey | a key where error messages will be set |
the | return value of the plugin's open function |
int elektraPluginProcessSend | ( | const ElektraPluginProcess * | pp, |
pluginprocess_t | command, | ||
KeySet * | originalKeySet, | ||
Key * | key | ||
) |
Call a plugin's function in a child process.
This will wrap all the required information to execute the given command in a keyset and send it over to the child process. Then it waits for the child process's answer and copies the result back into the original plugin keyset and plugin key.
Typically called like
pp | the data structure containing the plugin's process information |
command | the plugin command that should be executed, e.g. ELEKTRA_PLUGINPROCESS_GET |
originalKeySet | the original key set that the parent process receives |
key | the original key the parent process receives |
ELEKTRA_PLUGIN_STATUS_ERROR | if the child process communication failed |
the | called plugin's return value otherwise |
void elektraPluginProcessSetData | ( | ElektraPluginProcess * | pp, |
void * | data | ||
) |
Store a pointer to any plugin related data that is being executed inside an own process.
This is required in case additional arbitrary plugin data should be stored. Pluginprocess has to be stored using elektraPluginSetData. Plugin data for the child process has to be stored using this function like
Furthermore ensure to cleanup the data after the plugin is done like
This way you can use elektraPluginProcessGetData (handle) in your child process to get the data you want your plugin to work with.
pp | the data structure containing the plugin's process information |
data | the pointer to the data |
void elektraPluginProcessStart | ( | Plugin * | handle, |
ElektraPluginProcess * | pp | ||
) |
Start the child process' command loop.
This will make the child process wait for plugin commands and execute them, returning the result to the parent. This is typically called in a plugin's open function.
handle | the plugin's handle |
pp | the data structure containing the plugin's process information |