Fix: sessiond: leak of application socket on chmod failure
[lttng-tools.git] / src / bin / lttng / utils.c
CommitLineData
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
6c1c0768 18#define _LGPL_SOURCE
b9dfb167 19#include <assert.h>
f3ed775e 20#include <stdlib.h>
679b4943 21#include <ctype.h>
3badf2bf 22#include <limits.h>
8960e9cd
DG
23#include <sys/types.h>
24#include <sys/socket.h>
25#include <signal.h>
26#include <netinet/in.h>
27#include <arpa/inet.h>
20fb9e02 28#include <inttypes.h>
4ba92f18 29#include <unistd.h>
f3ed775e 30
db758600 31#include <common/error.h>
feb0f3e5 32#include <common/utils.h>
4ba92f18 33#include <common/defaults.h>
f3ed775e 34
beb8c75a 35#include "conf.h"
679b4943 36#include "utils.h"
3c9bd23c 37#include "command.h"
f3ed775e 38
b9dfb167
DG
39static const char *str_kernel = "Kernel";
40static const char *str_ust = "UST";
41static const char *str_jul = "JUL";
5cdb6027 42static const char *str_log4j = "LOG4J";
0e115563 43static const char *str_python = "Python";
4fd2697f
FD
44static const char *str_all = "ALL";
45static const char *str_tracepoint = "Tracepoint";
46static const char *str_syscall = "Syscall";
47static const char *str_probe = "Probe";
48static const char *str_userspace_probe = "Userspace Probe";
49static const char *str_function = "Function";
b9dfb167 50
1dac0189
PPM
51static
52char *_get_session_name(int quiet)
f3ed775e 53{
4f00620d
JG
54 const char *path;
55 char *session_name = NULL;
f3ed775e
DG
56
57 /* Get path to config file */
feb0f3e5 58 path = utils_get_home_dir();
f3ed775e
DG
59 if (path == NULL) {
60 goto error;
61 }
62
63 /* Get session name from config */
1dac0189
PPM
64 session_name = quiet ? config_read_session_name_quiet(path) :
65 config_read_session_name(path);
f3ed775e 66 if (session_name == NULL) {
58a97671 67 goto error;
f3ed775e
DG
68 }
69
3183dbb0 70 DBG2("Config file path found: %s", path);
cd80958d 71 DBG("Session name found: %s", session_name);
f3ed775e 72 return session_name;
3183dbb0
DG
73
74error:
75 return NULL;
f3ed775e 76}
679b4943 77
1dac0189
PPM
78/*
79 * get_session_name
80 *
81 * Return allocated string with the session name found in the config
82 * directory.
83 */
84char *get_session_name(void)
85{
86 return _get_session_name(0);
87}
88
89/*
90 * get_session_name_quiet (no warnings/errors emitted)
91 *
92 * Return allocated string with the session name found in the config
93 * directory.
94 */
95char *get_session_name_quiet(void)
96{
97 return _get_session_name(1);
98}
99
3c9bd23c
SM
100/*
101 * list_commands
102 *
103 * List commands line by line. This is mostly for bash auto completion and to
104 * avoid difficult parsing.
105 */
106void list_commands(struct cmd_struct *commands, FILE *ofp)
107{
108 int i = 0;
109 struct cmd_struct *cmd = NULL;
110
111 cmd = &commands[i];
112 while (cmd->name != NULL) {
113 fprintf(ofp, "%s\n", cmd->name);
114 i++;
115 cmd = &commands[i];
116 }
117}
679b4943
SM
118
119/*
120 * list_cmd_options
121 *
122 * Prints a simple list of the options available to a command. This is intended
123 * to be easily parsed for bash completion.
124 */
125void list_cmd_options(FILE *ofp, struct poptOption *options)
126{
127 int i;
128 struct poptOption *option = NULL;
129
130 for (i = 0; options[i].longName != NULL; i++) {
131 option = &options[i];
132
133 fprintf(ofp, "--%s\n", option->longName);
134
135 if (isprint(option->shortName)) {
136 fprintf(ofp, "-%c\n", option->shortName);
137 }
138 }
139}
8ce58bad
MD
140
141/*
142 * fls: returns the position of the most significant bit.
143 * Returns 0 if no bit is set, else returns the position of the most
144 * significant bit (from 1 to 32 on 32-bit, from 1 to 64 on 64-bit).
145 */
146#if defined(__i386) || defined(__x86_64)
147static inline
148unsigned int fls_u32(uint32_t x)
149{
150 int r;
151
152 asm("bsrl %1,%0\n\t"
153 "jnz 1f\n\t"
154 "movl $-1,%0\n\t"
155 "1:\n\t"
156 : "=r" (r) : "rm" (x));
157 return r + 1;
158}
159#define HAS_FLS_U32
160#endif
161
162#if defined(__x86_64)
163static inline
164unsigned int fls_u64(uint64_t x)
165{
166 long r;
167
168 asm("bsrq %1,%0\n\t"
169 "jnz 1f\n\t"
170 "movq $-1,%0\n\t"
171 "1:\n\t"
172 : "=r" (r) : "rm" (x));
173 return r + 1;
174}
175#define HAS_FLS_U64
176#endif
177
178#ifndef HAS_FLS_U64
179static __attribute__((unused))
180unsigned int fls_u64(uint64_t x)
181{
182 unsigned int r = 64;
183
184 if (!x)
185 return 0;
186
187 if (!(x & 0xFFFFFFFF00000000ULL)) {
188 x <<= 32;
189 r -= 32;
190 }
191 if (!(x & 0xFFFF000000000000ULL)) {
192 x <<= 16;
193 r -= 16;
194 }
195 if (!(x & 0xFF00000000000000ULL)) {
196 x <<= 8;
197 r -= 8;
198 }
199 if (!(x & 0xF000000000000000ULL)) {
200 x <<= 4;
201 r -= 4;
202 }
203 if (!(x & 0xC000000000000000ULL)) {
204 x <<= 2;
205 r -= 2;
206 }
207 if (!(x & 0x8000000000000000ULL)) {
208 x <<= 1;
209 r -= 1;
210 }
211 return r;
212}
213#endif
214
215#ifndef HAS_FLS_U32
216static __attribute__((unused))
217unsigned int fls_u32(uint32_t x)
218{
219 unsigned int r = 32;
220
221 if (!x)
222 return 0;
223 if (!(x & 0xFFFF0000U)) {
224 x <<= 16;
225 r -= 16;
226 }
227 if (!(x & 0xFF000000U)) {
228 x <<= 8;
229 r -= 8;
230 }
231 if (!(x & 0xF0000000U)) {
232 x <<= 4;
233 r -= 4;
234 }
235 if (!(x & 0xC0000000U)) {
236 x <<= 2;
237 r -= 2;
238 }
239 if (!(x & 0x80000000U)) {
240 x <<= 1;
241 r -= 1;
242 }
243 return r;
244}
245#endif
246
247static
248unsigned int fls_ulong(unsigned long x)
249{
250#if (CAA_BITS_PER_LONG == 32)
251 return fls_u32(x);
252#else
253 return fls_u64(x);
254#endif
255}
256
257/*
258 * Return the minimum order for which x <= (1UL << order).
259 * Return -1 if x is 0.
260 */
261int get_count_order_u32(uint32_t x)
262{
263 if (!x)
264 return -1;
265
266 return fls_u32(x - 1);
267}
268
269/*
270 * Return the minimum order for which x <= (1UL << order).
271 * Return -1 if x is 0.
272 */
273int get_count_order_u64(uint64_t x)
274{
275 if (!x)
276 return -1;
277
278 return fls_u64(x - 1);
279}
280
281/*
282 * Return the minimum order for which x <= (1UL << order).
283 * Return -1 if x is 0.
284 */
285int get_count_order_ulong(unsigned long x)
286{
287 if (!x)
288 return -1;
289
290 return fls_ulong(x - 1);
291}
b9dfb167
DG
292
293const char *get_domain_str(enum lttng_domain_type domain)
294{
295 const char *str_dom;
296
297 switch (domain) {
298 case LTTNG_DOMAIN_KERNEL:
299 str_dom = str_kernel;
300 break;
301 case LTTNG_DOMAIN_UST:
302 str_dom = str_ust;
303 break;
304 case LTTNG_DOMAIN_JUL:
305 str_dom = str_jul;
306 break;
5cdb6027
DG
307 case LTTNG_DOMAIN_LOG4J:
308 str_dom = str_log4j;
309 break;
0e115563
DG
310 case LTTNG_DOMAIN_PYTHON:
311 str_dom = str_python;
312 break;
b9dfb167
DG
313 default:
314 /* Should not have an unknown domain or else define it. */
315 assert(0);
316 }
317
318 return str_dom;
319}
8960e9cd 320
4fd2697f
FD
321const char *get_event_type_str(enum lttng_event_type type)
322{
323 const char *str_event_type;
324
325 switch (type) {
326 case LTTNG_EVENT_ALL:
327 str_event_type = str_all;
328 break;
329 case LTTNG_EVENT_TRACEPOINT:
330 str_event_type = str_tracepoint;
331 break;
332 case LTTNG_EVENT_SYSCALL:
333 str_event_type = str_syscall;
334 break;
335 case LTTNG_EVENT_PROBE:
336 str_event_type = str_probe;
337 break;
338 case LTTNG_EVENT_USERSPACE_PROBE:
339 str_event_type = str_userspace_probe;
340 break;
341 case LTTNG_EVENT_FUNCTION:
342 str_event_type = str_function;
343 break;
344 default:
345 /* Should not have an unknown event type or else define it. */
346 assert(0);
347 }
348
349 return str_event_type;
350}
351
8960e9cd
DG
352/*
353 * Spawn a lttng relayd daemon by forking and execv.
354 */
355int spawn_relayd(const char *pathname, int port)
356{
357 int ret = 0;
358 pid_t pid;
359 char url[255];
360
361 if (!port) {
362 port = DEFAULT_NETWORK_VIEWER_PORT;
363 }
364
365 ret = snprintf(url, sizeof(url), "tcp://localhost:%d", port);
366 if (ret < 0) {
367 goto end;
368 }
369
370 MSG("Spawning a relayd daemon");
371 pid = fork();
372 if (pid == 0) {
373 /*
374 * Spawn session daemon and tell
375 * it to signal us when ready.
376 */
377 execlp(pathname, "lttng-relayd", "-L", url, NULL);
378 /* execlp only returns if error happened */
379 if (errno == ENOENT) {
380 ERR("No relayd found. Use --relayd-path.");
381 } else {
6f04ed72 382 PERROR("execlp");
8960e9cd
DG
383 }
384 kill(getppid(), SIGTERM); /* wake parent */
385 exit(EXIT_FAILURE);
386 } else if (pid > 0) {
387 goto end;
388 } else {
6f04ed72 389 PERROR("fork");
8960e9cd
DG
390 ret = -1;
391 goto end;
392 }
393
394end:
395 return ret;
396}
397
398/*
399 * Check if relayd is alive.
400 *
401 * Return 1 if found else 0 if NOT found. Negative value on error.
402 */
403int check_relayd(void)
404{
405 int ret, fd;
406 struct sockaddr_in sin;
407
408 fd = socket(AF_INET, SOCK_STREAM, 0);
409 if (fd < 0) {
6f04ed72 410 PERROR("socket check relayd");
dd02a4c1
DG
411 ret = -1;
412 goto error_socket;
8960e9cd
DG
413 }
414
415 sin.sin_family = AF_INET;
416 sin.sin_port = htons(DEFAULT_NETWORK_VIEWER_PORT);
417 ret = inet_pton(sin.sin_family, "127.0.0.1", &sin.sin_addr);
418 if (ret < 1) {
6f04ed72 419 PERROR("inet_pton check relayd");
dd02a4c1 420 ret = -1;
8960e9cd
DG
421 goto error;
422 }
423
424 /*
425 * A successful connect means the relayd exists thus returning 0 else a
426 * negative value means it does NOT exists.
427 */
56efeab3 428 ret = connect(fd, (struct sockaddr *) &sin, sizeof(sin));
8960e9cd
DG
429 if (ret < 0) {
430 /* Not found. */
431 ret = 0;
432 } else {
433 /* Already spawned. */
434 ret = 1;
435 }
436
8960e9cd 437error:
dd02a4c1 438 if (close(fd) < 0) {
6f04ed72 439 PERROR("close relayd fd");
dd02a4c1
DG
440 }
441error_socket:
442 return ret;
8960e9cd 443}
3ecec76a
PP
444
445int print_missing_or_multiple_domains(unsigned int sum)
446{
447 int ret = 0;
448
449 if (sum == 0) {
450 ERR("Please specify a domain (-k/-u/-j).");
451 ret = -1;
452 } else if (sum > 1) {
453 ERR("Multiple domains specified.");
454 ret = -1;
455 }
456
457 return ret;
458}
20fb9e02
JD
459
460/*
461 * Get the discarded events and lost packet counts.
462 */
463void print_session_stats(const char *session_name)
464{
3e8f2238 465 int count, nb_domains, domain_idx, channel_idx, session_idx;
20fb9e02
JD
466 struct lttng_domain *domains;
467 struct lttng_channel *channels;
3e8f2238
JG
468 uint64_t discarded_events_total = 0, lost_packets_total = 0;
469 struct lttng_session *sessions = NULL;
470 const struct lttng_session *selected_session = NULL;
471
472 count = lttng_list_sessions(&sessions);
473 if (count < 1) {
474 ERR("Failed to retrieve session descriptions while printing session statistics.");
475 goto end;
476 }
477
478 /* Identify the currently-selected sessions. */
479 for (session_idx = 0; session_idx < count; session_idx++) {
480 if (!strcmp(session_name, sessions[session_idx].name)) {
481 selected_session = &sessions[session_idx];
482 break;
483 }
484 }
485 if (!selected_session) {
486 ERR("Failed to retrieve session \"%s\" description while printing session statistics.", session_name);
487 goto end;
488 }
20fb9e02
JD
489
490 nb_domains = lttng_list_domains(session_name, &domains);
491 if (nb_domains < 0) {
492 goto end;
493 }
494 for (domain_idx = 0; domain_idx < nb_domains; domain_idx++) {
495 struct lttng_handle *handle = lttng_create_handle(session_name,
496 &domains[domain_idx]);
497
498 if (!handle) {
3e8f2238 499 ERR("Failed to create session handle while printing session statistics.");
20fb9e02
JD
500 goto end;
501 }
502
503 count = lttng_list_channels(handle, &channels);
504 for (channel_idx = 0; channel_idx < count; channel_idx++) {
505 int ret;
3e8f2238 506 uint64_t discarded_events = 0, lost_packets = 0;
20fb9e02
JD
507 struct lttng_channel *channel = &channels[channel_idx];
508
509 ret = lttng_channel_get_discarded_event_count(channel,
3e8f2238 510 &discarded_events);
20fb9e02
JD
511 if (ret) {
512 ERR("Failed to retrieve discarded event count from channel %s",
513 channel->name);
514 }
515
516 ret = lttng_channel_get_lost_packet_count(channel,
3e8f2238 517 &lost_packets);
20fb9e02
JD
518 if (ret) {
519 ERR("Failed to retrieve lost packet count from channel %s",
520 channel->name);
521 }
522
3e8f2238
JG
523 discarded_events_total += discarded_events;
524 lost_packets_total += lost_packets;
20fb9e02
JD
525 }
526 lttng_destroy_handle(handle);
527 }
3e8f2238 528 if (discarded_events_total > 0 && !selected_session->snapshot_mode) {
20fb9e02
JD
529 MSG("[warning] %" PRIu64 " events discarded, please refer to "
530 "the documentation on channel configuration.",
3e8f2238 531 discarded_events_total);
20fb9e02 532 }
3e8f2238 533 if (lost_packets_total > 0 && !selected_session->snapshot_mode) {
20fb9e02
JD
534 MSG("[warning] %" PRIu64 " packets lost, please refer to "
535 "the documentation on channel configuration.",
3e8f2238 536 lost_packets_total);
20fb9e02
JD
537 }
538
539end:
3e8f2238 540 free(sessions);
20fb9e02 541}
4ba92f18 542
4fc83d94 543int show_cmd_help(const char *cmd_name, const char *help_msg)
4ba92f18
PP
544{
545 int ret;
546 char page_name[32];
547
548 ret = sprintf(page_name, "lttng-%s", cmd_name);
549 assert(ret > 0 && ret < 32);
4fc83d94
PP
550 ret = utils_show_help(1, page_name, help_msg);
551 if (ret && !help_msg) {
552 ERR("Cannot view man page `lttng-%s(1)`", cmd_name);
553 perror("exec");
554 }
4ba92f18 555
4fc83d94 556 return ret;
4ba92f18 557}
bbbfd849
JG
558
559int print_trace_archive_location(
560 const struct lttng_trace_archive_location *location,
561 const char *session_name)
562{
563 int ret = 0;
564 enum lttng_trace_archive_location_type location_type;
565 enum lttng_trace_archive_location_status status;
566 bool printed_location = false;
567
568 location_type = lttng_trace_archive_location_get_type(location);
569
570 _MSG("Trace chunk archive for session %s is now readable",
571 session_name);
572 switch (location_type) {
573 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_LOCAL:
574 {
575 const char *absolute_path;
576
577 status = lttng_trace_archive_location_local_get_absolute_path(
578 location, &absolute_path);
579 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
580 ret = -1;
581 goto end;
582 }
583 MSG(" at %s", absolute_path);
584 printed_location = true;
585 break;
586 }
587 case LTTNG_TRACE_ARCHIVE_LOCATION_TYPE_RELAY:
588 {
589 uint16_t control_port, data_port;
590 const char *host, *relative_path, *protocol_str;
591 enum lttng_trace_archive_location_relay_protocol_type protocol;
592
593 /* Fetch all relay location parameters. */
594 status = lttng_trace_archive_location_relay_get_protocol_type(
595 location, &protocol);
596 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
597 ret = -1;
598 goto end;
599 }
600
601 status = lttng_trace_archive_location_relay_get_host(
602 location, &host);
603 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
604 ret = -1;
605 goto end;
606 }
607
608 status = lttng_trace_archive_location_relay_get_control_port(
609 location, &control_port);
610 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
611 ret = -1;
612 goto end;
613 }
614
615 status = lttng_trace_archive_location_relay_get_data_port(
616 location, &data_port);
617 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
618 ret = -1;
619 goto end;
620 }
621
622 status = lttng_trace_archive_location_relay_get_relative_path(
623 location, &relative_path);
624 if (status != LTTNG_TRACE_ARCHIVE_LOCATION_STATUS_OK) {
625 ret = -1;
626 goto end;
627 }
628
629 switch (protocol) {
630 case LTTNG_TRACE_ARCHIVE_LOCATION_RELAY_PROTOCOL_TYPE_TCP:
631 protocol_str = "tcp";
632 break;
633 default:
634 protocol_str = "unknown";
635 break;
636 }
637
638 MSG(" on relay %s://%s/%s [control port %" PRIu16 ", data port %"
639 PRIu16 "]", protocol_str, host,
640 relative_path, control_port, data_port);
641 printed_location = true;
642 break;
643 }
644 default:
645 break;
646 }
647end:
648 if (!printed_location) {
649 MSG(" at an unknown location");
650 }
651 return ret;
652}
This page took 0.078218 seconds and 4 git commands to generate.