[Listener] Daemon mode (-D)

Basic implementation of the daemon mode.
This commit is contained in:
Matteo Cypriani 2012-01-07 16:02:05 +01:00
parent cf73bf6309
commit 2ed7ccd4bc
2 changed files with 32 additions and 7 deletions

View File

@ -62,7 +62,7 @@
/* Arguments & program configuration */ /* Arguments & program configuration */
#define OPTIONS "a:Af:hH:i:I:Kl:m:n:p:P:qr:t:vVw:" // getopt string #define OPTIONS "a:ADf:hH:i:I:Kl:m:n:p:P:qr:t:vVw:" // getopt string
#define DEFAULT_CONFIG_FILE "/usr/local/etc/owlps/owlps-listener.conf" #define DEFAULT_CONFIG_FILE "/usr/local/etc/owlps/owlps-listener.conf"
enum {MODE_ACTIVE = 'a', MODE_PASSIVE = 'p', MODE_MIXED = 'm'} ; enum {MODE_ACTIVE = 'a', MODE_PASSIVE = 'p', MODE_MIXED = 'm'} ;
#define DEFAULT_AUTOCALIBRATION_HELLO_DELAY 120 // seconds #define DEFAULT_AUTOCALIBRATION_HELLO_DELAY 120 // seconds
@ -213,6 +213,10 @@ void print_version(void) ;
(cfg_setint(cfg, "mode", (MODE))) (cfg_setint(cfg, "mode", (MODE)))
#define GET_MODE() \ #define GET_MODE() \
(cfg_getint(cfg, "mode")) (cfg_getint(cfg, "mode"))
#define SET_DAEMON() \
(cfg_setbool(cfg, "daemon", cfg_true))
#define GET_DAEMON() \
(cfg_getbool(cfg, "daemon"))
#define SET_AGGREGATION_IP(IP) \ #define SET_AGGREGATION_IP(IP) \
(cfg_setstr(cfg, "aggregation_ip", (IP))) (cfg_setstr(cfg, "aggregation_ip", (IP)))
#define GET_AGGREGATION_IP() \ #define GET_AGGREGATION_IP() \
@ -296,6 +300,10 @@ void print_version(void) ;
/* Home-made structure macros */ /* Home-made structure macros */
#else // USE_CONFIG_FILE #else // USE_CONFIG_FILE
#define SET_DAEMON() \
(options.daemon = owl_true)
#define GET_DAEMON() \
(options.daemon)
#define SET_MODE(MODE) \ #define SET_MODE(MODE) \
(options.mode = (MODE)) (options.mode = (MODE))
#define GET_MODE() \ #define GET_MODE() \

View File

@ -60,6 +60,7 @@ cfg_t *cfg = NULL ; // Configuration structure
* options. * options.
*/ */
struct { struct {
owl_bool daemon ;
char mode ; char mode ;
char aggregation_ip[INET_ADDRSTRLEN] ; char aggregation_ip[INET_ADDRSTRLEN] ;
uint_fast16_t aggregation_port ; uint_fast16_t aggregation_port ;
@ -84,6 +85,7 @@ struct {
#endif // USE_PTHREAD #endif // USE_PTHREAD
uint_fast8_t verbose ; uint_fast8_t verbose ;
} options = { // Initalise default options: } options = { // Initalise default options:
owl_false,
MODE_ACTIVE, MODE_ACTIVE,
"127.0.0.1", "127.0.0.1",
OWL_DEFAULT_LISTENER_PORT, OWL_DEFAULT_LISTENER_PORT,
@ -183,6 +185,14 @@ int main(int argc, char *argv[])
} }
#endif // USE_PTHREAD #endif // USE_PTHREAD
if (GET_DAEMON())
{
if (VERBOSE_WARNING)
fprintf(stderr, "Detaching to background...\n") ;
if (daemon(0, 0))
perror("Cannot daemonize") ;
}
ret = capture() ; // Capture loop ret = capture() ; // Capture loop
/* Wait for the threads to terminate */ /* Wait for the threads to terminate */
@ -290,6 +300,8 @@ int parse_config_file(int argc, char **argv)
// If we use libconfuse, we declare options: // If we use libconfuse, we declare options:
cfg_opt_t opts[] = cfg_opt_t opts[] =
{ {
// Daemon mode:
CFG_BOOL("daemon", cfg_false, CFGF_NONE),
// Listening mode: a for active, p for passive, m for mixed // Listening mode: a for active, p for passive, m for mixed
// (default: a): // (default: a):
CFG_INT("mode", MODE_ACTIVE, CFGF_NONE), CFG_INT("mode", MODE_ACTIVE, CFGF_NONE),
@ -456,6 +468,9 @@ int parse_main_options(int argc, char **argv)
SET_AUTOCALIBRATION_PORT(strtol(optarg, NULL, 0)) ; SET_AUTOCALIBRATION_PORT(strtol(optarg, NULL, 0)) ;
#endif // USE_PTHREAD #endif // USE_PTHREAD
break ; break ;
case 'D' :
SET_DAEMON() ;
break ;
case 'f' : // Config file case 'f' : // Config file
break ; // (already parsed) break ; // (already parsed)
case 'H' : case 'H' :
@ -1451,6 +1466,7 @@ void print_usage()
printf("Usage :\n" printf("Usage :\n"
"\t%s" "\t%s"
" [-f config_file]" " [-f config_file]"
" [-D]"
" [-m mode]" " [-m mode]"
" [-i aggregation_ip]" " [-i aggregation_ip]"
" [-l listening_port]" " [-l listening_port]"
@ -1476,6 +1492,12 @@ void print_usage()
"\t-f config_file\tUse 'config_file' instead of the default" "\t-f config_file\tUse 'config_file' instead of the default"
" configuration file (%s). Available only if program was" " configuration file (%s). Available only if program was"
" compiled with libconfuse.\n" " compiled with libconfuse.\n"
"\t-D\t\tDaemon mode.\n"
"\t-v\t\tBe verbose. You can use this option up to 3 times to"
" increase the level of verbosity (1 = warnings, 2 = useful"
" information, 3 = a lot of information, 4 = display each"
" captured packet).\n"
"\t-q\t\tQuiet mode (default): sets the verbose level to 0.\n"
"Capture options:\n" "Capture options:\n"
"\t-m mode\t\t\tCapture mode: a(ctive), p(assive), m(ixed)" "\t-m mode\t\t\tCapture mode: a(ctive), p(assive), m(ixed)"
@ -1510,16 +1532,11 @@ void print_usage()
"\tdirection x y z\t\tThe coordinates of the listener" "\tdirection x y z\t\tThe coordinates of the listener"
" (direction is an integer; x, y, z are floats).\n" " (direction is an integer; x, y, z are floats).\n"
"Other options:\n" "Miscelanneous options:\n"
"\t-K\tKeep the monitor mode up on wifi_iface. Use it with buggy" "\t-K\tKeep the monitor mode up on wifi_iface. Use it with buggy"
" drivers that disable monitor mode periodically. Available" " drivers that disable monitor mode periodically. Available"
" only if the program was compiled with the option" " only if the program was compiled with the option"
" ENABLE_KEEP_MONITOR.\n" " ENABLE_KEEP_MONITOR.\n"
"\t-v\tBe verbose. You can use this option up to 3 times to"
" increase the level of verbosity (1 = warnings, 2 = useful"
" information, 3 = a lot of information, 4 = display each"
" captured packet).\n"
"\t-q\tQuiet mode (default): sets the verbose level to 0.\n"
, ,
program_name, program_name,
program_name, program_name,