[] [] [] []
Next: UBS Event Modules Up: Writing UBS Modules Previous: Writing UBS Modules   Contents

UBS Daemons

The UBS only imposes a few rules on what it considers to be a valid daemon. The module name must be prefixed with a "ubs-" (which is how the shell finds all the program modules it should launch on startup), and must initialize itself with the functions provided in UBS library. A sample daemon might look something like this:
/* ubs-sample.c - A sample UBS daemon. */

/* Needed for most UBS functions */
#include "ubs.h"

int main(int argc, char *argv[]) {
 /* Need to initialize the global table.  If any local
    configuration tables are used, they must also be
    initialized here. */
 ubs_table_init(&GLOBAL);

 /* Define the program name, and set up default global
    variables. */
 ubs_init(argv[0]);

 /* Read the ubs.conf file into the global table. */
 read_config(DEF_CONFIG, "global", &GLOBAL);

 /* See if this daemon is already running, and exit if
    it is, as not to spawn two copies of it. */
 if(check_running() == FAIL) {
  return FAIL;
 }

 /* Steps needed for proper daemonization. */
 if(fork()) {
  exit(0);
 }
 setsid();
 chdir(ubs_table_data(&GLOBAL, "prefix"));
 umask(0);

 while(1) {
  /* Main algorithm goes here. */
 }

 return OK;
}
To build this program, compile by running:
gcc -o ubs-sample -IPREFIX/include ubs-sample.c PREFIX/lib/libubs.a
where PREFIX is the installation path of the UBS. This will build a program called ubs-sample, which can be copied to PREFIX/bin, where it will be launched with all the other UBS modules on program startup.
The above program simply initializes itself, reads the UBS configuration file, and exits. The steps taken by this program are as follows:
  1. Initialize all UBS table structures. This must be done before writing any data to any ubs_table. If the program has additional variable tables (for reading its own contexts in the ubs.conf file), these tables must also be initialized here.
  2. Call ubs_init with the program name, which is stored in argv[0]. This sets the global table up with the default set of variables (in case any variables are left undefined in the ubs.conf file), as well as binds a number of signals and initializes global variables in the UBS.
  3. Read in the configuration files. This is done by calling read_config, which will store all data in the table referenced in its third argument. To access this data, the ubs_table_data function may be used. For more information about this, see the programmer's API reference.
  4. Call check_running to see if a module by this name is already running. If so, it is probably a bad idea to launch another one.
  5. To be a "proper" unix daemon, a few steps must be done to launch the process into the background. By forking, and killing the parent, the child process is left in the background, and the user is returned to the command prompt. After forking off, the child process (now the UBS main process) must call setsid to be the process leader, and chdir to the UBS root directory. Setting the umask to 000 is also a good practice. At this point, UBS initialization is complete, and the daemon can run uninterrupted.


[] [] [] []
Next: UBS Event Modules Up: Writing UBS Modules Previous: Writing UBS Modules   Contents
2003-10-30