dlogfilterctl: allow dumping all rules 27/187927/4
authorMaciej Slodczyk <m.slodczyk2@partner.samsung.com>
Wed, 29 Aug 2018 12:36:03 +0000 (14:36 +0200)
committerHyotaek Shim <hyotaek.shim@samsung.com>
Thu, 30 Aug 2018 00:26:47 +0000 (00:26 +0000)
Change-Id: I2f128778561b520a6cf8f8ac767ee8d5579c03d7
Signed-off-by: Maciej Slodczyk <m.slodczyk2@partner.samsung.com>
include/loglimiter.h
src/libdlog/loglimiter.c
src/logfilterctl/logfilterctl.c

index 81ea3f8..1a290fc 100644 (file)
@@ -28,6 +28,8 @@ extern "C" {
 
 #include <logconfig.h>
 
+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
index a280654..14e64f5 100644 (file)
@@ -479,3 +479,41 @@ void __log_limiter_update(struct log_config *config)
        rules_destroy(&current_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;
+}
index f9e264f..684504e 100644 (file)
@@ -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](&params, full_inotify_path, &conf);