2 * Copyright 2017 Philippe Proulx <pproulx@efficios.com>
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 #include <babeltrace2/babeltrace.h>
27 #include "common/common.h"
28 #include "babeltrace2-cfg.h"
29 #include "babeltrace2-cfg-cli-args-connect.h"
31 static bool all_named_and_printable_in_array(GPtrArray
*comps
)
34 bool all_named_and_printable
= true;
36 for (i
= 0; i
< comps
->len
; i
++) {
37 struct bt_config_component
*comp
= g_ptr_array_index(comps
, i
);
39 if (comp
->instance_name
->len
== 0) {
40 all_named_and_printable
= false;
44 if (!bt_common_string_is_printable(comp
->instance_name
->str
)) {
45 all_named_and_printable
= false;
51 return all_named_and_printable
;
54 static bool all_named_and_printable(struct bt_config
*cfg
)
56 return all_named_and_printable_in_array(cfg
->cmd_data
.run
.sources
) &&
57 all_named_and_printable_in_array(cfg
->cmd_data
.run
.filters
) &&
58 all_named_and_printable_in_array(cfg
->cmd_data
.run
.sinks
);
61 static struct bt_config_connection
*bt_config_connection_create(const char *arg
)
63 struct bt_config_connection
*cfg_connection
;
65 cfg_connection
= g_new0(struct bt_config_connection
, 1);
66 if (!cfg_connection
) {
70 cfg_connection
->upstream_comp_name
= g_string_new(NULL
);
71 if (!cfg_connection
->upstream_comp_name
) {
75 cfg_connection
->downstream_comp_name
= g_string_new(NULL
);
76 if (!cfg_connection
->downstream_comp_name
) {
80 cfg_connection
->upstream_port_glob
= g_string_new("*");
81 if (!cfg_connection
->upstream_port_glob
) {
85 cfg_connection
->downstream_port_glob
= g_string_new("*");
86 if (!cfg_connection
->downstream_port_glob
) {
90 cfg_connection
->arg
= g_string_new(arg
);
91 if (!cfg_connection
->arg
) {
98 g_free(cfg_connection
);
99 cfg_connection
= NULL
;
102 return cfg_connection
;
105 static bool validate_port_glob(const char *port_glob
)
107 bool is_valid
= true;
108 const char *ch
= port_glob
;
110 BT_ASSERT(port_glob
);
112 while (*ch
!= '\0') {
125 * This is reserved for future use, to support
126 * full globbing patterns. Those characters must
127 * be escaped with `\`.
141 static int normalize_glob_pattern(GString
*glob_pattern_gs
)
144 char *glob_pattern
= strdup(glob_pattern_gs
->str
);
151 bt_common_normalize_star_glob_pattern(glob_pattern
);
152 g_string_assign(glob_pattern_gs
, glob_pattern
);
159 static struct bt_config_connection
*cfg_connection_from_arg(const char *arg
)
161 const char *at
= arg
;
163 struct bt_config_connection
*cfg_conn
= NULL
;
169 DOWNSTREAM_PORT_GLOB
,
170 } state
= UPSTREAM_NAME
;
172 if (!bt_common_string_is_printable(arg
)) {
176 cfg_conn
= bt_config_connection_create(arg
);
184 gs
= bt_common_string_until(at
, ".:\\", ".:", &end_pos
);
185 if (!gs
|| gs
->len
== 0) {
189 g_string_free(cfg_conn
->upstream_comp_name
, TRUE
);
190 cfg_conn
->upstream_comp_name
= gs
;
193 if (at
[end_pos
] == ':') {
194 state
= DOWNSTREAM_NAME
;
195 } else if (at
[end_pos
] == '.') {
196 state
= UPSTREAM_PORT_GLOB
;
203 case DOWNSTREAM_NAME
:
204 gs
= bt_common_string_until(at
, ".:\\", ".:", &end_pos
);
205 if (!gs
|| gs
->len
== 0) {
209 g_string_free(cfg_conn
->downstream_comp_name
, TRUE
);
210 cfg_conn
->downstream_comp_name
= gs
;
213 if (at
[end_pos
] == '.') {
214 state
= DOWNSTREAM_PORT_GLOB
;
215 } else if (at
[end_pos
] == '\0') {
223 case UPSTREAM_PORT_GLOB
:
224 gs
= bt_common_string_until(at
, ".:", ".:", &end_pos
);
225 if (!gs
|| gs
->len
== 0) {
229 if (!validate_port_glob(gs
->str
)) {
233 if (normalize_glob_pattern(gs
)) {
237 g_string_free(cfg_conn
->upstream_port_glob
, TRUE
);
238 cfg_conn
->upstream_port_glob
= gs
;
241 if (at
[end_pos
] == ':') {
242 state
= DOWNSTREAM_NAME
;
249 case DOWNSTREAM_PORT_GLOB
:
250 gs
= bt_common_string_until(at
, ".:", ".:", &end_pos
);
251 if (!gs
|| gs
->len
== 0) {
255 if (!validate_port_glob(gs
->str
)) {
259 if (normalize_glob_pattern(gs
)) {
263 g_string_free(cfg_conn
->downstream_port_glob
, TRUE
);
264 cfg_conn
->downstream_port_glob
= gs
;
267 if (at
[end_pos
] == '\0') {
279 bt_config_connection_destroy(cfg_conn
);
284 g_string_free(gs
, TRUE
);
290 static struct bt_config_component
*find_component_in_array(GPtrArray
*comps
,
294 struct bt_config_component
*found_comp
= NULL
;
296 for (i
= 0; i
< comps
->len
; i
++) {
297 struct bt_config_component
*comp
= g_ptr_array_index(comps
, i
);
299 if (strcmp(name
, comp
->instance_name
->str
) == 0) {
301 bt_object_get_ref(found_comp
);
310 static struct bt_config_component
*find_component(struct bt_config
*cfg
,
313 struct bt_config_component
*comp
;
315 comp
= find_component_in_array(cfg
->cmd_data
.run
.sources
, name
);
320 comp
= find_component_in_array(cfg
->cmd_data
.run
.filters
, name
);
325 comp
= find_component_in_array(cfg
->cmd_data
.run
.sinks
, name
);
334 static int validate_all_endpoints_exist(struct bt_config
*cfg
, char *error_buf
,
335 size_t error_buf_size
)
340 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
341 struct bt_config_connection
*connection
=
342 g_ptr_array_index(cfg
->cmd_data
.run
.connections
, i
);
343 struct bt_config_component
*comp
;
345 comp
= find_component(cfg
, connection
->upstream_comp_name
->str
);
346 bt_object_put_ref(comp
);
348 snprintf(error_buf
, error_buf_size
,
349 "Invalid connection: cannot find upstream component `%s`:\n %s\n",
350 connection
->upstream_comp_name
->str
,
351 connection
->arg
->str
);
356 comp
= find_component(cfg
, connection
->downstream_comp_name
->str
);
357 bt_object_put_ref(comp
);
359 snprintf(error_buf
, error_buf_size
,
360 "Invalid connection: cannot find downstream component `%s`:\n %s\n",
361 connection
->downstream_comp_name
->str
,
362 connection
->arg
->str
);
372 static int validate_connection_directions(struct bt_config
*cfg
,
373 char *error_buf
, size_t error_buf_size
)
377 struct bt_config_component
*src_comp
= NULL
;
378 struct bt_config_component
*dst_comp
= NULL
;
380 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
381 struct bt_config_connection
*connection
=
382 g_ptr_array_index(cfg
->cmd_data
.run
.connections
, i
);
384 src_comp
= find_component(cfg
,
385 connection
->upstream_comp_name
->str
);
387 dst_comp
= find_component(cfg
,
388 connection
->downstream_comp_name
->str
);
391 if (src_comp
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
) {
392 if (dst_comp
->type
!= BT_COMPONENT_CLASS_TYPE_FILTER
&&
393 dst_comp
->type
!= BT_COMPONENT_CLASS_TYPE_SINK
) {
394 snprintf(error_buf
, error_buf_size
,
395 "Invalid connection: source component `%s` not connected to filter or sink component:\n %s\n",
396 connection
->upstream_comp_name
->str
,
397 connection
->arg
->str
);
401 } else if (src_comp
->type
== BT_COMPONENT_CLASS_TYPE_FILTER
) {
402 if (dst_comp
->type
!= BT_COMPONENT_CLASS_TYPE_FILTER
&&
403 dst_comp
->type
!= BT_COMPONENT_CLASS_TYPE_SINK
) {
404 snprintf(error_buf
, error_buf_size
,
405 "Invalid connection: filter component `%s` not connected to filter or sink component:\n %s\n",
406 connection
->upstream_comp_name
->str
,
407 connection
->arg
->str
);
412 snprintf(error_buf
, error_buf_size
,
413 "Invalid connection: cannot connect sink component `%s` to component `%s`:\n %s\n",
414 connection
->upstream_comp_name
->str
,
415 connection
->downstream_comp_name
->str
,
416 connection
->arg
->str
);
421 BT_OBJECT_PUT_REF_AND_RESET(src_comp
);
422 BT_OBJECT_PUT_REF_AND_RESET(dst_comp
);
426 bt_object_put_ref(src_comp
);
427 bt_object_put_ref(dst_comp
);
431 static int validate_no_cycles_rec(struct bt_config
*cfg
, GPtrArray
*path
,
432 char *error_buf
, size_t error_buf_size
)
436 const char *src_comp_name
;
438 BT_ASSERT(path
&& path
->len
> 0);
439 src_comp_name
= g_ptr_array_index(path
, path
->len
- 1);
441 for (conn_i
= 0; conn_i
< cfg
->cmd_data
.run
.connections
->len
; conn_i
++) {
442 struct bt_config_connection
*conn
=
443 g_ptr_array_index(cfg
->cmd_data
.run
.connections
, conn_i
);
445 if (strcmp(conn
->upstream_comp_name
->str
, src_comp_name
) == 0) {
448 for (path_i
= 0; path_i
< path
->len
; path_i
++) {
449 const char *comp_name
=
450 g_ptr_array_index(path
, path_i
);
452 if (strcmp(comp_name
, conn
->downstream_comp_name
->str
) == 0) {
453 snprintf(error_buf
, error_buf_size
,
454 "Invalid connection: connection forms a cycle:\n %s\n",
461 g_ptr_array_add(path
, conn
->downstream_comp_name
->str
);
462 ret
= validate_no_cycles_rec(cfg
, path
, error_buf
,
468 g_ptr_array_remove_index(path
, path
->len
- 1);
476 static int validate_no_cycles(struct bt_config
*cfg
, char *error_buf
,
477 size_t error_buf_size
)
483 path
= g_ptr_array_new();
489 g_ptr_array_add(path
, NULL
);
491 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
492 struct bt_config_connection
*conn
=
493 g_ptr_array_index(cfg
->cmd_data
.run
.connections
, i
);
495 g_ptr_array_index(path
, 0) = conn
->upstream_comp_name
->str
;
496 ret
= validate_no_cycles_rec(cfg
, path
,
497 error_buf
, error_buf_size
);
505 g_ptr_array_free(path
, TRUE
);
511 static int validate_all_components_connected_in_array(GPtrArray
*comps
,
512 const bt_value
*connected_components
,
513 char *error_buf
, size_t error_buf_size
)
518 for (i
= 0; i
< comps
->len
; i
++) {
519 struct bt_config_component
*comp
= g_ptr_array_index(comps
, i
);
521 if (!bt_value_map_has_entry(connected_components
,
522 comp
->instance_name
->str
)) {
523 snprintf(error_buf
, error_buf_size
,
524 "Component `%s` is not connected\n",
525 comp
->instance_name
->str
);
535 static int validate_all_components_connected(struct bt_config
*cfg
,
536 char *error_buf
, size_t error_buf_size
)
540 bt_value
*connected_components
= bt_value_map_create();
542 if (!connected_components
) {
547 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
548 struct bt_config_connection
*connection
=
549 g_ptr_array_index(cfg
->cmd_data
.run
.connections
, i
);
551 ret
= bt_value_map_insert_entry(connected_components
,
552 connection
->upstream_comp_name
->str
, bt_value_null
);
557 ret
= bt_value_map_insert_entry(connected_components
,
558 connection
->downstream_comp_name
->str
, bt_value_null
);
564 ret
= validate_all_components_connected_in_array(
565 cfg
->cmd_data
.run
.sources
,
566 connected_components
,
567 error_buf
, error_buf_size
);
572 ret
= validate_all_components_connected_in_array(
573 cfg
->cmd_data
.run
.filters
,
574 connected_components
,
575 error_buf
, error_buf_size
);
580 ret
= validate_all_components_connected_in_array(
581 cfg
->cmd_data
.run
.sinks
,
582 connected_components
,
583 error_buf
, error_buf_size
);
589 bt_value_put_ref(connected_components
);
593 static int validate_no_duplicate_connection(struct bt_config
*cfg
,
594 char *error_buf
, size_t error_buf_size
)
598 bt_value
*flat_connection_names
=
599 bt_value_map_create();
600 GString
*flat_connection_name
= NULL
;
602 if (!flat_connection_names
) {
607 flat_connection_name
= g_string_new(NULL
);
608 if (!flat_connection_name
) {
613 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
614 struct bt_config_connection
*connection
=
615 g_ptr_array_index(cfg
->cmd_data
.run
.connections
, i
);
617 g_string_printf(flat_connection_name
, "%s\x01%s\x01%s\x01%s",
618 connection
->upstream_comp_name
->str
,
619 connection
->upstream_port_glob
->str
,
620 connection
->downstream_comp_name
->str
,
621 connection
->downstream_port_glob
->str
);
623 if (bt_value_map_has_entry(flat_connection_names
,
624 flat_connection_name
->str
)) {
625 snprintf(error_buf
, error_buf_size
,
626 "Duplicate connection:\n %s\n",
627 connection
->arg
->str
);
632 ret
= bt_value_map_insert_entry(flat_connection_names
,
633 flat_connection_name
->str
, bt_value_null
);
640 bt_value_put_ref(flat_connection_names
);
642 if (flat_connection_name
) {
643 g_string_free(flat_connection_name
, TRUE
);
649 static int validate_connections(struct bt_config
*cfg
, char *error_buf
,
650 size_t error_buf_size
)
654 ret
= validate_all_endpoints_exist(cfg
, error_buf
, error_buf_size
);
659 ret
= validate_connection_directions(cfg
, error_buf
, error_buf_size
);
664 ret
= validate_all_components_connected(cfg
, error_buf
, error_buf_size
);
669 ret
= validate_no_duplicate_connection(cfg
, error_buf
, error_buf_size
);
674 ret
= validate_no_cycles(cfg
, error_buf
, error_buf_size
);
683 int bt_config_cli_args_create_connections(struct bt_config
*cfg
,
684 const bt_value
*connection_args
,
685 char *error_buf
, size_t error_buf_size
)
690 if (!all_named_and_printable(cfg
)) {
691 snprintf(error_buf
, error_buf_size
,
692 "One or more components are unnamed (use --name) or contain a non-printable character\n");
696 for (i
= 0; i
< bt_value_array_get_length(connection_args
); i
++) {
697 const bt_value
*arg_value
=
698 bt_value_array_borrow_element_by_index_const(
701 struct bt_config_connection
*cfg_connection
;
703 arg
= bt_value_string_get(arg_value
);
704 cfg_connection
= cfg_connection_from_arg(arg
);
705 if (!cfg_connection
) {
706 snprintf(error_buf
, error_buf_size
, "Cannot parse --connect option's argument:\n %s\n",
711 g_ptr_array_add(cfg
->cmd_data
.run
.connections
,
716 ret
= validate_connections(cfg
, error_buf
, error_buf_size
);