2 * Babeltrace common functions
4 * Copyright 2016 Philippe Proulx <pproulx@efficios.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 #define BT_LOG_OUTPUT_LEVEL log_level
26 #define BT_LOG_TAG "COMMON"
27 #include "logging/log.h"
32 #include <sys/types.h>
35 #include "common/assert.h"
43 #include "common/macros.h"
44 #include "common/common.h"
45 #include "compat/unistd.h"
49 #include <sys/ioctl.h>
52 #define SYSTEM_PLUGIN_PATH BABELTRACE_PLUGINS_DIR
53 #define HOME_ENV_VAR "HOME"
54 #define HOME_PLUGIN_SUBPATH "/.local/lib/babeltrace2/plugins"
56 static const char *bt_common_color_code_reset
= "";
57 static const char *bt_common_color_code_bold
= "";
58 static const char *bt_common_color_code_fg_default
= "";
59 static const char *bt_common_color_code_fg_red
= "";
60 static const char *bt_common_color_code_fg_green
= "";
61 static const char *bt_common_color_code_fg_yellow
= "";
62 static const char *bt_common_color_code_fg_blue
= "";
63 static const char *bt_common_color_code_fg_magenta
= "";
64 static const char *bt_common_color_code_fg_cyan
= "";
65 static const char *bt_common_color_code_fg_light_gray
= "";
66 static const char *bt_common_color_code_fg_bright_red
= "";
67 static const char *bt_common_color_code_fg_bright_green
= "";
68 static const char *bt_common_color_code_fg_bright_yellow
= "";
69 static const char *bt_common_color_code_fg_bright_blue
= "";
70 static const char *bt_common_color_code_fg_bright_magenta
= "";
71 static const char *bt_common_color_code_fg_bright_cyan
= "";
72 static const char *bt_common_color_code_fg_bright_light_gray
= "";
73 static const char *bt_common_color_code_bg_default
= "";
74 static const char *bt_common_color_code_bg_red
= "";
75 static const char *bt_common_color_code_bg_green
= "";
76 static const char *bt_common_color_code_bg_yellow
= "";
77 static const char *bt_common_color_code_bg_blue
= "";
78 static const char *bt_common_color_code_bg_magenta
= "";
79 static const char *bt_common_color_code_bg_cyan
= "";
80 static const char *bt_common_color_code_bg_light_gray
= "";
83 * A color codes structure always filled with the proper color codes for the
86 static struct bt_common_color_codes color_codes
;
89 * A color codes structure always filled with empty strings, for when we want no
92 static struct bt_common_color_codes no_color_codes
= {
93 "", "", "", "", "", "", "", "", "", "",
94 "", "", "", "", "", "", "", "", "", "",
99 void __attribute__((constructor
)) bt_common_color_ctor(void)
101 const char *term_env_var
;
102 const char *bright_means_bold_env_var
;
103 bool bright_means_bold
= true;
104 const char *code_fg_bright_red
;
105 const char *code_fg_bright_green
;
106 const char *code_fg_bright_yellow
;
107 const char *code_fg_bright_blue
;
108 const char *code_fg_bright_magenta
;
109 const char *code_fg_bright_cyan
;
110 const char *code_fg_bright_light_gray
;
113 * Check whether or not the terminal supports having
114 * bold foreground colors which do _not_ become bright
115 * colors, that is, the lines
117 * $ echo -e "\033[31mTHIS\n\033[1mTHAT\033[0m"
119 * have the _same_ color, but `THAT` uses a bold font.
121 * This is the case of the kitty terminal emulator.
123 * It's also possible with GNOME Terminal since 3.27.2
124 * and xfce4-terminal since 0.8.7 (and GNOME VTE since
125 * 0.51.2), but it's user-configurable. Since we don't
126 * have this configuration value here, assume it's not
127 * the case to support old versions of GNOME Terminal.
129 * Any user can set the
130 * `BABELTRACE_TERM_COLOR_BRIGHT_MEANS_BOLD` environment
131 * variable to `0` to use the bright foreground color
132 * codes instead of making the normal foreground color
138 * `BABELTRACE_TERM_COLOR_BRIGHT_MEANS_BOLD` is `0`:
139 * Output bright colors using dedicated SGR codes
143 * Output bright colors with bold + SGR codes 30 to
146 term_env_var
= getenv("TERM");
148 if (term_env_var
&& strcmp(term_env_var
, "xterm-kitty") == 0) {
150 * The kitty terminal emulator supports
151 * non-bright bold foreground colors.
153 bright_means_bold
= false;
156 bright_means_bold_env_var
=
157 getenv("BABELTRACE_TERM_COLOR_BRIGHT_MEANS_BOLD");
159 if (bright_means_bold_env_var
) {
161 !(strcmp(bright_means_bold_env_var
, "0") == 0);
164 if (bright_means_bold
) {
165 code_fg_bright_red
= BT_COMMON_COLOR_FG_BOLD_RED
;
166 code_fg_bright_green
= BT_COMMON_COLOR_FG_BOLD_GREEN
;
167 code_fg_bright_yellow
= BT_COMMON_COLOR_FG_BOLD_YELLOW
;
168 code_fg_bright_blue
= BT_COMMON_COLOR_FG_BOLD_BLUE
;
169 code_fg_bright_magenta
= BT_COMMON_COLOR_FG_BOLD_MAGENTA
;
170 code_fg_bright_cyan
= BT_COMMON_COLOR_FG_BOLD_CYAN
;
171 code_fg_bright_light_gray
= BT_COMMON_COLOR_FG_BOLD_LIGHT_GRAY
;
173 code_fg_bright_red
= BT_COMMON_COLOR_FG_BRIGHT_RED
;
174 code_fg_bright_green
= BT_COMMON_COLOR_FG_BRIGHT_GREEN
;
175 code_fg_bright_yellow
= BT_COMMON_COLOR_FG_BRIGHT_YELLOW
;
176 code_fg_bright_blue
= BT_COMMON_COLOR_FG_BRIGHT_BLUE
;
177 code_fg_bright_magenta
= BT_COMMON_COLOR_FG_BRIGHT_MAGENTA
;
178 code_fg_bright_cyan
= BT_COMMON_COLOR_FG_BRIGHT_CYAN
;
179 code_fg_bright_light_gray
= BT_COMMON_COLOR_FG_BRIGHT_LIGHT_GRAY
;
182 if (bt_common_colors_supported()) {
183 bt_common_color_code_reset
= BT_COMMON_COLOR_RESET
;
184 bt_common_color_code_bold
= BT_COMMON_COLOR_BOLD
;
185 bt_common_color_code_fg_default
= BT_COMMON_COLOR_FG_DEFAULT
;
186 bt_common_color_code_fg_red
= BT_COMMON_COLOR_FG_RED
;
187 bt_common_color_code_fg_green
= BT_COMMON_COLOR_FG_GREEN
;
188 bt_common_color_code_fg_yellow
= BT_COMMON_COLOR_FG_YELLOW
;
189 bt_common_color_code_fg_blue
= BT_COMMON_COLOR_FG_BLUE
;
190 bt_common_color_code_fg_magenta
= BT_COMMON_COLOR_FG_MAGENTA
;
191 bt_common_color_code_fg_cyan
= BT_COMMON_COLOR_FG_CYAN
;
192 bt_common_color_code_fg_light_gray
= BT_COMMON_COLOR_FG_LIGHT_GRAY
;
194 bt_common_color_code_fg_bright_red
= code_fg_bright_red
;
195 bt_common_color_code_fg_bright_green
= code_fg_bright_green
;
196 bt_common_color_code_fg_bright_yellow
= code_fg_bright_yellow
;
197 bt_common_color_code_fg_bright_blue
= code_fg_bright_blue
;
198 bt_common_color_code_fg_bright_magenta
= code_fg_bright_magenta
;
199 bt_common_color_code_fg_bright_cyan
= code_fg_bright_cyan
;
200 bt_common_color_code_fg_bright_light_gray
= code_fg_bright_light_gray
;
202 bt_common_color_code_bg_default
= BT_COMMON_COLOR_BG_DEFAULT
;
203 bt_common_color_code_bg_red
= BT_COMMON_COLOR_BG_RED
;
204 bt_common_color_code_bg_green
= BT_COMMON_COLOR_BG_GREEN
;
205 bt_common_color_code_bg_yellow
= BT_COMMON_COLOR_BG_YELLOW
;
206 bt_common_color_code_bg_blue
= BT_COMMON_COLOR_BG_BLUE
;
207 bt_common_color_code_bg_magenta
= BT_COMMON_COLOR_BG_MAGENTA
;
208 bt_common_color_code_bg_cyan
= BT_COMMON_COLOR_BG_CYAN
;
209 bt_common_color_code_bg_light_gray
= BT_COMMON_COLOR_BG_LIGHT_GRAY
;
212 color_codes
.reset
= BT_COMMON_COLOR_RESET
;
213 color_codes
.bold
= BT_COMMON_COLOR_BOLD
;
214 color_codes
.fg_default
= BT_COMMON_COLOR_FG_DEFAULT
;
215 color_codes
.fg_red
= BT_COMMON_COLOR_FG_RED
;
216 color_codes
.fg_green
= BT_COMMON_COLOR_FG_GREEN
;
217 color_codes
.fg_yellow
= BT_COMMON_COLOR_FG_YELLOW
;
218 color_codes
.fg_blue
= BT_COMMON_COLOR_FG_BLUE
;
219 color_codes
.fg_magenta
= BT_COMMON_COLOR_FG_MAGENTA
;
220 color_codes
.fg_cyan
= BT_COMMON_COLOR_FG_CYAN
;
221 color_codes
.fg_light_gray
= BT_COMMON_COLOR_FG_LIGHT_GRAY
;
222 color_codes
.fg_bright_red
= code_fg_bright_red
;
223 color_codes
.fg_bright_green
= code_fg_bright_green
;
224 color_codes
.fg_bright_yellow
= code_fg_bright_yellow
;
225 color_codes
.fg_bright_blue
= code_fg_bright_blue
;
226 color_codes
.fg_bright_magenta
= code_fg_bright_magenta
;
227 color_codes
.fg_bright_cyan
= code_fg_bright_cyan
;
228 color_codes
.fg_bright_light_gray
= code_fg_bright_light_gray
;
229 color_codes
.bg_default
= BT_COMMON_COLOR_BG_DEFAULT
;
230 color_codes
.bg_red
= BT_COMMON_COLOR_BG_RED
;
231 color_codes
.bg_green
= BT_COMMON_COLOR_BG_GREEN
;
232 color_codes
.bg_yellow
= BT_COMMON_COLOR_BG_YELLOW
;
233 color_codes
.bg_blue
= BT_COMMON_COLOR_BG_BLUE
;
234 color_codes
.bg_magenta
= BT_COMMON_COLOR_BG_MAGENTA
;
235 color_codes
.bg_cyan
= BT_COMMON_COLOR_BG_CYAN
;
236 color_codes
.bg_light_gray
= BT_COMMON_COLOR_BG_LIGHT_GRAY
;
240 const char *bt_common_get_system_plugin_path(void)
242 return SYSTEM_PLUGIN_PATH
;
247 bool bt_common_is_setuid_setgid(void)
251 #else /* __MINGW32__ */
253 bool bt_common_is_setuid_setgid(void)
255 return (geteuid() != getuid() || getegid() != getgid());
257 #endif /* __MINGW32__ */
261 const char *bt_get_home_dir(int log_level
)
263 return g_get_home_dir();
265 #else /* __MINGW32__ */
267 char *bt_secure_getenv(const char *name
, int log_level
)
269 if (bt_common_is_setuid_setgid()) {
270 BT_LOGD("Disregarding environment variable for setuid/setgid binary: "
271 "name=\"%s\"", name
);
278 const char *bt_get_home_dir(int log_level
)
283 val
= bt_secure_getenv(HOME_ENV_VAR
, log_level
);
287 /* Fallback on password file. */
288 pwd
= getpwuid(getuid());
296 #endif /* __MINGW32__ */
299 char *bt_common_get_home_plugin_path(int log_level
)
302 const char *home_dir
;
305 home_dir
= bt_get_home_dir(log_level
);
310 length
= strlen(home_dir
) + strlen(HOME_PLUGIN_SUBPATH
) + 1;
312 if (length
>= PATH_MAX
) {
313 BT_LOGW("Home directory path is too long: "
314 "length=%zu, max-length=%u", length
, PATH_MAX
);
318 path
= malloc(PATH_MAX
);
323 strcpy(path
, home_dir
);
324 strcat(path
, HOME_PLUGIN_SUBPATH
);
331 int bt_common_append_plugin_path_dirs(const char *paths
, GPtrArray
*dirs
)
336 size_t init_dirs_len
;
339 init_dirs_len
= dirs
->len
;
342 /* Nothing to append */
347 end
= paths
+ strlen(paths
);
351 const char *next_sep
;
353 next_sep
= strchr(at
, G_SEARCHPATH_SEPARATOR
);
354 if (next_sep
== at
) {
356 * Empty path: try next character (supported
357 * to conform to the typical parsing of $PATH).
361 } else if (!next_sep
) {
362 /* No more separator: use the remaining */
363 next_sep
= paths
+ strlen(paths
);
366 path
= g_string_new(NULL
);
371 g_string_append_len(path
, at
, next_sep
- at
);
373 g_ptr_array_add(dirs
, path
);
381 /* Remove the new entries in dirs */
382 while (dirs
->len
> init_dirs_len
) {
383 g_ptr_array_remove_index(dirs
, init_dirs_len
);
391 bool isarealtty(int fd
)
394 struct stat tty_stats
;
401 if (fstat(fd
, &tty_stats
) == 0) {
402 if (!S_ISCHR(tty_stats
.st_mode
)) {
403 /* Not a character device: not a TTY */
415 bool bt_common_colors_supported(void)
417 static bool supports_colors
= false;
418 static bool supports_colors_set
= false;
419 const char *term_env_var
;
420 const char *term_color_env_var
;
422 if (supports_colors_set
) {
426 supports_colors_set
= true;
429 * `BABELTRACE_TERM_COLOR` environment variable always overrides
430 * the automatic color support detection.
432 term_color_env_var
= getenv("BABELTRACE_TERM_COLOR");
433 if (term_color_env_var
) {
434 if (g_ascii_strcasecmp(term_color_env_var
, "always") == 0) {
436 supports_colors
= true;
437 } else if (g_ascii_strcasecmp(term_color_env_var
, "never") == 0) {
438 /* Force no colors */
443 /* We need a compatible, known terminal */
444 term_env_var
= getenv("TERM");
449 if (strncmp(term_env_var
, "xterm", 5) != 0 &&
450 strncmp(term_env_var
, "rxvt", 4) != 0 &&
451 strncmp(term_env_var
, "konsole", 7) != 0 &&
452 strncmp(term_env_var
, "gnome", 5) != 0 &&
453 strncmp(term_env_var
, "screen", 5) != 0 &&
454 strncmp(term_env_var
, "tmux", 4) != 0 &&
455 strncmp(term_env_var
, "putty", 5) != 0) {
459 /* Both standard output and error streams need to be TTYs */
460 if (!isarealtty(STDOUT_FILENO
) || !isarealtty(STDERR_FILENO
)) {
464 supports_colors
= true;
467 return supports_colors
;
471 const char *bt_common_color_reset(void)
473 return bt_common_color_code_reset
;
477 const char *bt_common_color_bold(void)
479 return bt_common_color_code_bold
;
483 const char *bt_common_color_fg_default(void)
485 return bt_common_color_code_fg_default
;
489 const char *bt_common_color_fg_red(void)
491 return bt_common_color_code_fg_red
;
495 const char *bt_common_color_fg_green(void)
497 return bt_common_color_code_fg_green
;
501 const char *bt_common_color_fg_yellow(void)
503 return bt_common_color_code_fg_yellow
;
507 const char *bt_common_color_fg_blue(void)
509 return bt_common_color_code_fg_blue
;
513 const char *bt_common_color_fg_magenta(void)
515 return bt_common_color_code_fg_magenta
;
519 const char *bt_common_color_fg_cyan(void)
521 return bt_common_color_code_fg_cyan
;
525 const char *bt_common_color_fg_light_gray(void)
527 return bt_common_color_code_fg_light_gray
;
531 const char *bt_common_color_fg_bright_red(void)
533 return bt_common_color_code_fg_bright_red
;
537 const char *bt_common_color_fg_bright_green(void)
539 return bt_common_color_code_fg_bright_green
;
543 const char *bt_common_color_fg_bright_yellow(void)
545 return bt_common_color_code_fg_bright_yellow
;
549 const char *bt_common_color_fg_bright_blue(void)
551 return bt_common_color_code_fg_bright_blue
;
555 const char *bt_common_color_fg_bright_magenta(void)
557 return bt_common_color_code_fg_bright_magenta
;
561 const char *bt_common_color_fg_bright_cyan(void)
563 return bt_common_color_code_fg_bright_cyan
;
567 const char *bt_common_color_fg_bright_light_gray(void)
569 return bt_common_color_code_fg_bright_light_gray
;
573 const char *bt_common_color_bg_default(void)
575 return bt_common_color_code_bg_default
;
579 const char *bt_common_color_bg_red(void)
581 return bt_common_color_code_bg_red
;
585 const char *bt_common_color_bg_green(void)
587 return bt_common_color_code_bg_green
;
591 const char *bt_common_color_bg_yellow(void)
593 return bt_common_color_code_bg_yellow
;
597 const char *bt_common_color_bg_blue(void)
599 return bt_common_color_code_bg_blue
;
603 const char *bt_common_color_bg_magenta(void)
605 return bt_common_color_code_bg_magenta
;
609 const char *bt_common_color_bg_cyan(void)
611 return bt_common_color_code_bg_cyan
;
615 const char *bt_common_color_bg_light_gray(void)
617 return bt_common_color_code_bg_light_gray
;
621 void bt_common_color_get_codes(struct bt_common_color_codes
*codes
,
622 enum bt_common_color_when use_colors
)
624 if (use_colors
== BT_COMMON_COLOR_WHEN_ALWAYS
) {
625 *codes
= color_codes
;
626 } else if (use_colors
== BT_COMMON_COLOR_WHEN_NEVER
) {
627 *codes
= no_color_codes
;
629 BT_ASSERT(use_colors
== BT_COMMON_COLOR_WHEN_AUTO
);
631 if (bt_common_colors_supported()) {
632 *codes
= color_codes
;
634 *codes
= no_color_codes
;
640 GString
*bt_common_string_until(const char *input
, const char *escapable_chars
,
641 const char *end_chars
, size_t *end_pos
)
643 GString
*output
= g_string_new(NULL
);
646 const char *end_char
;
652 for (ch
= input
; *ch
!= '\0'; ch
++) {
654 bool continue_loop
= false;
657 /* `\` at the end of the string: append `\` */
658 g_string_append_c(output
, *ch
);
663 for (es_char
= escapable_chars
; *es_char
!= '\0'; es_char
++) {
664 if (ch
[1] == *es_char
) {
666 * `\` followed by an escapable
667 * character: append the escaped
670 g_string_append_c(output
, ch
[1]);
672 continue_loop
= true;
682 * `\` followed by a non-escapable character:
683 * append `\` and the character.
685 g_string_append_c(output
, *ch
);
686 g_string_append_c(output
, ch
[1]);
690 for (end_char
= end_chars
; *end_char
!= '\0'; end_char
++) {
691 if (*ch
== *end_char
) {
693 * End character found:
694 * terminate this loop.
700 /* Normal character: append */
701 g_string_append_c(output
, *ch
);
707 *end_pos
= ch
- input
;
714 g_string_free(output
, TRUE
);
723 GString
*bt_common_shell_quote(const char *input
, bool with_single_quotes
)
725 GString
*output
= g_string_new(NULL
);
727 bool no_quote
= true;
733 if (strlen(input
) == 0) {
734 if (with_single_quotes
) {
735 g_string_assign(output
, "''");
741 for (ch
= input
; *ch
!= '\0'; ch
++) {
744 if (!g_ascii_isalpha(c
) && !g_ascii_isdigit(c
) && c
!= '_' &&
745 c
!= '@' && c
!= '%' && c
!= '+' && c
!= '=' &&
746 c
!= ':' && c
!= ',' && c
!= '.' && c
!= '/' &&
754 g_string_assign(output
, input
);
758 if (with_single_quotes
) {
759 g_string_assign(output
, "'");
762 for (ch
= input
; *ch
!= '\0'; ch
++) {
764 g_string_append(output
, "'\"'\"'");
766 g_string_append_c(output
, *ch
);
770 if (with_single_quotes
) {
771 g_string_append_c(output
, '\'');
779 bool bt_common_string_is_printable(const char *input
)
782 bool printable
= true;
783 BT_ASSERT_DBG(input
);
785 for (ch
= input
; *ch
!= '\0'; ch
++) {
786 if (!isprint((unsigned char) *ch
) && *ch
!= '\n' && *ch
!= '\r' &&
787 *ch
!= '\t' && *ch
!= '\v') {
798 void bt_common_destroy_lttng_live_url_parts(
799 struct bt_common_lttng_live_url_parts
*parts
)
806 g_string_free(parts
->proto
, TRUE
);
810 if (parts
->hostname
) {
811 g_string_free(parts
->hostname
, TRUE
);
812 parts
->hostname
= NULL
;
815 if (parts
->target_hostname
) {
816 g_string_free(parts
->target_hostname
, TRUE
);
817 parts
->target_hostname
= NULL
;
820 if (parts
->session_name
) {
821 g_string_free(parts
->session_name
, TRUE
);
822 parts
->session_name
= NULL
;
830 struct bt_common_lttng_live_url_parts
bt_common_parse_lttng_live_url(
831 const char *url
, char *error_buf
, size_t error_buf_size
)
833 struct bt_common_lttng_live_url_parts parts
;
834 const char *at
= url
;
838 memset(&parts
, 0, sizeof(parts
));
842 parts
.proto
= bt_common_string_until(at
, "", ":", &end_pos
);
843 if (!parts
.proto
|| parts
.proto
->len
== 0) {
845 snprintf(error_buf
, error_buf_size
, "Missing protocol");
851 if (strcmp(parts
.proto
->str
, "net") == 0) {
852 g_string_assign(parts
.proto
, "net4");
855 if (strcmp(parts
.proto
->str
, "net4") != 0 &&
856 strcmp(parts
.proto
->str
, "net6") != 0) {
858 snprintf(error_buf
, error_buf_size
,
859 "Unknown protocol: `%s`", parts
.proto
->str
);
865 if (at
[end_pos
] != ':') {
867 snprintf(error_buf
, error_buf_size
,
868 "Expecting `:` after `%s`", parts
.proto
->str
);
877 if (strncmp(at
, "://", 3) != 0) {
879 snprintf(error_buf
, error_buf_size
,
880 "Expecting `://` after protocol");
890 parts
.hostname
= bt_common_string_until(at
, "", ":/", &end_pos
);
891 if (!parts
.hostname
|| parts
.hostname
->len
== 0) {
893 snprintf(error_buf
, error_buf_size
, "Missing hostname");
899 if (at
[end_pos
] == ':') {
904 port
= bt_common_string_until(at
, "", "/", &end_pos
);
905 if (!port
|| port
->len
== 0) {
907 snprintf(error_buf
, error_buf_size
, "Missing port");
913 if (sscanf(port
->str
, "%d", &parts
.port
) != 1) {
915 snprintf(error_buf
, error_buf_size
,
916 "Invalid port: `%s`", port
->str
);
919 g_string_free(port
, TRUE
);
923 g_string_free(port
, TRUE
);
925 if (parts
.port
< 0 || parts
.port
>= 65536) {
927 snprintf(error_buf
, error_buf_size
,
928 "Invalid port: %d", parts
.port
);
935 if (at
[end_pos
] == '\0') {
936 /* Relay daemon hostname and ports provided only */
943 if (strncmp(at
, "/host/", 6) != 0) {
945 snprintf(error_buf
, error_buf_size
,
946 "Expecting `/host/` after hostname or port");
954 /* Target hostname */
955 parts
.target_hostname
= bt_common_string_until(at
, "", "/", &end_pos
);
956 if (!parts
.target_hostname
|| parts
.target_hostname
->len
== 0) {
958 snprintf(error_buf
, error_buf_size
,
959 "Missing target hostname");
965 if (at
[end_pos
] == '\0') {
967 snprintf(error_buf
, error_buf_size
,
968 "Missing `/` after target hostname (`%s`)",
969 parts
.target_hostname
->str
);
979 parts
.session_name
= bt_common_string_until(at
, "", "/", &end_pos
);
980 if (!parts
.session_name
|| parts
.session_name
->len
== 0) {
982 snprintf(error_buf
, error_buf_size
,
983 "Missing session name");
989 if (at
[end_pos
] == '/') {
991 snprintf(error_buf
, error_buf_size
,
992 "Unexpected `/` after session name (`%s`)",
993 parts
.session_name
->str
);
1002 bt_common_destroy_lttng_live_url_parts(&parts
);
1009 void bt_common_normalize_star_glob_pattern(char *pattern
)
1013 bool got_star
= false;
1017 for (p
= pattern
, np
= pattern
; *p
!= '\0'; p
++) {
1021 /* Avoid consecutive stars. */
1028 /* Copy backslash character. */
1043 /* Copy single character. */
1053 bool at_end_of_pattern(const char *p
, const char *pattern
, size_t pattern_len
)
1055 return (p
- pattern
) == pattern_len
|| *p
== '\0';
1059 * Globbing matching function with the star feature only (`?` and
1060 * character sets are not supported). This matches `candidate` (plain
1061 * string) against `pattern`. A literal star can be escaped with `\` in
1064 * `pattern_len` or `candidate_len` can be greater than the actual
1065 * string length of `pattern` or `candidate` if the string is
1069 bool bt_common_star_glob_match(const char *pattern
, size_t pattern_len
,
1070 const char *candidate
, size_t candidate_len
) {
1071 const char *retry_c
= candidate
, *retry_p
= pattern
, *c
, *p
;
1072 bool got_a_star
= false;
1079 * The concept here is to retry a match in the specific case
1080 * where we already got a star. The retry position for the
1081 * pattern is just after the most recent star, and the retry
1082 * position for the candidate is the character following the
1083 * last try's first character.
1087 * candidate: hi ev every onyx one
1089 * pattern: hi*every*one
1092 * candidate: hi ev every onyx one
1094 * pattern: hi*every*one
1097 * candidate: hi ev every onyx one
1099 * pattern: hi*every*one
1102 * candidate: hi ev every onyx one
1104 * pattern: hi*every*one
1107 * candidate: hi ev every onyx one
1109 * pattern: hi*every*one
1112 * candidate: hi ev every onyx one
1114 * pattern: hi*every*one
1117 * candidate: hi ev every onyx one
1119 * pattern: hi*every*one
1122 * candidate: hi ev every onyx one
1124 * pattern: hi*every*one
1127 * candidate: hi ev every onyx one
1129 * pattern: hi*every*one
1132 * candidate: hi ev every onyx one
1134 * pattern: hi*every*one
1137 * candidate: hi ev every onyx one
1139 * pattern: hi*every*one
1142 * candidate: hi ev every onyx one
1144 * pattern: hi*every*one
1147 * candidate: hi ev every onyx one
1149 * pattern: hi*every*one
1152 * candidate: hi ev every onyx one
1154 * pattern: hi*every*one
1157 * candidate: hi ev every onyx one
1159 * pattern: hi*every*one
1162 * candidate: hi ev every onyx one
1164 * pattern: hi*every*one
1167 * candidate: hi ev every onyx one
1169 * pattern: hi*every*one
1172 * candidate: hi ev every onyx one
1174 * pattern: hi*every*one
1177 * candidate: hi ev every onyx one
1179 * pattern: hi*every*one
1182 * candidate: hi ev every onyx one
1184 * pattern: hi*every*one
1187 * candidate: hi ev every onyx one
1189 * pattern: hi*every*one
1192 * candidate: hi ev every onyx one
1194 * pattern: hi*every*one
1197 * candidate: hi ev every onyx one
1199 * pattern: hi*every*one
1202 * candidate: hi ev every onyx one
1204 * pattern: hi*every*one
1207 * candidate: hi ev every onyx one
1209 * pattern: hi*every*one
1212 * candidate: hi ev every onyx one
1214 * pattern: hi*every*one
1217 * candidate: hi ev every onyx one
1219 * pattern: hi*every*one
1222 while ((c
- candidate
) < candidate_len
&& *c
!= '\0') {
1225 if (at_end_of_pattern(p
, pattern
, pattern_len
)) {
1226 goto end_of_pattern
;
1234 * Our first try starts at the current candidate
1235 * character and after the star in the pattern.
1240 if (at_end_of_pattern(retry_p
, pattern
, pattern_len
)) {
1242 * Star at the end of the pattern at
1243 * this point: automatic match.
1250 /* Go to escaped character. */
1254 * Fall through the default case which compares
1255 * the escaped character now.
1259 if (at_end_of_pattern(p
, pattern
, pattern_len
) ||
1262 /* Character mismatch OR end of pattern. */
1265 * We didn't get any star yet,
1266 * so this first mismatch
1267 * automatically makes the whole
1274 * Next try: next candidate character,
1275 * original pattern character (following
1276 * the most recent star).
1284 /* Next pattern and candidate characters. */
1290 * We checked every candidate character and we're still in a
1291 * success state: the only pattern character allowed to remain
1294 if (at_end_of_pattern(p
, pattern
, pattern_len
)) {
1299 return p
[-1] == '*' && at_end_of_pattern(p
, pattern
, pattern_len
);
1304 GString
*bt_common_normalize_path(const char *path
, const char *wd
)
1307 GString
*norm_path
= NULL
;
1311 tmp
= _fullpath(NULL
, path
, PATH_MAX
);
1316 norm_path
= g_string_new(tmp
);
1324 g_string_free(norm_path
, TRUE
);
1333 void append_path_parts(const char *path
, GPtrArray
*parts
)
1335 const char *ch
= path
;
1336 const char *last
= path
;
1339 if (*ch
== G_DIR_SEPARATOR
|| *ch
== '\0') {
1340 if (ch
- last
> 0) {
1341 GString
*part
= g_string_new(NULL
);
1344 g_string_append_len(part
, last
, ch
- last
);
1345 g_ptr_array_add(parts
, part
);
1360 void destroy_gstring(void *gstring
)
1362 (void) g_string_free(gstring
, TRUE
);
1366 GString
*bt_common_normalize_path(const char *path
, const char *wd
)
1370 GPtrArray
*parts
= NULL
;
1373 norm_path
= g_string_new(G_DIR_SEPARATOR_S
);
1378 parts
= g_ptr_array_new_with_free_func(destroy_gstring
);
1383 if (path
[0] != G_DIR_SEPARATOR
) {
1384 /* Relative path: start with working directory */
1386 append_path_parts(wd
, parts
);
1388 gchar
*cd
= g_get_current_dir();
1390 append_path_parts(cd
, parts
);
1395 /* Append parts of the path parameter */
1396 append_path_parts(path
, parts
);
1398 /* Resolve special `..` and `.` parts */
1399 for (i
= 0; i
< parts
->len
; i
++) {
1400 GString
*part
= g_ptr_array_index(parts
, i
);
1402 if (strcmp(part
->str
, "..") == 0) {
1405 * First part of absolute path is `..`:
1411 /* Remove `..` and previous part */
1412 g_ptr_array_remove_index(parts
, i
- 1);
1413 g_ptr_array_remove_index(parts
, i
- 1);
1415 } else if (strcmp(part
->str
, ".") == 0) {
1417 g_ptr_array_remove_index(parts
, i
);
1422 /* Create normalized path with what's left */
1423 for (i
= 0; i
< parts
->len
; i
++) {
1424 GString
*part
= g_ptr_array_index(parts
, i
);
1426 g_string_append(norm_path
, part
->str
);
1428 if (i
< parts
->len
- 1) {
1429 g_string_append_c(norm_path
, G_DIR_SEPARATOR
);
1437 g_string_free(norm_path
, TRUE
);
1443 g_ptr_array_free(parts
, TRUE
);
1451 size_t bt_common_get_page_size(int log_level
)
1455 page_size
= bt_sysconf(_SC_PAGESIZE
);
1456 if (page_size
< 0) {
1457 BT_LOGF("Cannot get system's page size: ret=%d",
1465 #define BUF_STD_APPEND(...) \
1467 char _tmp_fmt[64]; \
1469 size_t _size = buf_size - (size_t) (*buf_ch - buf); \
1470 size_t _tmp_fmt_size = (size_t) (fmt_ch - *out_fmt_ch); \
1471 strncpy(_tmp_fmt, *out_fmt_ch, _tmp_fmt_size); \
1472 _tmp_fmt[_tmp_fmt_size] = '\0'; \
1473 _count = snprintf(*buf_ch, _size, _tmp_fmt, __VA_ARGS__); \
1474 BT_ASSERT_DBG(_count >= 0); \
1475 *buf_ch += MIN(_count, _size); \
1478 #define BUF_STD_APPEND_SINGLE_ARG(_type) \
1480 _type _arg = va_arg(*args, _type); \
1481 BUF_STD_APPEND(_arg); \
1484 static inline void handle_conversion_specifier_std(char *buf
, char **buf_ch
,
1485 size_t buf_size
, const char **out_fmt_ch
, va_list *args
)
1487 const char *fmt_ch
= *out_fmt_ch
;
1488 enum LENGTH_MODIFIER
{
1496 } length_mod
= LENGTH_MOD_NONE
;
1501 if (*fmt_ch
== '%') {
1527 if (*fmt_ch
< '0' || *fmt_ch
> '9') {
1535 if (*fmt_ch
== '.') {
1539 if (*fmt_ch
< '0' || *fmt_ch
> '9') {
1547 /* format (PRI*64) */
1548 if (strncmp(fmt_ch
, PRId64
, sizeof(PRId64
) - 1) == 0) {
1549 fmt_ch
+= sizeof(PRId64
) - 1;
1550 BUF_STD_APPEND_SINGLE_ARG(int64_t);
1552 } else if (strncmp(fmt_ch
, PRIu64
, sizeof(PRIu64
) - 1) == 0) {
1553 fmt_ch
+= sizeof(PRIu64
) - 1;
1554 BUF_STD_APPEND_SINGLE_ARG(uint64_t);
1556 } else if (strncmp(fmt_ch
, PRIx64
, sizeof(PRIx64
) - 1) == 0) {
1557 fmt_ch
+= sizeof(PRIx64
) - 1;
1558 BUF_STD_APPEND_SINGLE_ARG(uint64_t);
1560 } else if (strncmp(fmt_ch
, PRIX64
, sizeof(PRIX64
) - 1) == 0) {
1561 fmt_ch
+= sizeof(PRIX64
) - 1;
1562 BUF_STD_APPEND_SINGLE_ARG(uint64_t);
1564 } else if (strncmp(fmt_ch
, PRIo64
, sizeof(PRIo64
) - 1) == 0) {
1565 fmt_ch
+= sizeof(PRIo64
) - 1;
1566 BUF_STD_APPEND_SINGLE_ARG(uint64_t);
1568 } else if (strncmp(fmt_ch
, PRIi64
, sizeof(PRIi64
) - 1) == 0) {
1569 fmt_ch
+= sizeof(PRIi64
) - 1;
1570 BUF_STD_APPEND_SINGLE_ARG(int64_t);
1577 length_mod
= LENGTH_MOD_H
;
1580 if (*fmt_ch
== 'h') {
1581 length_mod
= LENGTH_MOD_HH
;
1587 length_mod
= LENGTH_MOD_LOW_L
;
1590 if (*fmt_ch
== 'l') {
1591 length_mod
= LENGTH_MOD_LOW_LL
;
1597 length_mod
= LENGTH_MOD_UP_L
;
1601 length_mod
= LENGTH_MOD_Z
;
1614 switch (length_mod
) {
1615 case LENGTH_MOD_NONE
:
1616 case LENGTH_MOD_LOW_L
:
1617 BUF_STD_APPEND_SINGLE_ARG(int);
1627 switch (length_mod
) {
1628 case LENGTH_MOD_NONE
:
1629 BUF_STD_APPEND_SINGLE_ARG(char *);
1631 case LENGTH_MOD_LOW_L
:
1632 BUF_STD_APPEND_SINGLE_ARG(wchar_t *);
1642 switch (length_mod
) {
1643 case LENGTH_MOD_NONE
:
1646 BUF_STD_APPEND_SINGLE_ARG(int);
1648 case LENGTH_MOD_LOW_L
:
1649 BUF_STD_APPEND_SINGLE_ARG(long);
1651 case LENGTH_MOD_LOW_LL
:
1652 BUF_STD_APPEND_SINGLE_ARG(long long);
1655 BUF_STD_APPEND_SINGLE_ARG(size_t);
1667 switch (length_mod
) {
1668 case LENGTH_MOD_NONE
:
1671 BUF_STD_APPEND_SINGLE_ARG(unsigned int);
1673 case LENGTH_MOD_LOW_L
:
1674 BUF_STD_APPEND_SINGLE_ARG(unsigned long);
1676 case LENGTH_MOD_LOW_LL
:
1677 BUF_STD_APPEND_SINGLE_ARG(unsigned long long);
1680 BUF_STD_APPEND_SINGLE_ARG(size_t);
1694 switch (length_mod
) {
1695 case LENGTH_MOD_NONE
:
1696 BUF_STD_APPEND_SINGLE_ARG(double);
1698 case LENGTH_MOD_UP_L
:
1699 BUF_STD_APPEND_SINGLE_ARG(long double);
1708 if (length_mod
== LENGTH_MOD_NONE
) {
1709 BUF_STD_APPEND_SINGLE_ARG(void *);
1719 *out_fmt_ch
= fmt_ch
;
1723 void bt_common_custom_vsnprintf(char *buf
, size_t buf_size
,
1725 bt_common_handle_custom_specifier_func handle_specifier
,
1726 void *priv_data
, const char *fmt
, va_list *args
)
1728 const char *fmt_ch
= fmt
;
1734 while (*fmt_ch
!= '\0') {
1737 BT_ASSERT_DBG(fmt_ch
[1] != '\0');
1739 if (fmt_ch
[1] == intro
) {
1740 handle_specifier(priv_data
, &buf_ch
,
1741 buf_size
- (size_t) (buf_ch
- buf
),
1744 handle_conversion_specifier_std(buf
, &buf_ch
,
1745 buf_size
, &fmt_ch
, args
);
1748 if (buf_ch
>= buf
+ buf_size
- 1) {
1755 if (buf_ch
>= buf
+ buf_size
- 1) {
1767 void bt_common_custom_snprintf(char *buf
, size_t buf_size
,
1769 bt_common_handle_custom_specifier_func handle_specifier
,
1770 void *priv_data
, const char *fmt
, ...)
1773 va_start(args
, fmt
);
1774 bt_common_custom_vsnprintf(buf
, buf_size
, intro
, handle_specifier
,
1775 priv_data
, fmt
, &args
);
1780 void bt_common_sep_digits(char *str
, unsigned int digits_per_group
, char sep
)
1789 BT_ASSERT_DBG(digits_per_group
> 0);
1790 BT_ASSERT_DBG(sep
!= '\0');
1792 /* Compute new length of `str` */
1793 orig_len
= strlen(str
);
1794 BT_ASSERT_DBG(orig_len
> 0);
1795 sep_count
= (orig_len
- 1) / digits_per_group
;
1796 new_len
= strlen(str
) + sep_count
;
1799 * Do the work in place. Have the reading pointer `rd` start at
1800 * the end of the original string, and the writing pointer `wr`
1801 * start at the end of the new string, making sure to also put a
1802 * null character there.
1804 rd
= str
+ orig_len
- 1;
1810 * Here's what the process looks like (3 digits per group):
1814 * Destination: 12345678#8
1819 * Destination: 1234567878
1824 * Destination: 1234567678
1829 * Destination: 123456,678
1834 * Destination: 123455,678
1839 * Destination: 123445,678
1844 * Destination: 123345,678
1849 * Destination: 12,345,678
1854 * Destination: 12,345,678
1859 * Destination: 12,345,678
1862 while (rd
!= str
- 1) {
1863 if (i
== digits_per_group
) {
1865 * Time to append the separator: decrement `wr`,
1866 * but keep `rd` as is.
1874 /* Copy read-side character to write-side character */
1883 GString
*bt_common_fold(const char *str
, unsigned int total_length
,
1884 unsigned int indent
)
1886 const unsigned int content_length
= total_length
- indent
;
1887 GString
*folded
= g_string_new(NULL
);
1888 GString
*tmp_line
= g_string_new(NULL
);
1889 gchar
**lines
= NULL
;
1890 gchar
**line_words
= NULL
;
1891 gchar
* const *line
;
1895 BT_ASSERT_DBG(indent
< total_length
);
1896 BT_ASSERT_DBG(tmp_line
);
1897 BT_ASSERT_DBG(folded
);
1899 if (strlen(str
) == 0) {
1900 /* Empty input string: empty output string */
1905 lines
= g_strsplit(str
, "\n", 0);
1906 BT_ASSERT_DBG(lines
);
1908 /* For each source line */
1909 for (line
= lines
; *line
; line
++) {
1910 gchar
* const *word
;
1913 * Append empty line without indenting if source line is
1916 if (strlen(*line
) == 0) {
1917 g_string_append_c(folded
, '\n');
1922 line_words
= g_strsplit(*line
, " ", 0);
1923 BT_ASSERT_DBG(line_words
);
1926 * Indent for first line (we know there's at least one
1927 * word at this point).
1929 for (i
= 0; i
< indent
; i
++) {
1930 g_string_append_c(folded
, ' ');
1933 /* Append words, folding when necessary */
1934 g_string_assign(tmp_line
, "");
1936 for (word
= line_words
; *word
; word
++) {
1938 * `tmp_line->len > 0` is in the condition so
1939 * that words that are larger than
1940 * `content_length` are at least written on
1943 * `tmp_line->len - 1` because the temporary
1944 * line always contains a trailing space which
1945 * won't be part of the line if we fold.
1947 if (tmp_line
->len
> 0 &&
1948 tmp_line
->len
- 1 + strlen(*word
) >= content_length
) {
1949 /* Fold (without trailing space) */
1950 g_string_append_len(folded
,
1951 tmp_line
->str
, tmp_line
->len
- 1);
1952 g_string_append_c(folded
, '\n');
1954 /* Indent new line */
1955 for (i
= 0; i
< indent
; i
++) {
1956 g_string_append_c(folded
, ' ');
1959 g_string_assign(tmp_line
, "");
1962 /* Append current word and space to temporary line */
1963 g_string_append(tmp_line
, *word
);
1964 g_string_append_c(tmp_line
, ' ');
1967 /* Append last line if any, without trailing space */
1968 if (tmp_line
->len
> 0) {
1969 g_string_append_len(folded
, tmp_line
->str
,
1973 /* Append source newline */
1974 g_string_append_c(folded
, '\n');
1976 /* Free array of this line's words */
1977 g_strfreev(line_words
);
1981 /* Remove trailing newline if any */
1982 if (folded
->str
[folded
->len
- 1] == '\n') {
1983 g_string_truncate(folded
, folded
->len
- 1);
1991 BT_ASSERT_DBG(!line_words
);
1994 g_string_free(tmp_line
, TRUE
);
2002 int bt_common_get_term_size(unsigned int *width
, unsigned int *height
)
2004 /* Not supported on Windows yet */
2007 #else /* __MINGW32__ */
2009 int bt_common_get_term_size(unsigned int *width
, unsigned int *height
)
2012 struct winsize winsize
;
2014 if (ioctl(STDOUT_FILENO
, TIOCGWINSZ
, &winsize
) < 0) {
2020 *width
= (unsigned int) winsize
.ws_col
;
2024 *height
= (unsigned int) winsize
.ws_row
;
2030 #endif /* __MINGW32__ */
2033 int bt_common_g_string_append_printf(GString
*str
, const char *fmt
, ...)
2036 gsize len
, allocated_len
, available_len
;
2039 /* str->len excludes \0. */
2041 /* Explicitly exclude \0. */
2042 allocated_len
= str
->allocated_len
- 1;
2043 available_len
= allocated_len
- len
;
2045 str
->len
= allocated_len
;
2047 print_len
= vsnprintf(str
->str
+ len
, available_len
+ 1, fmt
, ap
);
2049 if (print_len
< 0) {
2052 if (G_UNLIKELY(available_len
< print_len
)) {
2054 g_string_set_size(str
, len
+ print_len
);
2056 print_len
= vsprintf(str
->str
+ len
, fmt
, ap
);
2059 str
->len
= len
+ print_len
;
2065 int bt_common_append_file_content_to_g_string(GString
*str
, FILE *fp
)
2067 const size_t chunk_size
= 4096;
2071 gsize orig_len
= str
->len
;
2075 buf
= g_malloc(chunk_size
);
2091 read_len
= fread(buf
, 1, chunk_size
, fp
);
2092 g_string_append_len(str
, buf
, read_len
);
2097 /* Remove what was appended */
2098 g_string_truncate(str
, orig_len
);
2106 void bt_common_abort(void)
2108 static const char * const exec_on_abort_env_name
=
2109 "BABELTRACE_EXEC_ON_ABORT";
2110 const char *env_exec_on_abort
;
2112 env_exec_on_abort
= getenv(exec_on_abort_env_name
);
2113 if (env_exec_on_abort
) {
2114 if (bt_common_is_setuid_setgid()) {
2118 (void) g_spawn_command_line_sync(env_exec_on_abort
,
2119 NULL
, NULL
, NULL
, NULL
);