- char* programName = argv [0];
- int optionIndex = 0;
- int optc, ind;
- int nbcmd;
- struct option *gnuOptions;
-
- // ------------- Build session handler & init config -------
- memset(&aliasdir ,0,sizeof(aliasdir));
- session->config->aliasdir = aliasdir;
-
- // ------------------ Process Command Line -----------------------
-
- // if no argument print help and return
- if (argc < 2) {
- printHelp(programName);
- exit(1);
- }
-
- // build GNU getopt info from cliOptions
- nbcmd = sizeof (cliOptions) / sizeof (AFB_options);
- 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;
- gnuOptions [ind].flag = 0;
- gnuOptions [ind].val = cliOptions[ind].val;
- }
-
- // get all options from command line
- while ((optc = getopt_long (argc, argv, "vsp?", gnuOptions, &optionIndex))
- != EOF)
- {
- switch (optc)
- {
- case SET_VERBOSE:
- verbose = 1;
- break;
-
- case SET_TCP_PORT:
- if (optarg == 0) goto needValueForOption;
- if (!sscanf (optarg, "%d", &session->config->httpdPort)) goto notAnInteger;
- break;
-
- case SET_APITIMEOUT:
- if (optarg == 0) goto needValueForOption;
- if (!sscanf (optarg, "%d", &session->config->apiTimeout)) goto notAnInteger;
- break;
-
- case SET_CNTXTIMEOUT:
- if (optarg == 0) goto needValueForOption;
- if (!sscanf (optarg, "%d", &session->config->cntxTimeout)) goto notAnInteger;
- break;
-
- case SET_ROOT_DIR:
- if (optarg == 0) goto needValueForOption;
- session->config->rootdir = optarg;
- if (verbose) 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);
- 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);
- break;
-
- case SET_ALIAS:
- if (optarg == 0) goto needValueForOption;
- if (aliascount < MAX_ALIAS) {
- aliasdir[aliascount].url = strsep(&optarg,":");
- 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:%d] %s ignored\n", MAX_ALIAS, optarg);
- }
- break;
-
- case SET_AUTH_TOKEN:
- if (optarg == 0) goto needValueForOption;
- session->config->token = optarg;
- break;
-
- case SET_LDPATH:
- if (optarg == 0) goto needValueForOption;
- session->config->ldpaths = optarg;
- break;
-
- case SET_SESSION_DIR:
- if (optarg == 0) goto needValueForOption;
- session->config->sessiondir = optarg;
- break;
-
- case SET_CACHE_TIMEOUT:
- if (optarg == 0) goto needValueForOption;
- if (!sscanf (optarg, "%d", &session->config->cacheTimeout)) goto notAnInteger;
- break;
-
- case SET_FAKE_MOD:
- if (optarg != 0) goto noValueForOption;
- session->fakemod = 1;
- break;
-
- case SET_FORGROUND:
- if (optarg != 0) goto noValueForOption;
- session->foreground = 1;
- break;
-
- case SET_BACKGROUND:
- if (optarg != 0) goto noValueForOption;
- session->background = 1;
- break;
-
- case SET_MODE:
- if (optarg == 0) goto needValueForOption;
- if (!strcmp(optarg, "local")) session->config->mode = AFB_MODE_LOCAL;
- else if (!strcmp(optarg, "remote")) session->config->mode = AFB_MODE_REMOTE;
- else if (!strcmp(optarg, "global")) session->config->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();
- exit(0);
-
- case DISPLAY_HELP:
- default:
- printHelp(programName);
- exit(0);
- }
- }
- free(gnuOptions);
-
- config_set_default (session);
- 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);
+ int consoleFD;
+ int pid;
+
+ // open /dev/console to redirect output messAFBes
+ consoleFD = open(config->console, O_WRONLY | O_APPEND | O_CREAT, 0640);
+ if (consoleFD < 0) {
+ ERROR("AFB-daemon cannot open /dev/console (use --foreground)");
+ exit(1);
+ }
+ // fork process when running background mode
+ pid = fork();
+
+ // if fail nothing much to do
+ if (pid == -1) {
+ ERROR("AFB-daemon Failed to fork son process");
+ exit(1);
+ }
+ // if in father process, just leave
+ if (pid != 0)
+ _exit(0);
+
+ // son process get all data in standalone mode
+ NOTICE("background mode [pid:%d console:%s]", getpid(),
+ 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