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>
35 #include <babeltrace/compat/unistd-internal.h>
41 #define SYSTEM_PLUGIN_PATH INSTALL_LIBDIR "/babeltrace/plugins"
42 #define HOME_ENV_VAR "HOME"
43 #define HOME_PLUGIN_SUBPATH "/.local/lib/babeltrace/plugins"
45 static const char *bt_common_color_code_reset
= "";
46 static const char *bt_common_color_code_bold
= "";
47 static const char *bt_common_color_code_fg_default
= "";
48 static const char *bt_common_color_code_fg_red
= "";
49 static const char *bt_common_color_code_fg_green
= "";
50 static const char *bt_common_color_code_fg_yellow
= "";
51 static const char *bt_common_color_code_fg_blue
= "";
52 static const char *bt_common_color_code_fg_magenta
= "";
53 static const char *bt_common_color_code_fg_cyan
= "";
54 static const char *bt_common_color_code_fg_light_gray
= "";
55 static const char *bt_common_color_code_bg_default
= "";
56 static const char *bt_common_color_code_bg_red
= "";
57 static const char *bt_common_color_code_bg_green
= "";
58 static const char *bt_common_color_code_bg_yellow
= "";
59 static const char *bt_common_color_code_bg_blue
= "";
60 static const char *bt_common_color_code_bg_magenta
= "";
61 static const char *bt_common_color_code_bg_cyan
= "";
62 static const char *bt_common_color_code_bg_light_gray
= "";
65 void __attribute__((constructor
)) bt_common_color_ctor(void)
67 if (bt_common_colors_supported()) {
68 bt_common_color_code_reset
= BT_COMMON_COLOR_RESET
;
69 bt_common_color_code_bold
= BT_COMMON_COLOR_BOLD
;
70 bt_common_color_code_fg_default
= BT_COMMON_COLOR_FG_DEFAULT
;
71 bt_common_color_code_fg_red
= BT_COMMON_COLOR_FG_RED
;
72 bt_common_color_code_fg_green
= BT_COMMON_COLOR_FG_GREEN
;
73 bt_common_color_code_fg_yellow
= BT_COMMON_COLOR_FG_YELLOW
;
74 bt_common_color_code_fg_blue
= BT_COMMON_COLOR_FG_BLUE
;
75 bt_common_color_code_fg_magenta
= BT_COMMON_COLOR_FG_MAGENTA
;
76 bt_common_color_code_fg_cyan
= BT_COMMON_COLOR_FG_CYAN
;
77 bt_common_color_code_fg_light_gray
= BT_COMMON_COLOR_FG_LIGHT_GRAY
;
78 bt_common_color_code_bg_default
= BT_COMMON_COLOR_BG_DEFAULT
;
79 bt_common_color_code_bg_red
= BT_COMMON_COLOR_BG_RED
;
80 bt_common_color_code_bg_green
= BT_COMMON_COLOR_BG_GREEN
;
81 bt_common_color_code_bg_yellow
= BT_COMMON_COLOR_BG_YELLOW
;
82 bt_common_color_code_bg_blue
= BT_COMMON_COLOR_BG_BLUE
;
83 bt_common_color_code_bg_magenta
= BT_COMMON_COLOR_BG_MAGENTA
;
84 bt_common_color_code_bg_cyan
= BT_COMMON_COLOR_BG_CYAN
;
85 bt_common_color_code_bg_light_gray
= BT_COMMON_COLOR_BG_LIGHT_GRAY
;
90 const char *bt_common_get_system_plugin_path(void)
92 return SYSTEM_PLUGIN_PATH
;
97 bool bt_common_is_setuid_setgid(void)
101 #else /* __MINGW32__ */
103 bool bt_common_is_setuid_setgid(void)
105 return (geteuid() != getuid() || getegid() != getgid());
107 #endif /* __MINGW32__ */
110 char *bt_secure_getenv(const char *name
)
112 if (bt_common_is_setuid_setgid()) {
113 printf_error("Disregarding %s environment variable for setuid/setgid binary",
122 const char *bt_get_home_dir(void)
124 return g_get_home_dir();
126 #else /* __MINGW32__ */
128 const char *bt_get_home_dir(void)
133 val
= bt_secure_getenv(HOME_ENV_VAR
);
137 /* Fallback on password file. */
138 pwd
= getpwuid(getuid());
146 #endif /* __MINGW32__ */
149 char *bt_common_get_home_plugin_path(void)
152 const char *home_dir
;
154 home_dir
= bt_get_home_dir();
159 if (strlen(home_dir
) + strlen(HOME_PLUGIN_SUBPATH
) + 1 >= PATH_MAX
) {
160 printf_error("Home directory path is too long: `%s`\n",
165 path
= malloc(PATH_MAX
);
170 strcpy(path
, home_dir
);
171 strcat(path
, HOME_PLUGIN_SUBPATH
);
178 int bt_common_append_plugin_path_dirs(const char *paths
, GPtrArray
*dirs
)
183 size_t init_dirs_len
;
186 init_dirs_len
= dirs
->len
;
189 /* Nothing to append */
194 end
= paths
+ strlen(paths
);
198 const char *next_colon
;
200 next_colon
= strchr(at
, ':');
201 if (next_colon
== at
) {
203 * Empty path: try next character (supported
204 * to conform to the typical parsing of $PATH).
208 } else if (!next_colon
) {
209 /* No more colon: use the remaining */
210 next_colon
= paths
+ strlen(paths
);
213 path
= g_string_new(NULL
);
218 g_string_append_len(path
, at
, next_colon
- at
);
220 g_ptr_array_add(dirs
, path
);
228 /* Remove the new entries in dirs */
229 while (dirs
->len
> init_dirs_len
) {
230 g_ptr_array_remove_index(dirs
, init_dirs_len
);
238 bool bt_common_colors_supported(void)
240 static bool supports_colors
= false;
241 static bool supports_colors_set
= false;
244 if (supports_colors_set
) {
248 supports_colors_set
= true;
250 term
= getenv("TERM");
255 if (strncmp(term
, "xterm", 5) != 0 &&
256 strncmp(term
, "rxvt", 4) != 0 &&
257 strncmp(term
, "konsole", 7) != 0 &&
258 strncmp(term
, "gnome", 5) != 0 &&
259 strncmp(term
, "screen", 5) != 0 &&
260 strncmp(term
, "tmux", 4) != 0 &&
261 strncmp(term
, "putty", 5) != 0) {
269 supports_colors
= true;
272 return supports_colors
;
276 const char *bt_common_color_reset(void)
278 return bt_common_color_code_reset
;
282 const char *bt_common_color_bold(void)
284 return bt_common_color_code_bold
;
288 const char *bt_common_color_fg_default(void)
290 return bt_common_color_code_fg_default
;
294 const char *bt_common_color_fg_red(void)
296 return bt_common_color_code_fg_red
;
300 const char *bt_common_color_fg_green(void)
302 return bt_common_color_code_fg_green
;
306 const char *bt_common_color_fg_yellow(void)
308 return bt_common_color_code_fg_yellow
;
312 const char *bt_common_color_fg_blue(void)
314 return bt_common_color_code_fg_blue
;
318 const char *bt_common_color_fg_magenta(void)
320 return bt_common_color_code_fg_magenta
;
324 const char *bt_common_color_fg_cyan(void)
326 return bt_common_color_code_fg_cyan
;
330 const char *bt_common_color_fg_light_gray(void)
332 return bt_common_color_code_fg_light_gray
;
336 const char *bt_common_color_bg_default(void)
338 return bt_common_color_code_bg_default
;
342 const char *bt_common_color_bg_red(void)
344 return bt_common_color_code_bg_red
;
348 const char *bt_common_color_bg_green(void)
350 return bt_common_color_code_bg_green
;
354 const char *bt_common_color_bg_yellow(void)
356 return bt_common_color_code_bg_yellow
;
360 const char *bt_common_color_bg_blue(void)
362 return bt_common_color_code_bg_blue
;
366 const char *bt_common_color_bg_magenta(void)
368 return bt_common_color_code_bg_magenta
;
372 const char *bt_common_color_bg_cyan(void)
374 return bt_common_color_code_bg_cyan
;
378 const char *bt_common_color_bg_light_gray(void)
380 return bt_common_color_code_bg_light_gray
;
384 GString
*bt_common_string_until(const char *input
, const char *escapable_chars
,
385 const char *end_chars
, size_t *end_pos
)
387 GString
*output
= g_string_new(NULL
);
390 const char *end_char
;
396 for (ch
= input
; *ch
!= '\0'; ch
++) {
398 bool continue_loop
= false;
401 /* `\` at the end of the string: append `\` */
402 g_string_append_c(output
, *ch
);
407 for (es_char
= escapable_chars
; *es_char
!= '\0'; es_char
++) {
408 if (ch
[1] == *es_char
) {
410 * `\` followed by an escapable
411 * character: append the escaped
414 g_string_append_c(output
, ch
[1]);
416 continue_loop
= true;
426 * `\` followed by a non-escapable character:
427 * append `\` and the character.
429 g_string_append_c(output
, *ch
);
430 g_string_append_c(output
, ch
[1]);
434 for (end_char
= end_chars
; *end_char
!= '\0'; end_char
++) {
435 if (*ch
== *end_char
) {
437 * End character found:
438 * terminate this loop.
444 /* Normal character: append */
445 g_string_append_c(output
, *ch
);
451 *end_pos
= ch
- input
;
458 g_string_free(output
, TRUE
);
466 GString
*bt_common_shell_quote(const char *input
, bool with_single_quotes
)
468 GString
*output
= g_string_new(NULL
);
470 bool no_quote
= true;
476 if (strlen(input
) == 0) {
477 if (with_single_quotes
) {
478 g_string_assign(output
, "''");
484 for (ch
= input
; *ch
!= '\0'; ch
++) {
487 if (!g_ascii_isalpha(c
) && !g_ascii_isdigit(c
) && c
!= '_' &&
488 c
!= '@' && c
!= '%' && c
!= '+' && c
!= '=' &&
489 c
!= ':' && c
!= ',' && c
!= '.' && c
!= '/' &&
497 g_string_assign(output
, input
);
501 if (with_single_quotes
) {
502 g_string_assign(output
, "'");
505 for (ch
= input
; *ch
!= '\0'; ch
++) {
507 g_string_append(output
, "'\"'\"'");
509 g_string_append_c(output
, *ch
);
513 if (with_single_quotes
) {
514 g_string_append_c(output
, '\'');
522 bool bt_common_string_is_printable(const char *input
)
525 bool printable
= true;
528 for (ch
= input
; *ch
!= '\0'; ch
++) {
529 if (!isprint(*ch
) && *ch
!= '\n' && *ch
!= '\r' &&
530 *ch
!= '\t' && *ch
!= '\v') {
541 void bt_common_destroy_lttng_live_url_parts(
542 struct bt_common_lttng_live_url_parts
*parts
)
549 g_string_free(parts
->proto
, TRUE
);
553 if (parts
->hostname
) {
554 g_string_free(parts
->hostname
, TRUE
);
555 parts
->hostname
= NULL
;
558 if (parts
->target_hostname
) {
559 g_string_free(parts
->target_hostname
, TRUE
);
560 parts
->target_hostname
= NULL
;
563 if (parts
->session_name
) {
564 g_string_free(parts
->session_name
, TRUE
);
565 parts
->session_name
= NULL
;
573 struct bt_common_lttng_live_url_parts
bt_common_parse_lttng_live_url(
574 const char *url
, char *error_buf
, size_t error_buf_size
)
576 struct bt_common_lttng_live_url_parts parts
;
577 const char *at
= url
;
581 memset(&parts
, 0, sizeof(parts
));
585 parts
.proto
= bt_common_string_until(at
, "", ":", &end_pos
);
586 if (!parts
.proto
|| parts
.proto
->len
== 0) {
588 snprintf(error_buf
, error_buf_size
, "Missing protocol");
594 if (strcmp(parts
.proto
->str
, "net") == 0) {
595 g_string_assign(parts
.proto
, "net4");
598 if (strcmp(parts
.proto
->str
, "net4") != 0 &&
599 strcmp(parts
.proto
->str
, "net6") != 0) {
601 snprintf(error_buf
, error_buf_size
,
602 "Unknown protocol: `%s`", parts
.proto
->str
);
608 if (at
[end_pos
] != ':') {
610 snprintf(error_buf
, error_buf_size
,
611 "Expecting `:` after `%s`", parts
.proto
->str
);
620 if (strncmp(at
, "://", 3) != 0) {
622 snprintf(error_buf
, error_buf_size
,
623 "Expecting `://` after protocol");
632 parts
.hostname
= bt_common_string_until(at
, "", ":/", &end_pos
);
633 if (!parts
.hostname
|| parts
.hostname
->len
== 0) {
635 snprintf(error_buf
, error_buf_size
, "Missing hostname");
641 if (at
[end_pos
] == ':') {
646 port
= bt_common_string_until(at
, "", "/", &end_pos
);
647 if (!port
|| port
->len
== 0) {
649 snprintf(error_buf
, error_buf_size
, "Missing port");
655 if (sscanf(port
->str
, "%d", &parts
.port
) != 1) {
657 snprintf(error_buf
, error_buf_size
,
658 "Invalid port: `%s`", port
->str
);
661 g_string_free(port
, TRUE
);
665 g_string_free(port
, TRUE
);
667 if (parts
.port
< 0 || parts
.port
>= 65536) {
669 snprintf(error_buf
, error_buf_size
,
670 "Invalid port: %d", parts
.port
);
677 if (at
[end_pos
] == '\0') {
684 if (strncmp(at
, "/host/", 6) != 0) {
686 snprintf(error_buf
, error_buf_size
,
687 "Expecting `/host/` after hostname or port");
695 /* Target hostname */
696 parts
.target_hostname
= bt_common_string_until(at
, "", "/", &end_pos
);
697 if (!parts
.target_hostname
|| parts
.target_hostname
->len
== 0) {
699 snprintf(error_buf
, error_buf_size
,
700 "Missing target hostname");
706 if (at
[end_pos
] == '\0') {
713 parts
.session_name
= bt_common_string_until(at
, "", "/", &end_pos
);
714 if (!parts
.session_name
|| parts
.session_name
->len
== 0) {
716 snprintf(error_buf
, error_buf_size
,
717 "Missing session name");
723 if (at
[end_pos
] == '/') {
725 snprintf(error_buf
, error_buf_size
,
726 "Unexpected `/` after session name (`%s`)",
727 parts
.session_name
->str
);
736 bt_common_destroy_lttng_live_url_parts(&parts
);
743 void bt_common_normalize_star_glob_pattern(char *pattern
)
747 bool got_star
= false;
751 for (p
= pattern
, np
= pattern
; *p
!= '\0'; p
++) {
755 /* Avoid consecutive stars. */
762 /* Copy backslash character. */
771 /* Fall through default case. */
777 /* Copy single character. */
787 bool at_end_of_pattern(const char *p
, const char *pattern
, size_t pattern_len
)
789 return (p
- pattern
) == pattern_len
|| *p
== '\0';
793 * Globbing matching function with the star feature only (`?` and
794 * character sets are not supported). This matches `candidate` (plain
795 * string) against `pattern`. A literal star can be escaped with `\` in
798 * `pattern_len` or `candidate_len` can be greater than the actual
799 * string length of `pattern` or `candidate` if the string is
803 bool bt_common_star_glob_match(const char *pattern
, size_t pattern_len
,
804 const char *candidate
, size_t candidate_len
) {
805 const char *retry_c
= candidate
, *retry_p
= pattern
, *c
, *p
;
806 bool got_a_star
= false;
813 * The concept here is to retry a match in the specific case
814 * where we already got a star. The retry position for the
815 * pattern is just after the most recent star, and the retry
816 * position for the candidate is the character following the
817 * last try's first character.
821 * candidate: hi ev every onyx one
823 * pattern: hi*every*one
826 * candidate: hi ev every onyx one
828 * pattern: hi*every*one
831 * candidate: hi ev every onyx one
833 * pattern: hi*every*one
836 * candidate: hi ev every onyx one
838 * pattern: hi*every*one
841 * candidate: hi ev every onyx one
843 * pattern: hi*every*one
846 * candidate: hi ev every onyx one
848 * pattern: hi*every*one
851 * candidate: hi ev every onyx one
853 * pattern: hi*every*one
856 * candidate: hi ev every onyx one
858 * pattern: hi*every*one
861 * candidate: hi ev every onyx one
863 * pattern: hi*every*one
866 * candidate: hi ev every onyx one
868 * pattern: hi*every*one
871 * candidate: hi ev every onyx one
873 * pattern: hi*every*one
876 * candidate: hi ev every onyx one
878 * pattern: hi*every*one
881 * candidate: hi ev every onyx one
883 * pattern: hi*every*one
886 * candidate: hi ev every onyx one
888 * pattern: hi*every*one
891 * candidate: hi ev every onyx one
893 * pattern: hi*every*one
896 * candidate: hi ev every onyx one
898 * pattern: hi*every*one
901 * candidate: hi ev every onyx one
903 * pattern: hi*every*one
906 * candidate: hi ev every onyx one
908 * pattern: hi*every*one
911 * candidate: hi ev every onyx one
913 * pattern: hi*every*one
916 * candidate: hi ev every onyx one
918 * pattern: hi*every*one
921 * candidate: hi ev every onyx one
923 * pattern: hi*every*one
926 * candidate: hi ev every onyx one
928 * pattern: hi*every*one
931 * candidate: hi ev every onyx one
933 * pattern: hi*every*one
936 * candidate: hi ev every onyx one
938 * pattern: hi*every*one
941 * candidate: hi ev every onyx one
943 * pattern: hi*every*one
946 * candidate: hi ev every onyx one
948 * pattern: hi*every*one
951 * candidate: hi ev every onyx one
953 * pattern: hi*every*one
956 while ((c
- candidate
) < candidate_len
&& *c
!= '\0') {
959 if (at_end_of_pattern(p
, pattern
, pattern_len
)) {
968 * Our first try starts at the current candidate
969 * character and after the star in the pattern.
974 if (at_end_of_pattern(retry_p
, pattern
, pattern_len
)) {
976 * Star at the end of the pattern at
977 * this point: automatic match.
984 /* Go to escaped character. */
988 * Fall through the default case which compares
989 * the escaped character now.
992 if (at_end_of_pattern(p
, pattern
, pattern_len
) ||
995 /* Character mismatch OR end of pattern. */
998 * We didn't get any star yet,
999 * so this first mismatch
1000 * automatically makes the whole
1007 * Next try: next candidate character,
1008 * original pattern character (following
1009 * the most recent star).
1017 /* Next pattern and candidate characters. */
1023 * We checked every candidate character and we're still in a
1024 * success state: the only pattern character allowed to remain
1027 if (at_end_of_pattern(p
, pattern
, pattern_len
)) {
1032 return p
[-1] == '*' && at_end_of_pattern(p
, pattern
, pattern_len
);
1036 void append_path_parts(const char *path
, GPtrArray
*parts
)
1038 const char *ch
= path
;
1039 const char *last
= path
;
1042 if (*ch
== G_DIR_SEPARATOR
|| *ch
== '\0') {
1043 if (ch
- last
> 0) {
1044 GString
*part
= g_string_new(NULL
);
1047 g_string_append_len(part
, last
, ch
- last
);
1048 g_ptr_array_add(parts
, part
);
1063 void destroy_gstring(void *gstring
)
1065 (void) g_string_free(gstring
, TRUE
);
1069 GString
*bt_common_normalize_path(const char *path
, const char *wd
)
1073 GPtrArray
*parts
= NULL
;
1076 norm_path
= g_string_new(G_DIR_SEPARATOR_S
);
1081 parts
= g_ptr_array_new_with_free_func(destroy_gstring
);
1086 if (path
[0] != G_DIR_SEPARATOR
) {
1087 /* Relative path: start with working directory */
1089 append_path_parts(wd
, parts
);
1091 gchar
*cd
= g_get_current_dir();
1093 append_path_parts(cd
, parts
);
1098 /* Append parts of the path parameter */
1099 append_path_parts(path
, parts
);
1101 /* Resolve special `..` and `.` parts */
1102 for (i
= 0; i
< parts
->len
; i
++) {
1103 GString
*part
= g_ptr_array_index(parts
, i
);
1105 if (strcmp(part
->str
, "..") == 0) {
1108 * First part of absolute path is `..`:
1114 /* Remove `..` and previous part */
1115 g_ptr_array_remove_index(parts
, i
- 1);
1116 g_ptr_array_remove_index(parts
, i
- 1);
1118 } else if (strcmp(part
->str
, ".") == 0) {
1120 g_ptr_array_remove_index(parts
, i
);
1125 /* Create normalized path with what's left */
1126 for (i
= 0; i
< parts
->len
; i
++) {
1127 GString
*part
= g_ptr_array_index(parts
, i
);
1129 g_string_append(norm_path
, part
->str
);
1131 if (i
< parts
->len
- 1) {
1132 g_string_append_c(norm_path
, G_DIR_SEPARATOR
);
1140 g_string_free(norm_path
, TRUE
);
1146 g_ptr_array_free(parts
, TRUE
);
1153 size_t bt_common_get_page_size(void)
1157 page_size
= bt_sysconf(_SC_PAGESIZE
);
1158 if (page_size
< 0) {
1159 printf_error("Cannot get system page size.");