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/common-internal.h>
26 #include "babeltrace-cfg.h"
27 #include "babeltrace-cfg-cli-args-connect.h"
29 static bool all_named_and_printable_in_array(GPtrArray
*comps
)
32 bool all_named_and_printable
= true;
34 for (i
= 0; i
< comps
->len
; i
++) {
35 struct bt_config_component
*comp
= g_ptr_array_index(comps
, i
);
37 if (comp
->instance_name
->len
== 0) {
38 all_named_and_printable
= false;
42 if (!bt_common_string_is_printable(comp
->instance_name
->str
)) {
43 all_named_and_printable
= false;
49 return all_named_and_printable
;
52 static bool all_named_and_printable(struct bt_config
*cfg
)
54 return all_named_and_printable_in_array(cfg
->cmd_data
.run
.sources
) &&
55 all_named_and_printable_in_array(cfg
->cmd_data
.run
.filters
) &&
56 all_named_and_printable_in_array(cfg
->cmd_data
.run
.sinks
);
59 static struct bt_config_connection
*bt_config_connection_create(const char *arg
)
61 struct bt_config_connection
*cfg_connection
;
63 cfg_connection
= g_new0(struct bt_config_connection
, 1);
64 if (!cfg_connection
) {
68 cfg_connection
->upstream_comp_name
= g_string_new(NULL
);
69 if (!cfg_connection
->upstream_comp_name
) {
73 cfg_connection
->downstream_comp_name
= g_string_new(NULL
);
74 if (!cfg_connection
->downstream_comp_name
) {
78 cfg_connection
->upstream_port_glob
= g_string_new("*");
79 if (!cfg_connection
->upstream_port_glob
) {
83 cfg_connection
->downstream_port_glob
= g_string_new("*");
84 if (!cfg_connection
->downstream_port_glob
) {
88 cfg_connection
->arg
= g_string_new(arg
);
89 if (!cfg_connection
->arg
) {
96 g_free(cfg_connection
);
97 cfg_connection
= NULL
;
100 return cfg_connection
;
103 static bool validate_port_glob(const char *port_glob
)
105 bool is_valid
= true;
106 const char *ch
= port_glob
;
110 while (*ch
!= '\0') {
123 * This is reserved for future use, to support
124 * full globbing patterns. Those characters must
125 * be escaped with `\`.
139 static int normalize_glob_pattern(GString
*glob_pattern_gs
)
142 char *glob_pattern
= strdup(glob_pattern_gs
->str
);
149 bt_common_normalize_star_glob_pattern(glob_pattern
);
150 g_string_assign(glob_pattern_gs
, glob_pattern
);
157 static struct bt_config_connection
*cfg_connection_from_arg(const char *arg
)
159 const char *at
= arg
;
161 struct bt_config_connection
*cfg_conn
= NULL
;
167 DOWNSTREAM_PORT_GLOB
,
168 } state
= UPSTREAM_NAME
;
170 if (!bt_common_string_is_printable(arg
)) {
174 cfg_conn
= bt_config_connection_create(arg
);
182 gs
= bt_common_string_until(at
, ".:\\", ".:", &end_pos
);
183 if (!gs
|| gs
->len
== 0) {
187 g_string_free(cfg_conn
->upstream_comp_name
, TRUE
);
188 cfg_conn
->upstream_comp_name
= gs
;
191 if (at
[end_pos
] == ':') {
192 state
= DOWNSTREAM_NAME
;
193 } else if (at
[end_pos
] == '.') {
194 state
= UPSTREAM_PORT_GLOB
;
201 case DOWNSTREAM_NAME
:
202 gs
= bt_common_string_until(at
, ".:\\", ".:", &end_pos
);
203 if (!gs
|| gs
->len
== 0) {
207 g_string_free(cfg_conn
->downstream_comp_name
, TRUE
);
208 cfg_conn
->downstream_comp_name
= gs
;
211 if (at
[end_pos
] == '.') {
212 state
= DOWNSTREAM_PORT_GLOB
;
213 } else if (at
[end_pos
] == '\0') {
221 case UPSTREAM_PORT_GLOB
:
222 gs
= bt_common_string_until(at
, ".:", ".:", &end_pos
);
223 if (!gs
|| gs
->len
== 0) {
227 if (!validate_port_glob(gs
->str
)) {
231 if (normalize_glob_pattern(gs
)) {
235 g_string_free(cfg_conn
->upstream_port_glob
, TRUE
);
236 cfg_conn
->upstream_port_glob
= gs
;
239 if (at
[end_pos
] == ':') {
240 state
= DOWNSTREAM_NAME
;
247 case DOWNSTREAM_PORT_GLOB
:
248 gs
= bt_common_string_until(at
, ".:", ".:", &end_pos
);
249 if (!gs
|| gs
->len
== 0) {
253 if (!validate_port_glob(gs
->str
)) {
257 if (normalize_glob_pattern(gs
)) {
261 g_string_free(cfg_conn
->downstream_port_glob
, TRUE
);
262 cfg_conn
->downstream_port_glob
= gs
;
265 if (at
[end_pos
] == '\0') {
277 bt_config_connection_destroy(cfg_conn
);
282 g_string_free(gs
, TRUE
);
288 static struct bt_config_component
*find_component_in_array(GPtrArray
*comps
,
292 struct bt_config_component
*found_comp
= NULL
;
294 for (i
= 0; i
< comps
->len
; i
++) {
295 struct bt_config_component
*comp
= g_ptr_array_index(comps
, i
);
297 if (strcmp(name
, comp
->instance_name
->str
) == 0) {
298 found_comp
= bt_get(comp
);
307 static struct bt_config_component
*find_component(struct bt_config
*cfg
,
310 struct bt_config_component
*comp
;
312 comp
= find_component_in_array(cfg
->cmd_data
.run
.sources
, name
);
317 comp
= find_component_in_array(cfg
->cmd_data
.run
.filters
, name
);
322 comp
= find_component_in_array(cfg
->cmd_data
.run
.sinks
, name
);
331 static int validate_all_endpoints_exist(struct bt_config
*cfg
, char *error_buf
,
332 size_t error_buf_size
)
337 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
338 struct bt_config_connection
*connection
=
339 g_ptr_array_index(cfg
->cmd_data
.run
.connections
, i
);
340 struct bt_config_component
*comp
;
342 comp
= find_component(cfg
, connection
->upstream_comp_name
->str
);
345 snprintf(error_buf
, error_buf_size
,
346 "Invalid connection: cannot find upstream component `%s`:\n %s\n",
347 connection
->upstream_comp_name
->str
,
348 connection
->arg
->str
);
353 comp
= find_component(cfg
, connection
->downstream_comp_name
->str
);
356 snprintf(error_buf
, error_buf_size
,
357 "Invalid connection: cannot find downstream component `%s`:\n %s\n",
358 connection
->downstream_comp_name
->str
,
359 connection
->arg
->str
);
369 static int validate_connection_directions(struct bt_config
*cfg
,
370 char *error_buf
, size_t error_buf_size
)
374 struct bt_config_component
*src_comp
= NULL
;
375 struct bt_config_component
*dst_comp
= NULL
;
377 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
378 struct bt_config_connection
*connection
=
379 g_ptr_array_index(cfg
->cmd_data
.run
.connections
, i
);
381 src_comp
= find_component(cfg
,
382 connection
->upstream_comp_name
->str
);
384 dst_comp
= find_component(cfg
,
385 connection
->downstream_comp_name
->str
);
388 if (src_comp
->type
== BT_COMPONENT_CLASS_TYPE_SOURCE
) {
389 if (dst_comp
->type
!= BT_COMPONENT_CLASS_TYPE_FILTER
&&
390 dst_comp
->type
!= BT_COMPONENT_CLASS_TYPE_SINK
) {
391 snprintf(error_buf
, error_buf_size
,
392 "Invalid connection: source component `%s` not connected to filter or sink component:\n %s\n",
393 connection
->upstream_comp_name
->str
,
394 connection
->arg
->str
);
398 } else if (src_comp
->type
== BT_COMPONENT_CLASS_TYPE_FILTER
) {
399 if (dst_comp
->type
!= BT_COMPONENT_CLASS_TYPE_FILTER
&&
400 dst_comp
->type
!= BT_COMPONENT_CLASS_TYPE_SINK
) {
401 snprintf(error_buf
, error_buf_size
,
402 "Invalid connection: filter component `%s` not connected to filter or sink component:\n %s\n",
403 connection
->upstream_comp_name
->str
,
404 connection
->arg
->str
);
409 snprintf(error_buf
, error_buf_size
,
410 "Invalid connection: cannot connect sink component `%s` to component `%s`:\n %s\n",
411 connection
->upstream_comp_name
->str
,
412 connection
->downstream_comp_name
->str
,
413 connection
->arg
->str
);
428 static int validate_no_cycles_rec(struct bt_config
*cfg
, GPtrArray
*path
,
429 char *error_buf
, size_t error_buf_size
)
433 const char *src_comp_name
;
435 assert(path
&& path
->len
> 0);
436 src_comp_name
= g_ptr_array_index(path
, path
->len
- 1);
438 for (conn_i
= 0; conn_i
< cfg
->cmd_data
.run
.connections
->len
; conn_i
++) {
439 struct bt_config_connection
*conn
=
440 g_ptr_array_index(cfg
->cmd_data
.run
.connections
, conn_i
);
442 if (strcmp(conn
->upstream_comp_name
->str
, src_comp_name
) == 0) {
445 for (path_i
= 0; path_i
< path
->len
; path_i
++) {
446 const char *comp_name
=
447 g_ptr_array_index(path
, path_i
);
449 if (strcmp(comp_name
, conn
->downstream_comp_name
->str
) == 0) {
450 snprintf(error_buf
, error_buf_size
,
451 "Invalid connection: connection forms a cycle:\n %s\n",
458 g_ptr_array_add(path
, conn
->downstream_comp_name
->str
);
459 ret
= validate_no_cycles_rec(cfg
, path
, error_buf
,
465 g_ptr_array_remove_index(path
, path
->len
- 1);
473 static int validate_no_cycles(struct bt_config
*cfg
, char *error_buf
,
474 size_t error_buf_size
)
480 path
= g_ptr_array_new();
486 g_ptr_array_add(path
, NULL
);
488 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
489 struct bt_config_connection
*conn
=
490 g_ptr_array_index(cfg
->cmd_data
.run
.connections
, i
);
492 g_ptr_array_index(path
, 0) = conn
->upstream_comp_name
->str
;
493 ret
= validate_no_cycles_rec(cfg
, path
,
494 error_buf
, error_buf_size
);
502 g_ptr_array_free(path
, TRUE
);
508 static int validate_all_components_connected_in_array(GPtrArray
*comps
,
509 struct bt_value
*connected_components
,
510 char *error_buf
, size_t error_buf_size
)
515 for (i
= 0; i
< comps
->len
; i
++) {
516 struct bt_config_component
*comp
= g_ptr_array_index(comps
, i
);
518 if (!bt_value_map_has_key(connected_components
,
519 comp
->instance_name
->str
)) {
520 snprintf(error_buf
, error_buf_size
,
521 "Component `%s` is not connected\n",
522 comp
->instance_name
->str
);
532 static int validate_all_components_connected(struct bt_config
*cfg
,
533 char *error_buf
, size_t error_buf_size
)
537 struct bt_value
*connected_components
= bt_value_map_create();
539 if (!connected_components
) {
544 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
545 struct bt_config_connection
*connection
=
546 g_ptr_array_index(cfg
->cmd_data
.run
.connections
, i
);
548 ret
= bt_value_map_insert(connected_components
,
549 connection
->upstream_comp_name
->str
, bt_value_null
);
554 ret
= bt_value_map_insert(connected_components
,
555 connection
->downstream_comp_name
->str
, bt_value_null
);
561 ret
= validate_all_components_connected_in_array(
562 cfg
->cmd_data
.run
.sources
, connected_components
,
563 error_buf
, error_buf_size
);
568 ret
= validate_all_components_connected_in_array(
569 cfg
->cmd_data
.run
.filters
, connected_components
,
570 error_buf
, error_buf_size
);
575 ret
= validate_all_components_connected_in_array(
576 cfg
->cmd_data
.run
.sinks
, connected_components
,
577 error_buf
, error_buf_size
);
583 bt_put(connected_components
);
587 static int validate_no_duplicate_connection(struct bt_config
*cfg
,
588 char *error_buf
, size_t error_buf_size
)
592 struct bt_value
*flat_connection_names
= bt_value_map_create();
593 GString
*flat_connection_name
= NULL
;
595 if (!flat_connection_names
) {
600 flat_connection_name
= g_string_new(NULL
);
601 if (!flat_connection_name
) {
606 for (i
= 0; i
< cfg
->cmd_data
.run
.connections
->len
; i
++) {
607 struct bt_config_connection
*connection
=
608 g_ptr_array_index(cfg
->cmd_data
.run
.connections
, i
);
610 g_string_printf(flat_connection_name
, "%s\x01%s\x01%s\x01%s",
611 connection
->upstream_comp_name
->str
,
612 connection
->upstream_port_glob
->str
,
613 connection
->downstream_comp_name
->str
,
614 connection
->downstream_port_glob
->str
);
616 if (bt_value_map_has_key(flat_connection_names
,
617 flat_connection_name
->str
)) {
618 snprintf(error_buf
, error_buf_size
,
619 "Duplicate connection:\n %s\n",
620 connection
->arg
->str
);
625 ret
= bt_value_map_insert(flat_connection_names
,
626 flat_connection_name
->str
, bt_value_null
);
633 bt_put(flat_connection_names
);
635 if (flat_connection_name
) {
636 g_string_free(flat_connection_name
, TRUE
);
642 static int validate_connections(struct bt_config
*cfg
, char *error_buf
,
643 size_t error_buf_size
)
647 ret
= validate_all_endpoints_exist(cfg
, error_buf
, error_buf_size
);
652 ret
= validate_connection_directions(cfg
, error_buf
, error_buf_size
);
657 ret
= validate_all_components_connected(cfg
, error_buf
, error_buf_size
);
662 ret
= validate_no_duplicate_connection(cfg
, error_buf
, error_buf_size
);
667 ret
= validate_no_cycles(cfg
, error_buf
, error_buf_size
);
676 int bt_config_cli_args_create_connections(struct bt_config
*cfg
,
677 struct bt_value
*connection_args
,
678 char *error_buf
, size_t error_buf_size
)
683 if (!all_named_and_printable(cfg
)) {
684 snprintf(error_buf
, error_buf_size
,
685 "One or more components are unnamed (use --name) or contain a non-printable character\n");
689 for (i
= 0; i
< bt_value_array_size(connection_args
); i
++) {
690 struct bt_value
*arg_value
=
691 bt_value_array_get(connection_args
, i
);
693 struct bt_config_connection
*cfg_connection
;
695 ret
= bt_value_string_get(arg_value
, &arg
);
698 cfg_connection
= cfg_connection_from_arg(arg
);
699 if (!cfg_connection
) {
700 snprintf(error_buf
, error_buf_size
, "Cannot parse --connect option's argument:\n %s\n",
705 g_ptr_array_add(cfg
->cmd_data
.run
.connections
,
710 ret
= validate_connections(cfg
, error_buf
, error_buf_size
);