- goto normalExit;
-
- }
- }
- // Create session config
- configInit (/* session & config are initialized globally */);
-
- // if exist merge config file with CLI arguments
- configLoadFile (session, &cliconfig);
-
- // ------------------ sanity check ----------------------------------------
- if ((session->background) && (session->foreground)) {
- fprintf (stderr, "%s ERR: cannot select foreground & background at the same time\n",configTime());
- exit (-1);
- }
-
- // ------------------ Some useful default values -------------------------
- if ((session->background == 0) && (session->foreground == 0)) session->foreground=1;
-
- // open syslog if ever needed
- openlog("AGB-log", 0, LOG_DAEMON);
-
- // -------------- Try to kill any previsou process if asked ---------------------
- if (session->killPrevious) {
- pid = readPidFile (session->config); // enforce commandline option
- switch (pid) {
- case -1:
- fprintf (stderr, "%s ERR:main --kill ignored no PID file [%s]\n",configTime(), session->config->pidfile);
- break;
- case 0:
- fprintf (stderr, "%s ERR:main --kill ignored no active AFB process\n",configTime());
- break;
- default:
- status = kill (pid,SIGINT );
- if (status == 0) {
- if (verbose) printf ("%s INF:main signal INTR sent to pid:%d \n", configTime(), pid);
- } else {
- // try kill -9
- status = kill (pid,9);
- if (status != 0) fprintf (stderr, "%s ERR:main failled to killed pid=%d \n",configTime(), pid);
- }
- } // end switch pid
-
- if (session->killPrevious >= 2) goto normalExit;
- } // end killPrevious
-
-
- // ------------------ clean exit on CTR-C signal ------------------------
- if (signal (SIGINT, signalQuit) == SIG_ERR) {
- fprintf (stderr, "%s Quit Signal received.",configTime());
- return (-1);
- }
-
- // save exitpoint context when returning from longjmp closeSession and exit
- status = setjmp (exitpoint); // return !+ when coming from longjmp
- if (status != 0) {
- if (verbose) printf ("INF:main returning from longjump after signal [%d]\n", status);
- closeSession (session);
- goto exitOnSignal;
- }
-
- // let's run this program with a low priority
- status=nice (20);
-
-
- // ------------------ Finaly Process Commands -----------------------------
-
-
-
- // if --save then store config on disk upfront
- if (session->configsave) configStoreFile (session);
- if (session->forceexit) exit (0);
-
- if (session->config->setuid) {
- int err;
-
- err = setuid(session->config->setuid);
- if (err) fprintf (stderr, "Fail to change program cardid error=%s", strerror(err));