- // if exist merge config file with CLI arguments
- configLoadFile (session, &cliconfig);
- initPlugins(session);
-
- // ------------------ 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;
- struct passwd *passwd;
- passwd=getpwnam(session->config->setuid);
-
- if (passwd == NULL) goto errorSetuid;
-
- err = setuid(passwd->pw_uid);
- if (err) goto errorSetuid;
- }
-
- // let's not take the risk to run as ROOT
- if (getuid() == 0) goto errorNoRoot;
-
- // check session dir and create if it does not exist
- if (sessionCheckdir (session) != AFB_SUCCESS) goto errSessiondir;
- if (verbose) fprintf (stderr, "AFB:notice Init config done\n");