[Client] Daemon mode (-D)

This commit is contained in:
Matteo Cypriani 2012-01-07 17:16:47 +01:00
parent a4857e4b1f
commit 327f8ae199
1 changed files with 34 additions and 7 deletions

View File

@ -27,7 +27,7 @@
#define DEFAULT_FLOOD_DELAY 1000
/* Program arguments (getopt string) */
#define OPTIONS "F::hi:I:l::n:p:t:V"
#define OPTIONS "DF::hi:I:l::n:p:t:V"
/* Function headers */
@ -49,6 +49,7 @@ void print_version(void) ;
/* Options */
struct {
owl_bool daemon ;
char dest_ip[INET_ADDRSTRLEN] ; // Destination IP of the packets
uint_fast16_t dest_port ;
char iface[IFNAMSIZ + 1] ; // Source network interface
@ -62,6 +63,7 @@ struct {
float y ;
float z ;
} options = {
owl_false,
"",
OWL_DEFAULT_REQUEST_PORT,
"",
@ -101,6 +103,13 @@ int main(int argc, char *argv[])
action.sa_handler = owl_sigterm_handler ;
sigaction(SIGTERM, &action, NULL) ;
if (options.daemon)
{
fprintf(stderr, "Detaching to background...\n") ;
if (daemon(0, 0))
perror("Cannot daemonize") ;
}
create_socket() ;
send_request() ;
@ -143,6 +152,9 @@ void parse_main_options(int argc, char **argv)
{
switch (opt)
{
case 'D' :
options.daemon = owl_true ;
break ;
case 'F' :
/* Facultative getopt options do not handle separated values
* (like -F <delay>), so we have to test separately.
@ -320,15 +332,28 @@ void check_configuration()
options.listening_port = 0 ;
}
// We want to flood AND to be located, which is not allowed:
if (options.flood_delay >= 0 && options.listening_port > 0)
if (options.flood_delay >= 0)
{
// We want to flood AND to be located, which is not allowed:
if (options.listening_port > 0)
{
#ifdef DEBUG
fprintf(stderr, "Warning! You cannot wait for a server answer when"
" you flood. Option -l ignored…\n") ;
fprintf(stderr, "Warning! You cannot wait for a server answer"
" when you flood. Option -l ignored…\n") ;
#endif // DEBUG
options.listening_port = 0 ;
options.listening_port = 0 ;
}
}
else // Flood is unactivated
if (options.daemon)
{
#ifdef DEBUG
fprintf(stderr, "Warning! It is useless to detach from"
" the foreground if the flood mode is not activated"
" Option -D ignored…\n") ;
#endif // DEBUG
options.daemon = owl_false ;
}
}
@ -491,7 +516,7 @@ void print_usage()
" [-I iface]"
" [-t delay]"
" [-n nb_packets]"
" [-F [delay]]"
" [-F [delay] [-D]]"
" [-l [port]]\n"
"Calibration request:\n"
"\t%s"
@ -500,6 +525,7 @@ void print_usage()
" [-I iface]"
" [-t delay]"
" [-n nb_packets]"
" [-F [delay] [-D]]"
" direction x y z\n"
"\n"
"Options:\n"
@ -521,6 +547,7 @@ void print_usage()
" option.\n"
"\t-F [delay]\t\"Flood mode\": loop undefinitely, sending a"
" new request every <delay> milliseconds (default: %d ms).\n"
"\t-D\t\tDaemon mode. Useful only in flood mode.\n"
"\t-l [port]\tWait for the computed position and display it."
" The Optional argument <port> allows to specify the listening"
" port (default: %d).\n"