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
27 #include <sys/types.h>
33 #include <babeltrace/babeltrace-internal.h>
34 #include <babeltrace/common-internal.h>
36 #define SYSTEM_PLUGIN_PATH INSTALL_LIBDIR "/babeltrace/plugins"
37 #define HOME_ENV_VAR "HOME"
38 #define HOME_PLUGIN_SUBPATH "/.local/lib/babeltrace/plugins"
40 static const char *bt_common_color_code_reset
= "";
41 static const char *bt_common_color_code_bold
= "";
42 static const char *bt_common_color_code_fg_default
= "";
43 static const char *bt_common_color_code_fg_red
= "";
44 static const char *bt_common_color_code_fg_green
= "";
45 static const char *bt_common_color_code_fg_yellow
= "";
46 static const char *bt_common_color_code_fg_blue
= "";
47 static const char *bt_common_color_code_fg_magenta
= "";
48 static const char *bt_common_color_code_fg_cyan
= "";
49 static const char *bt_common_color_code_fg_light_gray
= "";
50 static const char *bt_common_color_code_bg_default
= "";
51 static const char *bt_common_color_code_bg_red
= "";
52 static const char *bt_common_color_code_bg_green
= "";
53 static const char *bt_common_color_code_bg_yellow
= "";
54 static const char *bt_common_color_code_bg_blue
= "";
55 static const char *bt_common_color_code_bg_magenta
= "";
56 static const char *bt_common_color_code_bg_cyan
= "";
57 static const char *bt_common_color_code_bg_light_gray
= "";
60 void __attribute__((constructor
)) bt_common_color_ctor(void)
62 if (bt_common_colors_supported()) {
63 bt_common_color_code_reset
= BT_COMMON_COLOR_RESET
;
64 bt_common_color_code_bold
= BT_COMMON_COLOR_BOLD
;
65 bt_common_color_code_fg_default
= BT_COMMON_COLOR_FG_DEFAULT
;
66 bt_common_color_code_fg_red
= BT_COMMON_COLOR_FG_RED
;
67 bt_common_color_code_fg_green
= BT_COMMON_COLOR_FG_GREEN
;
68 bt_common_color_code_fg_yellow
= BT_COMMON_COLOR_FG_YELLOW
;
69 bt_common_color_code_fg_blue
= BT_COMMON_COLOR_FG_BLUE
;
70 bt_common_color_code_fg_magenta
= BT_COMMON_COLOR_FG_MAGENTA
;
71 bt_common_color_code_fg_cyan
= BT_COMMON_COLOR_FG_CYAN
;
72 bt_common_color_code_fg_light_gray
= BT_COMMON_COLOR_FG_LIGHT_GRAY
;
73 bt_common_color_code_bg_default
= BT_COMMON_COLOR_BG_DEFAULT
;
74 bt_common_color_code_bg_red
= BT_COMMON_COLOR_BG_RED
;
75 bt_common_color_code_bg_green
= BT_COMMON_COLOR_BG_GREEN
;
76 bt_common_color_code_bg_yellow
= BT_COMMON_COLOR_BG_YELLOW
;
77 bt_common_color_code_bg_blue
= BT_COMMON_COLOR_BG_BLUE
;
78 bt_common_color_code_bg_magenta
= BT_COMMON_COLOR_BG_MAGENTA
;
79 bt_common_color_code_bg_cyan
= BT_COMMON_COLOR_BG_CYAN
;
80 bt_common_color_code_bg_light_gray
= BT_COMMON_COLOR_BG_LIGHT_GRAY
;
85 const char *bt_common_get_system_plugin_path(void)
87 return SYSTEM_PLUGIN_PATH
;
91 bool bt_common_is_setuid_setgid(void)
93 return (geteuid() != getuid() || getegid() != getgid());
96 static char *bt_secure_getenv(const char *name
)
98 if (bt_common_is_setuid_setgid()) {
99 printf_error("Disregarding %s environment variable for setuid/setgid binary",
106 static const char *get_home_dir(void)
111 val
= bt_secure_getenv(HOME_ENV_VAR
);
115 /* Fallback on password file. */
116 pwd
= getpwuid(getuid());
126 char *bt_common_get_home_plugin_path(void)
129 const char *home_dir
;
131 home_dir
= get_home_dir();
136 if (strlen(home_dir
) + strlen(HOME_PLUGIN_SUBPATH
) + 1 >= PATH_MAX
) {
137 printf_error("Home directory path is too long: `%s`\n",
142 path
= malloc(PATH_MAX
);
147 strcpy(path
, home_dir
);
148 strcat(path
, HOME_PLUGIN_SUBPATH
);
155 int bt_common_append_plugin_path_dirs(const char *paths
, GPtrArray
*dirs
)
160 size_t init_dirs_len
;
163 init_dirs_len
= dirs
->len
;
166 /* Nothing to append */
171 end
= paths
+ strlen(paths
);
175 const char *next_colon
;
177 next_colon
= strchr(at
, ':');
178 if (next_colon
== at
) {
180 * Empty path: try next character (supported
181 * to conform to the typical parsing of $PATH).
185 } else if (!next_colon
) {
186 /* No more colon: use the remaining */
187 next_colon
= paths
+ strlen(paths
);
190 path
= g_string_new(NULL
);
195 g_string_append_len(path
, at
, next_colon
- at
);
197 g_ptr_array_add(dirs
, path
);
205 /* Remove the new entries in dirs */
206 while (dirs
->len
> init_dirs_len
) {
207 g_ptr_array_remove_index(dirs
, init_dirs_len
);
215 bool bt_common_colors_supported(void)
217 static bool supports_colors
= false;
218 static bool supports_colors_set
= false;
221 if (supports_colors_set
) {
225 supports_colors_set
= true;
227 term
= getenv("TERM");
232 if (strncmp(term
, "xterm", 5) != 0 &&
233 strncmp(term
, "rxvt", 4) != 0 &&
234 strncmp(term
, "konsole", 7) != 0 &&
235 strncmp(term
, "gnome", 5) != 0 &&
236 strncmp(term
, "screen", 5) != 0 &&
237 strncmp(term
, "tmux", 4) != 0 &&
238 strncmp(term
, "putty", 5) != 0) {
246 supports_colors
= true;
249 return supports_colors
;
253 const char *bt_common_color_reset(void)
255 return bt_common_color_code_reset
;
259 const char *bt_common_color_bold(void)
261 return bt_common_color_code_bold
;
265 const char *bt_common_color_fg_default(void)
267 return bt_common_color_code_fg_default
;
271 const char *bt_common_color_fg_red(void)
273 return bt_common_color_code_fg_red
;
277 const char *bt_common_color_fg_green(void)
279 return bt_common_color_code_fg_green
;
283 const char *bt_common_color_fg_yellow(void)
285 return bt_common_color_code_fg_yellow
;
289 const char *bt_common_color_fg_blue(void)
291 return bt_common_color_code_fg_blue
;
295 const char *bt_common_color_fg_magenta(void)
297 return bt_common_color_code_fg_magenta
;
301 const char *bt_common_color_fg_cyan(void)
303 return bt_common_color_code_fg_cyan
;
307 const char *bt_common_color_fg_light_gray(void)
309 return bt_common_color_code_fg_light_gray
;
313 const char *bt_common_color_bg_default(void)
315 return bt_common_color_code_bg_default
;
319 const char *bt_common_color_bg_red(void)
321 return bt_common_color_code_bg_red
;
325 const char *bt_common_color_bg_green(void)
327 return bt_common_color_code_bg_green
;
331 const char *bt_common_color_bg_yellow(void)
333 return bt_common_color_code_bg_yellow
;
337 const char *bt_common_color_bg_blue(void)
339 return bt_common_color_code_bg_blue
;
343 const char *bt_common_color_bg_magenta(void)
345 return bt_common_color_code_bg_magenta
;
349 const char *bt_common_color_bg_cyan(void)
351 return bt_common_color_code_bg_cyan
;
355 const char *bt_common_color_bg_light_gray(void)
357 return bt_common_color_code_bg_light_gray
;
361 GString
*bt_common_string_until(const char *input
, const char *escapable_chars
,
362 const char *end_chars
, size_t *end_pos
)
364 GString
*output
= g_string_new(NULL
);
367 const char *end_char
;
373 for (ch
= input
; *ch
!= '\0'; ch
++) {
375 bool continue_loop
= false;
378 /* `\` at the end of the string: append `\` */
379 g_string_append_c(output
, *ch
);
384 for (es_char
= escapable_chars
; *es_char
!= '\0'; es_char
++) {
385 if (ch
[1] == *es_char
) {
387 * `\` followed by an escapable
388 * character: append the escaped
391 g_string_append_c(output
, ch
[1]);
393 continue_loop
= true;
403 * `\` followed by a non-escapable character:
404 * append `\` and the character.
406 g_string_append_c(output
, *ch
);
407 g_string_append_c(output
, ch
[1]);
411 for (end_char
= end_chars
; *end_char
!= '\0'; end_char
++) {
412 if (*ch
== *end_char
) {
414 * End character found:
415 * terminate this loop.
421 /* Normal character: append */
422 g_string_append_c(output
, *ch
);
428 *end_pos
= ch
- input
;
435 g_string_free(output
, TRUE
);
443 GString
*bt_common_shell_quote(const char *input
, bool with_single_quotes
)
445 GString
*output
= g_string_new(NULL
);
447 bool no_quote
= true;
453 if (strlen(input
) == 0) {
454 if (with_single_quotes
) {
455 g_string_assign(output
, "''");
461 for (ch
= input
; *ch
!= '\0'; ch
++) {
464 if (!g_ascii_isalpha(c
) && !g_ascii_isdigit(c
) && c
!= '_' &&
465 c
!= '@' && c
!= '%' && c
!= '+' && c
!= '=' &&
466 c
!= ':' && c
!= ',' && c
!= '.' && c
!= '/' &&
474 g_string_assign(output
, input
);
478 if (with_single_quotes
) {
479 g_string_assign(output
, "'");
482 for (ch
= input
; *ch
!= '\0'; ch
++) {
484 g_string_append(output
, "'\"'\"'");
486 g_string_append_c(output
, *ch
);
490 if (with_single_quotes
) {
491 g_string_append_c(output
, '\'');
499 bool bt_common_string_is_printable(const char *input
)
502 bool printable
= true;
505 for (ch
= input
; *ch
!= '\0'; ch
++) {
506 if (!isprint(*ch
) && *ch
!= '\n' && *ch
!= '\r' &&
507 *ch
!= '\t' && *ch
!= '\v') {
518 void bt_common_destroy_lttng_live_url_parts(
519 struct bt_common_lttng_live_url_parts
*parts
)
526 g_string_free(parts
->proto
, TRUE
);
530 if (parts
->hostname
) {
531 g_string_free(parts
->hostname
, TRUE
);
532 parts
->hostname
= NULL
;
535 if (parts
->target_hostname
) {
536 g_string_free(parts
->target_hostname
, TRUE
);
537 parts
->target_hostname
= NULL
;
540 if (parts
->session_name
) {
541 g_string_free(parts
->session_name
, TRUE
);
542 parts
->session_name
= NULL
;
550 struct bt_common_lttng_live_url_parts
bt_common_parse_lttng_live_url(
551 const char *url
, char *error_buf
, size_t error_buf_size
)
553 struct bt_common_lttng_live_url_parts parts
;
554 const char *at
= url
;
558 memset(&parts
, 0, sizeof(parts
));
562 parts
.proto
= bt_common_string_until(at
, "", ":", &end_pos
);
563 if (!parts
.proto
|| parts
.proto
->len
== 0) {
565 snprintf(error_buf
, error_buf_size
, "Missing protocol");
571 if (strcmp(parts
.proto
->str
, "net") == 0) {
572 g_string_assign(parts
.proto
, "net4");
575 if (strcmp(parts
.proto
->str
, "net4") != 0 &&
576 strcmp(parts
.proto
->str
, "net6") != 0) {
578 snprintf(error_buf
, error_buf_size
,
579 "Unknown protocol: `%s`", parts
.proto
->str
);
585 if (at
[end_pos
] != ':') {
587 snprintf(error_buf
, error_buf_size
,
588 "Expecting `:` after `%s`", parts
.proto
->str
);
597 if (strncmp(at
, "://", 3) != 0) {
599 snprintf(error_buf
, error_buf_size
,
600 "Expecting `://` after protocol");
609 parts
.hostname
= bt_common_string_until(at
, "", ":/", &end_pos
);
610 if (!parts
.hostname
|| parts
.hostname
->len
== 0) {
612 snprintf(error_buf
, error_buf_size
, "Missing hostname");
618 if (at
[end_pos
] == ':') {
623 port
= bt_common_string_until(at
, "", "/", &end_pos
);
624 if (!port
|| port
->len
== 0) {
626 snprintf(error_buf
, error_buf_size
, "Missing port");
632 if (sscanf(port
->str
, "%d", &parts
.port
) != 1) {
634 snprintf(error_buf
, error_buf_size
,
635 "Invalid port: `%s`", port
->str
);
638 g_string_free(port
, TRUE
);
642 g_string_free(port
, TRUE
);
644 if (parts
.port
< 0 || parts
.port
>= 65536) {
646 snprintf(error_buf
, error_buf_size
,
647 "Invalid port: %d", parts
.port
);
654 if (at
[end_pos
] == '\0') {
661 if (strncmp(at
, "/host/", 6) != 0) {
663 snprintf(error_buf
, error_buf_size
,
664 "Expecting `/host/` after hostname or port");
672 /* Target hostname */
673 parts
.target_hostname
= bt_common_string_until(at
, "", "/", &end_pos
);
674 if (!parts
.target_hostname
|| parts
.target_hostname
->len
== 0) {
676 snprintf(error_buf
, error_buf_size
,
677 "Missing target hostname");
683 if (at
[end_pos
] == '\0') {
690 parts
.session_name
= bt_common_string_until(at
, "", "/", &end_pos
);
691 if (!parts
.session_name
|| parts
.session_name
->len
== 0) {
693 snprintf(error_buf
, error_buf_size
,
694 "Missing session name");
700 if (at
[end_pos
] == '/') {
702 snprintf(error_buf
, error_buf_size
,
703 "Unexpected `/` after session name (`%s`)",
704 parts
.session_name
->str
);
713 bt_common_destroy_lttng_live_url_parts(&parts
);
720 void bt_common_normalize_star_glob_pattern(char *pattern
)
724 bool got_star
= false;
728 for (p
= pattern
, np
= pattern
; *p
!= '\0'; p
++) {
732 /* Avoid consecutive stars. */
739 /* Copy backslash character. */
748 /* Fall through default case. */
754 /* Copy single character. */
764 bool at_end_of_pattern(const char *p
, const char *pattern
, size_t pattern_len
)
766 return (p
- pattern
) == pattern_len
|| *p
== '\0';
770 * Globbing matching function with the star feature only (`?` and
771 * character sets are not supported). This matches `candidate` (plain
772 * string) against `pattern`. A literal star can be escaped with `\` in
775 * `pattern_len` or `candidate_len` can be greater than the actual
776 * string length of `pattern` or `candidate` if the string is
780 bool bt_common_star_glob_match(const char *pattern
, size_t pattern_len
,
781 const char *candidate
, size_t candidate_len
) {
782 const char *retry_c
= candidate
, *retry_p
= pattern
, *c
, *p
;
783 bool got_a_star
= false;
790 * The concept here is to retry a match in the specific case
791 * where we already got a star. The retry position for the
792 * pattern is just after the most recent star, and the retry
793 * position for the candidate is the character following the
794 * last try's first character.
798 * candidate: hi ev every onyx one
800 * pattern: hi*every*one
803 * candidate: hi ev every onyx one
805 * pattern: hi*every*one
808 * candidate: hi ev every onyx one
810 * pattern: hi*every*one
813 * candidate: hi ev every onyx one
815 * pattern: hi*every*one
818 * candidate: hi ev every onyx one
820 * pattern: hi*every*one
823 * candidate: hi ev every onyx one
825 * pattern: hi*every*one
828 * candidate: hi ev every onyx one
830 * pattern: hi*every*one
833 * candidate: hi ev every onyx one
835 * pattern: hi*every*one
838 * candidate: hi ev every onyx one
840 * pattern: hi*every*one
843 * candidate: hi ev every onyx one
845 * pattern: hi*every*one
848 * candidate: hi ev every onyx one
850 * pattern: hi*every*one
853 * candidate: hi ev every onyx one
855 * pattern: hi*every*one
858 * candidate: hi ev every onyx one
860 * pattern: hi*every*one
863 * candidate: hi ev every onyx one
865 * pattern: hi*every*one
868 * candidate: hi ev every onyx one
870 * pattern: hi*every*one
873 * candidate: hi ev every onyx one
875 * pattern: hi*every*one
878 * candidate: hi ev every onyx one
880 * pattern: hi*every*one
883 * candidate: hi ev every onyx one
885 * pattern: hi*every*one
888 * candidate: hi ev every onyx one
890 * pattern: hi*every*one
893 * candidate: hi ev every onyx one
895 * pattern: hi*every*one
898 * candidate: hi ev every onyx one
900 * pattern: hi*every*one
903 * candidate: hi ev every onyx one
905 * pattern: hi*every*one
908 * candidate: hi ev every onyx one
910 * pattern: hi*every*one
913 * candidate: hi ev every onyx one
915 * pattern: hi*every*one
918 * candidate: hi ev every onyx one
920 * pattern: hi*every*one
923 * candidate: hi ev every onyx one
925 * pattern: hi*every*one
928 * candidate: hi ev every onyx one
930 * pattern: hi*every*one
933 while ((c
- candidate
) < candidate_len
&& *c
!= '\0') {
936 if (at_end_of_pattern(p
, pattern
, pattern_len
)) {
945 * Our first try starts at the current candidate
946 * character and after the star in the pattern.
951 if (at_end_of_pattern(retry_p
, pattern
, pattern_len
)) {
953 * Star at the end of the pattern at
954 * this point: automatic match.
961 /* Go to escaped character. */
965 * Fall through the default case which compares
966 * the escaped character now.
969 if (at_end_of_pattern(p
, pattern
, pattern_len
) ||
972 /* Character mismatch OR end of pattern. */
975 * We didn't get any star yet,
976 * so this first mismatch
977 * automatically makes the whole
984 * Next try: next candidate character,
985 * original pattern character (following
986 * the most recent star).
994 /* Next pattern and candidate characters. */
1000 * We checked every candidate character and we're still in a
1001 * success state: the only pattern character allowed to remain
1004 if (at_end_of_pattern(p
, pattern
, pattern_len
)) {
1009 return p
[-1] == '*' && at_end_of_pattern(p
, pattern
, pattern_len
);
1013 void append_path_parts(const char *path
, GPtrArray
*parts
)
1015 const char *ch
= path
;
1016 const char *last
= path
;
1019 if (*ch
== G_DIR_SEPARATOR
|| *ch
== '\0') {
1020 if (ch
- last
> 0) {
1021 GString
*part
= g_string_new(NULL
);
1024 g_string_append_len(part
, last
, ch
- last
);
1025 g_ptr_array_add(parts
, part
);
1040 void destroy_gstring(void *gstring
)
1042 (void) g_string_free(gstring
, TRUE
);
1046 GString
*bt_common_normalize_path(const char *path
, const char *wd
)
1050 GPtrArray
*parts
= NULL
;
1053 norm_path
= g_string_new(G_DIR_SEPARATOR_S
);
1058 parts
= g_ptr_array_new_with_free_func(destroy_gstring
);
1063 if (path
[0] != G_DIR_SEPARATOR
) {
1064 /* Relative path: start with working directory */
1066 append_path_parts(wd
, parts
);
1068 gchar
*cd
= g_get_current_dir();
1070 append_path_parts(cd
, parts
);
1075 /* Append parts of the path parameter */
1076 append_path_parts(path
, parts
);
1078 /* Resolve special `..` and `.` parts */
1079 for (i
= 0; i
< parts
->len
; i
++) {
1080 GString
*part
= g_ptr_array_index(parts
, i
);
1082 if (strcmp(part
->str
, "..") == 0) {
1085 * First part of absolute path is `..`:
1091 /* Remove `..` and previous part */
1092 g_ptr_array_remove_index(parts
, i
- 1);
1093 g_ptr_array_remove_index(parts
, i
- 1);
1095 } else if (strcmp(part
->str
, ".") == 0) {
1097 g_ptr_array_remove_index(parts
, i
);
1102 /* Create normalized path with what's left */
1103 for (i
= 0; i
< parts
->len
; i
++) {
1104 GString
*part
= g_ptr_array_index(parts
, i
);
1106 g_string_append(norm_path
, part
->str
);
1108 if (i
< parts
->len
- 1) {
1109 g_string_append_c(norm_path
, G_DIR_SEPARATOR
);
1117 g_string_free(norm_path
, TRUE
);
1123 g_ptr_array_free(parts
, TRUE
);