Cleanup: clarify bytecode ownership
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Tue, 15 Dec 2020 14:24:30 +0000 (09:24 -0500)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Wed, 24 Feb 2021 15:14:42 +0000 (10:14 -0500)
Use a regular pattern for all commands:

If the command callback takes ownership of a pointer or file descriptor,
it sets them to NULL or -1. Therefore, the caller can always try to free
the pointer, or close it if it is greater or equal to 0.

Change-Id: Ie155d3cff80930271417075d693aa4528decbfc3
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
liblttng-ust/lttng-events.c
liblttng-ust/lttng-ust-abi.c
liblttng-ust/lttng-ust-comm.c
liblttng-ust/ust-events-internal.h

index fbebc338c6f0b7e49760ff6d6f4dc556f009bd36..664f8b5d05c6f82a1bb55e1a085ee6c9b4e6479f 100644 (file)
@@ -1456,14 +1456,16 @@ int lttng_event_enabler_disable(struct lttng_event_enabler *event_enabler)
 
 static
 void _lttng_enabler_attach_filter_bytecode(struct lttng_enabler *enabler,
-               struct lttng_ust_bytecode_node *bytecode)
+               struct lttng_ust_bytecode_node **bytecode)
 {
-       bytecode->enabler = enabler;
-       cds_list_add_tail(&bytecode->node, &enabler->filter_bytecode_head);
+       (*bytecode)->enabler = enabler;
+       cds_list_add_tail(&(*bytecode)->node, &enabler->filter_bytecode_head);
+       /* Take ownership of bytecode */
+       *bytecode = NULL;
 }
 
 int lttng_event_enabler_attach_filter_bytecode(struct lttng_event_enabler *event_enabler,
-               struct lttng_ust_bytecode_node *bytecode)
+               struct lttng_ust_bytecode_node **bytecode)
 {
        _lttng_enabler_attach_filter_bytecode(
                lttng_event_enabler_as_enabler(event_enabler), bytecode);
@@ -1512,7 +1514,7 @@ int lttng_event_notifier_enabler_disable(
 
 int lttng_event_notifier_enabler_attach_filter_bytecode(
                struct lttng_event_notifier_enabler *event_notifier_enabler,
-               struct lttng_ust_bytecode_node *bytecode)
+               struct lttng_ust_bytecode_node **bytecode)
 {
        _lttng_enabler_attach_filter_bytecode(
                lttng_event_notifier_enabler_as_enabler(event_notifier_enabler),
index 5063ec6c7c1b14d981ce8fed0ea4d222631cd6cf..9ba5acd5a60d72f9e9ede918a70b2cab87243abc 100644 (file)
@@ -712,7 +712,7 @@ long lttng_event_notifier_enabler_cmd(int objd, unsigned int cmd, unsigned long
        case LTTNG_UST_FILTER:
                return lttng_event_notifier_enabler_attach_filter_bytecode(
                        event_notifier_enabler,
-                       (struct lttng_ust_bytecode_node *) arg);
+                       (struct lttng_ust_bytecode_node **) arg);
        case LTTNG_UST_EXCLUSION:
                return lttng_event_notifier_enabler_attach_exclusion(event_notifier_enabler,
                        (struct lttng_ust_excluder_node **) arg);
@@ -1291,7 +1291,7 @@ long lttng_event_enabler_cmd(int objd, unsigned int cmd, unsigned long arg,
                int ret;
 
                ret = lttng_event_enabler_attach_filter_bytecode(enabler,
-                               (struct lttng_ust_bytecode_node *) arg);
+                               (struct lttng_ust_bytecode_node **) arg);
                if (ret)
                        return ret;
                return 0;
index 69bce955491db27da8b3c8818da605f9a50a6f5d..682992ca3d46fe719a8b22cfb761fd86ea47fa70 100644 (file)
@@ -772,7 +772,7 @@ static
 int handle_bytecode_recv(struct sock_info *sock_info,
                int sock, struct ustcomm_ust_msg *lum)
 {
-       struct lttng_ust_bytecode_node *bytecode;
+       struct lttng_ust_bytecode_node *bytecode = NULL;
        enum lttng_ust_bytecode_node_type type;
        const struct lttng_ust_objd_ops *ops;
        uint32_t data_size, data_size_max, reloc_offset;
@@ -829,7 +829,7 @@ int handle_bytecode_recv(struct sock_info *sock_info,
        switch (len) {
        case 0: /* orderly shutdown */
                ret = 0;
-               goto error_free_bytecode;
+               goto end;
        default:
                if (len == bytecode->bc.len) {
                        DBG("Bytecode %s data received",
@@ -842,41 +842,33 @@ int handle_bytecode_recv(struct sock_info *sock_info,
                                ERR("%s remote end closed connection",
                                                sock_info->name);
                                ret = len;
-                               goto error_free_bytecode;
+                               goto end;
                        }
                        ret = len;
-                       goto error_free_bytecode;
+                       goto end;
                } else {
                        DBG("Incorrect %s bytecode data message size: %zd",
                                        bytecode_type_str(lum->cmd), len);
                        ret = -EINVAL;
-                       goto error_free_bytecode;
+                       goto end;
                }
        }
 
        ops = objd_ops(lum->handle);
        if (!ops) {
                ret = -ENOENT;
-               goto error_free_bytecode;
+               goto end;
        }
 
-       if (ops->cmd) {
+       if (ops->cmd)
                ret = ops->cmd(lum->handle, lum->cmd,
-                       (unsigned long) bytecode,
+                       (unsigned long) &bytecode,
                        NULL, sock_info);
-               if (ret)
-                       goto error_free_bytecode;
-               /* don't free bytecode if everything went fine. */
-       } else {
+       else
                ret = -ENOSYS;
-               goto error_free_bytecode;
-       }
-
-       goto end;
 
-error_free_bytecode:
-       free(bytecode);
 end:
+       free(bytecode);
        return ret;
 }
 
index 73cc22bf511165e1dd5b14913a507da430dc62db..e1141add04015ef4f7e3308fe66750e9093d651b 100644 (file)
@@ -134,7 +134,7 @@ int lttng_event_enabler_disable(struct lttng_event_enabler *enabler);
 LTTNG_HIDDEN
 int lttng_event_enabler_attach_filter_bytecode(
                struct lttng_event_enabler *enabler,
-               struct lttng_ust_bytecode_node *bytecode);
+               struct lttng_ust_bytecode_node **bytecode);
 
 /*
  * Attach an application context to an event enabler.
@@ -224,7 +224,7 @@ int lttng_event_notifier_enabler_disable(
 LTTNG_HIDDEN
 int lttng_event_notifier_enabler_attach_filter_bytecode(
                struct lttng_event_notifier_enabler *event_notifier_enabler,
-               struct lttng_ust_bytecode_node *bytecode);
+               struct lttng_ust_bytecode_node **bytecode);
 
 /*
  * Attach capture bytecode program to `struct lttng_event_notifier_enabler` and
This page took 0.029768 seconds and 4 git commands to generate.