Fix: do not use BT_MOVE() when the destination memory could be uninitialized
[babeltrace.git] / lib / graph / graph.c
index b557fa43ab19c6dac82fc39ffd9ddacfa13528e1..10dd1944e840808e7b9669b9b8761deca26e590e 100644 (file)
@@ -191,10 +191,11 @@ error:
        goto end;
 }
 
-struct bt_connection *bt_graph_connect_ports(struct bt_graph *graph,
-               struct bt_port *upstream_port,
-               struct bt_port *downstream_port)
+enum bt_graph_status bt_graph_connect_ports(struct bt_graph *graph,
+               struct bt_port *upstream_port, struct bt_port *downstream_port,
+               struct bt_connection **user_connection)
 {
+       enum bt_graph_status status = BT_GRAPH_STATUS_OK;
        struct bt_connection *connection = NULL;
        struct bt_graph *upstream_graph = NULL;
        struct bt_graph *downstream_graph = NULL;
@@ -206,16 +207,19 @@ struct bt_connection *bt_graph_connect_ports(struct bt_graph *graph,
 
        if (!graph) {
                BT_LOGW_STR("Invalid parameter: graph is NULL.");
+               status = BT_GRAPH_STATUS_INVALID;
                goto end;
        }
 
        if (!upstream_port) {
                BT_LOGW_STR("Invalid parameter: upstream port is NULL.");
+               status = BT_GRAPH_STATUS_INVALID;
                goto end;
        }
 
        if (!downstream_port) {
                BT_LOGW_STR("Invalid parameter: downstream port is NULL.");
+               status = BT_GRAPH_STATUS_INVALID;
                goto end;
        }
 
@@ -228,27 +232,32 @@ struct bt_connection *bt_graph_connect_ports(struct bt_graph *graph,
 
        if (graph->canceled) {
                BT_LOGW_STR("Invalid parameter: graph is canceled.");
+               status = BT_GRAPH_STATUS_CANCELED;
                goto end;
        }
 
        /* Ensure appropriate types for upstream and downstream ports. */
        if (bt_port_get_type(upstream_port) != BT_PORT_TYPE_OUTPUT) {
                BT_LOGW_STR("Invalid parameter: upstream port is not an output port.");
+               status = BT_GRAPH_STATUS_INVALID;
                goto end;
        }
        if (bt_port_get_type(downstream_port) != BT_PORT_TYPE_INPUT) {
                BT_LOGW_STR("Invalid parameter: downstream port is not an input port.");
+               status = BT_GRAPH_STATUS_INVALID;
                goto end;
        }
 
        /* Ensure that both ports are currently unconnected. */
        if (bt_port_is_connected(upstream_port)) {
                BT_LOGW_STR("Invalid parameter: upstream port is already connected.");
+               status = BT_GRAPH_STATUS_INVALID;
                goto end;
        }
 
        if (bt_port_is_connected(downstream_port)) {
                BT_LOGW_STR("Invalid parameter: downstream port is already connected.");
+               status = BT_GRAPH_STATUS_INVALID;
                goto end;
        }
 
@@ -259,12 +268,14 @@ struct bt_connection *bt_graph_connect_ports(struct bt_graph *graph,
        upstream_component = bt_port_get_component(upstream_port);
        if (!upstream_component) {
                BT_LOGW_STR("Invalid parameter: upstream port is loose (does not belong to a component)");
+               status = BT_GRAPH_STATUS_INVALID;
                goto end;
        }
 
        downstream_component = bt_port_get_component(downstream_port);
        if (!downstream_component) {
                BT_LOGW_STR("Invalid parameter: downstream port is loose (does not belong to a component)");
+               status = BT_GRAPH_STATUS_INVALID;
                goto end;
        }
 
@@ -279,14 +290,16 @@ struct bt_connection *bt_graph_connect_ports(struct bt_graph *graph,
        if (upstream_graph && (graph != upstream_graph)) {
                BT_LOGW("Invalid parameter: upstream port's component is already part of another graph: "
                        "other-graph-addr=%p", upstream_graph);
-               goto error;
+               status = BT_GRAPH_STATUS_ALREADY_IN_A_GRAPH;
+               goto end;
        }
        upstream_was_already_in_graph = (graph == upstream_graph);
        downstream_graph = bt_component_get_graph(downstream_component);
        if (downstream_graph && (graph != downstream_graph)) {
                BT_LOGW("Invalid parameter: downstream port's component is already part of another graph: "
                        "other-graph-addr=%p", downstream_graph);
-               goto error;
+               status = BT_GRAPH_STATUS_ALREADY_IN_A_GRAPH;
+               goto end;
        }
        downstream_was_already_in_graph = (graph == downstream_graph);
 
@@ -306,7 +319,9 @@ struct bt_connection *bt_graph_connect_ports(struct bt_graph *graph,
                                "status=%s", bt_component_status_string(component_status));
                }
 
-               goto error;
+               status = bt_graph_status_from_component_status(
+                       component_status);
+               goto end;
        }
 
        BT_LOGD_STR("Asking downstream component to accept the connection.");
@@ -320,7 +335,9 @@ struct bt_connection *bt_graph_connect_ports(struct bt_graph *graph,
                                "status=%s", bt_component_status_string(component_status));
                }
 
-               goto error;
+               status = bt_graph_status_from_component_status(
+                       component_status);
+               goto end;
        }
 
        BT_LOGD_STR("Creating connection.");
@@ -328,7 +345,8 @@ struct bt_connection *bt_graph_connect_ports(struct bt_graph *graph,
                        downstream_port);
        if (!connection) {
                BT_LOGW("Cannot create connection object.");
-               goto error;
+               status = BT_GRAPH_STATUS_NOMEM;
+               goto end;
        }
 
        BT_LOGD("Connection object created: conn-addr=%p", connection);
@@ -386,39 +404,30 @@ struct bt_connection *bt_graph_connect_ports(struct bt_graph *graph,
                upstream_port, bt_port_get_name(upstream_port),
                downstream_port, bt_port_get_name(downstream_port));
 
+       if (user_connection) {
+               /* Move reference to user */
+               *user_connection = connection;
+               connection = NULL;
+       }
+
 end:
        bt_put(upstream_graph);
        bt_put(downstream_graph);
        bt_put(upstream_component);
        bt_put(downstream_component);
-       return connection;
-
-error:
-       BT_PUT(upstream_component);
-       BT_PUT(downstream_component);
-       goto end;
+       bt_put(connection);
+       return status;
 }
 
