Commit | Line | Data |
---|---|---|
91d76f53 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. | |
91d76f53 DG |
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 | * | |
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. | |
91d76f53 DG |
16 | */ |
17 | ||
18 | #define _GNU_SOURCE | |
6c1c0768 | 19 | #define _LGPL_SOURCE |
91d76f53 | 20 | #include <errno.h> |
7972aab2 | 21 | #include <inttypes.h> |
91d76f53 DG |
22 | #include <pthread.h> |
23 | #include <stdio.h> | |
24 | #include <stdlib.h> | |
099e26bd | 25 | #include <string.h> |
aba8e916 DG |
26 | #include <sys/stat.h> |
27 | #include <sys/types.h> | |
099e26bd | 28 | #include <unistd.h> |
0df502fd | 29 | #include <urcu/compiler.h> |
fb54cdbf | 30 | #include <lttng/ust-error.h> |
331744e3 | 31 | #include <signal.h> |
bec39940 | 32 | |
990570ed | 33 | #include <common/common.h> |
86acf0da | 34 | #include <common/sessiond-comm/sessiond-comm.h> |
1e307fab | 35 | |
7972aab2 | 36 | #include "buffer-registry.h" |
86acf0da | 37 | #include "fd-limit.h" |
8782cc74 | 38 | #include "health-sessiond.h" |
56fff090 | 39 | #include "ust-app.h" |
48842b30 | 40 | #include "ust-consumer.h" |
d80a6244 | 41 | #include "ust-ctl.h" |
0b2dc8df | 42 | #include "utils.h" |
d80a6244 | 43 | |
c4b88406 MD |
44 | static |
45 | int ust_app_flush_app_session(struct ust_app *app, struct ust_app_session *ua_sess); | |
46 | ||
d9bf3ca4 MD |
47 | /* Next available channel key. Access under next_channel_key_lock. */ |
48 | static uint64_t _next_channel_key; | |
49 | static pthread_mutex_t next_channel_key_lock = PTHREAD_MUTEX_INITIALIZER; | |
50 | ||
51 | /* Next available session ID. Access under next_session_id_lock. */ | |
52 | static uint64_t _next_session_id; | |
53 | static pthread_mutex_t next_session_id_lock = PTHREAD_MUTEX_INITIALIZER; | |
ffe60014 DG |
54 | |
55 | /* | |
d9bf3ca4 | 56 | * Return the incremented value of next_channel_key. |
ffe60014 | 57 | */ |
d9bf3ca4 | 58 | static uint64_t get_next_channel_key(void) |
ffe60014 | 59 | { |
d9bf3ca4 MD |
60 | uint64_t ret; |
61 | ||
62 | pthread_mutex_lock(&next_channel_key_lock); | |
63 | ret = ++_next_channel_key; | |
64 | pthread_mutex_unlock(&next_channel_key_lock); | |
65 | return ret; | |
ffe60014 DG |
66 | } |
67 | ||
68 | /* | |
7972aab2 | 69 | * Return the atomically incremented value of next_session_id. |
ffe60014 | 70 | */ |
d9bf3ca4 | 71 | static uint64_t get_next_session_id(void) |
ffe60014 | 72 | { |
d9bf3ca4 MD |
73 | uint64_t ret; |
74 | ||
75 | pthread_mutex_lock(&next_session_id_lock); | |
76 | ret = ++_next_session_id; | |
77 | pthread_mutex_unlock(&next_session_id_lock); | |
78 | return ret; | |
ffe60014 DG |
79 | } |
80 | ||
d65d2de8 DG |
81 | static void copy_channel_attr_to_ustctl( |
82 | struct ustctl_consumer_channel_attr *attr, | |
83 | struct lttng_ust_channel_attr *uattr) | |
84 | { | |
85 | /* Copy event attributes since the layout is different. */ | |
86 | attr->subbuf_size = uattr->subbuf_size; | |
87 | attr->num_subbuf = uattr->num_subbuf; | |
88 | attr->overwrite = uattr->overwrite; | |
89 | attr->switch_timer_interval = uattr->switch_timer_interval; | |
90 | attr->read_timer_interval = uattr->read_timer_interval; | |
91 | attr->output = uattr->output; | |
92 | } | |
93 | ||
025faf73 DG |
94 | /* |
95 | * Match function for the hash table lookup. | |
96 | * | |
97 | * It matches an ust app event based on three attributes which are the event | |
98 | * name, the filter bytecode and the loglevel. | |
99 | */ | |
18eace3b DG |
100 | static int ht_match_ust_app_event(struct cds_lfht_node *node, const void *_key) |
101 | { | |
102 | struct ust_app_event *event; | |
103 | const struct ust_app_ht_key *key; | |
104 | ||
105 | assert(node); | |
106 | assert(_key); | |
107 | ||
108 | event = caa_container_of(node, struct ust_app_event, node.node); | |
109 | key = _key; | |
110 | ||
1af53eb5 | 111 | /* Match the 4 elements of the key: name, filter, loglevel, exclusions */ |
18eace3b DG |
112 | |
113 | /* Event name */ | |
114 | if (strncmp(event->attr.name, key->name, sizeof(event->attr.name)) != 0) { | |
115 | goto no_match; | |
116 | } | |
117 | ||
118 | /* Event loglevel. */ | |
119 | if (event->attr.loglevel != key->loglevel) { | |
025faf73 DG |
120 | if (event->attr.loglevel_type == LTTNG_UST_LOGLEVEL_ALL |
121 | && key->loglevel == 0 && event->attr.loglevel == -1) { | |
122 | /* | |
123 | * Match is accepted. This is because on event creation, the | |
124 | * loglevel is set to -1 if the event loglevel type is ALL so 0 and | |
125 | * -1 are accepted for this loglevel type since 0 is the one set by | |
126 | * the API when receiving an enable event. | |
127 | */ | |
128 | } else { | |
129 | goto no_match; | |
130 | } | |
18eace3b DG |
131 | } |
132 | ||
133 | /* One of the filters is NULL, fail. */ | |
134 | if ((key->filter && !event->filter) || (!key->filter && event->filter)) { | |
135 | goto no_match; | |
136 | } | |
137 | ||
025faf73 DG |
138 | if (key->filter && event->filter) { |
139 | /* Both filters exists, check length followed by the bytecode. */ | |
140 | if (event->filter->len != key->filter->len || | |
141 | memcmp(event->filter->data, key->filter->data, | |
142 | event->filter->len) != 0) { | |
143 | goto no_match; | |
144 | } | |
18eace3b DG |
145 | } |
146 | ||
1af53eb5 JI |
147 | /* One of the exclusions is NULL, fail. */ |
148 | if ((key->exclusion && !event->exclusion) || (!key->exclusion && event->exclusion)) { | |
149 | goto no_match; | |
150 | } | |
151 | ||
152 | if (key->exclusion && event->exclusion) { | |
153 | /* Both exclusions exists, check count followed by the names. */ | |
154 | if (event->exclusion->count != key->exclusion->count || | |
155 | memcmp(event->exclusion->names, key->exclusion->names, | |
156 | event->exclusion->count * LTTNG_UST_SYM_NAME_LEN) != 0) { | |
157 | goto no_match; | |
158 | } | |
159 | } | |
160 | ||
161 | ||
025faf73 | 162 | /* Match. */ |
18eace3b DG |
163 | return 1; |
164 | ||
165 | no_match: | |
166 | return 0; | |
18eace3b DG |
167 | } |
168 | ||
025faf73 DG |
169 | /* |
170 | * Unique add of an ust app event in the given ht. This uses the custom | |
171 | * ht_match_ust_app_event match function and the event name as hash. | |
172 | */ | |
d0b96690 | 173 | static void add_unique_ust_app_event(struct ust_app_channel *ua_chan, |
18eace3b DG |
174 | struct ust_app_event *event) |
175 | { | |
176 | struct cds_lfht_node *node_ptr; | |
177 | struct ust_app_ht_key key; | |
d0b96690 | 178 | struct lttng_ht *ht; |
18eace3b | 179 | |
d0b96690 DG |
180 | assert(ua_chan); |
181 | assert(ua_chan->events); | |
18eace3b DG |
182 | assert(event); |
183 | ||
d0b96690 | 184 | ht = ua_chan->events; |
18eace3b DG |
185 | key.name = event->attr.name; |
186 | key.filter = event->filter; | |
187 | key.loglevel = event->attr.loglevel; | |
91c89f23 | 188 | key.exclusion = event->exclusion; |
18eace3b DG |
189 | |
190 | node_ptr = cds_lfht_add_unique(ht->ht, | |
191 | ht->hash_fct(event->node.key, lttng_ht_seed), | |
192 | ht_match_ust_app_event, &key, &event->node.node); | |
193 | assert(node_ptr == &event->node.node); | |
194 | } | |
195 | ||
d88aee68 DG |
196 | /* |
197 | * Close the notify socket from the given RCU head object. This MUST be called | |
198 | * through a call_rcu(). | |
199 | */ | |
200 | static void close_notify_sock_rcu(struct rcu_head *head) | |
201 | { | |
202 | int ret; | |
203 | struct ust_app_notify_sock_obj *obj = | |
204 | caa_container_of(head, struct ust_app_notify_sock_obj, head); | |
205 | ||
206 | /* Must have a valid fd here. */ | |
207 | assert(obj->fd >= 0); | |
208 | ||
209 | ret = close(obj->fd); | |
210 | if (ret) { | |
211 | ERR("close notify sock %d RCU", obj->fd); | |
212 | } | |
213 | lttng_fd_put(LTTNG_FD_APPS, 1); | |
214 | ||
215 | free(obj); | |
216 | } | |
217 | ||
7972aab2 DG |
218 | /* |
219 | * Return the session registry according to the buffer type of the given | |
220 | * session. | |
221 | * | |
222 | * A registry per UID object MUST exists before calling this function or else | |
223 | * it assert() if not found. RCU read side lock must be acquired. | |
224 | */ | |
225 | static struct ust_registry_session *get_session_registry( | |
226 | struct ust_app_session *ua_sess) | |
227 | { | |
228 | struct ust_registry_session *registry = NULL; | |
229 | ||
230 | assert(ua_sess); | |
231 | ||
232 | switch (ua_sess->buffer_type) { | |
233 | case LTTNG_BUFFER_PER_PID: | |
234 | { | |
235 | struct buffer_reg_pid *reg_pid = buffer_reg_pid_find(ua_sess->id); | |
236 | if (!reg_pid) { | |
237 | goto error; | |
238 | } | |
239 | registry = reg_pid->registry->reg.ust; | |
240 | break; | |
241 | } | |
242 | case LTTNG_BUFFER_PER_UID: | |
243 | { | |
244 | struct buffer_reg_uid *reg_uid = buffer_reg_uid_find( | |
245 | ua_sess->tracing_id, ua_sess->bits_per_long, ua_sess->uid); | |
246 | if (!reg_uid) { | |
247 | goto error; | |
248 | } | |
249 | registry = reg_uid->registry->reg.ust; | |
250 | break; | |
251 | } | |
252 | default: | |
253 | assert(0); | |
254 | }; | |
255 | ||
256 | error: | |
257 | return registry; | |
258 | } | |
259 | ||
55cc08a6 DG |
260 | /* |
261 | * Delete ust context safely. RCU read lock must be held before calling | |
262 | * this function. | |
263 | */ | |
264 | static | |
265 | void delete_ust_app_ctx(int sock, struct ust_app_ctx *ua_ctx) | |
266 | { | |
ffe60014 DG |
267 | int ret; |
268 | ||
269 | assert(ua_ctx); | |
270 | ||
55cc08a6 | 271 | if (ua_ctx->obj) { |
ffe60014 | 272 | ret = ustctl_release_object(sock, ua_ctx->obj); |
d0b96690 DG |
273 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
274 | ERR("UST app sock %d release ctx obj handle %d failed with ret %d", | |
275 | sock, ua_ctx->obj->handle, ret); | |
ffe60014 | 276 | } |
55cc08a6 DG |
277 | free(ua_ctx->obj); |
278 | } | |
279 | free(ua_ctx); | |
280 | } | |
281 | ||
d80a6244 DG |
282 | /* |
283 | * Delete ust app event safely. RCU read lock must be held before calling | |
284 | * this function. | |
285 | */ | |
8b366481 DG |
286 | static |
287 | void delete_ust_app_event(int sock, struct ust_app_event *ua_event) | |
d80a6244 | 288 | { |
ffe60014 DG |
289 | int ret; |
290 | ||
291 | assert(ua_event); | |
292 | ||
53a80697 | 293 | free(ua_event->filter); |
951f0b71 JI |
294 | if (ua_event->exclusion != NULL) |
295 | free(ua_event->exclusion); | |
edb67388 | 296 | if (ua_event->obj != NULL) { |
ffe60014 DG |
297 | ret = ustctl_release_object(sock, ua_event->obj); |
298 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
299 | ERR("UST app sock %d release event obj failed with ret %d", | |
300 | sock, ret); | |
301 | } | |
edb67388 DG |
302 | free(ua_event->obj); |
303 | } | |
d80a6244 DG |
304 | free(ua_event); |
305 | } | |
306 | ||
307 | /* | |
7972aab2 DG |
308 | * Release ust data object of the given stream. |
309 | * | |
310 | * Return 0 on success or else a negative value. | |
d80a6244 | 311 | */ |
7972aab2 | 312 | static int release_ust_app_stream(int sock, struct ust_app_stream *stream) |
d80a6244 | 313 | { |
7972aab2 | 314 | int ret = 0; |
ffe60014 DG |
315 | |
316 | assert(stream); | |
317 | ||
8b366481 | 318 | if (stream->obj) { |
ffe60014 DG |
319 | ret = ustctl_release_object(sock, stream->obj); |
320 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
321 | ERR("UST app sock %d release stream obj failed with ret %d", | |
322 | sock, ret); | |
323 | } | |
4063050c | 324 | lttng_fd_put(LTTNG_FD_APPS, 2); |
8b366481 DG |
325 | free(stream->obj); |
326 | } | |
7972aab2 DG |
327 | |
328 | return ret; | |
329 | } | |
330 | ||
331 | /* | |
332 | * Delete ust app stream safely. RCU read lock must be held before calling | |
333 | * this function. | |
334 | */ | |
335 | static | |
336 | void delete_ust_app_stream(int sock, struct ust_app_stream *stream) | |
337 | { | |
338 | assert(stream); | |
339 | ||
340 | (void) release_ust_app_stream(sock, stream); | |
84cd17c6 | 341 | free(stream); |
d80a6244 DG |
342 | } |
343 | ||
36b588ed MD |
344 | /* |
345 | * We need to execute ht_destroy outside of RCU read-side critical | |
0b2dc8df MD |
346 | * section and outside of call_rcu thread, so we postpone its execution |
347 | * using ht_cleanup_push. It is simpler than to change the semantic of | |
348 | * the many callers of delete_ust_app_session(). | |
36b588ed MD |
349 | */ |
350 | static | |
351 | void delete_ust_app_channel_rcu(struct rcu_head *head) | |
352 | { | |
353 | struct ust_app_channel *ua_chan = | |
354 | caa_container_of(head, struct ust_app_channel, rcu_head); | |
355 | ||
0b2dc8df MD |
356 | ht_cleanup_push(ua_chan->ctx); |
357 | ht_cleanup_push(ua_chan->events); | |
36b588ed MD |
358 | free(ua_chan); |
359 | } | |
360 | ||
d80a6244 DG |
361 | /* |
362 | * Delete ust app channel safely. RCU read lock must be held before calling | |
363 | * this function. | |
364 | */ | |
8b366481 | 365 | static |
d0b96690 DG |
366 | void delete_ust_app_channel(int sock, struct ust_app_channel *ua_chan, |
367 | struct ust_app *app) | |
d80a6244 DG |
368 | { |
369 | int ret; | |
bec39940 | 370 | struct lttng_ht_iter iter; |
d80a6244 | 371 | struct ust_app_event *ua_event; |
55cc08a6 | 372 | struct ust_app_ctx *ua_ctx; |
030a66fa | 373 | struct ust_app_stream *stream, *stmp; |
7972aab2 | 374 | struct ust_registry_session *registry; |
d80a6244 | 375 | |
ffe60014 DG |
376 | assert(ua_chan); |
377 | ||
378 | DBG3("UST app deleting channel %s", ua_chan->name); | |
379 | ||
55cc08a6 | 380 | /* Wipe stream */ |
d80a6244 | 381 | cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) { |
84cd17c6 | 382 | cds_list_del(&stream->list); |
d80a6244 DG |
383 | delete_ust_app_stream(sock, stream); |
384 | } | |
385 | ||
55cc08a6 | 386 | /* Wipe context */ |
bec39940 | 387 | cds_lfht_for_each_entry(ua_chan->ctx->ht, &iter.iter, ua_ctx, node.node) { |
31746f93 | 388 | cds_list_del(&ua_ctx->list); |
bec39940 | 389 | ret = lttng_ht_del(ua_chan->ctx, &iter); |
55cc08a6 DG |
390 | assert(!ret); |
391 | delete_ust_app_ctx(sock, ua_ctx); | |
392 | } | |
d80a6244 | 393 | |
55cc08a6 | 394 | /* Wipe events */ |
bec39940 DG |
395 | cds_lfht_for_each_entry(ua_chan->events->ht, &iter.iter, ua_event, |
396 | node.node) { | |
397 | ret = lttng_ht_del(ua_chan->events, &iter); | |
525b0740 | 398 | assert(!ret); |
d80a6244 DG |
399 | delete_ust_app_event(sock, ua_event); |
400 | } | |
edb67388 | 401 | |
c8335706 MD |
402 | if (ua_chan->session->buffer_type == LTTNG_BUFFER_PER_PID) { |
403 | /* Wipe and free registry from session registry. */ | |
404 | registry = get_session_registry(ua_chan->session); | |
405 | if (registry) { | |
406 | ust_registry_channel_del_free(registry, ua_chan->key); | |
407 | } | |
7972aab2 | 408 | } |
d0b96690 | 409 | |
edb67388 | 410 | if (ua_chan->obj != NULL) { |
d0b96690 DG |
411 | /* Remove channel from application UST object descriptor. */ |
412 | iter.iter.node = &ua_chan->ust_objd_node.node; | |
c6e62271 DG |
413 | ret = lttng_ht_del(app->ust_objd, &iter); |
414 | assert(!ret); | |
ffe60014 DG |
415 | ret = ustctl_release_object(sock, ua_chan->obj); |
416 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
417 | ERR("UST app sock %d release channel obj failed with ret %d", | |
418 | sock, ret); | |
419 | } | |
7972aab2 | 420 | lttng_fd_put(LTTNG_FD_APPS, 1); |
edb67388 DG |
421 | free(ua_chan->obj); |
422 | } | |
36b588ed | 423 | call_rcu(&ua_chan->rcu_head, delete_ust_app_channel_rcu); |
d80a6244 DG |
424 | } |
425 | ||
331744e3 | 426 | /* |
1b532a60 DG |
427 | * Push metadata to consumer socket. |
428 | * | |
dc2bbdae MD |
429 | * RCU read-side lock must be held to guarantee existance of socket. |
430 | * Must be called with the ust app session lock held. | |
431 | * Must be called with the registry lock held. | |
331744e3 JD |
432 | * |
433 | * On success, return the len of metadata pushed or else a negative value. | |
2c57e06d MD |
434 | * Returning a -EPIPE return value means we could not send the metadata, |
435 | * but it can be caused by recoverable errors (e.g. the application has | |
436 | * terminated concurrently). | |
331744e3 JD |
437 | */ |
438 | ssize_t ust_app_push_metadata(struct ust_registry_session *registry, | |
439 | struct consumer_socket *socket, int send_zero_data) | |
440 | { | |
441 | int ret; | |
442 | char *metadata_str = NULL; | |
443 | size_t len, offset; | |
444 | ssize_t ret_val; | |
445 | ||
446 | assert(registry); | |
447 | assert(socket); | |
1b532a60 | 448 | |
ce34fcd0 | 449 | /* |
dc2bbdae MD |
450 | * Means that no metadata was assigned to the session. This can |
451 | * happens if no start has been done previously. | |
ce34fcd0 MD |
452 | */ |
453 | if (!registry->metadata_key) { | |
ce34fcd0 MD |
454 | return 0; |
455 | } | |
456 | ||
1b532a60 | 457 | /* |
dc2bbdae MD |
458 | * On a push metadata error either the consumer is dead or the |
459 | * metadata channel has been destroyed because its endpoint | |
2c57e06d MD |
460 | * might have died (e.g: relayd), or because the application has |
461 | * exited. If so, the metadata closed flag is set to 1 so we | |
462 | * deny pushing metadata again which is not valid anymore on the | |
463 | * consumer side. | |
1b532a60 DG |
464 | */ |
465 | if (registry->metadata_closed) { | |
466 | return -EPIPE; | |
467 | } | |
331744e3 | 468 | |
331744e3 JD |
469 | offset = registry->metadata_len_sent; |
470 | len = registry->metadata_len - registry->metadata_len_sent; | |
471 | if (len == 0) { | |
472 | DBG3("No metadata to push for metadata key %" PRIu64, | |
473 | registry->metadata_key); | |
474 | ret_val = len; | |
475 | if (send_zero_data) { | |
476 | DBG("No metadata to push"); | |
477 | goto push_data; | |
478 | } | |
479 | goto end; | |
480 | } | |
481 | ||
482 | /* Allocate only what we have to send. */ | |
483 | metadata_str = zmalloc(len); | |
484 | if (!metadata_str) { | |
485 | PERROR("zmalloc ust app metadata string"); | |
486 | ret_val = -ENOMEM; | |
487 | goto error; | |
488 | } | |
489 | /* Copy what we haven't send out. */ | |
490 | memcpy(metadata_str, registry->metadata + offset, len); | |
491 | registry->metadata_len_sent += len; | |
492 | ||
493 | push_data: | |
331744e3 JD |
494 | ret = consumer_push_metadata(socket, registry->metadata_key, |
495 | metadata_str, len, offset); | |
496 | if (ret < 0) { | |
000baf6a | 497 | /* |
dc2bbdae MD |
498 | * There is an acceptable race here between the registry |
499 | * metadata key assignment and the creation on the | |
500 | * consumer. The session daemon can concurrently push | |
501 | * metadata for this registry while being created on the | |
502 | * consumer since the metadata key of the registry is | |
503 | * assigned *before* it is setup to avoid the consumer | |
504 | * to ask for metadata that could possibly be not found | |
505 | * in the session daemon. | |
000baf6a | 506 | * |
dc2bbdae MD |
507 | * The metadata will get pushed either by the session |
508 | * being stopped or the consumer requesting metadata if | |
509 | * that race is triggered. | |
000baf6a DG |
510 | */ |
511 | if (ret == -LTTCOMM_CONSUMERD_CHANNEL_FAIL) { | |
512 | ret = 0; | |
513 | } | |
ffa3f245 | 514 | |
dc2bbdae MD |
515 | /* |
516 | * Update back the actual metadata len sent since it | |
517 | * failed here. | |
518 | */ | |
ffa3f245 | 519 | registry->metadata_len_sent -= len; |
331744e3 JD |
520 | ret_val = ret; |
521 | goto error_push; | |
522 | } | |
523 | ||
524 | free(metadata_str); | |
525 | return len; | |
526 | ||
527 | end: | |
528 | error: | |
ce34fcd0 MD |
529 | if (ret_val) { |
530 | /* | |
dc2bbdae MD |
531 | * On error, flag the registry that the metadata is |
532 | * closed. We were unable to push anything and this | |
533 | * means that either the consumer is not responding or | |
534 | * the metadata cache has been destroyed on the | |
535 | * consumer. | |
ce34fcd0 MD |
536 | */ |
537 | registry->metadata_closed = 1; | |
538 | } | |
331744e3 JD |
539 | error_push: |
540 | free(metadata_str); | |
541 | return ret_val; | |
542 | } | |
543 | ||
d88aee68 | 544 | /* |
ce34fcd0 | 545 | * For a given application and session, push metadata to consumer. |
331744e3 JD |
546 | * Either sock or consumer is required : if sock is NULL, the default |
547 | * socket to send the metadata is retrieved from consumer, if sock | |
548 | * is not NULL we use it to send the metadata. | |
ce34fcd0 | 549 | * RCU read-side lock must be held while calling this function, |
dc2bbdae MD |
550 | * therefore ensuring existance of registry. It also ensures existance |
551 | * of socket throughout this function. | |
d88aee68 DG |
552 | * |
553 | * Return 0 on success else a negative error. | |
2c57e06d MD |
554 | * Returning a -EPIPE return value means we could not send the metadata, |
555 | * but it can be caused by recoverable errors (e.g. the application has | |
556 | * terminated concurrently). | |
d88aee68 | 557 | */ |
7972aab2 DG |
558 | static int push_metadata(struct ust_registry_session *registry, |
559 | struct consumer_output *consumer) | |
d88aee68 | 560 | { |
331744e3 JD |
561 | int ret_val; |
562 | ssize_t ret; | |
d88aee68 DG |
563 | struct consumer_socket *socket; |
564 | ||
7972aab2 DG |
565 | assert(registry); |
566 | assert(consumer); | |
567 | ||
ce34fcd0 | 568 | pthread_mutex_lock(®istry->lock); |
ce34fcd0 | 569 | if (registry->metadata_closed) { |
dc2bbdae MD |
570 | ret_val = -EPIPE; |
571 | goto error; | |
d88aee68 DG |
572 | } |
573 | ||
d88aee68 | 574 | /* Get consumer socket to use to push the metadata.*/ |
7972aab2 DG |
575 | socket = consumer_find_socket_by_bitness(registry->bits_per_long, |
576 | consumer); | |
d88aee68 | 577 | if (!socket) { |
331744e3 | 578 | ret_val = -1; |
ce34fcd0 | 579 | goto error; |
d88aee68 DG |
580 | } |
581 | ||
331744e3 | 582 | ret = ust_app_push_metadata(registry, socket, 0); |
d88aee68 | 583 | if (ret < 0) { |
331744e3 | 584 | ret_val = ret; |
ce34fcd0 | 585 | goto error; |
d88aee68 | 586 | } |
dc2bbdae | 587 | pthread_mutex_unlock(®istry->lock); |
d88aee68 DG |
588 | return 0; |
589 | ||
ce34fcd0 | 590 | error: |
dc2bbdae | 591 | pthread_mutex_unlock(®istry->lock); |
331744e3 | 592 | return ret_val; |
d88aee68 DG |
593 | } |
594 | ||
595 | /* | |
596 | * Send to the consumer a close metadata command for the given session. Once | |
597 | * done, the metadata channel is deleted and the session metadata pointer is | |
dc2bbdae | 598 | * nullified. The session lock MUST be held unless the application is |
d88aee68 DG |
599 | * in the destroy path. |
600 | * | |
601 | * Return 0 on success else a negative value. | |
602 | */ | |
7972aab2 DG |
603 | static int close_metadata(struct ust_registry_session *registry, |
604 | struct consumer_output *consumer) | |
d88aee68 DG |
605 | { |
606 | int ret; | |
607 | struct consumer_socket *socket; | |
608 | ||
7972aab2 DG |
609 | assert(registry); |
610 | assert(consumer); | |
d88aee68 | 611 | |
7972aab2 DG |
612 | rcu_read_lock(); |
613 | ||
ce34fcd0 MD |
614 | pthread_mutex_lock(®istry->lock); |
615 | ||
7972aab2 | 616 | if (!registry->metadata_key || registry->metadata_closed) { |
d88aee68 | 617 | ret = 0; |
1b532a60 | 618 | goto end; |
d88aee68 DG |
619 | } |
620 | ||
d88aee68 | 621 | /* Get consumer socket to use to push the metadata.*/ |
7972aab2 DG |
622 | socket = consumer_find_socket_by_bitness(registry->bits_per_long, |
623 | consumer); | |
d88aee68 DG |
624 | if (!socket) { |
625 | ret = -1; | |
7972aab2 | 626 | goto error; |
d88aee68 DG |
627 | } |
628 | ||
7972aab2 | 629 | ret = consumer_close_metadata(socket, registry->metadata_key); |
d88aee68 | 630 | if (ret < 0) { |
7972aab2 | 631 | goto error; |
d88aee68 DG |
632 | } |
633 | ||
d88aee68 | 634 | error: |
1b532a60 DG |
635 | /* |
636 | * Metadata closed. Even on error this means that the consumer is not | |
637 | * responding or not found so either way a second close should NOT be emit | |
638 | * for this registry. | |
639 | */ | |
640 | registry->metadata_closed = 1; | |
641 | end: | |
ce34fcd0 | 642 | pthread_mutex_unlock(®istry->lock); |
7972aab2 | 643 | rcu_read_unlock(); |
d88aee68 DG |
644 | return ret; |
645 | } | |
646 | ||
36b588ed MD |
647 | /* |
648 | * We need to execute ht_destroy outside of RCU read-side critical | |
0b2dc8df MD |
649 | * section and outside of call_rcu thread, so we postpone its execution |
650 | * using ht_cleanup_push. It is simpler than to change the semantic of | |
651 | * the many callers of delete_ust_app_session(). | |
36b588ed MD |
652 | */ |
653 | static | |
654 | void delete_ust_app_session_rcu(struct rcu_head *head) | |
655 | { | |
656 | struct ust_app_session *ua_sess = | |
657 | caa_container_of(head, struct ust_app_session, rcu_head); | |
658 | ||
0b2dc8df | 659 | ht_cleanup_push(ua_sess->channels); |
36b588ed MD |
660 | free(ua_sess); |
661 | } | |
662 | ||
d80a6244 DG |
663 | /* |
664 | * Delete ust app session safely. RCU read lock must be held before calling | |
665 | * this function. | |
666 | */ | |
8b366481 | 667 | static |
d0b96690 DG |
668 | void delete_ust_app_session(int sock, struct ust_app_session *ua_sess, |
669 | struct ust_app *app) | |
d80a6244 DG |
670 | { |
671 | int ret; | |
bec39940 | 672 | struct lttng_ht_iter iter; |
d80a6244 | 673 | struct ust_app_channel *ua_chan; |
7972aab2 | 674 | struct ust_registry_session *registry; |
d80a6244 | 675 | |
d88aee68 DG |
676 | assert(ua_sess); |
677 | ||
1b532a60 DG |
678 | pthread_mutex_lock(&ua_sess->lock); |
679 | ||
b161602a MD |
680 | assert(!ua_sess->deleted); |
681 | ua_sess->deleted = true; | |
682 | ||
7972aab2 | 683 | registry = get_session_registry(ua_sess); |
ce34fcd0 | 684 | if (registry) { |
d88aee68 | 685 | /* Push metadata for application before freeing the application. */ |
7972aab2 | 686 | (void) push_metadata(registry, ua_sess->consumer); |
d88aee68 | 687 | |
7972aab2 DG |
688 | /* |
689 | * Don't ask to close metadata for global per UID buffers. Close | |
1b532a60 DG |
690 | * metadata only on destroy trace session in this case. Also, the |
691 | * previous push metadata could have flag the metadata registry to | |
692 | * close so don't send a close command if closed. | |
7972aab2 | 693 | */ |
ce34fcd0 | 694 | if (ua_sess->buffer_type != LTTNG_BUFFER_PER_UID) { |
7972aab2 DG |
695 | /* And ask to close it for this session registry. */ |
696 | (void) close_metadata(registry, ua_sess->consumer); | |
697 | } | |
d80a6244 DG |
698 | } |
699 | ||
bec39940 DG |
700 | cds_lfht_for_each_entry(ua_sess->channels->ht, &iter.iter, ua_chan, |
701 | node.node) { | |
702 | ret = lttng_ht_del(ua_sess->channels, &iter); | |
525b0740 | 703 | assert(!ret); |
d0b96690 | 704 | delete_ust_app_channel(sock, ua_chan, app); |
d80a6244 | 705 | } |
d80a6244 | 706 | |
7972aab2 DG |
707 | /* In case of per PID, the registry is kept in the session. */ |
708 | if (ua_sess->buffer_type == LTTNG_BUFFER_PER_PID) { | |
709 | struct buffer_reg_pid *reg_pid = buffer_reg_pid_find(ua_sess->id); | |
710 | if (reg_pid) { | |
711 | buffer_reg_pid_remove(reg_pid); | |
712 | buffer_reg_pid_destroy(reg_pid); | |
713 | } | |
714 | } | |
d0b96690 | 715 | |
aee6bafd | 716 | if (ua_sess->handle != -1) { |
ffe60014 DG |
717 | ret = ustctl_release_handle(sock, ua_sess->handle); |
718 | if (ret < 0 && ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
719 | ERR("UST app sock %d release session handle failed with ret %d", | |
720 | sock, ret); | |
721 | } | |
aee6bafd | 722 | } |
1b532a60 DG |
723 | pthread_mutex_unlock(&ua_sess->lock); |
724 | ||
6addfa37 MD |
725 | consumer_output_put(ua_sess->consumer); |
726 | ||
36b588ed | 727 | call_rcu(&ua_sess->rcu_head, delete_ust_app_session_rcu); |
d80a6244 | 728 | } |
91d76f53 DG |
729 | |
730 | /* | |
284d8f55 DG |
731 | * Delete a traceable application structure from the global list. Never call |
732 | * this function outside of a call_rcu call. | |
36b588ed MD |
733 | * |
734 | * RCU read side lock should _NOT_ be held when calling this function. | |
91d76f53 | 735 | */ |
8b366481 DG |
736 | static |
737 | void delete_ust_app(struct ust_app *app) | |
91d76f53 | 738 | { |
8b366481 | 739 | int ret, sock; |
d42f20df | 740 | struct ust_app_session *ua_sess, *tmp_ua_sess; |
44d3bd01 | 741 | |
d80a6244 | 742 | /* Delete ust app sessions info */ |
852d0037 DG |
743 | sock = app->sock; |
744 | app->sock = -1; | |
d80a6244 | 745 | |
8b366481 | 746 | /* Wipe sessions */ |
d42f20df DG |
747 | cds_list_for_each_entry_safe(ua_sess, tmp_ua_sess, &app->teardown_head, |
748 | teardown_node) { | |
749 | /* Free every object in the session and the session. */ | |
36b588ed | 750 | rcu_read_lock(); |
d0b96690 | 751 | delete_ust_app_session(sock, ua_sess, app); |
36b588ed | 752 | rcu_read_unlock(); |
d80a6244 | 753 | } |
36b588ed | 754 | |
0b2dc8df MD |
755 | ht_cleanup_push(app->sessions); |
756 | ht_cleanup_push(app->ust_objd); | |
d80a6244 | 757 | |
6414a713 | 758 | /* |
852d0037 DG |
759 | * Wait until we have deleted the application from the sock hash table |
760 | * before closing this socket, otherwise an application could re-use the | |
761 | * socket ID and race with the teardown, using the same hash table entry. | |
762 | * | |
763 | * It's OK to leave the close in call_rcu. We want it to stay unique for | |
764 | * all RCU readers that could run concurrently with unregister app, | |
765 | * therefore we _need_ to only close that socket after a grace period. So | |
766 | * it should stay in this RCU callback. | |
767 | * | |
768 | * This close() is a very important step of the synchronization model so | |
769 | * every modification to this function must be carefully reviewed. | |
6414a713 | 770 | */ |
799e2c4f MD |
771 | ret = close(sock); |
772 | if (ret) { | |
773 | PERROR("close"); | |
774 | } | |
4063050c | 775 | lttng_fd_put(LTTNG_FD_APPS, 1); |
d80a6244 | 776 | |
852d0037 | 777 | DBG2("UST app pid %d deleted", app->pid); |
284d8f55 | 778 | free(app); |
099e26bd DG |
779 | } |
780 | ||
781 | /* | |
f6a9efaa | 782 | * URCU intermediate call to delete an UST app. |
099e26bd | 783 | */ |
8b366481 DG |
784 | static |
785 | void delete_ust_app_rcu(struct rcu_head *head) | |
099e26bd | 786 | { |
bec39940 DG |
787 | struct lttng_ht_node_ulong *node = |
788 | caa_container_of(head, struct lttng_ht_node_ulong, head); | |
f6a9efaa | 789 | struct ust_app *app = |
852d0037 | 790 | caa_container_of(node, struct ust_app, pid_n); |
f6a9efaa | 791 | |
852d0037 | 792 | DBG3("Call RCU deleting app PID %d", app->pid); |
f6a9efaa | 793 | delete_ust_app(app); |
099e26bd DG |
794 | } |
795 | ||
ffe60014 DG |
796 | /* |
797 | * Delete the session from the application ht and delete the data structure by | |
798 | * freeing every object inside and releasing them. | |
799 | */ | |
d0b96690 | 800 | static void destroy_app_session(struct ust_app *app, |
ffe60014 DG |
801 | struct ust_app_session *ua_sess) |
802 | { | |
803 | int ret; | |
804 | struct lttng_ht_iter iter; | |
805 | ||
806 | assert(app); | |
807 | assert(ua_sess); | |
808 | ||
809 | iter.iter.node = &ua_sess->node.node; | |
810 | ret = lttng_ht_del(app->sessions, &iter); | |
811 | if (ret) { | |
812 | /* Already scheduled for teardown. */ | |
813 | goto end; | |
814 | } | |
815 | ||
816 | /* Once deleted, free the data structure. */ | |
d0b96690 | 817 | delete_ust_app_session(app->sock, ua_sess, app); |
ffe60014 DG |
818 | |
819 | end: | |
820 | return; | |
821 | } | |
822 | ||
8b366481 DG |
823 | /* |
824 | * Alloc new UST app session. | |
825 | */ | |
826 | static | |
d0b96690 | 827 | struct ust_app_session *alloc_ust_app_session(struct ust_app *app) |
8b366481 DG |
828 | { |
829 | struct ust_app_session *ua_sess; | |
830 | ||
831 | /* Init most of the default value by allocating and zeroing */ | |
832 | ua_sess = zmalloc(sizeof(struct ust_app_session)); | |
833 | if (ua_sess == NULL) { | |
834 | PERROR("malloc"); | |
ffe60014 | 835 | goto error_free; |
8b366481 DG |
836 | } |
837 | ||
838 | ua_sess->handle = -1; | |
bec39940 | 839 | ua_sess->channels = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); |
ad7a9107 | 840 | ua_sess->metadata_attr.type = LTTNG_UST_CHAN_METADATA; |
84ad93e8 | 841 | pthread_mutex_init(&ua_sess->lock, NULL); |
ad7a9107 | 842 | |
8b366481 DG |
843 | return ua_sess; |
844 | ||
ffe60014 | 845 | error_free: |
8b366481 DG |
846 | return NULL; |
847 | } | |
848 | ||
849 | /* | |
850 | * Alloc new UST app channel. | |
851 | */ | |
852 | static | |
853 | struct ust_app_channel *alloc_ust_app_channel(char *name, | |
d0b96690 | 854 | struct ust_app_session *ua_sess, |
ffe60014 | 855 | struct lttng_ust_channel_attr *attr) |
8b366481 DG |
856 | { |
857 | struct ust_app_channel *ua_chan; | |
858 | ||
859 | /* Init most of the default value by allocating and zeroing */ | |
860 | ua_chan = zmalloc(sizeof(struct ust_app_channel)); | |
861 | if (ua_chan == NULL) { | |
862 | PERROR("malloc"); | |
863 | goto error; | |
864 | } | |
865 | ||
866 | /* Setup channel name */ | |
867 | strncpy(ua_chan->name, name, sizeof(ua_chan->name)); | |
868 | ua_chan->name[sizeof(ua_chan->name) - 1] = '\0'; | |
869 | ||
870 | ua_chan->enabled = 1; | |
871 | ua_chan->handle = -1; | |
45893984 | 872 | ua_chan->session = ua_sess; |
ffe60014 | 873 | ua_chan->key = get_next_channel_key(); |
bec39940 DG |
874 | ua_chan->ctx = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
875 | ua_chan->events = lttng_ht_new(0, LTTNG_HT_TYPE_STRING); | |
876 | lttng_ht_node_init_str(&ua_chan->node, ua_chan->name); | |
8b366481 DG |
877 | |
878 | CDS_INIT_LIST_HEAD(&ua_chan->streams.head); | |
31746f93 | 879 | CDS_INIT_LIST_HEAD(&ua_chan->ctx_list); |
8b366481 DG |
880 | |
881 | /* Copy attributes */ | |
882 | if (attr) { | |
ffe60014 | 883 | /* Translate from lttng_ust_channel to ustctl_consumer_channel_attr. */ |
2fe6e7f5 DG |
884 | ua_chan->attr.subbuf_size = attr->subbuf_size; |
885 | ua_chan->attr.num_subbuf = attr->num_subbuf; | |
886 | ua_chan->attr.overwrite = attr->overwrite; | |
887 | ua_chan->attr.switch_timer_interval = attr->switch_timer_interval; | |
888 | ua_chan->attr.read_timer_interval = attr->read_timer_interval; | |
889 | ua_chan->attr.output = attr->output; | |
8b366481 | 890 | } |
ffe60014 DG |
891 | /* By default, the channel is a per cpu channel. */ |
892 | ua_chan->attr.type = LTTNG_UST_CHAN_PER_CPU; | |
8b366481 DG |
893 | |
894 | DBG3("UST app channel %s allocated", ua_chan->name); | |
895 | ||
896 | return ua_chan; | |
897 | ||
898 | error: | |
899 | return NULL; | |
900 | } | |
901 | ||
37f1c236 DG |
902 | /* |
903 | * Allocate and initialize a UST app stream. | |
904 | * | |
905 | * Return newly allocated stream pointer or NULL on error. | |
906 | */ | |
ffe60014 | 907 | struct ust_app_stream *ust_app_alloc_stream(void) |
37f1c236 DG |
908 | { |
909 | struct ust_app_stream *stream = NULL; | |
910 | ||
911 | stream = zmalloc(sizeof(*stream)); | |
912 | if (stream == NULL) { | |
913 | PERROR("zmalloc ust app stream"); | |
914 | goto error; | |
915 | } | |
916 | ||
917 | /* Zero could be a valid value for a handle so flag it to -1. */ | |
918 | stream->handle = -1; | |
919 | ||
920 | error: | |
921 | return stream; | |
922 | } | |
923 | ||
8b366481 DG |
924 | /* |
925 | * Alloc new UST app event. | |
926 | */ | |
927 | static | |
928 | struct ust_app_event *alloc_ust_app_event(char *name, | |
929 | struct lttng_ust_event *attr) | |
930 | { | |
931 | struct ust_app_event *ua_event; | |
932 | ||
933 | /* Init most of the default value by allocating and zeroing */ | |
934 | ua_event = zmalloc(sizeof(struct ust_app_event)); | |
935 | if (ua_event == NULL) { | |
936 | PERROR("malloc"); | |
937 | goto error; | |
938 | } | |
939 | ||
940 | ua_event->enabled = 1; | |
941 | strncpy(ua_event->name, name, sizeof(ua_event->name)); | |
942 | ua_event->name[sizeof(ua_event->name) - 1] = '\0'; | |
bec39940 | 943 | lttng_ht_node_init_str(&ua_event->node, ua_event->name); |
8b366481 DG |
944 | |
945 | /* Copy attributes */ | |
946 | if (attr) { | |
947 | memcpy(&ua_event->attr, attr, sizeof(ua_event->attr)); | |
948 | } | |
949 | ||
950 | DBG3("UST app event %s allocated", ua_event->name); | |
951 | ||
952 | return ua_event; | |
953 | ||
954 | error: | |
955 | return NULL; | |
956 | } | |
957 | ||
958 | /* | |
959 | * Alloc new UST app context. | |
960 | */ | |
961 | static | |
962 | struct ust_app_ctx *alloc_ust_app_ctx(struct lttng_ust_context *uctx) | |
963 | { | |
964 | struct ust_app_ctx *ua_ctx; | |
965 | ||
966 | ua_ctx = zmalloc(sizeof(struct ust_app_ctx)); | |
967 | if (ua_ctx == NULL) { | |
968 | goto error; | |
969 | } | |
970 | ||
31746f93 DG |
971 | CDS_INIT_LIST_HEAD(&ua_ctx->list); |
972 | ||
8b366481 DG |
973 | if (uctx) { |
974 | memcpy(&ua_ctx->ctx, uctx, sizeof(ua_ctx->ctx)); | |
975 | } | |
976 | ||
977 | DBG3("UST app context %d allocated", ua_ctx->ctx.ctx); | |
978 | ||
979 | error: | |
980 | return ua_ctx; | |
981 | } | |
982 | ||
025faf73 DG |
983 | /* |
984 | * Allocate a filter and copy the given original filter. | |
985 | * | |
986 | * Return allocated filter or NULL on error. | |
987 | */ | |
51755dc8 JG |
988 | static struct lttng_filter_bytecode *copy_filter_bytecode( |
989 | struct lttng_filter_bytecode *orig_f) | |
025faf73 | 990 | { |
51755dc8 | 991 | struct lttng_filter_bytecode *filter = NULL; |
025faf73 DG |
992 | |
993 | /* Copy filter bytecode */ | |
994 | filter = zmalloc(sizeof(*filter) + orig_f->len); | |
995 | if (!filter) { | |
51755dc8 | 996 | PERROR("zmalloc alloc filter bytecode"); |
025faf73 DG |
997 | goto error; |
998 | } | |
999 | ||
1000 | memcpy(filter, orig_f, sizeof(*filter) + orig_f->len); | |
1001 | ||
1002 | error: | |
1003 | return filter; | |
1004 | } | |
1005 | ||
51755dc8 JG |
1006 | /* |
1007 | * Create a liblttng-ust filter bytecode from given bytecode. | |
1008 | * | |
1009 | * Return allocated filter or NULL on error. | |
1010 | */ | |
1011 | static struct lttng_ust_filter_bytecode *create_ust_bytecode_from_bytecode( | |
1012 | struct lttng_filter_bytecode *orig_f) | |
1013 | { | |
1014 | struct lttng_ust_filter_bytecode *filter = NULL; | |
1015 | ||
1016 | /* Copy filter bytecode */ | |
1017 | filter = zmalloc(sizeof(*filter) + orig_f->len); | |
1018 | if (!filter) { | |
1019 | PERROR("zmalloc alloc ust filter bytecode"); | |
1020 | goto error; | |
1021 | } | |
1022 | ||
1023 | assert(sizeof(struct lttng_filter_bytecode) == | |
1024 | sizeof(struct lttng_ust_filter_bytecode)); | |
1025 | memcpy(filter, orig_f, sizeof(*filter) + orig_f->len); | |
1026 | error: | |
1027 | return filter; | |
1028 | } | |
1029 | ||
099e26bd | 1030 | /* |
421cb601 DG |
1031 | * Find an ust_app using the sock and return it. RCU read side lock must be |
1032 | * held before calling this helper function. | |
099e26bd | 1033 | */ |
f20baf8e | 1034 | struct ust_app *ust_app_find_by_sock(int sock) |
099e26bd | 1035 | { |
bec39940 | 1036 | struct lttng_ht_node_ulong *node; |
bec39940 | 1037 | struct lttng_ht_iter iter; |
f6a9efaa | 1038 | |
852d0037 | 1039 | lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &iter); |
bec39940 | 1040 | node = lttng_ht_iter_get_node_ulong(&iter); |
f6a9efaa DG |
1041 | if (node == NULL) { |
1042 | DBG2("UST app find by sock %d not found", sock); | |
f6a9efaa DG |
1043 | goto error; |
1044 | } | |
852d0037 DG |
1045 | |
1046 | return caa_container_of(node, struct ust_app, sock_n); | |
f6a9efaa DG |
1047 | |
1048 | error: | |
1049 | return NULL; | |
099e26bd DG |
1050 | } |
1051 | ||
d0b96690 DG |
1052 | /* |
1053 | * Find an ust_app using the notify sock and return it. RCU read side lock must | |
1054 | * be held before calling this helper function. | |
1055 | */ | |
1056 | static struct ust_app *find_app_by_notify_sock(int sock) | |
1057 | { | |
1058 | struct lttng_ht_node_ulong *node; | |
1059 | struct lttng_ht_iter iter; | |
1060 | ||
1061 | lttng_ht_lookup(ust_app_ht_by_notify_sock, (void *)((unsigned long) sock), | |
1062 | &iter); | |
1063 | node = lttng_ht_iter_get_node_ulong(&iter); | |
1064 | if (node == NULL) { | |
1065 | DBG2("UST app find by notify sock %d not found", sock); | |
1066 | goto error; | |
1067 | } | |
1068 | ||
1069 | return caa_container_of(node, struct ust_app, notify_sock_n); | |
1070 | ||
1071 | error: | |
1072 | return NULL; | |
1073 | } | |
1074 | ||
025faf73 DG |
1075 | /* |
1076 | * Lookup for an ust app event based on event name, filter bytecode and the | |
1077 | * event loglevel. | |
1078 | * | |
1079 | * Return an ust_app_event object or NULL on error. | |
1080 | */ | |
18eace3b | 1081 | static struct ust_app_event *find_ust_app_event(struct lttng_ht *ht, |
51755dc8 | 1082 | char *name, struct lttng_filter_bytecode *filter, int loglevel, |
39c5a3a7 | 1083 | const struct lttng_event_exclusion *exclusion) |
18eace3b DG |
1084 | { |
1085 | struct lttng_ht_iter iter; | |
1086 | struct lttng_ht_node_str *node; | |
1087 | struct ust_app_event *event = NULL; | |
1088 | struct ust_app_ht_key key; | |
18eace3b DG |
1089 | |
1090 | assert(name); | |
1091 | assert(ht); | |
1092 | ||
1093 | /* Setup key for event lookup. */ | |
1094 | key.name = name; | |
1095 | key.filter = filter; | |
1096 | key.loglevel = loglevel; | |
39c5a3a7 | 1097 | /* lttng_event_exclusion and lttng_ust_event_exclusion structures are similar */ |
51755dc8 | 1098 | key.exclusion = exclusion; |
18eace3b | 1099 | |
025faf73 DG |
1100 | /* Lookup using the event name as hash and a custom match fct. */ |
1101 | cds_lfht_lookup(ht->ht, ht->hash_fct((void *) name, lttng_ht_seed), | |
1102 | ht_match_ust_app_event, &key, &iter.iter); | |
18eace3b DG |
1103 | node = lttng_ht_iter_get_node_str(&iter); |
1104 | if (node == NULL) { | |
1105 | goto end; | |
1106 | } | |
1107 | ||
1108 | event = caa_container_of(node, struct ust_app_event, node); | |
1109 | ||
1110 | end: | |
18eace3b DG |
1111 | return event; |
1112 | } | |
1113 | ||
55cc08a6 DG |
1114 | /* |
1115 | * Create the channel context on the tracer. | |
d0b96690 DG |
1116 | * |
1117 | * Called with UST app session lock held. | |
55cc08a6 DG |
1118 | */ |
1119 | static | |
1120 | int create_ust_channel_context(struct ust_app_channel *ua_chan, | |
1121 | struct ust_app_ctx *ua_ctx, struct ust_app *app) | |
1122 | { | |
1123 | int ret; | |
1124 | ||
840cb59c | 1125 | health_code_update(); |
86acf0da | 1126 | |
852d0037 | 1127 | ret = ustctl_add_context(app->sock, &ua_ctx->ctx, |
55cc08a6 DG |
1128 | ua_chan->obj, &ua_ctx->obj); |
1129 | if (ret < 0) { | |
ffe60014 DG |
1130 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
1131 | ERR("UST app create channel context failed for app (pid: %d) " | |
1132 | "with ret %d", app->pid, ret); | |
1133 | } else { | |
3757b385 DG |
1134 | /* |
1135 | * This is normal behavior, an application can die during the | |
1136 | * creation process. Don't report an error so the execution can | |
1137 | * continue normally. | |
1138 | */ | |
1139 | ret = 0; | |
ffe60014 DG |
1140 | DBG3("UST app disable event failed. Application is dead."); |
1141 | } | |
55cc08a6 DG |
1142 | goto error; |
1143 | } | |
1144 | ||
1145 | ua_ctx->handle = ua_ctx->obj->handle; | |
1146 | ||
d0b96690 DG |
1147 | DBG2("UST app context handle %d created successfully for channel %s", |
1148 | ua_ctx->handle, ua_chan->name); | |
55cc08a6 DG |
1149 | |
1150 | error: | |
840cb59c | 1151 | health_code_update(); |
55cc08a6 DG |
1152 | return ret; |
1153 | } | |
1154 | ||
53a80697 MD |
1155 | /* |
1156 | * Set the filter on the tracer. | |
1157 | */ | |
1158 | static | |
1159 | int set_ust_event_filter(struct ust_app_event *ua_event, | |
1160 | struct ust_app *app) | |
1161 | { | |
1162 | int ret; | |
51755dc8 | 1163 | struct lttng_ust_filter_bytecode *ust_bytecode = NULL; |
53a80697 | 1164 | |
840cb59c | 1165 | health_code_update(); |
86acf0da | 1166 | |
53a80697 | 1167 | if (!ua_event->filter) { |
86acf0da DG |
1168 | ret = 0; |
1169 | goto error; | |
53a80697 MD |
1170 | } |
1171 | ||
51755dc8 JG |
1172 | ust_bytecode = create_ust_bytecode_from_bytecode(ua_event->filter); |
1173 | if (!ust_bytecode) { | |
1174 | ret = -LTTNG_ERR_NOMEM; | |
1175 | goto error; | |
1176 | } | |
1177 | ret = ustctl_set_filter(app->sock, ust_bytecode, | |
53a80697 MD |
1178 | ua_event->obj); |
1179 | if (ret < 0) { | |
ffe60014 DG |
1180 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
1181 | ERR("UST app event %s filter failed for app (pid: %d) " | |
1182 | "with ret %d", ua_event->attr.name, app->pid, ret); | |
1183 | } else { | |
3757b385 DG |
1184 | /* |
1185 | * This is normal behavior, an application can die during the | |
1186 | * creation process. Don't report an error so the execution can | |
1187 | * continue normally. | |
1188 | */ | |
1189 | ret = 0; | |
ffe60014 DG |
1190 | DBG3("UST app filter event failed. Application is dead."); |
1191 | } | |
53a80697 MD |
1192 | goto error; |
1193 | } | |
1194 | ||
1195 | DBG2("UST filter set successfully for event %s", ua_event->name); | |
1196 | ||
1197 | error: | |
840cb59c | 1198 | health_code_update(); |
51755dc8 | 1199 | free(ust_bytecode); |
53a80697 MD |
1200 | return ret; |
1201 | } | |
1202 | ||
51755dc8 JG |
1203 | static |
1204 | struct lttng_ust_event_exclusion *create_ust_exclusion_from_exclusion( | |
1205 | struct lttng_event_exclusion *exclusion) | |
1206 | { | |
1207 | struct lttng_ust_event_exclusion *ust_exclusion = NULL; | |
1208 | size_t exclusion_alloc_size = sizeof(struct lttng_ust_event_exclusion) + | |
1209 | LTTNG_UST_SYM_NAME_LEN * exclusion->count; | |
1210 | ||
1211 | ust_exclusion = zmalloc(exclusion_alloc_size); | |
1212 | if (!ust_exclusion) { | |
1213 | PERROR("malloc"); | |
1214 | goto end; | |
1215 | } | |
1216 | ||
1217 | assert(sizeof(struct lttng_event_exclusion) == | |
1218 | sizeof(struct lttng_ust_event_exclusion)); | |
1219 | memcpy(ust_exclusion, exclusion, exclusion_alloc_size); | |
1220 | end: | |
1221 | return ust_exclusion; | |
1222 | } | |
1223 | ||
7cc9a73c JI |
1224 | /* |
1225 | * Set event exclusions on the tracer. | |
1226 | */ | |
1227 | static | |
1228 | int set_ust_event_exclusion(struct ust_app_event *ua_event, | |
1229 | struct ust_app *app) | |
1230 | { | |
1231 | int ret; | |
51755dc8 | 1232 | struct lttng_ust_event_exclusion *ust_exclusion = NULL; |
7cc9a73c JI |
1233 | |
1234 | health_code_update(); | |
1235 | ||
1236 | if (!ua_event->exclusion || !ua_event->exclusion->count) { | |
1237 | ret = 0; | |
1238 | goto error; | |
1239 | } | |
1240 | ||
51755dc8 JG |
1241 | ust_exclusion = create_ust_exclusion_from_exclusion( |
1242 | ua_event->exclusion); | |
1243 | if (!ust_exclusion) { | |
1244 | ret = -LTTNG_ERR_NOMEM; | |
1245 | goto error; | |
1246 | } | |
1247 | ret = ustctl_set_exclusion(app->sock, ust_exclusion, ua_event->obj); | |
7cc9a73c JI |
1248 | if (ret < 0) { |
1249 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
1250 | ERR("UST app event %s exclusions failed for app (pid: %d) " | |
1251 | "with ret %d", ua_event->attr.name, app->pid, ret); | |
1252 | } else { | |
1253 | /* | |
1254 | * This is normal behavior, an application can die during the | |
1255 | * creation process. Don't report an error so the execution can | |
1256 | * continue normally. | |
1257 | */ | |
1258 | ret = 0; | |
1259 | DBG3("UST app event exclusion failed. Application is dead."); | |
1260 | } | |
1261 | goto error; | |
1262 | } | |
1263 | ||
1264 | DBG2("UST exclusion set successfully for event %s", ua_event->name); | |
1265 | ||
1266 | error: | |
1267 | health_code_update(); | |
51755dc8 | 1268 | free(ust_exclusion); |
7cc9a73c JI |
1269 | return ret; |
1270 | } | |
1271 | ||
9730260e DG |
1272 | /* |
1273 | * Disable the specified event on to UST tracer for the UST session. | |
1274 | */ | |
1275 | static int disable_ust_event(struct ust_app *app, | |
1276 | struct ust_app_session *ua_sess, struct ust_app_event *ua_event) | |
1277 | { | |
1278 | int ret; | |
1279 | ||
840cb59c | 1280 | health_code_update(); |
86acf0da | 1281 | |
852d0037 | 1282 | ret = ustctl_disable(app->sock, ua_event->obj); |
9730260e | 1283 | if (ret < 0) { |
ffe60014 DG |
1284 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
1285 | ERR("UST app event %s disable failed for app (pid: %d) " | |
1286 | "and session handle %d with ret %d", | |
1287 | ua_event->attr.name, app->pid, ua_sess->handle, ret); | |
1288 | } else { | |
3757b385 DG |
1289 | /* |
1290 | * This is normal behavior, an application can die during the | |
1291 | * creation process. Don't report an error so the execution can | |
1292 | * continue normally. | |
1293 | */ | |
1294 | ret = 0; | |
ffe60014 DG |
1295 | DBG3("UST app disable event failed. Application is dead."); |
1296 | } | |
9730260e DG |
1297 | goto error; |
1298 | } | |
1299 | ||
1300 | DBG2("UST app event %s disabled successfully for app (pid: %d)", | |
852d0037 | 1301 | ua_event->attr.name, app->pid); |
9730260e DG |
1302 | |
1303 | error: | |
840cb59c | 1304 | health_code_update(); |
9730260e DG |
1305 | return ret; |
1306 | } | |
1307 | ||
78f0bacd DG |
1308 | /* |
1309 | * Disable the specified channel on to UST tracer for the UST session. | |
1310 | */ | |
1311 | static int disable_ust_channel(struct ust_app *app, | |
1312 | struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan) | |
1313 | { | |
1314 | int ret; | |
1315 | ||
840cb59c | 1316 | health_code_update(); |
86acf0da | 1317 | |
852d0037 | 1318 | ret = ustctl_disable(app->sock, ua_chan->obj); |
78f0bacd | 1319 | if (ret < 0) { |
ffe60014 DG |
1320 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
1321 | ERR("UST app channel %s disable failed for app (pid: %d) " | |
1322 | "and session handle %d with ret %d", | |
1323 | ua_chan->name, app->pid, ua_sess->handle, ret); | |
1324 | } else { | |
3757b385 DG |
1325 | /* |
1326 | * This is normal behavior, an application can die during the | |
1327 | * creation process. Don't report an error so the execution can | |
1328 | * continue normally. | |
1329 | */ | |
1330 | ret = 0; | |
ffe60014 DG |
1331 | DBG3("UST app disable channel failed. Application is dead."); |
1332 | } | |
78f0bacd DG |
1333 | goto error; |
1334 | } | |
1335 | ||
78f0bacd | 1336 | DBG2("UST app channel %s disabled successfully for app (pid: %d)", |
852d0037 | 1337 | ua_chan->name, app->pid); |
78f0bacd DG |
1338 | |
1339 | error: | |
840cb59c | 1340 | health_code_update(); |
78f0bacd DG |
1341 | return ret; |
1342 | } | |
1343 | ||
1344 | /* | |
1345 | * Enable the specified channel on to UST tracer for the UST session. | |
1346 | */ | |
1347 | static int enable_ust_channel(struct ust_app *app, | |
1348 | struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan) | |
1349 | { | |
1350 | int ret; | |
1351 | ||
840cb59c | 1352 | health_code_update(); |
86acf0da | 1353 | |
852d0037 | 1354 | ret = ustctl_enable(app->sock, ua_chan->obj); |
78f0bacd | 1355 | if (ret < 0) { |
ffe60014 DG |
1356 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
1357 | ERR("UST app channel %s enable failed for app (pid: %d) " | |
1358 | "and session handle %d with ret %d", | |
1359 | ua_chan->name, app->pid, ua_sess->handle, ret); | |
1360 | } else { | |
3757b385 DG |
1361 | /* |
1362 | * This is normal behavior, an application can die during the | |
1363 | * creation process. Don't report an error so the execution can | |
1364 | * continue normally. | |
1365 | */ | |
1366 | ret = 0; | |
ffe60014 DG |
1367 | DBG3("UST app enable channel failed. Application is dead."); |
1368 | } | |
78f0bacd DG |
1369 | goto error; |
1370 | } | |
1371 | ||
1372 | ua_chan->enabled = 1; | |
1373 | ||
1374 | DBG2("UST app channel %s enabled successfully for app (pid: %d)", | |
852d0037 | 1375 | ua_chan->name, app->pid); |
78f0bacd DG |
1376 | |
1377 | error: | |
840cb59c | 1378 | health_code_update(); |
78f0bacd DG |
1379 | return ret; |
1380 | } | |
1381 | ||
edb67388 DG |
1382 | /* |
1383 | * Enable the specified event on to UST tracer for the UST session. | |
1384 | */ | |
1385 | static int enable_ust_event(struct ust_app *app, | |
1386 | struct ust_app_session *ua_sess, struct ust_app_event *ua_event) | |
1387 | { | |
1388 | int ret; | |
1389 | ||
840cb59c | 1390 | health_code_update(); |
86acf0da | 1391 | |
852d0037 | 1392 | ret = ustctl_enable(app->sock, ua_event->obj); |
edb67388 | 1393 | if (ret < 0) { |
ffe60014 DG |
1394 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
1395 | ERR("UST app event %s enable failed for app (pid: %d) " | |
1396 | "and session handle %d with ret %d", | |
1397 | ua_event->attr.name, app->pid, ua_sess->handle, ret); | |
1398 | } else { | |
3757b385 DG |
1399 | /* |
1400 | * This is normal behavior, an application can die during the | |
1401 | * creation process. Don't report an error so the execution can | |
1402 | * continue normally. | |
1403 | */ | |
1404 | ret = 0; | |
ffe60014 DG |
1405 | DBG3("UST app enable event failed. Application is dead."); |
1406 | } | |
edb67388 DG |
1407 | goto error; |
1408 | } | |
1409 | ||
1410 | DBG2("UST app event %s enabled successfully for app (pid: %d)", | |
852d0037 | 1411 | ua_event->attr.name, app->pid); |
edb67388 DG |
1412 | |
1413 | error: | |
840cb59c | 1414 | health_code_update(); |
edb67388 DG |
1415 | return ret; |
1416 | } | |
1417 | ||
099e26bd | 1418 | /* |
7972aab2 | 1419 | * Send channel and stream buffer to application. |
4f3ab6ee | 1420 | * |
ffe60014 | 1421 | * Return 0 on success. On error, a negative value is returned. |
4f3ab6ee | 1422 | */ |
7972aab2 DG |
1423 | static int send_channel_pid_to_ust(struct ust_app *app, |
1424 | struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan) | |
4f3ab6ee DG |
1425 | { |
1426 | int ret; | |
ffe60014 | 1427 | struct ust_app_stream *stream, *stmp; |
4f3ab6ee DG |
1428 | |
1429 | assert(app); | |
ffe60014 | 1430 | assert(ua_sess); |
4f3ab6ee | 1431 | assert(ua_chan); |
4f3ab6ee | 1432 | |
840cb59c | 1433 | health_code_update(); |
4f3ab6ee | 1434 | |
7972aab2 DG |
1435 | DBG("UST app sending channel %s to UST app sock %d", ua_chan->name, |
1436 | app->sock); | |
86acf0da | 1437 | |
ffe60014 DG |
1438 | /* Send channel to the application. */ |
1439 | ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan); | |
a7169585 MD |
1440 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
1441 | ret = -ENOTCONN; /* Caused by app exiting. */ | |
1442 | goto error; | |
1443 | } else if (ret < 0) { | |
b551a063 DG |
1444 | goto error; |
1445 | } | |
1446 | ||
d88aee68 DG |
1447 | health_code_update(); |
1448 | ||
ffe60014 DG |
1449 | /* Send all streams to application. */ |
1450 | cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) { | |
1451 | ret = ust_consumer_send_stream_to_ust(app, ua_chan, stream); | |
a7169585 MD |
1452 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
1453 | ret = -ENOTCONN; /* Caused by app exiting. */ | |
1454 | goto error; | |
1455 | } else if (ret < 0) { | |
ffe60014 DG |
1456 | goto error; |
1457 | } | |
1458 | /* We don't need the stream anymore once sent to the tracer. */ | |
1459 | cds_list_del(&stream->list); | |
1460 | delete_ust_app_stream(-1, stream); | |
1461 | } | |
ffe60014 DG |
1462 | /* Flag the channel that it is sent to the application. */ |
1463 | ua_chan->is_sent = 1; | |
ffe60014 | 1464 | |
b551a063 | 1465 | error: |
840cb59c | 1466 | health_code_update(); |
b551a063 DG |
1467 | return ret; |
1468 | } | |
1469 | ||
91d76f53 | 1470 | /* |
5b4a0ec0 | 1471 | * Create the specified event onto the UST tracer for a UST session. |
d0b96690 DG |
1472 | * |
1473 | * Should be called with session mutex held. | |
91d76f53 | 1474 | */ |
edb67388 DG |
1475 | static |
1476 | int create_ust_event(struct ust_app *app, struct ust_app_session *ua_sess, | |
1477 | struct ust_app_channel *ua_chan, struct ust_app_event *ua_event) | |
91d76f53 | 1478 | { |
5b4a0ec0 | 1479 | int ret = 0; |
284d8f55 | 1480 | |
840cb59c | 1481 | health_code_update(); |
86acf0da | 1482 | |
5b4a0ec0 | 1483 | /* Create UST event on tracer */ |
852d0037 | 1484 | ret = ustctl_create_event(app->sock, &ua_event->attr, ua_chan->obj, |
5b4a0ec0 DG |
1485 | &ua_event->obj); |
1486 | if (ret < 0) { | |
ffe60014 DG |
1487 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { |
1488 | ERR("Error ustctl create event %s for app pid: %d with ret %d", | |
1489 | ua_event->attr.name, app->pid, ret); | |
1490 | } else { | |
3757b385 DG |
1491 | /* |
1492 | * This is normal behavior, an application can die during the | |
1493 | * creation process. Don't report an error so the execution can | |
1494 | * continue normally. | |
1495 | */ | |
1496 | ret = 0; | |
ffe60014 DG |
1497 | DBG3("UST app create event failed. Application is dead."); |
1498 | } | |
5b4a0ec0 | 1499 | goto error; |
91d76f53 | 1500 | } |
f6a9efaa | 1501 | |
5b4a0ec0 | 1502 | ua_event->handle = ua_event->obj->handle; |
284d8f55 | 1503 | |
5b4a0ec0 | 1504 | DBG2("UST app event %s created successfully for pid:%d", |
852d0037 | 1505 | ua_event->attr.name, app->pid); |
f6a9efaa | 1506 | |
840cb59c | 1507 | health_code_update(); |
86acf0da | 1508 | |
025faf73 DG |
1509 | /* Set filter if one is present. */ |
1510 | if (ua_event->filter) { | |
1511 | ret = set_ust_event_filter(ua_event, app); | |
1512 | if (ret < 0) { | |
1513 | goto error; | |
1514 | } | |
1515 | } | |
1516 | ||
7cc9a73c JI |
1517 | /* Set exclusions for the event */ |
1518 | if (ua_event->exclusion) { | |
1519 | ret = set_ust_event_exclusion(ua_event, app); | |
1520 | if (ret < 0) { | |
1521 | goto error; | |
1522 | } | |
1523 | } | |
1524 | ||
8535a6d9 | 1525 | /* If event not enabled, disable it on the tracer */ |
40113787 MD |
1526 | if (ua_event->enabled) { |
1527 | /* | |
1528 | * We now need to explicitly enable the event, since it | |
1529 | * is now disabled at creation. | |
1530 | */ | |
1531 | ret = enable_ust_event(app, ua_sess, ua_event); | |
1532 | if (ret < 0) { | |
1533 | /* | |
1534 | * If we hit an EPERM, something is wrong with our enable call. If | |
1535 | * we get an EEXIST, there is a problem on the tracer side since we | |
1536 | * just created it. | |
1537 | */ | |
1538 | switch (ret) { | |
1539 | case -LTTNG_UST_ERR_PERM: | |
1540 | /* Code flow problem */ | |
1541 | assert(0); | |
1542 | case -LTTNG_UST_ERR_EXIST: | |
1543 | /* It's OK for our use case. */ | |
1544 | ret = 0; | |
1545 | break; | |
1546 | default: | |
1547 | break; | |
1548 | } | |
1549 | goto error; | |
1550 | } | |
8535a6d9 DG |
1551 | } |
1552 | ||
5b4a0ec0 | 1553 | error: |
840cb59c | 1554 | health_code_update(); |
5b4a0ec0 | 1555 | return ret; |
91d76f53 | 1556 | } |
48842b30 | 1557 | |
5b4a0ec0 DG |
1558 | /* |
1559 | * Copy data between an UST app event and a LTT event. | |
1560 | */ | |
421cb601 | 1561 | static void shadow_copy_event(struct ust_app_event *ua_event, |
48842b30 DG |
1562 | struct ltt_ust_event *uevent) |
1563 | { | |
b4ffad32 JI |
1564 | size_t exclusion_alloc_size; |
1565 | ||
48842b30 DG |
1566 | strncpy(ua_event->name, uevent->attr.name, sizeof(ua_event->name)); |
1567 | ua_event->name[sizeof(ua_event->name) - 1] = '\0'; | |
1568 | ||
fc34caaa DG |
1569 | ua_event->enabled = uevent->enabled; |
1570 | ||
5b4a0ec0 DG |
1571 | /* Copy event attributes */ |
1572 | memcpy(&ua_event->attr, &uevent->attr, sizeof(ua_event->attr)); | |
1573 | ||
53a80697 MD |
1574 | /* Copy filter bytecode */ |
1575 | if (uevent->filter) { | |
51755dc8 | 1576 | ua_event->filter = copy_filter_bytecode(uevent->filter); |
025faf73 | 1577 | /* Filter might be NULL here in case of ENONEM. */ |
53a80697 | 1578 | } |
b4ffad32 JI |
1579 | |
1580 | /* Copy exclusion data */ | |
1581 | if (uevent->exclusion) { | |
51755dc8 | 1582 | exclusion_alloc_size = sizeof(struct lttng_event_exclusion) + |
b4ffad32 JI |
1583 | LTTNG_UST_SYM_NAME_LEN * uevent->exclusion->count; |
1584 | ua_event->exclusion = zmalloc(exclusion_alloc_size); | |
5f8df26c JI |
1585 | if (ua_event->exclusion == NULL) { |
1586 | PERROR("malloc"); | |
1587 | } else { | |
1588 | memcpy(ua_event->exclusion, uevent->exclusion, | |
1589 | exclusion_alloc_size); | |
b4ffad32 JI |
1590 | } |
1591 | } | |
48842b30 DG |
1592 | } |
1593 | ||
5b4a0ec0 DG |
1594 | /* |
1595 | * Copy data between an UST app channel and a LTT channel. | |
1596 | */ | |
421cb601 | 1597 | static void shadow_copy_channel(struct ust_app_channel *ua_chan, |
48842b30 DG |
1598 | struct ltt_ust_channel *uchan) |
1599 | { | |
bec39940 | 1600 | struct lttng_ht_iter iter; |
48842b30 | 1601 | struct ltt_ust_event *uevent; |
55cc08a6 | 1602 | struct ltt_ust_context *uctx; |
48842b30 | 1603 | struct ust_app_event *ua_event; |
55cc08a6 | 1604 | struct ust_app_ctx *ua_ctx; |
48842b30 | 1605 | |
fc34caaa | 1606 | DBG2("UST app shadow copy of channel %s started", ua_chan->name); |
48842b30 DG |
1607 | |
1608 | strncpy(ua_chan->name, uchan->name, sizeof(ua_chan->name)); | |
1609 | ua_chan->name[sizeof(ua_chan->name) - 1] = '\0'; | |
ffe60014 | 1610 | |
1624d5b7 JD |
1611 | ua_chan->tracefile_size = uchan->tracefile_size; |
1612 | ua_chan->tracefile_count = uchan->tracefile_count; | |
1613 | ||
ffe60014 DG |
1614 | /* Copy event attributes since the layout is different. */ |
1615 | ua_chan->attr.subbuf_size = uchan->attr.subbuf_size; | |
1616 | ua_chan->attr.num_subbuf = uchan->attr.num_subbuf; | |
1617 | ua_chan->attr.overwrite = uchan->attr.overwrite; | |
1618 | ua_chan->attr.switch_timer_interval = uchan->attr.switch_timer_interval; | |
1619 | ua_chan->attr.read_timer_interval = uchan->attr.read_timer_interval; | |
1620 | ua_chan->attr.output = uchan->attr.output; | |
1621 | /* | |
1622 | * Note that the attribute channel type is not set since the channel on the | |
1623 | * tracing registry side does not have this information. | |
1624 | */ | |
48842b30 | 1625 | |
fc34caaa | 1626 | ua_chan->enabled = uchan->enabled; |
7972aab2 | 1627 | ua_chan->tracing_channel_id = uchan->id; |
fc34caaa | 1628 | |
31746f93 | 1629 | cds_list_for_each_entry(uctx, &uchan->ctx_list, list) { |
55cc08a6 DG |
1630 | ua_ctx = alloc_ust_app_ctx(&uctx->ctx); |
1631 | if (ua_ctx == NULL) { | |
1632 | continue; | |
1633 | } | |
bec39940 DG |
1634 | lttng_ht_node_init_ulong(&ua_ctx->node, |
1635 | (unsigned long) ua_ctx->ctx.ctx); | |
aa3514e9 | 1636 | lttng_ht_add_ulong(ua_chan->ctx, &ua_ctx->node); |
31746f93 | 1637 | cds_list_add_tail(&ua_ctx->list, &ua_chan->ctx_list); |
55cc08a6 | 1638 | } |
48842b30 | 1639 | |
421cb601 | 1640 | /* Copy all events from ltt ust channel to ust app channel */ |
bec39940 | 1641 | cds_lfht_for_each_entry(uchan->events->ht, &iter.iter, uevent, node.node) { |
18eace3b | 1642 | ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name, |
39c5a3a7 | 1643 | uevent->filter, uevent->attr.loglevel, uevent->exclusion); |
18eace3b | 1644 | if (ua_event == NULL) { |
421cb601 | 1645 | DBG2("UST event %s not found on shadow copy channel", |
48842b30 | 1646 | uevent->attr.name); |
284d8f55 | 1647 | ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr); |
48842b30 | 1648 | if (ua_event == NULL) { |
5b4a0ec0 | 1649 | continue; |
48842b30 | 1650 | } |
421cb601 | 1651 | shadow_copy_event(ua_event, uevent); |
d0b96690 | 1652 | add_unique_ust_app_event(ua_chan, ua_event); |
48842b30 | 1653 | } |
48842b30 DG |
1654 | } |
1655 | ||
fc34caaa | 1656 | DBG3("UST app shadow copy of channel %s done", ua_chan->name); |
48842b30 DG |
1657 | } |
1658 | ||
5b4a0ec0 DG |
1659 | /* |
1660 | * Copy data between a UST app session and a regular LTT session. | |
1661 | */ | |
421cb601 | 1662 | static void shadow_copy_session(struct ust_app_session *ua_sess, |
bec39940 | 1663 | struct ltt_ust_session *usess, struct ust_app *app) |
48842b30 | 1664 | { |
bec39940 DG |
1665 | struct lttng_ht_node_str *ua_chan_node; |
1666 | struct lttng_ht_iter iter; | |
48842b30 DG |
1667 | struct ltt_ust_channel *uchan; |
1668 | struct ust_app_channel *ua_chan; | |
477d7741 MD |
1669 | time_t rawtime; |
1670 | struct tm *timeinfo; | |
1671 | char datetime[16]; | |
1672 | int ret; | |
d7ba1388 | 1673 | char tmp_shm_path[PATH_MAX]; |
477d7741 MD |
1674 | |
1675 | /* Get date and time for unique app path */ | |
1676 | time(&rawtime); | |
1677 | timeinfo = localtime(&rawtime); | |
1678 | strftime(datetime, sizeof(datetime), "%Y%m%d-%H%M%S", timeinfo); | |
48842b30 | 1679 | |
421cb601 | 1680 | DBG2("Shadow copy of session handle %d", ua_sess->handle); |
48842b30 | 1681 | |
7972aab2 DG |
1682 | ua_sess->tracing_id = usess->id; |
1683 | ua_sess->id = get_next_session_id(); | |
1684 | ua_sess->uid = app->uid; | |
1685 | ua_sess->gid = app->gid; | |
1686 | ua_sess->euid = usess->uid; | |
1687 | ua_sess->egid = usess->gid; | |
1688 | ua_sess->buffer_type = usess->buffer_type; | |
1689 | ua_sess->bits_per_long = app->bits_per_long; | |
6addfa37 | 1690 | |
7972aab2 | 1691 | /* There is only one consumer object per session possible. */ |
6addfa37 | 1692 | consumer_output_get(usess->consumer); |
7972aab2 | 1693 | ua_sess->consumer = usess->consumer; |
6addfa37 | 1694 | |
2bba9e53 | 1695 | ua_sess->output_traces = usess->output_traces; |
ecc48a90 | 1696 | ua_sess->live_timer_interval = usess->live_timer_interval; |
84ad93e8 DG |
1697 | copy_channel_attr_to_ustctl(&ua_sess->metadata_attr, |
1698 | &usess->metadata_attr); | |
7972aab2 DG |
1699 | |
1700 | switch (ua_sess->buffer_type) { | |
1701 | case LTTNG_BUFFER_PER_PID: | |
1702 | ret = snprintf(ua_sess->path, sizeof(ua_sess->path), | |
dec56f6c | 1703 | DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s", app->name, app->pid, |
7972aab2 DG |
1704 | datetime); |
1705 | break; | |
1706 | case LTTNG_BUFFER_PER_UID: | |
1707 | ret = snprintf(ua_sess->path, sizeof(ua_sess->path), | |
1708 | DEFAULT_UST_TRACE_UID_PATH, ua_sess->uid, app->bits_per_long); | |
1709 | break; | |
1710 | default: | |
1711 | assert(0); | |
1712 | goto error; | |
1713 | } | |
477d7741 MD |
1714 | if (ret < 0) { |
1715 | PERROR("asprintf UST shadow copy session"); | |
477d7741 | 1716 | assert(0); |
7972aab2 | 1717 | goto error; |
477d7741 MD |
1718 | } |
1719 | ||
3d071855 MD |
1720 | strncpy(ua_sess->root_shm_path, usess->root_shm_path, |
1721 | sizeof(ua_sess->root_shm_path)); | |
1722 | ua_sess->root_shm_path[sizeof(ua_sess->root_shm_path) - 1] = '\0'; | |
d7ba1388 MD |
1723 | strncpy(ua_sess->shm_path, usess->shm_path, |
1724 | sizeof(ua_sess->shm_path)); | |
1725 | ua_sess->shm_path[sizeof(ua_sess->shm_path) - 1] = '\0'; | |
1726 | if (ua_sess->shm_path[0]) { | |
1727 | switch (ua_sess->buffer_type) { | |
1728 | case LTTNG_BUFFER_PER_PID: | |
1729 | ret = snprintf(tmp_shm_path, sizeof(tmp_shm_path), | |
1730 | DEFAULT_UST_TRACE_PID_PATH "/%s-%d-%s", | |
1731 | app->name, app->pid, datetime); | |
1732 | break; | |
1733 | case LTTNG_BUFFER_PER_UID: | |
1734 | ret = snprintf(tmp_shm_path, sizeof(tmp_shm_path), | |
1735 | DEFAULT_UST_TRACE_UID_PATH, | |
1736 | app->uid, app->bits_per_long); | |
1737 | break; | |
1738 | default: | |
1739 | assert(0); | |
1740 | goto error; | |
1741 | } | |
1742 | if (ret < 0) { | |
1743 | PERROR("sprintf UST shadow copy session"); | |
1744 | assert(0); | |
1745 | goto error; | |
1746 | } | |
1747 | strncat(ua_sess->shm_path, tmp_shm_path, | |
1748 | sizeof(ua_sess->shm_path) - strlen(ua_sess->shm_path) - 1); | |
1749 | ua_sess->shm_path[sizeof(ua_sess->shm_path) - 1] = '\0'; | |
1750 | } | |
1751 | ||
48842b30 | 1752 | /* Iterate over all channels in global domain. */ |
bec39940 DG |
1753 | cds_lfht_for_each_entry(usess->domain_global.channels->ht, &iter.iter, |
1754 | uchan, node.node) { | |
1755 | struct lttng_ht_iter uiter; | |
ba767faf | 1756 | |
bec39940 DG |
1757 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &uiter); |
1758 | ua_chan_node = lttng_ht_iter_get_node_str(&uiter); | |
5b4a0ec0 | 1759 | if (ua_chan_node != NULL) { |
fc34caaa | 1760 | /* Session exist. Contiuing. */ |
5b4a0ec0 DG |
1761 | continue; |
1762 | } | |
421cb601 | 1763 | |
5b4a0ec0 DG |
1764 | DBG2("Channel %s not found on shadow session copy, creating it", |
1765 | uchan->name); | |
d0b96690 | 1766 | ua_chan = alloc_ust_app_channel(uchan->name, ua_sess, &uchan->attr); |
5b4a0ec0 | 1767 | if (ua_chan == NULL) { |
fc34caaa | 1768 | /* malloc failed FIXME: Might want to do handle ENOMEM .. */ |
5b4a0ec0 | 1769 | continue; |
48842b30 | 1770 | } |
5b4a0ec0 | 1771 | shadow_copy_channel(ua_chan, uchan); |
ffe60014 DG |
1772 | /* |
1773 | * The concept of metadata channel does not exist on the tracing | |
1774 | * registry side of the session daemon so this can only be a per CPU | |
1775 | * channel and not metadata. | |
1776 | */ | |
1777 | ua_chan->attr.type = LTTNG_UST_CHAN_PER_CPU; | |
1778 | ||
bec39940 | 1779 | lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node); |
48842b30 | 1780 | } |
6addfa37 | 1781 | return; |
7972aab2 DG |
1782 | |
1783 | error: | |
6addfa37 | 1784 | consumer_output_put(ua_sess->consumer); |
48842b30 DG |
1785 | } |
1786 | ||
78f0bacd DG |
1787 | /* |
1788 | * Lookup sesison wrapper. | |
1789 | */ | |
84cd17c6 MD |
1790 | static |
1791 | void __lookup_session_by_app(struct ltt_ust_session *usess, | |
bec39940 | 1792 | struct ust_app *app, struct lttng_ht_iter *iter) |
84cd17c6 MD |
1793 | { |
1794 | /* Get right UST app session from app */ | |
d9bf3ca4 | 1795 | lttng_ht_lookup(app->sessions, &usess->id, iter); |
84cd17c6 MD |
1796 | } |
1797 | ||
421cb601 DG |
1798 | /* |
1799 | * Return ust app session from the app session hashtable using the UST session | |
a991f516 | 1800 | * id. |
421cb601 | 1801 | */ |
48842b30 DG |
1802 | static struct ust_app_session *lookup_session_by_app( |
1803 | struct ltt_ust_session *usess, struct ust_app *app) | |
1804 | { | |
bec39940 | 1805 | struct lttng_ht_iter iter; |
d9bf3ca4 | 1806 | struct lttng_ht_node_u64 *node; |
48842b30 | 1807 | |
84cd17c6 | 1808 | __lookup_session_by_app(usess, app, &iter); |
d9bf3ca4 | 1809 | node = lttng_ht_iter_get_node_u64(&iter); |
48842b30 DG |
1810 | if (node == NULL) { |
1811 | goto error; | |
1812 | } | |
1813 | ||
1814 | return caa_container_of(node, struct ust_app_session, node); | |
1815 | ||
1816 | error: | |
1817 | return NULL; | |
1818 | } | |
1819 | ||
7972aab2 DG |
1820 | /* |
1821 | * Setup buffer registry per PID for the given session and application. If none | |
1822 | * is found, a new one is created, added to the global registry and | |
1823 | * initialized. If regp is valid, it's set with the newly created object. | |
1824 | * | |
1825 | * Return 0 on success or else a negative value. | |
1826 | */ | |
1827 | static int setup_buffer_reg_pid(struct ust_app_session *ua_sess, | |
1828 | struct ust_app *app, struct buffer_reg_pid **regp) | |
1829 | { | |
1830 | int ret = 0; | |
1831 | struct buffer_reg_pid *reg_pid; | |
1832 | ||
1833 | assert(ua_sess); | |
1834 | assert(app); | |
1835 | ||
1836 | rcu_read_lock(); | |
1837 | ||
1838 | reg_pid = buffer_reg_pid_find(ua_sess->id); | |
1839 | if (!reg_pid) { | |
1840 | /* | |
1841 | * This is the create channel path meaning that if there is NO | |
1842 | * registry available, we have to create one for this session. | |
1843 | */ | |
d7ba1388 | 1844 | ret = buffer_reg_pid_create(ua_sess->id, ®_pid, |
3d071855 | 1845 | ua_sess->root_shm_path, ua_sess->shm_path); |
7972aab2 DG |
1846 | if (ret < 0) { |
1847 | goto error; | |
1848 | } | |
7972aab2 DG |
1849 | } else { |
1850 | goto end; | |
1851 | } | |
1852 | ||
1853 | /* Initialize registry. */ | |
1854 | ret = ust_registry_session_init(®_pid->registry->reg.ust, app, | |
1855 | app->bits_per_long, app->uint8_t_alignment, | |
1856 | app->uint16_t_alignment, app->uint32_t_alignment, | |
af6142cf MD |
1857 | app->uint64_t_alignment, app->long_alignment, |
1858 | app->byte_order, app->version.major, | |
3d071855 MD |
1859 | app->version.minor, reg_pid->root_shm_path, |
1860 | reg_pid->shm_path, | |
d7ba1388 | 1861 | ua_sess->euid, ua_sess->egid); |
7972aab2 | 1862 | if (ret < 0) { |
286c991a MD |
1863 | /* |
1864 | * reg_pid->registry->reg.ust is NULL upon error, so we need to | |
1865 | * destroy the buffer registry, because it is always expected | |
1866 | * that if the buffer registry can be found, its ust registry is | |
1867 | * non-NULL. | |
1868 | */ | |
1869 | buffer_reg_pid_destroy(reg_pid); | |
7972aab2 DG |
1870 | goto error; |
1871 | } | |
1872 | ||
286c991a MD |
1873 | buffer_reg_pid_add(reg_pid); |
1874 | ||
7972aab2 DG |
1875 | DBG3("UST app buffer registry per PID created successfully"); |
1876 | ||
1877 | end: | |
1878 | if (regp) { | |
1879 | *regp = reg_pid; | |
1880 | } | |
1881 | error: | |
1882 | rcu_read_unlock(); | |
1883 | return ret; | |
1884 | } | |
1885 | ||
1886 | /* | |
1887 | * Setup buffer registry per UID for the given session and application. If none | |
1888 | * is found, a new one is created, added to the global registry and | |
1889 | * initialized. If regp is valid, it's set with the newly created object. | |
1890 | * | |
1891 | * Return 0 on success or else a negative value. | |
1892 | */ | |
1893 | static int setup_buffer_reg_uid(struct ltt_ust_session *usess, | |
d7ba1388 | 1894 | struct ust_app_session *ua_sess, |
7972aab2 DG |
1895 | struct ust_app *app, struct buffer_reg_uid **regp) |
1896 | { | |
1897 | int ret = 0; | |
1898 | struct buffer_reg_uid *reg_uid; | |
1899 | ||
1900 | assert(usess); | |
1901 | assert(app); | |
1902 | ||
1903 | rcu_read_lock(); | |
1904 | ||
1905 | reg_uid = buffer_reg_uid_find(usess->id, app->bits_per_long, app->uid); | |
1906 | if (!reg_uid) { | |
1907 | /* | |
1908 | * This is the create channel path meaning that if there is NO | |
1909 | * registry available, we have to create one for this session. | |
1910 | */ | |
1911 | ret = buffer_reg_uid_create(usess->id, app->bits_per_long, app->uid, | |
3d071855 MD |
1912 | LTTNG_DOMAIN_UST, ®_uid, |
1913 | ua_sess->root_shm_path, ua_sess->shm_path); | |
7972aab2 DG |
1914 | if (ret < 0) { |
1915 | goto error; | |
1916 | } | |
7972aab2 DG |
1917 | } else { |
1918 | goto end; | |
1919 | } | |
1920 | ||
1921 | /* Initialize registry. */ | |
af6142cf | 1922 | ret = ust_registry_session_init(®_uid->registry->reg.ust, NULL, |
7972aab2 DG |
1923 | app->bits_per_long, app->uint8_t_alignment, |
1924 | app->uint16_t_alignment, app->uint32_t_alignment, | |
af6142cf MD |
1925 | app->uint64_t_alignment, app->long_alignment, |
1926 | app->byte_order, app->version.major, | |
3d071855 MD |
1927 | app->version.minor, reg_uid->root_shm_path, |
1928 | reg_uid->shm_path, usess->uid, usess->gid); | |
7972aab2 | 1929 | if (ret < 0) { |
286c991a MD |
1930 | /* |
1931 | * reg_uid->registry->reg.ust is NULL upon error, so we need to | |
1932 | * destroy the buffer registry, because it is always expected | |
1933 | * that if the buffer registry can be found, its ust registry is | |
1934 | * non-NULL. | |
1935 | */ | |
1936 | buffer_reg_uid_destroy(reg_uid, NULL); | |
7972aab2 DG |
1937 | goto error; |
1938 | } | |
1939 | /* Add node to teardown list of the session. */ | |
1940 | cds_list_add(®_uid->lnode, &usess->buffer_reg_uid_list); | |
1941 | ||
286c991a | 1942 | buffer_reg_uid_add(reg_uid); |
7972aab2 | 1943 | |
286c991a | 1944 | DBG3("UST app buffer registry per UID created successfully"); |
7972aab2 DG |
1945 | end: |
1946 | if (regp) { | |
1947 | *regp = reg_uid; | |
1948 | } | |
1949 | error: | |
1950 | rcu_read_unlock(); | |
1951 | return ret; | |
1952 | } | |
1953 | ||
421cb601 | 1954 | /* |
3d8ca23b | 1955 | * Create a session on the tracer side for the given app. |
421cb601 | 1956 | * |
3d8ca23b DG |
1957 | * On success, ua_sess_ptr is populated with the session pointer or else left |
1958 | * untouched. If the session was created, is_created is set to 1. On error, | |
1959 | * it's left untouched. Note that ua_sess_ptr is mandatory but is_created can | |
1960 | * be NULL. | |
1961 | * | |
1962 | * Returns 0 on success or else a negative code which is either -ENOMEM or | |
1963 | * -ENOTCONN which is the default code if the ustctl_create_session fails. | |
421cb601 | 1964 | */ |
3d8ca23b DG |
1965 | static int create_ust_app_session(struct ltt_ust_session *usess, |
1966 | struct ust_app *app, struct ust_app_session **ua_sess_ptr, | |
1967 | int *is_created) | |
421cb601 | 1968 | { |
3d8ca23b | 1969 | int ret, created = 0; |
421cb601 DG |
1970 | struct ust_app_session *ua_sess; |
1971 | ||
3d8ca23b DG |
1972 | assert(usess); |
1973 | assert(app); | |
1974 | assert(ua_sess_ptr); | |
1975 | ||
840cb59c | 1976 | health_code_update(); |
86acf0da | 1977 | |
421cb601 DG |
1978 | ua_sess = lookup_session_by_app(usess, app); |
1979 | if (ua_sess == NULL) { | |
d9bf3ca4 | 1980 | DBG2("UST app pid: %d session id %" PRIu64 " not found, creating it", |
852d0037 | 1981 | app->pid, usess->id); |
d0b96690 | 1982 | ua_sess = alloc_ust_app_session(app); |
421cb601 DG |
1983 | if (ua_sess == NULL) { |
1984 | /* Only malloc can failed so something is really wrong */ | |
3d8ca23b DG |
1985 | ret = -ENOMEM; |
1986 | goto error; | |
421cb601 | 1987 | } |
477d7741 | 1988 | shadow_copy_session(ua_sess, usess, app); |
3d8ca23b | 1989 | created = 1; |
421cb601 DG |
1990 | } |
1991 | ||
7972aab2 DG |
1992 | switch (usess->buffer_type) { |
1993 | case LTTNG_BUFFER_PER_PID: | |
1994 | /* Init local registry. */ | |
1995 | ret = setup_buffer_reg_pid(ua_sess, app, NULL); | |
421cb601 | 1996 | if (ret < 0) { |
e64207cf | 1997 | delete_ust_app_session(-1, ua_sess, app); |
7972aab2 DG |
1998 | goto error; |
1999 | } | |
2000 | break; | |
2001 | case LTTNG_BUFFER_PER_UID: | |
2002 | /* Look for a global registry. If none exists, create one. */ | |
d7ba1388 | 2003 | ret = setup_buffer_reg_uid(usess, ua_sess, app, NULL); |
7972aab2 | 2004 | if (ret < 0) { |
e64207cf | 2005 | delete_ust_app_session(-1, ua_sess, app); |
7972aab2 DG |
2006 | goto error; |
2007 | } | |
2008 | break; | |
2009 | default: | |
2010 | assert(0); | |
2011 | ret = -EINVAL; | |
2012 | goto error; | |
2013 | } | |
2014 | ||
2015 | health_code_update(); | |
2016 | ||
2017 | if (ua_sess->handle == -1) { | |
2018 | ret = ustctl_create_session(app->sock); | |
2019 | if (ret < 0) { | |
2020 | if (ret != -EPIPE && ret != -LTTNG_UST_ERR_EXITING) { | |
2021 | ERR("Creating session for app pid %d with ret %d", | |
ffe60014 DG |
2022 | app->pid, ret); |
2023 | } else { | |
2024 | DBG("UST app creating session failed. Application is dead"); | |
3757b385 DG |
2025 | /* |
2026 | * This is normal behavior, an application can die during the | |
2027 | * creation process. Don't report an error so the execution can | |
2028 | * continue normally. This will get flagged ENOTCONN and the | |
2029 | * caller will handle it. | |
2030 | */ | |
2031 | ret = 0; | |
ffe60014 | 2032 | } |
d0b96690 | 2033 | delete_ust_app_session(-1, ua_sess, app); |
3d8ca23b DG |
2034 | if (ret != -ENOMEM) { |
2035 | /* | |
2036 | * Tracer is probably gone or got an internal error so let's | |
2037 | * behave like it will soon unregister or not usable. | |
2038 | */ | |
2039 | ret = -ENOTCONN; | |
2040 | } | |
2041 | goto error; | |
421cb601 DG |
2042 | } |
2043 | ||
7972aab2 DG |
2044 | ua_sess->handle = ret; |
2045 | ||
2046 | /* Add ust app session to app's HT */ | |
d9bf3ca4 MD |
2047 | lttng_ht_node_init_u64(&ua_sess->node, |
2048 | ua_sess->tracing_id); | |
2049 | lttng_ht_add_unique_u64(app->sessions, &ua_sess->node); | |
7972aab2 DG |
2050 | |
2051 | DBG2("UST app session created successfully with handle %d", ret); | |
2052 | } | |
2053 | ||
2054 | *ua_sess_ptr = ua_sess; | |
2055 | if (is_created) { | |
2056 | *is_created = created; | |
2057 | } | |
2058 | ||
2059 | /* Everything went well. */ | |
2060 | ret = 0; | |
2061 | ||
2062 | error: | |
2063 | health_code_update(); | |
2064 | return ret; | |
2065 | } | |
2066 | ||
6a6b2068 JG |
2067 | /* |
2068 | * Match function for a hash table lookup of ust_app_ctx. | |
2069 | * | |
2070 | * It matches an ust app context based on the context type and, in the case | |
2071 | * of perf counters, their name. | |
2072 | */ | |
2073 | static int ht_match_ust_app_ctx(struct cds_lfht_node *node, const void *_key) | |
2074 | { | |
2075 | struct ust_app_ctx *ctx; | |
2076 | const struct lttng_ust_context *key; | |
2077 | ||
2078 | assert(node); | |
2079 | assert(_key); | |
2080 | ||
2081 | ctx = caa_container_of(node, struct ust_app_ctx, node.node); | |
2082 | key = _key; | |
2083 | ||
2084 | /* Context type */ | |
2085 | if (ctx->ctx.ctx != key->ctx) { | |
2086 | goto no_match; | |
2087 | } | |
2088 | ||
2089 | /* Check the name in the case of perf thread counters. */ | |
2090 | if (key->ctx == LTTNG_UST_CONTEXT_PERF_THREAD_COUNTER) { | |
2091 | if (strncmp(key->u.perf_counter.name, | |
2092 | ctx->ctx.u.perf_counter.name, | |
2093 | sizeof(key->u.perf_counter.name))) { | |
2094 | goto no_match; | |
2095 | } | |
2096 | } | |
2097 | ||
2098 | /* Match. */ | |
2099 | return 1; | |
2100 | ||
2101 | no_match: | |
2102 | return 0; | |
2103 | } | |
2104 | ||
2105 | /* | |
2106 | * Lookup for an ust app context from an lttng_ust_context. | |
2107 | * | |
be184a0f | 2108 | * Must be called while holding RCU read side lock. |
6a6b2068 JG |
2109 | * Return an ust_app_ctx object or NULL on error. |
2110 | */ | |
2111 | static | |
2112 | struct ust_app_ctx *find_ust_app_context(struct lttng_ht *ht, | |
2113 | struct lttng_ust_context *uctx) | |
2114 | { | |
2115 | struct lttng_ht_iter iter; | |
2116 | struct lttng_ht_node_ulong *node; | |
2117 | struct ust_app_ctx *app_ctx = NULL; | |
2118 | ||
2119 | assert(uctx); | |
2120 | assert(ht); | |
2121 | ||
2122 | /* Lookup using the lttng_ust_context_type and a custom match fct. */ | |
2123 | cds_lfht_lookup(ht->ht, ht->hash_fct((void *) uctx->ctx, lttng_ht_seed), | |
2124 | ht_match_ust_app_ctx, uctx, &iter.iter); | |
2125 | node = lttng_ht_iter_get_node_ulong(&iter); | |
2126 | if (!node) { | |
2127 | goto end; | |
2128 | } | |
2129 | ||
2130 | app_ctx = caa_container_of(node, struct ust_app_ctx, node); | |
2131 | ||
2132 | end: | |
2133 | return app_ctx; | |
2134 | } | |
2135 | ||
7972aab2 DG |
2136 | /* |
2137 | * Create a context for the channel on the tracer. | |
2138 | * | |
2139 | * Called with UST app session lock held and a RCU read side lock. | |
2140 | */ | |
2141 | static | |
2142 | int create_ust_app_channel_context(struct ust_app_session *ua_sess, | |
2143 | struct ust_app_channel *ua_chan, struct lttng_ust_context *uctx, | |
2144 | struct ust_app *app) | |
2145 | { | |
2146 | int ret = 0; | |
7972aab2 DG |
2147 | struct ust_app_ctx *ua_ctx; |
2148 | ||
2149 | DBG2("UST app adding context to channel %s", ua_chan->name); | |
2150 | ||
6a6b2068 JG |
2151 | ua_ctx = find_ust_app_context(ua_chan->ctx, uctx); |
2152 | if (ua_ctx) { | |
7972aab2 DG |
2153 | ret = -EEXIST; |
2154 | goto error; | |
2155 | } | |
2156 | ||
2157 | ua_ctx = alloc_ust_app_ctx(uctx); | |
2158 | if (ua_ctx == NULL) { | |
2159 | /* malloc failed */ | |
2160 | ret = -1; | |
2161 | goto error; | |
2162 | } | |
2163 | ||
2164 | lttng_ht_node_init_ulong(&ua_ctx->node, (unsigned long) ua_ctx->ctx.ctx); | |
aa3514e9 | 2165 | lttng_ht_add_ulong(ua_chan->ctx, &ua_ctx->node); |
31746f93 | 2166 | cds_list_add_tail(&ua_ctx->list, &ua_chan->ctx_list); |
7972aab2 DG |
2167 | |
2168 | ret = create_ust_channel_context(ua_chan, ua_ctx, app); | |
2169 | if (ret < 0) { | |
2170 | goto error; | |
2171 | } | |
2172 | ||
2173 | error: | |
2174 | return ret; | |
2175 | } | |
2176 | ||
2177 | /* | |
2178 | * Enable on the tracer side a ust app event for the session and channel. | |
2179 | * | |
2180 | * Called with UST app session lock held. | |
2181 | */ | |
2182 | static | |
2183 | int enable_ust_app_event(struct ust_app_session *ua_sess, | |
2184 | struct ust_app_event *ua_event, struct ust_app *app) | |
2185 | { | |
2186 | int ret; | |
2187 | ||
2188 | ret = enable_ust_event(app, ua_sess, ua_event); | |
2189 | if (ret < 0) { | |
2190 | goto error; | |
2191 | } | |
2192 | ||
2193 | ua_event->enabled = 1; | |
2194 | ||
2195 | error: | |
2196 | return ret; | |
2197 | } | |
2198 | ||
2199 | /* | |
2200 | * Disable on the tracer side a ust app event for the session and channel. | |
2201 | */ | |
2202 | static int disable_ust_app_event(struct ust_app_session *ua_sess, | |
2203 | struct ust_app_event *ua_event, struct ust_app *app) | |
2204 | { | |
2205 | int ret; | |
2206 | ||
2207 | ret = disable_ust_event(app, ua_sess, ua_event); | |
2208 | if (ret < 0) { | |
2209 | goto error; | |
2210 | } | |
2211 | ||
2212 | ua_event->enabled = 0; | |
2213 | ||
2214 | error: | |
2215 | return ret; | |
2216 | } | |
2217 | ||
2218 | /* | |
2219 | * Lookup ust app channel for session and disable it on the tracer side. | |
2220 | */ | |
2221 | static | |
2222 | int disable_ust_app_channel(struct ust_app_session *ua_sess, | |
2223 | struct ust_app_channel *ua_chan, struct ust_app *app) | |
2224 | { | |
2225 | int ret; | |
2226 | ||
2227 | ret = disable_ust_channel(app, ua_sess, ua_chan); | |
2228 | if (ret < 0) { | |
2229 | goto error; | |
2230 | } | |
2231 | ||
2232 | ua_chan->enabled = 0; | |
2233 | ||
2234 | error: | |
2235 | return ret; | |
2236 | } | |
2237 | ||
2238 | /* | |
2239 | * Lookup ust app channel for session and enable it on the tracer side. This | |
2240 | * MUST be called with a RCU read side lock acquired. | |
2241 | */ | |
2242 | static int enable_ust_app_channel(struct ust_app_session *ua_sess, | |
2243 | struct ltt_ust_channel *uchan, struct ust_app *app) | |
2244 | { | |
2245 | int ret = 0; | |
2246 | struct lttng_ht_iter iter; | |
2247 | struct lttng_ht_node_str *ua_chan_node; | |
2248 | struct ust_app_channel *ua_chan; | |
2249 | ||
2250 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter); | |
2251 | ua_chan_node = lttng_ht_iter_get_node_str(&iter); | |
2252 | if (ua_chan_node == NULL) { | |
d9bf3ca4 | 2253 | DBG2("Unable to find channel %s in ust session id %" PRIu64, |
7972aab2 DG |
2254 | uchan->name, ua_sess->tracing_id); |
2255 | goto error; | |
2256 | } | |
2257 | ||
2258 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); | |
2259 | ||
2260 | ret = enable_ust_channel(app, ua_sess, ua_chan); | |
2261 | if (ret < 0) { | |
2262 | goto error; | |
2263 | } | |
2264 | ||
2265 | error: | |
2266 | return ret; | |
2267 | } | |
2268 | ||
2269 | /* | |
2270 | * Ask the consumer to create a channel and get it if successful. | |
2271 | * | |
2272 | * Return 0 on success or else a negative value. | |
2273 | */ | |
2274 | static int do_consumer_create_channel(struct ltt_ust_session *usess, | |
2275 | struct ust_app_session *ua_sess, struct ust_app_channel *ua_chan, | |
2276 | int bitness, struct ust_registry_session *registry) | |
2277 | { | |
2278 | int ret; | |
2279 | unsigned int nb_fd = 0; | |
2280 | struct consumer_socket *socket; | |
2281 | ||
2282 | assert(usess); | |
2283 | assert(ua_sess); | |
2284 | assert(ua_chan); | |
2285 | assert(registry); | |
2286 | ||
2287 | rcu_read_lock(); | |
2288 | health_code_update(); | |
2289 | ||
2290 | /* Get the right consumer socket for the application. */ | |
2291 | socket = consumer_find_socket_by_bitness(bitness, usess->consumer); | |
2292 | if (!socket) { | |
2293 | ret = -EINVAL; | |
2294 | goto error; | |
2295 | } | |
2296 | ||
2297 | health_code_update(); | |
2298 | ||
2299 | /* Need one fd for the channel. */ | |
2300 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); | |
2301 | if (ret < 0) { | |
2302 | ERR("Exhausted number of available FD upon create channel"); | |
2303 | goto error; | |
2304 | } | |
2305 | ||
2306 | /* | |
2307 | * Ask consumer to create channel. The consumer will return the number of | |
2308 | * stream we have to expect. | |
2309 | */ | |
2310 | ret = ust_consumer_ask_channel(ua_sess, ua_chan, usess->consumer, socket, | |
2311 | registry); | |
2312 | if (ret < 0) { | |
2313 | goto error_ask; | |
2314 | } | |
2315 | ||
2316 | /* | |
2317 | * Compute the number of fd needed before receiving them. It must be 2 per | |
2318 | * stream (2 being the default value here). | |
2319 | */ | |
2320 | nb_fd = DEFAULT_UST_STREAM_FD_NUM * ua_chan->expected_stream_count; | |
2321 | ||
2322 | /* Reserve the amount of file descriptor we need. */ | |
2323 | ret = lttng_fd_get(LTTNG_FD_APPS, nb_fd); | |
2324 | if (ret < 0) { | |
2325 | ERR("Exhausted number of available FD upon create channel"); | |
2326 | goto error_fd_get_stream; | |
2327 | } | |
2328 | ||
2329 | health_code_update(); | |
2330 | ||
2331 | /* | |
2332 | * Now get the channel from the consumer. This call wil populate the stream | |
2333 | * list of that channel and set the ust objects. | |
2334 | */ | |
d9078d0c DG |
2335 | if (usess->consumer->enabled) { |
2336 | ret = ust_consumer_get_channel(socket, ua_chan); | |
2337 | if (ret < 0) { | |
2338 | goto error_destroy; | |
2339 | } | |
7972aab2 DG |
2340 | } |
2341 | ||
2342 | rcu_read_unlock(); | |
2343 | return 0; | |
2344 | ||
2345 | error_destroy: | |
2346 | lttng_fd_put(LTTNG_FD_APPS, nb_fd); | |
2347 | error_fd_get_stream: | |
2348 | /* | |
2349 | * Initiate a destroy channel on the consumer since we had an error | |
2350 | * handling it on our side. The return value is of no importance since we | |
2351 | * already have a ret value set by the previous error that we need to | |
2352 | * return. | |
2353 | */ | |
2354 | (void) ust_consumer_destroy_channel(socket, ua_chan); | |
2355 | error_ask: | |
2356 | lttng_fd_put(LTTNG_FD_APPS, 1); | |
2357 | error: | |
2358 | health_code_update(); | |
2359 | rcu_read_unlock(); | |
2360 | return ret; | |
2361 | } | |
2362 | ||
2363 | /* | |
2364 | * Duplicate the ust data object of the ust app stream and save it in the | |
2365 | * buffer registry stream. | |
2366 | * | |
2367 | * Return 0 on success or else a negative value. | |
2368 | */ | |
2369 | static int duplicate_stream_object(struct buffer_reg_stream *reg_stream, | |
2370 | struct ust_app_stream *stream) | |
2371 | { | |
2372 | int ret; | |
2373 | ||
2374 | assert(reg_stream); | |
2375 | assert(stream); | |
2376 | ||
2377 | /* Reserve the amount of file descriptor we need. */ | |
2378 | ret = lttng_fd_get(LTTNG_FD_APPS, 2); | |
2379 | if (ret < 0) { | |
2380 | ERR("Exhausted number of available FD upon duplicate stream"); | |
2381 | goto error; | |
2382 | } | |
2383 | ||
2384 | /* Duplicate object for stream once the original is in the registry. */ | |
2385 | ret = ustctl_duplicate_ust_object_data(&stream->obj, | |
2386 | reg_stream->obj.ust); | |
2387 | if (ret < 0) { | |
2388 | ERR("Duplicate stream obj from %p to %p failed with ret %d", | |
2389 | reg_stream->obj.ust, stream->obj, ret); | |
2390 | lttng_fd_put(LTTNG_FD_APPS, 2); | |
2391 | goto error; | |
2392 | } | |
2393 | stream->handle = stream->obj->handle; | |
2394 | ||
2395 | error: | |
2396 | return ret; | |
2397 | } | |
2398 | ||
2399 | /* | |
2400 | * Duplicate the ust data object of the ust app. channel and save it in the | |
2401 | * buffer registry channel. | |
2402 | * | |
2403 | * Return 0 on success or else a negative value. | |
2404 | */ | |
2405 | static int duplicate_channel_object(struct buffer_reg_channel *reg_chan, | |
2406 | struct ust_app_channel *ua_chan) | |
2407 | { | |
2408 | int ret; | |
2409 | ||
2410 | assert(reg_chan); | |
2411 | assert(ua_chan); | |
2412 | ||
2413 | /* Need two fds for the channel. */ | |
2414 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); | |
2415 | if (ret < 0) { | |
2416 | ERR("Exhausted number of available FD upon duplicate channel"); | |
2417 | goto error_fd_get; | |
2418 | } | |
2419 | ||
2420 | /* Duplicate object for stream once the original is in the registry. */ | |
2421 | ret = ustctl_duplicate_ust_object_data(&ua_chan->obj, reg_chan->obj.ust); | |
2422 | if (ret < 0) { | |
2423 | ERR("Duplicate channel obj from %p to %p failed with ret: %d", | |
2424 | reg_chan->obj.ust, ua_chan->obj, ret); | |
2425 | goto error; | |
2426 | } | |
2427 | ua_chan->handle = ua_chan->obj->handle; | |
2428 | ||
2429 | return 0; | |
2430 | ||
2431 | error: | |
2432 | lttng_fd_put(LTTNG_FD_APPS, 1); | |
2433 | error_fd_get: | |
2434 | return ret; | |
2435 | } | |
2436 | ||
2437 | /* | |
2438 | * For a given channel buffer registry, setup all streams of the given ust | |
2439 | * application channel. | |
2440 | * | |
2441 | * Return 0 on success or else a negative value. | |
2442 | */ | |
2443 | static int setup_buffer_reg_streams(struct buffer_reg_channel *reg_chan, | |
2444 | struct ust_app_channel *ua_chan) | |
2445 | { | |
2446 | int ret = 0; | |
2447 | struct ust_app_stream *stream, *stmp; | |
2448 | ||
2449 | assert(reg_chan); | |
2450 | assert(ua_chan); | |
2451 | ||
2452 | DBG2("UST app setup buffer registry stream"); | |
2453 | ||
2454 | /* Send all streams to application. */ | |
2455 | cds_list_for_each_entry_safe(stream, stmp, &ua_chan->streams.head, list) { | |
2456 | struct buffer_reg_stream *reg_stream; | |
2457 | ||
2458 | ret = buffer_reg_stream_create(®_stream); | |
2459 | if (ret < 0) { | |
2460 | goto error; | |
2461 | } | |
2462 | ||
2463 | /* | |
2464 | * Keep original pointer and nullify it in the stream so the delete | |
2465 | * stream call does not release the object. | |
2466 | */ | |
2467 | reg_stream->obj.ust = stream->obj; | |
2468 | stream->obj = NULL; | |
2469 | buffer_reg_stream_add(reg_stream, reg_chan); | |
421cb601 | 2470 | |
7972aab2 DG |
2471 | /* We don't need the streams anymore. */ |
2472 | cds_list_del(&stream->list); | |
2473 | delete_ust_app_stream(-1, stream); | |
2474 | } | |
421cb601 | 2475 | |
7972aab2 DG |
2476 | error: |
2477 | return ret; | |
2478 | } | |
2479 | ||
2480 | /* | |
2481 | * Create a buffer registry channel for the given session registry and | |
2482 | * application channel object. If regp pointer is valid, it's set with the | |
2483 | * created object. Important, the created object is NOT added to the session | |
2484 | * registry hash table. | |
2485 | * | |
2486 | * Return 0 on success else a negative value. | |
2487 | */ | |
2488 | static int create_buffer_reg_channel(struct buffer_reg_session *reg_sess, | |
2489 | struct ust_app_channel *ua_chan, struct buffer_reg_channel **regp) | |
2490 | { | |
2491 | int ret; | |
2492 | struct buffer_reg_channel *reg_chan = NULL; | |
2493 | ||
2494 | assert(reg_sess); | |
2495 | assert(ua_chan); | |
2496 | ||
2497 | DBG2("UST app creating buffer registry channel for %s", ua_chan->name); | |
2498 | ||
2499 | /* Create buffer registry channel. */ | |
2500 | ret = buffer_reg_channel_create(ua_chan->tracing_channel_id, ®_chan); | |
2501 | if (ret < 0) { | |
2502 | goto error_create; | |
421cb601 | 2503 | } |
7972aab2 DG |
2504 | assert(reg_chan); |
2505 | reg_chan->consumer_key = ua_chan->key; | |
8c924c7b | 2506 | reg_chan->subbuf_size = ua_chan->attr.subbuf_size; |
d07ceecd | 2507 | reg_chan->num_subbuf = ua_chan->attr.num_subbuf; |
421cb601 | 2508 | |
7972aab2 DG |
2509 | /* Create and add a channel registry to session. */ |
2510 | ret = ust_registry_channel_add(reg_sess->reg.ust, | |
2511 | ua_chan->tracing_channel_id); | |
2512 | if (ret < 0) { | |
2513 | goto error; | |
d88aee68 | 2514 | } |
7972aab2 | 2515 | buffer_reg_channel_add(reg_sess, reg_chan); |
d88aee68 | 2516 | |
7972aab2 DG |
2517 | if (regp) { |
2518 | *regp = reg_chan; | |
3d8ca23b | 2519 | } |
d88aee68 | 2520 | |
7972aab2 | 2521 | return 0; |
3d8ca23b DG |
2522 | |
2523 | error: | |
7972aab2 DG |
2524 | /* Safe because the registry channel object was not added to any HT. */ |
2525 | buffer_reg_channel_destroy(reg_chan, LTTNG_DOMAIN_UST); | |
2526 | error_create: | |
3d8ca23b | 2527 | return ret; |
421cb601 DG |
2528 | } |
2529 | ||
55cc08a6 | 2530 | /* |
7972aab2 DG |
2531 | * Setup buffer registry channel for the given session registry and application |
2532 | * channel object. If regp pointer is valid, it's set with the created object. | |
d0b96690 | 2533 | * |
7972aab2 | 2534 | * Return 0 on success else a negative value. |
55cc08a6 | 2535 | */ |
7972aab2 DG |
2536 | static int setup_buffer_reg_channel(struct buffer_reg_session *reg_sess, |
2537 | struct ust_app_channel *ua_chan, struct buffer_reg_channel *reg_chan) | |
55cc08a6 | 2538 | { |
7972aab2 | 2539 | int ret; |
55cc08a6 | 2540 | |
7972aab2 DG |
2541 | assert(reg_sess); |
2542 | assert(reg_chan); | |
2543 | assert(ua_chan); | |
2544 | assert(ua_chan->obj); | |
55cc08a6 | 2545 | |
7972aab2 | 2546 | DBG2("UST app setup buffer registry channel for %s", ua_chan->name); |
55cc08a6 | 2547 | |
7972aab2 DG |
2548 | /* Setup all streams for the registry. */ |
2549 | ret = setup_buffer_reg_streams(reg_chan, ua_chan); | |
2550 | if (ret < 0) { | |
55cc08a6 DG |
2551 | goto error; |
2552 | } | |
2553 | ||
7972aab2 DG |
2554 | reg_chan->obj.ust = ua_chan->obj; |
2555 | ua_chan->obj = NULL; | |
55cc08a6 | 2556 | |
7972aab2 | 2557 | return 0; |
55cc08a6 DG |
2558 | |
2559 | error: | |
7972aab2 DG |
2560 | buffer_reg_channel_remove(reg_sess, reg_chan); |
2561 | buffer_reg_channel_destroy(reg_chan, LTTNG_DOMAIN_UST); | |
55cc08a6 DG |
2562 | return ret; |
2563 | } | |
2564 | ||
edb67388 | 2565 | /* |
7972aab2 | 2566 | * Send buffer registry channel to the application. |
d0b96690 | 2567 | * |
7972aab2 | 2568 | * Return 0 on success else a negative value. |
edb67388 | 2569 | */ |
7972aab2 DG |
2570 | static int send_channel_uid_to_ust(struct buffer_reg_channel *reg_chan, |
2571 | struct ust_app *app, struct ust_app_session *ua_sess, | |
2572 | struct ust_app_channel *ua_chan) | |
edb67388 DG |
2573 | { |
2574 | int ret; | |
7972aab2 | 2575 | struct buffer_reg_stream *reg_stream; |
edb67388 | 2576 | |
7972aab2 DG |
2577 | assert(reg_chan); |
2578 | assert(app); | |
2579 | assert(ua_sess); | |
2580 | assert(ua_chan); | |
2581 | ||
2582 | DBG("UST app sending buffer registry channel to ust sock %d", app->sock); | |
2583 | ||
2584 | ret = duplicate_channel_object(reg_chan, ua_chan); | |
edb67388 DG |
2585 | if (ret < 0) { |
2586 | goto error; | |
2587 | } | |
2588 | ||
7972aab2 DG |
2589 | /* Send channel to the application. */ |
2590 | ret = ust_consumer_send_channel_to_ust(app, ua_sess, ua_chan); | |
a7169585 MD |
2591 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
2592 | ret = -ENOTCONN; /* Caused by app exiting. */ | |
2593 | goto error; | |
2594 | } else if (ret < 0) { | |
7972aab2 DG |
2595 | goto error; |
2596 | } | |
2597 | ||
2598 | health_code_update(); | |
2599 | ||
2600 | /* Send all streams to application. */ | |
2601 | pthread_mutex_lock(®_chan->stream_list_lock); | |
2602 | cds_list_for_each_entry(reg_stream, ®_chan->streams, lnode) { | |
2603 | struct ust_app_stream stream; | |
2604 | ||
2605 | ret = duplicate_stream_object(reg_stream, &stream); | |
2606 | if (ret < 0) { | |
2607 | goto error_stream_unlock; | |
2608 | } | |
2609 | ||
2610 | ret = ust_consumer_send_stream_to_ust(app, ua_chan, &stream); | |
2611 | if (ret < 0) { | |
8c067051 | 2612 | (void) release_ust_app_stream(-1, &stream); |
a7169585 MD |
2613 | if (ret == -EPIPE || ret == -LTTNG_UST_ERR_EXITING) { |
2614 | ret = -ENOTCONN; /* Caused by app exiting. */ | |
2615 | goto error_stream_unlock; | |
2616 | } else if (ret < 0) { | |
2617 | goto error_stream_unlock; | |
2618 | } | |
7972aab2 DG |
2619 | goto error_stream_unlock; |
2620 | } | |
edb67388 | 2621 | |
7972aab2 DG |
2622 | /* |
2623 | * The return value is not important here. This function will output an | |
2624 | * error if needed. | |
2625 | */ | |
2626 | (void) release_ust_app_stream(-1, &stream); | |
2627 | } | |
2628 | ua_chan->is_sent = 1; | |
2629 | ||
2630 | error_stream_unlock: | |
2631 | pthread_mutex_unlock(®_chan->stream_list_lock); | |
edb67388 DG |
2632 | error: |
2633 | return ret; | |
2634 | } | |
2635 | ||
9730260e | 2636 | /* |
7972aab2 DG |
2637 | * Create and send to the application the created buffers with per UID buffers. |
2638 | * | |
2639 | * Return 0 on success else a negative value. | |
9730260e | 2640 | */ |
7972aab2 DG |
2641 | static int create_channel_per_uid(struct ust_app *app, |
2642 | struct ltt_ust_session *usess, struct ust_app_session *ua_sess, | |
2643 | struct ust_app_channel *ua_chan) | |
9730260e DG |
2644 | { |
2645 | int ret; | |
7972aab2 DG |
2646 | struct buffer_reg_uid *reg_uid; |
2647 | struct buffer_reg_channel *reg_chan; | |
9730260e | 2648 | |
7972aab2 DG |
2649 | assert(app); |
2650 | assert(usess); | |
2651 | assert(ua_sess); | |
2652 | assert(ua_chan); | |
2653 | ||
2654 | DBG("UST app creating channel %s with per UID buffers", ua_chan->name); | |
2655 | ||
2656 | reg_uid = buffer_reg_uid_find(usess->id, app->bits_per_long, app->uid); | |
2657 | /* | |
2658 | * The session creation handles the creation of this global registry | |
2659 | * object. If none can be find, there is a code flow problem or a | |
2660 | * teardown race. | |
2661 | */ | |
2662 | assert(reg_uid); | |
2663 | ||
2664 | reg_chan = buffer_reg_channel_find(ua_chan->tracing_channel_id, | |
2665 | reg_uid); | |
2666 | if (!reg_chan) { | |
2667 | /* Create the buffer registry channel object. */ | |
2668 | ret = create_buffer_reg_channel(reg_uid->registry, ua_chan, ®_chan); | |
2669 | if (ret < 0) { | |
f14256d6 MD |
2670 | ERR("Error creating the UST channel \"%s\" registry instance", |
2671 | ua_chan->name); | |
7972aab2 DG |
2672 | goto error; |
2673 | } | |
2674 | assert(reg_chan); | |
2675 | ||
2676 | /* | |
2677 | * Create the buffers on the consumer side. This call populates the | |
2678 | * ust app channel object with all streams and data object. | |
2679 | */ | |
2680 | ret = do_consumer_create_channel(usess, ua_sess, ua_chan, | |
2681 | app->bits_per_long, reg_uid->registry->reg.ust); | |
2682 | if (ret < 0) { | |
f14256d6 MD |
2683 | ERR("Error creating UST channel \"%s\" on the consumer daemon", |
2684 | ua_chan->name); | |
2685 | ||
07d2ae95 DG |
2686 | /* |
2687 | * Let's remove the previously created buffer registry channel so | |
2688 | * it's not visible anymore in the session registry. | |
2689 | */ | |
2690 | ust_registry_channel_del_free(reg_uid->registry->reg.ust, | |
2691 | ua_chan->tracing_channel_id); | |
2692 | buffer_reg_channel_remove(reg_uid->registry, reg_chan); | |
2693 | buffer_reg_channel_destroy(reg_chan, LTTNG_DOMAIN_UST); | |
7972aab2 DG |
2694 | goto error; |
2695 | } | |
2696 | ||
2697 | /* | |
2698 | * Setup the streams and add it to the session registry. | |
2699 | */ | |
2700 | ret = setup_buffer_reg_channel(reg_uid->registry, ua_chan, reg_chan); | |
2701 | if (ret < 0) { | |
f14256d6 MD |
2702 | ERR("Error setting up UST channel \"%s\"", |
2703 | ua_chan->name); | |
7972aab2 DG |
2704 | goto error; |
2705 | } | |
2706 | ||
2707 | } | |
2708 | ||
2709 | /* Send buffers to the application. */ | |
2710 | ret = send_channel_uid_to_ust(reg_chan, app, ua_sess, ua_chan); | |
9730260e | 2711 | if (ret < 0) { |
a7169585 MD |
2712 | if (ret != -ENOTCONN) { |
2713 | ERR("Error sending channel to application"); | |
2714 | } | |
9730260e DG |
2715 | goto error; |
2716 | } | |
2717 | ||
9730260e DG |
2718 | error: |
2719 | return ret; | |
2720 | } | |
2721 | ||
78f0bacd | 2722 | /* |
7972aab2 DG |
2723 | * Create and send to the application the created buffers with per PID buffers. |
2724 | * | |
2725 | * Return 0 on success else a negative value. | |
78f0bacd | 2726 | */ |
7972aab2 DG |
2727 | static int create_channel_per_pid(struct ust_app *app, |
2728 | struct ltt_ust_session *usess, struct ust_app_session *ua_sess, | |
2729 | struct ust_app_channel *ua_chan) | |
78f0bacd | 2730 | { |
8535a6d9 | 2731 | int ret; |
7972aab2 | 2732 | struct ust_registry_session *registry; |
78f0bacd | 2733 | |
7972aab2 DG |
2734 | assert(app); |
2735 | assert(usess); | |
2736 | assert(ua_sess); | |
2737 | assert(ua_chan); | |
2738 | ||
2739 | DBG("UST app creating channel %s with per PID buffers", ua_chan->name); | |
2740 | ||
2741 | rcu_read_lock(); | |
2742 | ||
2743 | registry = get_session_registry(ua_sess); | |
2744 | assert(registry); | |
2745 | ||
2746 | /* Create and add a new channel registry to session. */ | |
2747 | ret = ust_registry_channel_add(registry, ua_chan->key); | |
78f0bacd | 2748 | if (ret < 0) { |
f14256d6 MD |
2749 | ERR("Error creating the UST channel \"%s\" registry instance", |
2750 | ua_chan->name); | |
78f0bacd DG |
2751 | goto error; |
2752 | } | |
2753 | ||
7972aab2 DG |
2754 | /* Create and get channel on the consumer side. */ |
2755 | ret = do_consumer_create_channel(usess, ua_sess, ua_chan, | |
2756 | app->bits_per_long, registry); | |
2757 | if (ret < 0) { | |
f14256d6 MD |
2758 | ERR("Error creating UST channel \"%s\" on the consumer daemon", |
2759 | ua_chan->name); | |
7972aab2 DG |
2760 | goto error; |
2761 | } | |
2762 | ||
2763 | ret = send_channel_pid_to_ust(app, ua_sess, ua_chan); | |
2764 | if (ret < 0) { | |
a7169585 MD |
2765 | if (ret != -ENOTCONN) { |
2766 | ERR("Error sending channel to application"); | |
2767 | } | |
7972aab2 DG |
2768 | goto error; |
2769 | } | |
8535a6d9 | 2770 | |
78f0bacd | 2771 | error: |
7972aab2 | 2772 | rcu_read_unlock(); |
78f0bacd DG |
2773 | return ret; |
2774 | } | |
2775 | ||
2776 | /* | |
7972aab2 DG |
2777 | * From an already allocated ust app channel, create the channel buffers if |
2778 | * need and send it to the application. This MUST be called with a RCU read | |
2779 | * side lock acquired. | |
2780 | * | |
a7169585 MD |
2781 | * Return 0 on success or else a negative value. Returns -ENOTCONN if |
2782 | * the application exited concurrently. | |
78f0bacd | 2783 | */ |
7972aab2 DG |
2784 | static int do_create_channel(struct ust_app *app, |
2785 | struct ltt_ust_session *usess, struct ust_app_session *ua_sess, | |
2786 | struct ust_app_channel *ua_chan) | |
78f0bacd | 2787 | { |
7972aab2 | 2788 | int ret; |
78f0bacd | 2789 | |
7972aab2 DG |
2790 | assert(app); |
2791 | assert(usess); | |
2792 | assert(ua_sess); | |
2793 | assert(ua_chan); | |
2794 | ||
2795 | /* Handle buffer type before sending the channel to the application. */ | |
2796 | switch (usess->buffer_type) { | |
2797 | case LTTNG_BUFFER_PER_UID: | |
2798 | { | |
2799 | ret = create_channel_per_uid(app, usess, ua_sess, ua_chan); | |
2800 | if (ret < 0) { | |
2801 | goto error; | |
2802 | } | |
2803 | break; | |
2804 | } | |
2805 | case LTTNG_BUFFER_PER_PID: | |
2806 | { | |
2807 | ret = create_channel_per_pid(app, usess, ua_sess, ua_chan); | |
2808 | if (ret < 0) { | |
2809 | goto error; | |
2810 | } | |
2811 | break; | |
2812 | } | |
2813 | default: | |
2814 | assert(0); | |
2815 | ret = -EINVAL; | |
78f0bacd DG |
2816 | goto error; |
2817 | } | |
2818 | ||
7972aab2 DG |
2819 | /* Initialize ust objd object using the received handle and add it. */ |
2820 | lttng_ht_node_init_ulong(&ua_chan->ust_objd_node, ua_chan->handle); | |
2821 | lttng_ht_add_unique_ulong(app->ust_objd, &ua_chan->ust_objd_node); | |
78f0bacd | 2822 | |
7972aab2 DG |
2823 | /* If channel is not enabled, disable it on the tracer */ |
2824 | if (!ua_chan->enabled) { | |
2825 | ret = disable_ust_channel(app, ua_sess, ua_chan); | |
2826 | if (ret < 0) { | |
2827 | goto error; | |
2828 | } | |
78f0bacd DG |
2829 | } |
2830 | ||
2831 | error: | |
2832 | return ret; | |
2833 | } | |
2834 | ||
284d8f55 | 2835 | /* |
4d710ac2 DG |
2836 | * Create UST app channel and create it on the tracer. Set ua_chanp of the |
2837 | * newly created channel if not NULL. | |
d0b96690 | 2838 | * |
36b588ed | 2839 | * Called with UST app session lock and RCU read-side lock held. |
7972aab2 | 2840 | * |
a7169585 MD |
2841 | * Return 0 on success or else a negative value. Returns -ENOTCONN if |
2842 | * the application exited concurrently. | |
284d8f55 | 2843 | */ |
4d710ac2 DG |
2844 | static int create_ust_app_channel(struct ust_app_session *ua_sess, |
2845 | struct ltt_ust_channel *uchan, struct ust_app *app, | |
7972aab2 | 2846 | enum lttng_ust_chan_type type, struct ltt_ust_session *usess, |
4d710ac2 | 2847 | struct ust_app_channel **ua_chanp) |
5b4a0ec0 DG |
2848 | { |
2849 | int ret = 0; | |
bec39940 DG |
2850 | struct lttng_ht_iter iter; |
2851 | struct lttng_ht_node_str *ua_chan_node; | |
5b4a0ec0 DG |
2852 | struct ust_app_channel *ua_chan; |
2853 | ||
2854 | /* Lookup channel in the ust app session */ | |
bec39940 DG |
2855 | lttng_ht_lookup(ua_sess->channels, (void *)uchan->name, &iter); |
2856 | ua_chan_node = lttng_ht_iter_get_node_str(&iter); | |
fc34caaa | 2857 | if (ua_chan_node != NULL) { |
5b4a0ec0 | 2858 | ua_chan = caa_container_of(ua_chan_node, struct ust_app_channel, node); |
fc34caaa | 2859 | goto end; |
5b4a0ec0 DG |
2860 | } |
2861 | ||
d0b96690 | 2862 | ua_chan = alloc_ust_app_channel(uchan->name, ua_sess, &uchan->attr); |
fc34caaa DG |
2863 | if (ua_chan == NULL) { |
2864 | /* Only malloc can fail here */ | |
4d710ac2 | 2865 | ret = -ENOMEM; |
094d1690 | 2866 | goto error_alloc; |
fc34caaa DG |
2867 | } |
2868 | shadow_copy_channel(ua_chan, uchan); | |
2869 | ||
ffe60014 DG |
2870 | /* Set channel type. */ |
2871 | ua_chan->attr.type = type; | |
2872 | ||
7972aab2 | 2873 | ret = do_create_channel(app, usess, ua_sess, ua_chan); |
5b4a0ec0 DG |
2874 | if (ret < 0) { |
2875 | goto error; | |
2876 | } | |
2877 | ||
fc34caaa | 2878 | DBG2("UST app create channel %s for PID %d completed", ua_chan->name, |
852d0037 | 2879 | app->pid); |
fc34caaa | 2880 | |
d0b96690 DG |
2881 | /* Only add the channel if successful on the tracer side. */ |
2882 | lttng_ht_add_unique_str(ua_sess->channels, &ua_chan->node); | |
2883 | ||
fc34caaa | 2884 | end: |
4d710ac2 DG |
2885 | if (ua_chanp) { |
2886 | *ua_chanp = ua_chan; | |
2887 | } | |
2888 | ||
2889 | /* Everything went well. */ | |
2890 | return 0; | |
5b4a0ec0 DG |
2891 | |
2892 | error: | |
d0b96690 | 2893 | delete_ust_app_channel(ua_chan->is_sent ? app->sock : -1, ua_chan, app); |
094d1690 | 2894 | error_alloc: |
4d710ac2 | 2895 | return ret; |
5b4a0ec0 DG |
2896 | } |
2897 | ||
2898 | /* | |
2899 | * Create UST app event and create it on the tracer side. | |
d0b96690 DG |
2900 | * |
2901 | * Called with ust app session mutex held. | |
5b4a0ec0 | 2902 | */ |
edb67388 DG |
2903 | static |
2904 | int create_ust_app_event(struct ust_app_session *ua_sess, | |
2905 | struct ust_app_channel *ua_chan, struct ltt_ust_event *uevent, | |
2906 | struct ust_app *app) | |
284d8f55 | 2907 | { |
edb67388 | 2908 | int ret = 0; |
5b4a0ec0 | 2909 | struct ust_app_event *ua_event; |
284d8f55 | 2910 | |
5b4a0ec0 | 2911 | /* Get event node */ |
18eace3b | 2912 | ua_event = find_ust_app_event(ua_chan->events, uevent->attr.name, |
39c5a3a7 | 2913 | uevent->filter, uevent->attr.loglevel, uevent->exclusion); |
18eace3b | 2914 | if (ua_event != NULL) { |
fc34caaa | 2915 | ret = -EEXIST; |
edb67388 DG |
2916 | goto end; |
2917 | } | |
5b4a0ec0 | 2918 | |
edb67388 DG |
2919 | /* Does not exist so create one */ |
2920 | ua_event = alloc_ust_app_event(uevent->attr.name, &uevent->attr); | |
2921 | if (ua_event == NULL) { | |
2922 | /* Only malloc can failed so something is really wrong */ | |
2923 | ret = -ENOMEM; | |
fc34caaa | 2924 | goto end; |
5b4a0ec0 | 2925 | } |
edb67388 | 2926 | shadow_copy_event(ua_event, uevent); |
5b4a0ec0 | 2927 | |
edb67388 | 2928 | /* Create it on the tracer side */ |
5b4a0ec0 | 2929 | ret = create_ust_event(app, ua_sess, ua_chan, ua_event); |
284d8f55 | 2930 | if (ret < 0) { |
fc34caaa | 2931 | /* Not found previously means that it does not exist on the tracer */ |
76f66f63 | 2932 | assert(ret != -LTTNG_UST_ERR_EXIST); |
284d8f55 DG |
2933 | goto error; |
2934 | } | |
2935 | ||
d0b96690 | 2936 | add_unique_ust_app_event(ua_chan, ua_event); |
284d8f55 | 2937 | |
fc34caaa | 2938 | DBG2("UST app create event %s for PID %d completed", ua_event->name, |
852d0037 | 2939 | app->pid); |
7f79d3a1 | 2940 | |
edb67388 | 2941 | end: |
fc34caaa DG |
2942 | return ret; |
2943 | ||
5b4a0ec0 | 2944 | error: |
fc34caaa DG |
2945 | /* Valid. Calling here is already in a read side lock */ |
2946 | delete_ust_app_event(-1, ua_event); | |
edb67388 | 2947 | return ret; |
5b4a0ec0 DG |
2948 | } |
2949 | ||
2950 | /* | |
2951 | * Create UST metadata and open it on the tracer side. | |
d0b96690 | 2952 | * |
7972aab2 | 2953 | * Called with UST app session lock held and RCU read side lock. |
5b4a0ec0 DG |
2954 | */ |
2955 | static int create_ust_app_metadata(struct ust_app_session *ua_sess, | |
ad7a9107 | 2956 | struct ust_app *app, struct consumer_output *consumer) |
5b4a0ec0 DG |
2957 | { |
2958 | int ret = 0; | |
ffe60014 | 2959 | struct ust_app_channel *metadata; |
d88aee68 | 2960 | struct consumer_socket *socket; |
7972aab2 | 2961 | struct ust_registry_session *registry; |
5b4a0ec0 | 2962 | |
ffe60014 DG |
2963 | assert(ua_sess); |
2964 | assert(app); | |
d88aee68 | 2965 | assert(consumer); |
5b4a0ec0 | 2966 | |
7972aab2 DG |
2967 | registry = get_session_registry(ua_sess); |
2968 | assert(registry); | |
2969 | ||
ce34fcd0 MD |
2970 | pthread_mutex_lock(®istry->lock); |
2971 | ||
1b532a60 DG |
2972 | /* Metadata already exists for this registry or it was closed previously */ |
2973 | if (registry->metadata_key || registry->metadata_closed) { | |
7972aab2 DG |
2974 | ret = 0; |
2975 | goto error; | |
5b4a0ec0 DG |
2976 | } |
2977 | ||
ffe60014 | 2978 | /* Allocate UST metadata */ |
d0b96690 | 2979 | metadata = alloc_ust_app_channel(DEFAULT_METADATA_NAME, ua_sess, NULL); |
ffe60014 DG |
2980 | if (!metadata) { |
2981 | /* malloc() failed */ | |
2982 | ret = -ENOMEM; | |
2983 | goto error; | |
2984 | } | |
5b4a0ec0 | 2985 | |
ad7a9107 | 2986 | memcpy(&metadata->attr, &ua_sess->metadata_attr, sizeof(metadata->attr)); |
5b4a0ec0 | 2987 | |
7972aab2 DG |
2988 | /* Need one fd for the channel. */ |
2989 | ret = lttng_fd_get(LTTNG_FD_APPS, 1); | |
2990 | if (ret < 0) { | |
2991 | ERR("Exhausted number of available FD upon create metadata"); | |
2992 | goto error; | |
2993 | } | |
2994 | ||
4dc3dfc5 DG |
2995 | /* Get the right consumer socket for the application. */ |
2996 | socket = consumer_find_socket_by_bitness(app->bits_per_long, consumer); | |
2997 | if (!socket) { | |
2998 | ret = -EINVAL; | |
2999 | goto error_consumer; | |
3000 | } | |
3001 | ||
331744e3 JD |
3002 | /* |
3003 | * Keep metadata key so we can identify it on the consumer side. Assign it | |
3004 | * to the registry *before* we ask the consumer so we avoid the race of the | |
3005 | * consumer requesting the metadata and the ask_channel call on our side | |
3006 | * did not returned yet. | |
3007 | */ | |
3008 | registry->metadata_key = metadata->key; | |
3009 | ||
d88aee68 DG |
3010 | /* |
3011 | * Ask the metadata channel creation to the consumer. The metadata object | |
3012 | * will be created by the consumer and kept their. However, the stream is | |
3013 | * never added or monitored until we do a first push metadata to the | |
3014 | * consumer. | |
3015 | */ | |
7972aab2 DG |
3016 | ret = ust_consumer_ask_channel(ua_sess, metadata, consumer, socket, |
3017 | registry); | |
d88aee68 | 3018 | if (ret < 0) { |
f2a444f1 DG |
3019 | /* Nullify the metadata key so we don't try to close it later on. */ |
3020 | registry->metadata_key = 0; | |
d88aee68 DG |
3021 | goto error_consumer; |
3022 | } | |
3023 | ||
3024 | /* | |
3025 | * The setup command will make the metadata stream be sent to the relayd, | |
3026 | * if applicable, and the thread managing the metadatas. This is important | |
3027 | * because after this point, if an error occurs, the only way the stream | |
3028 | * can be deleted is to be monitored in the consumer. | |
3029 | */ | |
7972aab2 | 3030 | ret = consumer_setup_metadata(socket, metadata->key); |
ffe60014 | 3031 | if (ret < 0) { |
f2a444f1 DG |
3032 | /* Nullify the metadata key so we don't try to close it later on. */ |
3033 | registry->metadata_key = 0; | |
d88aee68 | 3034 | goto error_consumer; |
5b4a0ec0 DG |
3035 | } |
3036 | ||
7972aab2 DG |
3037 | DBG2("UST metadata with key %" PRIu64 " created for app pid %d", |
3038 | metadata->key, app->pid); | |
5b4a0ec0 | 3039 | |
d88aee68 | 3040 | error_consumer: |
b80f0b6c | 3041 | lttng_fd_put(LTTNG_FD_APPS, 1); |
d88aee68 | 3042 | delete_ust_app_channel(-1, metadata, app); |
5b4a0ec0 | 3043 | error: |
ce34fcd0 | 3044 | pthread_mutex_unlock(®istry->lock); |
ffe60014 | 3045 | return ret; |
5b4a0ec0 DG |
3046 | } |
3047 | ||
5b4a0ec0 | 3048 | /* |
d88aee68 DG |
3049 | * Return ust app pointer or NULL if not found. RCU read side lock MUST be |
3050 | * acquired before calling this function. | |
5b4a0ec0 DG |
3051 | */ |
3052 | struct ust_app *ust_app_find_by_pid(pid_t pid) | |
3053 | { | |
d88aee68 | 3054 | struct ust_app *app = NULL; |
bec39940 DG |
3055 | struct lttng_ht_node_ulong *node; |
3056 | struct lttng_ht_iter iter; | |
5b4a0ec0 | 3057 | |
bec39940 DG |
3058 | lttng_ht_lookup(ust_app_ht, (void *)((unsigned long) pid), &iter); |
3059 | node = lttng_ht_iter_get_node_ulong(&iter); | |
5b4a0ec0 DG |
3060 | if (node == NULL) { |
3061 | DBG2("UST app no found with pid %d", pid); | |
3062 | goto error; | |
3063 | } | |
5b4a0ec0 DG |
3064 | |
3065 | DBG2("Found UST app by pid %d", pid); | |
3066 | ||
d88aee68 | 3067 | app = caa_container_of(node, struct ust_app, pid_n); |
5b4a0ec0 DG |
3068 | |
3069 | error: | |
d88aee68 | 3070 | return app; |
5b4a0ec0 DG |
3071 | } |
3072 | ||
d88aee68 DG |
3073 | /* |
3074 | * Allocate and init an UST app object using the registration information and | |
3075 | * the command socket. This is called when the command socket connects to the | |
3076 | * session daemon. | |
3077 | * | |
3078 | * The object is returned on success or else NULL. | |
3079 | */ | |
d0b96690 | 3080 | struct ust_app *ust_app_create(struct ust_register_msg *msg, int sock) |
5b4a0ec0 | 3081 | { |
d0b96690 DG |
3082 | struct ust_app *lta = NULL; |
3083 | ||
3084 | assert(msg); | |
3085 | assert(sock >= 0); | |
3086 | ||
3087 | DBG3("UST app creating application for socket %d", sock); | |
5b4a0ec0 | 3088 | |
173af62f DG |
3089 | if ((msg->bits_per_long == 64 && |
3090 | (uatomic_read(&ust_consumerd64_fd) == -EINVAL)) | |
3091 | || (msg->bits_per_long == 32 && | |
3092 | (uatomic_read(&ust_consumerd32_fd) == -EINVAL))) { | |
f943b0fb | 3093 | ERR("Registration failed: application \"%s\" (pid: %d) has " |
d0b96690 DG |
3094 | "%d-bit long, but no consumerd for this size is available.\n", |
3095 | msg->name, msg->pid, msg->bits_per_long); | |
3096 | goto error; | |
3f2c5fcc | 3097 | } |
d0b96690 | 3098 | |
5b4a0ec0 DG |
3099 | lta = zmalloc(sizeof(struct ust_app)); |
3100 | if (lta == NULL) { | |
3101 | PERROR("malloc"); | |
d0b96690 | 3102 | goto error; |
5b4a0ec0 DG |
3103 | } |
3104 | ||
3105 | lta->ppid = msg->ppid; | |
3106 | lta->uid = msg->uid; | |
3107 | lta->gid = msg->gid; | |
d0b96690 | 3108 | |
7753dea8 | 3109 | lta->bits_per_long = msg->bits_per_long; |
d0b96690 DG |
3110 | lta->uint8_t_alignment = msg->uint8_t_alignment; |
3111 | lta->uint16_t_alignment = msg->uint16_t_alignment; | |
3112 | lta->uint32_t_alignment = msg->uint32_t_alignment; | |
3113 | lta->uint64_t_alignment = msg->uint64_t_alignment; | |
3114 | lta->long_alignment = msg->long_alignment; | |
3115 | lta->byte_order = msg->byte_order; | |
3116 | ||
5b4a0ec0 DG |
3117 | lta->v_major = msg->major; |
3118 | lta->v_minor = msg->minor; | |
d9bf3ca4 | 3119 | lta->sessions = lttng_ht_new(0, LTTNG_HT_TYPE_U64); |
d0b96690 DG |
3120 | lta->ust_objd = lttng_ht_new(0, LTTNG_HT_TYPE_ULONG); |
3121 | lta->notify_sock = -1; | |
d88aee68 DG |
3122 | |
3123 | /* Copy name and make sure it's NULL terminated. */ | |
3124 | strncpy(lta->name, msg->name, sizeof(lta->name)); | |
3125 | lta->name[UST_APP_PROCNAME_LEN] = '\0'; | |
3126 | ||
3127 | /* | |
3128 | * Before this can be called, when receiving the registration information, | |
3129 | * the application compatibility is checked. So, at this point, the | |
3130 | * application can work with this session daemon. | |
3131 | */ | |
d0b96690 | 3132 | lta->compatible = 1; |
5b4a0ec0 | 3133 | |
852d0037 | 3134 | lta->pid = msg->pid; |
d0b96690 | 3135 | lttng_ht_node_init_ulong(<a->pid_n, (unsigned long) lta->pid); |
852d0037 | 3136 | lta->sock = sock; |
d0b96690 | 3137 | lttng_ht_node_init_ulong(<a->sock_n, (unsigned long) lta->sock); |
5b4a0ec0 | 3138 | |
d42f20df DG |
3139 | CDS_INIT_LIST_HEAD(<a->teardown_head); |
3140 | ||
d0b96690 DG |
3141 | error: |
3142 | return lta; | |
3143 | } | |
3144 | ||
d88aee68 DG |
3145 | /* |
3146 | * For a given application object, add it to every hash table. | |
3147 | */ | |
d0b96690 DG |
3148 | void ust_app_add(struct ust_app *app) |
3149 | { | |
3150 | assert(app); | |
3151 | assert(app->notify_sock >= 0); | |
3152 | ||
5b4a0ec0 | 3153 | rcu_read_lock(); |
852d0037 DG |
3154 | |
3155 | /* | |
3156 | * On a re-registration, we want to kick out the previous registration of | |
3157 | * that pid | |
3158 | */ | |
d0b96690 | 3159 | lttng_ht_add_replace_ulong(ust_app_ht, &app->pid_n); |
852d0037 DG |
3160 | |
3161 | /* | |
3162 | * The socket _should_ be unique until _we_ call close. So, a add_unique | |
3163 | * for the ust_app_ht_by_sock is used which asserts fail if the entry was | |
3164 | * already in the table. | |
3165 | */ | |
d0b96690 | 3166 | lttng_ht_add_unique_ulong(ust_app_ht_by_sock, &app->sock_n); |
852d0037 | 3167 | |
d0b96690 DG |
3168 | /* Add application to the notify socket hash table. */ |
3169 | lttng_ht_node_init_ulong(&app->notify_sock_n, app->notify_sock); | |
3170 | lttng_ht_add_unique_ulong(ust_app_ht_by_notify_sock, &app->notify_sock_n); | |
5b4a0ec0 | 3171 | |
d0b96690 | 3172 | DBG("App registered with pid:%d ppid:%d uid:%d gid:%d sock:%d name:%s " |
d88aee68 DG |
3173 | "notify_sock:%d (version %d.%d)", app->pid, app->ppid, app->uid, |
3174 | app->gid, app->sock, app->name, app->notify_sock, app->v_major, | |
3175 | app->v_minor); | |
5b4a0ec0 | 3176 | |
d0b96690 DG |
3177 | rcu_read_unlock(); |
3178 | } | |
3179 | ||
d88aee68 DG |
3180 | /* |
3181 | * Set the application version into the object. | |
3182 | * | |
3183 | * Return 0 on success else a negative value either an errno code or a | |
3184 | * LTTng-UST error code. | |
3185 | */ | |
d0b96690 DG |
3186 | int ust_app_version(struct ust_app *app) |
3187 | { | |
d88aee68 DG |
3188 | int ret; |
3189 | ||
d0b96690 | 3190 | assert(app); |
d88aee68 DG |
3191 | |
3192 | ret = ustctl_tracer_version(app->sock, &app->version); | |
3193 | if (ret < 0) { | |
3194 | if (ret != -LTTNG_UST_ERR_EXITING && ret != -EPIPE) { | |
5368d366 | 3195 | ERR("UST app %d version failed with ret %d", app->sock, ret); |
d88aee68 | 3196 | } else { |
5368d366 | 3197 | DBG3("UST app %d version failed. Application is dead", app->sock); |
d88aee68 DG |
3198 | } |
3199 | } | |
3200 | ||
3201 | return ret; | |
5b4a0ec0 DG |
3202 | } |
3203 | ||
3204 | /* | |
3205 | * Unregister app by removing it from the global traceable app list and freeing | |
3206 | * the data struct. | |
3207 | * | |
3208 | * The socket is already closed at this point so no close to sock. | |
3209 | */ | |
3210 | void ust_app_unregister(int sock) | |
3211 | { | |
3212 | struct ust_app *lta; | |
bec39940 | 3213 | struct lttng_ht_node_ulong *node; |
c4b88406 | 3214 | struct lttng_ht_iter ust_app_sock_iter; |
bec39940 | 3215 | struct lttng_ht_iter iter; |
d42f20df | 3216 | struct ust_app_session *ua_sess; |
525b0740 | 3217 | int ret; |
5b4a0ec0 DG |
3218 | |
3219 | rcu_read_lock(); | |
886459c6 | 3220 | |
5b4a0ec0 | 3221 | /* Get the node reference for a call_rcu */ |
c4b88406 MD |
3222 | lttng_ht_lookup(ust_app_ht_by_sock, (void *)((unsigned long) sock), &ust_app_sock_iter); |
3223 | node = lttng_ht_iter_get_node_ulong(&ust_app_sock_iter); | |
d0b96690 | 3224 | assert(node); |
284d8f55 | 3225 | |
852d0037 | 3226 | lta = caa_container_of(node, struct ust_app, sock_n); |
852d0037 DG |
3227 | DBG("PID %d unregistering with sock %d", lta->pid, sock); |
3228 | ||
d88aee68 | 3229 | /* |
ce34fcd0 MD |
3230 | * For per-PID buffers, perform "push metadata" and flush all |
3231 | * application streams before removing app from hash tables, | |
3232 | * ensuring proper behavior of data_pending check. | |
c4b88406 | 3233 | * Remove sessions so they are not visible during deletion. |
d88aee68 | 3234 | */ |
d42f20df DG |
3235 | cds_lfht_for_each_entry(lta->sessions->ht, &iter.iter, ua_sess, |
3236 | node.node) { | |
7972aab2 DG |
3237 | struct ust_registry_session *registry; |
3238 | ||
d42f20df DG |
3239 | ret = lttng_ht_del(lta->sessions, &iter); |
3240 | if (ret) { | |
3241 | /* The session was already removed so scheduled for teardown. */ | |
3242 | continue; | |
3243 | } | |
3244 | ||
ce34fcd0 MD |
3245 | if (ua_sess->buffer_type == LTTNG_BUFFER_PER_PID) { |
3246 | (void) ust_app_flush_app_session(lta, ua_sess); | |
3247 | } | |
c4b88406 | 3248 | |
d42f20df DG |
3249 | /* |
3250 | * Add session to list for teardown. This is safe since at this point we | |
3251 | * are the only one using this list. | |
3252 | */ | |
d88aee68 DG |
3253 | pthread_mutex_lock(&ua_sess->lock); |
3254 | ||
b161602a MD |
3255 | if (ua_sess->deleted) { |
3256 | pthread_mutex_unlock(&ua_sess->lock); | |
3257 | continue; | |
3258 | } | |
3259 | ||
d88aee68 DG |
3260 | /* |
3261 | * Normally, this is done in the delete session process which is | |
3262 | * executed in the call rcu below. However, upon registration we can't | |
3263 | * afford to wait for the grace period before pushing data or else the | |
3264 | * data pending feature can race between the unregistration and stop | |
3265 | * command where the data pending command is sent *before* the grace | |
3266 | * period ended. | |
3267 | * | |
3268 | * The close metadata below nullifies the metadata pointer in the | |
3269 | * session so the delete session will NOT push/close a second time. | |
3270 | */ | |
7972aab2 | 3271 | registry = get_session_registry(ua_sess); |
ce34fcd0 | 3272 | if (registry) { |
7972aab2 DG |
3273 | /* Push metadata for application before freeing the application. */ |
3274 |