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
;
96 bool bt_common_is_setuid_setgid(void)
98 return (geteuid() != getuid() || getegid() != getgid());
102 char *bt_secure_getenv(const char *name
)
104 if (bt_common_is_setuid_setgid()) {
105 printf_error("Disregarding %s environment variable for setuid/setgid binary",
114 const char *bt_get_home_dir(void)
116 return g_get_home_dir();
118 #else /* __MINGW32__ */
120 const char *bt_get_home_dir(void)
125 val
= bt_secure_getenv(HOME_ENV_VAR
);
129 /* Fallback on password file. */
130 pwd
= getpwuid(getuid());
138 #endif /* __MINGW32__ */
141 char *bt_common_get_home_plugin_path(void)
144 const char *home_dir
;
146 home_dir
= bt_get_home_dir();
151 if (strlen(home_dir
) + strlen(HOME_PLUGIN_SUBPATH
) + 1 >= PATH_MAX
) {
152 printf_error("Home directory path is too long: `%s`\n",
157 path
= malloc(PATH_MAX
);
162 strcpy(path
, home_dir
);
163 strcat(path
, HOME_PLUGIN_SUBPATH
);
170 int bt_common_append_plugin_path_dirs(const char *paths
, GPtrArray
*dirs
)
175 size_t init_dirs_len
;
178 init_dirs_len
= dirs
->len
;
181 /* Nothing to append */
186 end
= paths
+ strlen(paths
);
190 const char *next_colon
;
192 next_colon
= strchr(at
, ':');
193 if (next_colon
== at
) {
195 * Empty path: try next character (supported
196 * to conform to the typical parsing of $PATH).
200 } else if (!next_colon
) {
201 /* No more colon: use the remaining */
202 next_colon
= paths
+ strlen(paths
);
205 path
= g_string_new(NULL
);
210 g_string_append_len(path
, at
, next_colon
- at
);
212 g_ptr_array_add(dirs
, path
);
220 /* Remove the new entries in dirs */
221 while (dirs
->len
> init_dirs_len
) {
222 g_ptr_array_remove_index(dirs
, init_dirs_len
);
230 bool bt_common_colors_supported(void)
232 static bool supports_colors
= false;
233 static bool supports_colors_set
= false;
236 if (supports_colors_set
) {
240 supports_colors_set
= true;
242 term
= getenv("TERM");
247 if (strncmp(term
, "xterm", 5) != 0 &&
248 strncmp(term
, "rxvt", 4) != 0 &&
249 strncmp(term
, "konsole", 7) != 0 &&
250 strncmp(term
, "gnome", 5) != 0 &&
251 strncmp(term
, "screen", 5) != 0 &&
252 strncmp(term
, "tmux", 4) != 0 &&
253 strncmp(term
, "putty", 5) != 0) {
261 supports_colors
= true;
264 return supports_colors
;
268 const char *bt_common_color_reset(void)
270 return bt_common_color_code_reset
;
274 const char *bt_common_color_bold(void)
276 return bt_common_color_code_bold
;
280 const char *bt_common_color_fg_default(void)
282 return bt_common_color_code_fg_default
;
286 const char *bt_common_color_fg_red(void)
288 return bt_common_color_code_fg_red
;
292 const char *bt_common_color_fg_green(void)
294 return bt_common_color_code_fg_green
;
298 const char *bt_common_color_fg_yellow(void)
300 return bt_common_color_code_fg_yellow
;
304 const char *bt_common_color_fg_blue(void)
306 return bt_common_color_code_fg_blue
;
310 const char *bt_common_color_fg_magenta(void)
312 return bt_common_color_code_fg_magenta
;
316 const char *bt_common_color_fg_cyan(void)
318 return bt_common_color_code_fg_cyan
;
322 const char *bt_common_color_fg_light_gray(void)
324 return bt_common_color_code_fg_light_gray
;
328 const char *bt_common_color_bg_default(void)
330 return bt_common_color_code_bg_default
;
334 const char *bt_common_color_bg_red(void)
336 return bt_common_color_code_bg_red
;
340 const char *bt_common_color_bg_green(void)
342 return bt_common_color_code_bg_green
;
346 const char *bt_common_color_bg_yellow(void)
348 return bt_common_color_code_bg_yellow
;
352 const char *bt_common_color_bg_blue(void)
354 return bt_common_color_code_bg_blue
;
358 const char *bt_common_color_bg_magenta(void)
360 return bt_common_color_code_bg_magenta
;
364 const char *bt_common_color_bg_cyan(void)
366 return bt_common_color_code_bg_cyan
;
370 const char *bt_common_color_bg_light_gray(void)
372 return bt_common_color_code_bg_light_gray
;
376 GString
*bt_common_string_until(const char *input
, const char *escapable_chars
,
377 const char *end_chars
, size_t *end_pos
)
379 GString
*output
= g_string_new(NULL
);
382 const char *end_char
;
388 for (ch
= input
; *ch
!= '\0'; ch
++) {
390 bool continue_loop
= false;
393 /* `\` at the end of the string: append `\` */
394 g_string_append_c(output
, *ch
);
399 for (es_char
= escapable_chars
; *es_char
!= '\0'; es_char
++) {
400 if (ch
[1] == *es_char
) {
402 * `\` followed by an escapable
403 * character: append the escaped
406 g_string_append_c(output
, ch
[1]);
408 continue_loop
= true;
418 * `\` followed by a non-escapable character:
419 * append `\` and the character.
421 g_string_append_c(output
, *ch
);
422 g_string_append_c(output
, ch
[1]);
426 for (end_char
= end_chars
; *end_char
!= '\0'; end_char
++) {
427 if (*ch
== *end_char
) {
429 * End character found:
430 * terminate this loop.
436 /* Normal character: append */
437 g_string_append_c(output
, *ch
);
443 *end_pos
= ch
- input
;
450 g_string_free(output
, TRUE
);
458 GString
*bt_common_shell_quote(const char *input
, bool with_single_quotes
)
460 GString
*output
= g_string_new(NULL
);
462 bool no_quote
= true;
468 if (strlen(input
) == 0) {
469 if (with_single_quotes
) {
470 g_string_assign(output
, "''");
476 for (ch
= input
; *ch
!= '\0'; ch
++) {
479 if (!g_ascii_isalpha(c
) && !g_ascii_isdigit(c
) && c
!= '_' &&
480 c
!= '@' && c
!= '%' && c
!= '+' && c
!= '=' &&
481 c
!= ':' && c
!= ',' && c
!= '.' && c
!= '/' &&
489 g_string_assign(output
, input
);
493 if (with_single_quotes
) {
494 g_string_assign(output
, "'");
497 for (ch
= input
; *ch
!= '\0'; ch
++) {
499 g_string_append(output
, "'\"'\"'");
501 g_string_append_c(output
, *ch
);
505 if (with_single_quotes
) {
506 g_string_append_c(output
, '\'');
514 bool bt_common_string_is_printable(const char *input
)
517 bool printable
= true;
520 for (ch
= input
; *ch
!= '\0'; ch
++) {
521 if (!isprint(*ch
) && *ch
!= '\n' && *ch
!= '\r' &&
522 *ch
!= '\t' && *ch
!= '\v') {
533 void bt_common_destroy_lttng_live_url_parts(
534 struct bt_common_lttng_live_url_parts
*parts
)
541 g_string_free(parts
->proto
, TRUE
);
545 if (parts
->hostname
) {
546 g_string_free(parts
->hostname
, TRUE
);
547 parts
->hostname
= NULL
;
550 if (parts
->target_hostname
) {
551 g_string_free(parts
->target_hostname
, TRUE
);
552 parts
->target_hostname
= NULL
;
555 if (parts
->session_name
) {
556 g_string_free(parts
->session_name
, TRUE
);
557 parts
->session_name
= NULL
;
565 struct bt_common_lttng_live_url_parts
bt_common_parse_lttng_live_url(
566 const char *url
, char *error_buf
, size_t error_buf_size
)
568 struct bt_common_lttng_live_url_parts parts
;
569 const char *at
= url
;
573 memset(&parts
, 0, sizeof(parts
));
577 parts
.proto
= bt_common_string_until(at
, "", ":", &end_pos
);
578 if (!parts
.proto
|| parts
.proto
->len
== 0) {
580 snprintf(error_buf
, error_buf_size
, "Missing protocol");
586 if (strcmp(parts
.proto
->str
, "net") == 0) {
587 g_string_assign(parts
.proto
, "net4");
590 if (strcmp(parts
.proto
->str
, "net4") != 0 &&
591 strcmp(parts
.proto
->str
, "net6") != 0) {
593 snprintf(error_buf
, error_buf_size
,
594 "Unknown protocol: `%s`", parts
.proto
->str
);
600 if (at
[end_pos
] != ':') {
602 snprintf(error_buf
, error_buf_size
,
603 "Expecting `:` after `%s`", parts
.proto
->str
);
612 if (strncmp(at
, "://", 3) != 0) {
614 snprintf(error_buf
, error_buf_size
,
615 "Expecting `://` after protocol");
624 parts
.hostname
= bt_common_string_until(at
, "", ":/", &end_pos
);
625 if (!parts
.hostname
|| parts
.hostname
->len
== 0) {
627 snprintf(error_buf
, error_buf_size
, "Missing hostname");
633 if (at
[end_pos
] == ':') {
638 port
= bt_common_string_until(at
, "", "/", &end_pos
);
639 if (!port
|| port
->len
== 0) {
641 snprintf(error_buf
, error_buf_size
, "Missing port");
647 if (sscanf(port
->str
, "%d", &parts
.port
) != 1) {
649 snprintf(error_buf
, error_buf_size
,
650 "Invalid port: `%s`", port
->str
);
653 g_string_free(port
, TRUE
);
657 g_string_free(port
, TRUE
);
659 if (parts
.port
< 0 || parts
.port
>= 65536) {
661 snprintf(error_buf
, error_buf_size
,
662 "Invalid port: %d", parts
.port
);
669 if (at
[end_pos
] == '\0') {
676 if (strncmp(at
, "/host/", 6) != 0) {
678 snprintf(error_buf
, error_buf_size
,
679 "Expecting `/host/` after hostname or port");
687 /* Target hostname */
688 parts
.target_hostname
= bt_common_string_until(at
, "", "/", &end_pos
);
689 if (!parts
.target_hostname
|| parts
.target_hostname
->len
== 0) {
691 snprintf(error_buf
, error_buf_size
,
692 "Missing target hostname");
698 if (at
[end_pos
] == '\0') {
705 parts
.session_name
= bt_common_string_until(at
, "", "/", &end_pos
);
706 if (!parts
.session_name
|| parts
.session_name
->len
== 0) {
708 snprintf(error_buf
, error_buf_size
,
709 "Missing session name");
715 if (at
[end_pos
] == '/') {
717 snprintf(error_buf
, error_buf_size
,
718 "Unexpected `/` after session name (`%s`)",
719 parts
.session_name
->str
);
728 bt_common_destroy_lttng_live_url_parts(&parts
);
735 void bt_common_normalize_star_glob_pattern(char *pattern
)
739 bool got_star
= false;
743 for (p
= pattern
, np
= pattern
; *p
!= '\0'; p
++) {
747 /* Avoid consecutive stars. */
754 /* Copy backslash character. */
763 /* Fall through default case. */
769 /* Copy single character. */
779 bool at_end_of_pattern(const char *p
, const char *pattern
, size_t pattern_len
)
781 return (p
- pattern
) == pattern_len
|| *p
== '\0';
785 * Globbing matching function with the star feature only (`?` and
786 * character sets are not supported). This matches `candidate` (plain
787 * string) against `pattern`. A literal star can be escaped with `\` in
790 * `pattern_len` or `candidate_len` can be greater than the actual
791 * string length of `pattern` or `candidate` if the string is
795 bool bt_common_star_glob_match(const char *pattern
, size_t pattern_len
,
796 const char *candidate
, size_t candidate_len
) {
797 const char *retry_c
= candidate
, *retry_p
= pattern
, *c
, *p
;
798 bool got_a_star
= false;
805 * The concept here is to retry a match in the specific case
806 * where we already got a star. The retry position for the
807 * pattern is just after the most recent star, and the retry
808 * position for the candidate is the character following the
809 * last try's first character.
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 * candidate: hi ev every onyx one
935 * pattern: hi*every*one
938 * candidate: hi ev every onyx one
940 * pattern: hi*every*one
943 * candidate: hi ev every onyx one
945 * pattern: hi*every*one
948 while ((c
- candidate
) < candidate_len
&& *c
!= '\0') {
951 if (at_end_of_pattern(p
, pattern
, pattern_len
)) {
960 * Our first try starts at the current candidate
961 * character and after the star in the pattern.
966 if (at_end_of_pattern(retry_p
, pattern
, pattern_len
)) {
968 * Star at the end of the pattern at
969 * this point: automatic match.
976 /* Go to escaped character. */
980 * Fall through the default case which compares
981 * the escaped character now.
984 if (at_end_of_pattern(p
, pattern
, pattern_len
) ||
987 /* Character mismatch OR end of pattern. */
990 * We didn't get any star yet,
991 * so this first mismatch
992 * automatically makes the whole
999 * Next try: next candidate character,
1000 * original pattern character (following
1001 * the most recent star).
1009 /* Next pattern and candidate characters. */
1015 * We checked every candidate character and we're still in a
1016 * success state: the only pattern character allowed to remain
1019 if (at_end_of_pattern(p
, pattern
, pattern_len
)) {
1024 return p
[-1] == '*' && at_end_of_pattern(p
, pattern
, pattern_len
);
1028 void append_path_parts(const char *path
, GPtrArray
*parts
)
1030 const char *ch
= path
;
1031 const char *last
= path
;
1034 if (*ch
== G_DIR_SEPARATOR
|| *ch
== '\0') {
1035 if (ch
- last
> 0) {
1036 GString
*part
= g_string_new(NULL
);
1039 g_string_append_len(part
, last
, ch
- last
);
1040 g_ptr_array_add(parts
, part
);
1055 void destroy_gstring(void *gstring
)
1057 (void) g_string_free(gstring
, TRUE
);
1061 GString
*bt_common_normalize_path(const char *path
, const char *wd
)
1065 GPtrArray
*parts
= NULL
;
1068 norm_path
= g_string_new(G_DIR_SEPARATOR_S
);
1073 parts
= g_ptr_array_new_with_free_func(destroy_gstring
);
1078 if (path
[0] != G_DIR_SEPARATOR
) {
1079 /* Relative path: start with working directory */
1081 append_path_parts(wd
, parts
);
1083 gchar
*cd
= g_get_current_dir();
1085 append_path_parts(cd
, parts
);
1090 /* Append parts of the path parameter */
1091 append_path_parts(path
, parts
);
1093 /* Resolve special `..` and `.` parts */
1094 for (i
= 0; i
< parts
->len
; i
++) {
1095 GString
*part
= g_ptr_array_index(parts
, i
);
1097 if (strcmp(part
->str
, "..") == 0) {
1100 * First part of absolute path is `..`:
1106 /* Remove `..` and previous part */
1107 g_ptr_array_remove_index(parts
, i
- 1);
1108 g_ptr_array_remove_index(parts
, i
- 1);
1110 } else if (strcmp(part
->str
, ".") == 0) {
1112 g_ptr_array_remove_index(parts
, i
);
1117 /* Create normalized path with what's left */
1118 for (i
= 0; i
< parts
->len
; i
++) {
1119 GString
*part
= g_ptr_array_index(parts
, i
);
1121 g_string_append(norm_path
, part
->str
);
1123 if (i
< parts
->len
- 1) {
1124 g_string_append_c(norm_path
, G_DIR_SEPARATOR
);
1132 g_string_free(norm_path
, TRUE
);
1138 g_ptr_array_free(parts
, TRUE
);
1145 size_t bt_common_get_page_size(void)
1149 page_size
= bt_sysconf(_SC_PAGESIZE
);
1150 if (page_size
< 0) {
1151 printf_error("Cannot get system page size.");