-enum bt_graph_status bt_graph_consume(struct bt_graph *graph)
+static
+enum bt_graph_status bt_graph_consume_no_check(struct bt_graph *graph)
 {
        struct bt_component *sink;
        enum bt_graph_status status = BT_GRAPH_STATUS_OK;
        enum bt_component_status comp_status;
        GList *current_node;
 
-       if (!graph) {
-               BT_LOGW_STR("Invalid parameter: graph is NULL.");
-               status = BT_GRAPH_STATUS_INVALID;
-               goto end;
-       }
-
-       BT_LOGV("Making sink consume: addr=%p", graph);
-
-       if (graph->canceled) {
-               BT_LOGW_STR("Invalid parameter: graph is canceled.");
-               status = BT_GRAPH_STATUS_CANCELED;
-               goto end;
-       }
+       BT_LOGV("Making next sink consume: addr=%p", graph);
 
        if (g_queue_is_empty(graph->sinks_to_consume)) {
                BT_LOGV_STR("Graph's sink queue is empty: end of graph.");
@@ -468,6 +477,29 @@ end:
        return status;
 }
 
+enum bt_graph_status bt_graph_consume(struct bt_graph *graph)
+{
+       enum bt_graph_status status = BT_GRAPH_STATUS_OK;
+
+       if (!graph) {
+               BT_LOGW_STR("Invalid parameter: graph is NULL.");
+               status = BT_GRAPH_STATUS_INVALID;
+               goto end;
+       }
+
+       if (graph->canceled) {
+               BT_LOGW("Invalid parameter: graph is canceled: "
+                       "graph-addr=%p", graph);
+               status = BT_GRAPH_STATUS_CANCELED;
+               goto end;
+       }
+
+       status = bt_graph_consume_no_check(graph);
+
+end:
+       return status;
+}
+
 enum bt_graph_status bt_graph_run(struct bt_graph *graph)
 {
        enum bt_graph_status status = BT_GRAPH_STATUS_OK;
@@ -478,9 +510,29 @@ enum bt_graph_status bt_graph_run(struct bt_graph *graph)
                goto end;
        }
 
+       if (graph->canceled) {
+               BT_LOGW("Invalid parameter: graph is canceled: "
+                       "graph-addr=%p", graph);
+               status = BT_GRAPH_STATUS_CANCELED;
+               goto end;
+       }
+
        BT_LOGV("Running graph: addr=%p", graph);
 
        do {
+               /*
+                * Check if the graph is canceled at each iteration. If
+                * the graph was canceled by another thread or by a
+                * signal, this is not a warning nor an error, it was
+                * intentional: log with a DEBUG level only.
+                */
+               if (graph->canceled) {
+                       BT_LOGD("Stopping the graph: graph is canceled: "
+                               "graph-addr=%p", graph);
+                       status = BT_GRAPH_STATUS_CANCELED;
+                       goto end;
+               }
+
                status = bt_graph_consume(graph);
                if (status == BT_GRAPH_STATUS_AGAIN) {
                        /*
@@ -510,7 +562,7 @@ end:
 }
 
 static
-void add_listener(GArray *listeners, void *func, void *data)
+int add_listener(GArray *listeners, void *func, void *data)
 {
        struct bt_graph_listener listener = {
                .func = func,
@@ -518,110 +570,115 @@ void add_listener(GArray *listeners, void *func, void *data)
        };
 
        g_array_append_val(listeners, listener);
+       return listeners->len - 1;
 }
 
-enum bt_graph_status bt_graph_add_port_added_listener(
+int bt_graph_add_port_added_listener(
                struct bt_graph *graph,
                bt_graph_port_added_listener listener, void *data)
 {
-       enum bt_graph_status status = BT_GRAPH_STATUS_OK;
+       int ret;
 
        if (!graph) {
                BT_LOGW_STR("Invalid parameter: graph is NULL.");
-               status = BT_GRAPH_STATUS_INVALID;
+               ret = -1;
                goto end;
        }
 
        if (!listener) {
                BT_LOGW_STR("Invalid parameter: listener is NULL.");
-               status = BT_GRAPH_STATUS_INVALID;
+               ret = -1;
                goto end;
        }
 
-       add_listener(graph->listeners.port_added, listener, data);
+       ret = add_listener(graph->listeners.port_added, listener, data);
        BT_LOGV("Added \"port added\" listener to graph: "
-               "graph-addr=%p, listener-addr=%p", graph, listener);
+               "graph-addr=%p, listener-addr=%p, pos=%d",
+               graph, listener, ret);
 
 end:
-       return status;
+       return ret;
 }
 
-enum bt_graph_status bt_graph_add_port_removed_listener(
+int bt_graph_add_port_removed_listener(
                struct bt_graph *graph,
                bt_graph_port_removed_listener listener, void *data)
 {
-       enum bt_graph_status status = BT_GRAPH_STATUS_OK;
+       int ret;
 
        if (!graph) {
                BT_LOGW_STR("Invalid parameter: graph is NULL.");
-               status = BT_GRAPH_STATUS_INVALID;
+               ret = -1;
                goto end;
        }
 
        if (!listener) {
                BT_LOGW_STR("Invalid parameter: listener is NULL.");
-               status = BT_GRAPH_STATUS_INVALID;
+               ret = -1;
                goto end;
        }
 
-       add_listener(graph->listeners.port_removed, listener, data);
+       ret = add_listener(graph->listeners.port_removed, listener, data);
        BT_LOGV("Added \"port removed\" listener to graph: "
-               "graph-addr=%p, listener-addr=%p", graph, listener);
+               "graph-addr=%p, listener-addr=%p, pos=%d",
+               graph, listener, ret);
 
 end:
-       return status;
+       return ret;
 }
 
-enum bt_graph_status bt_graph_add_ports_connected_listener(
+int bt_graph_add_ports_connected_listener(
                struct bt_graph *graph,
                bt_graph_ports_connected_listener listener, void *data)
 {
-       enum bt_graph_status status = BT_GRAPH_STATUS_OK;
+       int ret;
 
        if (!graph) {
                BT_LOGW_STR("Invalid parameter: graph is NULL.");
-               status = BT_GRAPH_STATUS_INVALID;
+               ret = -1;
                goto end;
        }
 
        if (!listener) {
                BT_LOGW_STR("Invalid parameter: listener is NULL.");
-               status = BT_GRAPH_STATUS_INVALID;
+               ret = -1;
                goto end;
        }
 
-       add_listener(graph->listeners.ports_connected, listener, data);
+       ret = add_listener(graph->listeners.ports_connected, listener, data);
        BT_LOGV("Added \"port connected\" listener to graph: "
-               "graph-addr=%p, listener-addr=%p", graph, listener);
+               "graph-addr=%p, listener-addr=%p, pos=%d",
+               graph, listener, ret);
 
 end:
-       return status;
+       return ret;
 }
 
-enum bt_graph_status bt_graph_add_ports_disconnected_listener(
+int bt_graph_add_ports_disconnected_listener(
                struct bt_graph *graph,
                bt_graph_ports_disconnected_listener listener, void *data)
 {
-       enum bt_graph_status status = BT_GRAPH_STATUS_OK;
+       int ret;
 
        if (!graph) {
                BT_LOGW_STR("Invalid parameter: graph is NULL.");
-               status = BT_GRAPH_STATUS_INVALID;
+               ret = -1;
                goto end;
        }
 
        if (!listener) {
                BT_LOGW_STR("Invalid parameter: listener is NULL.");
-               status = BT_GRAPH_STATUS_INVALID;
+               ret = -1;
                goto end;
        }
 
-       add_listener(graph->listeners.ports_disconnected, listener, data);
+       ret = add_listener(graph->listeners.ports_disconnected, listener, data);
        BT_LOGV("Added \"port disconnected\" listener to graph: "
-               "graph-addr=%p, listener-addr=%p", graph, listener);
+               "graph-addr=%p, listener-addr=%p, pos=%d",
+               graph, listener, ret);
 
 end:
-       return status;
+       return ret;
 }
 
 BT_HIDDEN
This page took 0.040769 seconds and 4 git commands to generate.