Add bt_plugin_create_from_name()
[babeltrace.git] / common / common.c
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
25 #include <string.h>
26 #include <sys/types.h>
27 #include <pwd.h>
28 #include <unistd.h>
29 #include <assert.h>
30 #include <glib.h>
31 #include <babeltrace/babeltrace-internal.h>
32
33 #define SYSTEM_PLUGIN_PATH INSTALL_LIBDIR "/babeltrace/plugins"
34 #define HOME_ENV_VAR "HOME"
35 #define HOME_PLUGIN_SUBPATH "/.local/lib/babeltrace/plugins"
36
37 BT_HIDDEN
38 const char *bt_common_get_system_plugin_path(void)
39 {
40 return SYSTEM_PLUGIN_PATH;
41 }
42
43 BT_HIDDEN
44 bool bt_common_is_setuid_setgid(void)
45 {
46 return (geteuid() != getuid() || getegid() != getgid());
47 }
48
49 static char *bt_secure_getenv(const char *name)
50 {
51 if (bt_common_is_setuid_setgid()) {
52 printf_error("Disregarding %s environment variable for setuid/setgid binary",
53 name);
54 return NULL;
55 }
56 return getenv(name);
57 }
58
59 static const char *get_home_dir(void)
60 {
61 char *val = NULL;
62 struct passwd *pwd;
63
64 val = bt_secure_getenv(HOME_ENV_VAR);
65 if (val) {
66 goto end;
67 }
68 /* Fallback on password file. */
69 pwd = getpwuid(getuid());
70 if (!pwd) {
71 goto end;
72 }
73 val = pwd->pw_dir;
74 end:
75 return val;
76 }
77
78 BT_HIDDEN
79 char *bt_common_get_home_plugin_path(void)
80 {
81 char *path = NULL;
82 const char *home_dir;
83
84 home_dir = get_home_dir();
85 if (!home_dir) {
86 goto end;
87 }
88
89 if (strlen(home_dir) + strlen(HOME_PLUGIN_SUBPATH) + 1 >= PATH_MAX) {
90 printf_error("Home directory path is too long: `%s`\n",
91 home_dir);
92 goto end;
93 }
94
95 path = malloc(PATH_MAX);
96 if (!path) {
97 goto end;
98 }
99
100 strcpy(path, home_dir);
101 strcat(path, HOME_PLUGIN_SUBPATH);
102
103 end:
104 return path;
105 }
106
107 BT_HIDDEN
108 int bt_common_append_plugin_path_dirs(const char *paths, GPtrArray *dirs)
109 {
110 int ret = 0;
111 const char *at;
112 const char *end;
113 size_t init_dirs_len;
114
115 assert(dirs);
116 init_dirs_len = dirs->len;
117
118 if (!paths) {
119 /* Nothing to append */
120 goto end;
121 }
122
123 at = paths;
124 end = paths + strlen(paths);
125
126 while (at < end) {
127 GString *path;
128 const char *next_colon;
129
130 next_colon = strchr(at, ':');
131 if (next_colon == at) {
132 /*
133 * Empty path: try next character (supported
134 * to conform to the typical parsing of $PATH).
135 */
136 at++;
137 continue;
138 } else if (!next_colon) {
139 /* No more colon: use the remaining */
140 next_colon = paths + strlen(paths);
141 }
142
143 path = g_string_new(NULL);
144 if (!path) {
145 goto error;
146 }
147
148 g_string_append_len(path, at, next_colon - at);
149 at = next_colon + 1;
150 g_ptr_array_add(dirs, path);
151 }
152
153 goto end;
154
155 error:
156 ret = -1;
157
158 /* Remove the new entries in dirs */
159 while (dirs->len > init_dirs_len) {
160 g_ptr_array_remove_index(dirs, init_dirs_len);
161 }
162
163 end:
164 return ret;
165 }
This page took 0.033443 seconds and 4 git commands to generate.