diff options
-rw-r--r-- | include/local-def.h | 7 | ||||
-rw-r--r-- | include/proto-def.h | 3 | ||||
-rw-r--r-- | src/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/config.c | 215 | ||||
-rw-r--r-- | src/helper-api.c | 1 | ||||
-rw-r--r-- | src/main.c | 314 |
6 files changed, 119 insertions, 422 deletions
diff --git a/include/local-def.h b/include/local-def.h index f03666a4..a55326f1 100644 --- a/include/local-def.h +++ b/include/local-def.h @@ -153,10 +153,7 @@ typedef struct { char *rootdir; // base dir for httpd file download char *rootbase; // Angular HTML5 base URL char *rootapi; // Base URL for REST APIs - char *pidfile; // where to store pid when running background char *sessiondir; // where to store mixer session files - char *configfile; // where to store configuration on gateway exit - char *setuid; // downgrade uid to username char *token; // initial authentication token [default NULL no session] int cacheTimeout; int apiTimeout; @@ -229,17 +226,15 @@ struct MHD_Daemon; typedef struct { AFB_config *config; // pointer to current config // List of commands to execute - int killPrevious; int background; // run in backround mode int foreground; // run in forground mode char *cacheTimeout; // http require timeout to be a string - struct MHD_Daemon *httpd; // anonymous structure for httpd handler + struct MHD_Daemon *httpd; // structure for httpd handler int fakemod; // respond to GET/POST request without interacting with sndboard int readyfd; // a #fd to signal when ready to serve AFB_plugin **plugins; // pointer to REST/API plugins magic_t magic; // Mime type file magic lib struct afb_hsrv_handler *handlers; - sigjmp_buf restartCkpt; // context save for restart set/longjmp } AFB_session; diff --git a/include/proto-def.h b/include/proto-def.h index 89b6e3d3..ea939aa6 100644 --- a/include/proto-def.h +++ b/include/proto-def.h @@ -66,8 +66,5 @@ extern AFB_error httpdLoop (AFB_session *session); extern void httpdStop (AFB_session *session); -// config management -extern AFB_session *configInit (void); -extern AFB_error configLoadFile (AFB_session * session, AFB_config *cliconfig); diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 77850a29..19be965d 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,7 +1,6 @@ ADD_LIBRARY(src OBJECT main.c - config.c session.c http-svc.c rest-api.c diff --git a/src/config.c b/src/config.c deleted file mode 100644 index 6f23664b..00000000 --- a/src/config.c +++ /dev/null @@ -1,215 +0,0 @@ -/* - * Copyright (C) 2015 "IoT.bzh" - * Author "Fulup Ar Foll" - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see <http://www.gnu.org/licenses/>. - - References: - https://www.gnu.org/software/libmicrohttpd/manual/html_node/index.html#Top - http://www-01.ibm.com/support/knowledgecenter/SSB23S_1.1.0.9/com.ibm.ztpf-ztpfdf.doc_put.09/gtpc2/cpp_vsprintf.html?cp=SSB23S_1.1.0.9%2F0-3-8-1-0-16-8 - -*/ - -#include "../include/local-def.h" -#include <stdarg.h> -#include <sys/stat.h> -#include <sys/types.h> - -#define AFB_CONFIG_JTYPE "AFB_config" - - -// load config from disk and merge with CLI option -PUBLIC AFB_error configLoadFile (AFB_session * session, AFB_config *cliconfig) { - static char cacheTimeout [10]; - int fd; - json_object * AFBConfig, *value; - - // TBD integrate alias-dir array with config-file - session->config->aliasdir = cliconfig->aliasdir; - session->config->mode = cliconfig->mode; - - // default HTTP port - if (cliconfig->httpdPort == 0) session->config->httpdPort=1234; - else session->config->httpdPort=cliconfig->httpdPort; - - // default Plugin API timeout - if (cliconfig->apiTimeout == 0) session->config->apiTimeout=DEFLT_API_TIMEOUT; - else session->config->apiTimeout=cliconfig->apiTimeout; - - // default AUTH_TOKEN - if (cliconfig->token == NULL) session->config->token= DEFLT_AUTH_TOKEN; - else session->config->token=cliconfig->token; - - // cache timeout default one hour - if (cliconfig->cacheTimeout == 0) session->config->cacheTimeout=DEFLT_CACHE_TIMEOUT; - else session->config->cacheTimeout=cliconfig->cacheTimeout; - - // cache timeout default one hour - if (cliconfig->cntxTimeout == 0) session->config->cntxTimeout=DEFLT_CNTX_TIMEOUT; - else session->config->cntxTimeout=cliconfig->cntxTimeout; - - if (cliconfig->rootdir == NULL) { - session->config->rootdir = getenv("AFBDIR"); - if (session->config->rootdir == NULL) { - session->config->rootdir = malloc (512); - strncpy (session->config->rootdir, getenv("HOME"),512); - strncat (session->config->rootdir, "/.AFB",512); - } - // if directory does not exist createit - mkdir (session->config->rootdir, O_RDWR | S_IRWXU | S_IRGRP); - } else { - session->config->rootdir = cliconfig->rootdir; - } - - // if no Angular/HTML5 rootbase let's try '/' as default - if (cliconfig->rootbase == NULL) { - session->config->rootbase = "/opa"; - } else { - session->config->rootbase= cliconfig->rootbase; - } - - if (cliconfig->rootapi == NULL) { - session->config->rootapi = "/api"; - } else { - session->config->rootapi= cliconfig->rootapi; - } - - if (cliconfig->ldpaths == NULL) { - session->config->ldpaths = PLUGIN_INSTALL_DIR; - } else { - session->config->ldpaths= cliconfig->ldpaths; - } - - // if no session dir create a default path from rootdir - if (cliconfig->sessiondir == NULL) { - session->config->sessiondir = malloc (512); - strncpy (session->config->sessiondir, session->config->rootdir, 512); - strncat (session->config->sessiondir, "/sessions",512); - } else { - session->config->sessiondir = cliconfig->sessiondir; - } - - // if no config dir create a default path from sessiondir - if (cliconfig->configfile == NULL) { - session->config->configfile = malloc (512); - strncpy (session->config->configfile, session->config->sessiondir, 512); - strncat (session->config->configfile, "/AFB-config.json",512); - } else { - session->config->configfile = cliconfig->configfile; - } - - // if no config dir create a default path from sessiondir - if (cliconfig->pidfile == NULL) { - session->config->pidfile = malloc (512); - strncpy (session->config->pidfile, session->config->sessiondir, 512); - strncat (session->config->pidfile, "/AFB-process.pid",512); - } else { - session->config->pidfile= cliconfig->pidfile; - } - - // if no config dir create a default path from sessiondir - if (cliconfig->console == NULL) { - session->config->console = malloc (512); - strncpy (session->config->console, session->config->sessiondir, 512); - strncat (session->config->console, "/AFB-console.out",512); - } else { - session->config->console= cliconfig->console; - } - - // just upload json object and return without any further processing - if((fd = open(session->config->configfile, O_RDONLY)) < 0) { - if (verbose) fprintf (stderr, "AFB:notice: config at %s: %s\n", session->config->configfile, strerror(errno)); - return AFB_EMPTY; - } - - // openjson from FD is not public API we need to reopen it !!! - close(fd); - AFBConfig = json_object_from_file (session->config->configfile); - - // check it is an AFB_config - if (json_object_object_get_ex (AFBConfig, "jtype", &value)) { - if (strcmp (AFB_CONFIG_JTYPE, json_object_get_string (value))) { - fprintf (stderr,"AFB: Error file [%s] is not a valid [%s] type\n ", session->config->configfile, AFB_CONFIG_JTYPE); - return AFB_FAIL; - } - } - - if (!cliconfig->rootdir && json_object_object_get_ex (AFBConfig, "rootdir", &value)) { - session->config->rootdir = strdup (json_object_get_string (value)); - } - - if (!cliconfig->rootbase && json_object_object_get_ex (AFBConfig, "rootbase", &value)) { - session->config->rootbase = strdup (json_object_get_string (value)); - } - - if (!cliconfig->rootapi && json_object_object_get_ex (AFBConfig, "rootapi", &value)) { - session->config->rootapi = strdup (json_object_get_string (value)); - } - - if (!cliconfig->ldpaths && json_object_object_get_ex (AFBConfig, "plugins", &value)) { - session->config->ldpaths = strdup (json_object_get_string (value)); - } - - if (!cliconfig->setuid && json_object_object_get_ex (AFBConfig, "setuid", &value)) { - session->config->setuid = strdup (json_object_get_string (value)); - } - - if (!cliconfig->sessiondir && json_object_object_get_ex (AFBConfig, "sessiondir", &value)) { - session->config->sessiondir = strdup (json_object_get_string (value)); - } - - if (!cliconfig->pidfile && json_object_object_get_ex (AFBConfig, "pidfile", &value)) { - session->config->pidfile = strdup (json_object_get_string (value)); - } - - if (!cliconfig->httpdPort && json_object_object_get_ex (AFBConfig, "httpdPort", &value)) { - session->config->httpdPort = json_object_get_int (value); - } - - - if (!cliconfig->localhostOnly && json_object_object_get_ex (AFBConfig, "localhostonly", &value)) { - session->config->localhostOnly = json_object_get_int (value); - } - - if (!cliconfig->cacheTimeout && json_object_object_get_ex (AFBConfig, "cachetimeout", &value)) { - session->config->cacheTimeout = json_object_get_int (value); - } - - if (!cliconfig->apiTimeout && json_object_object_get_ex (AFBConfig, "apitimeout", &value)) { - session->config->apiTimeout = json_object_get_int (value); - } - - if (!cliconfig->cntxTimeout && json_object_object_get_ex (AFBConfig, "cntxtimeout", &value)) { - session->config->cntxTimeout = json_object_get_int (value); - } - - // cacheTimeout is an integer but HTTPd wants it as a string - snprintf (cacheTimeout, sizeof (cacheTimeout),"%d", session->config->cacheTimeout); - session->cacheTimeout = cacheTimeout; // httpd uses cacheTimeout string version - - json_object_put (AFBConfig); // decrease reference count to free the json object - - - - return AFB_SUCCESS; -} - - -PUBLIC AFB_session *configInit () -{ - AFB_session *session = calloc (1, sizeof (AFB_session)); - session->config = calloc (1, sizeof (AFB_config)); - return session; -} - diff --git a/src/helper-api.c b/src/helper-api.c index 50abcea6..7c70330b 100644 --- a/src/helper-api.c +++ b/src/helper-api.c @@ -21,6 +21,7 @@ #include <dirent.h> #include <sys/stat.h> #include <sys/types.h> +#include <stdarg.h> // handle to hold queryAll values @@ -30,6 +30,8 @@ #include <signal.h> #include <getopt.h> #include <pwd.h> +#include <sys/stat.h> +#include <sys/types.h> #define AFB_VERSION "0.1" @@ -44,7 +46,7 @@ static sigjmp_buf exitPoint; // context save for set/longjmp fprintf (stderr,"\n----------------------------------------- \n"); 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,"| Copyright(C) 2016 /IoT.bzh [fulup -at- iot.bzh]\n"); fprintf (stderr,"| AFB comes with ABSOLUTELY NO WARRANTY.\n"); fprintf (stderr,"| Licence [what ever makes you happy] until you fix bugs by yourself :)\n\n"); exit (0); @@ -52,37 +54,30 @@ 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_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 +#define SET_VERBOSE 1 +#define SET_BACKGROUND 2 +#define SET_FORGROUND 3 +#define SET_FAKE_MOD 4 + +#define SET_TCP_PORT 5 +#define SET_ROOT_DIR 6 +#define SET_ROOT_BASE 7 +#define SET_ROOT_API 8 +#define SET_ROOT_ALIAS 9 + +#define SET_CACHE_TIMEOUT 10 +#define SET_SESSION_DIR 11 + +#define SET_AUTH_TOKEN 12 +#define SET_LDPATH 13 +#define SET_APITIMEOUT 14 +#define SET_CNTXTIMEOUT 15 + +#define DISPLAY_VERSION 16 +#define DISPLAY_HELP 17 + +#define SET_MODE 18 +#define SET_READYFD 19 // Command line structure hold cli --command + help text typedef struct { @@ -99,8 +94,6 @@ static AFB_options cliOptions [] = { {SET_FORGROUND ,0,"foreground" , "Get all in foreground mode"}, {SET_BACKGROUND ,0,"daemon" , "Get all in background mode"}, - {KILL_PREV_EXIT ,0,"kill" , "Kill active process if any and exit"}, - {KILL_PREV_REST ,0,"restart" , "Kill active process if any and restart"}, {SET_TCP_PORT ,1,"port" , "HTTP listening TCP port [default 1234]"}, {SET_ROOT_DIR ,1,"rootdir" , "HTTP Root Directory [default $HOME/.AFB]"}, @@ -110,12 +103,9 @@ static AFB_options cliOptions [] = { {SET_APITIMEOUT ,1,"apitimeout" , "Plugin API timeout in seconds [default 10]"}, {SET_CNTXTIMEOUT ,1,"cntxtimeout" , "Client Session Context Timeout [default 900]"}, - {SET_CACHE_TO ,1,"cache-eol" , "Client cache end of live [default 3600s]"}, + {SET_CACHE_TIMEOUT ,1,"cache-eol" , "Client cache end of live [default 3600s]"}, - {SET_USERID ,1,"setuid" , "Change user id [default don't change]"}, - {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_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]"}, @@ -131,6 +121,73 @@ static AFB_options cliOptions [] = { static AFB_aliasdir aliasdir[MAX_ALIAS]; static int aliascount=0; +// load config from disk and merge with CLI option +static AFB_error config_set_default (AFB_session * session) +{ + static char cacheTimeout [10]; + + // default HTTP port + if (session->config->httpdPort == 0) session->config->httpdPort=1234; + + // default Plugin API timeout + if (session->config->apiTimeout == 0) session->config->apiTimeout=DEFLT_API_TIMEOUT; + + // default AUTH_TOKEN + if (session->config->token == NULL) session->config->token= DEFLT_AUTH_TOKEN; + + // cache timeout default one hour + if (session->config->cacheTimeout == 0) session->config->cacheTimeout=DEFLT_CACHE_TIMEOUT; + + // cache timeout default one hour + if (session->config->cntxTimeout == 0) session->config->cntxTimeout=DEFLT_CNTX_TIMEOUT; + + if (session->config->rootdir == NULL) { + session->config->rootdir = getenv("AFBDIR"); + if (session->config->rootdir == NULL) { + session->config->rootdir = malloc (512); + strncpy (session->config->rootdir, getenv("HOME"),512); + strncat (session->config->rootdir, "/.AFB",512); + } + // if directory does not exist createit + mkdir (session->config->rootdir, O_RDWR | S_IRWXU | S_IRGRP); + } + + // if no Angular/HTML5 rootbase let's try '/' as default + if (session->config->rootbase == NULL) { + session->config->rootbase = "/opa"; + } + + if (session->config->rootapi == NULL) { + session->config->rootapi = "/api"; + } + + if (session->config->ldpaths == NULL) { + session->config->ldpaths = PLUGIN_INSTALL_DIR; + } + + // if no session dir create a default path from rootdir + if (session->config->sessiondir == NULL) { + session->config->sessiondir = malloc (512); + strncpy (session->config->sessiondir, session->config->rootdir, 512); + strncat (session->config->sessiondir, "/sessions",512); + } + + // if no config dir create a default path from sessiondir + if (session->config->console == NULL) { + session->config->console = malloc (512); + strncpy (session->config->console, session->config->sessiondir, 512); + strncat (session->config->console, "/AFB-console.out",512); + } + + // cacheTimeout is an integer but HTTPd wants it as a string + snprintf (cacheTimeout, sizeof (cacheTimeout),"%d", session->config->cacheTimeout); + session->cacheTimeout = cacheTimeout; // httpd uses cacheTimeout string version + + return AFB_SUCCESS; +} + + + /*---------------------------------------------------------- | timeout signalQuit | @@ -175,57 +232,6 @@ void signalQuit (int signum) { } // end printHelp /*---------------------------------------------------------- - | writePidFile - | write a file in /var/run/AFB with pid - +--------------------------------------------------------- */ -static int writePidFile (AFB_config *config, int pid) { - FILE *file; - - // if no pid file configure just return - if (config->pidfile == NULL) return 0; - - // open pid file in write mode - file = fopen(config->pidfile,"w"); - if (file == NULL) { - fprintf (stderr,"%s ERR:writePidFile fail to open [%s]\n",configTime(), config->pidfile); - return -1; - } - - // write pid in file and close - fprintf (file, "%d\n", pid); - fclose (file); - return 0; -} - -/*---------------------------------------------------------- - | readPidFile - | read file in /var/run/AFB with pid - +--------------------------------------------------------- */ -static int readPidFile (AFB_config *config) { - int pid; - FILE *file; - int status; - - if (config->pidfile == NULL) return -1; - - // open pid file in write mode - file = fopen(config->pidfile,"r"); - if (file == NULL) { - fprintf (stderr,"%s ERR:readPidFile fail to open [%s]\n",configTime(), config->pidfile); - return -1; - } - - // write pid in file and close - status = fscanf (file, "%d\n", &pid); - fclose (file); - - // never kill pid 0 - if (status != 1) return -1; - - return (pid); -} - -/*---------------------------------------------------------- | closeSession | try to close everything before leaving +--------------------------------------------------------- */ @@ -277,12 +283,10 @@ static void parse_arguments(int argc, char *argv[], AFB_session *session) 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 ------- - memset(&cliconfig,0,sizeof(cliconfig)); memset(&aliasdir ,0,sizeof(aliasdir)); - cliconfig.aliasdir = aliasdir; + session->config->aliasdir = aliasdir; // ------------------ Process Command Line ----------------------- @@ -314,35 +318,35 @@ static void parse_arguments(int argc, char *argv[], AFB_session *session) case SET_TCP_PORT: if (optarg == 0) goto needValueForOption; - if (!sscanf (optarg, "%d", &cliconfig.httpdPort)) goto notAnInteger; + if (!sscanf (optarg, "%d", &session->config->httpdPort)) goto notAnInteger; break; case SET_APITIMEOUT: if (optarg == 0) goto needValueForOption; - if (!sscanf (optarg, "%d", &cliconfig.apiTimeout)) goto notAnInteger; + if (!sscanf (optarg, "%d", &session->config->apiTimeout)) goto notAnInteger; break; case SET_CNTXTIMEOUT: if (optarg == 0) goto needValueForOption; - if (!sscanf (optarg, "%d", &cliconfig.cntxTimeout)) goto notAnInteger; + if (!sscanf (optarg, "%d", &session->config->cntxTimeout)) goto notAnInteger; break; case SET_ROOT_DIR: if (optarg == 0) goto needValueForOption; - cliconfig.rootdir = optarg; - if (verbose) fprintf(stderr, "Forcing Rootdir=%s\n",cliconfig.rootdir); + 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; - cliconfig.rootbase = optarg; - if (verbose) fprintf(stderr, "Forcing Rootbase=%s\n",cliconfig.rootbase); + 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; - cliconfig.rootapi = optarg; - if (verbose) fprintf(stderr, "Forcing Rootapi=%s\n",cliconfig.rootapi); + session->config->rootapi = optarg; + if (verbose) fprintf(stderr, "Forcing Rootapi=%s\n",session->config->rootapi); break; case SET_ROOT_ALIAS: @@ -364,37 +368,22 @@ static void parse_arguments(int argc, char *argv[], AFB_session *session) case SET_AUTH_TOKEN: if (optarg == 0) goto needValueForOption; - cliconfig.token = optarg; + session->config->token = optarg; break; case SET_LDPATH: if (optarg == 0) goto needValueForOption; - cliconfig.ldpaths = optarg; - break; - - case SET_PID_FILE: - if (optarg == 0) goto needValueForOption; - cliconfig.pidfile = optarg; + session->config->ldpaths = optarg; break; case SET_SESSION_DIR: if (optarg == 0) goto needValueForOption; - cliconfig.sessiondir = optarg; + session->config->sessiondir = optarg; break; - case SET_CONFIG_FILE: + case SET_CACHE_TIMEOUT: if (optarg == 0) goto needValueForOption; - cliconfig.configfile = optarg; - break; - - case SET_CACHE_TO: - if (optarg == 0) goto needValueForOption; - if (!sscanf (optarg, "%d", &cliconfig.cacheTimeout)) goto notAnInteger; - break; - - case SET_USERID: - if (optarg == 0) goto needValueForOption; - cliconfig.setuid = optarg; + if (!sscanf (optarg, "%d", &session->config->cacheTimeout)) goto notAnInteger; break; case SET_FAKE_MOD: @@ -412,21 +401,11 @@ static void parse_arguments(int argc, char *argv[], AFB_session *session) session->background = 1; break; - case KILL_PREV_REST: - if (optarg != 0) goto noValueForOption; - session->killPrevious = 1; - break; - - case KILL_PREV_EXIT: - if (optarg != 0) goto noValueForOption; - 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; + 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; @@ -449,7 +428,7 @@ static void parse_arguments(int argc, char *argv[], AFB_session *session) free(gnuOptions); // if exist merge config file with CLI arguments - configLoadFile (session, &cliconfig); + config_set_default (session); return; @@ -481,13 +460,14 @@ badMode: 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 (); + session = calloc (1, sizeof (AFB_session)); + session->config = calloc (1, sizeof (AFB_config)); parse_arguments(argc, argv, session); + initPlugins(session); // ------------------ sanity check ---------------------------------------- @@ -502,31 +482,6 @@ int main(int argc, char *argv[]) { // open syslog if ever needed openlog("AFB-log", 0, LOG_DAEMON); - // -------------- Try to kill any previous process if asked --------------------- - if (session->killPrevious) { - 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); - break; - case 0: - 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); - } else { - // try kill -9 - status = kill (pid,9); - if (status != 0) fprintf (stderr, "%s ERR: main failled to killed pid=%d \n",configTime(), pid); - } - } // end switch pid - - if (session->killPrevious >= 2) goto normalExit; - } // end killPrevious - - // ------------------ clean exit on CTR-C signal ------------------------ if (signal (SIGINT, signalQuit) == SIG_ERR) { fprintf (stderr, "%s Quit Signal received.",configTime()); @@ -545,18 +500,6 @@ int main(int argc, char *argv[]) { status=nice (20); // ------------------ Finaly Process Commands ----------------------------- - // if --save then store config on disk upfront - if (session->config->setuid) { - int err; - struct passwd *passwd; - passwd=getpwnam(session->config->setuid); - - if (passwd == NULL) goto errorSetuid; - - err = setuid(passwd->pw_uid); - if (err) goto errorSetuid; - } - // let's not take the risk to run as ROOT //if (getuid() == 0) goto errorNoRoot; @@ -571,10 +514,6 @@ int main(int argc, char *argv[]) { 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()); - if (status == -1) goto errorPidFile; - // enter listening loop in foreground listenLoop(session); goto exitInitLoop; @@ -598,7 +537,6 @@ 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); // redirect default I/O on console close (2); status=dup(consoleFD); // redirect stderr @@ -626,10 +564,6 @@ int main(int argc, char *argv[]) { // if fail nothing much to do if (pid == -1) goto errorFork; - // fork worked and we are in father process - status = writePidFile (session->config, pid); - if (status == -1) goto errorPidFile; - // we are in father process, we don't need this one _exit (0); @@ -641,18 +575,6 @@ normalExit: exit(0); // ------------- 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); - -//errorNoRoot: -// fprintf (stderr,"\nERR:AFB-daemon Not allow to run as root [use --seteuid=username option]\n\n"); -// exit (1); - -errorPidFile: - 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); @@ -671,8 +593,6 @@ errSessiondir: exit (1); exitInitLoop: - // try to unlink pid file if any - if (session->background && session->config->pidfile != NULL) unlink (session->config->pidfile); exit (1); } |