Add -B option to doesn't go in background

This commit is contained in:
Grégory Soutadé 2016-02-04 20:39:50 +01:00
parent feb89ff6b3
commit 148a2a81c8
4 changed files with 39 additions and 22 deletions

View File

@ -1,5 +1,5 @@
/*
File autogenerated by gengetopt version 2.22.6
File autogenerated by gengetopt version 2.22.5
generated with the following command:
gengetopt --input cmdline.ggo
@ -29,8 +29,6 @@ const char *gengetopt_args_info_purpose = "Convert an IP to country code";
const char *gengetopt_args_info_usage = "Usage: ip_to_geo --ip <ipv4>|--daemon [--port <port>] [--bind-ip <ip>]";
const char *gengetopt_args_info_versiontext = "";
const char *gengetopt_args_info_description = "Convert an IP to country code. Currently, onyl IPv4 supported";
const char *gengetopt_args_info_help[] = {
@ -44,10 +42,11 @@ const char *gengetopt_args_info_help[] = {
" -D, --daemon Daemonize (default=off)",
" -p, --port=INT Port (default=`53333')",
" -b, --bind-ip=STRING IP to bind to",
" -B, --no-background Don't go to background for daemon mode \n (default=off)",
"\nAdvanced daemon options:",
" -s, --sockets-per-thread=INT Number of sockets managed by a single thread\n (default=`10')",
" -s, --sockets-per-thread=INT Number of sockets managed by a single thread \n (default=`10')",
" -t, --sockets-timeout=INT Close connection after X seconds (default=`5')",
" -r, --client-max-requests=INT Number of requests allowed for one client\n (default=`100')",
" -r, --client-max-requests=INT Number of requests allowed for one client \n (default=`100')",
0
};
@ -81,6 +80,7 @@ void clear_given (struct gengetopt_args_info *args_info)
args_info->daemon_given = 0 ;
args_info->port_given = 0 ;
args_info->bind_ip_given = 0 ;
args_info->no_background_given = 0 ;
args_info->sockets_per_thread_given = 0 ;
args_info->sockets_timeout_given = 0 ;
args_info->client_max_requests_given = 0 ;
@ -99,6 +99,7 @@ void clear_args (struct gengetopt_args_info *args_info)
args_info->port_orig = NULL;
args_info->bind_ip_arg = NULL;
args_info->bind_ip_orig = NULL;
args_info->no_background_flag = 0;
args_info->sockets_per_thread_arg = 10;
args_info->sockets_per_thread_orig = NULL;
args_info->sockets_timeout_arg = 5;
@ -121,9 +122,10 @@ void init_args_info(struct gengetopt_args_info *args_info)
args_info->daemon_help = gengetopt_args_info_help[7] ;
args_info->port_help = gengetopt_args_info_help[8] ;
args_info->bind_ip_help = gengetopt_args_info_help[9] ;
args_info->sockets_per_thread_help = gengetopt_args_info_help[11] ;
args_info->sockets_timeout_help = gengetopt_args_info_help[12] ;
args_info->client_max_requests_help = gengetopt_args_info_help[13] ;
args_info->no_background_help = gengetopt_args_info_help[10] ;
args_info->sockets_per_thread_help = gengetopt_args_info_help[12] ;
args_info->sockets_timeout_help = gengetopt_args_info_help[13] ;
args_info->client_max_requests_help = gengetopt_args_info_help[14] ;
}
@ -133,9 +135,6 @@ cmdline_parser_print_version (void)
printf ("%s %s\n",
(strlen(CMDLINE_PARSER_PACKAGE_NAME) ? CMDLINE_PARSER_PACKAGE_NAME : CMDLINE_PARSER_PACKAGE),
CMDLINE_PARSER_VERSION);
if (strlen(gengetopt_args_info_versiontext) > 0)
printf("\n%s\n", gengetopt_args_info_versiontext);
}
static void print_help_common(void) {
@ -261,6 +260,8 @@ cmdline_parser_dump(FILE *outfile, struct gengetopt_args_info *args_info)
write_into_file(outfile, "port", args_info->port_orig, 0);
if (args_info->bind_ip_given)
write_into_file(outfile, "bind-ip", args_info->bind_ip_orig, 0);
if (args_info->no_background_given)
write_into_file(outfile, "no-background", 0, 0 );
if (args_info->sockets_per_thread_given)
write_into_file(outfile, "sockets-per-thread", args_info->sockets_per_thread_orig, 0);
if (args_info->sockets_timeout_given)
@ -491,7 +492,7 @@ cmdline_parser_internal (
{
int c; /* Character of the parsed option. */
int error_occurred = 0;
int error = 0;
struct gengetopt_args_info local_args_info;
int override;
@ -529,13 +530,14 @@ cmdline_parser_internal (
{ "daemon", 0, NULL, 'D' },
{ "port", 1, NULL, 'p' },
{ "bind-ip", 1, NULL, 'b' },
{ "no-background", 0, NULL, 'B' },
{ "sockets-per-thread", 1, NULL, 's' },
{ "sockets-timeout", 1, NULL, 't' },
{ "client-max-requests", 1, NULL, 'r' },
{ 0, 0, 0, 0 }
};
c = getopt_long (argc, argv, "hVi:qvDp:b:s:t:r:", long_options, &option_index);
c = getopt_long (argc, argv, "hVi:qvDp:b:Bs:t:r:", long_options, &option_index);
if (c == -1) break; /* Exit from `while (1)' loop. */
@ -616,6 +618,16 @@ cmdline_parser_internal (
additional_error))
goto failure;
break;
case 'B': /* Don't go to background for daemon mode. */
if (update_arg((void *)&(args_info->no_background_flag), 0, &(args_info->no_background_given),
&(local_args_info.no_background_given), optarg, 0, 0, ARG_FLAG,
check_ambiguity, override, 1, 0, "no-background", 'B',
additional_error))
goto failure;
break;
case 's': /* Number of sockets managed by a single thread. */
@ -670,7 +682,7 @@ cmdline_parser_internal (
cmdline_parser_release (&local_args_info);
if ( error_occurred )
if ( error )
return (EXIT_FAILURE);
return 0;

View File

@ -12,6 +12,7 @@ section "Daemon mode"
option "daemon" D "Daemonize" flag off
option "port" p "Port" default="53333" int optional
option "bind-ip" b "IP to bind to" string optional
option "no-background" B "Don't go to background for daemon mode" flag off
section "Advanced daemon options"
option "sockets-per-thread" s "Number of sockets managed by a single thread" default="10" int optional
option "sockets-timeout" t "Close connection after X seconds" default="5" int optional

View File

@ -1,6 +1,6 @@
/** @file cmdline.h
* @brief The header file for the command line option parser
* generated by GNU Gengetopt version 2.22.6
* generated by GNU Gengetopt version 2.22.5
* http://www.gnu.org/software/gengetopt.
* DO NOT modify this file, since it can be overwritten
* @author GNU Gengetopt by Lorenzo Bettini */
@ -54,6 +54,8 @@ struct gengetopt_args_info
char * bind_ip_arg; /**< @brief IP to bind to. */
char * bind_ip_orig; /**< @brief IP to bind to original value given at command line. */
const char *bind_ip_help; /**< @brief IP to bind to help description. */
int no_background_flag; /**< @brief Don't go to background for daemon mode (default=off). */
const char *no_background_help; /**< @brief Don't go to background for daemon mode help description. */
int sockets_per_thread_arg; /**< @brief Number of sockets managed by a single thread (default='10'). */
char * sockets_per_thread_orig; /**< @brief Number of sockets managed by a single thread original value given at command line. */
const char *sockets_per_thread_help; /**< @brief Number of sockets managed by a single thread help description. */
@ -72,6 +74,7 @@ struct gengetopt_args_info
unsigned int daemon_given ; /**< @brief Whether daemon was given. */
unsigned int port_given ; /**< @brief Whether port was given. */
unsigned int bind_ip_given ; /**< @brief Whether bind-ip was given. */
unsigned int no_background_given ; /**< @brief Whether no-background was given. */
unsigned int sockets_per_thread_given ; /**< @brief Whether sockets-per-thread was given. */
unsigned int sockets_timeout_given ; /**< @brief Whether sockets-timeout was given. */
unsigned int client_max_requests_given ; /**< @brief Whether client-max-requests was given. */
@ -92,8 +95,6 @@ struct cmdline_parser_params
extern const char *gengetopt_args_info_purpose;
/** @brief the usage string of the program */
extern const char *gengetopt_args_info_usage;
/** @brief the description string of the program */
extern const char *gengetopt_args_info_description;
/** @brief all the lines making the help output */
extern const char *gengetopt_args_info_help[];

View File

@ -375,12 +375,15 @@ int daemonize(struct gengetopt_args_info* params)
return -3;
}
ret = daemon(0, 0);
if (ret)
if (!params->no_background_flag)
{
if (!params->quiet_flag)
fprintf(stderr, "Daemon error (%m)\n");
return -4;
ret = daemon(0, 0);
if (ret)
{
if (!params->quiet_flag)
fprintf(stderr, "Daemon error (%m)\n");
return -4;
}
}
openlog("ip_to_geod", 0, LOG_DAEMON);