X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fmain.c;h=d19f7e0f9d1de01681f2d097befc8b03bb56a049;hb=29d04383ea3e499840aae0220da700a27cce26e4;hp=af90eaec35fa6cb751a952c859ab44fa664f2e38;hpb=b0d938b609b645b0353e95b7e5c6313f20811da9;p=src%2Fapp-framework-binder.git diff --git a/src/main.c b/src/main.c index af90eaec..d19f7e0f 100644 --- a/src/main.c +++ b/src/main.c @@ -116,8 +116,6 @@ static AFB_options cliOptions [] = { {SET_PID_FILE ,1,"pidfile" , "PID file path [default none]"}, {SET_SESSION_DIR ,1,"sessiondir" , "Sessions file path [default rootdir/sessions]"}, {SET_CONFIG_FILE ,1,"config" , "Config Filename [default rootdir/sessions/configs/default.AFB]"}, - {SET_CONFIG_SAVE ,0,"save" , "Save config on disk [default no]"}, - {SET_CONFIG_EXIT ,0,"saveonly" , "Save config on disk and then exit"}, {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]"}, @@ -272,29 +270,26 @@ static void listenLoop (AFB_session *session) { | Parse option and launch action +--------------------------------------------------------- */ -int main(int argc, char *argv[]) { - AFB_session *session; +static void parse_arguments(int argc, char *argv[], AFB_session *session) +{ char* programName = argv [0]; int optionIndex = 0; - int optc, ind, consoleFD; - int pid, nbcmd, status; + int optc, ind; + int nbcmd; + struct option *gnuOptions; AFB_config cliconfig; // temp structure to store CLI option before file config upload // ------------- Build session handler & init config ------- - session = configInit (); memset(&cliconfig,0,sizeof(cliconfig)); memset(&aliasdir ,0,sizeof(aliasdir)); cliconfig.aliasdir = aliasdir; - // GNU CLI getopts nterface. - struct option *gnuOptions; - // ------------------ Process Command Line ----------------------- // if no argument print help and return if (argc < 2) { printHelp(programName); - return (-1); + exit(1); } // build GNU getopt info from cliOptions @@ -397,17 +392,6 @@ int main(int argc, char *argv[]) { if (!sscanf (optarg, "%d", &cliconfig.cacheTimeout)) goto notAnInteger; break; - case SET_CONFIG_EXIT: - if (optarg != 0) goto noValueForOption; - session->configsave = 1; - session->forceexit = 1; - break; - - case SET_CONFIG_SAVE: - if (optarg != 0) goto noValueForOption; - session->configsave = 1; - break; - case SET_USERID: if (optarg == 0) goto needValueForOption; cliconfig.setuid = optarg; @@ -454,24 +438,62 @@ int main(int argc, char *argv[]) { case DISPLAY_VERSION: if (optarg != 0) goto noValueForOption; printVersion(); - goto normalExit; + exit(0); case DISPLAY_HELP: default: printHelp(programName); - goto normalExit; - + exit(0); } } + free(gnuOptions); // if exist merge config file with CLI arguments configLoadFile (session, &cliconfig); + return; + + +needValueForOption: + fprintf (stderr,"\nERR:AFB-daemon option [--%s] need a value i.e. --%s=xxx\n\n" + ,gnuOptions[optionIndex].name, gnuOptions[optionIndex].name); + exit (1); + +notAnInteger: + fprintf (stderr,"\nERR:AFB-daemon option [--%s] requirer an interger i.e. --%s=9\n\n" + ,gnuOptions[optionIndex].name, gnuOptions[optionIndex].name); + exit (1); + +noValueForOption: + fprintf (stderr,"\nERR:AFB-daemon option [--%s] don't take value\n\n" + ,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); +} + +/*--------------------------------------------------------- + | main + | Parse option and launch action + +--------------------------------------------------------- */ + +int main(int argc, char *argv[]) { + AFB_session *session; + char* programName = argv [0]; + int consoleFD; + int pid, status; + + // ------------- Build session handler & init config ------- + session = configInit (); + parse_arguments(argc, argv, session); 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); + exit (1); } // ------------------ Some useful default values ------------------------- @@ -508,7 +530,7 @@ int main(int argc, char *argv[]) { // ------------------ clean exit on CTR-C signal ------------------------ if (signal (SIGINT, signalQuit) == SIG_ERR) { fprintf (stderr, "%s Quit Signal received.",configTime()); - return (-1); + return 1; } // save exitPoint context when returning from longjmp closeSession and exit @@ -524,9 +546,6 @@ int main(int argc, char *argv[]) { // ------------------ 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; @@ -624,57 +643,38 @@ normalExit: // ------------- Fatal ERROR display error and quit ------------- errorSetuid: fprintf (stderr,"\nERR:AFB-daemon Failed to change UID to username=[%s]\n\n", session->config->setuid); - exit (-1); + exit (1); //errorNoRoot: // fprintf (stderr,"\nERR:AFB-daemon Not allow to run as root [use --seteuid=username option]\n\n"); -// exit (-1); +// exit (1); errorPidFile: fprintf (stderr,"\nERR:AFB-daemon Failed to write pid file [%s]\n\n", session->config->pidfile); - exit (-1); + exit (1); errorFork: fprintf (stderr,"\nERR:AFB-daemon Failed to fork son process\n\n"); - exit (-1); - -needValueForOption: - fprintf (stderr,"\nERR:AFB-daemon option [--%s] need a value i.e. --%s=xxx\n\n" - ,gnuOptions[optionIndex].name, gnuOptions[optionIndex].name); - exit (-1); - -noValueForOption: - fprintf (stderr,"\nERR:AFB-daemon option [--%s] don't take value\n\n" - ,gnuOptions[optionIndex].name); - exit (-1); - -notAnInteger: - fprintf (stderr,"\nERR:AFB-daemon option [--%s] requirer an interger i.e. --%s=9\n\n" - ,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); + 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()); - exit (-1); + exit (1); errConsole: fprintf (stderr,"\nERR:AFB-daemon cannot open /dev/console (use --foreground)\n\n"); - exit (-1); + exit (1); errSessiondir: fprintf (stderr,"\nERR:AFB-daemon cannot read/write session dir\n\n"); - exit (-1); + exit (1); exitInitLoop: // try to unlink pid file if any if (session->background && session->config->pidfile != NULL) unlink (session->config->pidfile); - exit (-1); + exit (1); + +} -} /* END AFB-daemon() */