| 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 | #include <errno.h> |
| 20 | #include <stdio.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | #include <unistd.h> |
| 24 | |
| 25 | #include <common/common.h> |
| 26 | #include <common/consumer.h> |
| 27 | #include <common/defaults.h> |
| 28 | |
| 29 | #include "consumer.h" |
| 30 | #include "ust-consumer.h" |
| 31 | |
| 32 | /* |
| 33 | * Return allocated full pathname of the session using the consumer trace path |
| 34 | * and subdir if available. On a successful allocation, the directory of the |
| 35 | * trace is created with the session credentials. |
| 36 | * |
| 37 | * The caller can safely free(3) the returned value. On error, NULL is |
| 38 | * returned. |
| 39 | */ |
| 40 | static char *setup_trace_path(struct consumer_output *consumer, |
| 41 | struct ust_app_session *ua_sess) |
| 42 | { |
| 43 | int ret; |
| 44 | char *pathname; |
| 45 | |
| 46 | assert(consumer); |
| 47 | assert(ua_sess); |
| 48 | |
| 49 | health_code_update(); |
| 50 | |
| 51 | /* Allocate our self the string to make sure we never exceed PATH_MAX. */ |
| 52 | pathname = zmalloc(PATH_MAX); |
| 53 | if (!pathname) { |
| 54 | goto error; |
| 55 | } |
| 56 | |
| 57 | /* Get correct path name destination */ |
| 58 | if (consumer->type == CONSUMER_DST_LOCAL) { |
| 59 | /* Set application path to the destination path */ |
| 60 | ret = snprintf(pathname, PATH_MAX, "%s/%s/%s", |
| 61 | consumer->dst.trace_path, consumer->subdir, ua_sess->path); |
| 62 | if (ret < 0) { |
| 63 | PERROR("snprintf channel path"); |
| 64 | goto error; |
| 65 | } |
| 66 | |
| 67 | /* Create directory. Ignore if exist. */ |
| 68 | ret = run_as_mkdir_recursive(pathname, S_IRWXU | S_IRWXG, ua_sess->uid, |
| 69 | ua_sess->gid); |
| 70 | if (ret < 0) { |
| 71 | if (ret != -EEXIST) { |
| 72 | ERR("Trace directory creation error"); |
| 73 | goto error; |
| 74 | } |
| 75 | } |
| 76 | } else { |
| 77 | ret = snprintf(pathname, PATH_MAX, "%s/%s", consumer->subdir, |
| 78 | ua_sess->path); |
| 79 | if (ret < 0) { |
| 80 | PERROR("snprintf channel path"); |
| 81 | goto error; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | return pathname; |
| 86 | |
| 87 | error: |
| 88 | free(pathname); |
| 89 | return NULL; |
| 90 | } |
| 91 | |
| 92 | /* |
| 93 | * Send a single channel to the consumer using command ADD_CHANNEL. |
| 94 | * |
| 95 | * Consumer socket MUST be acquired before calling this. |
| 96 | */ |
| 97 | static int ask_channel_creation(struct ust_app_session *ua_sess, |
| 98 | struct ust_app_channel *ua_chan, struct consumer_output *consumer, |
| 99 | struct consumer_socket *socket) |
| 100 | { |
| 101 | int ret; |
| 102 | unsigned long key; |
| 103 | char *pathname = NULL; |
| 104 | struct lttcomm_consumer_msg msg; |
| 105 | |
| 106 | assert(ua_sess); |
| 107 | assert(ua_chan); |
| 108 | assert(socket); |
| 109 | assert(consumer); |
| 110 | |
| 111 | DBG2("Asking UST consumer for channel"); |
| 112 | |
| 113 | /* Get and create full trace path of session. */ |
| 114 | pathname = setup_trace_path(consumer, ua_sess); |
| 115 | if (!pathname) { |
| 116 | ret = -1; |
| 117 | goto error; |
| 118 | } |
| 119 | |
| 120 | consumer_init_ask_channel_comm_msg(&msg, |
| 121 | ua_chan->attr.subbuf_size, |
| 122 | ua_chan->attr.num_subbuf, |
| 123 | ua_chan->attr.overwrite, |
| 124 | ua_chan->attr.switch_timer_interval, |
| 125 | ua_chan->attr.read_timer_interval, |
| 126 | (int) ua_chan->attr.output, |
| 127 | (int) ua_chan->attr.type, |
| 128 | ua_sess->id, |
| 129 | pathname, |
| 130 | ua_chan->name, |
| 131 | ua_sess->uid, |
| 132 | ua_sess->gid, |
| 133 | consumer->net_seq_index, |
| 134 | ua_chan->key, |
| 135 | ua_sess->registry.uuid); |
| 136 | |
| 137 | health_code_update(); |
| 138 | |
| 139 | ret = lttcomm_send_unix_sock(socket->fd, &msg, sizeof(msg)); |
| 140 | if (ret < 0) { |
| 141 | goto error; |
| 142 | } |
| 143 | |
| 144 | ret = consumer_recv_status_channel(socket, &key, |
| 145 | &ua_chan->expected_stream_count); |
| 146 | if (ret < 0) { |
| 147 | goto error; |
| 148 | } |
| 149 | /* Communication protocol error. */ |
| 150 | assert(key == ua_chan->key); |
| 151 | /* We need at least one where 1 stream for 1 cpu. */ |
| 152 | assert(ua_chan->expected_stream_count > 0); |
| 153 | |
| 154 | DBG2("UST ask channel %lu successfully done with %u stream(s)", key, |
| 155 | ua_chan->expected_stream_count); |
| 156 | |
| 157 | error: |
| 158 | free(pathname); |
| 159 | health_code_update(); |
| 160 | return ret; |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | * Ask consumer to create a channel for a given session. |
| 165 | * |
| 166 | * Returns 0 on success else a negative value. |
| 167 | */ |
| 168 | int ust_consumer_ask_channel(struct ust_app_session *ua_sess, |
| 169 | struct ust_app_channel *ua_chan, struct consumer_output *consumer, |
| 170 | struct consumer_socket *socket) |
| 171 | { |
| 172 | int ret; |
| 173 | |
| 174 | assert(ua_sess); |
| 175 | assert(ua_chan); |
| 176 | assert(consumer); |
| 177 | assert(socket); |
| 178 | assert(socket->fd >= 0); |
| 179 | |
| 180 | pthread_mutex_lock(socket->lock); |
| 181 | |
| 182 | ret = ask_channel_creation(ua_sess, ua_chan, consumer, socket); |
| 183 | if (ret < 0) { |
| 184 | goto error; |
| 185 | } |
| 186 | |
| 187 | error: |
| 188 | pthread_mutex_unlock(socket->lock); |
| 189 | return ret; |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | * Send a get channel command to consumer using the given channel key. The |
| 194 | * channel object is populated and the stream list. |
| 195 | * |
| 196 | * Return 0 on success else a negative value. |
| 197 | */ |
| 198 | int ust_consumer_get_channel(struct consumer_socket *socket, |
| 199 | struct ust_app_channel *ua_chan) |
| 200 | { |
| 201 | int ret; |
| 202 | struct lttcomm_consumer_msg msg; |
| 203 | |
| 204 | assert(ua_chan); |
| 205 | assert(socket); |
| 206 | assert(socket->fd >= 0); |
| 207 | |
| 208 | msg.cmd_type = LTTNG_CONSUMER_GET_CHANNEL; |
| 209 | msg.u.get_channel.key = ua_chan->key; |
| 210 | |
| 211 | pthread_mutex_lock(socket->lock); |
| 212 | health_code_update(); |
| 213 | |
| 214 | /* Send command and wait for OK reply. */ |
| 215 | ret = consumer_send_msg(socket, &msg); |
| 216 | if (ret < 0) { |
| 217 | goto error; |
| 218 | } |
| 219 | |
| 220 | /* First, get the channel from consumer. */ |
| 221 | ret = ustctl_recv_channel_from_consumer(socket->fd, &ua_chan->obj); |
| 222 | if (ret < 0) { |
| 223 | if (ret != -EPIPE) { |
| 224 | ERR("Error recv channel from consumer %d with ret %d", |
| 225 | socket->fd, ret); |
| 226 | } else { |
| 227 | DBG3("UST app recv channel from consumer. Consumer is dead."); |
| 228 | } |
| 229 | goto error; |
| 230 | } |
| 231 | |
| 232 | /* Next, get all streams. */ |
| 233 | while (1) { |
| 234 | struct ust_app_stream *stream; |
| 235 | |
| 236 | /* Create UST stream */ |
| 237 | stream = ust_app_alloc_stream(); |
| 238 | if (stream == NULL) { |
| 239 | ret = -ENOMEM; |
| 240 | goto error; |
| 241 | } |
| 242 | |
| 243 | /* Stream object is populated by this call if successful. */ |
| 244 | ret = ustctl_recv_stream_from_consumer(socket->fd, &stream->obj); |
| 245 | if (ret < 0) { |
| 246 | free(stream); |
| 247 | if (ret == -LTTNG_UST_ERR_NOENT) { |
| 248 | DBG3("UST app consumer has no more stream available"); |
| 249 | ret = 0; |
| 250 | break; |
| 251 | } |
| 252 | if (ret != -EPIPE) { |
| 253 | ERR("Recv stream from consumer %d with ret %d", |
| 254 | socket->fd, ret); |
| 255 | } else { |
| 256 | DBG3("UST app recv stream from consumer. Consumer is dead."); |
| 257 | } |
| 258 | goto error; |
| 259 | } |
| 260 | |
| 261 | /* Order is important this is why a list is used. */ |
| 262 | cds_list_add_tail(&stream->list, &ua_chan->streams.head); |
| 263 | ua_chan->streams.count++; |
| 264 | |
| 265 | DBG2("UST app stream %d received succesfully", ua_chan->streams.count); |
| 266 | } |
| 267 | |
| 268 | /* This MUST match or else we have a synchronization problem. */ |
| 269 | assert(ua_chan->expected_stream_count == ua_chan->streams.count); |
| 270 | |
| 271 | /* Wait for confirmation that we can proceed with the streams. */ |
| 272 | ret = consumer_recv_status_reply(socket); |
| 273 | if (ret < 0) { |
| 274 | goto error; |
| 275 | } |
| 276 | |
| 277 | error: |
| 278 | health_code_update(); |
| 279 | pthread_mutex_unlock(socket->lock); |
| 280 | return ret; |
| 281 | } |
| 282 | |
| 283 | /* |
| 284 | * Send a destroy channel command to consumer using the given channel key. |
| 285 | * |
| 286 | * Note that this command MUST be used prior to a successful |
| 287 | * LTTNG_CONSUMER_GET_CHANNEL because once this command is done successfully, |
| 288 | * the streams are dispatched to the consumer threads and MUST be teardown |
| 289 | * through the hang up process. |
| 290 | * |
| 291 | * Return 0 on success else a negative value. |
| 292 | */ |
| 293 | int ust_consumer_destroy_channel(struct consumer_socket *socket, |
| 294 | struct ust_app_channel *ua_chan) |
| 295 | { |
| 296 | int ret; |
| 297 | struct lttcomm_consumer_msg msg; |
| 298 | |
| 299 | assert(ua_chan); |
| 300 | assert(socket); |
| 301 | assert(socket->fd >= 0); |
| 302 | |
| 303 | msg.cmd_type = LTTNG_CONSUMER_DESTROY_CHANNEL; |
| 304 | msg.u.destroy_channel.key = ua_chan->key; |
| 305 | |
| 306 | pthread_mutex_lock(socket->lock); |
| 307 | health_code_update(); |
| 308 | |
| 309 | ret = consumer_send_msg(socket, &msg); |
| 310 | if (ret < 0) { |
| 311 | goto error; |
| 312 | } |
| 313 | |
| 314 | error: |
| 315 | health_code_update(); |
| 316 | pthread_mutex_unlock(socket->lock); |
| 317 | return ret; |
| 318 | } |
| 319 | |
| 320 | /* |
| 321 | * Send a given stream to UST tracer. |
| 322 | * |
| 323 | * On success return 0 else a negative value. |
| 324 | */ |
| 325 | int ust_consumer_send_stream_to_ust(struct ust_app *app, |
| 326 | struct ust_app_channel *channel, struct ust_app_stream *stream) |
| 327 | { |
| 328 | int ret; |
| 329 | |
| 330 | assert(app); |
| 331 | assert(stream); |
| 332 | assert(channel); |
| 333 | |
| 334 | DBG2("UST consumer send stream to app %d", app->sock); |
| 335 | |
| 336 | /* Relay stream to application. */ |
| 337 | ret = ustctl_send_stream_to_ust(app->sock, channel->obj, stream->obj); |
| 338 | if (ret < 0) { |
| 339 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 340 | ERR("Error ustctl send stream %s to app pid: %d with ret %d", |
| 341 | stream->name, app->pid, ret); |
| 342 | } else { |
| 343 | DBG3("UST app send stream to ust failed. Application is dead."); |
| 344 | } |
| 345 | goto error; |
| 346 | } |
| 347 | channel->handle = channel->obj->handle; |
| 348 | |
| 349 | error: |
| 350 | return ret; |
| 351 | } |
| 352 | |
| 353 | /* |
| 354 | * Send channel previously received from the consumer to the UST tracer. |
| 355 | * |
| 356 | * On success return 0 else a negative value. |
| 357 | */ |
| 358 | int ust_consumer_send_channel_to_ust(struct ust_app *app, |
| 359 | struct ust_app_session *ua_sess, struct ust_app_channel *channel) |
| 360 | { |
| 361 | int ret; |
| 362 | |
| 363 | assert(app); |
| 364 | assert(ua_sess); |
| 365 | assert(channel); |
| 366 | assert(channel->obj); |
| 367 | |
| 368 | DBG2("UST app send channel to app sock %d pid %d (name: %s, key: %lu)", |
| 369 | app->sock, app->pid, channel->name, channel->key); |
| 370 | |
| 371 | /* Send stream to application. */ |
| 372 | ret = ustctl_send_channel_to_ust(app->sock, ua_sess->handle, channel->obj); |
| 373 | if (ret < 0) { |
| 374 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
| 375 | ERR("Error ustctl send channel %s to app pid: %d with ret %d", |
| 376 | channel->name, app->pid, ret); |
| 377 | } else { |
| 378 | DBG3("UST app send channel to ust failed. Application is dead."); |
| 379 | } |
| 380 | goto error; |
| 381 | } |
| 382 | |
| 383 | error: |
| 384 | return ret; |
| 385 | } |