summaryrefslogtreecommitdiff
path: root/cleopatre
diff options
context:
space:
mode:
authorThierry Carré2013-03-19 11:24:57 +0100
committerThierry Carré2013-04-26 17:03:09 +0200
commit992be6541ecc5d375a2731242ffbe77c7e40f8fc (patch)
tree849f85f1159510b61cb3bd4a000180df582ab7c7 /cleopatre
parentf96fcead72d958665fed2ccc200a6955afe979b2 (diff)
cleo/devkit/plcd: avoid exit function in main()
All 'plcd_main_task_fct' must return, like that we have a proper end of program (with close, and uninit).
Diffstat (limited to 'cleopatre')
-rw-r--r--cleopatre/devkit/plcd/inc/plcd_ctx.h2
-rw-r--r--cleopatre/devkit/plcd/src/plcd_main.c81
2 files changed, 56 insertions, 27 deletions
diff --git a/cleopatre/devkit/plcd/inc/plcd_ctx.h b/cleopatre/devkit/plcd/inc/plcd_ctx.h
index eddf4669cd..af23c883c9 100644
--- a/cleopatre/devkit/plcd/inc/plcd_ctx.h
+++ b/cleopatre/devkit/plcd/inc/plcd_ctx.h
@@ -92,6 +92,8 @@ typedef struct
/** main context of plc daemon */
typedef struct
{
+ /** Pointer on the program name (argv[0]). */
+ const char *my_name;
/** full path of HPAV info file */
const char *hpav_info_path;
/** full path of HPAV config file */
diff --git a/cleopatre/devkit/plcd/src/plcd_main.c b/cleopatre/devkit/plcd/src/plcd_main.c
index 1140fae52d..2a2041697c 100644
--- a/cleopatre/devkit/plcd/src/plcd_main.c
+++ b/cleopatre/devkit/plcd/src/plcd_main.c
@@ -40,6 +40,8 @@
/* "utest_override" include must be the last include. */
#include "utest_override.h"
+typedef void (*plcd_main_task_fct_t)(plcd_ctx_t *ctx);
+
/* Interface name */
#define PLC_IFNAME "plc0"
@@ -61,18 +63,31 @@ nvram_init (plcd_nvram_t *nvram)
}
/**
+ * Print plcd version.
+ *
+ * \param ctx plc daemon context.
+ */
+static void
+plcd_main_print_version (plcd_ctx_t *ctx)
+{
+ fprintf (stdout, "%s\n", VERSION);
+}
+
+/**
* Print help for plcd usage.
*
- * \param cmd command
+ * \param ctx plc daemon context.
*/
static void
-plcd_print_usage (const char *cmd)
+plcd_main_print_usage (plcd_ctx_t *ctx)
{
+ PLCD_ASSERT (ctx);
+
fprintf (stderr, "Usage : %s " \
"[ -c --conf_file hpav_conf_file ]" \
"[ -i --info_file hpav_info_file ]" \
"[ -v --version show version number ]\n",
- cmd);
+ ctx->my_name);
}
/**
@@ -277,9 +292,10 @@ plcd_daemon_main (plcd_ctx_t *ctx)
* \param ctx plcd context
*/
static void
-plcd_main_init (plcd_ctx_t *ctx)
+plcd_main_init (plcd_ctx_t *ctx, const char *name)
{
PLCD_ASSERT (ctx);
+ PLCD_ASSERT (name);
memset (ctx, 0, sizeof (plcd_ctx_t));
@@ -287,6 +303,7 @@ plcd_main_init (plcd_ctx_t *ctx)
ctx->hpav_conf_path = LIBSPID_HPAV_CONF_PATH;
ctx->phy_conf_path = LIBSPID_PHY_CONF_PATH;
ctx->internal_conf_path = LIBSPID_INTERNAL_CONF_PATH;
+ ctx->my_name = name;
}
/**
@@ -315,45 +332,55 @@ main (int argc, char **argv)
{
/* Main context. */
static plcd_ctx_t plcd_ctx;
- int c = 0, opt_index = 0;
/* Open log process. */
openlog (argv[0], 0, LOG_DAEMON);
- plcd_main_init (&plcd_ctx);
+ plcd_main_init (&plcd_ctx, argv[0]);
/* input program arguments */
struct option long_opts[] = {{"conf_file", required_argument, NULL, 'c'},
{"info_file", required_argument, NULL, 'i'},
{"version", no_argument, NULL, 'v'}};
- /* process options */
- while (-1 != (c = getopt_long_only (argc, argv, "c:i:v:", long_opts,
- &opt_index)))
+ /* Parse options, and override default configuration.*/
+ int opt, opt_index = 0;
+ plcd_main_task_fct_t plcd_main_task_fct = &plcd_daemon_main;
+ while (-1 != (opt = getopt_long_only (
+ argc, argv, "c:i:v:",
+ long_opts, &opt_index)))
{
- switch(c)
+ switch (opt)
{
- case 'c':
- /* HPAV config file */
- plcd_ctx.hpav_conf_path = optarg;
- break;
- case 'i':
- /* HPAV info file */
- plcd_ctx.hpav_info_path = optarg;
- break;
- case 'v':
- /* version number */
- fprintf (stdout, "%s\n", VERSION);
- exit (EXIT_SUCCESS);
- default:
- plcd_print_usage (argv[0]);
- exit (EXIT_SUCCESS);
+ case 'c':
+ /* HPAV config file. */
+ plcd_ctx.hpav_conf_path = optarg;
+ break;
+
+ case 'i':
+ /* HPAV info file. */
+ plcd_ctx.hpav_info_path = optarg;
+ break;
+
+ case 'v':
+ plcd_main_task_fct = &plcd_main_print_version;
+ break;
+
+ default:
+ plcd_main_task_fct = &plcd_main_print_usage;
+ break;
}
+
+ if (plcd_main_task_fct != &plcd_daemon_main)
+ break;
}
- plcd_daemon_main (&plcd_ctx);
+ if (plcd_main_task_fct)
+ plcd_main_task_fct (&plcd_ctx);
+ else
+ syslog (LOG_WARNING, "Undefined main task to do");
plcd_main_uninit (&plcd_ctx);
closelog ();
- exit (EXIT_SUCCESS);
+ return EXIT_SUCCESS;
}