X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=src%2Fmain.c;h=414ffebc460295dc502ecb93da62f917ddf5cd75;hb=bf904980ffbf712b53b0684adf90a1428d8e62ff;hp=74e8745e4a1d768531f00fc0b693b1834e9198f3;hpb=369b595ca0a618ba8188d92ae26c4219625caa3d;p=src%2Fapp-framework-binder.git diff --git a/src/main.c b/src/main.c index 74e8745e..414ffebc 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"); @@ -82,6 +84,14 @@ static sigjmp_buf exitPoint; // context save for set/longjmp #define SET_MODE 160 #define SET_READYFD 161 +// Command line structure hold cli --command + help text +typedef struct { + int val; // command number within application + int has_arg; // command number within application + char *name; // command as used in --xxxx cli + char *help; // help text +} AFB_options; + // Supported option static AFB_options cliOptions [] = { @@ -106,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]"}, @@ -117,7 +125,7 @@ static AFB_options cliOptions [] = { {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, 0} + {0, 0, NULL, NULL} }; static AFB_aliasdir aliasdir[MAX_ALIAS]; @@ -262,35 +270,31 @@ 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 ggcOption; - 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 nbcmd = sizeof (cliOptions) / sizeof (AFB_options); - gnuOptions = malloc (sizeof (ggcOption) * nbcmd); + gnuOptions = malloc (sizeof (*gnuOptions) * (unsigned)nbcmd); for (ind=0; ind < nbcmd;ind++) { gnuOptions [ind].name = cliOptions[ind].name; gnuOptions [ind].has_arg = cliOptions[ind].has_arg; @@ -354,7 +358,7 @@ int main(int argc, char *argv[]) { 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; @@ -388,20 +392,9 @@ 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; - if (!sscanf (optarg, "%s", &cliconfig.setuid)) goto notAnInteger; + cliconfig.setuid = optarg; break; case SET_FAKE_MOD: @@ -445,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 ------------------------- @@ -476,19 +507,19 @@ int main(int argc, char *argv[]) { 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); + 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()); + 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); + 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); + if (status != 0) fprintf (stderr, "%s ERR: main failled to killed pid=%d \n",configTime(), pid); } } // end switch pid @@ -499,13 +530,13 @@ 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 status = setjmp (exitPoint); // return !+ when coming from longjmp if (status != 0) { - if (verbose) printf ("INF:main returning from longjump after signal [%d]\n", status); + if (verbose) printf ("INF: main returning from longjump after signal [%d]\n", status); closeSession (session); goto exitOnSignal; } @@ -515,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; @@ -534,14 +562,14 @@ int main(int argc, char *argv[]) { // 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 (verbose) 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 (verbose) fprintf (stderr,"AFB: notice Foreground mode\n"); // write a pid file for --kill-previous and --raise-debug option status = writePidFile (session->config, getpid()); @@ -570,7 +598,7 @@ int main(int argc, char *argv[]) { if (pid == 0) { printf ("\nAFB: background mode [pid:%d console:%s]\n", getpid(),session->config->console); - if (verbose) printf ("AFB:info use '%s --restart --rootdir=%s # [--pidfile=%s] to restart daemon\n", programName,session->config->rootdir, session->config->pidfile); + if (verbose) printf ("AFB: info use '%s --restart --rootdir=%s # [--pidfile=%s] to restart daemon\n", programName,session->config->rootdir, session->config->pidfile); // redirect default I/O on console close (2); status=dup(consoleFD); // redirect stderr @@ -582,7 +610,7 @@ int main(int argc, char *argv[]) { sleep (2); // allow main to leave and release port fprintf (stderr, "----------------------------\n"); - fprintf (stderr, "%s INF:main background pid=%d\n", configTime(), getpid()); + fprintf (stderr, "%s INF: main background pid=%d\n", configTime(), getpid()); fflush (stderr); // if everything look OK then look forever @@ -590,7 +618,7 @@ int main(int argc, char *argv[]) { // should normally never return from this loop listenLoop(session); - syslog (LOG_ERR, "AFB:FAIL background infinite loop exited check [%s]\n", session->config->console); + syslog (LOG_ERR, "AFB: FAIL background infinite loop exited check [%s]\n", session->config->console); goto exitInitLoop; } @@ -614,62 +642,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); + fprintf (stderr,"\nERR: AFB-daemon Failed to change UID to username=[%s]\n\n", session->config->setuid); + 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); + fprintf (stderr,"\nERR: AFB-daemon Failed to write pid file [%s]\n\n", session->config->pidfile); + 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); + fprintf (stderr,"\nERR: AFB-daemon Failed to fork son process\n\n"); + exit (1); exitOnSignal: - fprintf (stderr,"\n%s INF:AFB-daemon pid=%d received exit signal (Hopefully crtl-C or --kill-previous !!!)\n\n" + 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); + 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); - -errSoundCard: - fprintf (stderr,"\nERR:AFB-daemon fail to probe sound cards\n\n"); - exit (-1); + fprintf (stderr,"\nERR: AFB-daemon cannot read/write session dir\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); + exit (1); +} -}; /* END AFB-daemon() */