From: Philippe Proulx Date: Wed, 7 Jun 2017 21:45:59 +0000 (-0400) Subject: Make bt_private_connection_create_notification_iterator() return a status code X-Git-Tag: v2.0.0-pre1~42 X-Git-Url: http://git.efficios.com/?a=commitdiff_plain;h=73d5c1adb1411e16c9c613c38a4c74a29ee608ae;hp=bd7cc15bddddb727ea02fb44bde2d9f15cd82847;p=babeltrace.git Make bt_private_connection_create_notification_iterator() return a status code Signed-off-by: Philippe Proulx Signed-off-by: Jérémie Galarneau --- diff --git a/include/babeltrace/graph/connection-internal.h b/include/babeltrace/graph/connection-internal.h index 2a852968..13e7d9ee 100644 --- a/include/babeltrace/graph/connection-internal.h +++ b/include/babeltrace/graph/connection-internal.h @@ -28,6 +28,7 @@ */ #include +#include #include #include @@ -82,4 +83,35 @@ BT_HIDDEN void bt_connection_remove_iterator(struct bt_connection *conn, struct bt_notification_iterator *iterator); +static inline +enum bt_connection_status +bt_connection_status_from_notification_iterator_status( + enum bt_notification_iterator_status iter_status) +{ + switch (iter_status) { + case BT_NOTIFICATION_ITERATOR_STATUS_CANCELED: + return BT_CONNECTION_STATUS_ERROR; + case BT_NOTIFICATION_ITERATOR_STATUS_AGAIN: + return BT_CONNECTION_STATUS_ERROR; + case BT_NOTIFICATION_ITERATOR_STATUS_END: + return BT_CONNECTION_STATUS_ERROR; + case BT_NOTIFICATION_ITERATOR_STATUS_OK: + return BT_CONNECTION_STATUS_OK; + case BT_NOTIFICATION_ITERATOR_STATUS_INVALID: + return BT_CONNECTION_STATUS_INVALID; + case BT_NOTIFICATION_ITERATOR_STATUS_ERROR: + return BT_CONNECTION_STATUS_ERROR; + case BT_NOTIFICATION_ITERATOR_STATUS_NOMEM: + return BT_CONNECTION_STATUS_NOMEM; + case BT_NOTIFICATION_ITERATOR_STATUS_UNSUPPORTED: + return BT_CONNECTION_STATUS_ERROR; + default: +#ifdef BT_LOGF + BT_LOGF("Unknown notification iterator status: status=%d", + iter_status); +#endif + abort(); + } +} + #endif /* BABELTRACE_COMPONENT_CONNECTION_INTERNAL_H */ diff --git a/include/babeltrace/graph/connection.h b/include/babeltrace/graph/connection.h index 60b9eef8..566d5182 100644 --- a/include/babeltrace/graph/connection.h +++ b/include/babeltrace/graph/connection.h @@ -27,6 +27,8 @@ * SOFTWARE. */ +#include + #ifdef __cplusplus extern "C" { #endif @@ -34,6 +36,15 @@ extern "C" { struct bt_component; struct bt_connection; +enum bt_connection_status { + BT_CONNECTION_STATUS_GRAPH_IS_CANCELED = 125, + BT_CONNECTION_STATUS_OK = 0, + BT_CONNECTION_STATUS_INVALID = -22, + BT_CONNECTION_STATUS_ERROR = -1, + BT_CONNECTION_STATUS_NOMEM = -12, + BT_CONNECTION_STATUS_IS_ENDED = 104, +}; + /* Returns the "downstream" input port. */ extern struct bt_port *bt_connection_get_downstream_port( struct bt_connection *connection); diff --git a/include/babeltrace/graph/notification-iterator-internal.h b/include/babeltrace/graph/notification-iterator-internal.h index f5454744..b7aafe01 100644 --- a/include/babeltrace/graph/notification-iterator-internal.h +++ b/include/babeltrace/graph/notification-iterator-internal.h @@ -132,18 +132,13 @@ bt_private_notification_iterator_from_notification_iterator( return (void *) notification_iterator; } -/** - * Allocate a notification iterator. - * - * @param component Component instance - * @returns A notification iterator instance - */ BT_HIDDEN -struct bt_notification_iterator *bt_notification_iterator_create( - struct bt_component *upstream_component, +enum bt_connection_status bt_notification_iterator_create( + struct bt_component *upstream_comp, struct bt_port *upstream_port, const enum bt_notification_type *notification_types, - struct bt_connection *connection); + struct bt_connection *connection, + struct bt_notification_iterator **iterator); BT_HIDDEN void bt_notification_iterator_finalize( diff --git a/include/babeltrace/graph/private-connection.h b/include/babeltrace/graph/private-connection.h index 59db640e..ea44d932 100644 --- a/include/babeltrace/graph/private-connection.h +++ b/include/babeltrace/graph/private-connection.h @@ -23,6 +23,7 @@ * SOFTWARE. */ +#include #include #ifdef __cplusplus @@ -36,10 +37,11 @@ struct bt_private_connection; struct bt_connection *bt_connection_from_private_connection( struct bt_private_connection *private_connection); -extern struct bt_notification_iterator * +extern enum bt_connection_status bt_private_connection_create_notification_iterator( struct bt_private_connection *private_connection, - const enum bt_notification_type *notification_types); + const enum bt_notification_type *notification_types, + struct bt_notification_iterator **iterator); #ifdef __cplusplus } diff --git a/lib/graph/connection.c b/lib/graph/connection.c index a13bce3e..c7e32c07 100644 --- a/lib/graph/connection.c +++ b/lib/graph/connection.c @@ -272,10 +272,11 @@ struct bt_port *bt_connection_get_downstream_port( return connection ? bt_get(connection->downstream_port) : NULL; } -struct bt_notification_iterator * +enum bt_connection_status bt_private_connection_create_notification_iterator( struct bt_private_connection *private_connection, - const enum bt_notification_type *notification_types) + const enum bt_notification_type *notification_types, + struct bt_notification_iterator **user_iterator) { enum bt_component_class_type upstream_comp_class_type; struct bt_notification_iterator *iterator = NULL; @@ -284,6 +285,7 @@ bt_private_connection_create_notification_iterator( struct bt_component_class *upstream_comp_class = NULL; struct bt_connection *connection = NULL; bt_component_class_notification_iterator_init_method init_method = NULL; + enum bt_connection_status status; static const enum bt_notification_type all_notif_types[] = { BT_NOTIFICATION_TYPE_ALL, BT_NOTIFICATION_TYPE_SENTINEL, @@ -291,7 +293,14 @@ bt_private_connection_create_notification_iterator( if (!private_connection) { BT_LOGW_STR("Invalid parameter: private connection is NULL."); - goto error; + status = BT_CONNECTION_STATUS_INVALID; + goto end; + } + + if (!user_iterator) { + BT_LOGW_STR("Invalid parameter: notification iterator pointer is NULL."); + status = BT_CONNECTION_STATUS_INVALID; + goto end; } if (!notification_types) { @@ -300,10 +309,11 @@ bt_private_connection_create_notification_iterator( } connection = bt_connection_from_private(private_connection); - if (!connection->upstream_port || !connection->downstream_port) { + if (bt_connection_is_ended(connection)) { BT_LOGW("Invalid parameter: connection is ended: " "conn-addr=%p", connection); - goto error; + status = BT_CONNECTION_STATUS_IS_ENDED; + goto end; } upstream_port = connection->upstream_port; @@ -320,20 +330,13 @@ bt_private_connection_create_notification_iterator( upstream_component, bt_component_get_name(upstream_component)); upstream_comp_class_type = bt_component_get_class_type(upstream_component); - if (upstream_comp_class_type != BT_COMPONENT_CLASS_TYPE_SOURCE && - upstream_comp_class_type != BT_COMPONENT_CLASS_TYPE_FILTER) { - /* Unsupported operation. */ - BT_LOGW("Upstream component's class is not a source or filter component class: " - "comp-class-type=%s", - bt_component_class_type_string(upstream_comp_class_type)); - goto error; - } - - iterator = bt_notification_iterator_create(upstream_component, - upstream_port, notification_types, connection); - if (!iterator) { + assert(upstream_comp_class_type == BT_COMPONENT_CLASS_TYPE_SOURCE || + upstream_comp_class_type == BT_COMPONENT_CLASS_TYPE_FILTER); + status = bt_notification_iterator_create(upstream_component, + upstream_port, notification_types, connection, &iterator); + if (status != BT_CONNECTION_STATUS_OK) { BT_LOGW("Cannot create notification iterator from connection."); - goto error; + goto end; } switch (upstream_comp_class_type) { @@ -355,22 +358,26 @@ bt_private_connection_create_notification_iterator( } default: /* Unreachable. */ + BT_LOGF("Unknown component class type: type=%d", + upstream_comp_class_type); abort(); } if (init_method) { - enum bt_notification_iterator_status status; + enum bt_notification_iterator_status iter_status; BT_LOGD("Calling user's initialization method: iter-addr=%p", iterator); - status = init_method( + iter_status = init_method( bt_private_notification_iterator_from_notification_iterator(iterator), bt_private_port_from_port(upstream_port)); BT_LOGD("User method returned: status=%s", - bt_notification_iterator_status_string(status)); - if (status < 0) { + bt_notification_iterator_status_string(iter_status)); + if (iter_status != BT_NOTIFICATION_ITERATOR_STATUS_OK) { BT_LOGW_STR("Initialization method failed."); - goto error; + status = bt_connection_status_from_notification_iterator_status( + iter_status); + goto end; } } @@ -383,14 +390,12 @@ bt_private_connection_create_notification_iterator( bt_port_get_name(connection->upstream_port), upstream_component, bt_component_get_name(upstream_component), iterator); - goto end; - -error: - BT_PUT(iterator); + BT_MOVE(*user_iterator, iterator); end: bt_put(upstream_component); - return iterator; + bt_put(iterator); + return status; } BT_HIDDEN diff --git a/lib/graph/iterator.c b/lib/graph/iterator.c index 07eaa1fd..6d6058f5 100644 --- a/lib/graph/iterator.c +++ b/lib/graph/iterator.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -513,12 +514,14 @@ end: } BT_HIDDEN -struct bt_notification_iterator *bt_notification_iterator_create( +enum bt_connection_status bt_notification_iterator_create( struct bt_component *upstream_comp, struct bt_port *upstream_port, const enum bt_notification_type *notification_types, - struct bt_connection *connection) + struct bt_connection *connection, + struct bt_notification_iterator **user_iterator) { + enum bt_connection_status status = BT_CONNECTION_STATUS_OK; enum bt_component_class_type type; struct bt_notification_iterator *iterator = NULL; @@ -526,6 +529,7 @@ struct bt_notification_iterator *bt_notification_iterator_create( assert(upstream_port); assert(notification_types); assert(bt_port_is_connected(upstream_port)); + assert(user_iterator); BT_LOGD("Creating notification iterator: " "upstream-comp-addr=%p, upstream-comp-name=\"%s\", " "upstream-port-addr=%p, upstream-port-name=\"%s\", " @@ -539,7 +543,8 @@ struct bt_notification_iterator *bt_notification_iterator_create( iterator = g_new0(struct bt_notification_iterator, 1); if (!iterator) { BT_LOGE_STR("Failed to allocate one notification iterator."); - goto error; + status = BT_CONNECTION_STATUS_NOMEM; + goto end; } bt_object_init(iterator, bt_notification_iterator_destroy); @@ -547,26 +552,30 @@ struct bt_notification_iterator *bt_notification_iterator_create( if (create_subscription_mask_from_notification_types(iterator, notification_types)) { BT_LOGW_STR("Cannot create subscription mask from notification types."); - goto error; + status = BT_CONNECTION_STATUS_INVALID; + goto end; } iterator->stream_states = g_hash_table_new_full(g_direct_hash, g_direct_equal, NULL, (GDestroyNotify) destroy_stream_state); if (!iterator->stream_states) { BT_LOGE_STR("Failed to allocate a GHashTable."); - goto error; + status = BT_CONNECTION_STATUS_NOMEM; + goto end; } iterator->queue = g_queue_new(); if (!iterator->queue) { BT_LOGE_STR("Failed to allocate a GQueue."); - goto error; + status = BT_CONNECTION_STATUS_NOMEM; + goto end; } iterator->actions = g_array_new(FALSE, FALSE, sizeof(struct action)); if (!iterator->actions) { BT_LOGE_STR("Failed to allocate a GArray."); - goto error; + status = BT_CONNECTION_STATUS_NOMEM; + goto end; } iterator->upstream_component = upstream_comp; @@ -580,13 +589,11 @@ struct bt_notification_iterator *bt_notification_iterator_create( upstream_comp, bt_component_get_name(upstream_comp), upstream_port, bt_port_get_name(upstream_port), connection, iterator); - goto end; - -error: - BT_PUT(iterator); + BT_MOVE(*user_iterator, iterator); end: - return iterator; + bt_put(iterator); + return status; } void *bt_private_notification_iterator_get_user_data( diff --git a/plugins/ctf/fs-sink/writer.c b/plugins/ctf/fs-sink/writer.c index 1bc5966d..1e8aaf76 100644 --- a/plugins/ctf/fs-sink/writer.c +++ b/plugins/ctf/fs-sink/writer.c @@ -28,6 +28,7 @@ #include #include +#include #include #include #include @@ -213,6 +214,7 @@ void writer_component_port_connected( { struct bt_private_connection *connection; struct writer_component *writer; + enum bt_connection_status conn_status; static const enum bt_notification_type notif_types[] = { BT_NOTIFICATION_TYPE_EVENT, BT_NOTIFICATION_TYPE_PACKET_BEGIN, @@ -227,11 +229,9 @@ void writer_component_port_connected( assert(!writer->input_iterator); connection = bt_private_port_get_private_connection(self_port); assert(connection); - writer->input_iterator = - bt_private_connection_create_notification_iterator(connection, - notif_types); - - if (!writer->input_iterator) { + conn_status = bt_private_connection_create_notification_iterator( + connection, notif_types, &writer->input_iterator); + if (conn_status != BT_CONNECTION_STATUS_OK) { writer->error = true; } diff --git a/plugins/lttng-utils/plugin.c b/plugins/lttng-utils/plugin.c index fb00dee3..ec9c70ad 100644 --- a/plugins/lttng-utils/plugin.c +++ b/plugins/lttng-utils/plugin.c @@ -28,6 +28,7 @@ #include #include +#include #include #include #include @@ -275,6 +276,7 @@ enum bt_notification_iterator_status debug_info_iterator_init( enum bt_notification_iterator_status ret = BT_NOTIFICATION_ITERATOR_STATUS_OK; enum bt_notification_iterator_status it_ret; + enum bt_connection_status conn_status; struct bt_private_connection *connection = NULL; struct bt_private_component *component = bt_private_notification_iterator_get_private_component(iterator); @@ -307,10 +309,10 @@ enum bt_notification_iterator_status debug_info_iterator_init( goto end; } - it_data->input_iterator = bt_private_connection_create_notification_iterator( - connection, notif_types); - if (!it_data->input_iterator) { - ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM; + conn_status = bt_private_connection_create_notification_iterator( + connection, notif_types, &it_data->input_iterator); + if (conn_status != BT_CONNECTION_STATUS_OK) { + ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; goto end; } diff --git a/plugins/text/pretty/pretty.c b/plugins/text/pretty/pretty.c index 3d470867..e8f89d3a 100644 --- a/plugins/text/pretty/pretty.c +++ b/plugins/text/pretty/pretty.c @@ -167,6 +167,7 @@ void pretty_port_connected( struct bt_private_port *self_port, struct bt_port *other_port) { + enum bt_connection_status conn_status; struct bt_private_connection *connection; struct pretty_component *pretty; static const enum bt_notification_type notif_types[] = { @@ -179,11 +180,9 @@ void pretty_port_connected( assert(!pretty->input_iterator); connection = bt_private_port_get_private_connection(self_port); assert(connection); - pretty->input_iterator = - bt_private_connection_create_notification_iterator(connection, - notif_types); - - if (!pretty->input_iterator) { + conn_status = bt_private_connection_create_notification_iterator( + connection, notif_types, &pretty->input_iterator); + if (conn_status != BT_CONNECTION_STATUS_OK) { pretty->error = true; } diff --git a/plugins/utils/dummy/dummy.c b/plugins/utils/dummy/dummy.c index df2ddff9..c3434a59 100644 --- a/plugins/utils/dummy/dummy.c +++ b/plugins/utils/dummy/dummy.c @@ -21,6 +21,7 @@ */ #include +#include #include #include #include @@ -97,14 +98,15 @@ void dummy_port_connected( struct dummy *dummy; struct bt_notification_iterator *iterator; struct bt_private_connection *connection; + enum bt_connection_status conn_status; dummy = bt_private_component_get_user_data(component); assert(dummy); connection = bt_private_port_get_private_connection(self_port); assert(connection); - iterator = bt_private_connection_create_notification_iterator( - connection, NULL); - if (!iterator) { + conn_status = bt_private_connection_create_notification_iterator( + connection, NULL, &iterator); + if (conn_status != BT_CONNECTION_STATUS_OK) { dummy->error = true; goto end; } diff --git a/plugins/utils/muxer/muxer.c b/plugins/utils/muxer/muxer.c index 30864edb..47ffb866 100644 --- a/plugins/utils/muxer/muxer.c +++ b/plugins/utils/muxer/muxer.c @@ -37,6 +37,7 @@ #include #include #include +#include #include #include #include @@ -336,6 +337,7 @@ struct bt_notification_iterator *create_notif_iter_on_input_port( struct bt_port *port = bt_port_from_private_port(priv_port); struct bt_notification_iterator *notif_iter = NULL; struct bt_private_connection *priv_conn = NULL; + enum bt_connection_status conn_status; assert(ret); *ret = 0; @@ -351,9 +353,9 @@ struct bt_notification_iterator *create_notif_iter_on_input_port( // TODO: Advance the iterator to >= the time of the latest // returned notification by the muxer notification // iterator which creates it. - notif_iter = bt_private_connection_create_notification_iterator( - priv_conn, NULL); - if (!notif_iter) { + conn_status = bt_private_connection_create_notification_iterator( + priv_conn, NULL, ¬if_iter); + if (conn_status != BT_CONNECTION_STATUS_OK) { *ret = -1; goto end; } diff --git a/plugins/utils/trimmer/iterator.c b/plugins/utils/trimmer/iterator.c index 8241014d..1e94b15e 100644 --- a/plugins/utils/trimmer/iterator.c +++ b/plugins/utils/trimmer/iterator.c @@ -39,6 +39,7 @@ #include #include #include +#include #include #include #include @@ -85,6 +86,7 @@ enum bt_notification_iterator_status trimmer_iterator_init( enum bt_notification_iterator_status ret = BT_NOTIFICATION_ITERATOR_STATUS_OK; enum bt_notification_iterator_status it_ret; + enum bt_connection_status conn_status; struct bt_private_port *input_port = NULL; struct bt_private_connection *connection = NULL; struct bt_private_component *component = @@ -110,11 +112,10 @@ enum bt_notification_iterator_status trimmer_iterator_init( connection = bt_private_port_get_private_connection(input_port); assert(connection); - it_data->input_iterator = - bt_private_connection_create_notification_iterator(connection, - notif_types); - if (!it_data->input_iterator) { - ret = BT_NOTIFICATION_ITERATOR_STATUS_NOMEM; + conn_status = bt_private_connection_create_notification_iterator(connection, + notif_types, &it_data->input_iterator); + if (conn_status != BT_CONNECTION_STATUS_OK) { + ret = BT_NOTIFICATION_ITERATOR_STATUS_ERROR; goto end; } diff --git a/tests/lib/test_bt_notification_iterator.c b/tests/lib/test_bt_notification_iterator.c index 8dff7d0e..cbce6b4c 100644 --- a/tests/lib/test_bt_notification_iterator.c +++ b/tests/lib/test_bt_notification_iterator.c @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #include @@ -824,13 +825,13 @@ void sink_port_connected(struct bt_private_component *private_component, bt_private_port_get_private_connection(self_private_port); struct sink_user_data *user_data = bt_private_component_get_user_data( private_component); + enum bt_connection_status conn_status; assert(user_data); assert(priv_conn); - user_data->notif_iter = - bt_private_connection_create_notification_iterator(priv_conn, - NULL); - assert(user_data->notif_iter); + conn_status = bt_private_connection_create_notification_iterator( + priv_conn, NULL, &user_data->notif_iter); + assert(conn_status == 0); bt_put(priv_conn); } diff --git a/tests/plugins/test-utils-muxer.c b/tests/plugins/test-utils-muxer.c index 0045b0bf..6600a72e 100644 --- a/tests/plugins/test-utils-muxer.c +++ b/tests/plugins/test-utils-muxer.c @@ -871,13 +871,13 @@ void sink_port_connected(struct bt_private_component *private_component, bt_private_port_get_private_connection(self_private_port); struct sink_user_data *user_data = bt_private_component_get_user_data( private_component); + enum bt_connection_status conn_status; assert(user_data); assert(priv_conn); - user_data->notif_iter = - bt_private_connection_create_notification_iterator(priv_conn, - NULL); - assert(user_data->notif_iter); + conn_status = bt_private_connection_create_notification_iterator( + priv_conn, NULL, &user_data->notif_iter); + assert(conn_status == 0); bt_put(priv_conn); }