Background Operation

 

Background Operation #

A small set of C functions manages RPCL background operation. Actually this set of functions is independent from RPCL, it works with any application communicating through the standard stdin/stdout/stderr file descriptors.

  • All functions require root permissions (geteuid() == 0 is true).
  • The named pipes, .pid-files and necessary lock files are managed in /run (where /var/run symbolically links to).

Backend Functions #

CTLDaemonize() #

This function “daemonizes” the current application and registers it with the specified identifier.

  • If there is already a background process running with the same identifier, an error is signalled.
  • This function needs to be called before any memory mapping or additional threads are started.
  • Internally daemon() is called with the request to change directory and leaving the redirected standard file descriptors open.
void CTLDaemonize(char* identifier);

Frontend Functions #

CTLFrontend() #

  • This connects to a running backend with basic line editing.
  • Only one session can be active at one time.
  • There’s an inactivity timeout of 10 minutes.
  • On start a newline (empty line) is sent to the backend for synchronization (which leads to the initial ok prompt when connected to an RPCL interpreter instance).
  • The frontend session can be terminated with EOF (^D) or ^C.
void CTLFrontend(char* identifier);

CTLStatus() #

void CTLStatus(char* identifier);

CTLStop() #

void CTLStop(char* identifier);

An Example: ctltest #

This is how a RPCL interpreter may be instantiated to run in background with the identifier ctltest:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>
#include <rpcl.h>
#include <ctl.h>

int main(int argc, char** argv) {

  RPCL* rpcl;

  CTLDaemonize("ctltest");

  rpcl = RPCLCreate(argc, argv);
  RPCLMainLoop(rpcl);
  RPCLDestroy(rpcl);
  exit(EXIT_SUCCESS);
}

The control Frontend #

The control program makes the CTL frontend functions available like this:

$ ./control -h
usage: control <identifier>          control background process
       control <identifier> status   show background process status
       control <identifier> stop     terminate background process
       control -h|--help             show usage
       control -v|--version          show version
       control -c|--copyright        show copyright
$ ▂

This is an example dialog:

$ sudo ./control ctltest status
ctltest not running
$ sudo ./ctltest
ctltest going to background ...
$ sudo ./control ctltest status
ctltest PID 7098 running
$ sudo ./control ctltest
connected to ctltest PID 7098
ok .d
version.rpcl                     1.58
ok