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