2 * Copyright (C) 2015 "IoT.bzh"
3 * Author "Fulup Ar Foll"
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * Author: "Fulup Ar Foll"
23 * Created on 05 December 2015, 15:38
26 #include "local-def.h"
34 #include <sys/types.h>
36 #define AFB_VERSION "0.1"
38 static sigjmp_buf exitPoint; // context save for set/longjmp
40 /*----------------------------------------------------------
42 | print version and copyright
43 +--------------------------------------------------------- */
44 static void printVersion (void) {
46 fprintf (stderr,"\n----------------------------------------- \n");
47 fprintf (stderr,"| AFB [Application Framework Binder] version=%s |\n", AFB_VERSION);
48 fprintf (stderr,"----------------------------------------- \n");
49 fprintf (stderr,"| Copyright(C) 2016 /IoT.bzh [fulup -at- iot.bzh]\n");
50 fprintf (stderr,"| AFB comes with ABSOLUTELY NO WARRANTY.\n");
51 fprintf (stderr,"| Licence [what ever makes you happy] until you fix bugs by yourself :)\n\n");
56 // Define command line option
58 #define SET_BACKGROUND 2
59 #define SET_FORGROUND 3
60 #define SET_FAKE_MOD 4
62 #define SET_TCP_PORT 5
63 #define SET_ROOT_DIR 6
64 #define SET_ROOT_BASE 7
65 #define SET_ROOT_API 8
66 #define SET_ROOT_ALIAS 9
68 #define SET_CACHE_TIMEOUT 10
69 #define SET_SESSION_DIR 11
71 #define SET_AUTH_TOKEN 12
73 #define SET_APITIMEOUT 14
74 #define SET_CNTXTIMEOUT 15
76 #define DISPLAY_VERSION 16
77 #define DISPLAY_HELP 17
80 #define SET_READYFD 19
82 // Command line structure hold cli --command + help text
84 int val; // command number within application
85 int has_arg; // command number within application
86 char *name; // command as used in --xxxx cli
87 char *help; // help text
92 static AFB_options cliOptions [] = {
93 {SET_VERBOSE ,0,"verbose" , "Verbose Mode"},
95 {SET_FORGROUND ,0,"foreground" , "Get all in foreground mode"},
96 {SET_BACKGROUND ,0,"daemon" , "Get all in background mode"},
98 {SET_TCP_PORT ,1,"port" , "HTTP listening TCP port [default 1234]"},
99 {SET_ROOT_DIR ,1,"rootdir" , "HTTP Root Directory [default $HOME/.AFB]"},
100 {SET_ROOT_BASE ,1,"rootbase" , "Angular Base Root URL [default /opa]"},
101 {SET_ROOT_API ,1,"rootapi" , "HTML Root API URL [default /api]"},
102 {SET_ROOT_ALIAS ,1,"alias" , "Muliple url map outside of rootdir [eg: --alias=/icons:/usr/share/icons]"},
104 {SET_APITIMEOUT ,1,"apitimeout" , "Plugin API timeout in seconds [default 10]"},
105 {SET_CNTXTIMEOUT ,1,"cntxtimeout" , "Client Session Context Timeout [default 900]"},
106 {SET_CACHE_TIMEOUT ,1,"cache-eol" , "Client cache end of live [default 3600s]"},
108 {SET_SESSION_DIR ,1,"sessiondir" , "Sessions file path [default rootdir/sessions]"},
110 {SET_LDPATH ,1,"ldpaths" , "Load Plugins from dir1:dir2:... [default = PLUGIN_INSTALL_DIR"},
111 {SET_AUTH_TOKEN ,1,"token" , "Initial Secret [default=no-session, --token="" for session without authentication]"},
113 {DISPLAY_VERSION ,0,"version" , "Display version and copyright"},
114 {DISPLAY_HELP ,0,"help" , "Display this help"},
116 {SET_MODE ,1,"mode" , "set the mode: either local, remote or global"},
117 {SET_READYFD ,1,"readyfd" , "set the #fd to signal when ready"},
121 static AFB_aliasdir aliasdir[MAX_ALIAS];
122 static int aliascount=0;
124 // load config from disk and merge with CLI option
125 static AFB_error config_set_default (AFB_session * session)
127 static char cacheTimeout [10];
130 if (session->config->httpdPort == 0) session->config->httpdPort=1234;
132 // default Plugin API timeout
133 if (session->config->apiTimeout == 0) session->config->apiTimeout=DEFLT_API_TIMEOUT;
135 // default AUTH_TOKEN
136 if (session->config->token == NULL) session->config->token= DEFLT_AUTH_TOKEN;
138 // cache timeout default one hour
139 if (session->config->cacheTimeout == 0) session->config->cacheTimeout=DEFLT_CACHE_TIMEOUT;
141 // cache timeout default one hour
142 if (session->config->cntxTimeout == 0) session->config->cntxTimeout=DEFLT_CNTX_TIMEOUT;
144 if (session->config->rootdir == NULL) {
145 session->config->rootdir = getenv("AFBDIR");
146 if (session->config->rootdir == NULL) {
147 session->config->rootdir = malloc (512);
148 strncpy (session->config->rootdir, getenv("HOME"),512);
149 strncat (session->config->rootdir, "/.AFB",512);
151 // if directory does not exist createit
152 mkdir (session->config->rootdir, O_RDWR | S_IRWXU | S_IRGRP);
155 // if no Angular/HTML5 rootbase let's try '/' as default
156 if (session->config->rootbase == NULL) {
157 session->config->rootbase = "/opa";
160 if (session->config->rootapi == NULL) {
161 session->config->rootapi = "/api";
164 if (session->config->ldpaths == NULL) {
165 session->config->ldpaths = PLUGIN_INSTALL_DIR;
168 // if no session dir create a default path from rootdir
169 if (session->config->sessiondir == NULL) {
170 session->config->sessiondir = malloc (512);
171 strncpy (session->config->sessiondir, session->config->rootdir, 512);
172 strncat (session->config->sessiondir, "/sessions",512);
175 // if no config dir create a default path from sessiondir
176 if (session->config->console == NULL) {
177 session->config->console = malloc (512);
178 strncpy (session->config->console, session->config->sessiondir, 512);
179 strncat (session->config->console, "/AFB-console.out",512);
182 // cacheTimeout is an integer but HTTPd wants it as a string
183 snprintf (cacheTimeout, sizeof (cacheTimeout),"%d", session->config->cacheTimeout);
184 session->cacheTimeout = cacheTimeout; // httpd uses cacheTimeout string version
191 /*----------------------------------------------------------
194 +--------------------------------------------------------- */
195 void signalQuit (int signum) {
199 // unlock timeout signal to allow a new signal to come
200 sigemptyset (&sigset);
201 sigaddset (&sigset, SIGABRT);
202 sigprocmask (SIG_UNBLOCK, &sigset, 0);
204 fprintf (stderr, "%s ERR:Received signal quit\n",configTime());
205 syslog (LOG_ERR, "Daemon got kill3 & quit [please report bug]");
206 longjmp (exitPoint, signum);
210 /*----------------------------------------------------------
212 | print information from long option array
213 +--------------------------------------------------------- */
215 static void printHelp(char *name) {
219 fprintf (stderr,"%s:\nallowed options\n", name);
220 for (ind=0; cliOptions [ind].name != NULL;ind++)
223 if (cliOptions [ind].has_arg == 0 )
225 fprintf (stderr," --%-15s %s\n", cliOptions [ind].name, cliOptions[ind].help);
227 sprintf(command,"%s=xxxx", cliOptions [ind].name);
228 fprintf (stderr," --%-15s %s\n", command, cliOptions[ind].help);
231 fprintf (stderr,"Example:\n %s\\\n --verbose --port=1234 --token='azerty' --ldpaths=build/plugins:/usr/lib64/agl/plugins\n", name);
234 /*----------------------------------------------------------
236 | try to close everything before leaving
237 +--------------------------------------------------------- */
238 static void closeSession (AFB_session *session) {
243 /*----------------------------------------------------------
245 | Main listening HTTP loop
246 +--------------------------------------------------------- */
247 static void listenLoop (AFB_session *session) {
250 if (signal (SIGABRT, signalQuit) == SIG_ERR) {
251 fprintf (stderr, "%s ERR: main fail to install Signal handler\n", configTime());
255 // ------ Start httpd server
256 if (session->config->httpdPort > 0) {
258 err = httpdStart (session);
259 if (err != AFB_SUCCESS) return;
261 if (session->readyfd != 0) {
262 static const char readystr[] = "READY=1";
263 write(session->readyfd, readystr, sizeof(readystr) - 1);
264 close(session->readyfd);
270 fprintf (stderr, "hoops returned from infinite loop [report bug]\n");
274 /*---------------------------------------------------------
276 | Parse option and launch action
277 +--------------------------------------------------------- */
279 static void parse_arguments(int argc, char *argv[], AFB_session *session)
281 char* programName = argv [0];
285 struct option *gnuOptions;
287 // ------------- Build session handler & init config -------
288 memset(&aliasdir ,0,sizeof(aliasdir));
289 session->config->aliasdir = aliasdir;
291 // ------------------ Process Command Line -----------------------
293 // if no argument print help and return
295 printHelp(programName);
299 // build GNU getopt info from cliOptions
300 nbcmd = sizeof (cliOptions) / sizeof (AFB_options);
301 gnuOptions = malloc (sizeof (*gnuOptions) * (unsigned)nbcmd);
302 for (ind=0; ind < nbcmd;ind++) {
303 gnuOptions [ind].name = cliOptions[ind].name;
304 gnuOptions [ind].has_arg = cliOptions[ind].has_arg;
305 gnuOptions [ind].flag = 0;
306 gnuOptions [ind].val = cliOptions[ind].val;
309 // get all options from command line
310 while ((optc = getopt_long (argc, argv, "vsp?", gnuOptions, &optionIndex))
320 if (optarg == 0) goto needValueForOption;
321 if (!sscanf (optarg, "%d", &session->config->httpdPort)) goto notAnInteger;
325 if (optarg == 0) goto needValueForOption;
326 if (!sscanf (optarg, "%d", &session->config->apiTimeout)) goto notAnInteger;
329 case SET_CNTXTIMEOUT:
330 if (optarg == 0) goto needValueForOption;
331 if (!sscanf (optarg, "%d", &session->config->cntxTimeout)) goto notAnInteger;
335 if (optarg == 0) goto needValueForOption;
336 session->config->rootdir = optarg;
337 if (verbose) fprintf(stderr, "Forcing Rootdir=%s\n",session->config->rootdir);
341 if (optarg == 0) goto needValueForOption;
342 session->config->rootbase = optarg;
343 if (verbose) fprintf(stderr, "Forcing Rootbase=%s\n",session->config->rootbase);
347 if (optarg == 0) goto needValueForOption;
348 session->config->rootapi = optarg;
349 if (verbose) fprintf(stderr, "Forcing Rootapi=%s\n",session->config->rootapi);
353 if (optarg == 0) goto needValueForOption;
354 if (aliascount < MAX_ALIAS) {
355 aliasdir[aliascount].url = strsep(&optarg,":");
356 if (optarg == NULL) {
357 fprintf(stderr, "missing ':' in alias %s, ignored\n", aliasdir[aliascount].url);
359 aliasdir[aliascount].path = optarg;
360 aliasdir[aliascount].len = strlen(aliasdir[aliascount].url);
361 if (verbose) fprintf(stderr, "Alias url=%s path=%s\n", aliasdir[aliascount].url, aliasdir[aliascount].path);
365 fprintf(stderr, "Too many aliases [max:%d] %s ignored\n", MAX_ALIAS, optarg);
370 if (optarg == 0) goto needValueForOption;
371 session->config->token = optarg;
375 if (optarg == 0) goto needValueForOption;
376 session->config->ldpaths = optarg;
379 case SET_SESSION_DIR:
380 if (optarg == 0) goto needValueForOption;
381 session->config->sessiondir = optarg;
384 case SET_CACHE_TIMEOUT:
385 if (optarg == 0) goto needValueForOption;
386 if (!sscanf (optarg, "%d", &session->config->cacheTimeout)) goto notAnInteger;
390 if (optarg != 0) goto noValueForOption;
391 session->fakemod = 1;
395 if (optarg != 0) goto noValueForOption;
396 session->foreground = 1;
400 if (optarg != 0) goto noValueForOption;
401 session->background = 1;
405 if (optarg == 0) goto needValueForOption;
406 if (!strcmp(optarg, "local")) session->config->mode = AFB_MODE_LOCAL;
407 else if (!strcmp(optarg, "remote")) session->config->mode = AFB_MODE_REMOTE;
408 else if (!strcmp(optarg, "global")) session->config->mode = AFB_MODE_GLOBAL;
413 if (optarg == 0) goto needValueForOption;
414 if (!sscanf (optarg, "%u", &session->readyfd)) goto notAnInteger;
417 case DISPLAY_VERSION:
418 if (optarg != 0) goto noValueForOption;
424 printHelp(programName);
430 // if exist merge config file with CLI arguments
431 config_set_default (session);
436 fprintf (stderr,"\nERR: AFB-daemon option [--%s] need a value i.e. --%s=xxx\n\n"
437 ,gnuOptions[optionIndex].name, gnuOptions[optionIndex].name);
441 fprintf (stderr,"\nERR: AFB-daemon option [--%s] requirer an interger i.e. --%s=9\n\n"
442 ,gnuOptions[optionIndex].name, gnuOptions[optionIndex].name);
446 fprintf (stderr,"\nERR: AFB-daemon option [--%s] don't take value\n\n"
447 ,gnuOptions[optionIndex].name);
451 fprintf (stderr,"\nERR: AFB-daemon option [--%s] only accepts local, global or remote.\n\n"
452 ,gnuOptions[optionIndex].name);
456 /*---------------------------------------------------------
458 | Parse option and launch action
459 +--------------------------------------------------------- */
461 int main(int argc, char *argv[]) {
462 AFB_session *session;
466 // ------------- Build session handler & init config -------
467 session = calloc (1, sizeof (AFB_session));
468 session->config = calloc (1, sizeof (AFB_config));
469 parse_arguments(argc, argv, session);
471 initPlugins(session);
473 // ------------------ sanity check ----------------------------------------
474 if ((session->background) && (session->foreground)) {
475 fprintf (stderr, "%s ERR: cannot select foreground & background at the same time\n",configTime());
479 // ------------------ Some useful default values -------------------------
480 if ((session->background == 0) && (session->foreground == 0)) session->foreground=1;
482 // open syslog if ever needed
483 openlog("AFB-log", 0, LOG_DAEMON);
485 // ------------------ clean exit on CTR-C signal ------------------------
486 if (signal (SIGINT, signalQuit) == SIG_ERR) {
487 fprintf (stderr, "%s Quit Signal received.",configTime());
491 // save exitPoint context when returning from longjmp closeSession and exit
492 status = setjmp (exitPoint); // return !+ when coming from longjmp
494 if (verbose) printf ("INF: main returning from longjump after signal [%d]\n", status);
495 closeSession (session);
499 // let's run this program with a low priority
502 // ------------------ Finaly Process Commands -----------------------------
503 // let's not take the risk to run as ROOT
504 //if (getuid() == 0) goto errorNoRoot;
506 // check session dir and create if it does not exist
507 if (sessionCheckdir (session) != AFB_SUCCESS) goto errSessiondir;
508 if (verbose) fprintf (stderr, "AFB: notice Init config done\n");
512 // ---- run in foreground mode --------------------
513 if (session->foreground) {
515 if (verbose) fprintf (stderr,"AFB: notice Foreground mode\n");
517 // enter listening loop in foreground
523 // --------- run in background mode -----------
524 if (session->background) {
526 // if (status != 0) goto errorCommand;
527 if (verbose) printf ("AFB: Entering background mode\n");
529 // open /dev/console to redirect output messAFBes
530 consoleFD = open(session->config->console, O_WRONLY | O_APPEND | O_CREAT , 0640);
531 if (consoleFD < 0) goto errConsole;
533 // fork process when running background mode
536 // son process get all data in standalone mode
539 printf ("\nAFB: background mode [pid:%d console:%s]\n", getpid(),session->config->console);
541 // redirect default I/O on console
542 close (2); status=dup(consoleFD); // redirect stderr
543 close (1); status=dup(consoleFD); // redirect stdout
544 close (0); // no need for stdin
547 setsid(); // allow father process to fully exit
548 sleep (2); // allow main to leave and release port
550 fprintf (stderr, "----------------------------\n");
551 fprintf (stderr, "%s INF: main background pid=%d\n", configTime(), getpid());
554 // if everything look OK then look forever
555 syslog (LOG_ERR, "AFB: Entering infinite loop in background mode");
557 // should normally never return from this loop
559 syslog (LOG_ERR, "AFB: FAIL background infinite loop exited check [%s]\n", session->config->console);
564 // if fail nothing much to do
565 if (pid == -1) goto errorFork;
567 // we are in father process, we don't need this one
570 } // end background-foreground
573 closeSession (session); // try to close everything before leaving
574 if (verbose) printf ("\n---- Application Framework Binder Normal End ------\n");
577 // ------------- Fatal ERROR display error and quit -------------
579 fprintf (stderr,"\nERR: AFB-daemon Failed to fork son process\n\n");
583 fprintf (stderr,"\n%s INF: AFB-daemon pid=%d received exit signal (Hopefully crtl-C or --kill-previous !!!)\n\n"
584 ,configTime(), getpid());
588 fprintf (stderr,"\nERR: AFB-daemon cannot open /dev/console (use --foreground)\n\n");
592 fprintf (stderr,"\nERR: AFB-daemon cannot read/write session dir\n\n");