cppcheck: don't check NULL pointer before freeing them
[lttng-tools.git] / src / lib / lttng-ctl / lttng-ctl.c
1 /*
2 * liblttngctl.c
3 *
4 * Linux Trace Toolkit Control Library
5 *
6 * Copyright (C) 2011 David Goulet <david.goulet@polymtl.ca>
7 *
8 * This library is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License, version 2.1 only,
10 * as published by the Free Software Foundation.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public License
18 * along with this library; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #define _GNU_SOURCE
23 #include <assert.h>
24 #include <grp.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
30
31 #include <common/common.h>
32 #include <common/defaults.h>
33 #include <common/sessiond-comm/sessiond-comm.h>
34 #include <common/uri.h>
35 #include <lttng/lttng.h>
36
37 #include "filter/filter-ast.h"
38 #include "filter/filter-parser.h"
39 #include "filter/filter-bytecode.h"
40 #include "filter/memstream.h"
41
42 #ifdef DEBUG
43 static const int print_xml = 1;
44 #define dbg_printf(fmt, args...) \
45 printf("[debug liblttng-ctl] " fmt, ## args)
46 #else
47 static const int print_xml = 0;
48 #define dbg_printf(fmt, args...) \
49 do { \
50 /* do nothing but check printf format */ \
51 if (0) \
52 printf("[debug liblttnctl] " fmt, ## args); \
53 } while (0)
54 #endif
55
56
57 /* Socket to session daemon for communication */
58 static int sessiond_socket;
59 static char sessiond_sock_path[PATH_MAX];
60 static char health_sock_path[PATH_MAX];
61
62 /* Variables */
63 static char *tracing_group;
64 static int connected;
65
66 /* Global */
67
68 /*
69 * Those two variables are used by error.h to silent or control the verbosity of
70 * error message. They are global to the library so application linking with it
71 * are able to compile correctly and also control verbosity of the library.
72 */
73 int lttng_opt_quiet;
74 int lttng_opt_verbose;
75
76 static void set_default_url_attr(struct lttng_uri *uri,
77 enum lttng_stream_type stype)
78 {
79 uri->stype = stype;
80 if (uri->dtype != LTTNG_DST_PATH && uri->port == 0) {
81 uri->port = (stype == LTTNG_STREAM_CONTROL) ?
82 DEFAULT_NETWORK_CONTROL_PORT : DEFAULT_NETWORK_DATA_PORT;
83 }
84 }
85
86 /*
87 * Parse a string URL and creates URI(s) returning the size of the populated
88 * array.
89 */
90 static ssize_t parse_str_urls_to_uri(const char *ctrl_url, const char *data_url,
91 struct lttng_uri **uris)
92 {
93 unsigned int equal = 1, idx = 0;
94 /* Add the "file://" size to the URL maximum size */
95 char url[PATH_MAX + 7];
96 ssize_t size_ctrl = 0, size_data = 0, size;
97 struct lttng_uri *ctrl_uris = NULL, *data_uris = NULL;
98 struct lttng_uri *tmp_uris = NULL;
99
100 /* No URL(s) is allowed. This means that the consumer will be disabled. */
101 if (ctrl_url == NULL && data_url == NULL) {
102 return 0;
103 }
104
105 /* Check if URLs are equal and if so, only use the control URL */
106 if (ctrl_url && data_url) {
107 equal = !strcmp(ctrl_url, data_url);
108 }
109
110 /*
111 * Since we allow the str_url to be a full local filesystem path, we are
112 * going to create a valid file:// URL if it's the case.
113 *
114 * Check if first character is a '/' or else reject the URL.
115 */
116 if (ctrl_url && ctrl_url[0] == '/') {
117 int ret;
118
119 ret = snprintf(url, sizeof(url), "file://%s", ctrl_url);
120 if (ret < 0) {
121 PERROR("snprintf file url");
122 goto parse_error;
123 }
124 ctrl_url = url;
125 }
126
127 /* Parse the control URL if there is one */
128 if (ctrl_url) {
129 size_ctrl = uri_parse(ctrl_url, &ctrl_uris);
130 if (size_ctrl < 1) {
131 ERR("Unable to parse the URL %s", ctrl_url);
132 goto parse_error;
133 }
134
135 /* At this point, we know there is at least one URI in the array */
136 set_default_url_attr(&ctrl_uris[0], LTTNG_STREAM_CONTROL);
137
138 if (ctrl_uris[0].dtype == LTTNG_DST_PATH && data_url) {
139 ERR("Can not have a data URL when destination is file://");
140 goto error;
141 }
142
143 /* URL are not equal but the control URL uses a net:// protocol */
144 if (size_ctrl == 2) {
145 if (!equal) {
146 ERR("Control URL uses the net:// protocol and the data URL is "
147 "different. Not allowed.");
148 goto error;
149 } else {
150 set_default_url_attr(&ctrl_uris[1], LTTNG_STREAM_DATA);
151 /*
152 * The data_url and ctrl_url are equal and the ctrl_url
153 * contains a net:// protocol so we just skip the data part.
154 */
155 data_url = NULL;
156 }
157 }
158 }
159
160 if (data_url) {
161 /* We have to parse the data URL in this case */
162 size_data = uri_parse(data_url, &data_uris);
163 if (size_data < 1) {
164 ERR("Unable to parse the URL %s", data_url);
165 goto error;
166 } else if (size_data == 2) {
167 ERR("Data URL can not be set with the net[4|6]:// protocol");
168 goto error;
169 }
170
171 set_default_url_attr(&data_uris[0], LTTNG_STREAM_DATA);
172 }
173
174 /* Compute total size */
175 size = size_ctrl + size_data;
176
177 tmp_uris = zmalloc(sizeof(struct lttng_uri) * size);
178 if (tmp_uris == NULL) {
179 PERROR("zmalloc uris");
180 goto error;
181 }
182
183 if (ctrl_uris) {
184 /* It's possible the control URIs array contains more than one URI */
185 memcpy(tmp_uris, ctrl_uris, sizeof(struct lttng_uri) * size_ctrl);
186 ++idx;
187 free(ctrl_uris);
188 }
189
190 if (data_uris) {
191 memcpy(&tmp_uris[idx], data_uris, sizeof(struct lttng_uri));
192 free(data_uris);
193 }
194
195 *uris = tmp_uris;
196
197 return size;
198
199 error:
200 free(ctrl_uris);
201 free(data_uris);
202 free(tmp_uris);
203 parse_error:
204 return -1;
205 }
206
207 /*
208 * Copy string from src to dst and enforce null terminated byte.
209 */
210 static void copy_string(char *dst, const char *src, size_t len)
211 {
212 if (src && dst) {
213 strncpy(dst, src, len);
214 /* Enforce the NULL terminated byte */
215 dst[len - 1] = '\0';
216 } else if (dst) {
217 dst[0] = '\0';
218 }
219 }
220
221 /*
222 * Copy domain to lttcomm_session_msg domain.
223 *
224 * If domain is unknown, default domain will be the kernel.
225 */
226 static void copy_lttng_domain(struct lttng_domain *dst, struct lttng_domain *src)
227 {
228 if (src && dst) {
229 switch (src->type) {
230 case LTTNG_DOMAIN_KERNEL:
231 case LTTNG_DOMAIN_UST:
232 /*
233 case LTTNG_DOMAIN_UST_EXEC_NAME:
234 case LTTNG_DOMAIN_UST_PID:
235 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
236 */
237 memcpy(dst, src, sizeof(struct lttng_domain));
238 break;
239 default:
240 memset(dst, 0, sizeof(struct lttng_domain));
241 break;
242 }
243 }
244 }
245
246 /*
247 * Send lttcomm_session_msg to the session daemon.
248 *
249 * On success, returns the number of bytes sent (>=0)
250 * On error, returns -1
251 */
252 static int send_session_msg(struct lttcomm_session_msg *lsm)
253 {
254 int ret;
255
256 if (!connected) {
257 ret = -LTTNG_ERR_NO_SESSIOND;
258 goto end;
259 }
260
261 DBG("LSM cmd type : %d", lsm->cmd_type);
262
263 ret = lttcomm_send_creds_unix_sock(sessiond_socket, lsm,
264 sizeof(struct lttcomm_session_msg));
265 if (ret < 0) {
266 ret = -LTTNG_ERR_FATAL;
267 }
268
269 end:
270 return ret;
271 }
272
273 /*
274 * Send var len data to the session daemon.
275 *
276 * On success, returns the number of bytes sent (>=0)
277 * On error, returns -1
278 */
279 static int send_session_varlen(void *data, size_t len)
280 {
281 int ret;
282
283 if (!connected) {
284 ret = -LTTNG_ERR_NO_SESSIOND;
285 goto end;
286 }
287
288 if (!data || !len) {
289 ret = 0;
290 goto end;
291 }
292
293 ret = lttcomm_send_unix_sock(sessiond_socket, data, len);
294 if (ret < 0) {
295 ret = -LTTNG_ERR_FATAL;
296 }
297
298 end:
299 return ret;
300 }
301
302 /*
303 * Receive data from the sessiond socket.
304 *
305 * On success, returns the number of bytes received (>=0)
306 * On error, returns -1 (recvmsg() error) or -ENOTCONN
307 */
308 static int recv_data_sessiond(void *buf, size_t len)
309 {
310 int ret;
311
312 if (!connected) {
313 ret = -LTTNG_ERR_NO_SESSIOND;
314 goto end;
315 }
316
317 ret = lttcomm_recv_unix_sock(sessiond_socket, buf, len);
318 if (ret < 0) {
319 ret = -LTTNG_ERR_FATAL;
320 }
321
322 end:
323 return ret;
324 }
325
326 /*
327 * Check if we are in the specified group.
328 *
329 * If yes return 1, else return -1.
330 */
331 static int check_tracing_group(const char *grp_name)
332 {
333 struct group *grp_tracing; /* no free(). See getgrnam(3) */
334 gid_t *grp_list;
335 int grp_list_size, grp_id, i;
336 int ret = -1;
337
338 /* Get GID of group 'tracing' */
339 grp_tracing = getgrnam(grp_name);
340 if (!grp_tracing) {
341 /* If grp_tracing is NULL, the group does not exist. */
342 goto end;
343 }
344
345 /* Get number of supplementary group IDs */
346 grp_list_size = getgroups(0, NULL);
347 if (grp_list_size < 0) {
348 perror("getgroups");
349 goto end;
350 }
351
352 /* Alloc group list of the right size */
353 grp_list = malloc(grp_list_size * sizeof(gid_t));
354 if (!grp_list) {
355 perror("malloc");
356 goto end;
357 }
358 grp_id = getgroups(grp_list_size, grp_list);
359 if (grp_id < 0) {
360 perror("getgroups");
361 goto free_list;
362 }
363
364 for (i = 0; i < grp_list_size; i++) {
365 if (grp_list[i] == grp_tracing->gr_gid) {
366 ret = 1;
367 break;
368 }
369 }
370
371 free_list:
372 free(grp_list);
373
374 end:
375 return ret;
376 }
377
378 /*
379 * Try connect to session daemon with sock_path.
380 *
381 * Return 0 on success, else -1
382 */
383 static int try_connect_sessiond(const char *sock_path)
384 {
385 int ret;
386
387 /* If socket exist, we check if the daemon listens for connect. */
388 ret = access(sock_path, F_OK);
389 if (ret < 0) {
390 /* Not alive */
391 goto error;
392 }
393
394 ret = lttcomm_connect_unix_sock(sock_path);
395 if (ret < 0) {
396 /* Not alive */
397 goto error;
398 }
399
400 ret = lttcomm_close_unix_sock(ret);
401 if (ret < 0) {
402 perror("lttcomm_close_unix_sock");
403 }
404
405 return 0;
406
407 error:
408 return -1;
409 }
410
411 /*
412 * Set sessiond socket path by putting it in the global sessiond_sock_path
413 * variable.
414 *
415 * Returns 0 on success, negative value on failure (the sessiond socket path
416 * is somehow too long or ENOMEM).
417 */
418 static int set_session_daemon_path(void)
419 {
420 int in_tgroup = 0; /* In tracing group */
421 uid_t uid;
422
423 uid = getuid();
424
425 if (uid != 0) {
426 /* Are we in the tracing group ? */
427 in_tgroup = check_tracing_group(tracing_group);
428 }
429
430 if ((uid == 0) || in_tgroup) {
431 copy_string(sessiond_sock_path, DEFAULT_GLOBAL_CLIENT_UNIX_SOCK,
432 sizeof(sessiond_sock_path));
433 }
434
435 if (uid != 0) {
436 int ret;
437
438 if (in_tgroup) {
439 /* Tracing group */
440 ret = try_connect_sessiond(sessiond_sock_path);
441 if (ret >= 0) {
442 goto end;
443 }
444 /* Global session daemon not available... */
445 }
446 /* ...or not in tracing group (and not root), default */
447
448 /*
449 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
450 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
451 */
452 ret = snprintf(sessiond_sock_path, sizeof(sessiond_sock_path),
453 DEFAULT_HOME_CLIENT_UNIX_SOCK, getenv("HOME"));
454 if ((ret < 0) || (ret >= sizeof(sessiond_sock_path))) {
455 goto error;
456 }
457 }
458 end:
459 return 0;
460
461 error:
462 return -1;
463 }
464
465 /*
466 * Connect to the LTTng session daemon.
467 *
468 * On success, return 0. On error, return -1.
469 */
470 static int connect_sessiond(void)
471 {
472 int ret;
473
474 ret = set_session_daemon_path();
475 if (ret < 0) {
476 goto error;
477 }
478
479 /* Connect to the sesssion daemon */
480 ret = lttcomm_connect_unix_sock(sessiond_sock_path);
481 if (ret < 0) {
482 goto error;
483 }
484
485 sessiond_socket = ret;
486 connected = 1;
487
488 return 0;
489
490 error:
491 return -1;
492 }
493
494 /*
495 * Clean disconnect from the session daemon.
496 * On success, return 0. On error, return -1.
497 */
498 static int disconnect_sessiond(void)
499 {
500 int ret = 0;
501
502 if (connected) {
503 ret = lttcomm_close_unix_sock(sessiond_socket);
504 sessiond_socket = 0;
505 connected = 0;
506 }
507
508 return ret;
509 }
510
511 /*
512 * Ask the session daemon a specific command and put the data into buf.
513 * Takes extra var. len. data as input to send to the session daemon.
514 *
515 * Return size of data (only payload, not header) or a negative error code.
516 */
517 static int ask_sessiond_varlen(struct lttcomm_session_msg *lsm,
518 void *vardata, size_t varlen, void **buf)
519 {
520 int ret;
521 size_t size;
522 void *data = NULL;
523 struct lttcomm_lttng_msg llm;
524
525 ret = connect_sessiond();
526 if (ret < 0) {
527 ret = -LTTNG_ERR_NO_SESSIOND;
528 goto end;
529 }
530
531 /* Send command to session daemon */
532 ret = send_session_msg(lsm);
533 if (ret < 0) {
534 /* Ret value is a valid lttng error code. */
535 goto end;
536 }
537 /* Send var len data */
538 ret = send_session_varlen(vardata, varlen);
539 if (ret < 0) {
540 /* Ret value is a valid lttng error code. */
541 goto end;
542 }
543
544 /* Get header from data transmission */
545 ret = recv_data_sessiond(&llm, sizeof(llm));
546 if (ret < 0) {
547 /* Ret value is a valid lttng error code. */
548 goto end;
549 }
550
551 /* Check error code if OK */
552 if (llm.ret_code != LTTNG_OK) {
553 ret = -llm.ret_code;
554 goto end;
555 }
556
557 size = llm.data_size;
558 if (size == 0) {
559 /* If client free with size 0 */
560 if (buf != NULL) {
561 *buf = NULL;
562 }
563 ret = 0;
564 goto end;
565 }
566
567 data = (void*) malloc(size);
568
569 /* Get payload data */
570 ret = recv_data_sessiond(data, size);
571 if (ret < 0) {
572 free(data);
573 goto end;
574 }
575
576 /*
577 * Extra protection not to dereference a NULL pointer. If buf is NULL at
578 * this point, an error is returned and data is freed.
579 */
580 if (buf == NULL) {
581 ret = -LTTNG_ERR_INVALID;
582 free(data);
583 goto end;
584 }
585
586 *buf = data;
587 ret = size;
588
589 end:
590 disconnect_sessiond();
591 return ret;
592 }
593
594 /*
595 * Ask the session daemon a specific command and put the data into buf.
596 *
597 * Return size of data (only payload, not header) or a negative error code.
598 */
599 static int ask_sessiond(struct lttcomm_session_msg *lsm, void **buf)
600 {
601 return ask_sessiond_varlen(lsm, NULL, 0, buf);
602 }
603
604 /*
605 * Create lttng handle and return pointer.
606 * The returned pointer will be NULL in case of malloc() error.
607 */
608 struct lttng_handle *lttng_create_handle(const char *session_name,
609 struct lttng_domain *domain)
610 {
611 struct lttng_handle *handle = NULL;
612
613 if (domain == NULL) {
614 goto end;
615 }
616
617 handle = malloc(sizeof(struct lttng_handle));
618 if (handle == NULL) {
619 PERROR("malloc handle");
620 goto end;
621 }
622
623 /* Copy session name */
624 copy_string(handle->session_name, session_name,
625 sizeof(handle->session_name));
626
627 /* Copy lttng domain */
628 copy_lttng_domain(&handle->domain, domain);
629
630 end:
631 return handle;
632 }
633
634 /*
635 * Destroy handle by free(3) the pointer.
636 */
637 void lttng_destroy_handle(struct lttng_handle *handle)
638 {
639 free(handle);
640 }
641
642 /*
643 * Register an outside consumer.
644 * Returns size of returned session payload data or a negative error code.
645 */
646 int lttng_register_consumer(struct lttng_handle *handle,
647 const char *socket_path)
648 {
649 struct lttcomm_session_msg lsm;
650
651 if (handle == NULL || socket_path == NULL) {
652 return -LTTNG_ERR_INVALID;
653 }
654
655 lsm.cmd_type = LTTNG_REGISTER_CONSUMER;
656 copy_string(lsm.session.name, handle->session_name,
657 sizeof(lsm.session.name));
658 copy_lttng_domain(&lsm.domain, &handle->domain);
659
660 copy_string(lsm.u.reg.path, socket_path, sizeof(lsm.u.reg.path));
661
662 return ask_sessiond(&lsm, NULL);
663 }
664
665 /*
666 * Start tracing for all traces of the session.
667 * Returns size of returned session payload data or a negative error code.
668 */
669 int lttng_start_tracing(const char *session_name)
670 {
671 struct lttcomm_session_msg lsm;
672
673 if (session_name == NULL) {
674 return -LTTNG_ERR_INVALID;
675 }
676
677 lsm.cmd_type = LTTNG_START_TRACE;
678
679 copy_string(lsm.session.name, session_name, sizeof(lsm.session.name));
680
681 return ask_sessiond(&lsm, NULL);
682 }
683
684 /*
685 * Stop tracing for all traces of the session.
686 */
687 static int _lttng_stop_tracing(const char *session_name, int wait)
688 {
689 int ret, data_ret;
690 struct lttcomm_session_msg lsm;
691
692 if (session_name == NULL) {
693 return -LTTNG_ERR_INVALID;
694 }
695
696 lsm.cmd_type = LTTNG_STOP_TRACE;
697
698 copy_string(lsm.session.name, session_name, sizeof(lsm.session.name));
699
700 ret = ask_sessiond(&lsm, NULL);
701 if (ret < 0 && ret != -LTTNG_ERR_TRACE_ALREADY_STOPPED) {
702 goto error;
703 }
704
705 if (!wait) {
706 goto end;
707 }
708
709 _MSG("Waiting for data availability");
710
711 /* Check for data availability */
712 do {
713 data_ret = lttng_data_pending(session_name);
714 if (data_ret < 0) {
715 /* Return the data available call error. */
716 ret = data_ret;
717 goto error;
718 }
719
720 /*
721 * Data sleep time before retrying (in usec). Don't sleep if the call
722 * returned value indicates availability.
723 */
724 if (data_ret) {
725 usleep(DEFAULT_DATA_AVAILABILITY_WAIT_TIME);
726 _MSG(".");
727 }
728 } while (data_ret != 0);
729
730 MSG("");
731
732 end:
733 error:
734 return ret;
735 }
736
737 /*
738 * Stop tracing and wait for data availability.
739 */
740 int lttng_stop_tracing(const char *session_name)
741 {
742 return _lttng_stop_tracing(session_name, 1);
743 }
744
745 /*
746 * Stop tracing but _don't_ wait for data availability.
747 */
748 int lttng_stop_tracing_no_wait(const char *session_name)
749 {
750 return _lttng_stop_tracing(session_name, 0);
751 }
752
753 /*
754 * Add context to a channel.
755 *
756 * If the given channel is NULL, add the contexts to all channels.
757 * The event_name param is ignored.
758 *
759 * Returns the size of the returned payload data or a negative error code.
760 */
761 int lttng_add_context(struct lttng_handle *handle,
762 struct lttng_event_context *ctx, const char *event_name,
763 const char *channel_name)
764 {
765 struct lttcomm_session_msg lsm;
766
767 /* Safety check. Both are mandatory */
768 if (handle == NULL || ctx == NULL) {
769 return -LTTNG_ERR_INVALID;
770 }
771
772 memset(&lsm, 0, sizeof(lsm));
773
774 lsm.cmd_type = LTTNG_ADD_CONTEXT;
775
776 /* Copy channel name */
777 copy_string(lsm.u.context.channel_name, channel_name,
778 sizeof(lsm.u.context.channel_name));
779
780 copy_lttng_domain(&lsm.domain, &handle->domain);
781
782 memcpy(&lsm.u.context.ctx, ctx, sizeof(struct lttng_event_context));
783
784 copy_string(lsm.session.name, handle->session_name,
785 sizeof(lsm.session.name));
786
787 return ask_sessiond(&lsm, NULL);
788 }
789
790 /*
791 * Enable event(s) for a channel.
792 * If no event name is specified, all events are enabled.
793 * If no channel name is specified, the default 'channel0' is used.
794 * Returns size of returned session payload data or a negative error code.
795 */
796 int lttng_enable_event(struct lttng_handle *handle,
797 struct lttng_event *ev, const char *channel_name)
798 {
799 struct lttcomm_session_msg lsm;
800
801 if (handle == NULL || ev == NULL) {
802 return -LTTNG_ERR_INVALID;
803 }
804
805 memset(&lsm, 0, sizeof(lsm));
806
807 /* If no channel name, we put the default name */
808 if (channel_name == NULL) {
809 copy_string(lsm.u.enable.channel_name, DEFAULT_CHANNEL_NAME,
810 sizeof(lsm.u.enable.channel_name));
811 } else {
812 copy_string(lsm.u.enable.channel_name, channel_name,
813 sizeof(lsm.u.enable.channel_name));
814 }
815
816 copy_lttng_domain(&lsm.domain, &handle->domain);
817
818 if (ev->name[0] != '\0') {
819 lsm.cmd_type = LTTNG_ENABLE_EVENT;
820 } else {
821 lsm.cmd_type = LTTNG_ENABLE_ALL_EVENT;
822 }
823 memcpy(&lsm.u.enable.event, ev, sizeof(lsm.u.enable.event));
824
825 copy_string(lsm.session.name, handle->session_name,
826 sizeof(lsm.session.name));
827
828 return ask_sessiond(&lsm, NULL);
829 }
830
831 /*
832 * Create or enable an event with a filter expression.
833 *
834 * Return negative error value on error.
835 * Return size of returned session payload data if OK.
836 */
837 int lttng_enable_event_with_filter(struct lttng_handle *handle,
838 struct lttng_event *event, const char *channel_name,
839 const char *filter_expression)
840 {
841 struct lttcomm_session_msg lsm;
842 struct filter_parser_ctx *ctx;
843 FILE *fmem;
844 int ret = 0;
845
846 if (!filter_expression) {
847 /*
848 * Fall back to normal event enabling if no filter
849 * specified.
850 */
851 return lttng_enable_event(handle, event, channel_name);
852 }
853
854 /*
855 * Empty filter string will always be rejected by the parser
856 * anyway, so treat this corner-case early to eliminate
857 * lttng_fmemopen error for 0-byte allocation.
858 */
859 if (handle == NULL || filter_expression[0] == '\0') {
860 return -LTTNG_ERR_INVALID;
861 }
862
863 /*
864 * casting const to non-const, as the underlying function will
865 * use it in read-only mode.
866 */
867 fmem = lttng_fmemopen((void *) filter_expression,
868 strlen(filter_expression), "r");
869 if (!fmem) {
870 fprintf(stderr, "Error opening memory as stream\n");
871 return -LTTNG_ERR_FILTER_NOMEM;
872 }
873 ctx = filter_parser_ctx_alloc(fmem);
874 if (!ctx) {
875 fprintf(stderr, "Error allocating parser\n");
876 ret = -LTTNG_ERR_FILTER_NOMEM;
877 goto alloc_error;
878 }
879 ret = filter_parser_ctx_append_ast(ctx);
880 if (ret) {
881 fprintf(stderr, "Parse error\n");
882 ret = -LTTNG_ERR_FILTER_INVAL;
883 goto parse_error;
884 }
885 ret = filter_visitor_set_parent(ctx);
886 if (ret) {
887 fprintf(stderr, "Set parent error\n");
888 ret = -LTTNG_ERR_FILTER_INVAL;
889 goto parse_error;
890 }
891 if (print_xml) {
892 ret = filter_visitor_print_xml(ctx, stdout, 0);
893 if (ret) {
894 fflush(stdout);
895 fprintf(stderr, "XML print error\n");
896 ret = -LTTNG_ERR_FILTER_INVAL;
897 goto parse_error;
898 }
899 }
900
901 dbg_printf("Generating IR... ");
902 fflush(stdout);
903 ret = filter_visitor_ir_generate(ctx);
904 if (ret) {
905 fprintf(stderr, "Generate IR error\n");
906 ret = -LTTNG_ERR_FILTER_INVAL;
907 goto parse_error;
908 }
909 dbg_printf("done\n");
910
911 dbg_printf("Validating IR... ");
912 fflush(stdout);
913 ret = filter_visitor_ir_check_binary_op_nesting(ctx);
914 if (ret) {
915 ret = -LTTNG_ERR_FILTER_INVAL;
916 goto parse_error;
917 }
918 dbg_printf("done\n");
919
920 dbg_printf("Generating bytecode... ");
921 fflush(stdout);
922 ret = filter_visitor_bytecode_generate(ctx);
923 if (ret) {
924 fprintf(stderr, "Generate bytecode error\n");
925 ret = -LTTNG_ERR_FILTER_INVAL;
926 goto parse_error;
927 }
928 dbg_printf("done\n");
929 dbg_printf("Size of bytecode generated: %u bytes.\n",
930 bytecode_get_len(&ctx->bytecode->b));
931
932 memset(&lsm, 0, sizeof(lsm));
933
934 lsm.cmd_type = LTTNG_ENABLE_EVENT_WITH_FILTER;
935
936 /* Copy channel name */
937 copy_string(lsm.u.enable.channel_name, channel_name,
938 sizeof(lsm.u.enable.channel_name));
939 /* Copy event name */
940 if (event) {
941 memcpy(&lsm.u.enable.event, event, sizeof(lsm.u.enable.event));
942 }
943
944 lsm.u.enable.bytecode_len = sizeof(ctx->bytecode->b)
945 + bytecode_get_len(&ctx->bytecode->b);
946
947 copy_lttng_domain(&lsm.domain, &handle->domain);
948
949 copy_string(lsm.session.name, handle->session_name,
950 sizeof(lsm.session.name));
951
952 ret = ask_sessiond_varlen(&lsm, &ctx->bytecode->b,
953 lsm.u.enable.bytecode_len, NULL);
954
955 filter_bytecode_free(ctx);
956 filter_ir_free(ctx);
957 filter_parser_ctx_free(ctx);
958 if (fclose(fmem) != 0) {
959 perror("fclose");
960 }
961 return ret;
962
963 parse_error:
964 filter_bytecode_free(ctx);
965 filter_ir_free(ctx);
966 filter_parser_ctx_free(ctx);
967 alloc_error:
968 if (fclose(fmem) != 0) {
969 perror("fclose");
970 }
971 return ret;
972 }
973
974 /*
975 * Disable event(s) of a channel and domain.
976 * If no event name is specified, all events are disabled.
977 * If no channel name is specified, the default 'channel0' is used.
978 * Returns size of returned session payload data or a negative error code.
979 */
980 int lttng_disable_event(struct lttng_handle *handle, const char *name,
981 const char *channel_name)
982 {
983 struct lttcomm_session_msg lsm;
984
985 if (handle == NULL) {
986 return -LTTNG_ERR_INVALID;
987 }
988
989 memset(&lsm, 0, sizeof(lsm));
990
991 if (channel_name) {
992 copy_string(lsm.u.disable.channel_name, channel_name,
993 sizeof(lsm.u.disable.channel_name));
994 } else {
995 copy_string(lsm.u.disable.channel_name, DEFAULT_CHANNEL_NAME,
996 sizeof(lsm.u.disable.channel_name));
997 }
998
999 copy_lttng_domain(&lsm.domain, &handle->domain);
1000
1001 if (name != NULL) {
1002 copy_string(lsm.u.disable.name, name, sizeof(lsm.u.disable.name));
1003 lsm.cmd_type = LTTNG_DISABLE_EVENT;
1004 } else {
1005 lsm.cmd_type = LTTNG_DISABLE_ALL_EVENT;
1006 }
1007
1008 copy_string(lsm.session.name, handle->session_name,
1009 sizeof(lsm.session.name));
1010
1011 return ask_sessiond(&lsm, NULL);
1012 }
1013
1014 /*
1015 * Enable channel per domain
1016 * Returns size of returned session payload data or a negative error code.
1017 */
1018 int lttng_enable_channel(struct lttng_handle *handle,
1019 struct lttng_channel *chan)
1020 {
1021 struct lttcomm_session_msg lsm;
1022
1023 /*
1024 * NULL arguments are forbidden. No default values.
1025 */
1026 if (handle == NULL || chan == NULL) {
1027 return -LTTNG_ERR_INVALID;
1028 }
1029
1030 memset(&lsm, 0, sizeof(lsm));
1031
1032 memcpy(&lsm.u.channel.chan, chan, sizeof(lsm.u.channel.chan));
1033
1034 lsm.cmd_type = LTTNG_ENABLE_CHANNEL;
1035
1036 copy_lttng_domain(&lsm.domain, &handle->domain);
1037
1038 copy_string(lsm.session.name, handle->session_name,
1039 sizeof(lsm.session.name));
1040
1041 return ask_sessiond(&lsm, NULL);
1042 }
1043
1044 /*
1045 * All tracing will be stopped for registered events of the channel.
1046 * Returns size of returned session payload data or a negative error code.
1047 */
1048 int lttng_disable_channel(struct lttng_handle *handle, const char *name)
1049 {
1050 struct lttcomm_session_msg lsm;
1051
1052 /* Safety check. Both are mandatory */
1053 if (handle == NULL || name == NULL) {
1054 return -LTTNG_ERR_INVALID;
1055 }
1056
1057 memset(&lsm, 0, sizeof(lsm));
1058
1059 lsm.cmd_type = LTTNG_DISABLE_CHANNEL;
1060
1061 copy_string(lsm.u.disable.channel_name, name,
1062 sizeof(lsm.u.disable.channel_name));
1063
1064 copy_lttng_domain(&lsm.domain, &handle->domain);
1065
1066 copy_string(lsm.session.name, handle->session_name,
1067 sizeof(lsm.session.name));
1068
1069 return ask_sessiond(&lsm, NULL);
1070 }
1071
1072 /*
1073 * Lists all available tracepoints of domain.
1074 * Sets the contents of the events array.
1075 * Returns the number of lttng_event entries in events;
1076 * on error, returns a negative value.
1077 */
1078 int lttng_list_tracepoints(struct lttng_handle *handle,
1079 struct lttng_event **events)
1080 {
1081 int ret;
1082 struct lttcomm_session_msg lsm;
1083
1084 if (handle == NULL) {
1085 return -LTTNG_ERR_INVALID;
1086 }
1087
1088 lsm.cmd_type = LTTNG_LIST_TRACEPOINTS;
1089 copy_lttng_domain(&lsm.domain, &handle->domain);
1090
1091 ret = ask_sessiond(&lsm, (void **) events);
1092 if (ret < 0) {
1093 return ret;
1094 }
1095
1096 return ret / sizeof(struct lttng_event);
1097 }
1098
1099 /*
1100 * Lists all available tracepoint fields of domain.
1101 * Sets the contents of the event field array.
1102 * Returns the number of lttng_event_field entries in events;
1103 * on error, returns a negative value.
1104 */
1105 int lttng_list_tracepoint_fields(struct lttng_handle *handle,
1106 struct lttng_event_field **fields)
1107 {
1108 int ret;
1109 struct lttcomm_session_msg lsm;
1110
1111 if (handle == NULL) {
1112 return -LTTNG_ERR_INVALID;
1113 }
1114
1115 lsm.cmd_type = LTTNG_LIST_TRACEPOINT_FIELDS;
1116 copy_lttng_domain(&lsm.domain, &handle->domain);
1117
1118 ret = ask_sessiond(&lsm, (void **) fields);
1119 if (ret < 0) {
1120 return ret;
1121 }
1122
1123 return ret / sizeof(struct lttng_event_field);
1124 }
1125
1126 /*
1127 * Returns a human readable string describing
1128 * the error code (a negative value).
1129 */
1130 const char *lttng_strerror(int code)
1131 {
1132 return error_get_str(code);
1133 }
1134
1135 /*
1136 * Create a brand new session using name and url for destination.
1137 *
1138 * Returns LTTNG_OK on success or a negative error code.
1139 */
1140 int lttng_create_session(const char *name, const char *url)
1141 {
1142 int ret;
1143 ssize_t size;
1144 struct lttcomm_session_msg lsm;
1145 struct lttng_uri *uris = NULL;
1146
1147 if (name == NULL) {
1148 return -LTTNG_ERR_INVALID;
1149 }
1150
1151 memset(&lsm, 0, sizeof(lsm));
1152
1153 lsm.cmd_type = LTTNG_CREATE_SESSION;
1154 copy_string(lsm.session.name, name, sizeof(lsm.session.name));
1155
1156 /* There should never be a data URL */
1157 size = parse_str_urls_to_uri(url, NULL, &uris);
1158 if (size < 0) {
1159 return -LTTNG_ERR_INVALID;
1160 }
1161
1162 lsm.u.uri.size = size;
1163
1164 ret = ask_sessiond_varlen(&lsm, uris, sizeof(struct lttng_uri) * size,
1165 NULL);
1166
1167 free(uris);
1168 return ret;
1169 }
1170
1171 /*
1172 * Destroy session using name.
1173 * Returns size of returned session payload data or a negative error code.
1174 */
1175 int lttng_destroy_session(const char *session_name)
1176 {
1177 struct lttcomm_session_msg lsm;
1178
1179 if (session_name == NULL) {
1180 return -LTTNG_ERR_INVALID;
1181 }
1182
1183 lsm.cmd_type = LTTNG_DESTROY_SESSION;
1184
1185 copy_string(lsm.session.name, session_name, sizeof(lsm.session.name));
1186
1187 return ask_sessiond(&lsm, NULL);
1188 }
1189
1190 /*
1191 * Ask the session daemon for all available sessions.
1192 * Sets the contents of the sessions array.
1193 * Returns the number of lttng_session entries in sessions;
1194 * on error, returns a negative value.
1195 */
1196 int lttng_list_sessions(struct lttng_session **sessions)
1197 {
1198 int ret;
1199 struct lttcomm_session_msg lsm;
1200
1201 lsm.cmd_type = LTTNG_LIST_SESSIONS;
1202 ret = ask_sessiond(&lsm, (void**) sessions);
1203 if (ret < 0) {
1204 return ret;
1205 }
1206
1207 return ret / sizeof(struct lttng_session);
1208 }
1209
1210 /*
1211 * Ask the session daemon for all available domains of a session.
1212 * Sets the contents of the domains array.
1213 * Returns the number of lttng_domain entries in domains;
1214 * on error, returns a negative value.
1215 */
1216 int lttng_list_domains(const char *session_name,
1217 struct lttng_domain **domains)
1218 {
1219 int ret;
1220 struct lttcomm_session_msg lsm;
1221
1222 if (session_name == NULL) {
1223 return -LTTNG_ERR_INVALID;
1224 }
1225
1226 lsm.cmd_type = LTTNG_LIST_DOMAINS;
1227
1228 copy_string(lsm.session.name, session_name, sizeof(lsm.session.name));
1229
1230 ret = ask_sessiond(&lsm, (void**) domains);
1231 if (ret < 0) {
1232 return ret;
1233 }
1234
1235 return ret / sizeof(struct lttng_domain);
1236 }
1237
1238 /*
1239 * Ask the session daemon for all available channels of a session.
1240 * Sets the contents of the channels array.
1241 * Returns the number of lttng_channel entries in channels;
1242 * on error, returns a negative value.
1243 */
1244 int lttng_list_channels(struct lttng_handle *handle,
1245 struct lttng_channel **channels)
1246 {
1247 int ret;
1248 struct lttcomm_session_msg lsm;
1249
1250 if (handle == NULL) {
1251 return -LTTNG_ERR_INVALID;
1252 }
1253
1254 lsm.cmd_type = LTTNG_LIST_CHANNELS;
1255 copy_string(lsm.session.name, handle->session_name,
1256 sizeof(lsm.session.name));
1257
1258 copy_lttng_domain(&lsm.domain, &handle->domain);
1259
1260 ret = ask_sessiond(&lsm, (void**) channels);
1261 if (ret < 0) {
1262 return ret;
1263 }
1264
1265 return ret / sizeof(struct lttng_channel);
1266 }
1267
1268 /*
1269 * Ask the session daemon for all available events of a session channel.
1270 * Sets the contents of the events array.
1271 * Returns the number of lttng_event entries in events;
1272 * on error, returns a negative value.
1273 */
1274 int lttng_list_events(struct lttng_handle *handle,
1275 const char *channel_name, struct lttng_event **events)
1276 {
1277 int ret;
1278 struct lttcomm_session_msg lsm;
1279
1280 /* Safety check. An handle and channel name are mandatory */
1281 if (handle == NULL || channel_name == NULL) {
1282 return -LTTNG_ERR_INVALID;
1283 }
1284
1285 lsm.cmd_type = LTTNG_LIST_EVENTS;
1286 copy_string(lsm.session.name, handle->session_name,
1287 sizeof(lsm.session.name));
1288 copy_string(lsm.u.list.channel_name, channel_name,
1289 sizeof(lsm.u.list.channel_name));
1290
1291 copy_lttng_domain(&lsm.domain, &handle->domain);
1292
1293 ret = ask_sessiond(&lsm, (void**) events);
1294 if (ret < 0) {
1295 return ret;
1296 }
1297
1298 return ret / sizeof(struct lttng_event);
1299 }
1300
1301 /*
1302 * Sets the tracing_group variable with name.
1303 * This function allocates memory pointed to by tracing_group.
1304 * On success, returns 0, on error, returns -1 (null name) or -ENOMEM.
1305 */
1306 int lttng_set_tracing_group(const char *name)
1307 {
1308 if (name == NULL) {
1309 return -LTTNG_ERR_INVALID;
1310 }
1311
1312 if (asprintf(&tracing_group, "%s", name) < 0) {
1313 return -LTTNG_ERR_FATAL;
1314 }
1315
1316 return 0;
1317 }
1318
1319 /*
1320 * Returns size of returned session payload data or a negative error code.
1321 */
1322 int lttng_calibrate(struct lttng_handle *handle,
1323 struct lttng_calibrate *calibrate)
1324 {
1325 struct lttcomm_session_msg lsm;
1326
1327 /* Safety check. NULL pointer are forbidden */
1328 if (handle == NULL || calibrate == NULL) {
1329 return -LTTNG_ERR_INVALID;
1330 }
1331
1332 lsm.cmd_type = LTTNG_CALIBRATE;
1333 copy_lttng_domain(&lsm.domain, &handle->domain);
1334
1335 memcpy(&lsm.u.calibrate, calibrate, sizeof(lsm.u.calibrate));
1336
1337 return ask_sessiond(&lsm, NULL);
1338 }
1339
1340 /*
1341 * Set default channel attributes.
1342 * If either or both of the arguments are null, attr content is zeroe'd.
1343 */
1344 void lttng_channel_set_default_attr(struct lttng_domain *domain,
1345 struct lttng_channel_attr *attr)
1346 {
1347 /* Safety check */
1348 if (attr == NULL || domain == NULL) {
1349 return;
1350 }
1351
1352 memset(attr, 0, sizeof(struct lttng_channel_attr));
1353
1354 switch (domain->type) {
1355 case LTTNG_DOMAIN_KERNEL:
1356 attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
1357 attr->switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
1358 attr->read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
1359
1360 attr->subbuf_size = default_get_kernel_channel_subbuf_size();
1361 attr->num_subbuf = DEFAULT_KERNEL_CHANNEL_SUBBUF_NUM;
1362 attr->output = DEFAULT_KERNEL_CHANNEL_OUTPUT;
1363 break;
1364 case LTTNG_DOMAIN_UST:
1365 #if 0
1366 case LTTNG_DOMAIN_UST_EXEC_NAME:
1367 case LTTNG_DOMAIN_UST_PID:
1368 case LTTNG_DOMAIN_UST_PID_FOLLOW_CHILDREN:
1369 #endif
1370 attr->overwrite = DEFAULT_CHANNEL_OVERWRITE;
1371 attr->switch_timer_interval = DEFAULT_CHANNEL_SWITCH_TIMER;
1372 attr->read_timer_interval = DEFAULT_CHANNEL_READ_TIMER;
1373
1374 attr->subbuf_size = default_get_ust_channel_subbuf_size();
1375 attr->num_subbuf = DEFAULT_UST_CHANNEL_SUBBUF_NUM;
1376 attr->output = DEFAULT_UST_CHANNEL_OUTPUT;
1377 break;
1378 default:
1379 /* Default behavior: leave set to 0. */
1380 break;
1381 }
1382 }
1383
1384 /*
1385 * Check if session daemon is alive.
1386 *
1387 * Return 1 if alive or 0 if not.
1388 * On error returns a negative value.
1389 */
1390 int lttng_session_daemon_alive(void)
1391 {
1392 int ret;
1393
1394 ret = set_session_daemon_path();
1395 if (ret < 0) {
1396 /* Error */
1397 return ret;
1398 }
1399
1400 if (*sessiond_sock_path == '\0') {
1401 /*
1402 * No socket path set. Weird error which means the constructor was not
1403 * called.
1404 */
1405 assert(0);
1406 }
1407
1408 ret = try_connect_sessiond(sessiond_sock_path);
1409 if (ret < 0) {
1410 /* Not alive */
1411 return 0;
1412 }
1413
1414 /* Is alive */
1415 return 1;
1416 }
1417
1418 /*
1419 * Set URL for a consumer for a session and domain.
1420 *
1421 * Return 0 on success, else a negative value.
1422 */
1423 int lttng_set_consumer_url(struct lttng_handle *handle,
1424 const char *control_url, const char *data_url)
1425 {
1426 int ret;
1427 ssize_t size;
1428 struct lttcomm_session_msg lsm;
1429 struct lttng_uri *uris = NULL;
1430
1431 if (handle == NULL || (control_url == NULL && data_url == NULL)) {
1432 return -LTTNG_ERR_INVALID;
1433 }
1434
1435 memset(&lsm, 0, sizeof(lsm));
1436
1437 lsm.cmd_type = LTTNG_SET_CONSUMER_URI;
1438
1439 copy_string(lsm.session.name, handle->session_name,
1440 sizeof(lsm.session.name));
1441 copy_lttng_domain(&lsm.domain, &handle->domain);
1442
1443 size = parse_str_urls_to_uri(control_url, data_url, &uris);
1444 if (size < 0) {
1445 return -LTTNG_ERR_INVALID;
1446 }
1447
1448 lsm.u.uri.size = size;
1449
1450 ret = ask_sessiond_varlen(&lsm, uris, sizeof(struct lttng_uri) * size,
1451 NULL);
1452
1453 free(uris);
1454 return ret;
1455 }
1456
1457 /*
1458 * Enable consumer for a session and domain.
1459 *
1460 * Return 0 on success, else a negative value.
1461 */
1462 int lttng_enable_consumer(struct lttng_handle *handle)
1463 {
1464 struct lttcomm_session_msg lsm;
1465
1466 if (handle == NULL) {
1467 return -LTTNG_ERR_INVALID;
1468 }
1469
1470 lsm.cmd_type = LTTNG_ENABLE_CONSUMER;
1471
1472 copy_string(lsm.session.name, handle->session_name,
1473 sizeof(lsm.session.name));
1474 copy_lttng_domain(&lsm.domain, &handle->domain);
1475
1476 return ask_sessiond(&lsm, NULL);
1477 }
1478
1479 /*
1480 * Disable consumer for a session and domain.
1481 *
1482 * Return 0 on success, else a negative value.
1483 */
1484 int lttng_disable_consumer(struct lttng_handle *handle)
1485 {
1486 struct lttcomm_session_msg lsm;
1487
1488 if (handle == NULL) {
1489 return -LTTNG_ERR_INVALID;
1490 }
1491
1492 lsm.cmd_type = LTTNG_DISABLE_CONSUMER;
1493
1494 copy_string(lsm.session.name, handle->session_name,
1495 sizeof(lsm.session.name));
1496 copy_lttng_domain(&lsm.domain, &handle->domain);
1497
1498 return ask_sessiond(&lsm, NULL);
1499 }
1500
1501 /*
1502 * Set health socket path by putting it in the global health_sock_path
1503 * variable.
1504 *
1505 * Returns 0 on success or assert(0) on ENOMEM.
1506 */
1507 static int set_health_socket_path(void)
1508 {
1509 int in_tgroup = 0; /* In tracing group */
1510 uid_t uid;
1511 const char *home;
1512
1513 uid = getuid();
1514
1515 if (uid != 0) {
1516 /* Are we in the tracing group ? */
1517 in_tgroup = check_tracing_group(tracing_group);
1518 }
1519
1520 if ((uid == 0) || in_tgroup) {
1521 copy_string(health_sock_path, DEFAULT_GLOBAL_HEALTH_UNIX_SOCK,
1522 sizeof(health_sock_path));
1523 }
1524
1525 if (uid != 0) {
1526 int ret;
1527
1528 /*
1529 * With GNU C < 2.1, snprintf returns -1 if the target buffer is too small;
1530 * With GNU C >= 2.1, snprintf returns the required size (excluding closing null)
1531 */
1532 home = getenv("HOME");
1533 if (home == NULL) {
1534 /* Fallback in /tmp .. */
1535 home = "/tmp";
1536 }
1537
1538 ret = snprintf(health_sock_path, sizeof(health_sock_path),
1539 DEFAULT_HOME_HEALTH_UNIX_SOCK, home);
1540 if ((ret < 0) || (ret >= sizeof(health_sock_path))) {
1541 /* ENOMEM at this point... just kill the control lib. */
1542 assert(0);
1543 }
1544 }
1545
1546 return 0;
1547 }
1548
1549 /*
1550 * Check session daemon health for a specific health component.
1551 *
1552 * Return 0 if health is OK or else 1 if BAD.
1553 *
1554 * Any other negative value is a lttng error code which can be translated with
1555 * lttng_strerror().
1556 */
1557 int lttng_health_check(enum lttng_health_component c)
1558 {
1559 int sock, ret;
1560 struct lttcomm_health_msg msg;
1561 struct lttcomm_health_data reply;
1562
1563 /* Connect to the sesssion daemon */
1564 sock = lttcomm_connect_unix_sock(health_sock_path);
1565 if (sock < 0) {
1566 ret = -LTTNG_ERR_NO_SESSIOND;
1567 goto error;
1568 }
1569
1570 msg.cmd = LTTNG_HEALTH_CHECK;
1571 msg.component = c;
1572
1573 ret = lttcomm_send_unix_sock(sock, (void *)&msg, sizeof(msg));
1574 if (ret < 0) {
1575 ret = -LTTNG_ERR_FATAL;
1576 goto close_error;
1577 }
1578
1579 ret = lttcomm_recv_unix_sock(sock, (void *)&reply, sizeof(reply));
1580 if (ret < 0) {
1581 ret = -LTTNG_ERR_FATAL;
1582 goto close_error;
1583 }
1584
1585 ret = reply.ret_code;
1586
1587 close_error:
1588 close(sock);
1589
1590 error:
1591 return ret;
1592 }
1593
1594 /*
1595 * This is an extension of create session that is ONLY and SHOULD only be used
1596 * by the lttng command line program. It exists to avoid using URI parsing in
1597 * the lttng client.
1598 *
1599 * We need the date and time for the trace path subdirectory for the case where
1600 * the user does NOT define one using either -o or -U. Using the normal
1601 * lttng_create_session API call, we have no clue on the session daemon side if
1602 * the URL was generated automatically by the client or define by the user.
1603 *
1604 * So this function "wrapper" is hidden from the public API, takes the datetime
1605 * string and appends it if necessary to the URI subdirectory before sending it
1606 * to the session daemon.
1607 *
1608 * With this extra function, the lttng_create_session call behavior is not
1609 * changed and the timestamp is appended to the URI on the session daemon side
1610 * if necessary.
1611 */
1612 int _lttng_create_session_ext(const char *name, const char *url,
1613 const char *datetime)
1614 {
1615 int ret;
1616 ssize_t size;
1617 struct lttcomm_session_msg lsm;
1618 struct lttng_uri *uris = NULL;
1619
1620 if (name == NULL || datetime == NULL) {
1621 return -LTTNG_ERR_INVALID;
1622 }
1623
1624 memset(&lsm, 0, sizeof(lsm));
1625
1626 lsm.cmd_type = LTTNG_CREATE_SESSION;
1627 copy_string(lsm.session.name, name, sizeof(lsm.session.name));
1628
1629 /* There should never be a data URL */
1630 size = parse_str_urls_to_uri(url, NULL, &uris);
1631 if (size < 0) {
1632 ret = -LTTNG_ERR_INVALID;
1633 goto error;
1634 }
1635
1636 lsm.u.uri.size = size;
1637
1638 if (size > 0 && uris[0].dtype != LTTNG_DST_PATH && strlen(uris[0].subdir) == 0) {
1639 ret = snprintf(uris[0].subdir, sizeof(uris[0].subdir), "%s-%s", name,
1640 datetime);
1641 if (ret < 0) {
1642 PERROR("snprintf uri subdir");
1643 ret = -LTTNG_ERR_FATAL;
1644 goto error;
1645 }
1646 }
1647
1648 ret = ask_sessiond_varlen(&lsm, uris, sizeof(struct lttng_uri) * size,
1649 NULL);
1650
1651 error:
1652 free(uris);
1653 return ret;
1654 }
1655
1656 /*
1657 * For a given session name, this call checks if the data is ready to be read
1658 * or is still being extracted by the consumer(s) hence not ready to be used by
1659 * any readers.
1660 */
1661 int lttng_data_pending(const char *session_name)
1662 {
1663 int ret;
1664 struct lttcomm_session_msg lsm;
1665
1666 if (session_name == NULL) {
1667 return -LTTNG_ERR_INVALID;
1668 }
1669
1670 lsm.cmd_type = LTTNG_DATA_PENDING;
1671
1672 copy_string(lsm.session.name, session_name, sizeof(lsm.session.name));
1673
1674 ret = ask_sessiond(&lsm, NULL);
1675
1676 /*
1677 * The ask_sessiond function negate the return code if it's not LTTNG_OK so
1678 * getting -1 means that the reply ret_code was 1 thus meaning that the
1679 * data is available. Yes it is hackish but for now this is the only way.
1680 */
1681 if (ret == -1) {
1682 ret = 1;
1683 }
1684
1685 return ret;
1686 }
1687
1688 /*
1689 * lib constructor
1690 */
1691 static void __attribute__((constructor)) init()
1692 {
1693 /* Set default session group */
1694 lttng_set_tracing_group(DEFAULT_TRACING_GROUP);
1695 /* Set socket for health check */
1696 (void) set_health_socket_path();
1697 }
1698
1699 /*
1700 * lib destructor
1701 */
1702 static void __attribute__((destructor)) lttng_ctl_exit()
1703 {
1704 free(tracing_group);
1705 }
This page took 0.104424 seconds and 4 git commands to generate.