main: use valid exit value
[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   {SET_CONFIG_SAVE  ,0,"save"            , "Save config on disk [default no]"},
120   {SET_CONFIG_EXIT  ,0,"saveonly"        , "Save config on disk and then exit"},
121
122   {SET_LDPATH       ,1,"ldpaths"         , "Load Plugins from dir1:dir2:... [default = PLUGIN_INSTALL_DIR"},
123   {SET_AUTH_TOKEN   ,1,"token"           , "Initial Secret [default=no-session, --token="" for session without authentication]"},
124   
125   {DISPLAY_VERSION  ,0,"version"         , "Display version and copyright"},
126   {DISPLAY_HELP     ,0,"help"            , "Display this help"},
127
128   {SET_MODE         ,1,"mode"            , "set the mode: either local, remote or global"},
129   {SET_READYFD      ,1,"readyfd"         , "set the #fd to signal when ready"},
130   {0, 0, NULL, NULL}
131  };
132
133 static AFB_aliasdir aliasdir[MAX_ALIAS];
134 static int aliascount=0;
135
136 /*----------------------------------------------------------
137  | timeout signalQuit
138  |
139  +--------------------------------------------------------- */
140 void signalQuit (int signum) {
141
142   sigset_t sigset;
143
144   // unlock timeout signal to allow a new signal to come
145   sigemptyset (&sigset);
146   sigaddset   (&sigset, SIGABRT);
147   sigprocmask (SIG_UNBLOCK, &sigset, 0);
148
149   fprintf (stderr, "%s ERR:Received signal quit\n",configTime());
150   syslog (LOG_ERR, "Daemon got kill3 & quit [please report bug]");
151   longjmp (exitPoint, signum);
152 }
153
154
155 /*----------------------------------------------------------
156  | printHelp
157  |   print information from long option array
158  +--------------------------------------------------------- */
159
160  static void printHelp(char *name) {
161     int ind;
162     char command[20];
163
164     fprintf (stderr,"%s:\nallowed options\n", name);
165     for (ind=0; cliOptions [ind].name != NULL;ind++)
166     {
167       // display options
168       if (cliOptions [ind].has_arg == 0 )
169       {
170              fprintf (stderr,"  --%-15s %s\n", cliOptions [ind].name, cliOptions[ind].help);
171       } else {
172          sprintf(command,"%s=xxxx", cliOptions [ind].name);
173          fprintf (stderr,"  --%-15s %s\n", command, cliOptions[ind].help);
174       }
175     }
176     fprintf (stderr,"Example:\n  %s\\\n  --verbose --port=1234 --token='azerty' --ldpaths=build/plugins:/usr/lib64/agl/plugins\n", name);
177 } // end printHelp
178
179 /*----------------------------------------------------------
180  | writePidFile
181  |   write a file in /var/run/AFB with pid
182  +--------------------------------------------------------- */
183 static int writePidFile (AFB_config *config, int pid) {
184   FILE *file;
185
186   // if no pid file configure just return
187   if (config->pidfile == NULL) return 0;
188
189   // open pid file in write mode
190   file = fopen(config->pidfile,"w");
191   if (file == NULL) {
192     fprintf (stderr,"%s ERR:writePidFile fail to open [%s]\n",configTime(), config->pidfile);
193     return -1;
194   }
195
196   // write pid in file and close
197   fprintf (file, "%d\n", pid);
198   fclose  (file);
199   return 0;
200 }
201
202 /*----------------------------------------------------------
203  | readPidFile
204  |   read file in /var/run/AFB with pid
205  +--------------------------------------------------------- */
206 static int readPidFile (AFB_config *config) {
207   int  pid;
208   FILE *file;
209   int  status;
210
211   if (config->pidfile == NULL) return -1;
212
213   // open pid file in write mode
214   file = fopen(config->pidfile,"r");
215   if (file == NULL) {
216     fprintf (stderr,"%s ERR:readPidFile fail to open [%s]\n",configTime(), config->pidfile);
217     return -1;
218   }
219
220   // write pid in file and close
221   status = fscanf  (file, "%d\n", &pid);
222   fclose  (file);
223
224   // never kill pid 0
225   if (status != 1) return -1;
226
227   return (pid);
228 }
229
230 /*----------------------------------------------------------
231  | closeSession
232  |   try to close everything before leaving
233  +--------------------------------------------------------- */
234 static void closeSession (AFB_session *session) {
235
236
237 }
238
239 /*----------------------------------------------------------
240  | listenLoop
241  |   Main listening HTTP loop
242  +--------------------------------------------------------- */
243 static void listenLoop (AFB_session *session) {
244   AFB_error  err;
245
246   if (signal (SIGABRT, signalQuit) == SIG_ERR) {
247         fprintf (stderr, "%s ERR: main fail to install Signal handler\n", configTime());
248         return;
249   }
250
251   // ------ Start httpd server
252   if (session->config->httpdPort > 0) {
253
254         err = httpdStart (session);
255         if (err != AFB_SUCCESS) return;
256
257         if (session->readyfd != 0) {
258                 static const char readystr[] = "READY=1";
259                 write(session->readyfd, readystr, sizeof(readystr) - 1);
260                 close(session->readyfd);
261         }
262
263         // infinite loop
264         httpdLoop(session);
265
266         fprintf (stderr, "hoops returned from infinite loop [report bug]\n");
267   }
268 }
269   
270 /*---------------------------------------------------------
271  | main
272  |   Parse option and launch action
273  +--------------------------------------------------------- */
274
275 int main(int argc, char *argv[])  {
276   AFB_session    *session;
277   char*          programName = argv [0];
278   int            optionIndex = 0;
279   int            optc, ind, consoleFD;
280   int            pid, nbcmd, status;
281   AFB_config     cliconfig; // temp structure to store CLI option before file config upload
282
283   // ------------- Build session handler & init config -------
284   session = configInit ();
285   memset(&cliconfig,0,sizeof(cliconfig));
286   memset(&aliasdir  ,0,sizeof(aliasdir));
287   cliconfig.aliasdir = aliasdir;
288
289   // GNU CLI getopts nterface.
290   struct option *gnuOptions;
291
292   // ------------------ Process Command Line -----------------------
293
294   // if no argument print help and return
295   if (argc < 2) {
296        printHelp(programName);
297        return 1;
298   }
299
300   // build GNU getopt info from cliOptions
301   nbcmd = sizeof (cliOptions) / sizeof (AFB_options);
302   gnuOptions = malloc (sizeof (*gnuOptions) * (unsigned)nbcmd);
303   for (ind=0; ind < nbcmd;ind++) {
304     gnuOptions [ind].name    = cliOptions[ind].name;
305     gnuOptions [ind].has_arg = cliOptions[ind].has_arg;
306     gnuOptions [ind].flag    = 0;
307     gnuOptions [ind].val     = cliOptions[ind].val;
308   }
309
310   // get all options from command line
311   while ((optc = getopt_long (argc, argv, "vsp?", gnuOptions, &optionIndex))
312         != EOF)
313   {
314     switch (optc)
315     {
316      case SET_VERBOSE:
317        verbose = 1;
318        break;
319
320     case SET_TCP_PORT:
321        if (optarg == 0) goto needValueForOption;
322        if (!sscanf (optarg, "%d", &cliconfig.httpdPort)) goto notAnInteger;
323        break;
324        
325     case SET_APITIMEOUT:
326        if (optarg == 0) goto needValueForOption;
327        if (!sscanf (optarg, "%d", &cliconfig.apiTimeout)) goto notAnInteger;
328        break;
329
330     case SET_CNTXTIMEOUT:
331        if (optarg == 0) goto needValueForOption;
332        if (!sscanf (optarg, "%d", &cliconfig.cntxTimeout)) goto notAnInteger;
333        break;
334
335     case SET_ROOT_DIR:
336        if (optarg == 0) goto needValueForOption;
337        cliconfig.rootdir   = optarg;
338        if (verbose) fprintf(stderr, "Forcing Rootdir=%s\n",cliconfig.rootdir);
339        break;       
340        
341     case SET_ROOT_BASE:
342        if (optarg == 0) goto needValueForOption;
343        cliconfig.rootbase   = optarg;
344        if (verbose) fprintf(stderr, "Forcing Rootbase=%s\n",cliconfig.rootbase);
345        break;
346
347     case SET_ROOT_API:
348        if (optarg == 0) goto needValueForOption;
349        cliconfig.rootapi   = optarg;
350        if (verbose) fprintf(stderr, "Forcing Rootapi=%s\n",cliconfig.rootapi);
351        break;
352        
353     case SET_ROOT_ALIAS:
354        if (optarg == 0) goto needValueForOption;
355        if (aliascount < MAX_ALIAS) {
356             aliasdir[aliascount].url  = strsep(&optarg,":");
357             if (optarg == NULL) {
358               fprintf(stderr, "missing ':' in alias %s, ignored\n", aliasdir[aliascount].url);
359             } else {
360               aliasdir[aliascount].path = optarg;
361               aliasdir[aliascount].len  = strlen(aliasdir[aliascount].url);
362               if (verbose) fprintf(stderr, "Alias url=%s path=%s\n", aliasdir[aliascount].url, aliasdir[aliascount].path);
363               aliascount++;
364             }
365        } else {
366            fprintf(stderr, "Too many aliases [max:%d] %s ignored\n", MAX_ALIAS, optarg);
367        }     
368        break;
369        
370     case SET_AUTH_TOKEN:
371        if (optarg == 0) goto needValueForOption;
372        cliconfig.token   = optarg;
373        break;
374
375     case SET_LDPATH:
376        if (optarg == 0) goto needValueForOption;
377        cliconfig.ldpaths = optarg;
378        break;
379
380     case SET_PID_FILE:
381        if (optarg == 0) goto needValueForOption;
382        cliconfig.pidfile   = optarg;
383        break;
384
385     case SET_SESSION_DIR:
386        if (optarg == 0) goto needValueForOption;
387        cliconfig.sessiondir   = optarg;
388        break;
389
390     case  SET_CONFIG_FILE:
391        if (optarg == 0) goto needValueForOption;
392        cliconfig.configfile   = optarg;
393        break;
394
395     case  SET_CACHE_TO:
396        if (optarg == 0) goto needValueForOption;
397        if (!sscanf (optarg, "%d", &cliconfig.cacheTimeout)) goto notAnInteger;
398        break;
399
400     case SET_CONFIG_EXIT:
401        if (optarg != 0) goto noValueForOption;
402        session->configsave  = 1;
403        session->forceexit   = 1;
404        break;
405
406     case SET_CONFIG_SAVE:
407        if (optarg != 0) goto noValueForOption;
408        session->configsave  = 1;
409        break;
410
411     case SET_USERID:
412        if (optarg == 0) goto needValueForOption;
413        cliconfig.setuid = optarg;
414        break;
415
416     case SET_FAKE_MOD:
417        if (optarg != 0) goto noValueForOption;
418        session->fakemod  = 1;
419        break;
420
421     case SET_FORGROUND:
422        if (optarg != 0) goto noValueForOption;
423        session->foreground  = 1;
424        break;
425
426     case SET_BACKGROUND:
427        if (optarg != 0) goto noValueForOption;
428        session->background  = 1;
429        break;
430
431      case KILL_PREV_REST:
432        if (optarg != 0) goto noValueForOption;
433        session->killPrevious  = 1;
434        break;
435
436      case KILL_PREV_EXIT:
437        if (optarg != 0) goto noValueForOption;
438        session->killPrevious  = 2;
439        break;
440
441     case SET_MODE:
442        if (optarg == 0) goto needValueForOption;
443        if (!strcmp(optarg, "local")) cliconfig.mode = AFB_MODE_LOCAL;
444        else if (!strcmp(optarg, "remote")) cliconfig.mode = AFB_MODE_REMOTE;
445        else if (!strcmp(optarg, "global")) cliconfig.mode = AFB_MODE_GLOBAL;
446        else goto badMode;
447        break;
448
449     case SET_READYFD:
450        if (optarg == 0) goto needValueForOption;
451        if (!sscanf (optarg, "%u", &session->readyfd)) goto notAnInteger;
452        break;
453
454     case DISPLAY_VERSION:
455        if (optarg != 0) goto noValueForOption;
456        printVersion();
457        goto normalExit;
458
459     case DISPLAY_HELP:
460      default:
461        printHelp(programName);
462        goto normalExit;
463
464     }
465   }
466  
467   // if exist merge config file with CLI arguments
468   configLoadFile  (session, &cliconfig);
469   initPlugins(session);
470
471   // ------------------ sanity check ----------------------------------------
472   if  ((session->background) && (session->foreground)) {
473     fprintf (stderr, "%s ERR: cannot select foreground & background at the same time\n",configTime());
474      exit (1);
475   }
476
477   // ------------------ Some useful default values -------------------------
478   if  ((session->background == 0) && (session->foreground == 0)) session->foreground=1;
479
480   // open syslog if ever needed
481   openlog("AFB-log", 0, LOG_DAEMON);
482
483   // -------------- Try to kill any previous process if asked ---------------------
484   if (session->killPrevious) {
485     pid = readPidFile (session->config);  // enforce commandline option
486     switch (pid) {
487     case -1:
488       fprintf (stderr, "%s ERR:main --kill ignored no PID file [%s]\n",configTime(), session->config->pidfile);
489       break;
490     case 0:
491       fprintf (stderr, "%s ERR:main --kill ignored no active AFB process\n",configTime());
492       break;
493     default:
494       status = kill (pid,SIGINT );
495       if (status == 0) {
496              if (verbose) printf ("%s INF:main signal INTR sent to pid:%d \n", configTime(), pid);
497       } else {
498          // try kill -9
499          status = kill (pid,9);
500          if (status != 0)  fprintf (stderr, "%s ERR:main failled to killed pid=%d \n",configTime(), pid);
501       }
502     } // end switch pid
503
504     if (session->killPrevious >= 2) goto normalExit;
505   } // end killPrevious
506
507
508   // ------------------ clean exit on CTR-C signal ------------------------
509   if (signal (SIGINT, signalQuit) == SIG_ERR) {
510     fprintf (stderr, "%s Quit Signal received.",configTime());
511     return 1;
512   }
513
514   // save exitPoint context when returning from longjmp closeSession and exit
515   status = setjmp (exitPoint); // return !+ when coming from longjmp
516   if (status != 0) {
517     if (verbose) printf ("INF:main returning from longjump after signal [%d]\n", status);
518     closeSession (session);
519     goto exitOnSignal;
520   }
521
522   // let's run this program with a low priority
523   status=nice (20);
524
525   // ------------------ Finaly Process Commands -----------------------------
526    // if --save then store config on disk upfront
527    if (session->configsave) configStoreFile (session);
528    if (session->forceexit)  exit (0);
529
530     if (session->config->setuid) {
531         int err;
532         struct passwd *passwd;
533         passwd=getpwnam(session->config->setuid);
534         
535         if (passwd == NULL) goto errorSetuid;
536         
537         err = setuid(passwd->pw_uid);
538         if (err) goto errorSetuid;
539     }
540
541     // let's not take the risk to run as ROOT
542     //if (getuid() == 0)  goto errorNoRoot;
543
544     // check session dir and create if it does not exist
545     if (sessionCheckdir (session) != AFB_SUCCESS) goto errSessiondir;
546     if (verbose) fprintf (stderr, "AFB:notice Init config done\n");
547
548
549
550     // ---- run in foreground mode --------------------
551     if (session->foreground) {
552
553         if (verbose) fprintf (stderr,"AFB:notice Foreground mode\n");
554
555         // write a pid file for --kill-previous and --raise-debug option
556         status = writePidFile (session->config, getpid());
557         if (status == -1) goto errorPidFile;
558
559         // enter listening loop in foreground
560         listenLoop(session);
561         goto exitInitLoop;
562   } // end foreground
563
564
565   // --------- run in background mode -----------
566   if (session->background) {
567
568        // if (status != 0) goto errorCommand;
569       if (verbose) printf ("AFB: Entering background mode\n");
570
571       // open /dev/console to redirect output messAFBes
572       consoleFD = open(session->config->console, O_WRONLY | O_APPEND | O_CREAT , 0640);
573       if (consoleFD < 0) goto errConsole;
574
575       // fork process when running background mode
576       pid = fork ();
577
578       // son process get all data in standalone mode
579       if (pid == 0) {
580
581              printf ("\nAFB: background mode [pid:%d console:%s]\n", getpid(),session->config->console);
582              if (verbose) printf ("AFB:info use '%s --restart --rootdir=%s # [--pidfile=%s] to restart daemon\n", programName,session->config->rootdir, session->config->pidfile);
583
584          // redirect default I/O on console
585          close (2); status=dup(consoleFD);  // redirect stderr
586          close (1); status=dup(consoleFD);  // redirect stdout
587          close (0);           // no need for stdin
588          close (consoleFD);
589
590          setsid();   // allow father process to fully exit
591              sleep (2);  // allow main to leave and release port
592
593          fprintf (stderr, "----------------------------\n");
594          fprintf (stderr, "%s INF:main background pid=%d\n", configTime(), getpid());
595          fflush  (stderr);
596
597          // if everything look OK then look forever
598          syslog (LOG_ERR, "AFB: Entering infinite loop in background mode");
599
600          // should normally never return from this loop
601          listenLoop(session);
602          syslog (LOG_ERR, "AFB:FAIL background infinite loop exited check [%s]\n", session->config->console);
603
604          goto exitInitLoop;
605       }
606
607       // if fail nothing much to do
608       if (pid == -1) goto errorFork;
609
610       // fork worked and we are in father process
611       status = writePidFile (session->config, pid);
612       if (status == -1) goto errorPidFile;
613
614       // we are in father process, we don't need this one
615       _exit (0);
616
617   } // end background-foreground
618
619 normalExit:
620   closeSession (session);   // try to close everything before leaving
621   if (verbose) printf ("\n---- Application Framework Binder Normal End ------\n");
622   exit(0);
623
624 // ------------- Fatal ERROR display error and quit  -------------
625 errorSetuid:
626   fprintf (stderr,"\nERR:AFB-daemon Failed to change UID to username=[%s]\n\n", session->config->setuid);
627   exit (1);
628   
629 //errorNoRoot:
630 //  fprintf (stderr,"\nERR:AFB-daemon Not allow to run as root [use --seteuid=username option]\n\n");
631 //  exit (1);
632
633 errorPidFile:
634   fprintf (stderr,"\nERR:AFB-daemon Failed to write pid file [%s]\n\n", session->config->pidfile);
635   exit (1);
636
637 errorFork:
638   fprintf (stderr,"\nERR:AFB-daemon Failed to fork son process\n\n");
639   exit (1);
640
641 needValueForOption:
642   fprintf (stderr,"\nERR:AFB-daemon option [--%s] need a value i.e. --%s=xxx\n\n"
643           ,gnuOptions[optionIndex].name, gnuOptions[optionIndex].name);
644   exit (1);
645
646 noValueForOption:
647   fprintf (stderr,"\nERR:AFB-daemon option [--%s] don't take value\n\n"
648           ,gnuOptions[optionIndex].name);
649   exit (1);
650
651 notAnInteger:
652   fprintf (stderr,"\nERR:AFB-daemon option [--%s] requirer an interger i.e. --%s=9\n\n"
653           ,gnuOptions[optionIndex].name, gnuOptions[optionIndex].name);
654   exit (1);
655
656 badMode:
657   fprintf (stderr,"\nERR:AFB-daemon option [--%s] only accepts local, global or remote.\n\n"
658           ,gnuOptions[optionIndex].name);
659   exit (1);
660
661 exitOnSignal:
662   fprintf (stderr,"\n%s INF:AFB-daemon pid=%d received exit signal (Hopefully crtl-C or --kill-previous !!!)\n\n"
663                  ,configTime(), getpid());
664   exit (1);
665
666 errConsole:
667   fprintf (stderr,"\nERR:AFB-daemon cannot open /dev/console (use --foreground)\n\n");
668   exit (1);
669
670 errSessiondir:
671   fprintf (stderr,"\nERR:AFB-daemon cannot read/write session dir\n\n");
672   exit (1);
673
674 exitInitLoop:
675   // try to unlink pid file if any
676   if (session->background && session->config->pidfile != NULL)  unlink (session->config->pidfile);
677   exit (1);
678
679 } /* END AFB-daemon() */
680