From: Maciej Slodczyk Date: Wed, 29 Aug 2018 12:36:03 +0000 (+0200) Subject: dlogfilterctl: allow dumping all rules X-Git-Tag: accepted/tizen/unified/20180904.062847~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=d45fb36d1b17ee9aab796b4ce56d1b166d0da015;p=platform%2Fcore%2Fsystem%2Fdlog.git dlogfilterctl: allow dumping all rules Change-Id: I2f128778561b520a6cf8f8ac767ee8d5579c03d7 Signed-off-by: Maciej Slodczyk --- diff --git a/include/loglimiter.h b/include/loglimiter.h index 81ea3f8..1a290fc 100644 --- a/include/loglimiter.h +++ b/include/loglimiter.h @@ -28,6 +28,8 @@ extern "C" { #include +struct rule; + struct limiter_limits { int tag_and_prio; int tag; @@ -45,6 +47,8 @@ struct limiter_limits __log_limiter_get_limits(); void __log_limiter_update(struct log_config *config); +int __log_limiter_dump_rule(struct rule **, char *, const size_t); + #ifdef __cplusplus } #endif diff --git a/src/libdlog/loglimiter.c b/src/libdlog/loglimiter.c index a280654..14e64f5 100644 --- a/src/libdlog/loglimiter.c +++ b/src/libdlog/loglimiter.c @@ -479,3 +479,41 @@ void __log_limiter_update(struct log_config *config) rules_destroy(¤t_rules_table); current_rules_table = rules_table; } + +/** + * @brief Dump a limiter rule to provided buffer + * @details Formats a string containing information about a rule and inserts it into provided buffer + * @param[in,out] r The pointer to the rule to be dumped (in), the pointer to the previous rule (out) + * @param[in,out] buf The buffer to dump the rule into + * @param[in] size The size of the buffer + * @returns 0 on success, -1 on failure + */ +int __log_limiter_dump_rule(struct rule **r, char *buf, const size_t size) +{ + struct rule *ruleptr = *r ? : current_rules_table; + int s = 0; + + if (!ruleptr) { + *r = NULL; + return 0; + } + + if (ruleptr->limit == 0) + s = snprintf(buf, size, "Deny "); + else if (ruleptr->limit == (__LOG_LIMITER_LIMIT_MAX + 1)) + s = snprintf(buf, size, "Allow "); + else + s = snprintf(buf, size, "%d logs/minute ", ruleptr->limit); + + if (s < 0) + return -1; + + s = snprintf(buf + s, size - s, "for %s:%c", + ruleptr->tag, + util_prio_to_char(ruleptr->prio)); + if (s < 0) + return -1; + + *r = ruleptr->prev; + return 0; +} diff --git a/src/logfilterctl/logfilterctl.c b/src/logfilterctl/logfilterctl.c index f9e264f..684504e 100644 --- a/src/logfilterctl/logfilterctl.c +++ b/src/logfilterctl/logfilterctl.c @@ -21,6 +21,7 @@ struct parsed_params { ACTION_NONE = 0, ACTION_GET, ACTION_SET, + ACTION_DUMP, } action; const char *tag; char prio; @@ -38,6 +39,7 @@ bool parse_options(int argc, const char **argv, struct parsed_params *params, in params->tag = "*"; params->prio = '*'; + bool dump = true; for (;;) { int opt = getopt(argc, (char **) argv, ":hgs:t:p:"); @@ -60,11 +62,13 @@ bool parse_options(int argc, const char **argv, struct parsed_params *params, in break; } case 't': { params->tag = optarg; + dump = false; break; } case 'p': params->prio = toupper(*optarg); if (!strchr("DVIWEFS*", params->prio)) goto failure; + dump = false; break; case ':': if (optopt == 's') @@ -81,6 +85,9 @@ bool parse_options(int argc, const char **argv, struct parsed_params *params, in goto print_help; } + if (params->action == ACTION_GET && dump) + params->action = ACTION_DUMP; + return true; failure: @@ -252,6 +259,32 @@ int handle_get(const struct parsed_params *params, char *filters_path, struct lo return EXIT_SUCCESS; } +int handle_dump(const struct parsed_params *params, char *filters_path, struct log_config *conf) +{ + if (!__log_limiter_create(conf)) { + ERR("error creating limiter\n"); + return EXIT_FAILURE; + } + char buf[1024]; + + log_config_read_file(conf, filters_path); // not an error on failure - config still valid if missing, static rules apply + __log_limiter_update(conf); + + struct rule *r = NULL; + do { + int ret = __log_limiter_dump_rule(&r, buf, sizeof buf); + if (ret < 0) { + ERR("Error dumping rule\n"); + return EXIT_FAILURE; + } + printf("%s\n", buf); + } while (r); + + __log_limiter_destroy(); + return EXIT_SUCCESS; +} + + int main(int argc, const char **argv) { int exit_code = EXIT_FAILURE; @@ -282,6 +315,7 @@ int main(int argc, const char **argv) int (*handles[])(const struct parsed_params *, char *, struct log_config *) = { [ACTION_GET] = handle_get, [ACTION_SET] = handle_set, + [ACTION_DUMP] = handle_dump, }; exit_code = handles[params.action](¶ms, full_inotify_path, &conf);