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_TAG "COMMON"
30 #include <sys/types.h>
36 #include <babeltrace/babeltrace-internal.h>
37 #include <babeltrace/common-internal.h>
38 #include <babeltrace/compat/unistd-internal.h>
44 #define SYSTEM_PLUGIN_PATH INSTALL_LIBDIR "/babeltrace/plugins"
45 #define HOME_ENV_VAR "HOME"
46 #define HOME_PLUGIN_SUBPATH "/.local/lib/babeltrace/plugins"
48 static const char *bt_common_color_code_reset
= "";
49 static const char *bt_common_color_code_bold
= "";
50 static const char *bt_common_color_code_fg_default
= "";
51 static const char *bt_common_color_code_fg_red
= "";
52 static const char *bt_common_color_code_fg_green
= "";
53 static const char *bt_common_color_code_fg_yellow
= "";
54 static const char *bt_common_color_code_fg_blue
= "";
55 static const char *bt_common_color_code_fg_magenta
= "";
56 static const char *bt_common_color_code_fg_cyan
= "";
57 static const char *bt_common_color_code_fg_light_gray
= "";
58 static const char *bt_common_color_code_bg_default
= "";
59 static const char *bt_common_color_code_bg_red
= "";
60 static const char *bt_common_color_code_bg_green
= "";
61 static const char *bt_common_color_code_bg_yellow
= "";
62 static const char *bt_common_color_code_bg_blue
= "";
63 static const char *bt_common_color_code_bg_magenta
= "";
64 static const char *bt_common_color_code_bg_cyan
= "";
65 static const char *bt_common_color_code_bg_light_gray
= "";
68 void __attribute__((constructor
)) bt_common_color_ctor(void)
70 if (bt_common_colors_supported()) {
71 bt_common_color_code_reset
= BT_COMMON_COLOR_RESET
;
72 bt_common_color_code_bold
= BT_COMMON_COLOR_BOLD
;
73 bt_common_color_code_fg_default
= BT_COMMON_COLOR_FG_DEFAULT
;
74 bt_common_color_code_fg_red
= BT_COMMON_COLOR_FG_RED
;
75 bt_common_color_code_fg_green
= BT_COMMON_COLOR_FG_GREEN
;
76 bt_common_color_code_fg_yellow
= BT_COMMON_COLOR_FG_YELLOW
;
77 bt_common_color_code_fg_blue
= BT_COMMON_COLOR_FG_BLUE
;
78 bt_common_color_code_fg_magenta
= BT_COMMON_COLOR_FG_MAGENTA
;
79 bt_common_color_code_fg_cyan
= BT_COMMON_COLOR_FG_CYAN
;
80 bt_common_color_code_fg_light_gray
= BT_COMMON_COLOR_FG_LIGHT_GRAY
;
81 bt_common_color_code_bg_default
= BT_COMMON_COLOR_BG_DEFAULT
;
82 bt_common_color_code_bg_red
= BT_COMMON_COLOR_BG_RED
;
83 bt_common_color_code_bg_green
= BT_COMMON_COLOR_BG_GREEN
;
84 bt_common_color_code_bg_yellow
= BT_COMMON_COLOR_BG_YELLOW
;
85 bt_common_color_code_bg_blue
= BT_COMMON_COLOR_BG_BLUE
;
86 bt_common_color_code_bg_magenta
= BT_COMMON_COLOR_BG_MAGENTA
;
87 bt_common_color_code_bg_cyan
= BT_COMMON_COLOR_BG_CYAN
;
88 bt_common_color_code_bg_light_gray
= BT_COMMON_COLOR_BG_LIGHT_GRAY
;
93 const char *bt_common_get_system_plugin_path(void)
95 return SYSTEM_PLUGIN_PATH
;
100 bool bt_common_is_setuid_setgid(void)
104 #else /* __MINGW32__ */
106 bool bt_common_is_setuid_setgid(void)
108 return (geteuid() != getuid() || getegid() != getgid());
110 #endif /* __MINGW32__ */
113 char *bt_secure_getenv(const char *name
)
115 if (bt_common_is_setuid_setgid()) {
116 BT_LOGD("Disregarding environment variable for setuid/setgid binary: "
117 "name=\"%s\"", name
);
125 const char *bt_get_home_dir(void)
127 return g_get_home_dir();
129 #else /* __MINGW32__ */
131 const char *bt_get_home_dir(void)
136 val
= bt_secure_getenv(HOME_ENV_VAR
);
140 /* Fallback on password file. */
141 pwd
= getpwuid(getuid());
149 #endif /* __MINGW32__ */
152 char *bt_common_get_home_plugin_path(void)
155 const char *home_dir
;
158 home_dir
= bt_get_home_dir();
163 length
= strlen(home_dir
) + strlen(HOME_PLUGIN_SUBPATH
) + 1;
165 if (length
>= PATH_MAX
) {
166 BT_LOGW("Home directory path is too long: length=%zu",
171 path
= malloc(PATH_MAX
);
176 strcpy(path
, home_dir
);
177 strcat(path
, HOME_PLUGIN_SUBPATH
);
184 int bt_common_append_plugin_path_dirs(const char *paths
, GPtrArray
*dirs
)
189 size_t init_dirs_len
;
192 init_dirs_len
= dirs
->len
;
195 /* Nothing to append */
200 end
= paths
+ strlen(paths
);
204 const char *next_colon
;
206 next_colon
= strchr(at
, ':');
207 if (next_colon
== at
) {
209 * Empty path: try next character (supported
210 * to conform to the typical parsing of $PATH).
214 } else if (!next_colon
) {
215 /* No more colon: use the remaining */
216 next_colon
= paths
+ strlen(paths
);
219 path
= g_string_new(NULL
);
224 g_string_append_len(path
, at
, next_colon
- at
);
226 g_ptr_array_add(dirs
, path
);
234 /* Remove the new entries in dirs */
235 while (dirs
->len
> init_dirs_len
) {
236 g_ptr_array_remove_index(dirs
, init_dirs_len
);
244 bool bt_common_colors_supported(void)
246 static bool supports_colors
= false;
247 static bool supports_colors_set
= false;
251 if (supports_colors_set
) {
255 supports_colors_set
= true;
257 force
= getenv("BABELTRACE_FORCE_COLORS");
258 if (force
&& strcmp(force
, "1") == 0) {
259 supports_colors
= true;
263 term
= getenv("TERM");
268 if (strncmp(term
, "xterm", 5) != 0 &&
269 strncmp(term
, "rxvt", 4) != 0 &&
270 strncmp(term
, "konsole", 7) != 0 &&
271 strncmp(term
, "gnome", 5) != 0 &&
272 strncmp(term
, "screen", 5) != 0 &&
273 strncmp(term
, "tmux", 4) != 0 &&
274 strncmp(term
, "putty", 5) != 0) {
282 supports_colors
= true;
285 return supports_colors
;
289 const char *bt_common_color_reset(void)
291 return bt_common_color_code_reset
;
295 const char *bt_common_color_bold(void)
297 return bt_common_color_code_bold
;
301 const char *bt_common_color_fg_default(void)
303 return bt_common_color_code_fg_default
;
307 const char *bt_common_color_fg_red(void)
309 return bt_common_color_code_fg_red
;
313 const char *bt_common_color_fg_green(void)
315 return bt_common_color_code_fg_green
;
319 const char *bt_common_color_fg_yellow(void)
321 return bt_common_color_code_fg_yellow
;
325 const char *bt_common_color_fg_blue(void)
327 return bt_common_color_code_fg_blue
;
331 const char *bt_common_color_fg_magenta(void)
333 return bt_common_color_code_fg_magenta
;
337 const char *bt_common_color_fg_cyan(void)
339 return bt_common_color_code_fg_cyan
;
343 const char *bt_common_color_fg_light_gray(void)
345 return bt_common_color_code_fg_light_gray
;
349 const char *bt_common_color_bg_default(void)
351 return bt_common_color_code_bg_default
;
355 const char *bt_common_color_bg_red(void)
357 return bt_common_color_code_bg_red
;
361 const char *bt_common_color_bg_green(void)
363 return bt_common_color_code_bg_green
;
367 const char *bt_common_color_bg_yellow(void)
369 return bt_common_color_code_bg_yellow
;
373 const char *bt_common_color_bg_blue(void)
375 return bt_common_color_code_bg_blue
;
379 const char *bt_common_color_bg_magenta(void)
381 return bt_common_color_code_bg_magenta
;
385 const char *bt_common_color_bg_cyan(void)
387 return bt_common_color_code_bg_cyan
;
391 const char *bt_common_color_bg_light_gray(void)
393 return bt_common_color_code_bg_light_gray
;
397 GString
*bt_common_string_until(const char *input
, const char *escapable_chars
,
398 const char *end_chars
, size_t *end_pos
)
400 GString
*output
= g_string_new(NULL
);
403 const char *end_char
;
409 for (ch
= input
; *ch
!= '\0'; ch
++) {
411 bool continue_loop
= false;
414 /* `\` at the end of the string: append `\` */
415 g_string_append_c(output
, *ch
);
420 for (es_char
= escapable_chars
; *es_char
!= '\0'; es_char
++) {
421 if (ch
[1] == *es_char
) {
423 * `\` followed by an escapable
424 * character: append the escaped
427 g_string_append_c(output
, ch
[1]);
429 continue_loop
= true;
439 * `\` followed by a non-escapable character:
440 * append `\` and the character.
442 g_string_append_c(output
, *ch
);
443 g_string_append_c(output
, ch
[1]);
447 for (end_char
= end_chars
; *end_char
!= '\0'; end_char
++) {
448 if (*ch
== *end_char
) {
450 * End character found:
451 * terminate this loop.
457 /* Normal character: append */
458 g_string_append_c(output
, *ch
);
464 *end_pos
= ch
- input
;
471 g_string_free(output
, TRUE
);
479 GString
*bt_common_shell_quote(const char *input
, bool with_single_quotes
)
481 GString
*output
= g_string_new(NULL
);
483 bool no_quote
= true;
489 if (strlen(input
) == 0) {
490 if (with_single_quotes
) {
491 g_string_assign(output
, "''");
497 for (ch
= input
; *ch
!= '\0'; ch
++) {
500 if (!g_ascii_isalpha(c
) && !g_ascii_isdigit(c
) && c
!= '_' &&
501 c
!= '@' && c
!= '%' && c
!= '+' && c
!= '=' &&
502 c
!= ':' && c
!= ',' && c
!= '.' && c
!= '/' &&
510 g_string_assign(output
, input
);
514 if (with_single_quotes
) {
515 g_string_assign(output
, "'");
518 for (ch
= input
; *ch
!= '\0'; ch
++) {
520 g_string_append(output
, "'\"'\"'");
522 g_string_append_c(output
, *ch
);
526 if (with_single_quotes
) {
527 g_string_append_c(output
, '\'');
535 bool bt_common_string_is_printable(const char *input
)
538 bool printable
= true;
541 for (ch
= input
; *ch
!= '\0'; ch
++) {
542 if (!isprint(*ch
) && *ch
!= '\n' && *ch
!= '\r' &&
543 *ch
!= '\t' && *ch
!= '\v') {
554 void bt_common_destroy_lttng_live_url_parts(
555 struct bt_common_lttng_live_url_parts
*parts
)
562 g_string_free(parts
->proto
, TRUE
);
566 if (parts
->hostname
) {
567 g_string_free(parts
->hostname
, TRUE
);
568 parts
->hostname
= NULL
;
571 if (parts
->target_hostname
) {
572 g_string_free(parts
->target_hostname
, TRUE
);
573 parts
->target_hostname
= NULL
;
576 if (parts
->session_name
) {
577 g_string_free(parts
->session_name
, TRUE
);
578 parts
->session_name
= NULL
;
586 struct bt_common_lttng_live_url_parts
bt_common_parse_lttng_live_url(
587 const char *url
, char *error_buf
, size_t error_buf_size
)
589 struct bt_common_lttng_live_url_parts parts
;
590 const char *at
= url
;
594 memset(&parts
, 0, sizeof(parts
));
598 parts
.proto
= bt_common_string_until(at
, "", ":", &end_pos
);
599 if (!parts
.proto
|| parts
.proto
->len
== 0) {
601 snprintf(error_buf
, error_buf_size
, "Missing protocol");
607 if (strcmp(parts
.proto
->str
, "net") == 0) {
608 g_string_assign(parts
.proto
, "net4");
611 if (strcmp(parts
.proto
->str
, "net4") != 0 &&
612 strcmp(parts
.proto
->str
, "net6") != 0) {
614 snprintf(error_buf
, error_buf_size
,
615 "Unknown protocol: `%s`", parts
.proto
->str
);
621 if (at
[end_pos
] != ':') {
623 snprintf(error_buf
, error_buf_size
,
624 "Expecting `:` after `%s`", parts
.proto
->str
);
633 if (strncmp(at
, "://", 3) != 0) {
635 snprintf(error_buf
, error_buf_size
,
636 "Expecting `://` after protocol");
645 parts
.hostname
= bt_common_string_until(at
, "", ":/", &end_pos
);
646 if (!parts
.hostname
|| parts
.hostname
->len
== 0) {
648 snprintf(error_buf
, error_buf_size
, "Missing hostname");
654 if (at
[end_pos
] == ':') {
659 port
= bt_common_string_until(at
, "", "/", &end_pos
);
660 if (!port
|| port
->len
== 0) {
662 snprintf(error_buf
, error_buf_size
, "Missing port");
668 if (sscanf(port
->str
, "%d", &parts
.port
) != 1) {
670 snprintf(error_buf
, error_buf_size
,
671 "Invalid port: `%s`", port
->str
);
674 g_string_free(port
, TRUE
);
678 g_string_free(port
, TRUE
);
680 if (parts
.port
< 0 || parts
.port
>= 65536) {
682 snprintf(error_buf
, error_buf_size
,
683 "Invalid port: %d", parts
.port
);
690 if (at
[end_pos
] == '\0') {
697 if (strncmp(at
, "/host/", 6) != 0) {
699 snprintf(error_buf
, error_buf_size
,
700 "Expecting `/host/` after hostname or port");
708 /* Target hostname */
709 parts
.target_hostname
= bt_common_string_until(at
, "", "/", &end_pos
);
710 if (!parts
.target_hostname
|| parts
.target_hostname
->len
== 0) {
712 snprintf(error_buf
, error_buf_size
,
713 "Missing target hostname");
719 if (at
[end_pos
] == '\0') {
726 parts
.session_name
= bt_common_string_until(at
, "", "/", &end_pos
);
727 if (!parts
.session_name
|| parts
.session_name
->len
== 0) {
729 snprintf(error_buf
, error_buf_size
,
730 "Missing session name");
736 if (at
[end_pos
] == '/') {
738 snprintf(error_buf
, error_buf_size
,
739 "Unexpected `/` after session name (`%s`)",
740 parts
.session_name
->str
);
749 bt_common_destroy_lttng_live_url_parts(&parts
);
756 void bt_common_normalize_star_glob_pattern(char *pattern
)
760 bool got_star
= false;
764 for (p
= pattern
, np
= pattern
; *p
!= '\0'; p
++) {
768 /* Avoid consecutive stars. */
775 /* Copy backslash character. */
784 /* Fall through default case. */
790 /* Copy single character. */
800 bool at_end_of_pattern(const char *p
, const char *pattern
, size_t pattern_len
)
802 return (p
- pattern
) == pattern_len
|| *p
== '\0';
806 * Globbing matching function with the star feature only (`?` and
807 * character sets are not supported). This matches `candidate` (plain
808 * string) against `pattern`. A literal star can be escaped with `\` in
811 * `pattern_len` or `candidate_len` can be greater than the actual
812 * string length of `pattern` or `candidate` if the string is
816 bool bt_common_star_glob_match(const char *pattern
, size_t pattern_len
,
817 const char *candidate
, size_t candidate_len
) {
818 const char *retry_c
= candidate
, *retry_p
= pattern
, *c
, *p
;
819 bool got_a_star
= false;
826 * The concept here is to retry a match in the specific case
827 * where we already got a star. The retry position for the
828 * pattern is just after the most recent star, and the retry
829 * position for the candidate is the character following the
830 * last try's first character.
834 * candidate: hi ev every onyx one
836 * pattern: hi*every*one
839 * candidate: hi ev every onyx one
841 * pattern: hi*every*one
844 * candidate: hi ev every onyx one
846 * pattern: hi*every*one
849 * candidate: hi ev every onyx one
851 * pattern: hi*every*one
854 * candidate: hi ev every onyx one
856 * pattern: hi*every*one
859 * candidate: hi ev every onyx one
861 * pattern: hi*every*one
864 * candidate: hi ev every onyx one
866 * pattern: hi*every*one
869 * candidate: hi ev every onyx one
871 * pattern: hi*every*one
874 * candidate: hi ev every onyx one
876 * pattern: hi*every*one
879 * candidate: hi ev every onyx one
881 * pattern: hi*every*one
884 * candidate: hi ev every onyx one
886 * pattern: hi*every*one
889 * candidate: hi ev every onyx one
891 * pattern: hi*every*one
894 * candidate: hi ev every onyx one
896 * pattern: hi*every*one
899 * candidate: hi ev every onyx one
901 * pattern: hi*every*one
904 * candidate: hi ev every onyx one
906 * pattern: hi*every*one
909 * candidate: hi ev every onyx one
911 * pattern: hi*every*one
914 * candidate: hi ev every onyx one
916 * pattern: hi*every*one
919 * candidate: hi ev every onyx one
921 * pattern: hi*every*one
924 * candidate: hi ev every onyx one
926 * pattern: hi*every*one
929 * candidate: hi ev every onyx one
931 * pattern: hi*every*one
934 * candidate: hi ev every onyx one
936 * pattern: hi*every*one
939 * candidate: hi ev every onyx one
941 * pattern: hi*every*one
944 * candidate: hi ev every onyx one
946 * pattern: hi*every*one
949 * candidate: hi ev every onyx one
951 * pattern: hi*every*one
954 * candidate: hi ev every onyx one
956 * pattern: hi*every*one
959 * candidate: hi ev every onyx one
961 * pattern: hi*every*one
964 * candidate: hi ev every onyx one
966 * pattern: hi*every*one
969 while ((c
- candidate
) < candidate_len
&& *c
!= '\0') {
972 if (at_end_of_pattern(p
, pattern
, pattern_len
)) {
981 * Our first try starts at the current candidate
982 * character and after the star in the pattern.
987 if (at_end_of_pattern(retry_p
, pattern
, pattern_len
)) {
989 * Star at the end of the pattern at
990 * this point: automatic match.
997 /* Go to escaped character. */
1001 * Fall through the default case which compares
1002 * the escaped character now.
1005 if (at_end_of_pattern(p
, pattern
, pattern_len
) ||
1008 /* Character mismatch OR end of pattern. */
1011 * We didn't get any star yet,
1012 * so this first mismatch
1013 * automatically makes the whole
1020 * Next try: next candidate character,
1021 * original pattern character (following
1022 * the most recent star).
1030 /* Next pattern and candidate characters. */
1036 * We checked every candidate character and we're still in a
1037 * success state: the only pattern character allowed to remain
1040 if (at_end_of_pattern(p
, pattern
, pattern_len
)) {
1045 return p
[-1] == '*' && at_end_of_pattern(p
, pattern
, pattern_len
);
1049 void append_path_parts(const char *path
, GPtrArray
*parts
)
1051 const char *ch
= path
;
1052 const char *last
= path
;
1055 if (*ch
== G_DIR_SEPARATOR
|| *ch
== '\0') {
1056 if (ch
- last
> 0) {
1057 GString
*part
= g_string_new(NULL
);
1060 g_string_append_len(part
, last
, ch
- last
);
1061 g_ptr_array_add(parts
, part
);
1076 void destroy_gstring(void *gstring
)
1078 (void) g_string_free(gstring
, TRUE
);
1082 GString
*bt_common_normalize_path(const char *path
, const char *wd
)
1086 GPtrArray
*parts
= NULL
;
1089 norm_path
= g_string_new(G_DIR_SEPARATOR_S
);
1094 parts
= g_ptr_array_new_with_free_func(destroy_gstring
);
1099 if (path
[0] != G_DIR_SEPARATOR
) {
1100 /* Relative path: start with working directory */
1102 append_path_parts(wd
, parts
);
1104 gchar
*cd
= g_get_current_dir();
1106 append_path_parts(cd
, parts
);
1111 /* Append parts of the path parameter */
1112 append_path_parts(path
, parts
);
1114 /* Resolve special `..` and `.` parts */
1115 for (i
= 0; i
< parts
->len
; i
++) {
1116 GString
*part
= g_ptr_array_index(parts
, i
);
1118 if (strcmp(part
->str
, "..") == 0) {
1121 * First part of absolute path is `..`:
1127 /* Remove `..` and previous part */
1128 g_ptr_array_remove_index(parts
, i
- 1);
1129 g_ptr_array_remove_index(parts
, i
- 1);
1131 } else if (strcmp(part
->str
, ".") == 0) {
1133 g_ptr_array_remove_index(parts
, i
);
1138 /* Create normalized path with what's left */
1139 for (i
= 0; i
< parts
->len
; i
++) {
1140 GString
*part
= g_ptr_array_index(parts
, i
);
1142 g_string_append(norm_path
, part
->str
);
1144 if (i
< parts
->len
- 1) {
1145 g_string_append_c(norm_path
, G_DIR_SEPARATOR
);
1153 g_string_free(norm_path
, TRUE
);
1159 g_ptr_array_free(parts
, TRUE
);
1166 size_t bt_common_get_page_size(void)
1170 page_size
= bt_sysconf(_SC_PAGESIZE
);
1171 if (page_size
< 0) {
1172 BT_LOGF("Cannot get system's page size: ret=%d",