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.
25 #include <common/common.h>
26 #include <common/consumer.h>
27 #include <common/defaults.h>
30 #include "ust-consumer.h"
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.
37 * The caller can safely free(3) the returned value. On error, NULL is
40 static char *setup_trace_path(struct consumer_output
*consumer
,
41 struct ust_app_session
*ua_sess
)
51 /* Allocate our self the string to make sure we never exceed PATH_MAX. */
52 pathname
= zmalloc(PATH_MAX
);
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
);
63 PERROR("snprintf channel path");
67 /* Create directory. Ignore if exist. */
68 ret
= run_as_mkdir_recursive(pathname
, S_IRWXU
| S_IRWXG
, ua_sess
->uid
,
72 ERR("Trace directory creation error");
77 ret
= snprintf(pathname
, PATH_MAX
, "%s/%s", consumer
->subdir
,
80 PERROR("snprintf channel path");
93 * Send a single channel to the consumer using command ADD_CHANNEL.
95 * Consumer socket MUST be acquired before calling this.
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
)
103 char *pathname
= NULL
;
104 struct lttcomm_consumer_msg msg
;
111 DBG2("Asking UST consumer for channel");
113 /* Get and create full trace path of session. */
114 pathname
= setup_trace_path(consumer
, ua_sess
);
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
,
133 consumer
->net_seq_index
,
135 ua_sess
->registry
.uuid
);
137 health_code_update();
139 ret
= lttcomm_send_unix_sock(socket
->fd
, &msg
, sizeof(msg
));
144 ret
= consumer_recv_status_channel(socket
, &key
,
145 &ua_chan
->expected_stream_count
);
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);
154 DBG2("UST ask channel %lu successfully done with %u stream(s)", key
,
155 ua_chan
->expected_stream_count
);
159 health_code_update();
164 * Ask consumer to create a channel for a given session.
166 * Returns 0 on success else a negative value.
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
)
178 assert(socket
->fd
>= 0);
180 pthread_mutex_lock(socket
->lock
);
182 ret
= ask_channel_creation(ua_sess
, ua_chan
, consumer
, socket
);
188 pthread_mutex_unlock(socket
->lock
);
193 * Send a get channel command to consumer using the given channel key. The
194 * channel object is populated and the stream list.
196 * Return 0 on success else a negative value.
198 int ust_consumer_get_channel(struct consumer_socket
*socket
,
199 struct ust_app_channel
*ua_chan
)
202 struct lttcomm_consumer_msg msg
;
206 assert(socket
->fd
>= 0);
208 msg
.cmd_type
= LTTNG_CONSUMER_GET_CHANNEL
;
209 msg
.u
.get_channel
.key
= ua_chan
->key
;
211 pthread_mutex_lock(socket
->lock
);
212 health_code_update();
214 /* Send command and wait for OK reply. */
215 ret
= consumer_send_msg(socket
, &msg
);
220 /* First, get the channel from consumer. */
221 ret
= ustctl_recv_channel_from_consumer(socket
->fd
, &ua_chan
->obj
);
224 ERR("Error recv channel from consumer %d with ret %d",
227 DBG3("UST app recv channel from consumer. Consumer is dead.");
232 /* Next, get all streams. */
234 struct ust_app_stream
*stream
;
236 /* Create UST stream */
237 stream
= ust_app_alloc_stream();
238 if (stream
== NULL
) {
243 /* Stream object is populated by this call if successful. */
244 ret
= ustctl_recv_stream_from_consumer(socket
->fd
, &stream
->obj
);
247 if (ret
== -LTTNG_UST_ERR_NOENT
) {
248 DBG3("UST app consumer has no more stream available");
253 ERR("Recv stream from consumer %d with ret %d",
256 DBG3("UST app recv stream from consumer. Consumer is dead.");
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
++;
265 DBG2("UST app stream %d received succesfully", ua_chan
->streams
.count
);
268 /* This MUST match or else we have a synchronization problem. */
269 assert(ua_chan
->expected_stream_count
== ua_chan
->streams
.count
);
271 /* Wait for confirmation that we can proceed with the streams. */
272 ret
= consumer_recv_status_reply(socket
);
278 health_code_update();
279 pthread_mutex_unlock(socket
->lock
);
284 * Send a destroy channel command to consumer using the given channel key.
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.
291 * Return 0 on success else a negative value.
293 int ust_consumer_destroy_channel(struct consumer_socket
*socket
,
294 struct ust_app_channel
*ua_chan
)
297 struct lttcomm_consumer_msg msg
;
301 assert(socket
->fd
>= 0);
303 msg
.cmd_type
= LTTNG_CONSUMER_DESTROY_CHANNEL
;
304 msg
.u
.destroy_channel
.key
= ua_chan
->key
;
306 pthread_mutex_lock(socket
->lock
);
307 health_code_update();
309 ret
= consumer_send_msg(socket
, &msg
);
315 health_code_update();
316 pthread_mutex_unlock(socket
->lock
);
321 * Send a given stream to UST tracer.
323 * On success return 0 else a negative value.
325 int ust_consumer_send_stream_to_ust(struct ust_app
*app
,
326 struct ust_app_channel
*channel
, struct ust_app_stream
*stream
)
334 DBG2("UST consumer send stream to app %d", app
->sock
);
336 /* Relay stream to application. */
337 ret
= ustctl_send_stream_to_ust(app
->sock
, channel
->obj
, stream
->obj
);
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
);
343 DBG3("UST app send stream to ust failed. Application is dead.");
347 channel
->handle
= channel
->obj
->handle
;
354 * Send channel previously received from the consumer to the UST tracer.
356 * On success return 0 else a negative value.
358 int ust_consumer_send_channel_to_ust(struct ust_app
*app
,
359 struct ust_app_session
*ua_sess
, struct ust_app_channel
*channel
)
366 assert(channel
->obj
);
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
);
371 /* Send stream to application. */
372 ret
= ustctl_send_channel_to_ust(app
->sock
, ua_sess
->handle
, channel
->obj
);
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
);
378 DBG3("UST app send channel to ust failed. Application is dead.");