Fix: missing reference release in field reset
[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>
31#include <glib.h>
32#include <babeltrace/babeltrace-internal.h>
ad96d936 33#include <babeltrace/common-internal.h>
1670bffd
PP
34
35#define SYSTEM_PLUGIN_PATH INSTALL_LIBDIR "/babeltrace/plugins"
36#define HOME_ENV_VAR "HOME"
37#define HOME_PLUGIN_SUBPATH "/.local/lib/babeltrace/plugins"
38
39BT_HIDDEN
40const char *bt_common_get_system_plugin_path(void)
41{
42 return SYSTEM_PLUGIN_PATH;
43}
44
45BT_HIDDEN
46bool bt_common_is_setuid_setgid(void)
47{
48 return (geteuid() != getuid() || getegid() != getgid());
49}
50
51static char *bt_secure_getenv(const char *name)
52{
53 if (bt_common_is_setuid_setgid()) {
54 printf_error("Disregarding %s environment variable for setuid/setgid binary",
55 name);
56 return NULL;
57 }
58 return getenv(name);
59}
60
61static const char *get_home_dir(void)
62{
63 char *val = NULL;
64 struct passwd *pwd;
65
66 val = bt_secure_getenv(HOME_ENV_VAR);
67 if (val) {
68 goto end;
69 }
70 /* Fallback on password file. */
71 pwd = getpwuid(getuid());
72 if (!pwd) {
73 goto end;
74 }
75 val = pwd->pw_dir;
76end:
77 return val;
78}
79
80BT_HIDDEN
81char *bt_common_get_home_plugin_path(void)
82{
83 char *path = NULL;
84 const char *home_dir;
85
86 home_dir = get_home_dir();
87 if (!home_dir) {
88 goto end;
89 }
90
91 if (strlen(home_dir) + strlen(HOME_PLUGIN_SUBPATH) + 1 >= PATH_MAX) {
92 printf_error("Home directory path is too long: `%s`\n",
93 home_dir);
94 goto end;
95 }
96
97 path = malloc(PATH_MAX);
98 if (!path) {
99 goto end;
100 }
101
102 strcpy(path, home_dir);
103 strcat(path, HOME_PLUGIN_SUBPATH);
104
105end:
106 return path;
107}
108
109BT_HIDDEN
110int bt_common_append_plugin_path_dirs(const char *paths, GPtrArray *dirs)
111{
112 int ret = 0;
113 const char *at;
114 const char *end;
115 size_t init_dirs_len;
116
117 assert(dirs);
118 init_dirs_len = dirs->len;
119
120 if (!paths) {
121 /* Nothing to append */
122 goto end;
123 }
124
125 at = paths;
126 end = paths + strlen(paths);
127
128 while (at < end) {
129 GString *path;
130 const char *next_colon;
131
132 next_colon = strchr(at, ':');
133 if (next_colon == at) {
134 /*
135 * Empty path: try next character (supported
136 * to conform to the typical parsing of $PATH).
137 */
138 at++;
139 continue;
140 } else if (!next_colon) {
141 /* No more colon: use the remaining */
142 next_colon = paths + strlen(paths);
143 }
144
145 path = g_string_new(NULL);
146 if (!path) {
147 goto error;
148 }
149
150 g_string_append_len(path, at, next_colon - at);
151 at = next_colon + 1;
152 g_ptr_array_add(dirs, path);
153 }
154
155 goto end;
156
157error:
158 ret = -1;
159
160 /* Remove the new entries in dirs */
161 while (dirs->len > init_dirs_len) {
162 g_ptr_array_remove_index(dirs, init_dirs_len);
163 }
164
165end:
166 return ret;
167}
290725f7 168
ad96d936
PP
169BT_HIDDEN
170bool bt_common_colors_supported(void)
290725f7
PP
171{
172 static bool supports_colors = false;
173 static bool supports_colors_set = false;
174 const char *term;
175
176 if (supports_colors_set) {
177 goto end;
178 }
179
180 supports_colors_set = true;
181
182 term = getenv("TERM");
183 if (!term) {
184 goto end;
185 }
186
187 if (strncmp(term, "xterm", 5) != 0 &&
188 strncmp(term, "rxvt", 4) != 0 &&
189 strncmp(term, "konsole", 7) != 0 &&
69c72672
PP
190 strncmp(term, "gnome", 5) != 0 &&
191 strncmp(term, "screen", 5) != 0 &&
192 strncmp(term, "tmux", 4) != 0 &&
193 strncmp(term, "putty", 5) != 0) {
290725f7
PP
194 goto end;
195 }
196
197 if (!isatty(1)) {
198 goto end;
199 }
200
201 supports_colors = true;
202
203end:
204 return supports_colors;
205}
206
207BT_HIDDEN
208const char *bt_common_color_reset(void)
209{
ad96d936 210 return bt_common_colors_supported() ? BT_COMMON_COLOR_RESET : "";
290725f7
PP
211}
212
213BT_HIDDEN
214const char *bt_common_color_bold(void)
215{
ad96d936 216 return bt_common_colors_supported() ? BT_COMMON_COLOR_BOLD : "";
290725f7
PP
217}
218
219BT_HIDDEN
220const char *bt_common_color_fg_default(void)
221{
ad96d936 222 return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_DEFAULT : "";
290725f7
PP
223}
224
225BT_HIDDEN
226const char *bt_common_color_fg_red(void)
227{
ad96d936 228 return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_RED : "";
290725f7
PP
229}
230
231BT_HIDDEN
232const char *bt_common_color_fg_green(void)
233{
ad96d936 234 return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_GREEN : "";
290725f7
PP
235}
236
237BT_HIDDEN
238const char *bt_common_color_fg_yellow(void)
239{
ad96d936 240 return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_YELLOW : "";
290725f7
PP
241}
242
243BT_HIDDEN
244const char *bt_common_color_fg_blue(void)
245{
ad96d936 246 return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_BLUE : "";
290725f7
PP
247}
248
249BT_HIDDEN
250const char *bt_common_color_fg_magenta(void)
251{
ad96d936 252 return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_MAGENTA : "";
290725f7
PP
253}
254
255BT_HIDDEN
256const char *bt_common_color_fg_cyan(void)
257{
ad96d936 258 return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_CYAN : "";
290725f7
PP
259}
260
261BT_HIDDEN
262const char *bt_common_color_fg_light_gray(void)
263{
ad96d936
PP
264 return bt_common_colors_supported() ?
265 BT_COMMON_COLOR_FG_LIGHT_GRAY : "";
290725f7
PP
266}
267
268BT_HIDDEN
269const char *bt_common_color_bg_default(void)
270{
ad96d936 271 return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_DEFAULT : "";
290725f7
PP
272}
273
274BT_HIDDEN
275const char *bt_common_color_bg_red(void)
276{
ad96d936 277 return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_RED : "";
290725f7
PP
278}
279
280BT_HIDDEN
281const char *bt_common_color_bg_green(void)
282{
ad96d936 283 return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_GREEN : "";
290725f7
PP
284}
285
286BT_HIDDEN
287const char *bt_common_color_bg_yellow(void)
288{
ad96d936 289 return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_YELLOW : "";
290725f7
PP
290}
291
292BT_HIDDEN
293const char *bt_common_color_bg_blue(void)
294{
ad96d936 295 return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_BLUE : "";
290725f7
PP
296}
297
298BT_HIDDEN
299const char *bt_common_color_bg_magenta(void)
300{
ad96d936 301 return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_MAGENTA : "";
290725f7
PP
302}
303
304BT_HIDDEN
305const char *bt_common_color_bg_cyan(void)
306{
ad96d936 307 return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_CYAN : "";
290725f7
PP
308}
309
310BT_HIDDEN
311const char *bt_common_color_bg_light_gray(void)
312{
ad96d936
PP
313 return bt_common_colors_supported() ?
314 BT_COMMON_COLOR_BG_LIGHT_GRAY : "";
290725f7 315}
This page took 0.035492 seconds and 4 git commands to generate.