main: fix typo
[src/app-framework-binder.git] / src / main.c
1 /* 
2  * Copyright (C) 2015 "IoT.bzh"
3  * Author "Fulup Ar Foll"
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 /* 
20  * File:   main.c
21  * Author: "Fulup Ar Foll"
22  *
23  * Created on 05 December 2015, 15:38
24  */
25
26 #include "local-def.h"
27
28 #include <syslog.h>
29 #include <setjmp.h>
30 #include <signal.h>
31 #include <getopt.h>
32 #include <pwd.h>
33
34 #define AFB_VERSION    "0.1"
35
36 static sigjmp_buf exitPoint; // context save for set/longjmp
37
38 /*----------------------------------------------------------
39  | printversion
40  |   print version and copyright
41  +--------------------------------------------------------- */
42  static void printVersion (void) {
43
44    fprintf (stderr,"\n----------------------------------------- \n");
45    fprintf (stderr,"|  AFB [Application Framework Binder] version=%s |\n", AFB_VERSION);
46    fprintf (stderr,"----------------------------------------- \n");
47    fprintf (stderr,"|  Copyright(C) 2015 Fulup Ar Foll /IoT.bzh [fulup -at- iot.bzh]\n");
48    fprintf (stderr,"|  AFB comes with ABSOLUTELY NO WARRANTY.\n");
49    fprintf (stderr,"|  Licence [what ever makes you happy] until you fix bugs by yourself :)\n\n");
50    exit (0);
51  } // end printVersion
52
53
54 // Define command line option
55 #define SET_VERBOSE        101
56 #define SET_BACKGROUND     105
57 #define SET_FORGROUND      106
58 #define KILL_PREV_EXIT     107
59 #define KILL_PREV_REST     108
60 #define SET_FAKE_MOD       109
61
62 #define SET_TCP_PORT       120
63 #define SET_ROOT_DIR       121
64 #define SET_ROOT_BASE      122
65 #define SET_ROOT_API       123
66 #define SET_ROOT_ALIAS     124
67
68 #define SET_CACHE_TO       130
69 #define SET_USERID         131
70 #define SET_PID_FILE       132
71 #define SET_SESSION_DIR    133
72 #define SET_CONFIG_FILE    134
73 #define SET_CONFIG_SAVE    135
74 #define SET_CONFIG_EXIT    138
75
76 #define SET_AUTH_TOKEN     141
77 #define SET_LDPATH         142
78 #define SET_APITIMEOUT     143
79 #define SET_CNTXTIMEOUT    144
80
81 #define DISPLAY_VERSION    150
82 #define DISPLAY_HELP       151
83
84 #define SET_MODE           160
85 #define SET_READYFD        161
86
87 // Command line structure hold cli --command + help text
88 typedef struct {
89   int  val;        // command number within application
90   int  has_arg;    // command number within application
91   char *name;      // command as used in --xxxx cli
92   char *help;      // help text
93 } AFB_options;
94
95
96 // Supported option
97 static  AFB_options cliOptions [] = {
98   {SET_VERBOSE      ,0,"verbose"         , "Verbose Mode"},
99
100   {SET_FORGROUND    ,0,"foreground"      , "Get all in foreground mode"},
101   {SET_BACKGROUND   ,0,"daemon"          , "Get all in background mode"},
102   {KILL_PREV_EXIT   ,0,"kill"            , "Kill active process if any and exit"},
103   {KILL_PREV_REST   ,0,"restart"         , "Kill active process if any and restart"},
104
105   {SET_TCP_PORT     ,1,"port"            , "HTTP listening TCP port  [default 1234]"},
106   {SET_ROOT_DIR     ,1,"rootdir"         , "HTTP Root Directory [default $HOME/.AFB]"},
107   {SET_ROOT_BASE    ,1,"rootbase"        , "Angular Base Root URL [default /opa]"},
108   {SET_ROOT_API     ,1,"rootapi"         , "HTML Root API URL [default /api]"},
109   {SET_ROOT_ALIAS   ,1,"alias"           , "Muliple url map outside of rootdir [eg: --alias=/icons:/usr/share/icons]"},
110   
111   {SET_APITIMEOUT   ,1,"apitimeout"      , "Plugin API timeout in seconds [default 10]"},
112   {SET_CNTXTIMEOUT  ,1,"cntxtimeout"     , "Client Session Context Timeout [default 900]"},
113   {SET_CACHE_TO     ,1,"cache-eol"       , "Client cache end of live [default 3600s]"},
114   
115   {SET_USERID       ,1,"setuid"          , "Change user id [default don't change]"},
116   {SET_PID_FILE     ,1,"pidfile"         , "PID file path [default none]"},
117   {SET_SESSION_DIR  ,1,"sessiondir"      , "Sessions file path [default rootdir/sessions]"},
118   {SET_CONFIG_FILE  ,1,"config"          , "Config Filename [default rootdir/sessions/configs/default.AFB]"},
119
120   {SET_LDPATH       ,1,"ldpaths"         , "Load Plugins from dir1:dir2:... [default = PLUGIN_INSTALL_DIR"},
121   {SET_AUTH_TOKEN   ,1,"token"           , "Initial Secret [default=no-session, --token="" for session without authentication]"},
122   
123   {DISPLAY_VERSION  ,0,"version"         , "Display version and copyright"},
124   {DISPLAY_HELP     ,0,"help"            , "Display this help"},
125
126   {SET_MODE         ,1,"mode"            , "set the mode: either local, remote or global"},
127   {SET_READYFD      ,1,"readyfd"         , "set the #fd to signal when ready"},
128   {0, 0, NULL, NULL}
129  };
130
131 static AFB_aliasdir aliasdir[MAX_ALIAS];
132 static int aliascount=0;
133
134 /*----------------------------------------------------------
135  | timeout signalQuit
136  |
137  +--------------------------------------------------------- */
138 void signalQuit (int signum) {
139
140   sigset_t sigset;
141
142   // unlock timeout signal to allow a new signal to come
143   sigemptyset (&sigset);
144   sigaddset   (&sigset, SIGABRT);
145   sigprocmask (SIG_UNBLOCK, &sigset, 0);
146
147   fprintf (stderr, "%s ERR:Received signal quit\n",configTime());
148   syslog (LOG_ERR, "Daemon got kill3 & quit [please report bug]");
149   longjmp (exitPoint, signum);
150 }
151
152
153 /*----------------------------------------------------------
154  | printHelp
155  |   print information from long option array
156  +--------------------------------------------------------- */
157
158  static void printHelp(char *name) {
159     int ind;
160     char command[20];
161
162     fprintf (stderr,"%s:\nallowed options\n", name);
163     for (ind=0; cliOptions [ind].name != NULL;ind++)
164     {
165       // display options
166       if (cliOptions [ind].has_arg == 0 )
167       {
168              fprintf (stderr,"  --%-15s %s\n", cliOptions [ind].name, cliOptions[ind].help);
169       } else {
170          sprintf(command,"%s=xxxx", cliOptions [ind].name);
171          fprintf (stderr,"  --%-15s %s\n", command, cliOptions[ind].help);
172       }
173     }
174     fprintf (stderr,"Example:\n  %s\\\n  --verbose --port=1234 --token='azerty' --ldpaths=build/plugins:/usr/lib64/agl/plugins\n", name);
175 } // end printHelp
176
177 /*----------------------------------------------------------
178  | writePidFile
179  |   write a file in /var/run/AFB with pid
180  +--------------------------------------------------------- */
181 static int writePidFile (AFB_config *config, int pid) {
182   FILE *file;
183
184   // if no pid file configure just return
185   if (config->pidfile == NULL) return 0;
186
187   // open pid file in write mode
188   file = fopen(config->pidfile,"w");
189   if (file == NULL) {
190     fprintf (stderr,"%s ERR:writePidFile fail to open [%s]\n",configTime(), config->pidfile);
191     return -1;
192   }
193
194   // write pid in file and close
195   fprintf (file, "%d\n", pid);
196   fclose  (file);
197   return 0;
198 }
199
200 /*----------------------------------------------------------
201  | readPidFile
202  |   read file in /var/run/AFB with pid
203  +--------------------------------------------------------- */
204 static int readPidFile (AFB_config *config) {
205   int  pid;
206   FILE *file;
207   int  status;
208
209   if (config->pidfile == NULL) return -1;
210
211   // open pid file in write mode
212   file = fopen(config->pidfile,"r");
213   if (file == NULL) {
214     fprintf (stderr,"%s ERR:readPidFile fail to open [%s]\n",configTime(), config->pidfile);
215     return -1;
216   }
217
218   // write pid in file and close
219   status = fscanf  (file, "%d\n", &pid);
220   fclose  (file);
221
222   // never kill pid 0
223   if (status != 1) return -1;
224
225   return (pid);
226 }
227
228 /*----------------------------------------------------------
229  | closeSession
230  |   try to close everything before leaving
231  +--------------------------------------------------------- */
232 static void closeSession (AFB_session *session) {
233
234
235 }
236
237 /*----------------------------------------------------------
238  | listenLoop
239  |   Main listening HTTP loop
240  +--------------------------------------------------------- */
241 static void listenLoop (AFB_session *session) {
242   AFB_error  err;
243
244   if (signal (SIGABRT, signalQuit) == SIG_ERR) {
245         fprintf (stderr, "%s ERR: main fail to install Signal handler\n", configTime());
246         return;
247   }
248
249   // ------ Start httpd server
250   if (session->config->httpdPort > 0) {
251
252         err = httpdStart (session);
253         if (err != AFB_SUCCESS) return;
254
255         if (session->readyfd != 0) {
256                 static const char readystr[] = "READY=1";
257                 write(session->readyfd, readystr, sizeof(readystr) - 1);
258                 close(session->readyfd);
259         }
260
261         // infinite loop
262         httpdLoop(session);
263
264         fprintf (stderr, "hoops returned from infinite loop [report bug]\n");
265   }
266 }
267   
268 /*---------------------------------------------------------
269  | main
270  |   Parse option and launch action
271  +--------------------------------------------------------- */
272
273 static void parse_arguments(int argc, char *argv[], AFB_session *session)
274 {
275   char*          programName = argv [0];
276   int            optionIndex = 0;
277   int            optc, ind;
278   int            nbcmd;
279   struct option *gnuOptions;
280   AFB_config     cliconfig; // temp structure to store CLI option before file config upload
281
282   // ------------- Build session handler & init config -------
283   memset(&cliconfig,0,sizeof(cliconfig));
284   memset(&aliasdir  ,0,sizeof(aliasdir));
285   cliconfig.aliasdir = aliasdir;
286
287   // ------------------ Process Command Line -----------------------
288
289   // if no argument print help and return
290   if (argc < 2) {
291        printHelp(programName);
292        exit(1);
293   }
294
295   // build GNU getopt info from cliOptions
296   nbcmd = sizeof (cliOptions) / sizeof (AFB_options);
297   gnuOptions = malloc (sizeof (*gnuOptions) * (unsigned)nbcmd);
298   for (ind=0; ind < nbcmd;ind++) {
299     gnuOptions [ind].name    = cliOptions[ind].name;
300     gnuOptions [ind].has_arg = cliOptions[ind].has_arg;
301     gnuOptions [ind].flag    = 0;
302     gnuOptions [ind].val     = cliOptions[ind].val;
303   }
304
305   // get all options from command line
306   while ((optc = getopt_long (argc, argv, "vsp?", gnuOptions, &optionIndex))
307         != EOF)
308   {
309     switch (optc)
310     {
311      case SET_VERBOSE:
312        verbose = 1;
313        break;
314
315     case SET_TCP_PORT:
316        if (optarg == 0) goto needValueForOption;
317        if (!sscanf (optarg, "%d", &cliconfig.httpdPort)) goto notAnInteger;
318        break;
319        
320     case SET_APITIMEOUT:
321        if (optarg == 0) goto needValueForOption;
322        if (!sscanf (optarg, "%d", &cliconfig.apiTimeout)) goto notAnInteger;
323        break;
324
325     case SET_CNTXTIMEOUT:
326        if (optarg == 0) goto needValueForOption;
327        if (!sscanf (optarg, "%d", &cliconfig.cntxTimeout)) goto notAnInteger;
328        break;
329
330     case SET_ROOT_DIR:
331        if (optarg == 0) goto needValueForOption;
332        cliconfig.rootdir   = optarg;
333        if (verbose) fprintf(stderr, "Forcing Rootdir=%s\n",cliconfig.rootdir);
334        break;       
335        
336     case SET_ROOT_BASE:
337        if (optarg == 0) goto needValueForOption;
338        cliconfig.rootbase   = optarg;
339        if (verbose) fprintf(stderr, "Forcing Rootbase=%s\n",cliconfig.rootbase);
340        break;
341
342     case SET_ROOT_API:
343        if (optarg == 0) goto needValueForOption;
344        cliconfig.rootapi   = optarg;
345        if (verbose) fprintf(stderr, "Forcing Rootapi=%s\n",cliconfig.rootapi);
346        break;
347        
348     case SET_ROOT_ALIAS:
349        if (optarg == 0) goto needValueForOption;
350        if (aliascount < MAX_ALIAS) {
351             aliasdir[aliascount].url  = strsep(&optarg,":");
352             if (optarg == NULL) {
353               fprintf(stderr, "missing ':' in alias %s, ignored\n", aliasdir[aliascount].url);
354             } else {
355               aliasdir[aliascount].path = optarg;
356               aliasdir[aliascount].len  = strlen(aliasdir[aliascount].url);
357               if (verbose) fprintf(stderr, "Alias url=%s path=%s\n", aliasdir[aliascount].url, aliasdir[aliascount].path);
358               aliascount++;
359             }
360        } else {
361            fprintf(stderr, "Too many aliases [max:%d] %s ignored\n", MAX_ALIAS, optarg);
362        }     
363        break;
364        
365     case SET_AUTH_TOKEN:
366        if (optarg == 0) goto needValueForOption;
367        cliconfig.token   = optarg;
368        break;
369
370     case SET_LDPATH:
371        if (optarg == 0) goto needValueForOption;
372        cliconfig.ldpaths = optarg;
373        break;
374
375     case SET_PID_FILE:
376        if (optarg == 0) goto needValueForOption;
377        cliconfig.pidfile   = optarg;
378        break;
379
380     case SET_SESSION_DIR:
381        if (optarg == 0) goto needValueForOption;
382        cliconfig.sessiondir   = optarg;
383        break;
384
385     case  SET_CONFIG_FILE:
386        if (optarg == 0) goto needValueForOption;
387        cliconfig.configfile   = optarg;
388        break;
389
390     case  SET_CACHE_TO:
391        if (optarg == 0) goto needValueForOption;
392        if (!sscanf (optarg, "%d", &cliconfig.cacheTimeout)) goto notAnInteger;
393        break;
394
395     case SET_USERID:
396        if (optarg == 0) goto needValueForOption;
397        cliconfig.setuid = optarg;
398        break;
399
400     case SET_FAKE_MOD:
401        if (optarg != 0) goto noValueForOption;
402        session->fakemod  = 1;
403        break;
404
405     case SET_FORGROUND:
406        if (optarg != 0) goto noValueForOption;
407        session->foreground  = 1;
408        break;
409
410     case SET_BACKGROUND:
411        if (optarg != 0) goto noValueForOption;
412        session->background  = 1;
413        break;
414
415      case KILL_PREV_REST:
416        if (optarg != 0) goto noValueForOption;
417        session->killPrevious  = 1;
418        break;
419
420      case KILL_PREV_EXIT:
421        if (optarg != 0) goto noValueForOption;
422        session->killPrevious  = 2;
423        break;
424
425     case SET_MODE:
426        if (optarg == 0) goto needValueForOption;
427        if (!strcmp(optarg, "local")) cliconfig.mode = AFB_MODE_LOCAL;
428        else if (!strcmp(optarg, "remote")) cliconfig.mode = AFB_MODE_REMOTE;
429        else if (!strcmp(optarg, "global")) cliconfig.mode = AFB_MODE_GLOBAL;
430        else goto badMode;
431        break;
432
433     case SET_READYFD:
434        if (optarg == 0) goto needValueForOption;
435        if (!sscanf (optarg, "%u", &session->readyfd)) goto notAnInteger;
436        break;
437
438     case DISPLAY_VERSION:
439        if (optarg != 0) goto noValueForOption;
440        printVersion();
441        exit(0);
442
443     case DISPLAY_HELP:
444      default:
445        printHelp(programName);
446        exit(0);
447     }
448   }
449   free(gnuOptions);
450  
451   // if exist merge config file with CLI arguments
452   configLoadFile  (session, &cliconfig);
453   return;
454
455
456 needValueForOption:
457   fprintf (stderr,"\nERR: AFB-daemon option [--%s] need a value i.e. --%s=xxx\n\n"
458           ,gnuOptions[optionIndex].name, gnuOptions[optionIndex].name);
459   exit (1);
460
461 notAnInteger:
462   fprintf (stderr,"\nERR: AFB-daemon option [--%s] requirer an interger i.e. --%s=9\n\n"
463           ,gnuOptions[optionIndex].name, gnuOptions[optionIndex].name);
464   exit (1);
465
466 noValueForOption:
467   fprintf (stderr,"\nERR: AFB-daemon option [--%s] don't take value\n\n"
468           ,gnuOptions[optionIndex].name);
469   exit (1);
470
471 badMode:
472   fprintf (stderr,"\nERR: AFB-daemon option [--%s] only accepts local, global or remote.\n\n"
473           ,gnuOptions[optionIndex].name);
474   exit (1);
475 }
476
477 /*---------------------------------------------------------
478  | main
479  |   Parse option and launch action
480  +--------------------------------------------------------- */
481
482 int main(int argc, char *argv[])  {
483   AFB_session    *session;
484   char*          programName = argv [0];
485   int            consoleFD;
486   int            pid, status;
487
488   // ------------- Build session handler & init config -------
489   session = configInit ();
490   parse_arguments(argc, argv, session);
491   initPlugins(session);
492
493   // ------------------ sanity check ----------------------------------------
494   if  ((session->background) && (session->foreground)) {
495     fprintf (stderr, "%s ERR: cannot select foreground & background at the same time\n",configTime());
496      exit (1);
497   }
498
499   // ------------------ Some useful default values -------------------------
500   if  ((session->background == 0) && (session->foreground == 0)) session->foreground=1;
501
502   // open syslog if ever needed
503   openlog("AFB-log", 0, LOG_DAEMON);
504
505   // -------------- Try to kill any previous process if asked ---------------------
506   if (session->killPrevious) {
507     pid = readPidFile (session->config);  // enforce commandline option
508     switch (pid) {
509     case -1:
510       fprintf (stderr, "%s ERR: main --kill ignored no PID file [%s]\n",configTime(), session->config->pidfile);
511       break;
512     case 0:
513       fprintf (stderr, "%s ERR: main --kill ignored no active AFB process\n",configTime());
514       break;
515     default:
516       status = kill (pid,SIGINT );
517       if (status == 0) {
518              if (verbose) printf ("%s INF: main signal INTR sent to pid:%d \n", configTime(), pid);
519       } else {
520          // try kill -9
521          status = kill (pid,9);
522          if (status != 0)  fprintf (stderr, "%s ERR: main failled to killed pid=%d \n",configTime(), pid);
523       }
524     } // end switch pid
525
526     if (session->killPrevious >= 2) goto normalExit;
527   } // end killPrevious
528
529
530   // ------------------ clean exit on CTR-C signal ------------------------
531   if (signal (SIGINT, signalQuit) == SIG_ERR) {
532     fprintf (stderr, "%s Quit Signal received.",configTime());
533     return 1;
534   }
535
536   // save exitPoint context when returning from longjmp closeSession and exit
537   status = setjmp (exitPoint); // return !+ when coming from longjmp
538   if (status != 0) {
539     if (verbose) printf ("INF: main returning from longjump after signal [%d]\n", status);
540     closeSession (session);
541     goto exitOnSignal;
542   }
543
544   // let's run this program with a low priority
545   status=nice (20);
546
547   // ------------------ Finaly Process Commands -----------------------------
548    // if --save then store config on disk upfront
549     if (session->config->setuid) {
550         int err;
551         struct passwd *passwd;
552         passwd=getpwnam(session->config->setuid);
553         
554         if (passwd == NULL) goto errorSetuid;
555         
556         err = setuid(passwd->pw_uid);
557         if (err) goto errorSetuid;
558     }
559
560     // let's not take the risk to run as ROOT
561     //if (getuid() == 0)  goto errorNoRoot;
562
563     // check session dir and create if it does not exist
564     if (sessionCheckdir (session) != AFB_SUCCESS) goto errSessiondir;
565     if (verbose) fprintf (stderr, "AFB: notice Init config done\n");
566
567
568
569     // ---- run in foreground mode --------------------
570     if (session->foreground) {
571
572         if (verbose) fprintf (stderr,"AFB: notice Foreground mode\n");
573
574         // write a pid file for --kill-previous and --raise-debug option
575         status = writePidFile (session->config, getpid());
576         if (status == -1) goto errorPidFile;
577
578         // enter listening loop in foreground
579         listenLoop(session);
580         goto exitInitLoop;
581   } // end foreground
582
583
584   // --------- run in background mode -----------
585   if (session->background) {
586
587        // if (status != 0) goto errorCommand;
588       if (verbose) printf ("AFB: Entering background mode\n");
589
590       // open /dev/console to redirect output messAFBes
591       consoleFD = open(session->config->console, O_WRONLY | O_APPEND | O_CREAT , 0640);
592       if (consoleFD < 0) goto errConsole;
593
594       // fork process when running background mode
595       pid = fork ();
596
597       // son process get all data in standalone mode
598       if (pid == 0) {
599
600              printf ("\nAFB: background mode [pid:%d console:%s]\n", getpid(),session->config->console);
601              if (verbose) printf ("AFB: info use '%s --restart --rootdir=%s # [--pidfile=%s] to restart daemon\n", programName,session->config->rootdir, session->config->pidfile);
602
603          // redirect default I/O on console
604          close (2); status=dup(consoleFD);  // redirect stderr
605          close (1); status=dup(consoleFD);  // redirect stdout
606          close (0);           // no need for stdin
607          close (consoleFD);
608
609          setsid();   // allow father process to fully exit
610              sleep (2);  // allow main to leave and release port
611
612          fprintf (stderr, "----------------------------\n");
613          fprintf (stderr, "%s INF: main background pid=%d\n", configTime(), getpid());
614          fflush  (stderr);
615
616          // if everything look OK then look forever
617          syslog (LOG_ERR, "AFB: Entering infinite loop in background mode");
618
619          // should normally never return from this loop
620          listenLoop(session);
621          syslog (LOG_ERR, "AFB: FAIL background infinite loop exited check [%s]\n", session->config->console);
622
623          goto exitInitLoop;
624       }
625
626       // if fail nothing much to do
627       if (pid == -1) goto errorFork;
628
629       // fork worked and we are in father process
630       status = writePidFile (session->config, pid);
631       if (status == -1) goto errorPidFile;
632
633       // we are in father process, we don't need this one
634       _exit (0);
635
636   } // end background-foreground
637
638 normalExit:
639   closeSession (session);   // try to close everything before leaving
640   if (verbose) printf ("\n---- Application Framework Binder Normal End ------\n");
641   exit(0);
642
643 // ------------- Fatal ERROR display error and quit  -------------
644 errorSetuid:
645   fprintf (stderr,"\nERR: AFB-daemon Failed to change UID to username=[%s]\n\n", session->config->setuid);
646   exit (1);
647   
648 //errorNoRoot:
649 //  fprintf (stderr,"\nERR:AFB-daemon Not allow to run as root [use --seteuid=username option]\n\n");
650 //  exit (1);
651
652 errorPidFile:
653   fprintf (stderr,"\nERR: AFB-daemon Failed to write pid file [%s]\n\n", session->config->pidfile);
654   exit (1);
655
656 errorFork:
657   fprintf (stderr,"\nERR: AFB-daemon Failed to fork son process\n\n");
658   exit (1);
659
660 exitOnSignal:
661   fprintf (stderr,"\n%s INF: AFB-daemon pid=%d received exit signal (Hopefully crtl-C or --kill-previous !!!)\n\n"
662                  ,configTime(), getpid());
663   exit (1);
664
665 errConsole:
666   fprintf (stderr,"\nERR: AFB-daemon cannot open /dev/console (use --foreground)\n\n");
667   exit (1);
668
669 errSessiondir:
670   fprintf (stderr,"\nERR: AFB-daemon cannot read/write session dir\n\n");
671   exit (1);
672
673 exitInitLoop:
674   // try to unlink pid file if any
675   if (session->background && session->config->pidfile != NULL)  unlink (session->config->pidfile);
676   exit (1);
677 }
678
679