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