Commit | Line | Data |
---|---|---|
48842b30 DG |
1 | /* |
2 | * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca> | |
3 | * | |
d14d33bf AM |
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. | |
48842b30 | 7 | * |
d14d33bf AM |
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. | |
48842b30 | 12 | * |
d14d33bf AM |
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. | |
48842b30 DG |
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 | ||
990570ed | 25 | #include <common/common.h> |
db758600 | 26 | #include <common/consumer.h> |
990570ed | 27 | #include <common/defaults.h> |
48842b30 | 28 | |
00e2e675 | 29 | #include "consumer.h" |
48842b30 DG |
30 | #include "ust-consumer.h" |
31 | ||
32 | /* | |
ffe60014 DG |
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. | |
48842b30 | 39 | */ |
ffe60014 DG |
40 | static char *setup_trace_path(struct consumer_output *consumer, |
41 | struct ust_app_session *ua_sess) | |
48842b30 | 42 | { |
ffe60014 DG |
43 | int ret; |
44 | char *pathname; | |
37278a1e | 45 | |
ffe60014 DG |
46 | assert(consumer); |
47 | assert(ua_sess); | |
00e2e675 | 48 | |
840cb59c | 49 | health_code_update(); |
ca03de58 | 50 | |
ffe60014 DG |
51 | /* Allocate our self the string to make sure we never exceed PATH_MAX. */ |
52 | pathname = zmalloc(PATH_MAX); | |
53 | if (!pathname) { | |
48842b30 DG |
54 | goto error; |
55 | } | |
00e2e675 | 56 | |
ffe60014 DG |
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 | } | |
ca03de58 | 66 | |
ffe60014 DG |
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 | } | |
48842b30 DG |
83 | } |
84 | ||
ffe60014 | 85 | return pathname; |
ca03de58 | 86 | |
37278a1e | 87 | error: |
ffe60014 DG |
88 | free(pathname); |
89 | return NULL; | |
37278a1e DG |
90 | } |
91 | ||
92 | /* | |
ffe60014 DG |
93 | * Send a single channel to the consumer using command ADD_CHANNEL. |
94 | * | |
95 | * Consumer socket MUST be acquired before calling this. | |
37278a1e | 96 | */ |
ffe60014 DG |
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) | |
37278a1e | 100 | { |
ffe60014 DG |
101 | int ret; |
102 | unsigned long key; | |
103 | char *pathname = NULL; | |
37278a1e DG |
104 | struct lttcomm_consumer_msg msg; |
105 | ||
ffe60014 DG |
106 | assert(ua_sess); |
107 | assert(ua_chan); | |
108 | assert(socket); | |
37278a1e | 109 | assert(consumer); |
ffe60014 DG |
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, | |
ca22feea | 129 | pathname, |
ffe60014 DG |
130 | ua_chan->name, |
131 | ua_sess->uid, | |
132 | ua_sess->gid, | |
133 | consumer->net_seq_index, | |
134 | ua_chan->key, | |
135 | ua_sess->uuid); | |
37278a1e | 136 | |
840cb59c | 137 | health_code_update(); |
ca03de58 | 138 | |
ffe60014 | 139 | ret = lttcomm_send_unix_sock(socket->fd, &msg, sizeof(msg)); |
37278a1e DG |
140 | if (ret < 0) { |
141 | goto error; | |
142 | } | |
143 | ||
ffe60014 DG |
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); | |
ca03de58 | 156 | |
37278a1e | 157 | error: |
ffe60014 DG |
158 | free(pathname); |
159 | health_code_update(); | |
37278a1e DG |
160 | return ret; |
161 | } | |
162 | ||
163 | /* | |
ffe60014 DG |
164 | * Ask consumer to create a channel for a given session. |
165 | * | |
166 | * Returns 0 on success else a negative value. | |
37278a1e | 167 | */ |
ffe60014 DG |
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) | |
37278a1e DG |
171 | { |
172 | int ret; | |
37278a1e | 173 | |
ffe60014 DG |
174 | assert(ua_sess); |
175 | assert(ua_chan); | |
176 | assert(consumer); | |
177 | assert(socket); | |
178 | assert(socket->fd >= 0); | |
f50f23d9 | 179 | |
ffe60014 | 180 | pthread_mutex_lock(socket->lock); |
37278a1e | 181 | |
ffe60014 | 182 | ret = ask_channel_creation(ua_sess, ua_chan, consumer, socket); |
37278a1e DG |
183 | if (ret < 0) { |
184 | goto error; | |
185 | } | |
186 | ||
48842b30 | 187 | error: |
ffe60014 | 188 | pthread_mutex_unlock(socket->lock); |
48842b30 DG |
189 | return ret; |
190 | } | |
191 | ||
192 | /* | |
ffe60014 DG |
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. | |
48842b30 | 197 | */ |
ffe60014 DG |
198 | int ust_consumer_get_channel(struct consumer_socket *socket, |
199 | struct ust_app_channel *ua_chan) | |
48842b30 | 200 | { |
ffe60014 | 201 | int ret; |
37278a1e | 202 | struct lttcomm_consumer_msg msg; |
48842b30 | 203 | |
ffe60014 DG |
204 | assert(ua_chan); |
205 | assert(socket); | |
206 | assert(socket->fd >= 0); | |
48842b30 | 207 | |
ffe60014 DG |
208 | msg.cmd_type = LTTNG_CONSUMER_GET_CHANNEL; |
209 | msg.u.get_channel.key = ua_chan->key; | |
37278a1e | 210 | |
ffe60014 | 211 | pthread_mutex_lock(socket->lock); |
840cb59c | 212 | health_code_update(); |
ca03de58 | 213 | |
ffe60014 DG |
214 | /* Send command and wait for OK reply. */ |
215 | ret = consumer_send_msg(socket, &msg); | |
37278a1e DG |
216 | if (ret < 0) { |
217 | goto error; | |
218 | } | |
219 | ||
ffe60014 DG |
220 | /* First, get the channel from consumer. */ |
221 | ret = ustctl_recv_channel_from_consumer(socket->fd, &ua_chan->obj); | |
37278a1e | 222 | if (ret < 0) { |
ffe60014 DG |
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 | } | |
37278a1e DG |
229 | goto error; |
230 | } | |
ffe60014 | 231 | ua_chan->handle = ua_chan->obj->handle; |
00e2e675 | 232 | |
ffe60014 DG |
233 | /* Next, get all streams. */ |
234 | while (1) { | |
235 | struct ust_app_stream *stream; | |
ca03de58 | 236 | |
ffe60014 DG |
237 | /* Create UST stream */ |
238 | stream = ust_app_alloc_stream(); | |
239 | if (stream == NULL) { | |
240 | ret = -ENOMEM; | |
48842b30 DG |
241 | goto error; |
242 | } | |
243 | ||
ffe60014 DG |
244 | /* Stream object is populated by this call if successful. */ |
245 | ret = ustctl_recv_stream_from_consumer(socket->fd, &stream->obj); | |
37278a1e | 246 | if (ret < 0) { |
ffe60014 DG |
247 | free(stream); |
248 | if (ret == -LTTNG_UST_ERR_NOENT) { | |
249 | DBG3("UST app consumer has no more stream available"); | |
250 | ret = 0; | |
251 | break; | |
252 | } | |
253 | if (ret != -EPIPE) { | |
254 | ERR("Recv stream from consumer %d with ret %d", | |
255 | socket->fd, ret); | |
256 | } else { | |
257 | DBG3("UST app recv stream from consumer. Consumer is dead."); | |
00e2e675 | 258 | } |
48842b30 DG |
259 | goto error; |
260 | } | |
37278a1e | 261 | |
ffe60014 DG |
262 | /* Order is important this is why a list is used. */ |
263 | cds_list_add_tail(&stream->list, &ua_chan->streams.head); | |
264 | ua_chan->streams.count++; | |
37278a1e | 265 | |
ffe60014 DG |
266 | DBG2("UST app stream %d received succesfully", ua_chan->streams.count); |
267 | } | |
268 | ||
269 | /* This MUST match or else we have a synchronization problem. */ | |
270 | assert(ua_chan->expected_stream_count == ua_chan->streams.count); | |
ca03de58 | 271 | |
ffe60014 DG |
272 | /* Wait for confirmation that we can proceed with the streams. */ |
273 | ret = consumer_recv_status_reply(socket); | |
37278a1e DG |
274 | if (ret < 0) { |
275 | goto error; | |
276 | } | |
277 | ||
278 | error: | |
ffe60014 DG |
279 | health_code_update(); |
280 | pthread_mutex_unlock(socket->lock); | |
37278a1e DG |
281 | return ret; |
282 | } | |
283 | ||
284 | /* | |
ffe60014 DG |
285 | * Send a destroy channel command to consumer using the given channel key. |
286 | * | |
287 | * Note that this command MUST be used prior to a successful | |
288 | * LTTNG_CONSUMER_GET_CHANNEL because once this command is done successfully, | |
289 | * the streams are dispatched to the consumer threads and MUST be teardown | |
290 | * through the hang up process. | |
291 | * | |
292 | * Return 0 on success else a negative value. | |
37278a1e | 293 | */ |
ffe60014 DG |
294 | int ust_consumer_destroy_channel(struct consumer_socket *socket, |
295 | struct ust_app_channel *ua_chan) | |
37278a1e | 296 | { |
ffe60014 DG |
297 | int ret; |
298 | struct lttcomm_consumer_msg msg; | |
a4b92340 | 299 | |
ffe60014 DG |
300 | assert(ua_chan); |
301 | assert(socket); | |
302 | assert(socket->fd >= 0); | |
37278a1e | 303 | |
ffe60014 DG |
304 | msg.cmd_type = LTTNG_CONSUMER_DESTROY_CHANNEL; |
305 | msg.u.destroy_channel.key = ua_chan->key; | |
173af62f | 306 | |
ffe60014 DG |
307 | pthread_mutex_lock(socket->lock); |
308 | health_code_update(); | |
37278a1e | 309 | |
ffe60014 | 310 | ret = consumer_send_msg(socket, &msg); |
37278a1e DG |
311 | if (ret < 0) { |
312 | goto error; | |
48842b30 DG |
313 | } |
314 | ||
ffe60014 DG |
315 | error: |
316 | health_code_update(); | |
317 | pthread_mutex_unlock(socket->lock); | |
318 | return ret; | |
319 | } | |
aeb96892 | 320 | |
ffe60014 DG |
321 | /* |
322 | * Send a given stream to UST tracer. | |
323 | * | |
324 | * On success return 0 else a negative value. | |
325 | */ | |
326 | int ust_consumer_send_stream_to_ust(struct ust_app *app, | |
327 | struct ust_app_channel *channel, struct ust_app_stream *stream) | |
328 | { | |
329 | int ret; | |
330 | ||
331 | assert(app); | |
332 | assert(stream); | |
333 | assert(channel); | |
334 | ||
335 | DBG2("UST consumer send stream to app %d", app->sock); | |
336 | ||
337 | /* Relay stream to application. */ | |
338 | ret = ustctl_send_stream_to_ust(app->sock, channel->obj, stream->obj); | |
339 | if (ret < 0) { | |
340 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
341 | ERR("Error ustctl send stream %s to app pid: %d with ret %d", | |
342 | stream->name, app->pid, ret); | |
343 | } else { | |
344 | DBG3("UST app send stream to ust failed. Application is dead."); | |
48842b30 | 345 | } |
ffe60014 | 346 | goto error; |
48842b30 | 347 | } |
48842b30 | 348 | |
ffe60014 DG |
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); | |
48842b30 | 370 | |
ffe60014 DG |
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 | } | |
48842b30 DG |
382 | |
383 | error: | |
384 | return ret; | |
385 | } |