2 * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
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.
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.
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.
27 #include <common/common.h>
28 #include <common/consumer.h>
29 #include <common/defaults.h>
32 #include "health-sessiond.h"
33 #include "ust-consumer.h"
34 #include "buffer-registry.h"
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.
42 * The caller can safely free(3) the returned value. On error, NULL is
45 static char *setup_trace_path(struct consumer_output
*consumer
,
46 struct ust_app_session
*ua_sess
)
56 /* Allocate our self the string to make sure we never exceed PATH_MAX. */
57 pathname
= zmalloc(PATH_MAX
);
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
);
68 PERROR("snprintf channel path");
72 /* Create directory. Ignore if exist. */
73 ret
= run_as_mkdir_recursive(pathname
, S_IRWXU
| S_IRWXG
,
74 ua_sess
->euid
, ua_sess
->egid
);
77 ERR("Trace directory creation error");
82 ret
= snprintf(pathname
, PATH_MAX
, "%s%s", consumer
->subdir
,
85 PERROR("snprintf channel path");
98 * Send a single channel to the consumer using command ADD_CHANNEL.
100 * Consumer socket lock MUST be acquired before calling this.
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
)
108 uint64_t key
, chan_reg_key
;
109 char *pathname
= NULL
;
110 struct lttcomm_consumer_msg msg
;
111 struct ust_registry_channel
*chan_reg
;
119 DBG2("Asking UST consumer for channel");
121 /* Get and create full trace path of session. */
122 if (ua_sess
->output_traces
) {
123 pathname
= setup_trace_path(consumer
, ua_sess
);
130 /* Depending on the buffer type, a different channel key is used. */
131 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
132 chan_reg_key
= ua_chan
->tracing_channel_id
;
134 chan_reg_key
= ua_chan
->key
;
137 if (ua_chan
->attr
.type
== LTTNG_UST_CHAN_METADATA
) {
140 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
142 chan_id
= chan_reg
->chan_id
;
145 switch (ua_chan
->attr
.output
) {
148 output
= LTTNG_EVENT_MMAP
;
152 consumer_init_ask_channel_comm_msg(&msg
,
153 ua_chan
->attr
.subbuf_size
,
154 ua_chan
->attr
.num_subbuf
,
155 ua_chan
->attr
.overwrite
,
156 ua_chan
->attr
.switch_timer_interval
,
157 ua_chan
->attr
.read_timer_interval
,
158 ua_sess
->live_timer_interval
,
160 (int) ua_chan
->attr
.type
,
166 consumer
->net_seq_index
,
170 ua_chan
->tracefile_size
,
171 ua_chan
->tracefile_count
,
173 ua_sess
->output_traces
,
176 health_code_update();
178 ret
= consumer_socket_send(socket
, &msg
, sizeof(msg
));
183 ret
= consumer_recv_status_channel(socket
, &key
,
184 &ua_chan
->expected_stream_count
);
188 /* Communication protocol error. */
189 assert(key
== ua_chan
->key
);
190 /* We need at least one where 1 stream for 1 cpu. */
191 if (ua_sess
->output_traces
) {
192 assert(ua_chan
->expected_stream_count
> 0);
195 DBG2("UST ask channel %" PRIu64
" successfully done with %u stream(s)", key
,
196 ua_chan
->expected_stream_count
);
200 health_code_update();
205 * Ask consumer to create a channel for a given session.
207 * Returns 0 on success else a negative value.
209 int ust_consumer_ask_channel(struct ust_app_session
*ua_sess
,
210 struct ust_app_channel
*ua_chan
, struct consumer_output
*consumer
,
211 struct consumer_socket
*socket
, struct ust_registry_session
*registry
)
221 if (!consumer
->enabled
) {
222 ret
= -LTTNG_ERR_NO_CONSUMER
;
223 DBG3("Consumer is disabled");
227 pthread_mutex_lock(socket
->lock
);
229 ret
= ask_channel_creation(ua_sess
, ua_chan
, consumer
, socket
, registry
);
235 pthread_mutex_unlock(socket
->lock
);
240 * Send a get channel command to consumer using the given channel key. The
241 * channel object is populated and the stream list.
243 * Return 0 on success else a negative value.
245 int ust_consumer_get_channel(struct consumer_socket
*socket
,
246 struct ust_app_channel
*ua_chan
)
249 struct lttcomm_consumer_msg msg
;
254 memset(&msg
, 0, sizeof(msg
));
255 msg
.cmd_type
= LTTNG_CONSUMER_GET_CHANNEL
;
256 msg
.u
.get_channel
.key
= ua_chan
->key
;
258 pthread_mutex_lock(socket
->lock
);
259 health_code_update();
261 /* Send command and wait for OK reply. */
262 ret
= consumer_send_msg(socket
, &msg
);
267 /* First, get the channel from consumer. */
268 ret
= ustctl_recv_channel_from_consumer(*socket
->fd_ptr
, &ua_chan
->obj
);
271 ERR("Error recv channel from consumer %d with ret %d",
272 *socket
->fd_ptr
, ret
);
274 DBG3("UST app recv channel from consumer. Consumer is dead.");
279 /* Next, get all streams. */
281 struct ust_app_stream
*stream
;
283 /* Create UST stream */
284 stream
= ust_app_alloc_stream();
285 if (stream
== NULL
) {
290 /* Stream object is populated by this call if successful. */
291 ret
= ustctl_recv_stream_from_consumer(*socket
->fd_ptr
, &stream
->obj
);
294 if (ret
== -LTTNG_UST_ERR_NOENT
) {
295 DBG3("UST app consumer has no more stream available");
300 ERR("Recv stream from consumer %d with ret %d",
301 *socket
->fd_ptr
, ret
);
303 DBG3("UST app recv stream from consumer. Consumer is dead.");
308 /* Order is important this is why a list is used. */
309 cds_list_add_tail(&stream
->list
, &ua_chan
->streams
.head
);
310 ua_chan
->streams
.count
++;
312 DBG2("UST app stream %d received successfully", ua_chan
->streams
.count
);
315 /* This MUST match or else we have a synchronization problem. */
316 assert(ua_chan
->expected_stream_count
== ua_chan
->streams
.count
);
318 /* Wait for confirmation that we can proceed with the streams. */
319 ret
= consumer_recv_status_reply(socket
);
325 health_code_update();
326 pthread_mutex_unlock(socket
->lock
);
331 * Send a destroy channel command to consumer using the given channel key.
333 * Note that this command MUST be used prior to a successful
334 * LTTNG_CONSUMER_GET_CHANNEL because once this command is done successfully,
335 * the streams are dispatched to the consumer threads and MUST be teardown
336 * through the hang up process.
338 * Return 0 on success else a negative value.
340 int ust_consumer_destroy_channel(struct consumer_socket
*socket
,
341 struct ust_app_channel
*ua_chan
)
344 struct lttcomm_consumer_msg msg
;
349 memset(&msg
, 0, sizeof(msg
));
350 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
351 msg
.u
.destroy_channel
.key
= ua_chan
->key
;
353 pthread_mutex_lock(socket
->lock
);
354 health_code_update();
356 ret
= consumer_send_msg(socket
, &msg
);
362 health_code_update();
363 pthread_mutex_unlock(socket
->lock
);
368 * Send a given stream to UST tracer.
370 * On success return 0 else a negative value.
372 int ust_consumer_send_stream_to_ust(struct ust_app
*app
,
373 struct ust_app_channel
*channel
, struct ust_app_stream
*stream
)
381 DBG2("UST consumer send stream to app %d", app
->sock
);
383 /* Relay stream to application. */
384 ret
= ustctl_send_stream_to_ust(app
->sock
, channel
->obj
, stream
->obj
);
386 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
387 ERR("ustctl send stream handle %d to app pid: %d with ret %d",
388 stream
->obj
->handle
, app
->pid
, ret
);
390 DBG3("UST app send stream to ust failed. Application is dead.");
394 channel
->handle
= channel
->obj
->handle
;
401 * Send channel previously received from the consumer to the UST tracer.
403 * On success return 0 else a negative value.
405 int ust_consumer_send_channel_to_ust(struct ust_app
*app
,
406 struct ust_app_session
*ua_sess
, struct ust_app_channel
*channel
)
413 assert(channel
->obj
);
415 DBG2("UST app send channel to sock %d pid %d (name: %s, key: %" PRIu64
")",
416 app
->sock
, app
->pid
, channel
->name
, channel
->tracing_channel_id
);
418 /* Send stream to application. */
419 ret
= ustctl_send_channel_to_ust(app
->sock
, ua_sess
->handle
, channel
->obj
);
421 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
422 ERR("Error ustctl send channel %s to app pid: %d with ret %d",
423 channel
->name
, app
->pid
, ret
);
425 DBG3("UST app send channel to ust failed. Application is dead.");
435 * Handle the metadata requests from the UST consumer
437 * Return 0 on success else a negative value.
439 int ust_consumer_metadata_request(struct consumer_socket
*socket
)
443 struct lttcomm_metadata_request_msg request
;
444 struct buffer_reg_uid
*reg_uid
;
445 struct ust_registry_session
*ust_reg
;
446 struct lttcomm_consumer_msg msg
;
451 pthread_mutex_lock(socket
->lock
);
453 health_code_update();
455 /* Wait for a metadata request */
456 ret
= consumer_socket_recv(socket
, &request
, sizeof(request
));
461 DBG("Metadata request received for session %" PRIu64
", key %" PRIu64
,
462 request
.session_id
, request
.key
);
464 reg_uid
= buffer_reg_uid_find(request
.session_id
,
465 request
.bits_per_long
, request
.uid
);
467 ust_reg
= reg_uid
->registry
->reg
.ust
;
469 struct buffer_reg_pid
*reg_pid
=
470 buffer_reg_pid_find(request
.session_id_per_pid
);
472 DBG("PID registry not found for session id %" PRIu64
,
473 request
.session_id_per_pid
);
475 memset(&msg
, 0, sizeof(msg
));
476 msg
.cmd_type
= LTTNG_ERR_UND
;
477 (void) consumer_send_msg(socket
, &msg
);
479 * This is possible since the session might have been destroyed
480 * during a consumer metadata request. So here, return gracefully
481 * because the destroy session will push the remaining metadata to
487 ust_reg
= reg_pid
->registry
->reg
.ust
;
491 ret_push
= ust_app_push_metadata(ust_reg
, socket
, 1);
493 ERR("Pushing metadata");
497 DBG("UST Consumer metadata pushed successfully");
501 pthread_mutex_unlock(socket
->lock
);