X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fmain.c;h=37183e72e36a5c7ea92d579a43f7e7e05dbbd105;hb=04331cc45e03325c6470bc5285d5c09843b24afd;hp=ccff5fa8daff2630332c0c42692c3e02792c18cb;hpb=9c9253cd9106e656195aba6f0cadb29f6a940fe1;p=src%2Fapp-framework-binder.git diff --git a/src/main.c b/src/main.c index ccff5fa8..37183e72 100644 --- a/src/main.c +++ b/src/main.c @@ -16,15 +16,23 @@ * limitations under the License. */ -#include +#define _GNU_SOURCE +#include +#include +#include #include #include -#include -#include +#include +#include #include #include +#include "afb-plugin.h" + #include "local-def.h" +#include "afb-apis.h" +#include "session.h" +#include "verbose.h" #if !defined(PLUGIN_INSTALL_DIR) #error "you should define PLUGIN_INSTALL_DIR" @@ -206,57 +214,6 @@ static AFB_error config_set_default (AFB_session * session) fprintf (stderr,"Example:\n %s\\\n --verbose --port=1234 --token='azerty' --ldpaths=build/plugins:/usr/lib64/agl/plugins\n", name); } // end printHelp -/*---------------------------------------------------------- - | closeSession - | try to close everything before leaving - +--------------------------------------------------------- */ -static void closeSession (int status, void *data) { -// AFB_session *session = data; -} - -/*---------------------------------------------------------- - | timeout signalQuit - | - +--------------------------------------------------------- */ -void signalQuit (int signum) { - - sigset_t sigset; - - // unlock timeout signal to allow a new signal to come - sigemptyset (&sigset); - sigaddset (&sigset, SIGABRT); - sigprocmask (SIG_UNBLOCK, &sigset, 0); - - fprintf (stderr, "ERR: Received signal quit\n"); - syslog (LOG_ERR, "Daemon got kill3 & quit [please report bug]"); - exit(1); -} - - -/*---------------------------------------------------------- - | listenLoop - | Main listening HTTP loop - +--------------------------------------------------------- */ -static void listenLoop (AFB_session *session) { - AFB_error err; - - // ------ Start httpd server - - err = httpdStart (session); - if (err != AFB_SUCCESS) return; - - if (session->readyfd != 0) { - static const char readystr[] = "READY=1"; - write(session->readyfd, readystr, sizeof(readystr) - 1); - close(session->readyfd); - } - - // infinite loop - httpdLoop(session); - - fprintf (stderr, "hoops returned from infinite loop [report bug]\n"); -} - /*--------------------------------------------------------- | main | Parse option and launch action @@ -299,7 +256,7 @@ static void parse_arguments(int argc, char *argv[], AFB_session *session) switch (optc) { case SET_VERBOSE: - verbose = 1; + verbosity++; break; case SET_TCP_PORT: @@ -320,19 +277,19 @@ static void parse_arguments(int argc, char *argv[], AFB_session *session) case SET_ROOT_DIR: if (optarg == 0) goto needValueForOption; session->config->rootdir = optarg; - if (verbose) fprintf(stderr, "Forcing Rootdir=%s\n",session->config->rootdir); + if (verbosity) fprintf(stderr, "Forcing Rootdir=%s\n",session->config->rootdir); break; case SET_ROOT_BASE: if (optarg == 0) goto needValueForOption; session->config->rootbase = optarg; - if (verbose) fprintf(stderr, "Forcing Rootbase=%s\n",session->config->rootbase); + if (verbosity) fprintf(stderr, "Forcing Rootbase=%s\n",session->config->rootbase); break; case SET_ROOT_API: if (optarg == 0) goto needValueForOption; session->config->rootapi = optarg; - if (verbose) fprintf(stderr, "Forcing Rootapi=%s\n",session->config->rootapi); + if (verbosity) fprintf(stderr, "Forcing Rootapi=%s\n",session->config->rootapi); break; case SET_ALIAS: @@ -344,7 +301,7 @@ static void parse_arguments(int argc, char *argv[], AFB_session *session) } else { aliasdir[aliascount].path = optarg; aliasdir[aliascount].len = strlen(aliasdir[aliascount].url); - if (verbose) fprintf(stderr, "Alias url=%s path=%s\n", aliasdir[aliascount].url, aliasdir[aliascount].path); + if (verbosity) fprintf(stderr, "Alias url=%s path=%s\n", aliasdir[aliascount].url, aliasdir[aliascount].path); aliascount++; } } else { @@ -438,6 +395,134 @@ badMode: exit (1); } +/*---------------------------------------------------------- + | closeSession + | try to close everything before leaving + +--------------------------------------------------------- */ +static void closeSession (int status, void *data) { + /* AFB_session *session = data; */ +} + +/*---------------------------------------------------------- + | timeout signalQuit + | + +--------------------------------------------------------- */ +void signalQuit (int signum) { + + sigset_t sigset; + + // unlock timeout signal to allow a new signal to come + sigemptyset (&sigset); + sigaddset (&sigset, SIGABRT); + sigprocmask (SIG_UNBLOCK, &sigset, 0); + + fprintf (stderr, "ERR: Received signal quit\n"); + syslog (LOG_ERR, "Daemon got kill3 & quit [please report bug]"); + exit(1); +} + + +/*---------------------------------------------------------- + | 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 + +--------------------------------------------------------- */ +static void listenLoop (AFB_session *session) { + AFB_error err; + + // ------ Start httpd server + + err = httpdStart (session); + if (err != AFB_SUCCESS) return; + + if (session->readyfd != 0) { + static const char readystr[] = "READY=1"; + write(session->readyfd, readystr, sizeof(readystr) - 1); + close(session->readyfd); + } + + // infinite loop + httpdLoop(session); + + fprintf (stderr, "hoops returned from infinite loop [report bug]\n"); +} + +/*---------------------------------------------------------- + | daemonize + | set the process in background + +--------------------------------------------------------- */ +static void daemonize(AFB_session *session) +{ + int consoleFD; + int pid; + + // open /dev/console to redirect output messAFBes + consoleFD = open(session->config->console, O_WRONLY | O_APPEND | O_CREAT , 0640); + if (consoleFD < 0) { + fprintf (stderr,"\nERR: AFB-daemon cannot open /dev/console (use --foreground)\n\n"); + exit (1); + } + + // fork process when running background mode + pid = fork (); + + // if fail nothing much to do + if (pid == -1) { + fprintf (stderr,"\nERR: AFB-daemon Failed to fork son process\n\n"); + exit (1); + } + + // if in father process, just leave + if (pid != 0) _exit (0); + + // son process get all data in standalone mode + printf ("\nAFB: background mode [pid:%d console:%s]\n", getpid(),session->config->console); + + // redirect default I/O on console + close (2); dup(consoleFD); // redirect stderr + close (1); dup(consoleFD); // redirect stdout + close (0); // no need for stdin + close (consoleFD); + +#if 0 + setsid(); // allow father process to fully exit + sleep (2); // allow main to leave and release port +#endif + + fprintf (stderr, "----------------------------\n"); + fprintf (stderr, "INF: main background pid=%d\n", getpid()); + fflush (stderr); +} + /*--------------------------------------------------------- | main | Parse option and launch action @@ -445,8 +530,6 @@ badMode: int main(int argc, char *argv[]) { AFB_session *session; - int consoleFD; - int pid; // open syslog if ever needed openlog("afb-daemon", 0, LOG_DAEMON); @@ -458,8 +541,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"); @@ -470,6 +551,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; @@ -487,88 +575,40 @@ 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) goto errSessiondir; - if (verbose) fprintf (stderr, "AFB: notice Init config done\n"); + if (sessionCheckdir (session) != AFB_SUCCESS) { + fprintf (stderr,"\nERR: AFB-daemon cannot read/write session dir\n\n"); + exit (1); + } +#endif + if (verbosity) fprintf (stderr, "AFB: notice Init config done\n"); // ---- run in foreground mode -------------------- if (session->foreground) { - if (verbose) fprintf (stderr,"AFB: notice Foreground mode\n"); + if (verbosity) fprintf (stderr,"AFB: notice Foreground mode\n"); - // enter listening loop in foreground - listenLoop(session); - goto exitInitLoop; } // end foreground - // --------- run in background mode ----------- if (session->background) { - // if (status != 0) goto errorCommand; - if (verbose) printf ("AFB: Entering background mode\n"); + if (verbosity) printf ("AFB: Entering background mode\n"); - // open /dev/console to redirect output messAFBes - consoleFD = open(session->config->console, O_WRONLY | O_APPEND | O_CREAT , 0640); - if (consoleFD < 0) goto errConsole; - - // fork process when running background mode - pid = fork (); - - // son process get all data in standalone mode - if (pid == 0) { - - printf ("\nAFB: background mode [pid:%d console:%s]\n", getpid(),session->config->console); - - // redirect default I/O on console - close (2); dup(consoleFD); // redirect stderr - close (1); dup(consoleFD); // redirect stdout - close (0); // no need for stdin - close (consoleFD); - - setsid(); // allow father process to fully exit - sleep (2); // allow main to leave and release port - - fprintf (stderr, "----------------------------\n"); - fprintf (stderr, "INF: main background pid=%d\n", getpid()); - fflush (stderr); + daemonize(session); // if everything look OK then look forever syslog (LOG_ERR, "AFB: Entering infinite loop in background mode"); - // should normally never return from this loop - listenLoop(session); - syslog (LOG_ERR, "AFB: FAIL background infinite loop exited check [%s]\n", session->config->console); - - goto exitInitLoop; - } - - // if fail nothing much to do - if (pid == -1) goto errorFork; - - // we are in father process, we don't need this one - _exit (0); } // end background-foreground - if (verbose) printf ("\n---- Application Framework Binder Normal End ------\n"); - exit(0); - -// ------------- Fatal ERROR display error and quit ------------- -errorFork: - fprintf (stderr,"\nERR: AFB-daemon Failed to fork son process\n\n"); - exit (1); -errConsole: - fprintf (stderr,"\nERR: AFB-daemon cannot open /dev/console (use --foreground)\n\n"); - exit (1); - -errSessiondir: - fprintf (stderr,"\nERR: AFB-daemon cannot read/write session dir\n\n"); - exit (1); + listenLoop(session); + if (verbosity) printf ("\n---- Application Framework Binder Normal End ------\n"); + exit(0); -exitInitLoop: - exit (1); }