DTS Application Library  0.2.3
Application library containing referenced objects and interfaces to common libraries
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
Distrotech Application Library

A Collection of helper functions and wrapped up interfaces to other libraries. More...

Modules

 Referenced Lockable Objects
 Utilities for managing referenced lockable objects.
 
 Posix thread interface
 Functions for starting and managing threads.
 
 Network socket interface
 Allocate and initialise a socket for use as a client or server.
 
 Linux network interface functions
 Implement various interface routines from libnetlink.
 
 INI Style config file Interface
 Reads a ini config file into grouped hashed buckets.
 
 Radius client interface
 Simple implementation of experimental radius client.
 
 Micelaneous utilities.
 Utilities commonly used.
 
 IPv4 and IPv6 functions
 Helper functions for various calculations.
 
 File utility functions
 Convinece wrappers arround stat.
 
 Openldap/SASL Interface
 Functions to interface with a LDAP server.
 
 XML Interface
 Utilities for managing XML documents.
 
 CURL Url interface.
 Interface to libCURL.
 
 Zlib Interface
 Simplified implementation of zlib functions.
 
 Burtle Bob hash algorythim.
 lookup3.c, by Bob Jenkins, May 2006, Public Domain (Original Documentation)
 
 IPv6 Nat Mapping
 Implementation of RFC6296.
 
 Windows Support
 Support for building with mingw32 (Requires XP SP1+)
 

Files

file  dtsapp.h
 DTS Application library API Include file.
 
file  main.c
 Application framework.
 

Macros

#define FRAMEWORK_MAIN(progname, name, email, www, year, runfile, flags, sighfunc)
 A macro to replace main() with initilization and daemonization code. More...
 
#define ALLOC_CONST(const_var, val)
 Macro to assign values to char const. More...
 

Typedefs

typedef int(* frameworkfunc )(int, char **)
 Framework callback function. More...
 
typedef void(* syssighandler )(int, siginfo_t *, void *)
 Callback to user supplied signal handler. More...
 

Enumerations

enum  framework_flags { FRAMEWORK_FLAG_DAEMON = 1 << 0, FRAMEWORK_FLAG_NOGNU = 1 << 1, FRAMEWORK_FLAG_DAEMONLOCK = 1 << 2 }
 Application control flags. More...
 

Functions

void printgnu (const char *pname, int year, const char *dev, const char *email, const char *www)
 Print a brief GNU copyright notice on console. More...
 
void daemonize ()
 Daemonise the application using fork/exit. More...
 
int lockpidfile (const char *runfile)
 Lock the run file in the framework application info. More...
 
void framework_mkcore (char *progname, char *name, char *email, char *web, int year, char *runfile, int flags, syssighandler sigfunc)
 Initilise application data structure and return a reference. More...
 
int framework_init (int argc, char *argv[], frameworkfunc callback)
 Initilise the application daemonise and join the manager thread. More...
 

Detailed Description

A Collection of helper functions and wrapped up interfaces to other libraries.

Macro Definition Documentation

#define ALLOC_CONST (   const_var,
  val 
)
Value:
{ \
char *tmp_char; \
if (val) { \
tmp_char = (char*)malloc(strlen(val) + 1); \
strcpy(tmp_char, val); \
const_var = (const char*)tmp_char; \
} else { \
const_var = NULL; \
} \
}

Macro to assign values to char const.

Definition at line 959 of file dtsapp.h.

Referenced by add_radserver(), framework_mkcore(), ldap_addinit(), ldap_modifyinit(), ldap_saslbind(), xml_modify(), and xslt_addparam().

#define FRAMEWORK_MAIN (   progname,
  name,
  email,
  www,
  year,
  runfile,
  flags,
  sighfunc 
)
Value:
static int framework_main(int argc, char *argv[]); \
int main(int argc, char *argv[]) { \
framework_mkcore(progname, name, email, www, year, runfile, flags, sighfunc); \
return (framework_init(argc, argv, framework_main)); \
} \
static int framework_main(int argc, char *argv[])
void framework_mkcore(char *progname, char *name, char *email, char *web, int year, char *runfile, int flags, syssighandler sigfunc)
Initilise application data structure and return a reference.
Definition: main.c:221
int framework_init(int argc, char *argv[], frameworkfunc callback)
Initilise the application daemonise and join the manager thread.
Definition: main.c:260
enum sock_flags flags
Socket control flags.
Definition: dtsapp.h:140

A macro to replace main() with initilization and daemonization code.

