diff options
-rw-r--r-- | lib/libmf.h | 32 | ||||
-rw-r--r-- | lib/optcache.c | 76 | ||||
-rw-r--r-- | lib/utils.c | 43 | ||||
-rw-r--r-- | src/main.c | 32 | ||||
-rw-r--r-- | src/mfdbtool.c | 13 | ||||
-rw-r--r-- | src/srvcfg.c | 75 |
6 files changed, 127 insertions, 144 deletions
diff --git a/lib/libmf.h b/lib/libmf.h index 96421687..4344d1a6 100644 --- a/lib/libmf.h +++ b/lib/libmf.h @@ -291,9 +291,19 @@ void mf_server_start(const char *program, const char *dir, /* optcache.c */ +union mf_option_value { + int ov_bool; + char *ov_string; + time_t ov_time; + size_t ov_size; + mu_list_t ov_list; +}; + struct mf_option_cache { - char *name; /* option name */ - void *value; /* current value */ - int (*handler)(char *opt, void **pval, char *newval); + char *name; /* option name */ + int (*handler)(char const *opt, union mf_option_value *pval, char const *arg); /* handler function, which verifies and assigns - newval to pval. */ - void (*set)(void *value); /* function that actually sets the option */ + arg to pval. */ + void (*set)(union mf_option_value *value); + /* function that actually sets the option */ + union mf_option_value value; /* current value */ + int isset; /* is the value set */ }; @@ -304,9 +314,9 @@ struct mf_option_cache { void mf_optcache_add(struct mf_option_cache *tab, size_t size, int flags); -int mf_optcache_set_option(char *name, char *value); +int mf_optcache_set_option(char const *name, char const *value); void mf_optcache_flush(void); -int mf_option_string(char *opt, void **pval, char *newval); -int mf_option_boolean(char *opt, void **pval, char *newval); -int mf_option_time(char *opt, void **pval, char *newval); -int mf_option_time_t(char *opt, void **pval, char *newval); -int mf_option_size_t(char *opt, void **pval, char *newval); +int mf_option_string(char const *opt, union mf_option_value *val, char const *arg); +int mf_option_boolean(char const *opt, union mf_option_value *val, char const *arg); +int mf_option_timeout(char const *opt, union mf_option_value *val, char const *arg); +int mf_option_size(char const *opt, union mf_option_value *val, char const *arg); + diff --git a/lib/optcache.c b/lib/optcache.c index a059b29a..15b011b5 100644 --- a/lib/optcache.c +++ b/lib/optcache.c @@ -44,2 +44,3 @@ optcache_dup(struct mf_option_cache *tab, size_t size) newtab[i].name = mu_strdup(newtab[i].name); + newtab[i].isset = 0; } @@ -74,3 +75,3 @@ mf_optcache_add(struct mf_option_cache *tab, size_t size, int flags) static struct mf_option_cache * -find_option(char *name) +find_option(char const *name) { @@ -91,4 +92,5 @@ find_option(char *name) int -mf_optcache_set_option(char *name, char *value) +mf_optcache_set_option(char const *name, char const *value) { + int rc; struct mf_option_cache *p = find_option(name); @@ -98,3 +100,6 @@ mf_optcache_set_option(char *name, char *value) } - return p->handler(name, &p->value, value); + rc = p->handler(name, &p->value, value); + if (rc == 0) + p->isset = 1; + return rc; } @@ -111,4 +116,4 @@ mf_optcache_flush() for (i = 0, opt = elt->opt; i < elt->size; i++, opt++) - if (opt->value && opt->set) - opt->set(opt->value); + if (opt->isset && opt->set) + opt->set(&opt->value); } @@ -118,7 +123,6 @@ mf_optcache_flush() int -mf_option_string(char *opt, void **pval, char *newval) +mf_option_string(char const *opt, union mf_option_value *val, char const *arg) { - if (*pval) - free(*pval); - *pval = strdup(newval); + free(val->ov_string); + val->ov_string = strdup(arg); return 0; @@ -127,14 +131,14 @@ mf_option_string(char *opt, void **pval, char *newval) int -mf_option_boolean(char *opt, void **pval, char *newval) +mf_option_boolean(char const *opt, union mf_option_value *val, char const *arg) { - int val; - - if (strcmp (newval, "yes") == 0 - || strcmp (newval, "true") == 0 - || strcmp (newval, "t") == 0) - val = 1; - else if (strcmp (newval, "no") == 0 - || strcmp (newval, "false") == 0 - || strcmp (newval, "nil") == 0) - val = 0; + int b; + + if (strcmp (arg, "yes") == 0 + || strcmp (arg, "true") == 0 + || strcmp (arg, "t") == 0) + b = 1; + else if (strcmp (arg, "no") == 0 + || strcmp (arg, "false") == 0 + || strcmp (arg, "nil") == 0) + b = 0; else { @@ -142,3 +146,3 @@ mf_option_boolean(char *opt, void **pval, char *newval) - val = strtoul(newval, &p, 10); + b = strtoul(arg, &p, 10); if (*p) { @@ -149,3 +153,3 @@ mf_option_boolean(char *opt, void **pval, char *newval) - *pval = (void*) val; + val->ov_bool = b; return 0; @@ -154,3 +158,3 @@ mf_option_boolean(char *opt, void **pval, char *newval) int -mf_option_time(char *opt, void **pval, char *newval) +mf_option_timeout(char const *opt, union mf_option_value *val, char const *arg) { @@ -158,3 +162,4 @@ mf_option_time(char *opt, void **pval, char *newval) const char *endp; - if (parse_time_interval(newval, &interval, &endp)) { + + if (parse_time_interval(arg, &interval, &endp)) { mu_error(_("%s: unrecognized time format (near `%s')"), @@ -163,5 +168,3 @@ mf_option_time(char *opt, void **pval, char *newval) } - if (!*pval) - *pval = mu_alloc(sizeof(time_t)); - *(time_t*) *pval = interval; + val->ov_time = interval; return 0; @@ -170,16 +173,9 @@ mf_option_time(char *opt, void **pval, char *newval) int -mf_option_time_t(char *opt, void **pval, char *newval) +mf_option_size(char const *opt, union mf_option_value *val, char const *arg) { - if (!*pval) - *pval = mu_alloc(sizeof(time_t)); - memcpy(*pval, newval, sizeof(time_t)); - return 0; -} - -int -mf_option_size_t(char *opt, void **pval, char *newval) -{ - if (!*pval) - *pval = mu_alloc(sizeof(size_t)); - memcpy(*pval, newval, sizeof(size_t)); + char *p; + unsigned long n = strtoul(arg, &p, 10); + if (*p) + return 1; + val->ov_size = n; return 0; diff --git a/lib/utils.c b/lib/utils.c index 3fc123a3..6a19e505 100644 --- a/lib/utils.c +++ b/lib/utils.c @@ -116,12 +116,2 @@ config_cb_timeout (struct timeval *pt, mu_config_value_t *val) int -config_cb_time_t(void *data, mu_config_value_t *arg) -{ - struct timeval tv; - int rc = config_cb_timeout (&tv, arg); - if (rc == 0) - *(time_t*) data = tv.tv_sec; - return rc; -} - -int config_cb_ignore(void *data, mu_config_value_t *val) @@ -137,9 +127,5 @@ config_cb_lock_retry_count(void *data, mu_config_value_t *val) { - char *p; - size_t n; - if (mu_cfg_assert_value_type(val, MU_CFG_STRING)) return 1; - n = strtoul(val->v.string, &p, 10); - if (*p) { + if (mf_optcache_set_option("lock-retry-count", val->v.string)) { mu_error(_("not a number")); @@ -147,3 +133,2 @@ config_cb_lock_retry_count(void *data, mu_config_value_t *val) } - mf_optcache_set_option("lock-retry-count", (void*)&n); return 0; @@ -154,9 +139,3 @@ config_cb_lock_retry_timeout(void *data, mu_config_value_t *val) { - int rc; - time_t t; - - rc = config_cb_time_t(&t, val); - if (rc == 0) - mf_optcache_set_option("lock-retry-timeout", (void*)&t); - return rc; + return mf_optcache_set_option("lock-retry-timeout", val->v.string); } @@ -164,7 +143,5 @@ config_cb_lock_retry_timeout(void *data, mu_config_value_t *val) static void -set_lock_retry_count(void *value) +set_lock_retry_count(union mf_option_value *val) { - size_t *n = value; - mu_locker_set_default_retry_count(*n); - free(value); + mu_locker_set_default_retry_count(val->ov_size); } @@ -172,7 +149,5 @@ set_lock_retry_count(void *value) static void -set_lock_retry_timeout(void *value) +set_lock_retry_timeout(union mf_option_value *val) { - time_t *t = value; - mu_locker_set_default_retry_timeout(*t); - free(value); + mu_locker_set_default_retry_timeout(val->ov_time); } @@ -180,6 +155,4 @@ set_lock_retry_timeout(void *value) static struct mf_option_cache lock_option_cache[] = { - { "lock-retry-count", NULL, mf_option_size_t, - set_lock_retry_count }, - { "lock-retry-timeout", NULL, mf_option_time_t, - set_lock_retry_timeout }, + { "lock-retry-count", mf_option_size, set_lock_retry_count }, + { "lock-retry-timeout", mf_option_timeout, set_lock_retry_timeout }, { NULL } @@ -261,8 +261,7 @@ host_in_relayed_domain_p(char *client) static void -set_milter_timeout(void *value) +set_milter_timeout(union mf_option_value *val) { - time_t to = *(time_t*) value; - free(value); - if (smfi_settimeout(to) == MI_FAILURE) { - mu_error(_("invalid milter timeout: %lu"), (unsigned long) to); + if (smfi_settimeout(val->ov_time) == MI_FAILURE) { + mu_error(_("invalid milter timeout: %lu"), + (unsigned long)val->ov_time); exit(EX_USAGE); @@ -279,5 +278,6 @@ load_relay_file(void *item, void *data) static void -set_relay(void *value) +set_relay(union mf_option_value *val) { - mu_list_foreach(value, load_relay_file, NULL); + mu_list_foreach(val->ov_list, load_relay_file, NULL); + mu_list_destroy(&val->ov_list); } @@ -285,5 +285,5 @@ set_relay(void *value) void -set_stack_trace(void *value) +set_stack_trace(union mf_option_value *val) { - stack_trace_option = (int) value; + stack_trace_option = val->ov_bool; } @@ -291,7 +291,7 @@ set_stack_trace(void *value) static int -option_relay(char *opt, void **pval, char *newval) +option_relay(char const *opt, union mf_option_value *val, char const *newval) { - if (!*pval) - mu_list_create((mu_list_t*)pval); - mu_list_append(*pval, strdup(newval)); + if (!val->ov_list) + mu_list_create(&val->ov_list); + mu_list_append(val->ov_list, strdup(newval)); return 0; @@ -300,5 +300,5 @@ option_relay(char *opt, void **pval, char *newval) struct mf_option_cache option_cache[] = { - { "stack-trace", NULL, mf_option_boolean, set_stack_trace }, - { "milter-timeout", NULL, mf_option_time, set_milter_timeout }, - { "relay", NULL, option_relay, set_relay }, + { "stack-trace", mf_option_boolean, set_stack_trace }, + { "milter-timeout", mf_option_timeout, set_milter_timeout }, + { "relay", option_relay, set_relay }, { NULL } diff --git a/src/mfdbtool.c b/src/mfdbtool.c index dd723c38..4af386ad 100644 --- a/src/mfdbtool.c +++ b/src/mfdbtool.c @@ -360,5 +360,5 @@ struct mu_cfg_param mfdbtool_cfg_param[] = { static void -set_state_directory(void *value) +set_state_directory(union mf_option_value *val) { - state_dir = value; + state_dir = val->ov_string; } @@ -366,5 +366,6 @@ set_state_directory(void *value) static void -set_debug(void *value) +set_debug(union mf_option_value *val) { - mu_debug_parse_spec(value); + mu_debug_parse_spec(val->ov_string); + free(val->ov_string); } @@ -372,4 +373,4 @@ set_debug(void *value) static struct mf_option_cache option_cache[] = { - { "state-directory", NULL, mf_option_string, set_state_directory }, - { "debug", NULL, mf_option_string, set_debug }, + { "state-directory", mf_option_string, set_state_directory }, + { "debug", mf_option_string, set_debug }, diff --git a/src/srvcfg.c b/src/srvcfg.c index 40d56b89..4b338f82 100644 --- a/src/srvcfg.c +++ b/src/srvcfg.c @@ -139,5 +139,6 @@ parse_milter_url(const char *str) static void -set_debug(void *value) +set_debug(union mf_option_value *val) { - mu_debug_parse_spec(value); + mu_debug_parse_spec(val->ov_string); + free(val->ov_string); } @@ -145,5 +146,5 @@ set_debug(void *value) void -set_source_info(void *value) +set_source_info(union mf_option_value *val) { - mu_debug_line_info = (int) value; + mu_debug_line_info = val->ov_bool; } @@ -151,5 +152,5 @@ set_source_info(void *value) static void -set_user(void *value) +set_user(union mf_option_value *val) { - mf_server_user = value; + mf_server_user = val->ov_string; } @@ -187,5 +188,5 @@ mf_option_group(const char *arg) static int -option_group(char *opt, void **pval, char *newval) +option_group(char const *opt, union mf_option_value *val, char const *arg) { - return mf_option_group(newval); + return mf_option_group(arg); } @@ -193,5 +194,5 @@ option_group(char *opt, void **pval, char *newval) static int -option_pidfile(char *opt, void **pval, char *newval) +option_pidfile(char const *opt, union mf_option_value *val, char const *arg) { - if (newval[0] != '/') { + if (arg[0] != '/') { mu_error(_("invalid pidfile name: must be absolute")); @@ -199,3 +200,3 @@ option_pidfile(char *opt, void **pval, char *newval) } - return mf_option_string(opt, pval, newval); + return mf_option_string(opt, val, arg); } @@ -203,5 +204,5 @@ option_pidfile(char *opt, void **pval, char *newval) static void -set_pidfile(void *value) +set_pidfile(union mf_option_value *val) { - pidfile = value; + pidfile = val->ov_string; } @@ -209,6 +210,5 @@ set_pidfile(void *value) static void -set_io_timeout(void *value) +set_io_timeout(union mf_option_value *val) { - io_timeout = *(time_t*) value; - free(value); + io_timeout = val->ov_time; } @@ -216,5 +216,5 @@ set_io_timeout(void *value) static void -set_logger_option(void *value) +set_logger_option(union mf_option_value *val) { - log_stream = value; + log_stream = val->ov_string; } @@ -245,3 +245,3 @@ mf_option_state_directory(const char *arg) void -set_state_directory(void *value) +set_state_directory(union mf_option_value *val) { @@ -251,5 +251,6 @@ set_state_directory(void *value) int -option_state_directory(char *opt, void **pval, char *newval) +option_state_directory(char const *opt, union mf_option_value *val, + char const *arg) { - return mf_option_state_directory(newval); + return mf_option_state_directory(arg); } @@ -284,5 +285,6 @@ mf_srvcfg_add(const char *type, const char *urlstr) static void -set_port(void *value) +set_port(union mf_option_value *val) { - mf_srvcfg_add("default", value); + mf_srvcfg_add("default", val->ov_string); + free(val->ov_string); } @@ -290,3 +292,3 @@ set_port(void *value) static void -set_source_ip(void *value) +set_source_ip(union mf_option_value *val) { @@ -299,3 +301,3 @@ set_source_ip(void *value) hints.protocol = IPPROTO_TCP; - rc = mu_sockaddr_from_node(&source_address, (char*)value, NULL, + rc = mu_sockaddr_from_node(&source_address, val->ov_string, NULL, &hints); @@ -303,4 +305,5 @@ set_source_ip(void *value) mu_error(_("cannot convert %s to sockaddr: %s"), - (char*)value, mu_strerror(rc)); + val->ov_string, mu_strerror(rc)); } + free(val->ov_string); } @@ -308,13 +311,13 @@ set_source_ip(void *value) static struct mf_option_cache srv_option_cache[] = { - { "debug", NULL, mf_option_string, set_debug }, - { "source-info", NULL, mf_option_boolean, set_source_info }, - { "user", NULL, mf_option_string, set_user }, - { "group", NULL, option_group, NULL }, - { "pidfile", NULL, option_pidfile, set_pidfile }, - { "source-ip", NULL, mf_option_string, set_source_ip }, - { "io-timeout", NULL, mf_option_time, set_io_timeout }, - { "logger", NULL, mf_option_string, set_logger_option }, - { "state-directory", NULL, option_state_directory, + { "debug", mf_option_string, set_debug }, + { "source-info", mf_option_boolean, set_source_info }, + { "user", mf_option_string, set_user }, + { "group", option_group, NULL }, + { "pidfile", option_pidfile, set_pidfile }, + { "source-ip", mf_option_string, set_source_ip }, + { "io-timeout", mf_option_timeout, set_io_timeout }, + { "logger", mf_option_string, set_logger_option }, + { "state-directory", option_state_directory, set_state_directory }, - { "port", NULL, mf_option_string, set_port }, + { "port", mf_option_string, set_port }, { NULL } |