work in progress
[src/app-framework-binder.git] / src / main.c
index fdf8a34..2401043 100644 (file)
  * limitations under the License.
  */
 
-#include <syslog.h>
+#define _GNU_SOURCE
+#include <stdio.h>
+#include <string.h>
+#include <getopt.h>
 #include <setjmp.h>
 #include <signal.h>
-#include <getopt.h>
-#include <pwd.h>
+#include <syslog.h>
+#include <fcntl.h>
 #include <sys/stat.h>
 #include <sys/types.h>
 
+/*
+#include <pwd.h>
+#include <pthread.h>
+*/
+
 #include "local-def.h"
+#include "afb-apis.h"
+#include "session.h"
 
 #if !defined(PLUGIN_INSTALL_DIR)
 #error "you should define PLUGIN_INSTALL_DIR"
@@ -414,6 +424,36 @@ void signalQuit (int signum) {
 }
 
 
+/*----------------------------------------------------------
+ | Error signals
+ |
+ +--------------------------------------------------------- */
+__thread sigjmp_buf *error_handler;
+static void signalError(int signum)
+{
+       sigset_t sigset;
+
+       // unlock signal to allow a new signal to come
+       if (error_handler != NULL) {
+               sigemptyset(&sigset);
+               sigaddset(&sigset, signum);
+               sigprocmask(SIG_UNBLOCK, &sigset, 0);
+               longjmp(*error_handler, signum);
+       }
+}
+
+static void install_error_handlers()
+{
+       int i, signals[] = { SIGALRM, SIGSEGV, SIGFPE, 0 };
+
+       for (i = 0; signals[i] != 0; i++) {
+               if (signal(signals[i], signalError) == SIG_ERR) {
+                       fprintf(stderr, "Signal handler error\n");
+                       exit(1);
+               }
+       }
+}
+
 /*----------------------------------------------------------
  | listenLoop
  |   Main listening HTTP loop
@@ -503,8 +543,6 @@ int main(int argc, char *argv[])  {
   on_exit(closeSession, session);
   parse_arguments(argc, argv, session);
 
-  initPlugins(session);
-
   // ------------------ sanity check ----------------------------------------
   if  ((session->background) && (session->foreground)) {
     fprintf (stderr, "ERR: cannot select foreground & background at the same time\n");
@@ -515,6 +553,13 @@ int main(int argc, char *argv[])  {
      exit (1);
   }
 
+  if (session->config->ldpaths) 
+    afb_apis_add_pathset(session->config->ldpaths);
+
+  ctxStoreInit(CTX_NBCLIENTS, session->config->cntxTimeout, afb_apis_count(), session->config->token);
+
+  install_error_handlers();
+
   // ------------------ Some useful default values -------------------------
   if  ((session->background == 0) && (session->foreground == 0)) session->foreground=1;
 
@@ -532,11 +577,13 @@ int main(int argc, char *argv[])  {
   // let's not take the risk to run as ROOT
   //if (getuid() == 0)  goto errorNoRoot;
 
+#if defined(ALLOWS_SESSION_FILES)
   // check session dir and create if it does not exist
   if (sessionCheckdir (session) != AFB_SUCCESS) {
        fprintf (stderr,"\nERR: AFB-daemon cannot read/write session dir\n\n");
        exit (1);
   }
+#endif
   if (verbose) fprintf (stderr, "AFB: notice Init config done\n");
 
   // ---- run in foreground mode --------------------