Rename tests/bin -> tests/cli
[babeltrace.git] / common / common.c
CommitLineData
1670bffd
PP
1/*
2 * Babeltrace common functions
3 *
4 * Copyright 2016 Philippe Proulx <pproulx@efficios.com>
5 *
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:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
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
22 * SOFTWARE.
23 */
24
290725f7 25#include <unistd.h>
1670bffd
PP
26#include <string.h>
27#include <sys/types.h>
28#include <pwd.h>
29#include <unistd.h>
30#include <assert.h>
db0f160a 31#include <ctype.h>
1670bffd
PP
32#include <glib.h>
33#include <babeltrace/babeltrace-internal.h>
ad96d936 34#include <babeltrace/common-internal.h>
1670bffd
PP
35
36#define SYSTEM_PLUGIN_PATH INSTALL_LIBDIR "/babeltrace/plugins"
37#define HOME_ENV_VAR "HOME"
38#define HOME_PLUGIN_SUBPATH "/.local/lib/babeltrace/plugins"
39
40BT_HIDDEN
41const char *bt_common_get_system_plugin_path(void)
42{
43 return SYSTEM_PLUGIN_PATH;
44}
45
46BT_HIDDEN
47bool bt_common_is_setuid_setgid(void)
48{
49 return (geteuid() != getuid() || getegid() != getgid());
50}
51
52static char *bt_secure_getenv(const char *name)
53{
54 if (bt_common_is_setuid_setgid()) {
55 printf_error("Disregarding %s environment variable for setuid/setgid binary",
56 name);
57 return NULL;
58 }
59 return getenv(name);
60}
61
62static const char *get_home_dir(void)
63{
64 char *val = NULL;
65 struct passwd *pwd;
66
67 val = bt_secure_getenv(HOME_ENV_VAR);
68 if (val) {
69 goto end;
70 }
71 /* Fallback on password file. */
72 pwd = getpwuid(getuid());
73 if (!pwd) {
74 goto end;
75 }
76 val = pwd->pw_dir;
77end:
78 return val;
79}
80
81BT_HIDDEN
82char *bt_common_get_home_plugin_path(void)
83{
84 char *path = NULL;
85 const char *home_dir;
86
87 home_dir = get_home_dir();
88 if (!home_dir) {
89 goto end;
90 }
91
92 if (strlen(home_dir) + strlen(HOME_PLUGIN_SUBPATH) + 1 >= PATH_MAX) {
93 printf_error("Home directory path is too long: `%s`\n",
94 home_dir);
95 goto end;
96 }
97
98 path = malloc(PATH_MAX);
99 if (!path) {
100 goto end;
101 }
102
103 strcpy(path, home_dir);
104 strcat(path, HOME_PLUGIN_SUBPATH);
105
106end:
107 return path;
108}
109
110BT_HIDDEN
111int bt_common_append_plugin_path_dirs(const char *paths, GPtrArray *dirs)
112{
113 int ret = 0;
114 const char *at;
115 const char *end;
116 size_t init_dirs_len;
117
118 assert(dirs);
119 init_dirs_len = dirs->len;
120
121 if (!paths) {
122 /* Nothing to append */
123 goto end;
124 }
125
126 at = paths;
127 end = paths + strlen(paths);
128
129 while (at < end) {
130 GString *path;
131 const char *next_colon;
132
133 next_colon = strchr(at, ':');
134 if (next_colon == at) {
135 /*
136 * Empty path: try next character (supported
137 * to conform to the typical parsing of $PATH).
138 */
139 at++;
140 continue;
141 } else if (!next_colon) {
142 /* No more colon: use the remaining */
143 next_colon = paths + strlen(paths);
144 }
145
146 path = g_string_new(NULL);
147 if (!path) {
148 goto error;
149 }
150
151 g_string_append_len(path, at, next_colon - at);
152 at = next_colon + 1;
153 g_ptr_array_add(dirs, path);
154 }
155
156 goto end;
157
158error:
159 ret = -1;
160
161 /* Remove the new entries in dirs */
162 while (dirs->len > init_dirs_len) {
163 g_ptr_array_remove_index(dirs, init_dirs_len);
164 }
165
166end:
167 return ret;
168}
290725f7 169
ad96d936
PP
170BT_HIDDEN
171bool bt_common_colors_supported(void)
290725f7
PP
172{
173 static bool supports_colors = false;
174 static bool supports_colors_set = false;
175 const char *term;
176
177 if (supports_colors_set) {
178 goto end;
179 }
180
181 supports_colors_set = true;
182
183 term = getenv("TERM");
184 if (!term) {
185 goto end;
186 }
187
188 if (strncmp(term, "xterm", 5) != 0 &&
189 strncmp(term, "rxvt", 4) != 0 &&
190 strncmp(term, "konsole", 7) != 0 &&
69c72672
PP
191 strncmp(term, "gnome", 5) != 0 &&
192 strncmp(term, "screen", 5) != 0 &&
193 strncmp(term, "tmux", 4) != 0 &&
194 strncmp(term, "putty", 5) != 0) {
290725f7
PP
195 goto end;
196 }
197
198 if (!isatty(1)) {
199 goto end;
200 }
201
202 supports_colors = true;
203
204end:
205 return supports_colors;
206}
207
208BT_HIDDEN
209const char *bt_common_color_reset(void)
210{
ad96d936 211 return bt_common_colors_supported() ? BT_COMMON_COLOR_RESET : "";
290725f7
PP
212}
213
214BT_HIDDEN
215const char *bt_common_color_bold(void)
216{
ad96d936 217 return bt_common_colors_supported() ? BT_COMMON_COLOR_BOLD : "";
290725f7
PP
218}
219
220BT_HIDDEN
221const char *bt_common_color_fg_default(void)
222{
ad96d936 223 return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_DEFAULT : "";
290725f7
PP
224}
225
226BT_HIDDEN
227const char *bt_common_color_fg_red(void)
228{
ad96d936 229 return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_RED : "";
290725f7
PP
230}
231
232BT_HIDDEN
233const char *bt_common_color_fg_green(void)
234{
ad96d936 235 return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_GREEN : "";
290725f7
PP
236}
237
238BT_HIDDEN
239const char *bt_common_color_fg_yellow(void)
240{
ad96d936 241 return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_YELLOW : "";
290725f7
PP
242}
243
244BT_HIDDEN
245const char *bt_common_color_fg_blue(void)
246{
ad96d936 247 return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_BLUE : "";
290725f7
PP
248}
249
250BT_HIDDEN
251const char *bt_common_color_fg_magenta(void)
252{
ad96d936 253 return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_MAGENTA : "";
290725f7
PP
254}
255
256BT_HIDDEN
257const char *bt_common_color_fg_cyan(void)
258{
ad96d936 259 return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_CYAN : "";
290725f7
PP
260}
261
262BT_HIDDEN
263const char *bt_common_color_fg_light_gray(void)
264{
ad96d936
PP
265 return bt_common_colors_supported() ?
266 BT_COMMON_COLOR_FG_LIGHT_GRAY : "";
290725f7
PP
267}
268
269BT_HIDDEN
270const char *bt_common_color_bg_default(void)
271{
ad96d936 272 return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_DEFAULT : "";
290725f7
PP
273}
274
275BT_HIDDEN
276const char *bt_common_color_bg_red(void)
277{
ad96d936 278 return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_RED : "";
290725f7
PP
279}
280
281BT_HIDDEN
282const char *bt_common_color_bg_green(void)
283{
ad96d936 284 return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_GREEN : "";
290725f7
PP
285}
286
287BT_HIDDEN
288const char *bt_common_color_bg_yellow(void)
289{
ad96d936 290 return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_YELLOW : "";
290725f7
PP
291}
292
293BT_HIDDEN
294const char *bt_common_color_bg_blue(void)
295{
ad96d936 296 return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_BLUE : "";
290725f7
PP
297}
298
299BT_HIDDEN
300const char *bt_common_color_bg_magenta(void)
301{
ad96d936 302 return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_MAGENTA : "";
290725f7
PP
303}
304
305BT_HIDDEN
306const char *bt_common_color_bg_cyan(void)
307{
ad96d936 308 return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_CYAN : "";
290725f7
PP
309}
310
311BT_HIDDEN
312const char *bt_common_color_bg_light_gray(void)
313{
ad96d936
PP
314 return bt_common_colors_supported() ?
315 BT_COMMON_COLOR_BG_LIGHT_GRAY : "";
290725f7 316}
db0f160a
PP
317
318BT_HIDDEN
319GString *bt_common_string_until(const char *input, const char *escapable_chars,
320 const char *end_chars, size_t *end_pos)
321{
322 GString *output = g_string_new(NULL);
323 const char *ch;
324 const char *es_char;
325 const char *end_char;
326
327 if (!output) {
328 goto error;
329 }
330
331 for (ch = input; *ch != '\0'; ch++) {
332 if (*ch == '\\') {
333 bool continue_loop = false;
334
335 if (ch[1] == '\0') {
336 /* `\` at the end of the string: append `\` */
337 g_string_append_c(output, *ch);
338 ch++;
339 goto set_end_pos;
340 }
341
342 for (es_char = escapable_chars; *es_char != '\0'; es_char++) {
343 if (ch[1] == *es_char) {
344 /*
345 * `\` followed by an escapable
346 * character: append the escaped
347 * character only.
348 */
349 g_string_append_c(output, ch[1]);
350 ch++;
351 continue_loop = true;
352 break;
353 }
354 }
355
356 if (continue_loop) {
357 continue;
358 }
359
360 /*
361 * `\` followed by a non-escapable character:
362 * append `\` and the character.
363 */
364 g_string_append_c(output, *ch);
365 g_string_append_c(output, ch[1]);
366 ch++;
367 continue;
368 } else {
369 for (end_char = end_chars; *end_char != '\0'; end_char++) {
370 if (*ch == *end_char) {
371 /*
372 * End character found:
373 * terminate this loop.
374 */
375 goto set_end_pos;
376 }
377 }
378
379 /* Normal character: append */
380 g_string_append_c(output, *ch);
381 }
382 }
383
384set_end_pos:
385 if (end_pos) {
386 *end_pos = ch - input;
387 }
388
389 goto end;
390
391error:
392 if (output) {
393 g_string_free(output, TRUE);
394 }
395
396end:
397 return output;
398}
399
400BT_HIDDEN
401GString *bt_common_shell_quote(const char *input)
402{
403 GString *output = g_string_new(NULL);
404 const char *ch;
405 bool no_quote = true;
406
407 if (!output) {
408 goto end;
409 }
410
411 if (strlen(input) == 0) {
412 g_string_assign(output, "''");
413 goto end;
414 }
415
416 for (ch = input; *ch != '\0'; ch++) {
417 const char c = *ch;
418
419 if (!g_ascii_isalpha(c) && !g_ascii_isdigit(c) && c != '_' &&
420 c != '@' && c != '%' && c != '+' && c != '=' &&
421 c != ':' && c != ',' && c != '.' && c != '/' &&
422 c != '-') {
423 no_quote = false;
424 break;
425 }
426 }
427
428 if (no_quote) {
429 g_string_assign(output, input);
430 goto end;
431 }
432
433 g_string_assign(output, "'");
434
435 for (ch = input; *ch != '\0'; ch++) {
436 if (*ch == '\'') {
437 g_string_append(output, "'\"'\"'");
438 } else {
439 g_string_append_c(output, *ch);
440 }
441 }
442
443 g_string_append_c(output, '\'');
444
445end:
446 return output;
447}
448
449BT_HIDDEN
450bool bt_common_string_is_printable(const char *input)
451{
452 const char *ch;
453 bool printable = true;
454 assert(input);
455
456 for (ch = input; *ch != '\0'; ch++) {
457 if (!isprint(*ch) && *ch != '\n' && *ch != '\r' &&
458 *ch != '\t' && *ch != '\v') {
459 printable = false;
460 goto end;
461 }
462 }
463
464end:
465 return printable;
466}
467
468BT_HIDDEN
469void bt_common_destroy_lttng_live_url_parts(
470 struct bt_common_lttng_live_url_parts *parts)
471{
472 if (!parts) {
473 goto end;
474 }
475
476 if (parts->proto) {
477 g_string_free(parts->proto, TRUE);
478 parts->proto = NULL;
479 }
480
481 if (parts->hostname) {
482 g_string_free(parts->hostname, TRUE);
483 parts->hostname = NULL;
484 }
485
486 if (parts->target_hostname) {
487 g_string_free(parts->target_hostname, TRUE);
488 parts->target_hostname = NULL;
489 }
490
491 if (parts->session_name) {
492 g_string_free(parts->session_name, TRUE);
493 parts->session_name = NULL;
494 }
495
496end:
497 return;
498}
499
500BT_HIDDEN
501struct bt_common_lttng_live_url_parts bt_common_parse_lttng_live_url(
502 const char *url, char *error_buf, size_t error_buf_size)
503{
504 struct bt_common_lttng_live_url_parts parts;
505 const char *at = url;
506 size_t end_pos;
507
508 assert(url);
509 memset(&parts, 0, sizeof(parts));
510 parts.port = -1;
511
512 /* Protocol */
513 parts.proto = bt_common_string_until(at, "", ":", &end_pos);
514 if (!parts.proto || parts.proto->len == 0) {
515 if (error_buf) {
516 snprintf(error_buf, error_buf_size, "Missing protocol");
517 }
518
519 goto error;
520 }
521
522 if (strcmp(parts.proto->str, "net") == 0) {
523 g_string_assign(parts.proto, "net4");
524 }
525
526 if (strcmp(parts.proto->str, "net4") != 0 &&
527 strcmp(parts.proto->str, "net6") != 0) {
528 if (error_buf) {
529 snprintf(error_buf, error_buf_size,
530 "Unknown protocol: `%s`", parts.proto->str);
531 }
532
533 goto error;
534 }
535
536 if (at[end_pos] != ':') {
537 if (error_buf) {
538 snprintf(error_buf, error_buf_size,
539 "Expecting `:` after `%s`", parts.proto->str);
540 }
541
542 goto error;
543 }
544
545 at += end_pos;
546
547 /* :// */
548 if (strncmp(at, "://", 3) != 0) {
549 if (error_buf) {
550 snprintf(error_buf, error_buf_size,
551 "Expecting `://` after protocol");
552 }
553
554 goto error;
555 }
556
557 at += 3;
558
559 /* Hostname */
560 parts.hostname = bt_common_string_until(at, "", ":/", &end_pos);
561 if (!parts.hostname || parts.hostname->len == 0) {
562 if (error_buf) {
563 snprintf(error_buf, error_buf_size, "Missing hostname");
564 }
565
566 goto error;
567 }
568
569 if (at[end_pos] == ':') {
570 /* Port */
571 GString *port;
572
573 at += end_pos + 1;
574 port = bt_common_string_until(at, "", "/", &end_pos);
575 if (!port || port->len == 0) {
576 if (error_buf) {
577 snprintf(error_buf, error_buf_size, "Missing port");
578 }
579
580 goto error;
581 }
582
583 if (sscanf(port->str, "%d", &parts.port) != 1) {
584 if (error_buf) {
585 snprintf(error_buf, error_buf_size,
586 "Invalid port: `%s`", port->str);
587 }
588
589 g_string_free(port, TRUE);
590 goto error;
591 }
592
593 g_string_free(port, TRUE);
594
595 if (parts.port < 0 || parts.port >= 65536) {
596 if (error_buf) {
597 snprintf(error_buf, error_buf_size,
598 "Invalid port: %d", parts.port);
599 }
600
601 goto error;
602 }
603 }
604
605 if (at[end_pos] == '\0') {
606 goto end;
607 }
608
609 at += end_pos;
610
611 /* /host/ */
612 if (strncmp(at, "/host/", 6) != 0) {
613 if (error_buf) {
614 snprintf(error_buf, error_buf_size,
615 "Expecting `/host/` after hostname or port");
616 }
617
618 goto error;
619 }
620
621 at += 6;
622
623 /* Target hostname */
624 parts.target_hostname = bt_common_string_until(at, "", "/", &end_pos);
625 if (!parts.target_hostname || parts.target_hostname->len == 0) {
626 if (error_buf) {
627 snprintf(error_buf, error_buf_size,
628 "Missing target hostname");
629 }
630
631 goto error;
632 }
633
634 if (at[end_pos] == '\0') {
635 goto end;
636 }
637
638 at += end_pos + 1;
639
640 /* Session name */
641 parts.session_name = bt_common_string_until(at, "", "/", &end_pos);
642 if (!parts.session_name || parts.session_name->len == 0) {
643 if (error_buf) {
644 snprintf(error_buf, error_buf_size,
645 "Missing session name");
646 }
647
648 goto error;
649 }
650
651 if (at[end_pos] == '/') {
652 if (error_buf) {
653 snprintf(error_buf, error_buf_size,
654 "Unexpected `/` after session name (`%s`)",
655 parts.session_name->str);
656 }
657
658 goto error;
659 }
660
661 goto end;
662
663error:
664 bt_common_destroy_lttng_live_url_parts(&parts);
665
666end:
667 return parts;
668}
This page took 0.047984 seconds and 4 git commands to generate.