event-rule: Normalize pattern for syscall and tracepoint
[lttng-tools.git] / src / common / event-rule / syscall.c
index ef7ccd0f9a038505c4cec79fea5b0f6c7fb4428e..fbaa65310a6966548690bbb7db65c9c6ca91712b 100644 (file)
@@ -6,11 +6,15 @@
  */
 
 #include <assert.h>
+#include <common/credentials.h>
 #include <common/error.h>
 #include <common/macros.h>
 #include <common/payload.h>
 #include <common/payload-view.h>
 #include <common/runas.h>
+#include <common/hashtable/hashtable.h>
+#include <common/hashtable/utils.h>
+#include <common/string-utils/string-utils.h>
 #include <lttng/event-rule/event-rule-internal.h>
 #include <lttng/event-rule/syscall-internal.h>
 
@@ -137,14 +141,15 @@ end:
 }
 
 static enum lttng_error_code lttng_event_rule_syscall_generate_filter_bytecode(
-               struct lttng_event_rule *rule, uid_t uid, gid_t gid)
+               struct lttng_event_rule *rule,
+               const struct lttng_credentials *creds)
 {
        int ret;
        enum lttng_error_code ret_code = LTTNG_OK;
        struct lttng_event_rule_syscall *syscall;
        enum lttng_event_rule_status status;
        const char *filter;
-       struct lttng_filter_bytecode *bytecode = NULL;
+       struct lttng_bytecode *bytecode = NULL;
 
        assert(rule);
 
@@ -177,7 +182,7 @@ static enum lttng_error_code lttng_event_rule_syscall_generate_filter_bytecode(
        }
 
        ret = run_as_generate_filter_bytecode(
-                       syscall->internal_filter.filter, uid, gid, &bytecode);
+                       syscall->internal_filter.filter, creds, &bytecode);
        if (ret) {
                ret_code = LTTNG_ERR_FILTER_INVAL;
        }
@@ -201,7 +206,7 @@ static const char *lttng_event_rule_syscall_get_internal_filter(
        return syscall->internal_filter.filter;
 }
 
-static const struct lttng_filter_bytecode *
+static const struct lttng_bytecode *
 lttng_event_rule_syscall_get_internal_filter_bytecode(
                const struct lttng_event_rule *rule)
 {
@@ -213,18 +218,39 @@ lttng_event_rule_syscall_get_internal_filter_bytecode(
        return syscall->internal_filter.bytecode;
 }
 
-static struct lttng_event_exclusion *
-lttng_event_rule_syscall_generate_exclusions(
+static enum lttng_event_rule_generate_exclusions_status
+lttng_event_rule_syscall_generate_exclusions(const struct lttng_event_rule *rule,
+               struct lttng_event_exclusion **exclusions)
+{
+       /* Unsupported. */
+       *exclusions = NULL;
+       return LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE;
+}
+
+static unsigned long
+lttng_event_rule_syscall_hash(
                const struct lttng_event_rule *rule)
 {
-       /* Not supported. */
-       return NULL;
+       unsigned long hash;
+       struct lttng_event_rule_syscall *syscall_rule =
+                       container_of(rule, typeof(*syscall_rule), parent);
+
+       hash = hash_key_ulong((void *) LTTNG_EVENT_RULE_TYPE_SYSCALL,
+                       lttng_ht_seed);
+       hash ^= hash_key_str(syscall_rule->pattern, lttng_ht_seed);
+       if (syscall_rule->filter_expression) {
+               hash ^= hash_key_str(syscall_rule->filter_expression,
+                               lttng_ht_seed);
+       }
+
+       return hash;
 }
 
-struct lttng_event_rule *lttng_event_rule_syscall_create()
+struct lttng_event_rule *lttng_event_rule_syscall_create(void)
 {
        struct lttng_event_rule *rule = NULL;
        struct lttng_event_rule_syscall *syscall_rule;
+       enum lttng_event_rule_status status;
 
        syscall_rule = zmalloc(sizeof(struct lttng_event_rule_syscall));
        if (!syscall_rule) {
@@ -246,6 +272,15 @@ struct lttng_event_rule *lttng_event_rule_syscall_create()
                        lttng_event_rule_syscall_get_internal_filter_bytecode;
        syscall_rule->parent.generate_exclusions =
                        lttng_event_rule_syscall_generate_exclusions;
+       syscall_rule->parent.hash = lttng_event_rule_syscall_hash;
+
+       /* Default pattern is '*'. */
+       status = lttng_event_rule_syscall_set_pattern(rule, "*");
+       if (status != LTTNG_EVENT_RULE_STATUS_OK) {
+               lttng_event_rule_destroy(rule);
+               rule = NULL;
+       }
+
 end:
        return rule;
 }
@@ -276,13 +311,12 @@ ssize_t lttng_event_rule_syscall_create_from_payload(
 
        current_buffer_view = lttng_buffer_view_from_view(
                        &view->buffer, offset, sizeof(*syscall_comm));
-       syscall_comm = (typeof(syscall_comm)) current_buffer_view.data;
-
-       if (!syscall_comm) {
+       if (!lttng_buffer_view_is_valid(&current_buffer_view)) {
                ret = -1;
                goto end;
        }
 
+       syscall_comm = (typeof(syscall_comm)) current_buffer_view.data;
        rule = lttng_event_rule_syscall_create();
        if (!rule) {
                ERR("Failed to create event rule syscall");
@@ -296,12 +330,12 @@ ssize_t lttng_event_rule_syscall_create_from_payload(
        /* Map the pattern. */
        current_buffer_view = lttng_buffer_view_from_view(
                        &view->buffer, offset, syscall_comm->pattern_len);
-       pattern = current_buffer_view.data;
-       if (!pattern) {
+       if (!lttng_buffer_view_is_valid(&current_buffer_view)) {
                ret = -1;
                goto end;
        }
 
+       pattern = current_buffer_view.data;
        if (!lttng_buffer_view_contains_string(&current_buffer_view, pattern,
                        syscall_comm->pattern_len)) {
                ret = -1;
@@ -318,12 +352,12 @@ ssize_t lttng_event_rule_syscall_create_from_payload(
        /* Map the filter_expression. */
        current_buffer_view = lttng_buffer_view_from_view(&view->buffer, offset,
                        syscall_comm->filter_expression_len);
-       filter_expression = current_buffer_view.data;
-       if (!filter_expression) {
+       if (!lttng_buffer_view_is_valid(&current_buffer_view)) {
                ret = -1;
                goto end;
        }
 
+       filter_expression = current_buffer_view.data;
        if (!lttng_buffer_view_contains_string(&current_buffer_view,
                                filter_expression,
                                syscall_comm->filter_expression_len)) {
@@ -381,9 +415,9 @@ enum lttng_event_rule_status lttng_event_rule_syscall_set_pattern(
                goto end;
        }
 
-       if (syscall->pattern) {
-               free(syscall->pattern);
-       }
+       strutils_normalize_star_glob_pattern(pattern_copy);
+
+       free(syscall->pattern);
 
        syscall->pattern = pattern_copy;
        pattern_copy = NULL;
This page took 0.035845 seconds and 4 git commands to generate.