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.
26 #include <common/common.h>
27 #include <common/consumer.h>
28 #include <common/defaults.h>
32 #include "ust-consumer.h"
35 * Return allocated full pathname of the session using the consumer trace path
36 * and subdir if available. On a successful allocation, the directory of the
37 * trace is created with the session credentials.
39 * The caller can safely free(3) the returned value. On error, NULL is
42 static char *setup_trace_path(struct consumer_output
*consumer
,
43 struct ust_app_session
*ua_sess
)
53 /* Allocate our self the string to make sure we never exceed PATH_MAX. */
54 pathname
= zmalloc(PATH_MAX
);
59 /* Get correct path name destination */
60 if (consumer
->type
== CONSUMER_DST_LOCAL
) {
61 /* Set application path to the destination path */
62 ret
= snprintf(pathname
, PATH_MAX
, "%s/%s/%s",
63 consumer
->dst
.trace_path
, consumer
->subdir
, ua_sess
->path
);
65 PERROR("snprintf channel path");
69 /* Create directory. Ignore if exist. */
70 ret
= run_as_mkdir_recursive(pathname
, S_IRWXU
| S_IRWXG
,
71 ua_sess
->euid
, ua_sess
->egid
);
74 ERR("Trace directory creation error");
79 ret
= snprintf(pathname
, PATH_MAX
, "%s/%s", consumer
->subdir
,
82 PERROR("snprintf channel path");
95 * Send a single channel to the consumer using command ADD_CHANNEL.
97 * Consumer socket lock MUST be acquired before calling this.
99 static int ask_channel_creation(struct ust_app_session
*ua_sess
,
100 struct ust_app_channel
*ua_chan
, struct consumer_output
*consumer
,
101 struct consumer_socket
*socket
, struct ust_registry_session
*registry
)
105 uint64_t key
, chan_reg_key
;
106 char *pathname
= NULL
;
107 struct lttcomm_consumer_msg msg
;
108 struct ust_registry_channel
*chan_reg
;
116 DBG2("Asking UST consumer for channel");
118 /* Get and create full trace path of session. */
119 pathname
= setup_trace_path(consumer
, ua_sess
);
125 /* Depending on the buffer type, a different channel key is used. */
126 if (ua_sess
->buffer_type
== LTTNG_BUFFER_PER_UID
) {
127 chan_reg_key
= ua_chan
->tracing_channel_id
;
129 chan_reg_key
= ua_chan
->key
;
132 if (ua_chan
->attr
.type
== LTTNG_UST_CHAN_METADATA
) {
135 chan_reg
= ust_registry_channel_find(registry
, chan_reg_key
);
137 chan_id
= chan_reg
->chan_id
;
140 consumer_init_ask_channel_comm_msg(&msg
,
141 ua_chan
->attr
.subbuf_size
,
142 ua_chan
->attr
.num_subbuf
,
143 ua_chan
->attr
.overwrite
,
144 ua_chan
->attr
.switch_timer_interval
,
145 ua_chan
->attr
.read_timer_interval
,
146 (int) ua_chan
->attr
.output
,
147 (int) ua_chan
->attr
.type
,
153 consumer
->net_seq_index
,
158 health_code_update();
160 ret
= lttcomm_send_unix_sock(socket
->fd
, &msg
, sizeof(msg
));
165 ret
= consumer_recv_status_channel(socket
, &key
,
166 &ua_chan
->expected_stream_count
);
170 /* Communication protocol error. */
171 assert(key
== ua_chan
->key
);
172 /* We need at least one where 1 stream for 1 cpu. */
173 assert(ua_chan
->expected_stream_count
> 0);
175 DBG2("UST ask channel %" PRIu64
" successfully done with %u stream(s)", key
,
176 ua_chan
->expected_stream_count
);
180 health_code_update();
185 * Ask consumer to create a channel for a given session.
187 * Returns 0 on success else a negative value.
189 int ust_consumer_ask_channel(struct ust_app_session
*ua_sess
,
190 struct ust_app_channel
*ua_chan
, struct consumer_output
*consumer
,
191 struct consumer_socket
*socket
, struct ust_registry_session
*registry
)
199 assert(socket
->fd
>= 0);
202 pthread_mutex_lock(socket
->lock
);
204 ret
= ask_channel_creation(ua_sess
, ua_chan
, consumer
, socket
, registry
);
210 pthread_mutex_unlock(socket
->lock
);
215 * Send a get channel command to consumer using the given channel key. The
216 * channel object is populated and the stream list.
218 * Return 0 on success else a negative value.
220 int ust_consumer_get_channel(struct consumer_socket
*socket
,
221 struct ust_app_channel
*ua_chan
)
224 struct lttcomm_consumer_msg msg
;
228 assert(socket
->fd
>= 0);
230 msg
.cmd_type
= LTTNG_CONSUMER_GET_CHANNEL
;
231 msg
.u
.get_channel
.key
= ua_chan
->key
;
233 pthread_mutex_lock(socket
->lock
);
234 health_code_update();
236 /* Send command and wait for OK reply. */
237 ret
= consumer_send_msg(socket
, &msg
);
242 /* First, get the channel from consumer. */
243 ret
= ustctl_recv_channel_from_consumer(socket
->fd
, &ua_chan
->obj
);
246 ERR("Error recv channel from consumer %d with ret %d",
249 DBG3("UST app recv channel from consumer. Consumer is dead.");
254 /* Next, get all streams. */
256 struct ust_app_stream
*stream
;
258 /* Create UST stream */
259 stream
= ust_app_alloc_stream();
260 if (stream
== NULL
) {
265 /* Stream object is populated by this call if successful. */
266 ret
= ustctl_recv_stream_from_consumer(socket
->fd
, &stream
->obj
);
269 if (ret
== -LTTNG_UST_ERR_NOENT
) {
270 DBG3("UST app consumer has no more stream available");
275 ERR("Recv stream from consumer %d with ret %d",
278 DBG3("UST app recv stream from consumer. Consumer is dead.");
283 /* Order is important this is why a list is used. */
284 cds_list_add_tail(&stream
->list
, &ua_chan
->streams
.head
);
285 ua_chan
->streams
.count
++;
287 DBG2("UST app stream %d received succesfully", ua_chan
->streams
.count
);
290 /* This MUST match or else we have a synchronization problem. */
291 assert(ua_chan
->expected_stream_count
== ua_chan
->streams
.count
);
293 /* Wait for confirmation that we can proceed with the streams. */
294 ret
= consumer_recv_status_reply(socket
);
300 health_code_update();
301 pthread_mutex_unlock(socket
->lock
);
306 * Send a destroy channel command to consumer using the given channel key.
308 * Note that this command MUST be used prior to a successful
309 * LTTNG_CONSUMER_GET_CHANNEL because once this command is done successfully,
310 * the streams are dispatched to the consumer threads and MUST be teardown
311 * through the hang up process.
313 * Return 0 on success else a negative value.
315 int ust_consumer_destroy_channel(struct consumer_socket
*socket
,
316 struct ust_app_channel
*ua_chan
)
319 struct lttcomm_consumer_msg msg
;
323 assert(socket
->fd
>= 0);
325 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
326 msg
.u
.destroy_channel
.key
= ua_chan
->key
;
328 pthread_mutex_lock(socket
->lock
);
329 health_code_update();
331 ret
= consumer_send_msg(socket
, &msg
);
337 health_code_update();
338 pthread_mutex_unlock(socket
->lock
);
343 * Send a given stream to UST tracer.
345 * On success return 0 else a negative value.
347 int ust_consumer_send_stream_to_ust(struct ust_app
*app
,
348 struct ust_app_channel
*channel
, struct ust_app_stream
*stream
)
356 DBG2("UST consumer send stream to app %d", app
->sock
);
358 /* Relay stream to application. */
359 ret
= ustctl_send_stream_to_ust(app
->sock
, channel
->obj
, stream
->obj
);
361 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
362 ERR("Error ustctl send stream %s to app pid: %d with ret %d",
363 stream
->name
, app
->pid
, ret
);
365 DBG3("UST app send stream to ust failed. Application is dead.");
369 channel
->handle
= channel
->obj
->handle
;
376 * Send channel previously received from the consumer to the UST tracer.
378 * On success return 0 else a negative value.
380 int ust_consumer_send_channel_to_ust(struct ust_app
*app
,
381 struct ust_app_session
*ua_sess
, struct ust_app_channel
*channel
)
388 assert(channel
->obj
);
390 DBG2("UST app send channel to sock %d pid %d (name: %s, key: %" PRIu64
")",
391 app
->sock
, app
->pid
, channel
->name
, channel
->tracing_channel_id
);
393 /* Send stream to application. */
394 ret
= ustctl_send_channel_to_ust(app
->sock
, ua_sess
->handle
, channel
->obj
);
396 if (ret
!= -EPIPE
&& ret
!= -LTTNG_UST_ERR_EXITING
) {
397 ERR("Error ustctl send channel %s to app pid: %d with ret %d",
398 channel
->name
, app
->pid
, ret
);
400 DBG3("UST app send channel to ust failed. Application is dead.");