Note
Argument count is argc and arguments is array argv.
See Also
framework_flags
framework_mkcore()
framework_init()
Parameters
prognameDescriptive program name.
nameCopyright holders name.
emailCopyright holders email.
wwwWeb address.
yearCopyright year.
runfileApplication runfile.
flagsApplication flags.
sighfuncSignal handler function.

Definition at line 949 of file dtsapp.h.

Typedef Documentation

typedef int(* frameworkfunc)(int, char **)

Framework callback function.

Parameters
argcArgument count.
argvArgument array.
Returns
Application exit code.

Definition at line 219 of file dtsapp.h.

typedef void(* syssighandler)(int, siginfo_t *, void *)

Callback to user supplied signal handler.

Parameters
sigSignal been handled.
siSa sigaction.
unsedUnused cast to void from ucontext_t

Definition at line 228 of file dtsapp.h.

Enumeration Type Documentation

Application control flags.

Enumerator
FRAMEWORK_FLAG_DAEMON 

Allow application daemonization.

FRAMEWORK_FLAG_NOGNU 

Dont print GNU copyright.

FRAMEWORK_FLAG_DAEMONLOCK 

Create lockfile on daemonize latter.

Its possible you want to call daemonize latter and want the lockfile created then

Note
not compatible with FRAMEWORK_FLAG_DAEMON and has no effect FRAMEWORK_FLAG_DAEMON is set.

Definition at line 310 of file dtsapp.h.

310  {
312  FRAMEWORK_FLAG_DAEMON = 1 << 0,
314  FRAMEWORK_FLAG_NOGNU = 1 << 1,
320 };
Create lockfile on daemonize latter.
Definition: dtsapp.h:319
Dont print GNU copyright.
Definition: dtsapp.h:314
Allow application daemonization.
Definition: dtsapp.h:312

Function Documentation

void daemonize ( )

Daemonise the application using fork/exit.

This should be run early before file descriptors and threads are started

See Also
FRAMEWORK_MAIN()
Warning
on failure the program will exit.
Todo:
WIN32 options is there a alternative for this.

Definition at line 94 of file main.c.

References framework_core::flags, framework_core::flock, FRAMEWORK_FLAG_DAEMONLOCK, lockpidfile(), objunref(), and framework_core::runfile.

Referenced by framework_init(), and FRAMEWORK_MAIN().

94  {
95  struct framework_core *ci = framework_core_info;
96 
97 #ifndef __WIN32__
98  pid_t forkpid;
99 
100  /* fork and die daemonize*/
101  forkpid = fork();
102  if (forkpid > 0) {
103  /* im all grown up and can pass onto child*/
104  exit(0);
105  } else if (forkpid < 0) {
106  /* could not fork*/
107  exit(-1);
108  }
109 
110  setsid();
111 
112  /* Dont want these as a daemon*/
113  signal(SIGTSTP, SIG_IGN);
114  signal(SIGCHLD, SIG_IGN);
115 #endif
116 
117  /*delayed lock file from FRAMEWORK_MAIN / framework_init*/
118  if (ci && (ci->flags & FRAMEWORK_FLAG_DAEMONLOCK)) {
119  if ((ci->flock = lockpidfile(ci->runfile)) < 0) {
120  printf("Could not lock pid file Exiting\n");
121  while(framework_core_info) {
122  objunref(framework_core_info);
123  }
124  exit (-1);
125  }
126  objunref(ci);
127  }
128 }
int flock
if there is a file locked this is the FD that will be unlocked and unlinked
Definition: dtsapp.h:340
int flags
Application Options.
Definition: dtsapp.h:348
int lockpidfile(const char *runfile)
Lock the run file in the framework application info.
Definition: main.c:135
Create lockfile on daemonize latter.
Definition: dtsapp.h:319
Application framework data.
Definition: dtsapp.h:326
const char * runfile
File to write PID too and lock.
Definition: dtsapp.h:334
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
int framework_init ( int  argc,
char *  argv[],
frameworkfunc  callback 
)

Initilise the application daemonise and join the manager thread.

Warning
failure to pass a callback will require running stopthreads and jointhreads.
framework information configured by framework_mkcore will be freed on exit.
Parameters
argcArgument count argv[0] will be program name.
argvArgument array.
callbackFunction to pass control too.

Definition at line 260 of file main.c.

References daemonize(), framework_core::developer, framework_core::email, framework_core::flags, framework_core::flock, FRAMEWORK_FLAG_DAEMON, FRAMEWORK_FLAG_DAEMONLOCK, FRAMEWORK_FLAG_NOGNU, lockpidfile(), objref(), objunref(), printgnu(), framework_core::progname, framework_core::runfile, framework_core::sa, seedrand(), sslstartup(), stopthreads(), unrefconfigfiles(), framework_core::www, and framework_core::year.

