| 1 | /* |
| 2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License, version 2 only, |
| 6 | * as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 | * GNU General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License along |
| 14 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 16 | */ |
| 17 | |
| 18 | #define _GNU_SOURCE |
| 19 | #define _LGPL_SOURCE |
| 20 | #include <errno.h> |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <string.h> |
| 24 | #include <unistd.h> |
| 25 | #include <inttypes.h> |
| 26 | |
| 27 | #include <common/common.h> |
| 28 | #include <common/consumer.h> |
| 29 | #include <common/defaults.h> |
| 30 | |
| 31 | #include "consumer.h" |
| 32 | #include "health-sessiond.h" |
| 33 | #include "ust-consumer.h" |
| 34 | #include "buffer-registry.h" |
| 35 | #include "session.h" |
| 36 | |
| 37 | /* |
| 38 | * Return allocated full pathname of the session using the consumer trace path |
| 39 | * and subdir if available. On a successful allocation, the directory of the |
| 40 | * trace is created with the session credentials. |
| 41 | * |
| 42 | * The caller can safely free(3) the returned value. On error, NULL is |
| 43 | * returned. |
| 44 | */ |
| 45 | static char *setup_trace_path(struct consumer_output *consumer, |
| 46 | struct ust_app_session *ua_sess) |
| 47 | { |
| 48 | int ret; |
| 49 | char *pathname; |
| 50 | |
| 51 | assert(consumer); |
| 52 | assert(ua_sess); |
| 53 | |
| 54 | health_code_update(); |
| 55 | |
| 56 | /* Allocate our self the string to make sure we never exceed PATH_MAX. */ |
| 57 | pathname = zmalloc(PATH_MAX); |
| 58 | if (!pathname) { |
| 59 | goto error; |
| 60 | } |
| 61 | |
| 62 | /* Get correct path name destination */ |
| 63 | if (consumer->type == CONSUMER_DST_LOCAL) { |
| 64 | /* Set application path to the destination path */ |
| 65 | ret = snprintf(pathname, PATH_MAX, "%s%s%s", |
| 66 | consumer->dst.trace_path, consumer->subdir, ua_sess->path); |
| 67 | if (ret < 0) { |
| 68 | PERROR("snprintf channel path"); |
| 69 | goto error; |
| 70 | } |
| 71 | |
| 72 | /* Create directory. Ignore if exist. */ |
| 73 | ret = run_as_mkdir_recursive(pathname, S_IRWXU | S_IRWXG, |
| 74 | ua_sess->euid, ua_sess->egid); |
| 75 | if (ret < 0) { |
| 76 | if (errno != EEXIST) { |
| 77 | ERR("Trace directory creation error"); |
| 78 | goto error; |
| 79 | } |
| 80 | } |
| 81 | } else { |
| 82 | ret = snprintf(pathname, PATH_MAX, "%s%s", consumer->subdir, |
| 83 | ua_sess->path); |
| 84 | if (ret < 0) { |
| 85 | PERROR("snprintf channel path"); |
| 86 | goto error; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | return pathname; |
| 91 | |
| 92 | error: |
| 93 | free(pathname); |
| 94 | return NULL; |
| 95 | } |
| 96 | |
| 97 | /* |
| 98 | * Send a single channel to the consumer using command ADD_CHANNEL. |
| 99 | * |
| 100 | * Consumer socket lock MUST be acquired before calling this. |
| 101 | */ |
| 102 | static int ask_channel_creation(struct ust_app_session *ua_sess, |
| 103 | struct ust_app_channel *ua_chan, struct consumer_output *consumer, |
| 104 | struct consumer_socket *socket, struct ust_registry_session *registry) |
| 105 | { |
| 106 | int ret, output; |
| 107 | uint32_t chan_id; |
| 108 | uint64_t key, chan_reg_key; |
| 109 | char *pathname = NULL; |
| 110 | struct lttcomm_consumer_msg msg; |
| 111 | struct ust_registry_channel *chan_reg; |
| 112 | char shm_path[PATH_MAX] = ""; |
| 113 | char root_shm_path[PATH_MAX] = ""; |
| 114 | |
| 115 | assert(ua_sess); |
| 116 | assert(ua_chan); |
| 117 | assert(socket); |
| 118 | assert(consumer); |
| 119 | assert(registry); |
| 120 | |
| 121 | DBG2("Asking UST consumer for channel"); |
| 122 | |
| 123 | /* Get and create full trace path of session. */ |
| 124 | if (ua_sess->output_traces) { |
| 125 | pathname = setup_trace_path(consumer, ua_sess); |
| 126 | if (!pathname) { |
| 127 | ret = -1; |
| 128 | goto error; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /* Depending on the buffer type, a different channel key is used. */ |
| 133 | if (ua_sess->buffer_type == LTTNG_BUFFER_PER_UID) { |
| 134 | chan_reg_key = ua_chan->tracing_channel_id; |
| 135 | } else { |
| 136 | chan_reg_key = ua_chan->key; |
| 137 | } |
| 138 | |
| 139 | if (ua_chan->attr.type == LTTNG_UST_CHAN_METADATA) { |
| 140 | chan_id = -1U; |
| 141 | /* |
| 142 | * Metadata channels shm_path (buffers) are handled within |
| 143 | * session daemon. Consumer daemon should not try to create |
| 144 | * those buffer files. |
| 145 | */ |
| 146 | } else { |
| 147 | chan_reg = ust_registry_channel_find(registry, chan_reg_key); |
| 148 | assert(chan_reg); |
| 149 | chan_id = chan_reg->chan_id; |
| 150 | if (ua_sess->shm_path[0]) { |
| 151 | strncpy(shm_path, ua_sess->shm_path, sizeof(shm_path)); |
| 152 | shm_path[sizeof(shm_path) - 1] = '\0'; |
| 153 | strncat(shm_path, "/", |
| 154 | sizeof(shm_path) - strlen(shm_path) - 1); |
| 155 | strncat(shm_path, ua_chan->name, |
| 156 | sizeof(shm_path) - strlen(shm_path) - 1); |
| 157 | strncat(shm_path, "_", |
| 158 | sizeof(shm_path) - strlen(shm_path) - 1); |
| 159 | } |
| 160 | strncpy(root_shm_path, ua_sess->root_shm_path, sizeof(root_shm_path)); |
| 161 | root_shm_path[sizeof(root_shm_path) - 1] = '\0'; |
| 162 | } |
| 163 | |
| 164 | switch (ua_chan->attr.output) { |
| 165 | case LTTNG_UST_MMAP: |
| 166 | default: |
| 167 | output = LTTNG_EVENT_MMAP; |
| 168 | break; |
| 169 | } |
| 170 | |
| 171 | consumer_init_ask_channel_comm_msg(&msg, |
| 172 | ua_chan->attr.subbuf_size, |
| 173 | ua_chan->attr.num_subbuf, |
| 174 | ua_chan->attr.overwrite, |
| 175 | ua_chan->attr.switch_timer_interval, |
| 176 | ua_chan->attr.read_timer_interval, |
| 177 | ua_sess->live_timer_interval, |
| 178 | output, |
| 179 | (int) ua_chan->attr.type, |
| 180 | ua_sess->tracing_id, |
| 181 | pathname, |
| 182 | ua_chan->name, |
| 183 | ua_sess->euid, |
| 184 | ua_sess->egid, |
| 185 | consumer->net_seq_index, |
| 186 | ua_chan->key, |
| 187 | registry->uuid, |
| 188 | chan_id, |
| 189 | ua_chan->tracefile_size, |
| 190 | ua_chan->tracefile_count, |
| 191 | ua_sess->id, |
| 192 | ua_sess->output_traces, |
| 193 | ua_sess->uid, |
| 194 | root_shm_path, shm_path); |
| 195 | |
| 196 | health_code_update(); |
| 197 | |
| 198 | ret = consumer_socket_send(socket, &msg, sizeof(msg)); |
| 199 | if (ret < 0) { |
| 200 | goto error; |
| 201 | } |
| 202 | |
| 203 | ret = consumer_recv_status_channel(socket, &key, |
| 204 | &ua_chan->expected_stream_count); |
| 205 | if (ret < 0) { |
| 206 | goto error; |
| 207 | } |
| 208 | /* Communication protocol error. */ |
| 209 | assert(key == ua_chan->key); |
| 210 | /* We need at least one where 1 stream for 1 cpu. */ |
| 211 | if (ua_sess->output_traces) { |
| 212 | assert(ua_chan->expected_stream_count > 0); |
| 213 | } |
| 214 | |
| 215 | DBG2("UST ask channel %" PRIu64 " successfully done with %u stream(s)", key, |
| 216 | ua_chan->expected_stream_count); |
| 217 | |
| 218 | error: |
| 219 | free(pathname); |
| 220 | health_code_update(); |
| 221 | return ret; |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * Ask consumer to create a channel for a given session. |
| 226 | * |
| 227 | * Returns 0 on success else a negative value. |
| 228 | */ |
| 229 | int ust_consumer_ask_channel(struct ust_app_session *ua_sess, |
| 230 | struct ust_app_channel *ua_chan, struct consumer_output *consumer, |
| 231 | struct consumer_socket *socket, struct ust_registry_session *registry) |
| 232 | { |
| 233 | int ret; |
| 234 | |
| 235 | assert(ua_sess); |
| 236 | assert(ua_chan); |
| 237 | assert(consumer); |
| 238 | assert(socket); |
| 239 | assert(registry); |
| 240 | |
| 241 | if (!consumer->enabled) { |
| 242 | ret = -LTTNG_ERR_NO_CONSUMER; |
| 243 | DBG3("Consumer is disabled"); |
| 244 | goto error; |
| 245 | } |
| 246 | |
| 247 | pthread_mutex_lock(socket->lock); |
| 248 | ret = ask_channel_creation(ua_sess, ua_chan, consumer, socket, registry); |
| 249 | pthread_mutex_unlock(socket->lock); |
| 250 | if (ret < 0) { |
| 251 | goto error; |
| 252 | } |
| 253 | |
| 254 | error: |
| 255 | return ret; |
| 256 | } |
| 257 | |
| 258 | /* |
| 259 | * Send a get channel command to consumer using the given channel key. The |
| 260 | * channel object is populated and the stream list. |
| 261 | * |
| 262 | * Return 0 on success else a negative value. |
| 263 | */ |
| 264 | int ust_consumer_get_channel(struct consumer_socket *socket, |
| 265 | struct ust_app_channel *ua_chan) |
| 266 | { |
| 267 | int ret; |
| 268 | struct lttcomm_consumer_msg msg; |
| 269 | |
| 270 | assert(ua_chan); |
| 271 | assert(socket); |
| 272 | |
| 273 | memset(&msg, 0, sizeof(msg)); |
| 274 | msg.cmd_type = LTTNG_CONSUMER_GET_CHANNEL; |
| 275 | msg.u.get_channel.key = ua_chan->key; |
| 276 | |
| 277 | pthread_mutex_lock(socket->lock); |
| 278 | health_code_update(); |
| 279 | |
| 280 | /* Send command and wait for OK reply. */ |
| 281 | ret = consumer_send_msg(socket, &msg); |
| 282 | if (ret < 0) { |
| 283 | goto error; |
| 284 | } |
| 285 | |
| 286 | /* First, get the channel from consumer. */ |
| 287 | ret = ustctl_recv_channel_from_consumer(*socket->fd_ptr, &ua_chan->obj); |
| 288 | if (ret < 0) { |
| 289 | if (ret != -EPIPE) { |
| 290 | ERR("Error recv channel from consumer %d with ret %d", |
| 291 | *socket->fd_ptr, ret); |
| 292 | } else { |
| 293 | DBG3("UST app recv channel from consumer. Consumer is dead."); |
| 294 | } |
| 295 | goto error; |
| 296 | } |
| 297 | |
| 298 | /* Next, get all streams. */ |
| 299 | while (1) { |
| 300 | struct ust_app_stream *stream; |
| 301 | |
| 302 | /* Create UST stream */ |
| 303 | stream = ust_app_alloc_stream(); |
| 304 | if (stream == NULL) { |
| 305 | ret = -ENOMEM; |
| 306 | goto error; |
| 307 | } |
| 308 | |
| 309 | /* Stream object is populated by this call if successful. */ |
| 310 | ret = ustctl_recv_stream_from_consumer(*socket->fd_ptr, &stream->obj); |
| 311 | if (ret < 0) { |
| 312 | free(stream); |
| 313 | if (ret == -LTTNG_UST_ERR_NOENT) { |
| 314 | DBG3("UST app consumer has no more stream available"); |
| 315 | ret = 0; |
| 316 | break; |
| 317 | } |
| 318 | if (ret != -EPIPE) { |
| 319 | ERR("Recv stream from consumer %d with ret %d", |
| 320 | *socket->fd_ptr, ret); |
| 321 | } else { |
| 322 | DBG3("UST app recv stream from consumer. Consumer is dead."); |
| 323 | } |
| 324 | goto error; |
| 325 | } |
| 326 | |
| 327 | /* Order is important this is why a list is used. */ |
| 328 | cds_list_add_tail(&stream->list, &ua_chan->streams.head); |
| 329 | ua_chan->streams.count++; |
| 330 | |
| 331 | DBG2("UST app stream %d received successfully", ua_chan->streams.count); |
| 332 | } |
| 333 | |
| 334 | /* This MUST match or else we have a synchronization problem. */ |
| 335 | assert(ua_chan->expected_stream_count == ua_chan->streams.count); |
| 336 | |
| 337 | /* Wait for confirmation that we can proceed with the streams. */ |
| 338 | ret = consumer_recv_status_reply(socket); |
| 339 | if (ret < 0) { |
| 340 | goto error; |
| 341 | } |
| 342 | |
| 343 | error: |
| 344 | health_code_update(); |
| 345 | pthread_mutex_unlock(socket->lock); |
| 346 | return ret; |
| 347 | } |
| 348 | |
| 349 | /* |
| 350 | * Send a destroy channel command to consumer using the given channel key. |
| 351 | * |
| 352 | * Note that this command MUST be used prior to a successful |
| 353 | * LTTNG_CONSUMER_GET_CHANNEL because once this command is done successfully, |
| 354 | * the streams are dispatched to the consumer threads and MUST be teardown |
| 355 | * through the hang up process. |
| 356 | * |
| 357 | * Return 0 on success else a negative value. |
| 358 | */ |
| 359 | int ust_consumer_destroy_channel(struct consumer_socket *socket, |
| 360 | struct ust_app_channel *ua_chan) |
| 361 | { |
| 362 | int ret; |
| 363 | struct lttcomm_consumer_msg msg; |
| 364 | |
| 365 | assert(ua_chan); |
| 366 | assert(socket); |
| 367 | |
| 368 | memset(&msg, 0, sizeof(msg)); |
| 369 | msg.cmd_type = LTTNG_CONSUMER_DESTROY_CHANNEL; |
| 370 | msg.u.destroy_channel.key = ua_chan->key; |
| 371 | |
| 372 | pthread_mutex_lock(socket->lock); |
| 373 | health_code_update(); |
| 374 | |
| 375 | ret = consumer_send_msg(socket, &msg); |
| 376 | if (ret < 0) { |
| 377 | goto error; |
| 378 | } |
| 379 | |
| 380 | error: |
| 381 | health_code_update(); |
| 382 | pthread_mutex_unlock(socket->lock); |
| 383 | return ret; |
| 384 | } |
| 385 | |
| 386 | /* |
| 387 | * Send a given stream to UST tracer. |
| 388 | * |
| 389 | * On success return 0 else a negative value. |
| 390 | */ |
| 391 | int ust_consumer_send_stream_to_ust(struct ust_app *app, |
| 392 | struct ust_app_channel *channel, struct ust_app_stream *stream) |
| 393 | { |
| 394 | int ret; |
| 395 | |
| 396 | assert(app); |
| 397 | assert(stream); |
| 398 | assert(channel); |
| 399 | |
| 400 | DBG2("UST consumer send stream to app %d", app->sock); |
| 401 | |
| 402 | /* Relay stream to application. */ |
| 403 | ret = ustctl_send_stream_to_ust(app->sock, channel->obj, stream->obj); |
| 404 | if (ret < 0) { |
| 405 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 406 | ERR("ustctl send stream handle %d to app pid: %d with ret %d", |
| 407 | stream->obj->handle, app->pid, ret); |
| 408 | } else { |
| 409 | DBG3("UST app send stream to ust failed. Application is dead."); |
| 410 | } |
| 411 | goto error; |
| 412 | } |
| 413 | channel->handle = channel->obj->handle; |
| 414 | |
| 415 | error: |
| 416 | return ret; |
| 417 | } |
| 418 | |
| 419 | /* |
| 420 | * Send channel previously received from the consumer to the UST tracer. |
| 421 | * |
| 422 | * On success return 0 else a negative value. |
| 423 | */ |
| 424 | int ust_consumer_send_channel_to_ust(struct ust_app *app, |
| 425 | struct ust_app_session *ua_sess, struct ust_app_channel *channel) |
| 426 | { |
| 427 | int ret; |
| 428 | |
| 429 | assert(app); |
| 430 | assert(ua_sess); |
| 431 | assert(channel); |
| 432 | assert(channel->obj); |
| 433 | |
| 434 | DBG2("UST app send channel to sock %d pid %d (name: %s, key: %" PRIu64 ")", |
| 435 | app->sock, app->pid, channel->name, channel->tracing_channel_id); |
| 436 | |
| 437 | /* Send stream to application. */ |
| 438 | ret = ustctl_send_channel_to_ust(app->sock, ua_sess->handle, channel->obj); |
| 439 | if (ret < 0) { |
| 440 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 441 | ERR("Error ustctl send channel %s to app pid: %d with ret %d", |
| 442 | channel->name, app->pid, ret); |
| 443 | } else { |
| 444 | DBG3("UST app send channel to ust failed. Application is dead."); |
| 445 | } |
| 446 | goto error; |
| 447 | } |
| 448 | |
| 449 | error: |
| 450 | return ret; |
| 451 | } |
| 452 | |
| 453 | /* |
| 454 | * Handle the metadata requests from the UST consumer |
| 455 | * |
| 456 | * Return 0 on success else a negative value. |
| 457 | */ |
| 458 | int ust_consumer_metadata_request(struct consumer_socket *socket) |
| 459 | { |
| 460 | int ret; |
| 461 | ssize_t ret_push; |
| 462 | struct lttcomm_metadata_request_msg request; |
| 463 | struct buffer_reg_uid *reg_uid; |
| 464 | struct ust_registry_session *ust_reg; |
| 465 | struct lttcomm_consumer_msg msg; |
| 466 | |
| 467 | assert(socket); |
| 468 | |
| 469 | rcu_read_lock(); |
| 470 | health_code_update(); |
| 471 | |
| 472 | /* Wait for a metadata request */ |
| 473 | pthread_mutex_lock(socket->lock); |
| 474 | ret = consumer_socket_recv(socket, &request, sizeof(request)); |
| 475 | pthread_mutex_unlock(socket->lock); |
| 476 | if (ret < 0) { |
| 477 | goto end; |
| 478 | } |
| 479 | |
| 480 | DBG("Metadata request received for session %" PRIu64 ", key %" PRIu64, |
| 481 | request.session_id, request.key); |
| 482 | |
| 483 | reg_uid = buffer_reg_uid_find(request.session_id, |
| 484 | request.bits_per_long, request.uid); |
| 485 | if (reg_uid) { |
| 486 | ust_reg = reg_uid->registry->reg.ust; |
| 487 | } else { |
| 488 | struct buffer_reg_pid *reg_pid = |
| 489 | buffer_reg_pid_find(request.session_id_per_pid); |
| 490 | if (!reg_pid) { |
| 491 | DBG("PID registry not found for session id %" PRIu64, |
| 492 | request.session_id_per_pid); |
| 493 | |
| 494 | memset(&msg, 0, sizeof(msg)); |
| 495 | msg.cmd_type = LTTNG_ERR_UND; |
| 496 | (void) consumer_send_msg(socket, &msg); |
| 497 | /* |
| 498 | * This is possible since the session might have been destroyed |
| 499 | * during a consumer metadata request. So here, return gracefully |
| 500 | * because the destroy session will push the remaining metadata to |
| 501 | * the consumer. |
| 502 | */ |
| 503 | ret = 0; |
| 504 | goto end; |
| 505 | } |
| 506 | ust_reg = reg_pid->registry->reg.ust; |
| 507 | } |
| 508 | assert(ust_reg); |
| 509 | |
| 510 | pthread_mutex_lock(&ust_reg->lock); |
| 511 | ret_push = ust_app_push_metadata(ust_reg, socket, 1); |
| 512 | pthread_mutex_unlock(&ust_reg->lock); |
| 513 | if (ret_push == -EPIPE) { |
| 514 | DBG("Application or relay closed while pushing metadata"); |
| 515 | } else if (ret_push < 0) { |
| 516 | ERR("Pushing metadata"); |
| 517 | ret = -1; |
| 518 | goto end; |
| 519 | } else { |
| 520 | DBG("UST Consumer metadata pushed successfully"); |
| 521 | } |
| 522 | ret = 0; |
| 523 | |
| 524 | end: |
| 525 | rcu_read_unlock(); |
| 526 | return ret; |
| 527 | } |