| 1 | /* |
| 2 | * Copyright (C) 2012 David Goulet <dgoulet@efficios.com> |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0-only |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #define _LGPL_SOURCE |
| 9 | #include <assert.h> |
| 10 | #include <stdio.h> |
| 11 | #include <stdlib.h> |
| 12 | #include <string.h> |
| 13 | #include <sys/stat.h> |
| 14 | #include <inttypes.h> |
| 15 | |
| 16 | #include <common/common.h> |
| 17 | #include <common/defaults.h> |
| 18 | #include <common/compat/endian.h> |
| 19 | #include <common/compat/string.h> |
| 20 | #include <common/sessiond-comm/relayd.h> |
| 21 | #include <common/index/ctf-index.h> |
| 22 | #include <common/trace-chunk.h> |
| 23 | #include <common/string-utils/format.h> |
| 24 | |
| 25 | #include "relayd.h" |
| 26 | |
| 27 | static |
| 28 | bool relayd_supports_chunks(const struct lttcomm_relayd_sock *sock) |
| 29 | { |
| 30 | if (sock->major > 2) { |
| 31 | return true; |
| 32 | } else if (sock->major == 2 && sock->minor >= 11) { |
| 33 | return true; |
| 34 | } |
| 35 | return false; |
| 36 | } |
| 37 | |
| 38 | static |
| 39 | bool relayd_supports_get_configuration(const struct lttcomm_relayd_sock *sock) |
| 40 | { |
| 41 | if (sock->major > 2) { |
| 42 | return true; |
| 43 | } else if (sock->major == 2 && sock->minor >= 12) { |
| 44 | return true; |
| 45 | } |
| 46 | return false; |
| 47 | } |
| 48 | |
| 49 | /* |
| 50 | * Send command. Fill up the header and append the data. |
| 51 | */ |
| 52 | static int send_command(struct lttcomm_relayd_sock *rsock, |
| 53 | enum lttcomm_relayd_command cmd, const void *data, size_t size, |
| 54 | int flags) |
| 55 | { |
| 56 | int ret; |
| 57 | struct lttcomm_relayd_hdr header; |
| 58 | char *buf; |
| 59 | uint64_t buf_size = sizeof(header); |
| 60 | |
| 61 | if (rsock->sock.fd < 0) { |
| 62 | return -ECONNRESET; |
| 63 | } |
| 64 | |
| 65 | if (data) { |
| 66 | buf_size += size; |
| 67 | } |
| 68 | |
| 69 | buf = zmalloc(buf_size); |
| 70 | if (buf == NULL) { |
| 71 | PERROR("zmalloc relayd send command buf"); |
| 72 | ret = -1; |
| 73 | goto alloc_error; |
| 74 | } |
| 75 | |
| 76 | memset(&header, 0, sizeof(header)); |
| 77 | header.cmd = htobe32(cmd); |
| 78 | header.data_size = htobe64(size); |
| 79 | |
| 80 | /* Zeroed for now since not used. */ |
| 81 | header.cmd_version = 0; |
| 82 | header.circuit_id = 0; |
| 83 | |
| 84 | /* Prepare buffer to send. */ |
| 85 | memcpy(buf, &header, sizeof(header)); |
| 86 | if (data) { |
| 87 | memcpy(buf + sizeof(header), data, size); |
| 88 | } |
| 89 | |
| 90 | DBG3("Relayd sending command %d of size %" PRIu64, (int) cmd, buf_size); |
| 91 | ret = rsock->sock.ops->sendmsg(&rsock->sock, buf, buf_size, flags); |
| 92 | if (ret < 0) { |
| 93 | PERROR("Failed to send command %d of size %" PRIu64, |
| 94 | (int) cmd, buf_size); |
| 95 | ret = -errno; |
| 96 | goto error; |
| 97 | } |
| 98 | error: |
| 99 | free(buf); |
| 100 | alloc_error: |
| 101 | return ret; |
| 102 | } |
| 103 | |
| 104 | /* |
| 105 | * Receive reply data on socket. This MUST be call after send_command or else |
| 106 | * could result in unexpected behavior(s). |
| 107 | */ |
| 108 | static int recv_reply(struct lttcomm_relayd_sock *rsock, void *data, size_t size) |
| 109 | { |
| 110 | int ret; |
| 111 | |
| 112 | if (rsock->sock.fd < 0) { |
| 113 | return -ECONNRESET; |
| 114 | } |
| 115 | |
| 116 | DBG3("Relayd waiting for reply of size %zu", size); |
| 117 | |
| 118 | ret = rsock->sock.ops->recvmsg(&rsock->sock, data, size, 0); |
| 119 | if (ret <= 0 || ret != size) { |
| 120 | if (ret == 0) { |
| 121 | /* Orderly shutdown. */ |
| 122 | DBG("Socket %d has performed an orderly shutdown", rsock->sock.fd); |
| 123 | } else { |
| 124 | DBG("Receiving reply failed on sock %d for size %zu with ret %d", |
| 125 | rsock->sock.fd, size, ret); |
| 126 | } |
| 127 | /* Always return -1 here and the caller can use errno. */ |
| 128 | ret = -1; |
| 129 | goto error; |
| 130 | } |
| 131 | |
| 132 | error: |
| 133 | return ret; |
| 134 | } |
| 135 | |
| 136 | /* |
| 137 | * Starting from 2.11, RELAYD_CREATE_SESSION payload (session_name, |
| 138 | * hostname, and base_path) have no length restriction on the sender side. |
| 139 | * Length for both payloads is stored in the msg struct. A new dynamic size |
| 140 | * payload size is introduced. |
| 141 | */ |
| 142 | static int relayd_create_session_2_11(struct lttcomm_relayd_sock *rsock, |
| 143 | const char *session_name, const char *hostname, |
| 144 | const char *base_path, int session_live_timer, |
| 145 | unsigned int snapshot, uint64_t sessiond_session_id, |
| 146 | const lttng_uuid sessiond_uuid, const uint64_t *current_chunk_id, |
| 147 | time_t creation_time, bool session_name_contains_creation_time, |
| 148 | struct lttcomm_relayd_create_session_reply_2_11 *reply, |
| 149 | char *output_path) |
| 150 | { |
| 151 | int ret; |
| 152 | struct lttcomm_relayd_create_session_2_11 *msg = NULL; |
| 153 | size_t session_name_len; |
| 154 | size_t hostname_len; |
| 155 | size_t base_path_len; |
| 156 | size_t msg_length; |
| 157 | char *dst; |
| 158 | |
| 159 | if (!base_path) { |
| 160 | base_path = ""; |
| 161 | } |
| 162 | /* The three names are sent with a '\0' delimiter between them. */ |
| 163 | session_name_len = strlen(session_name) + 1; |
| 164 | hostname_len = strlen(hostname) + 1; |
| 165 | base_path_len = strlen(base_path) + 1; |
| 166 | |
| 167 | msg_length = sizeof(*msg) + session_name_len + hostname_len + base_path_len; |
| 168 | msg = zmalloc(msg_length); |
| 169 | if (!msg) { |
| 170 | PERROR("zmalloc create_session_2_11 command message"); |
| 171 | ret = -1; |
| 172 | goto error; |
| 173 | } |
| 174 | |
| 175 | assert(session_name_len <= UINT32_MAX); |
| 176 | msg->session_name_len = htobe32(session_name_len); |
| 177 | |
| 178 | assert(hostname_len <= UINT32_MAX); |
| 179 | msg->hostname_len = htobe32(hostname_len); |
| 180 | |
| 181 | assert(base_path_len <= UINT32_MAX); |
| 182 | msg->base_path_len = htobe32(base_path_len); |
| 183 | |
| 184 | dst = msg->names; |
| 185 | if (lttng_strncpy(dst, session_name, session_name_len)) { |
| 186 | ret = -1; |
| 187 | goto error; |
| 188 | } |
| 189 | dst += session_name_len; |
| 190 | if (lttng_strncpy(dst, hostname, hostname_len)) { |
| 191 | ret = -1; |
| 192 | goto error; |
| 193 | } |
| 194 | dst += hostname_len; |
| 195 | if (lttng_strncpy(dst, base_path, base_path_len)) { |
| 196 | ret = -1; |
| 197 | goto error; |
| 198 | } |
| 199 | |
| 200 | msg->live_timer = htobe32(session_live_timer); |
| 201 | msg->snapshot = !!snapshot; |
| 202 | |
| 203 | lttng_uuid_copy(msg->sessiond_uuid, sessiond_uuid); |
| 204 | msg->session_id = htobe64(sessiond_session_id); |
| 205 | msg->session_name_contains_creation_time = session_name_contains_creation_time; |
| 206 | if (current_chunk_id) { |
| 207 | LTTNG_OPTIONAL_SET(&msg->current_chunk_id, |
| 208 | htobe64(*current_chunk_id)); |
| 209 | } |
| 210 | |
| 211 | msg->creation_time = htobe64((uint64_t) creation_time); |
| 212 | |
| 213 | /* Send command */ |
| 214 | ret = send_command(rsock, RELAYD_CREATE_SESSION, msg, msg_length, 0); |
| 215 | if (ret < 0) { |
| 216 | goto error; |
| 217 | } |
| 218 | /* Receive response */ |
| 219 | ret = recv_reply(rsock, reply, sizeof(*reply)); |
| 220 | if (ret < 0) { |
| 221 | goto error; |
| 222 | } |
| 223 | reply->generic.session_id = be64toh(reply->generic.session_id); |
| 224 | reply->generic.ret_code = be32toh(reply->generic.ret_code); |
| 225 | reply->output_path_length = be32toh(reply->output_path_length); |
| 226 | if (reply->output_path_length >= LTTNG_PATH_MAX) { |
| 227 | ERR("Invalid session output path length in reply (%" PRIu32 " bytes) exceeds maximal allowed length (%d bytes)", |
| 228 | reply->output_path_length, LTTNG_PATH_MAX); |
| 229 | ret = -1; |
| 230 | goto error; |
| 231 | } |
| 232 | ret = recv_reply(rsock, output_path, reply->output_path_length); |
| 233 | if (ret < 0) { |
| 234 | goto error; |
| 235 | } |
| 236 | error: |
| 237 | free(msg); |
| 238 | return ret; |
| 239 | } |
| 240 | /* |
| 241 | * From 2.4 to 2.10, RELAYD_CREATE_SESSION takes additional parameters to |
| 242 | * support the live reading capability. |
| 243 | */ |
| 244 | static int relayd_create_session_2_4(struct lttcomm_relayd_sock *rsock, |
| 245 | const char *session_name, const char *hostname, |
| 246 | int session_live_timer, unsigned int snapshot, |
| 247 | struct lttcomm_relayd_status_session *reply) |
| 248 | { |
| 249 | int ret; |
| 250 | struct lttcomm_relayd_create_session_2_4 msg; |
| 251 | |
| 252 | if (lttng_strncpy(msg.session_name, session_name, |
| 253 | sizeof(msg.session_name))) { |
| 254 | ret = -1; |
| 255 | goto error; |
| 256 | } |
| 257 | if (lttng_strncpy(msg.hostname, hostname, sizeof(msg.hostname))) { |
| 258 | ret = -1; |
| 259 | goto error; |
| 260 | } |
| 261 | msg.live_timer = htobe32(session_live_timer); |
| 262 | msg.snapshot = htobe32(snapshot); |
| 263 | |
| 264 | /* Send command */ |
| 265 | ret = send_command(rsock, RELAYD_CREATE_SESSION, &msg, sizeof(msg), 0); |
| 266 | if (ret < 0) { |
| 267 | goto error; |
| 268 | } |
| 269 | |
| 270 | /* Receive response */ |
| 271 | ret = recv_reply(rsock, reply, sizeof(*reply)); |
| 272 | if (ret < 0) { |
| 273 | goto error; |
| 274 | } |
| 275 | reply->session_id = be64toh(reply->session_id); |
| 276 | reply->ret_code = be32toh(reply->ret_code); |
| 277 | error: |
| 278 | return ret; |
| 279 | } |
| 280 | |
| 281 | /* |
| 282 | * RELAYD_CREATE_SESSION from 2.1 to 2.3. |
| 283 | */ |
| 284 | static int relayd_create_session_2_1(struct lttcomm_relayd_sock *rsock, |
| 285 | struct lttcomm_relayd_status_session *reply) |
| 286 | { |
| 287 | int ret; |
| 288 | |
| 289 | /* Send command */ |
| 290 | ret = send_command(rsock, RELAYD_CREATE_SESSION, NULL, 0, 0); |
| 291 | if (ret < 0) { |
| 292 | goto error; |
| 293 | } |
| 294 | |
| 295 | /* Receive response */ |
| 296 | ret = recv_reply(rsock, reply, sizeof(*reply)); |
| 297 | if (ret < 0) { |
| 298 | goto error; |
| 299 | } |
| 300 | reply->session_id = be64toh(reply->session_id); |
| 301 | reply->ret_code = be32toh(reply->ret_code); |
| 302 | error: |
| 303 | return ret; |
| 304 | } |
| 305 | |
| 306 | /* |
| 307 | * Send a RELAYD_CREATE_SESSION command to the relayd with the given socket and |
| 308 | * set session_id of the relayd if we have a successful reply from the relayd. |
| 309 | * |
| 310 | * On success, return 0 else a negative value which is either an errno error or |
| 311 | * a lttng error code from the relayd. |
| 312 | */ |
| 313 | int relayd_create_session(struct lttcomm_relayd_sock *rsock, |
| 314 | uint64_t *relayd_session_id, |
| 315 | const char *session_name, const char *hostname, |
| 316 | const char *base_path, int session_live_timer, |
| 317 | unsigned int snapshot, uint64_t sessiond_session_id, |
| 318 | const lttng_uuid sessiond_uuid, |
| 319 | const uint64_t *current_chunk_id, |
| 320 | time_t creation_time, bool session_name_contains_creation_time, |
| 321 | char *output_path) |
| 322 | { |
| 323 | int ret; |
| 324 | struct lttcomm_relayd_create_session_reply_2_11 reply = {}; |
| 325 | |
| 326 | assert(rsock); |
| 327 | assert(relayd_session_id); |
| 328 | |
| 329 | DBG("Relayd create session"); |
| 330 | |
| 331 | if (rsock->minor < 4) { |
| 332 | /* From 2.1 to 2.3 */ |
| 333 | ret = relayd_create_session_2_1(rsock, &reply.generic); |
| 334 | } else if (rsock->minor >= 4 && rsock->minor < 11) { |
| 335 | /* From 2.4 to 2.10 */ |
| 336 | ret = relayd_create_session_2_4(rsock, session_name, |
| 337 | hostname, session_live_timer, snapshot, |
| 338 | &reply.generic); |
| 339 | } else { |
| 340 | /* From 2.11 to ... */ |
| 341 | ret = relayd_create_session_2_11(rsock, session_name, |
| 342 | hostname, base_path, session_live_timer, snapshot, |
| 343 | sessiond_session_id, sessiond_uuid, |
| 344 | current_chunk_id, creation_time, |
| 345 | session_name_contains_creation_time, |
| 346 | &reply, output_path); |
| 347 | } |
| 348 | |
| 349 | if (ret < 0) { |
| 350 | goto error; |
| 351 | } |
| 352 | |
| 353 | /* Return session id or negative ret code. */ |
| 354 | if (reply.generic.ret_code != LTTNG_OK) { |
| 355 | ret = -1; |
| 356 | ERR("Relayd create session replied error %d", |
| 357 | reply.generic.ret_code); |
| 358 | goto error; |
| 359 | } else { |
| 360 | ret = 0; |
| 361 | *relayd_session_id = reply.generic.session_id; |
| 362 | } |
| 363 | |
| 364 | DBG("Relayd session created with id %" PRIu64, reply.generic.session_id); |
| 365 | |
| 366 | error: |
| 367 | return ret; |
| 368 | } |
| 369 | |
| 370 | static int relayd_add_stream_2_1(struct lttcomm_relayd_sock *rsock, |
| 371 | const char *channel_name, const char *pathname) |
| 372 | { |
| 373 | int ret; |
| 374 | struct lttcomm_relayd_add_stream msg; |
| 375 | |
| 376 | memset(&msg, 0, sizeof(msg)); |
| 377 | if (lttng_strncpy(msg.channel_name, channel_name, |
| 378 | sizeof(msg.channel_name))) { |
| 379 | ret = -1; |
| 380 | goto error; |
| 381 | } |
| 382 | |
| 383 | if (lttng_strncpy(msg.pathname, pathname, |
| 384 | sizeof(msg.pathname))) { |
| 385 | ret = -1; |
| 386 | goto error; |
| 387 | } |
| 388 | |
| 389 | /* Send command */ |
| 390 | ret = send_command(rsock, RELAYD_ADD_STREAM, (void *) &msg, sizeof(msg), 0); |
| 391 | if (ret < 0) { |
| 392 | ret = -1; |
| 393 | goto error; |
| 394 | } |
| 395 | ret = 0; |
| 396 | error: |
| 397 | return ret; |
| 398 | } |
| 399 | |
| 400 | static int relayd_add_stream_2_2(struct lttcomm_relayd_sock *rsock, |
| 401 | const char *channel_name, const char *pathname, |
| 402 | uint64_t tracefile_size, uint64_t tracefile_count) |
| 403 | { |
| 404 | int ret; |
| 405 | struct lttcomm_relayd_add_stream_2_2 msg; |
| 406 | |
| 407 | memset(&msg, 0, sizeof(msg)); |
| 408 | /* Compat with relayd 2.2 to 2.10 */ |
| 409 | if (lttng_strncpy(msg.channel_name, channel_name, |
| 410 | sizeof(msg.channel_name))) { |
| 411 | ret = -1; |
| 412 | goto error; |
| 413 | } |
| 414 | if (lttng_strncpy(msg.pathname, pathname, |
| 415 | sizeof(msg.pathname))) { |
| 416 | ret = -1; |
| 417 | goto error; |
| 418 | } |
| 419 | msg.tracefile_size = htobe64(tracefile_size); |
| 420 | msg.tracefile_count = htobe64(tracefile_count); |
| 421 | |
| 422 | /* Send command */ |
| 423 | ret = send_command(rsock, RELAYD_ADD_STREAM, (void *) &msg, sizeof(msg), 0); |
| 424 | if (ret < 0) { |
| 425 | goto error; |
| 426 | } |
| 427 | ret = 0; |
| 428 | error: |
| 429 | return ret; |
| 430 | } |
| 431 | |
| 432 | static int relayd_add_stream_2_11(struct lttcomm_relayd_sock *rsock, |
| 433 | const char *channel_name, const char *pathname, |
| 434 | uint64_t tracefile_size, uint64_t tracefile_count, |
| 435 | uint64_t trace_archive_id) |
| 436 | { |
| 437 | int ret; |
| 438 | struct lttcomm_relayd_add_stream_2_11 *msg = NULL; |
| 439 | size_t channel_name_len; |
| 440 | size_t pathname_len; |
| 441 | size_t msg_length; |
| 442 | |
| 443 | /* The two names are sent with a '\0' delimiter between them. */ |
| 444 | channel_name_len = strlen(channel_name) + 1; |
| 445 | pathname_len = strlen(pathname) + 1; |
| 446 | |
| 447 | msg_length = sizeof(*msg) + channel_name_len + pathname_len; |
| 448 | msg = zmalloc(msg_length); |
| 449 | if (!msg) { |
| 450 | PERROR("zmalloc add_stream_2_11 command message"); |
| 451 | ret = -1; |
| 452 | goto error; |
| 453 | } |
| 454 | |
| 455 | assert(channel_name_len <= UINT32_MAX); |
| 456 | msg->channel_name_len = htobe32(channel_name_len); |
| 457 | |
| 458 | assert(pathname_len <= UINT32_MAX); |
| 459 | msg->pathname_len = htobe32(pathname_len); |
| 460 | |
| 461 | if (lttng_strncpy(msg->names, channel_name, channel_name_len)) { |
| 462 | ret = -1; |
| 463 | goto error; |
| 464 | } |
| 465 | if (lttng_strncpy(msg->names + channel_name_len, pathname, pathname_len)) { |
| 466 | ret = -1; |
| 467 | goto error; |
| 468 | } |
| 469 | |
| 470 | msg->tracefile_size = htobe64(tracefile_size); |
| 471 | msg->tracefile_count = htobe64(tracefile_count); |
| 472 | msg->trace_chunk_id = htobe64(trace_archive_id); |
| 473 | |
| 474 | /* Send command */ |
| 475 | ret = send_command(rsock, RELAYD_ADD_STREAM, (void *) msg, msg_length, 0); |
| 476 | if (ret < 0) { |
| 477 | goto error; |
| 478 | } |
| 479 | ret = 0; |
| 480 | error: |
| 481 | free(msg); |
| 482 | return ret; |
| 483 | } |
| 484 | |
| 485 | /* |
| 486 | * Add stream on the relayd and assign stream handle to the stream_id argument. |
| 487 | * |
| 488 | * Chunks are not supported by relayd prior to 2.11, but are used to |
| 489 | * internally between session daemon and consumer daemon to keep track |
| 490 | * of the channel and stream output path. |
| 491 | * |
| 492 | * On success return 0 else return ret_code negative value. |
| 493 | */ |
| 494 | int relayd_add_stream(struct lttcomm_relayd_sock *rsock, const char *channel_name, |
| 495 | const char *domain_name, const char *_pathname, uint64_t *stream_id, |
| 496 | uint64_t tracefile_size, uint64_t tracefile_count, |
| 497 | struct lttng_trace_chunk *trace_chunk) |
| 498 | { |
| 499 | int ret; |
| 500 | struct lttcomm_relayd_status_stream reply; |
| 501 | char pathname[RELAYD_COMM_LTTNG_PATH_MAX]; |
| 502 | |
| 503 | /* Code flow error. Safety net. */ |
| 504 | assert(rsock); |
| 505 | assert(channel_name); |
| 506 | assert(domain_name); |
| 507 | assert(_pathname); |
| 508 | assert(trace_chunk); |
| 509 | |
| 510 | DBG("Relayd adding stream for channel name %s", channel_name); |
| 511 | |
| 512 | /* Compat with relayd 2.1 */ |
| 513 | if (rsock->minor == 1) { |
| 514 | /* For 2.1 */ |
| 515 | ret = relayd_add_stream_2_1(rsock, channel_name, _pathname); |
| 516 | |
| 517 | } else if (rsock->minor > 1 && rsock->minor < 11) { |
| 518 | /* From 2.2 to 2.10 */ |
| 519 | ret = relayd_add_stream_2_2(rsock, channel_name, _pathname, |
| 520 | tracefile_size, tracefile_count); |
| 521 | } else { |
| 522 | const char *separator; |
| 523 | enum lttng_trace_chunk_status chunk_status; |
| 524 | uint64_t chunk_id; |
| 525 | |
| 526 | if (_pathname[0] == '\0') { |
| 527 | separator = ""; |
| 528 | } else { |
| 529 | separator = "/"; |
| 530 | } |
| 531 | |
| 532 | ret = snprintf(pathname, RELAYD_COMM_LTTNG_PATH_MAX, "%s%s%s", |
| 533 | domain_name, separator, _pathname); |
| 534 | if (ret <= 0 || ret >= RELAYD_COMM_LTTNG_PATH_MAX) { |
| 535 | ERR("Failed to format stream path: %s", |
| 536 | ret <= 0 ? "formatting error" : |
| 537 | "path exceeds maximal allowed length"); |
| 538 | ret = -1; |
| 539 | goto error; |
| 540 | } |
| 541 | |
| 542 | chunk_status = lttng_trace_chunk_get_id(trace_chunk, |
| 543 | &chunk_id); |
| 544 | assert(chunk_status == LTTNG_TRACE_CHUNK_STATUS_OK); |
| 545 | |
| 546 | /* From 2.11 to ...*/ |
| 547 | ret = relayd_add_stream_2_11(rsock, channel_name, pathname, |
| 548 | tracefile_size, tracefile_count, |
| 549 | chunk_id); |
| 550 | } |
| 551 | |
| 552 | if (ret) { |
| 553 | ret = -1; |
| 554 | goto error; |
| 555 | } |
| 556 | |
| 557 | /* Waiting for reply */ |
| 558 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
| 559 | if (ret < 0) { |
| 560 | goto error; |
| 561 | } |
| 562 | |
| 563 | /* Back to host bytes order. */ |
| 564 | reply.handle = be64toh(reply.handle); |
| 565 | reply.ret_code = be32toh(reply.ret_code); |
| 566 | |
| 567 | /* Return session id or negative ret code. */ |
| 568 | if (reply.ret_code != LTTNG_OK) { |
| 569 | ret = -1; |
| 570 | ERR("Relayd add stream replied error %d", reply.ret_code); |
| 571 | } else { |
| 572 | /* Success */ |
| 573 | ret = 0; |
| 574 | *stream_id = reply.handle; |
| 575 | } |
| 576 | |
| 577 | DBG("Relayd stream added successfully with handle %" PRIu64, |
| 578 | reply.handle); |
| 579 | |
| 580 | error: |
| 581 | return ret; |
| 582 | } |
| 583 | |
| 584 | /* |
| 585 | * Inform the relay that all the streams for the current channel has been sent. |
| 586 | * |
| 587 | * On success return 0 else return ret_code negative value. |
| 588 | */ |
| 589 | int relayd_streams_sent(struct lttcomm_relayd_sock *rsock) |
| 590 | { |
| 591 | int ret; |
| 592 | struct lttcomm_relayd_generic_reply reply; |
| 593 | |
| 594 | /* Code flow error. Safety net. */ |
| 595 | assert(rsock); |
| 596 | |
| 597 | DBG("Relayd sending streams sent."); |
| 598 | |
| 599 | /* This feature was introduced in 2.4, ignore it for earlier versions. */ |
| 600 | if (rsock->minor < 4) { |
| 601 | ret = 0; |
| 602 | goto end; |
| 603 | } |
| 604 | |
| 605 | /* Send command */ |
| 606 | ret = send_command(rsock, RELAYD_STREAMS_SENT, NULL, 0, 0); |
| 607 | if (ret < 0) { |
| 608 | goto error; |
| 609 | } |
| 610 | |
| 611 | /* Waiting for reply */ |
| 612 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
| 613 | if (ret < 0) { |
| 614 | goto error; |
| 615 | } |
| 616 | |
| 617 | /* Back to host bytes order. */ |
| 618 | reply.ret_code = be32toh(reply.ret_code); |
| 619 | |
| 620 | /* Return session id or negative ret code. */ |
| 621 | if (reply.ret_code != LTTNG_OK) { |
| 622 | ret = -1; |
| 623 | ERR("Relayd streams sent replied error %d", reply.ret_code); |
| 624 | goto error; |
| 625 | } else { |
| 626 | /* Success */ |
| 627 | ret = 0; |
| 628 | } |
| 629 | |
| 630 | DBG("Relayd streams sent success"); |
| 631 | |
| 632 | error: |
| 633 | end: |
| 634 | return ret; |
| 635 | } |
| 636 | |
| 637 | /* |
| 638 | * Check version numbers on the relayd. |
| 639 | * If major versions are compatible, we assign minor_to_use to the |
| 640 | * minor version of the procotol we are going to use for this session. |
| 641 | * |
| 642 | * Return 0 if the two daemons are compatible, LTTNG_ERR_RELAYD_VERSION_FAIL |
| 643 | * otherwise, or a negative value on network errors. |
| 644 | */ |
| 645 | int relayd_version_check(struct lttcomm_relayd_sock *rsock) |
| 646 | { |
| 647 | int ret; |
| 648 | struct lttcomm_relayd_version msg; |
| 649 | |
| 650 | /* Code flow error. Safety net. */ |
| 651 | assert(rsock); |
| 652 | |
| 653 | DBG("Relayd version check for major.minor %u.%u", rsock->major, |
| 654 | rsock->minor); |
| 655 | |
| 656 | memset(&msg, 0, sizeof(msg)); |
| 657 | /* Prepare network byte order before transmission. */ |
| 658 | msg.major = htobe32(rsock->major); |
| 659 | msg.minor = htobe32(rsock->minor); |
| 660 | |
| 661 | /* Send command */ |
| 662 | ret = send_command(rsock, RELAYD_VERSION, (void *) &msg, sizeof(msg), 0); |
| 663 | if (ret < 0) { |
| 664 | goto error; |
| 665 | } |
| 666 | |
| 667 | /* Receive response */ |
| 668 | ret = recv_reply(rsock, (void *) &msg, sizeof(msg)); |
| 669 | if (ret < 0) { |
| 670 | goto error; |
| 671 | } |
| 672 | |
| 673 | /* Set back to host bytes order */ |
| 674 | msg.major = be32toh(msg.major); |
| 675 | msg.minor = be32toh(msg.minor); |
| 676 | |
| 677 | /* |
| 678 | * Only validate the major version. If the other side is higher, |
| 679 | * communication is not possible. Only major version equal can talk to each |
| 680 | * other. If the minor version differs, the lowest version is used by both |
| 681 | * sides. |
| 682 | */ |
| 683 | if (msg.major != rsock->major) { |
| 684 | /* Not compatible */ |
| 685 | ret = LTTNG_ERR_RELAYD_VERSION_FAIL; |
| 686 | DBG2("Relayd version is NOT compatible. Relayd version %u != %u (us)", |
| 687 | msg.major, rsock->major); |
| 688 | goto error; |
| 689 | } |
| 690 | |
| 691 | /* |
| 692 | * If the relayd's minor version is higher, it will adapt to our version so |
| 693 | * we can continue to use the latest relayd communication data structure. |
| 694 | * If the received minor version is higher, the relayd should adapt to us. |
| 695 | */ |
| 696 | if (rsock->minor > msg.minor) { |
| 697 | rsock->minor = msg.minor; |
| 698 | } |
| 699 | |
| 700 | /* Version number compatible */ |
| 701 | DBG2("Relayd version is compatible, using protocol version %u.%u", |
| 702 | rsock->major, rsock->minor); |
| 703 | ret = 0; |
| 704 | |
| 705 | error: |
| 706 | return ret; |
| 707 | } |
| 708 | |
| 709 | /* |
| 710 | * Add stream on the relayd and assign stream handle to the stream_id argument. |
| 711 | * |
| 712 | * On success return 0 else return ret_code negative value. |
| 713 | */ |
| 714 | int relayd_send_metadata(struct lttcomm_relayd_sock *rsock, size_t len) |
| 715 | { |
| 716 | int ret; |
| 717 | |
| 718 | /* Code flow error. Safety net. */ |
| 719 | assert(rsock); |
| 720 | |
| 721 | DBG("Relayd sending metadata of size %zu", len); |
| 722 | |
| 723 | /* Send command */ |
| 724 | ret = send_command(rsock, RELAYD_SEND_METADATA, NULL, len, 0); |
| 725 | if (ret < 0) { |
| 726 | goto error; |
| 727 | } |
| 728 | |
| 729 | DBG2("Relayd metadata added successfully"); |
| 730 | |
| 731 | /* |
| 732 | * After that call, the metadata data MUST be sent to the relayd so the |
| 733 | * receive size on the other end matches the len of the metadata packet |
| 734 | * header. This is why we don't wait for a reply here. |
| 735 | */ |
| 736 | |
| 737 | error: |
| 738 | return ret; |
| 739 | } |
| 740 | |
| 741 | /* |
| 742 | * Connect to relay daemon with an allocated lttcomm_relayd_sock. |
| 743 | */ |
| 744 | int relayd_connect(struct lttcomm_relayd_sock *rsock) |
| 745 | { |
| 746 | /* Code flow error. Safety net. */ |
| 747 | assert(rsock); |
| 748 | |
| 749 | if (!rsock->sock.ops) { |
| 750 | /* |
| 751 | * Attempting a connect on a non-initialized socket. |
| 752 | */ |
| 753 | return -ECONNRESET; |
| 754 | } |
| 755 | |
| 756 | DBG3("Relayd connect ..."); |
| 757 | |
| 758 | return rsock->sock.ops->connect(&rsock->sock); |
| 759 | } |
| 760 | |
| 761 | /* |
| 762 | * Close relayd socket with an allocated lttcomm_relayd_sock. |
| 763 | * |
| 764 | * If no socket operations are found, simply return 0 meaning that everything |
| 765 | * is fine. Without operations, the socket can not possibly be opened or used. |
| 766 | * This is possible if the socket was allocated but not created. However, the |
| 767 | * caller could simply use it to store a valid file descriptor for instance |
| 768 | * passed over a Unix socket and call this to cleanup but still without a valid |
| 769 | * ops pointer. |
| 770 | * |
| 771 | * Return the close returned value. On error, a negative value is usually |
| 772 | * returned back from close(2). |
| 773 | */ |
| 774 | int relayd_close(struct lttcomm_relayd_sock *rsock) |
| 775 | { |
| 776 | int ret; |
| 777 | |
| 778 | /* Code flow error. Safety net. */ |
| 779 | assert(rsock); |
| 780 | |
| 781 | /* An invalid fd is fine, return success. */ |
| 782 | if (rsock->sock.fd < 0) { |
| 783 | ret = 0; |
| 784 | goto end; |
| 785 | } |
| 786 | |
| 787 | DBG3("Relayd closing socket %d", rsock->sock.fd); |
| 788 | |
| 789 | if (rsock->sock.ops) { |
| 790 | ret = rsock->sock.ops->close(&rsock->sock); |
| 791 | } else { |
| 792 | /* Default call if no specific ops found. */ |
| 793 | ret = close(rsock->sock.fd); |
| 794 | if (ret < 0) { |
| 795 | PERROR("relayd_close default close"); |
| 796 | } |
| 797 | } |
| 798 | rsock->sock.fd = -1; |
| 799 | |
| 800 | end: |
| 801 | return ret; |
| 802 | } |
| 803 | |
| 804 | /* |
| 805 | * Send data header structure to the relayd. |
| 806 | */ |
| 807 | int relayd_send_data_hdr(struct lttcomm_relayd_sock *rsock, |
| 808 | struct lttcomm_relayd_data_hdr *hdr, size_t size) |
| 809 | { |
| 810 | int ret; |
| 811 | |
| 812 | /* Code flow error. Safety net. */ |
| 813 | assert(rsock); |
| 814 | assert(hdr); |
| 815 | |
| 816 | if (rsock->sock.fd < 0) { |
| 817 | return -ECONNRESET; |
| 818 | } |
| 819 | |
| 820 | DBG3("Relayd sending data header of size %zu", size); |
| 821 | |
| 822 | /* Again, safety net */ |
| 823 | if (size == 0) { |
| 824 | size = sizeof(struct lttcomm_relayd_data_hdr); |
| 825 | } |
| 826 | |
| 827 | /* Only send data header. */ |
| 828 | ret = rsock->sock.ops->sendmsg(&rsock->sock, hdr, size, 0); |
| 829 | if (ret < 0) { |
| 830 | ret = -errno; |
| 831 | goto error; |
| 832 | } |
| 833 | |
| 834 | /* |
| 835 | * The data MUST be sent right after that command for the receive on the |
| 836 | * other end to match the size in the header. |
| 837 | */ |
| 838 | |
| 839 | error: |
| 840 | return ret; |
| 841 | } |
| 842 | |
| 843 | /* |
| 844 | * Send close stream command to the relayd. |
| 845 | */ |
| 846 | int relayd_send_close_stream(struct lttcomm_relayd_sock *rsock, uint64_t stream_id, |
| 847 | uint64_t last_net_seq_num) |
| 848 | { |
| 849 | int ret; |
| 850 | struct lttcomm_relayd_close_stream msg; |
| 851 | struct lttcomm_relayd_generic_reply reply; |
| 852 | |
| 853 | /* Code flow error. Safety net. */ |
| 854 | assert(rsock); |
| 855 | |
| 856 | DBG("Relayd closing stream id %" PRIu64, stream_id); |
| 857 | |
| 858 | memset(&msg, 0, sizeof(msg)); |
| 859 | msg.stream_id = htobe64(stream_id); |
| 860 | msg.last_net_seq_num = htobe64(last_net_seq_num); |
| 861 | |
| 862 | /* Send command */ |
| 863 | ret = send_command(rsock, RELAYD_CLOSE_STREAM, (void *) &msg, sizeof(msg), 0); |
| 864 | if (ret < 0) { |
| 865 | goto error; |
| 866 | } |
| 867 | |
| 868 | /* Receive response */ |
| 869 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
| 870 | if (ret < 0) { |
| 871 | goto error; |
| 872 | } |
| 873 | |
| 874 | reply.ret_code = be32toh(reply.ret_code); |
| 875 | |
| 876 | /* Return session id or negative ret code. */ |
| 877 | if (reply.ret_code != LTTNG_OK) { |
| 878 | ret = -1; |
| 879 | ERR("Relayd close stream replied error %d", reply.ret_code); |
| 880 | } else { |
| 881 | /* Success */ |
| 882 | ret = 0; |
| 883 | } |
| 884 | |
| 885 | DBG("Relayd close stream id %" PRIu64 " successfully", stream_id); |
| 886 | |
| 887 | error: |
| 888 | return ret; |
| 889 | } |
| 890 | |
| 891 | /* |
| 892 | * Check for data availability for a given stream id. |
| 893 | * |
| 894 | * Return 0 if NOT pending, 1 if so and a negative value on error. |
| 895 | */ |
| 896 | int relayd_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t stream_id, |
| 897 | uint64_t last_net_seq_num) |
| 898 | { |
| 899 | int ret; |
| 900 | struct lttcomm_relayd_data_pending msg; |
| 901 | struct lttcomm_relayd_generic_reply reply; |
| 902 | |
| 903 | /* Code flow error. Safety net. */ |
| 904 | assert(rsock); |
| 905 | |
| 906 | DBG("Relayd data pending for stream id %" PRIu64, stream_id); |
| 907 | |
| 908 | memset(&msg, 0, sizeof(msg)); |
| 909 | msg.stream_id = htobe64(stream_id); |
| 910 | msg.last_net_seq_num = htobe64(last_net_seq_num); |
| 911 | |
| 912 | /* Send command */ |
| 913 | ret = send_command(rsock, RELAYD_DATA_PENDING, (void *) &msg, |
| 914 | sizeof(msg), 0); |
| 915 | if (ret < 0) { |
| 916 | goto error; |
| 917 | } |
| 918 | |
| 919 | /* Receive response */ |
| 920 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
| 921 | if (ret < 0) { |
| 922 | goto error; |
| 923 | } |
| 924 | |
| 925 | reply.ret_code = be32toh(reply.ret_code); |
| 926 | |
| 927 | /* Return session id or negative ret code. */ |
| 928 | if (reply.ret_code >= LTTNG_OK) { |
| 929 | ERR("Relayd data pending replied error %d", reply.ret_code); |
| 930 | } |
| 931 | |
| 932 | /* At this point, the ret code is either 1 or 0 */ |
| 933 | ret = reply.ret_code; |
| 934 | |
| 935 | DBG("Relayd data is %s pending for stream id %" PRIu64, |
| 936 | ret == 1 ? "" : "NOT", stream_id); |
| 937 | |
| 938 | error: |
| 939 | return ret; |
| 940 | } |
| 941 | |
| 942 | /* |
| 943 | * Check on the relayd side for a quiescent state on the control socket. |
| 944 | */ |
| 945 | int relayd_quiescent_control(struct lttcomm_relayd_sock *rsock, |
| 946 | uint64_t metadata_stream_id) |
| 947 | { |
| 948 | int ret; |
| 949 | struct lttcomm_relayd_quiescent_control msg; |
| 950 | struct lttcomm_relayd_generic_reply reply; |
| 951 | |
| 952 | /* Code flow error. Safety net. */ |
| 953 | assert(rsock); |
| 954 | |
| 955 | DBG("Relayd checking quiescent control state"); |
| 956 | |
| 957 | memset(&msg, 0, sizeof(msg)); |
| 958 | msg.stream_id = htobe64(metadata_stream_id); |
| 959 | |
| 960 | /* Send command */ |
| 961 | ret = send_command(rsock, RELAYD_QUIESCENT_CONTROL, &msg, sizeof(msg), 0); |
| 962 | if (ret < 0) { |
| 963 | goto error; |
| 964 | } |
| 965 | |
| 966 | /* Receive response */ |
| 967 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
| 968 | if (ret < 0) { |
| 969 | goto error; |
| 970 | } |
| 971 | |
| 972 | reply.ret_code = be32toh(reply.ret_code); |
| 973 | |
| 974 | /* Return session id or negative ret code. */ |
| 975 | if (reply.ret_code != LTTNG_OK) { |
| 976 | ret = -1; |
| 977 | ERR("Relayd quiescent control replied error %d", reply.ret_code); |
| 978 | goto error; |
| 979 | } |
| 980 | |
| 981 | /* Control socket is quiescent */ |
| 982 | return 0; |
| 983 | |
| 984 | error: |
| 985 | return ret; |
| 986 | } |
| 987 | |
| 988 | /* |
| 989 | * Begin a data pending command for a specific session id. |
| 990 | */ |
| 991 | int relayd_begin_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t id) |
| 992 | { |
| 993 | int ret; |
| 994 | struct lttcomm_relayd_begin_data_pending msg; |
| 995 | struct lttcomm_relayd_generic_reply reply; |
| 996 | |
| 997 | /* Code flow error. Safety net. */ |
| 998 | assert(rsock); |
| 999 | |
| 1000 | DBG("Relayd begin data pending"); |
| 1001 | |
| 1002 | memset(&msg, 0, sizeof(msg)); |
| 1003 | msg.session_id = htobe64(id); |
| 1004 | |
| 1005 | /* Send command */ |
| 1006 | ret = send_command(rsock, RELAYD_BEGIN_DATA_PENDING, &msg, sizeof(msg), 0); |
| 1007 | if (ret < 0) { |
| 1008 | goto error; |
| 1009 | } |
| 1010 | |
| 1011 | /* Receive response */ |
| 1012 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
| 1013 | if (ret < 0) { |
| 1014 | goto error; |
| 1015 | } |
| 1016 | |
| 1017 | reply.ret_code = be32toh(reply.ret_code); |
| 1018 | |
| 1019 | /* Return session id or negative ret code. */ |
| 1020 | if (reply.ret_code != LTTNG_OK) { |
| 1021 | ret = -1; |
| 1022 | ERR("Relayd begin data pending replied error %d", reply.ret_code); |
| 1023 | goto error; |
| 1024 | } |
| 1025 | |
| 1026 | return 0; |
| 1027 | |
| 1028 | error: |
| 1029 | return ret; |
| 1030 | } |
| 1031 | |
| 1032 | /* |
| 1033 | * End a data pending command for a specific session id. |
| 1034 | * |
| 1035 | * Return 0 on success and set is_data_inflight to 0 if no data is being |
| 1036 | * streamed or 1 if it is the case. |
| 1037 | */ |
| 1038 | int relayd_end_data_pending(struct lttcomm_relayd_sock *rsock, uint64_t id, |
| 1039 | unsigned int *is_data_inflight) |
| 1040 | { |
| 1041 | int ret, recv_ret; |
| 1042 | struct lttcomm_relayd_end_data_pending msg; |
| 1043 | struct lttcomm_relayd_generic_reply reply; |
| 1044 | |
| 1045 | /* Code flow error. Safety net. */ |
| 1046 | assert(rsock); |
| 1047 | |
| 1048 | DBG("Relayd end data pending"); |
| 1049 | |
| 1050 | memset(&msg, 0, sizeof(msg)); |
| 1051 | msg.session_id = htobe64(id); |
| 1052 | |
| 1053 | /* Send command */ |
| 1054 | ret = send_command(rsock, RELAYD_END_DATA_PENDING, &msg, sizeof(msg), 0); |
| 1055 | if (ret < 0) { |
| 1056 | goto error; |
| 1057 | } |
| 1058 | |
| 1059 | /* Receive response */ |
| 1060 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
| 1061 | if (ret < 0) { |
| 1062 | goto error; |
| 1063 | } |
| 1064 | |
| 1065 | recv_ret = be32toh(reply.ret_code); |
| 1066 | if (recv_ret < 0) { |
| 1067 | ret = recv_ret; |
| 1068 | goto error; |
| 1069 | } |
| 1070 | |
| 1071 | *is_data_inflight = recv_ret; |
| 1072 | |
| 1073 | DBG("Relayd end data pending is data inflight: %d", recv_ret); |
| 1074 | |
| 1075 | return 0; |
| 1076 | |
| 1077 | error: |
| 1078 | return ret; |
| 1079 | } |
| 1080 | |
| 1081 | /* |
| 1082 | * Send index to the relayd. |
| 1083 | */ |
| 1084 | int relayd_send_index(struct lttcomm_relayd_sock *rsock, |
| 1085 | struct ctf_packet_index *index, uint64_t relay_stream_id, |
| 1086 | uint64_t net_seq_num) |
| 1087 | { |
| 1088 | int ret; |
| 1089 | struct lttcomm_relayd_index msg; |
| 1090 | struct lttcomm_relayd_generic_reply reply; |
| 1091 | |
| 1092 | /* Code flow error. Safety net. */ |
| 1093 | assert(rsock); |
| 1094 | |
| 1095 | if (rsock->minor < 4) { |
| 1096 | DBG("Not sending indexes before protocol 2.4"); |
| 1097 | ret = 0; |
| 1098 | goto error; |
| 1099 | } |
| 1100 | |
| 1101 | DBG("Relayd sending index for stream ID %" PRIu64, relay_stream_id); |
| 1102 | |
| 1103 | memset(&msg, 0, sizeof(msg)); |
| 1104 | msg.relay_stream_id = htobe64(relay_stream_id); |
| 1105 | msg.net_seq_num = htobe64(net_seq_num); |
| 1106 | |
| 1107 | /* The index is already in big endian. */ |
| 1108 | msg.packet_size = index->packet_size; |
| 1109 | msg.content_size = index->content_size; |
| 1110 | msg.timestamp_begin = index->timestamp_begin; |
| 1111 | msg.timestamp_end = index->timestamp_end; |
| 1112 | msg.events_discarded = index->events_discarded; |
| 1113 | msg.stream_id = index->stream_id; |
| 1114 | |
| 1115 | if (rsock->minor >= 8) { |
| 1116 | msg.stream_instance_id = index->stream_instance_id; |
| 1117 | msg.packet_seq_num = index->packet_seq_num; |
| 1118 | } |
| 1119 | |
| 1120 | /* Send command */ |
| 1121 | ret = send_command(rsock, RELAYD_SEND_INDEX, &msg, |
| 1122 | lttcomm_relayd_index_len(lttng_to_index_major(rsock->major, |
| 1123 | rsock->minor), |
| 1124 | lttng_to_index_minor(rsock->major, rsock->minor)), |
| 1125 | 0); |
| 1126 | if (ret < 0) { |
| 1127 | goto error; |
| 1128 | } |
| 1129 | |
| 1130 | /* Receive response */ |
| 1131 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
| 1132 | if (ret < 0) { |
| 1133 | goto error; |
| 1134 | } |
| 1135 | |
| 1136 | reply.ret_code = be32toh(reply.ret_code); |
| 1137 | |
| 1138 | /* Return session id or negative ret code. */ |
| 1139 | if (reply.ret_code != LTTNG_OK) { |
| 1140 | ret = -1; |
| 1141 | ERR("Relayd send index replied error %d", reply.ret_code); |
| 1142 | } else { |
| 1143 | /* Success */ |
| 1144 | ret = 0; |
| 1145 | } |
| 1146 | |
| 1147 | error: |
| 1148 | return ret; |
| 1149 | } |
| 1150 | |
| 1151 | /* |
| 1152 | * Ask the relay to reset the metadata trace file (regeneration). |
| 1153 | */ |
| 1154 | int relayd_reset_metadata(struct lttcomm_relayd_sock *rsock, |
| 1155 | uint64_t stream_id, uint64_t version) |
| 1156 | { |
| 1157 | int ret; |
| 1158 | struct lttcomm_relayd_reset_metadata msg; |
| 1159 | struct lttcomm_relayd_generic_reply reply; |
| 1160 | |
| 1161 | /* Code flow error. Safety net. */ |
| 1162 | assert(rsock); |
| 1163 | |
| 1164 | /* Should have been prevented by the sessiond. */ |
| 1165 | if (rsock->minor < 8) { |
| 1166 | ERR("Metadata regeneration unsupported before 2.8"); |
| 1167 | ret = -1; |
| 1168 | goto error; |
| 1169 | } |
| 1170 | |
| 1171 | DBG("Relayd reset metadata stream id %" PRIu64, stream_id); |
| 1172 | |
| 1173 | memset(&msg, 0, sizeof(msg)); |
| 1174 | msg.stream_id = htobe64(stream_id); |
| 1175 | msg.version = htobe64(version); |
| 1176 | |
| 1177 | /* Send command */ |
| 1178 | ret = send_command(rsock, RELAYD_RESET_METADATA, (void *) &msg, sizeof(msg), 0); |
| 1179 | if (ret < 0) { |
| 1180 | goto error; |
| 1181 | } |
| 1182 | |
| 1183 | /* Receive response */ |
| 1184 | ret = recv_reply(rsock, (void *) &reply, sizeof(reply)); |
| 1185 | if (ret < 0) { |
| 1186 | goto error; |
| 1187 | } |
| 1188 | |
| 1189 | reply.ret_code = be32toh(reply.ret_code); |
| 1190 | |
| 1191 | /* Return session id or negative ret code. */ |
| 1192 | if (reply.ret_code != LTTNG_OK) { |
| 1193 | ret = -1; |
| 1194 | ERR("Relayd reset metadata replied error %d", reply.ret_code); |
| 1195 | } else { |
| 1196 | /* Success */ |
| 1197 | ret = 0; |
| 1198 | } |
| 1199 | |
| 1200 | DBG("Relayd reset metadata stream id %" PRIu64 " successfully", stream_id); |
| 1201 | |
| 1202 | error: |
| 1203 | return ret; |
| 1204 | } |
| 1205 | |
| 1206 | int relayd_rotate_streams(struct lttcomm_relayd_sock *sock, |
| 1207 | unsigned int stream_count, const uint64_t *new_chunk_id, |
| 1208 | const struct relayd_stream_rotation_position *positions) |
| 1209 | { |
| 1210 | int ret; |
| 1211 | unsigned int i; |
| 1212 | struct lttng_dynamic_buffer payload; |
| 1213 | struct lttcomm_relayd_generic_reply reply = {}; |
| 1214 | const struct lttcomm_relayd_rotate_streams msg = { |
| 1215 | .stream_count = htobe32((uint32_t) stream_count), |
| 1216 | .new_chunk_id = (typeof(msg.new_chunk_id)) { |
| 1217 | .is_set = !!new_chunk_id, |
| 1218 | .value = htobe64(new_chunk_id ? *new_chunk_id : 0), |
| 1219 | }, |
| 1220 | }; |
| 1221 | char new_chunk_id_buf[MAX_INT_DEC_LEN(*new_chunk_id)] = {}; |
| 1222 | const char *new_chunk_id_str; |
| 1223 | |
| 1224 | if (!relayd_supports_chunks(sock)) { |
| 1225 | DBG("Refusing to rotate remote streams: relayd does not support chunks"); |
| 1226 | return 0; |
| 1227 | } |
| 1228 | |
| 1229 | lttng_dynamic_buffer_init(&payload); |
| 1230 | |
| 1231 | /* Code flow error. Safety net. */ |
| 1232 | assert(sock); |
| 1233 | |
| 1234 | if (new_chunk_id) { |
| 1235 | ret = snprintf(new_chunk_id_buf, sizeof(new_chunk_id_buf), |
| 1236 | "%" PRIu64, *new_chunk_id); |
| 1237 | if (ret == -1 || ret >= sizeof(new_chunk_id_buf)) { |
| 1238 | new_chunk_id_str = "formatting error"; |
| 1239 | } else { |
| 1240 | new_chunk_id_str = new_chunk_id_buf; |
| 1241 | } |
| 1242 | } else { |
| 1243 | new_chunk_id_str = "none"; |
| 1244 | } |
| 1245 | |
| 1246 | DBG("Preparing \"rotate streams\" command payload: new_chunk_id = %s, stream_count = %u", |
| 1247 | new_chunk_id_str, stream_count); |
| 1248 | |
| 1249 | ret = lttng_dynamic_buffer_append(&payload, &msg, sizeof(msg)); |
| 1250 | if (ret) { |
| 1251 | ERR("Failed to allocate \"rotate streams\" command payload"); |
| 1252 | goto error; |
| 1253 | } |
| 1254 | |
| 1255 | for (i = 0; i < stream_count; i++) { |
| 1256 | const struct relayd_stream_rotation_position *position = |
| 1257 | &positions[i]; |
| 1258 | const struct lttcomm_relayd_stream_rotation_position comm_position = { |
| 1259 | .stream_id = htobe64(position->stream_id), |
| 1260 | .rotate_at_seq_num = htobe64( |
| 1261 | position->rotate_at_seq_num), |
| 1262 | }; |
| 1263 | |
| 1264 | DBG("Rotate stream %" PRIu64 " at sequence number %" PRIu64, |
| 1265 | position->stream_id, |
| 1266 | position->rotate_at_seq_num); |
| 1267 | ret = lttng_dynamic_buffer_append(&payload, &comm_position, |
| 1268 | sizeof(comm_position)); |
| 1269 | if (ret) { |
| 1270 | ERR("Failed to allocate \"rotate streams\" command payload"); |
| 1271 | goto error; |
| 1272 | } |
| 1273 | } |
| 1274 | |
| 1275 | /* Send command. */ |
| 1276 | ret = send_command(sock, RELAYD_ROTATE_STREAMS, payload.data, |
| 1277 | payload.size, 0); |
| 1278 | if (ret < 0) { |
| 1279 | ERR("Failed to send \"rotate stream\" command"); |
| 1280 | goto error; |
| 1281 | } |
| 1282 | |
| 1283 | /* Receive response. */ |
| 1284 | ret = recv_reply(sock, &reply, sizeof(reply)); |
| 1285 | if (ret < 0) { |
| 1286 | ERR("Failed to receive \"rotate streams\" command reply"); |
| 1287 | goto error; |
| 1288 | } |
| 1289 | |
| 1290 | reply.ret_code = be32toh(reply.ret_code); |
| 1291 | if (reply.ret_code != LTTNG_OK) { |
| 1292 | ret = -1; |
| 1293 | ERR("Relayd rotate streams replied error %d", reply.ret_code); |
| 1294 | } else { |
| 1295 | /* Success. */ |
| 1296 | ret = 0; |
| 1297 | DBG("Relayd rotated streams successfully"); |
| 1298 | } |
| 1299 | |
| 1300 | error: |
| 1301 | lttng_dynamic_buffer_reset(&payload); |
| 1302 | return ret; |
| 1303 | } |
| 1304 | |
| 1305 | int relayd_create_trace_chunk(struct lttcomm_relayd_sock *sock, |
| 1306 | struct lttng_trace_chunk *chunk) |
| 1307 | { |
| 1308 | int ret = 0; |
| 1309 | enum lttng_trace_chunk_status status; |
| 1310 | struct lttcomm_relayd_create_trace_chunk msg = {}; |
| 1311 | struct lttcomm_relayd_generic_reply reply = {}; |
| 1312 | struct lttng_dynamic_buffer payload; |
| 1313 | uint64_t chunk_id; |
| 1314 | time_t creation_timestamp; |
| 1315 | const char *chunk_name; |
| 1316 | size_t chunk_name_length; |
| 1317 | bool overridden_name; |
| 1318 | |
| 1319 | lttng_dynamic_buffer_init(&payload); |
| 1320 | |
| 1321 | if (!relayd_supports_chunks(sock)) { |
| 1322 | DBG("Refusing to create remote trace chunk: relayd does not support chunks"); |
| 1323 | goto end; |
| 1324 | } |
| 1325 | |
| 1326 | status = lttng_trace_chunk_get_id(chunk, &chunk_id); |
| 1327 | if (status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
| 1328 | ret = -1; |
| 1329 | goto end; |
| 1330 | } |
| 1331 | |
| 1332 | status = lttng_trace_chunk_get_creation_timestamp( |
| 1333 | chunk, &creation_timestamp); |
| 1334 | if (status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
| 1335 | ret = -1; |
| 1336 | goto end; |
| 1337 | } |
| 1338 | |
| 1339 | status = lttng_trace_chunk_get_name( |
| 1340 | chunk, &chunk_name, &overridden_name); |
| 1341 | if (status != LTTNG_TRACE_CHUNK_STATUS_OK && |
| 1342 | status != LTTNG_TRACE_CHUNK_STATUS_NONE) { |
| 1343 | ret = -1; |
| 1344 | goto end; |
| 1345 | } |
| 1346 | |
| 1347 | chunk_name_length = overridden_name ? (strlen(chunk_name) + 1) : 0; |
| 1348 | msg = (typeof(msg)){ |
| 1349 | .chunk_id = htobe64(chunk_id), |
| 1350 | .creation_timestamp = htobe64((uint64_t) creation_timestamp), |
| 1351 | .override_name_length = htobe32((uint32_t) chunk_name_length), |
| 1352 | }; |
| 1353 | |
| 1354 | ret = lttng_dynamic_buffer_append(&payload, &msg, sizeof(msg)); |
| 1355 | if (ret) { |
| 1356 | goto end; |
| 1357 | } |
| 1358 | if (chunk_name_length) { |
| 1359 | ret = lttng_dynamic_buffer_append( |
| 1360 | &payload, chunk_name, chunk_name_length); |
| 1361 | if (ret) { |
| 1362 | goto end; |
| 1363 | } |
| 1364 | } |
| 1365 | |
| 1366 | ret = send_command(sock, RELAYD_CREATE_TRACE_CHUNK, payload.data, |
| 1367 | payload.size, 0); |
| 1368 | if (ret < 0) { |
| 1369 | ERR("Failed to send trace chunk creation command to relay daemon"); |
| 1370 | goto end; |
| 1371 | } |
| 1372 | |
| 1373 | ret = recv_reply(sock, &reply, sizeof(reply)); |
| 1374 | if (ret < 0) { |
| 1375 | ERR("Failed to receive relay daemon trace chunk creation command reply"); |
| 1376 | goto end; |
| 1377 | } |
| 1378 | |
| 1379 | reply.ret_code = be32toh(reply.ret_code); |
| 1380 | if (reply.ret_code != LTTNG_OK) { |
| 1381 | ret = -1; |
| 1382 | ERR("Relayd trace chunk create replied error %d", |
| 1383 | reply.ret_code); |
| 1384 | } else { |
| 1385 | ret = 0; |
| 1386 | DBG("Relayd successfully created trace chunk: chunk_id = %" PRIu64, |
| 1387 | chunk_id); |
| 1388 | } |
| 1389 | |
| 1390 | end: |
| 1391 | lttng_dynamic_buffer_reset(&payload); |
| 1392 | return ret; |
| 1393 | } |
| 1394 | |
| 1395 | int relayd_close_trace_chunk(struct lttcomm_relayd_sock *sock, |
| 1396 | struct lttng_trace_chunk *chunk, |
| 1397 | char *path) |
| 1398 | { |
| 1399 | int ret = 0; |
| 1400 | enum lttng_trace_chunk_status status; |
| 1401 | struct lttcomm_relayd_close_trace_chunk msg = {}; |
| 1402 | struct lttcomm_relayd_close_trace_chunk_reply reply = {}; |
| 1403 | uint64_t chunk_id; |
| 1404 | time_t close_timestamp; |
| 1405 | LTTNG_OPTIONAL(enum lttng_trace_chunk_command_type) close_command = {}; |
| 1406 | |
| 1407 | if (!relayd_supports_chunks(sock)) { |
| 1408 | DBG("Refusing to close remote trace chunk: relayd does not support chunks"); |
| 1409 | goto end; |
| 1410 | } |
| 1411 | |
| 1412 | status = lttng_trace_chunk_get_id(chunk, &chunk_id); |
| 1413 | if (status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
| 1414 | ERR("Failed to get trace chunk id"); |
| 1415 | ret = -1; |
| 1416 | goto end; |
| 1417 | } |
| 1418 | |
| 1419 | status = lttng_trace_chunk_get_close_timestamp(chunk, &close_timestamp); |
| 1420 | if (status != LTTNG_TRACE_CHUNK_STATUS_OK) { |
| 1421 | ERR("Failed to get trace chunk close timestamp"); |
| 1422 | ret = -1; |
| 1423 | goto end; |
| 1424 | } |
| 1425 | |
| 1426 | status = lttng_trace_chunk_get_close_command(chunk, |
| 1427 | &close_command.value); |
| 1428 | switch (status) { |
| 1429 | case LTTNG_TRACE_CHUNK_STATUS_OK: |
| 1430 | close_command.is_set = 1; |
| 1431 | break; |
| 1432 | case LTTNG_TRACE_CHUNK_STATUS_NONE: |
| 1433 | break; |
| 1434 | default: |
| 1435 | ERR("Failed to get trace chunk close command"); |
| 1436 | ret = -1; |
| 1437 | goto end; |
| 1438 | } |
| 1439 | |
| 1440 | msg = (typeof(msg)){ |
| 1441 | .chunk_id = htobe64(chunk_id), |
| 1442 | .close_timestamp = htobe64((uint64_t) close_timestamp), |
| 1443 | .close_command = { |
| 1444 | .value = htobe32((uint32_t) close_command.value), |
| 1445 | .is_set = close_command.is_set, |
| 1446 | }, |
| 1447 | }; |
| 1448 | |
| 1449 | ret = send_command(sock, RELAYD_CLOSE_TRACE_CHUNK, &msg, sizeof(msg), |
| 1450 | 0); |
| 1451 | if (ret < 0) { |
| 1452 | ERR("Failed to send trace chunk close command to relay daemon"); |
| 1453 | goto end; |
| 1454 | } |
| 1455 | |
| 1456 | ret = recv_reply(sock, &reply, sizeof(reply)); |
| 1457 | if (ret < 0) { |
| 1458 | ERR("Failed to receive relay daemon trace chunk close command reply"); |
| 1459 | goto end; |
| 1460 | } |
| 1461 | |
| 1462 | reply.path_length = be32toh(reply.path_length); |
| 1463 | if (reply.path_length >= LTTNG_PATH_MAX) { |
| 1464 | ERR("Chunk path too long"); |
| 1465 | ret = -1; |
| 1466 | goto end; |
| 1467 | } |
| 1468 | |
| 1469 | ret = recv_reply(sock, path, reply.path_length); |
| 1470 | if (ret < 0) { |
| 1471 | ERR("Failed to receive relay daemon trace chunk close command reply"); |
| 1472 | goto end; |
| 1473 | } |
| 1474 | if (path[reply.path_length - 1] != '\0') { |
| 1475 | ERR("Invalid trace chunk path returned by relay daemon (not null-terminated)"); |
| 1476 | ret = -1; |
| 1477 | goto end; |
| 1478 | } |
| 1479 | |
| 1480 | reply.generic.ret_code = be32toh(reply.generic.ret_code); |
| 1481 | if (reply.generic.ret_code != LTTNG_OK) { |
| 1482 | ret = -1; |
| 1483 | ERR("Relayd trace chunk close replied error %d", |
| 1484 | reply.generic.ret_code); |
| 1485 | } else { |
| 1486 | ret = 0; |
| 1487 | DBG("Relayd successfully closed trace chunk: chunk_id = %" PRIu64, |
| 1488 | chunk_id); |
| 1489 | } |
| 1490 | end: |
| 1491 | return ret; |
| 1492 | } |
| 1493 | |
| 1494 | int relayd_trace_chunk_exists(struct lttcomm_relayd_sock *sock, |
| 1495 | uint64_t chunk_id, bool *chunk_exists) |
| 1496 | { |
| 1497 | int ret = 0; |
| 1498 | struct lttcomm_relayd_trace_chunk_exists msg = {}; |
| 1499 | struct lttcomm_relayd_trace_chunk_exists_reply reply = {}; |
| 1500 | |
| 1501 | if (!relayd_supports_chunks(sock)) { |
| 1502 | DBG("Refusing to check for trace chunk existence: relayd does not support chunks"); |
| 1503 | /* The chunk will never exist */ |
| 1504 | *chunk_exists = false; |
| 1505 | goto end; |
| 1506 | } |
| 1507 | |
| 1508 | msg = (typeof(msg)){ |
| 1509 | .chunk_id = htobe64(chunk_id), |
| 1510 | }; |
| 1511 | |
| 1512 | ret = send_command(sock, RELAYD_TRACE_CHUNK_EXISTS, &msg, sizeof(msg), |
| 1513 | 0); |
| 1514 | if (ret < 0) { |
| 1515 | ERR("Failed to send trace chunk exists command to relay daemon"); |
| 1516 | goto end; |
| 1517 | } |
| 1518 | |
| 1519 | ret = recv_reply(sock, &reply, sizeof(reply)); |
| 1520 | if (ret < 0) { |
| 1521 | ERR("Failed to receive relay daemon trace chunk close command reply"); |
| 1522 | goto end; |
| 1523 | } |
| 1524 | |
| 1525 | reply.generic.ret_code = be32toh(reply.generic.ret_code); |
| 1526 | if (reply.generic.ret_code != LTTNG_OK) { |
| 1527 | ret = -1; |
| 1528 | ERR("Relayd trace chunk close replied error %d", |
| 1529 | reply.generic.ret_code); |
| 1530 | } else { |
| 1531 | ret = 0; |
| 1532 | DBG("Relayd successfully checked trace chunk existence: chunk_id = %" PRIu64 |
| 1533 | ", exists = %s", chunk_id, |
| 1534 | reply.trace_chunk_exists ? "true" : "false"); |
| 1535 | *chunk_exists = !!reply.trace_chunk_exists; |
| 1536 | } |
| 1537 | end: |
| 1538 | return ret; |
| 1539 | } |
| 1540 | |
| 1541 | int relayd_get_configuration(struct lttcomm_relayd_sock *sock, |
| 1542 | uint64_t query_flags, |
| 1543 | uint64_t *result_flags) |
| 1544 | { |
| 1545 | int ret = 0; |
| 1546 | struct lttcomm_relayd_get_configuration msg = (typeof(msg)) { |
| 1547 | .query_flags = htobe64(query_flags), |
| 1548 | }; |
| 1549 | struct lttcomm_relayd_get_configuration_reply reply = {}; |
| 1550 | |
| 1551 | if (!relayd_supports_get_configuration(sock)) { |
| 1552 | DBG("Refusing to get relayd configuration (unsupported by relayd)"); |
| 1553 | if (query_flags) { |
| 1554 | ret = -1; |
| 1555 | goto end; |
| 1556 | } |
| 1557 | *result_flags = 0; |
| 1558 | goto end; |
| 1559 | } |
| 1560 | |
| 1561 | ret = send_command(sock, RELAYD_GET_CONFIGURATION, &msg, sizeof(msg), |
| 1562 | 0); |
| 1563 | if (ret < 0) { |
| 1564 | ERR("Failed to send get configuration command to relay daemon"); |
| 1565 | goto end; |
| 1566 | } |
| 1567 | |
| 1568 | ret = recv_reply(sock, &reply, sizeof(reply)); |
| 1569 | if (ret < 0) { |
| 1570 | ERR("Failed to receive relay daemon get configuration command reply"); |
| 1571 | goto end; |
| 1572 | } |
| 1573 | |
| 1574 | reply.generic.ret_code = be32toh(reply.generic.ret_code); |
| 1575 | if (reply.generic.ret_code != LTTNG_OK) { |
| 1576 | ret = -1; |
| 1577 | ERR("Relayd get configuration replied error %d", |
| 1578 | reply.generic.ret_code); |
| 1579 | } else { |
| 1580 | reply.relayd_configuration_flags = |
| 1581 | be64toh(reply.relayd_configuration_flags); |
| 1582 | ret = 0; |
| 1583 | DBG("Relayd successfully got configuration: query_flags = %" PRIu64 |
| 1584 | ", results_flags = %" PRIu64, query_flags, |
| 1585 | reply.relayd_configuration_flags); |
| 1586 | *result_flags = reply.relayd_configuration_flags; |
| 1587 | } |
| 1588 | end: |
| 1589 | return ret; |
| 1590 | } |