Commit | Line | Data |
---|---|---|
f3ed775e DG |
1 | /* |
2 | * Copyright (c) 2011 David Goulet <david.goulet@polymtl.ca> | |
3 | * | |
d14d33bf AM |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License, version 2 only, | |
6 | * as published by the Free Software Foundation. | |
f3ed775e DG |
7 | * |
8 | * This program is distributed in the hope that it will be useful, | |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
11 | * GNU General Public License for more details. | |
12 | * | |
13 | * You should have received a copy of the GNU General Public License along | |
14 | * with this program; if not, write to the Free Software Foundation, Inc., | |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. | |
16 | */ | |
17 | ||
4c462e79 | 18 | #define _GNU_SOURCE |
6c1c0768 | 19 | #define _LGPL_SOURCE |
b9dfb167 | 20 | #include <assert.h> |
f3ed775e | 21 | #include <stdlib.h> |
679b4943 | 22 | #include <ctype.h> |
3badf2bf | 23 | #include <limits.h> |
8960e9cd DG |
24 | #include <sys/types.h> |
25 | #include <sys/socket.h> | |
26 | #include <signal.h> | |
27 | #include <netinet/in.h> | |
28 | #include <arpa/inet.h> | |
f3ed775e | 29 | |
db758600 | 30 | #include <common/error.h> |
feb0f3e5 | 31 | #include <common/utils.h> |
f3ed775e | 32 | |
beb8c75a | 33 | #include "conf.h" |
679b4943 | 34 | #include "utils.h" |
3c9bd23c | 35 | #include "command.h" |
f3ed775e | 36 | |
b9dfb167 DG |
37 | static const char *str_kernel = "Kernel"; |
38 | static const char *str_ust = "UST"; | |
39 | static const char *str_jul = "JUL"; | |
5cdb6027 | 40 | static const char *str_log4j = "LOG4J"; |
0e115563 | 41 | static const char *str_python = "Python"; |
b9dfb167 | 42 | |
1dac0189 PPM |
43 | static |
44 | char *_get_session_name(int quiet) | |
f3ed775e DG |
45 | { |
46 | char *path, *session_name = NULL; | |
47 | ||
48 | /* Get path to config file */ | |
feb0f3e5 | 49 | path = utils_get_home_dir(); |
f3ed775e DG |
50 | if (path == NULL) { |
51 | goto error; | |
52 | } | |
53 | ||
54 | /* Get session name from config */ | |
1dac0189 PPM |
55 | session_name = quiet ? config_read_session_name_quiet(path) : |
56 | config_read_session_name(path); | |
f3ed775e | 57 | if (session_name == NULL) { |
58a97671 | 58 | goto error; |
f3ed775e DG |
59 | } |
60 | ||
3183dbb0 | 61 | DBG2("Config file path found: %s", path); |
cd80958d | 62 | DBG("Session name found: %s", session_name); |
f3ed775e | 63 | return session_name; |
3183dbb0 DG |
64 | |
65 | error: | |
66 | return NULL; | |
f3ed775e | 67 | } |
679b4943 | 68 | |
1dac0189 PPM |
69 | /* |
70 | * get_session_name | |
71 | * | |
72 | * Return allocated string with the session name found in the config | |
73 | * directory. | |
74 | */ | |
75 | char *get_session_name(void) | |
76 | { | |
77 | return _get_session_name(0); | |
78 | } | |
79 | ||
80 | /* | |
81 | * get_session_name_quiet (no warnings/errors emitted) | |
82 | * | |
83 | * Return allocated string with the session name found in the config | |
84 | * directory. | |
85 | */ | |
86 | char *get_session_name_quiet(void) | |
87 | { | |
88 | return _get_session_name(1); | |
89 | } | |
90 | ||
3c9bd23c SM |
91 | /* |
92 | * list_commands | |
93 | * | |
94 | * List commands line by line. This is mostly for bash auto completion and to | |
95 | * avoid difficult parsing. | |
96 | */ | |
97 | void list_commands(struct cmd_struct *commands, FILE *ofp) | |
98 | { | |
99 | int i = 0; | |
100 | struct cmd_struct *cmd = NULL; | |
101 | ||
102 | cmd = &commands[i]; | |
103 | while (cmd->name != NULL) { | |
104 | fprintf(ofp, "%s\n", cmd->name); | |
105 | i++; | |
106 | cmd = &commands[i]; | |
107 | } | |
108 | } | |
679b4943 SM |
109 | |
110 | /* | |
111 | * list_cmd_options | |
112 | * | |
113 | * Prints a simple list of the options available to a command. This is intended | |
114 | * to be easily parsed for bash completion. | |
115 | */ | |
116 | void list_cmd_options(FILE *ofp, struct poptOption *options) | |
117 | { | |
118 | int i; | |
119 | struct poptOption *option = NULL; | |
120 | ||
121 | for (i = 0; options[i].longName != NULL; i++) { | |
122 | option = &options[i]; | |
123 | ||
124 | fprintf(ofp, "--%s\n", option->longName); | |
125 | ||
126 | if (isprint(option->shortName)) { | |
127 | fprintf(ofp, "-%c\n", option->shortName); | |
128 | } | |
129 | } | |
130 | } | |
8ce58bad MD |
131 | |
132 | /* | |
133 | * fls: returns the position of the most significant bit. | |
134 | * Returns 0 if no bit is set, else returns the position of the most | |
135 | * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit). | |
136 | */ | |
137 | #if defined(__i386) || defined(__x86_64) | |
138 | static inline | |
139 | unsigned int fls_u32(uint32_t x) | |
140 | { | |
141 | int r; | |
142 | ||
143 | asm("bsrl %1,%0\n\t" | |
144 | "jnz 1f\n\t" | |
145 | "movl $-1,%0\n\t" | |
146 | "1:\n\t" | |
147 | : "=r" (r) : "rm" (x)); | |
148 | return r + 1; | |
149 | } | |
150 | #define HAS_FLS_U32 | |
151 | #endif | |
152 | ||
153 | #if defined(__x86_64) | |
154 | static inline | |
155 | unsigned int fls_u64(uint64_t x) | |
156 | { | |
157 | long r; | |
158 | ||
159 | asm("bsrq %1,%0\n\t" | |
160 | "jnz 1f\n\t" | |
161 | "movq $-1,%0\n\t" | |
162 | "1:\n\t" | |
163 | : "=r" (r) : "rm" (x)); | |
164 | return r + 1; | |
165 | } | |
166 | #define HAS_FLS_U64 | |
167 | #endif | |
168 | ||
169 | #ifndef HAS_FLS_U64 | |
170 | static __attribute__((unused)) | |
171 | unsigned int fls_u64(uint64_t x) | |
172 | { | |
173 | unsigned int r = 64; | |
174 | ||
175 | if (!x) | |
176 | return 0; | |
177 | ||
178 | if (!(x & 0xFFFFFFFF00000000ULL)) { | |
179 | x <<= 32; | |
180 | r -= 32; | |
181 | } | |
182 | if (!(x & 0xFFFF000000000000ULL)) { | |
183 | x <<= 16; | |
184 | r -= 16; | |
185 | } | |
186 | if (!(x & 0xFF00000000000000ULL)) { | |
187 | x <<= 8; | |
188 | r -= 8; | |
189 | } | |
190 | if (!(x & 0xF000000000000000ULL)) { | |
191 | x <<= 4; | |
192 | r -= 4; | |
193 | } | |
194 | if (!(x & 0xC000000000000000ULL)) { | |
195 | x <<= 2; | |
196 | r -= 2; | |
197 | } | |
198 | if (!(x & 0x8000000000000000ULL)) { | |
199 | x <<= 1; | |
200 | r -= 1; | |
201 | } | |
202 | return r; | |
203 | } | |
204 | #endif | |
205 | ||
206 | #ifndef HAS_FLS_U32 | |
207 | static __attribute__((unused)) | |
208 | unsigned int fls_u32(uint32_t x) | |
209 | { | |
210 | unsigned int r = 32; | |
211 | ||
212 | if (!x) | |
213 | return 0; | |
214 | if (!(x & 0xFFFF0000U)) { | |
215 | x <<= 16; | |
216 | r -= 16; | |
217 | } | |
218 | if (!(x & 0xFF000000U)) { | |
219 | x <<= 8; | |
220 | r -= 8; | |
221 | } | |
222 | if (!(x & 0xF0000000U)) { | |
223 | x <<= 4; | |
224 | r -= 4; | |
225 | } | |
226 | if (!(x & 0xC0000000U)) { | |
227 | x <<= 2; | |
228 | r -= 2; | |
229 | } | |
230 | if (!(x & 0x80000000U)) { | |
231 | x <<= 1; | |
232 | r -= 1; | |
233 | } | |
234 | return r; | |
235 | } | |
236 | #endif | |
237 | ||
238 | static | |
239 | unsigned int fls_ulong(unsigned long x) | |
240 | { | |
241 | #if (CAA_BITS_PER_LONG == 32) | |
242 | return fls_u32(x); | |
243 | #else | |
244 | return fls_u64(x); | |
245 | #endif | |
246 | } | |
247 | ||
248 | /* | |
249 | * Return the minimum order for which x <= (1UL << order). | |
250 | * Return -1 if x is 0. | |
251 | */ | |
252 | int get_count_order_u32(uint32_t x) | |
253 | { | |
254 | if (!x) | |
255 | return -1; | |
256 | ||
257 | return fls_u32(x - 1); | |
258 | } | |
259 | ||
260 | /* | |
261 | * Return the minimum order for which x <= (1UL << order). | |
262 | * Return -1 if x is 0. | |
263 | */ | |
264 | int get_count_order_u64(uint64_t x) | |
265 | { | |
266 | if (!x) | |
267 | return -1; | |
268 | ||
269 | return fls_u64(x - 1); | |
270 | } | |
271 | ||
272 | /* | |
273 | * Return the minimum order for which x <= (1UL << order). | |
274 | * Return -1 if x is 0. | |
275 | */ | |
276 | int get_count_order_ulong(unsigned long x) | |
277 | { | |
278 | if (!x) | |
279 | return -1; | |
280 | ||
281 | return fls_ulong(x - 1); | |
282 | } | |
b9dfb167 DG |
283 | |
284 | const char *get_domain_str(enum lttng_domain_type domain) | |
285 | { | |
286 | const char *str_dom; | |
287 | ||
288 | switch (domain) { | |
289 | case LTTNG_DOMAIN_KERNEL: | |
290 | str_dom = str_kernel; | |
291 | break; | |
292 | case LTTNG_DOMAIN_UST: | |
293 | str_dom = str_ust; | |
294 | break; | |
295 | case LTTNG_DOMAIN_JUL: | |
296 | str_dom = str_jul; | |
297 | break; | |
5cdb6027 DG |
298 | case LTTNG_DOMAIN_LOG4J: |
299 | str_dom = str_log4j; | |
300 | break; | |
0e115563 DG |
301 | case LTTNG_DOMAIN_PYTHON: |
302 | str_dom = str_python; | |
303 | break; | |
b9dfb167 DG |
304 | default: |
305 | /* Should not have an unknown domain or else define it. */ | |
306 | assert(0); | |
307 | } | |
308 | ||
309 | return str_dom; | |
310 | } | |
8960e9cd DG |
311 | |
312 | /* | |
313 | * Spawn a lttng relayd daemon by forking and execv. | |
314 | */ | |
315 | int spawn_relayd(const char *pathname, int port) | |
316 | { | |
317 | int ret = 0; | |
318 | pid_t pid; | |
319 | char url[255]; | |
320 | ||
321 | if (!port) { | |
322 | port = DEFAULT_NETWORK_VIEWER_PORT; | |
323 | } | |
324 | ||
325 | ret = snprintf(url, sizeof(url), "tcp://localhost:%d", port); | |
326 | if (ret < 0) { | |
327 | goto end; | |
328 | } | |
329 | ||
330 | MSG("Spawning a relayd daemon"); | |
331 | pid = fork(); | |
332 | if (pid == 0) { | |
333 | /* | |
334 | * Spawn session daemon and tell | |
335 | * it to signal us when ready. | |
336 | */ | |
337 | execlp(pathname, "lttng-relayd", "-L", url, NULL); | |
338 | /* execlp only returns if error happened */ | |
339 | if (errno == ENOENT) { | |
340 | ERR("No relayd found. Use --relayd-path."); | |
341 | } else { | |
6f04ed72 | 342 | PERROR("execlp"); |
8960e9cd DG |
343 | } |
344 | kill(getppid(), SIGTERM); /* wake parent */ | |
345 | exit(EXIT_FAILURE); | |
346 | } else if (pid > 0) { | |
347 | goto end; | |
348 | } else { | |
6f04ed72 | 349 | PERROR("fork"); |
8960e9cd DG |
350 | ret = -1; |
351 | goto end; | |
352 | } | |
353 | ||
354 | end: | |
355 | return ret; | |
356 | } | |
357 | ||
358 | /* | |
359 | * Check if relayd is alive. | |
360 | * | |
361 | * Return 1 if found else 0 if NOT found. Negative value on error. | |
362 | */ | |
363 | int check_relayd(void) | |
364 | { | |
365 | int ret, fd; | |
366 | struct sockaddr_in sin; | |
367 | ||
368 | fd = socket(AF_INET, SOCK_STREAM, 0); | |
369 | if (fd < 0) { | |
6f04ed72 | 370 | PERROR("socket check relayd"); |
dd02a4c1 DG |
371 | ret = -1; |
372 | goto error_socket; | |
8960e9cd DG |
373 | } |
374 | ||
375 | sin.sin_family = AF_INET; | |
376 | sin.sin_port = htons(DEFAULT_NETWORK_VIEWER_PORT); | |
377 | ret = inet_pton(sin.sin_family, "127.0.0.1", &sin.sin_addr); | |
378 | if (ret < 1) { | |
6f04ed72 | 379 | PERROR("inet_pton check relayd"); |
dd02a4c1 | 380 | ret = -1; |
8960e9cd DG |
381 | goto error; |
382 | } | |
383 | ||
384 | /* | |
385 | * A successful connect means the relayd exists thus returning 0 else a | |
386 | * negative value means it does NOT exists. | |
387 | */ | |
388 | ret = connect(fd, &sin, sizeof(sin)); | |
389 | if (ret < 0) { | |
390 | /* Not found. */ | |
391 | ret = 0; | |
392 | } else { | |
393 | /* Already spawned. */ | |
394 | ret = 1; | |
395 | } | |
396 | ||
8960e9cd | 397 | error: |
dd02a4c1 | 398 | if (close(fd) < 0) { |
6f04ed72 | 399 | PERROR("close relayd fd"); |
dd02a4c1 DG |
400 | } |
401 | error_socket: | |
402 | return ret; | |
8960e9cd | 403 | } |