260  {
261  struct framework_core *ci = framework_core_info;
262  int ret = 0;
263 
264  seedrand();
265  sslstartup();
266 
267  /*prinit out a GNU licence summary*/
268  if (ci && !(ci->flags & FRAMEWORK_FLAG_NOGNU)) {
269  printgnu(ci->progname, ci->year, ci->developer, ci->email, ci->www);
270  }
271 
272  /* grab a ref for framework_core_info to be used latter*/
273  if (ci && ci->flags & FRAMEWORK_FLAG_DAEMONLOCK) {
274  objref(ci);
275  }
276 
277  /* fork the process to daemonize it*/
278  if (ci && ci->flags & FRAMEWORK_FLAG_DAEMON) {
279  daemonize();
280  }
281 
282  /* write pid to lockfile this should be done post daemonize*/
283  if (ci && !(ci->flags & FRAMEWORK_FLAG_DAEMONLOCK)) {
284  if ((ci->flock = lockpidfile(ci->runfile)) < 0) {
285  printf("Could not lock pid file Exiting\n");
286  return -1;
287  }
288  }
289 
290 #ifndef __WIN32__
291  /* interupt handler close clean on term so physical is reset*/
292  configure_sigact(framework_core_info->sa);
293 #endif
294 
295  /*run the code from the application*/
296  if (callback) {
297  ret = callback(argc, argv);
298  /* wait for all threads to end*/
299  stopthreads(1);
300  }
301 
302  /* turn off the lights*/
303  objunref(ci);
304  if (framework_core_info && framework_core_info->flags & FRAMEWORK_FLAG_DAEMONLOCK) {
305  objunref(framework_core_info);
306  }
308  return (ret);
309 }
const char * email
Email address of copyright holder.
Definition: dtsapp.h:330
const char * progname
Detailed application name.
Definition: dtsapp.h:336
int objref(void *data)
Reference a object.
Definition: refobj.c:153
struct sigaction * sa
sigaction structure allocated on execution
Definition: dtsapp.h:342
const char * developer
Developer/Copyright holder.
Definition: dtsapp.h:328
void stopthreads(int join)
Signal manager to stop and cancel all running threads.
Definition: thread.c:303
int flock
if there is a file locked this is the FD that will be unlocked and unlinked
Definition: dtsapp.h:340
const char * www
URL displayed (use full URL ie with http://)
Definition: dtsapp.h:332
void sslstartup(void)
Initialise SSL support this should be called at startup.
Definition: sslutil.c:639
int flags
Application Options.
Definition: dtsapp.h:348
int lockpidfile(const char *runfile)
Lock the run file in the framework application info.
Definition: main.c:135
int year
Copyright year.
Definition: dtsapp.h:338
void unrefconfigfiles(void)
Empty out and unreference config files.
Definition: config.c:78
Create lockfile on daemonize latter.
Definition: dtsapp.h:319
void seedrand(void)
Seed openssl random number generator.
Definition: util.c:68
Application framework data.
Definition: dtsapp.h:326
Dont print GNU copyright.
Definition: dtsapp.h:314
void daemonize()
Daemonise the application using fork/exit.
Definition: main.c:94
const char * runfile
File to write PID too and lock.
Definition: dtsapp.h:334
void printgnu(const char *pname, int year, const char *dev, const char *email, const char *www)
Print a brief GNU copyright notice on console.
Definition: main.c:78
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
Allow application daemonization.
Definition: dtsapp.h:312
void framework_mkcore ( char *  progname,
char *  name,
char *  email,
char *  web,
int  year,
char *  runfile,
int  flags,
syssighandler  sigfunc 
)

Initilise application data structure and return a reference.

Warning
failure to supply a signal handler on non WIN32 systems will deafault to exiting with -1 on SIGINT/SIGKILL.
Todo:
does threads actually work in windows with no sighandler.
Warning
do not call this function without calling framework_init as the memory allocated will not be freed.
Parameters
prognameDescrioptive program name.
nameCopyright holder.
emailCopyright email address.
webWebsite address.
yearCopyright year.
runfileRun file that will store the pid and be locked (flock).
flagsApplication flags.
sigfuncSignal handler.

Definition at line 221 of file main.c.

References ALLOC_CONST, framework_core::developer, framework_core::email, framework_core::flags, objalloc(), objunref(), framework_core::progname, framework_core::runfile, framework_core::sa, framework_core::sig_handler, framework_core::www, and framework_core::year.

221  {
222  struct framework_core *core_info;
223  if (framework_core_info) {
224  objunref(framework_core_info);
225  framework_core_info = NULL;
226  }
227 
228  if (!(core_info = objalloc(sizeof(*core_info), framework_free))) {
229  return;
230  }
231 
232 #ifndef __WIN32__
233  if (core_info && !(core_info->sa = malloc(sizeof(*core_info->sa)))) {
234  free(core_info);
235  return;
236  }
237 #endif
238 
239  ALLOC_CONST(core_info->developer, name);
240  ALLOC_CONST(core_info->email, email);
241  ALLOC_CONST(core_info->www, web);
242  ALLOC_CONST(core_info->runfile, runfile);
243  ALLOC_CONST(core_info->progname, progname);
244  core_info->year = year;
245  core_info->flags = flags;
246 #ifndef __WIN32__
247  core_info->sig_handler = sigfunc;
248 #endif
249  /* Pass reference to static system variable*/
250  framework_core_info = core_info;
251 }
const char * email
Email address of copyright holder.
Definition: dtsapp.h:330
const char * progname
Detailed application name.
Definition: dtsapp.h:336
struct sigaction * sa
sigaction structure allocated on execution
Definition: dtsapp.h:342
const char * developer
Developer/Copyright holder.
Definition: dtsapp.h:328
void * objalloc(int size, objdestroy)
Allocate a referenced lockable object.
Definition: refobj.c:129
const char * www
URL displayed (use full URL ie with http://)
Definition: dtsapp.h:332
int flags
Application Options.
Definition: dtsapp.h:348
int year
Copyright year.
Definition: dtsapp.h:338
Application framework data.
Definition: dtsapp.h:326
const char * runfile
File to write PID too and lock.
Definition: dtsapp.h:334
#define ALLOC_CONST(const_var, val)
Macro to assign values to char const.
Definition: dtsapp.h:959
int objunref(void *data)
Drop reference held.
Definition: refobj.c:184
syssighandler sig_handler
Signal handler to pass signals too.
Definition: dtsapp.h:345
int lockpidfile ( const char *  runfile)

Lock the run file in the framework application info.

This can be delayed till running daemonize in the user function loop setting flag FRAMEWORK_FLAG_DAEMONLOCK

Parameters
runfileFile to write pid to and lock.
Returns
0 if no file is specified or not supported. The file descriptor on success.

Definition at line 135 of file main.c.

References framework_core::flock.

Referenced by daemonize(), and framework_init().

135  {
136  int lck_fd = 0;
137 #ifndef __WIN32__
138  char pidstr[12];
139  pid_t mypid;
140 
141  mypid = getpid();
142  sprintf(pidstr,"%i\n", (int)mypid);
143  if (runfile && ((lck_fd = open(runfile, O_RDWR|O_CREAT, 0640)) > 0) && (!flock(lck_fd, LOCK_EX | LOCK_NB))) {
144  if (write(lck_fd, pidstr, strlen(pidstr)) < 0) {
145  close(lck_fd);
146  lck_fd = -1;
147  }
148  /* file was opened and not locked*/
149  } else if (runfile && lck_fd) {
150  close(lck_fd);
151  lck_fd = -1;
152  }
153 #endif
154  return (lck_fd);
155 }
int flock
if there is a file locked this is the FD that will be unlocked and unlinked
Definition: dtsapp.h:340
const char * runfile
File to write PID too and lock.
Definition: dtsapp.h:334
void printgnu ( const char *  pname,
int  year,
const char *  dev,
const char *  email,
const char *  www 
)

Print a brief GNU copyright notice on console.

See Also
FRAMEWORK_MAIN()
framework_mkcore()
Parameters
pnameDetailed application name.
yearCopyright year.
devProgramer / copyright holder name.
emailEmail address.
wwwHTTP URL.

Definition at line 78 of file main.c.

Referenced by framework_init().

78  {
79  printf("\n"
80  " %s\n\n"
81  " Copyright (C) %i %s <%s>\n\n"
82  " %s\n\n"
83  " This program comes with ABSOLUTELY NO WARRANTY\n"
84  " This is free software, and you are welcome to redistribute it\n"
85  " under certain conditions.\n\n", pname, year, dev, email, www);
86 }
const char * email
Email address of copyright holder.
Definition: dtsapp.h:330
const char * www
URL displayed (use full URL ie with http://)
Definition: dtsapp.h:332
int year
Copyright year.
Definition: dtsapp.h:338