Fix: ust app leak on UST buffer creation error
[lttng-tools.git] / src / bin / lttng-sessiond / ust-consumer.c
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 #define _LGPL_SOURCE
20 #include <errno.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <inttypes.h>
26
27 #include <common/common.h>
28 #include <common/consumer.h>
29 #include <common/defaults.h>
30
31 #include "consumer.h"
32 #include "health-sessiond.h"
33 #include "ust-consumer.h"
34 #include "buffer-registry.h"
35 #include "session.h"
36
37 /*
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.
41 *
42 * The caller can safely free(3) the returned value. On error, NULL is
43 * returned.
44 */
45 static char *setup_trace_path(struct consumer_output *consumer,
46 struct ust_app_session *ua_sess)
47 {
48 int ret;
49 char *pathname;
50
51 assert(consumer);
52 assert(ua_sess);
53
54 health_code_update();
55
56 /* Allocate our self the string to make sure we never exceed PATH_MAX. */
57 pathname = zmalloc(PATH_MAX);
58 if (!pathname) {
59 goto error;
60 }
61
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);
67 if (ret < 0) {
68 PERROR("snprintf channel path");
69 goto error;
70 }
71
72 /* Create directory. Ignore if exist. */
73 ret = run_as_mkdir_recursive(pathname, S_IRWXU | S_IRWXG,
74 ua_sess->euid, ua_sess->egid);
75 if (ret < 0) {
76 if (ret != -EEXIST) {
77 ERR("Trace directory creation error");
78 goto error;
79 }
80 }
81 } else {
82 ret = snprintf(pathname, PATH_MAX, "%s%s", consumer->subdir,
83 ua_sess->path);
84 if (ret < 0) {
85 PERROR("snprintf channel path");
86 goto error;
87 }
88 }
89
90 return pathname;
91
92 error:
93 free(pathname);
94 return NULL;
95 }
96
97 /*
98 * Send a single channel to the consumer using command ADD_CHANNEL.
99 *
100 * Consumer socket lock MUST be acquired before calling this.
101 */
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)
105 {
106 int ret, output;
107 uint32_t chan_id;
108 uint64_t key, chan_reg_key;
109 char *pathname = NULL;
110 struct lttcomm_consumer_msg msg;
111 struct ust_registry_channel *chan_reg;
112
113 assert(ua_sess);
114 assert(ua_chan);
115 assert(socket);
116 assert(consumer);
117 assert(registry);
118
119 DBG2("Asking UST consumer for channel");
120
121 /* Get and create full trace path of session. */
122 if (ua_sess->output_traces) {
123 pathname = setup_trace_path(consumer, ua_sess);
124 if (!pathname) {
125 ret = -1;
126 goto error;
127 }
128 }
129
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;
133 } else {
134 chan_reg_key = ua_chan->key;
135 }
136
137 if (ua_chan->attr.type == LTTNG_UST_CHAN_METADATA) {
138 chan_id = -1U;
139 } else {
140 chan_reg = ust_registry_channel_find(registry, chan_reg_key);
141 assert(chan_reg);
142 chan_id = chan_reg->chan_id;
143 }
144
145 switch (ua_chan->attr.output) {
146 case LTTNG_UST_MMAP:
147 default:
148 output = LTTNG_EVENT_MMAP;
149 break;
150 }
151
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,
159 output,
160 (int) ua_chan->attr.type,
161 ua_sess->tracing_id,
162 pathname,
163 ua_chan->name,
164 ua_sess->euid,
165 ua_sess->egid,
166 consumer->net_seq_index,
167 ua_chan->key,
168 registry->uuid,
169 chan_id,
170 ua_chan->tracefile_size,
171 ua_chan->tracefile_count,
172 ua_sess->id,
173 ua_sess->output_traces,
174 ua_sess->uid);
175
176 health_code_update();
177
178 ret = consumer_socket_send(socket, &msg, sizeof(msg));
179 if (ret < 0) {
180 goto error;
181 }
182
183 ret = consumer_recv_status_channel(socket, &key,
184 &ua_chan->expected_stream_count);
185 if (ret < 0) {
186 goto error;
187 }
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);
193 }
194
195 DBG2("UST ask channel %" PRIu64 " successfully done with %u stream(s)", key,
196 ua_chan->expected_stream_count);
197
198 error:
199 free(pathname);
200 health_code_update();
201 return ret;
202 }
203
204 /*
205 * Ask consumer to create a channel for a given session.
206 *
207 * Returns 0 on success else a negative value.
208 */
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)
212 {
213 int ret;
214
215 assert(ua_sess);
216 assert(ua_chan);
217 assert(consumer);
218 assert(socket);
219 assert(registry);
220
221 if (!consumer->enabled) {
222 ret = -LTTNG_ERR_NO_CONSUMER;
223 DBG3("Consumer is disabled");
224 goto error;
225 }
226
227 pthread_mutex_lock(socket->lock);
228
229 ret = ask_channel_creation(ua_sess, ua_chan, consumer, socket, registry);
230 if (ret < 0) {
231 goto error;
232 }
233
234 error:
235 pthread_mutex_unlock(socket->lock);
236 return ret;
237 }
238
239 /*
240 * Send a get channel command to consumer using the given channel key. The
241 * channel object is populated and the stream list.
242 *
243 * Return 0 on success else a negative value.
244 */
245 int ust_consumer_get_channel(struct consumer_socket *socket,
246 struct ust_app_channel *ua_chan)
247 {
248 int ret;
249 struct lttcomm_consumer_msg msg;
250
251 assert(ua_chan);
252 assert(socket);
253
254 memset(&msg, 0, sizeof(msg));
255 msg.cmd_type = LTTNG_CONSUMER_GET_CHANNEL;
256 msg.u.get_channel.key = ua_chan->key;
257
258 pthread_mutex_lock(socket->lock);
259 health_code_update();
260
261 /* Send command and wait for OK reply. */
262 ret = consumer_send_msg(socket, &msg);
263 if (ret < 0) {
264 goto error;
265 }
266
267 /* First, get the channel from consumer. */
268 ret = ustctl_recv_channel_from_consumer(*socket->fd_ptr, &ua_chan->obj);
269 if (ret < 0) {
270 if (ret != -EPIPE) {
271 ERR("Error recv channel from consumer %d with ret %d",
272 *socket->fd_ptr, ret);
273 } else {
274 DBG3("UST app recv channel from consumer. Consumer is dead.");
275 }
276 goto error;
277 }
278
279 /* Next, get all streams. */
280 while (1) {
281 struct ust_app_stream *stream;
282
283 /* Create UST stream */
284 stream = ust_app_alloc_stream();
285 if (stream == NULL) {
286 ret = -ENOMEM;
287 goto error;
288 }
289
290 /* Stream object is populated by this call if successful. */
291 ret = ustctl_recv_stream_from_consumer(*socket->fd_ptr, &stream->obj);
292 if (ret < 0) {
293 free(stream);
294 if (ret == -LTTNG_UST_ERR_NOENT) {
295 DBG3("UST app consumer has no more stream available");
296 ret = 0;
297 break;
298 }
299 if (ret != -EPIPE) {
300 ERR("Recv stream from consumer %d with ret %d",
301 *socket->fd_ptr, ret);
302 } else {
303 DBG3("UST app recv stream from consumer. Consumer is dead.");
304 }
305 goto error;
306 }
307
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++;
311
312 DBG2("UST app stream %d received successfully", ua_chan->streams.count);
313 }
314
315 /* This MUST match or else we have a synchronization problem. */
316 assert(ua_chan->expected_stream_count == ua_chan->streams.count);
317
318 /* Wait for confirmation that we can proceed with the streams. */
319 ret = consumer_recv_status_reply(socket);
320 if (ret < 0) {
321 goto error;
322 }
323
324 error:
325 health_code_update();
326 pthread_mutex_unlock(socket->lock);
327 return ret;
328 }
329
330 /*
331 * Send a destroy channel command to consumer using the given channel key.
332 *
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.
337 *
338 * Return 0 on success else a negative value.
339 */
340 int ust_consumer_destroy_channel(struct consumer_socket *socket,
341 struct ust_app_channel *ua_chan)
342 {
343 int ret;
344 struct lttcomm_consumer_msg msg;
345
346 assert(ua_chan);
347 assert(socket);
348
349 memset(&msg, 0, sizeof(msg));
350 msg.cmd_type = LTTNG_CONSUMER_DESTROY_CHANNEL;
351 msg.u.destroy_channel.key = ua_chan->key;
352
353 pthread_mutex_lock(socket->lock);
354 health_code_update();
355
356 ret = consumer_send_msg(socket, &msg);
357 if (ret < 0) {
358 goto error;
359 }
360
361 error:
362 health_code_update();
363 pthread_mutex_unlock(socket->lock);
364 return ret;
365 }
366
367 /*
368 * Send a given stream to UST tracer.
369 *
370 * On success return 0 else a negative value.
371 */
372 int ust_consumer_send_stream_to_ust(struct ust_app *app,
373 struct ust_app_channel *channel, struct ust_app_stream *stream)
374 {
375 int ret;
376
377 assert(app);
378 assert(stream);
379 assert(channel);
380
381 DBG2("UST consumer send stream to app %d", app->sock);
382
383 /* Relay stream to application. */
384 ret = ustctl_send_stream_to_ust(app->sock, channel->obj, stream->obj);
385 if (ret < 0) {
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);
389 } else {
390 DBG3("UST app send stream to ust failed. Application is dead.");
391 }
392 goto error;
393 }
394 channel->handle = channel->obj->handle;
395
396 error:
397 return ret;
398 }
399
400 /*
401 * Send channel previously received from the consumer to the UST tracer.
402 *
403 * On success return 0 else a negative value.
404 */
405 int ust_consumer_send_channel_to_ust(struct ust_app *app,
406 struct ust_app_session *ua_sess, struct ust_app_channel *channel)
407 {
408 int ret;
409
410 assert(app);
411 assert(ua_sess);
412 assert(channel);
413 assert(channel->obj);
414
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);
417
418 /* Send stream to application. */
419 ret = ustctl_send_channel_to_ust(app->sock, ua_sess->handle, channel->obj);
420 if (ret < 0) {
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);
424 } else {
425 DBG3("UST app send channel to ust failed. Application is dead.");
426 }
427 goto error;
428 }
429
430 error:
431 return ret;
432 }
433
434 /*
435 * Handle the metadata requests from the UST consumer
436 *
437 * Return 0 on success else a negative value.
438 */
439 int ust_consumer_metadata_request(struct consumer_socket *socket)
440 {
441 int ret;
442 ssize_t ret_push;
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;
447
448 assert(socket);
449
450 rcu_read_lock();
451 pthread_mutex_lock(socket->lock);
452
453 health_code_update();
454
455 /* Wait for a metadata request */
456 ret = consumer_socket_recv(socket, &request, sizeof(request));
457 if (ret < 0) {
458 goto end;
459 }
460
461 DBG("Metadata request received for session %" PRIu64 ", key %" PRIu64,
462 request.session_id, request.key);
463
464 reg_uid = buffer_reg_uid_find(request.session_id,
465 request.bits_per_long, request.uid);
466 if (reg_uid) {
467 ust_reg = reg_uid->registry->reg.ust;
468 } else {
469 struct buffer_reg_pid *reg_pid =
470 buffer_reg_pid_find(request.session_id_per_pid);
471 if (!reg_pid) {
472 DBG("PID registry not found for session id %" PRIu64,
473 request.session_id_per_pid);
474
475 memset(&msg, 0, sizeof(msg));
476 msg.cmd_type = LTTNG_ERR_UND;
477 (void) consumer_send_msg(socket, &msg);
478 /*
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
482 * the consumer.
483 */
484 ret = 0;
485 goto end;
486 }
487 ust_reg = reg_pid->registry->reg.ust;
488 }
489 assert(ust_reg);
490
491 ret_push = ust_app_push_metadata(ust_reg, socket, 1);
492 if (ret_push < 0) {
493 ERR("Pushing metadata");
494 ret = -1;
495 goto end;
496 }
497 DBG("UST Consumer metadata pushed successfully");
498 ret = 0;
499
500 end:
501 pthread_mutex_unlock(socket->lock);
502 rcu_read_unlock();
503 return ret;
504 }
This page took 0.039433 seconds and 4 git commands to generate.