X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fmain.c;h=9769f5269d9e71dc33d3ac5eed06c50cef35d123;hb=7192018f97f6406ccc6711fc5d76cea63ec080ec;hp=aa0e9bcdcc6d7206bd71d80101ba56e205ae7a48;hpb=3bce4a6a8648c91b6ddea478116b3b0679ef4648;p=src%2Fapp-framework-binder.git diff --git a/src/main.c b/src/main.c index aa0e9bcd..9769f526 100644 --- a/src/main.c +++ b/src/main.c @@ -31,6 +31,8 @@ #include #include +#define AFB_VERSION "0.1" + static sigjmp_buf exitPoint; // context save for set/longjmp /*---------------------------------------------------------- @@ -40,7 +42,7 @@ static sigjmp_buf exitPoint; // context save for set/longjmp static void printVersion (void) { fprintf (stderr,"\n----------------------------------------- \n"); - fprintf (stderr,"| AFB [Application Framework Binder] version=%s |\n", AJQ_VERSION); + fprintf (stderr,"| AFB [Application Framework Binder] version=%s |\n", AFB_VERSION); fprintf (stderr,"----------------------------------------- \n"); fprintf (stderr,"| Copyright(C) 2015 Fulup Ar Foll /IoT.bzh [fulup -at- iot.bzh]\n"); fprintf (stderr,"| AFB comes with ABSOLUTELY NO WARRANTY.\n"); @@ -50,35 +52,37 @@ static sigjmp_buf exitPoint; // context save for set/longjmp // Define command line option - #define SET_VERBOSE 101 - #define SET_BACKGROUND 105 - #define SET_FORGROUND 106 - #define KILL_PREV_EXIT 107 - #define KILL_PREV_REST 108 - #define SET_FAKE_MOD 109 - - #define SET_TCP_PORT 120 - #define SET_ROOT_DIR 121 - #define SET_ROOT_BASE 122 - #define SET_ROOT_API 123 - #define SET_ROOT_ALIAS 124 - - #define SET_CACHE_TO 130 - #define SET_USERID 131 - #define SET_PID_FILE 132 - #define SET_SESSION_DIR 133 - #define SET_CONFIG_FILE 134 - #define SET_CONFIG_SAVE 135 - #define SET_CONFIG_EXIT 138 - - #define SET_SMACK 140 - #define SET_AUTH_TOKEN 141 - #define SET_PLUGINS 142 - #define SET_APITIMEOUT 143 - #define SET_CNTXTIMEOUT 144 - - #define DISPLAY_VERSION 150 - #define DISPLAY_HELP 151 +#define SET_VERBOSE 101 +#define SET_BACKGROUND 105 +#define SET_FORGROUND 106 +#define KILL_PREV_EXIT 107 +#define KILL_PREV_REST 108 +#define SET_FAKE_MOD 109 + +#define SET_TCP_PORT 120 +#define SET_ROOT_DIR 121 +#define SET_ROOT_BASE 122 +#define SET_ROOT_API 123 +#define SET_ROOT_ALIAS 124 + +#define SET_CACHE_TO 130 +#define SET_USERID 131 +#define SET_PID_FILE 132 +#define SET_SESSION_DIR 133 +#define SET_CONFIG_FILE 134 +#define SET_CONFIG_SAVE 135 +#define SET_CONFIG_EXIT 138 + +#define SET_AUTH_TOKEN 141 +#define SET_LDPATH 142 +#define SET_APITIMEOUT 143 +#define SET_CNTXTIMEOUT 144 + +#define DISPLAY_VERSION 150 +#define DISPLAY_HELP 151 + +#define SET_MODE 160 +#define SET_READYFD 161 // Supported option @@ -107,13 +111,15 @@ static AFB_options cliOptions [] = { {SET_CONFIG_SAVE ,0,"save" , "Save config on disk [default no]"}, {SET_CONFIG_EXIT ,0,"saveonly" , "Save config on disk and then exit"}, - {SET_SMACK ,1,"smack" , "Set Smack Label [default demo]"}, - {SET_PLUGINS ,1,"mods" , "Enable module [default all]"}, + {SET_LDPATH ,1,"ldpaths" , "Load Plugins from dir1:dir2:... [default = PLUGIN_INSTALL_DIR"}, {SET_AUTH_TOKEN ,1,"token" , "Initial Secret [default=no-session, --token="" for session without authentication]"}, {DISPLAY_VERSION ,0,"version" , "Display version and copyright"}, {DISPLAY_HELP ,0,"help" , "Display this help"}, - {0, 0, 0} + + {SET_MODE ,1,"mode" , "set the mode: either local, remote or global"}, + {SET_READYFD ,1,"readyfd" , "set the #fd to signal when ready"}, + {0, 0, NULL, NULL} }; static AFB_aliasdir aliasdir[MAX_ALIAS]; @@ -159,7 +165,7 @@ void signalQuit (int signum) { fprintf (stderr," --%-15s %s\n", command, cliOptions[ind].help); } } - fprintf (stderr,"Example:\n %s\\\n --verbose --port=1234 --smack=xxxx --token='azerty' --mods=alsa:dbus\n", name); + fprintf (stderr,"Example:\n %s\\\n --verbose --port=1234 --token='azerty' --ldpaths=build/plugins:/usr/lib64/agl/plugins\n", name); } // end printHelp /*---------------------------------------------------------- @@ -240,6 +246,12 @@ static void listenLoop (AFB_session *session) { 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); @@ -280,7 +292,7 @@ int main(int argc, char *argv[]) { // build GNU getopt info from cliOptions nbcmd = sizeof (cliOptions) / sizeof (AFB_options); - gnuOptions = malloc (sizeof (ggcOption) * nbcmd); + gnuOptions = malloc (sizeof (ggcOption) * (unsigned)nbcmd); for (ind=0; ind < nbcmd;ind++) { gnuOptions [ind].name = cliOptions[ind].name; gnuOptions [ind].has_arg = cliOptions[ind].has_arg; @@ -335,30 +347,27 @@ int main(int argc, char *argv[]) { if (optarg == 0) goto needValueForOption; if (aliascount < MAX_ALIAS) { aliasdir[aliascount].url = strsep(&optarg,":"); - aliasdir[aliascount].path = strsep(&optarg,":"); - aliasdir[aliascount].len = strlen(aliasdir[aliascount].url); - if (verbose) fprintf(stderr, "Alias url=%s path=%s\n", aliasdir[aliascount].url, aliasdir[aliascount].path); - aliascount++; + if (optarg == NULL) { + fprintf(stderr, "missing ':' in alias %s, ignored\n", aliasdir[aliascount].url); + } 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); + aliascount++; + } } else { - fprintf(stderr, "Too many aliases [max:%s] %s ignored\n", optarg, MAX_ALIAS-1); + fprintf(stderr, "Too many aliases [max:%d] %s ignored\n", MAX_ALIAS, optarg); } break; - case SET_SMACK: - if (optarg == 0) goto needValueForOption; - fprintf (stderr, "Not Implemented yet\n"); - cliconfig.smack = optarg; - break; - case SET_AUTH_TOKEN: if (optarg == 0) goto needValueForOption; cliconfig.token = optarg; break; - case SET_PLUGINS: + case SET_LDPATH: if (optarg == 0) goto needValueForOption; - fprintf (stderr, "Not Implemented yet\n"); - cliconfig.plugins = optarg; + cliconfig.ldpaths = optarg; break; case SET_PID_FILE: @@ -394,7 +403,7 @@ int main(int argc, char *argv[]) { case SET_USERID: if (optarg == 0) goto needValueForOption; - if (!sscanf (optarg, "%s", &cliconfig.setuid)) goto notAnInteger; + cliconfig.setuid = optarg; break; case SET_FAKE_MOD: @@ -422,6 +431,19 @@ int main(int argc, char *argv[]) { session->killPrevious = 2; break; + case SET_MODE: + if (optarg == 0) goto needValueForOption; + if (!strcmp(optarg, "local")) cliconfig.mode = AFB_MODE_LOCAL; + else if (!strcmp(optarg, "remote")) cliconfig.mode = AFB_MODE_REMOTE; + else if (!strcmp(optarg, "global")) cliconfig.mode = AFB_MODE_GLOBAL; + else goto badMode; + break; + + case SET_READYFD: + if (optarg == 0) goto needValueForOption; + if (!sscanf (optarg, "%u", &session->readyfd)) goto notAnInteger; + break; + case DISPLAY_VERSION: if (optarg != 0) goto noValueForOption; printVersion(); @@ -432,7 +454,7 @@ int main(int argc, char *argv[]) { printHelp(programName); goto normalExit; - } + } } // if exist merge config file with CLI arguments @@ -449,9 +471,9 @@ int main(int argc, char *argv[]) { if ((session->background == 0) && (session->foreground == 0)) session->foreground=1; // open syslog if ever needed - openlog("AGB-log", 0, LOG_DAEMON); + openlog("AFB-log", 0, LOG_DAEMON); - // -------------- Try to kill any previsou process if asked --------------------- + // -------------- Try to kill any previous process if asked --------------------- if (session->killPrevious) { pid = readPidFile (session->config); // enforce commandline option switch (pid) { @@ -583,14 +605,14 @@ int main(int argc, char *argv[]) { if (status == -1) goto errorPidFile; // we are in father process, we don't need this one - exit (0); + _exit (0); } // end background-foreground normalExit: closeSession (session); // try to close everything before leaving if (verbose) printf ("\n---- Application Framework Binder Normal End ------\n"); - exit (0); + exit(0); // ------------- Fatal ERROR display error and quit ------------- errorSetuid: @@ -624,6 +646,11 @@ notAnInteger: ,gnuOptions[optionIndex].name, gnuOptions[optionIndex].name); exit (-1); +badMode: + fprintf (stderr,"\nERR:AFB-daemon option [--%s] only accepts local, global or remote.\n\n" + ,gnuOptions[optionIndex].name); + exit (-1); + exitOnSignal: fprintf (stderr,"\n%s INF:AFB-daemon pid=%d received exit signal (Hopefully crtl-C or --kill-previous !!!)\n\n" ,configTime(), getpid()); @@ -637,14 +664,10 @@ errSessiondir: fprintf (stderr,"\nERR:AFB-daemon cannot read/write session dir\n\n"); exit (-1); -errSoundCard: - fprintf (stderr,"\nERR:AFB-daemon fail to probe sound cards\n\n"); - exit (-1); - exitInitLoop: // try to unlink pid file if any if (session->background && session->config->pidfile != NULL) unlink (session->config->pidfile); exit (-1); -}; /* END AFB-daemon() */ +} /* END AFB-daemon() */