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