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
24 #include <babeltrace/values.h>
25 #include <babeltrace/private-values.h>
26 #include <babeltrace/common-internal.h>
27 #include "babeltrace-cfg.h"
28 #include "babeltrace-cfg-cli-args-connect.h"
30 static bool all_named_and_printable_in_array(GPtrArray
*comps
)
33 bool all_named_and_printable
= true;
35 for (i
= 0; i
< comps
->len
; i
++) {
36 struct bt_config_component
*comp
= g_ptr_array_index(comps
, i
);
38 if (comp
->instance_name
->len
== 0) {
39 all_named_and_printable
= false;
43 if (!bt_common_string_is_printable(comp
->instance_name
->str
)) {
44 all_named_and_printable
= false;
50 return all_named_and_printable
;
53 static bool all_named_and_printable(struct bt_config
*cfg
)
55 return all_named_and_printable_in_array(cfg
->cmd_data
.run
.sources
) &&
56 all_named_and_printable_in_array(cfg
->cmd_data
.run
.filters
) &&
57 all_named_and_printable_in_array(cfg
->cmd_data
.run
.sinks
);
60 static struct bt_config_connection
*bt_config_connection_create(const char *arg
)
62 struct bt_config_connection
*cfg_connection
;
64 cfg_connection
= g_new0(struct bt_config_connection
, 1);
65 if (!cfg_connection
) {
69 cfg_connection
->upstream_comp_name
= g_string_new(NULL
);
70 if (!cfg_connection
->upstream_comp_name
) {
74 cfg_connection
->downstream_comp_name
= g_string_new(NULL
);
75 if (!cfg_connection
->downstream_comp_name
) {
79 cfg_connection
->upstream_port_glob
= g_string_new("*");
80 if (!cfg_connection
->upstream_port_glob
) {
84 cfg_connection
->downstream_port_glob
= g_string_new("*");
85 if (!cfg_connection
->downstream_port_glob
) {
89 cfg_connection
->arg
= g_string_new(arg
);
90 if (!cfg_connection
->arg
) {
97 g_free(cfg_connection
);
98 cfg_connection
= NULL
;
101 return cfg_connection
;
104 static bool validate_port_glob(const char *port_glob
)
106 bool is_valid
= true;
107 const char *ch
= port_glob
;
109 BT_ASSERT(port_glob
);
111 while (*ch
!= '\0') {
124 * This is reserved for future use, to support
125 * full globbing patterns. Those characters must
126 * be escaped with `\`.
140 static int normalize_glob_pattern(GString
*glob_pattern_gs
)
143 char *glob_pattern
= strdup(glob_pattern_gs
->str
);
150 bt_common_normalize_star_glob_pattern(glob_pattern
);
151 g_string_assign(glob_pattern_gs
, glob_pattern
);
158 static struct bt_config_connection
*cfg_connection_from_arg(const char *arg
)
160 const char *at
= arg
;
162 struct bt_config_connection
*cfg_conn
= NULL
;
168 DOWNSTREAM_PORT_GLOB
,
169 } state
= UPSTREAM_NAME
;
171 if (!bt_common_string_is_printable(arg
)) {
175 cfg_conn
= bt_config_connection_create(arg
);
183 gs
= bt_common_string_until(at
, ".:\\", ".:", &end_pos
);
184 if (!gs
|| gs
->len
== 0) {
188 g_string_free(cfg_conn
->upstream_comp_name
, TRUE
);
189 cfg_conn
->upstream_comp_name
= gs
;
192 if (at
[end_pos
] == ':') {
193 state
= DOWNSTREAM_NAME
;
194 } else if (at
[end_pos
] == '.') {
195 state
= UPSTREAM_PORT_GLOB
;
202 case DOWNSTREAM_NAME
:
203 gs
= bt_common_string_until(at
, ".:\\", ".:", &end_pos
);
204 if (!gs
|| gs
->len
== 0) {
208 g_string_free(cfg_conn
->downstream_comp_name
, TRUE
);
209 cfg_conn
->downstream_comp_name
= gs
;
212 if (at
[end_pos
] == '.') {
213 state
= DOWNSTREAM_PORT_GLOB
;
214 } else if (at
[end_pos
] == '\0') {
222 case UPSTREAM_PORT_GLOB
:
223 gs
= bt_common_string_until(at
, ".:", ".:", &end_pos
);
224 if (!gs
|| gs
->len
== 0) {
228 if (!validate_port_glob(gs
->str
)) {
232 if (normalize_glob_pattern(gs
)) {
236 g_string_free(cfg_conn
->upstream_port_glob
, TRUE
);
237 cfg_conn
->upstream_port_glob
= gs
;
240 if (at
[end_pos
] == ':') {
241 state
= DOWNSTREAM_NAME
;
248 case DOWNSTREAM_PORT_GLOB
:
249 gs
= bt_common_string_until(at
, ".:", ".:", &end_pos
);
250 if (!gs
|| gs
->len
== 0) {
254 if (!validate_port_glob(gs
->str
)) {
258 if (normalize_glob_pattern(gs
)) {
262 g_string_free(cfg_conn
->downstream_port_glob
, TRUE
);
263 cfg_conn
->downstream_port_glob
= gs
;
266 if (at
[end_pos
] == '\0') {
278 bt_config_connection_destroy(cfg_conn
);
283 g_string_free(gs
, TRUE
);
289 static struct bt_config_component
*find_component_in_array(GPtrArray
*comps
,
293 struct bt_config_component
*found_comp
= NULL
;
295 for (i
= 0; i
< comps
->len
; i
++) {
296 struct bt_config_component
*comp
= g_ptr_array_index(comps
, i
);
298 if (strcmp(name
, comp
->instance_name
->str
) == 0) {
299 found_comp
= bt_object_get_ref(comp
);
308 static struct bt_config_component
*find_component(struct bt_config
*cfg
,
311 struct bt_config_component
*comp
;
313 comp
= find_component_in_array(cfg
->cmd_data
.run
.sources
, name
);
318 comp
= find_component_in_array(cfg
->cmd_data
.run
.filters
, name
);
323 comp
= find_component_in_array(cfg
->cmd_data
.run
.sinks
, name
);
332 static int validate_all_endpoints_exist(struct bt_config
*cfg
, char *error_buf
,
333 size_t error_buf_size
)
338 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
339 struct bt_config_connection
*connection
=
340 g_ptr_array_index(cfg
->cmd_data
.run
.connections
, i
);
341 struct bt_config_component
*comp
;
343 comp
= find_component(cfg
, connection
->upstream_comp_name
->str
);
344 bt_object_put_ref(comp
);
346 snprintf(error_buf
, error_buf_size
,
347 "Invalid connection: cannot find upstream component `%s`:\n %s\n",
348 connection
->upstream_comp_name
->str
,
349 connection
->arg
->str
);
354 comp
= find_component(cfg
, connection
->downstream_comp_name
->str
);
355 bt_object_put_ref(comp
);
357 snprintf(error_buf
, error_buf_size
,
358 "Invalid connection: cannot find downstream component `%s`:\n %s\n",
359 connection
->downstream_comp_name
->str
,
360 connection
->arg
->str
);
370 static int validate_connection_directions(struct bt_config
*cfg
,
371 char *error_buf
, size_t error_buf_size
)
375 struct bt_config_component
*src_comp
= NULL
;
376 struct bt_config_component
*dst_comp
= NULL
;
378 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
379 struct bt_config_connection
*connection
=
380 g_ptr_array_index(cfg
->cmd_data
.run
.connections
, i
);
382 src_comp
= find_component(cfg
,
383 connection
->upstream_comp_name
->str
);
385 dst_comp
= find_component(cfg
,
386 connection
->downstream_comp_name
->str
);
389 if (src_comp
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
) {
390 if (dst_comp
->type
!= BT_COMPONENT_CLASS_TYPE_FILTER
&&
391 dst_comp
->type
!= BT_COMPONENT_CLASS_TYPE_SINK
) {
392 snprintf(error_buf
, error_buf_size
,
393 "Invalid connection: source component `%s` not connected to filter or sink component:\n %s\n",
394 connection
->upstream_comp_name
->str
,
395 connection
->arg
->str
);
399 } else if (src_comp
->type
== BT_COMPONENT_CLASS_TYPE_FILTER
) {
400 if (dst_comp
->type
!= BT_COMPONENT_CLASS_TYPE_FILTER
&&
401 dst_comp
->type
!= BT_COMPONENT_CLASS_TYPE_SINK
) {
402 snprintf(error_buf
, error_buf_size
,
403 "Invalid connection: filter component `%s` not connected to filter or sink component:\n %s\n",
404 connection
->upstream_comp_name
->str
,
405 connection
->arg
->str
);
410 snprintf(error_buf
, error_buf_size
,
411 "Invalid connection: cannot connect sink component `%s` to component `%s`:\n %s\n",
412 connection
->upstream_comp_name
->str
,
413 connection
->downstream_comp_name
->str
,
414 connection
->arg
->str
);
419 BT_OBJECT_PUT_REF_AND_RESET(src_comp
);
420 BT_OBJECT_PUT_REF_AND_RESET(dst_comp
);
424 bt_object_put_ref(src_comp
);
425 bt_object_put_ref(dst_comp
);
429 static int validate_no_cycles_rec(struct bt_config
*cfg
, GPtrArray
*path
,
430 char *error_buf
, size_t error_buf_size
)
434 const char *src_comp_name
;
436 BT_ASSERT(path
&& path
->len
> 0);
437 src_comp_name
= g_ptr_array_index(path
, path
->len
- 1);
439 for (conn_i
= 0; conn_i
< cfg
->cmd_data
.run
.connections
->len
; conn_i
++) {
440 struct bt_config_connection
*conn
=
441 g_ptr_array_index(cfg
->cmd_data
.run
.connections
, conn_i
);
443 if (strcmp(conn
->upstream_comp_name
->str
, src_comp_name
) == 0) {
446 for (path_i
= 0; path_i
< path
->len
; path_i
++) {
447 const char *comp_name
=
448 g_ptr_array_index(path
, path_i
);
450 if (strcmp(comp_name
, conn
->downstream_comp_name
->str
) == 0) {
451 snprintf(error_buf
, error_buf_size
,
452 "Invalid connection: connection forms a cycle:\n %s\n",
459 g_ptr_array_add(path
, conn
->downstream_comp_name
->str
);
460 ret
= validate_no_cycles_rec(cfg
, path
, error_buf
,
466 g_ptr_array_remove_index(path
, path
->len
- 1);
474 static int validate_no_cycles(struct bt_config
*cfg
, char *error_buf
,
475 size_t error_buf_size
)
481 path
= g_ptr_array_new();
487 g_ptr_array_add(path
, NULL
);
489 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
490 struct bt_config_connection
*conn
=
491 g_ptr_array_index(cfg
->cmd_data
.run
.connections
, i
);
493 g_ptr_array_index(path
, 0) = conn
->upstream_comp_name
->str
;
494 ret
= validate_no_cycles_rec(cfg
, path
,
495 error_buf
, error_buf_size
);
503 g_ptr_array_free(path
, TRUE
);
509 static int validate_all_components_connected_in_array(GPtrArray
*comps
,
510 struct bt_value
*connected_components
,
511 char *error_buf
, size_t error_buf_size
)
516 for (i
= 0; i
< comps
->len
; i
++) {
517 struct bt_config_component
*comp
= g_ptr_array_index(comps
, i
);
519 if (!bt_value_map_has_entry(connected_components
,
520 comp
->instance_name
->str
)) {
521 snprintf(error_buf
, error_buf_size
,
522 "Component `%s` is not connected\n",
523 comp
->instance_name
->str
);
533 static int validate_all_components_connected(struct bt_config
*cfg
,
534 char *error_buf
, size_t error_buf_size
)
538 struct bt_private_value
*connected_components
=
539 bt_private_value_map_create();
541 if (!connected_components
) {
546 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
547 struct bt_config_connection
*connection
=
548 g_ptr_array_index(cfg
->cmd_data
.run
.connections
, i
);
550 ret
= bt_private_value_map_insert_entry(connected_components
,
551 connection
->upstream_comp_name
->str
, bt_value_null
);
556 ret
= bt_private_value_map_insert_entry(connected_components
,
557 connection
->downstream_comp_name
->str
, bt_value_null
);
563 ret
= validate_all_components_connected_in_array(
564 cfg
->cmd_data
.run
.sources
,
565 bt_value_borrow_from_private(connected_components
),
566 error_buf
, error_buf_size
);
571 ret
= validate_all_components_connected_in_array(
572 cfg
->cmd_data
.run
.filters
,
573 bt_value_borrow_from_private(connected_components
),
574 error_buf
, error_buf_size
);
579 ret
= validate_all_components_connected_in_array(
580 cfg
->cmd_data
.run
.sinks
,
581 bt_value_borrow_from_private(connected_components
),
582 error_buf
, error_buf_size
);
588 bt_object_put_ref(connected_components
);
592 static int validate_no_duplicate_connection(struct bt_config
*cfg
,
593 char *error_buf
, size_t error_buf_size
)
597 struct bt_private_value
*flat_connection_names
=
598 bt_private_value_map_create();
599 GString
*flat_connection_name
= NULL
;
601 if (!flat_connection_names
) {
606 flat_connection_name
= g_string_new(NULL
);
607 if (!flat_connection_name
) {
612 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
613 struct bt_config_connection
*connection
=
614 g_ptr_array_index(cfg
->cmd_data
.run
.connections
, i
);
616 g_string_printf(flat_connection_name
, "%s\x01%s\x01%s\x01%s",
617 connection
->upstream_comp_name
->str
,
618 connection
->upstream_port_glob
->str
,
619 connection
->downstream_comp_name
->str
,
620 connection
->downstream_port_glob
->str
);
622 if (bt_value_map_has_entry(bt_value_borrow_from_private(
623 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_private_value_map_insert_entry(flat_connection_names
,
633 flat_connection_name
->str
, bt_value_null
);
640 bt_object_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 struct 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_size(connection_args
); i
++) {
697 struct bt_value
*arg_value
=
698 bt_value_array_borrow_element_by_index(
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
);