Fix: sessiond: bad fd used while rotating exiting app's buffers
[lttng-tools.git] / src / bin / lttng-sessiond / ust-app.h
... / ...
CommitLineData
1/*
2 * Copyright (C) 2011 EfficiOS Inc.
3 * Copyright (C) 2016 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 *
7 */
8
9#ifndef _LTT_UST_APP_H
10#define _LTT_UST_APP_H
11
12#include <stdint.h>
13#include <urcu/ref.h>
14
15#include <common/index-allocator.h>
16#include <common/uuid.h>
17
18#include "trace-ust.h"
19#include "ust-registry.h"
20#include "session.h"
21
22#define UST_APP_EVENT_LIST_SIZE 32
23
24/* Process name (short). */
25#define UST_APP_PROCNAME_LEN 16
26
27struct lttng_bytecode;
28struct lttng_ust_filter_bytecode;
29
30extern int the_ust_consumerd64_fd, the_ust_consumerd32_fd;
31
32/*
33 * Object used to close the notify socket in a call_rcu(). Since the
34 * application might not be found, we need an independant object containing the
35 * notify socket fd.
36 */
37struct ust_app_notify_sock_obj {
38 int fd;
39 struct rcu_head head;
40};
41
42struct ust_app_ht_key {
43 const char *name;
44 const struct lttng_bytecode *filter;
45 enum lttng_ust_abi_loglevel_type loglevel_type;
46 int loglevel_value;
47 const struct lttng_event_exclusion *exclusion;
48};
49
50/*
51 * Application registration data structure.
52 */
53struct ust_register_msg {
54 enum lttng_ust_ctl_socket_type type;
55 uint32_t major;
56 uint32_t minor;
57 uint32_t abi_major;
58 uint32_t abi_minor;
59 pid_t pid;
60 pid_t ppid;
61 uid_t uid;
62 gid_t gid;
63 uint32_t bits_per_long;
64 uint32_t uint8_t_alignment;
65 uint32_t uint16_t_alignment;
66 uint32_t uint32_t_alignment;
67 uint32_t uint64_t_alignment;
68 uint32_t long_alignment;
69 int byte_order; /* BIG_ENDIAN or LITTLE_ENDIAN */
70 char name[LTTNG_UST_ABI_PROCNAME_LEN];
71};
72
73/*
74 * Global applications HT used by the session daemon. This table is indexed by
75 * PID using the pid_n node and pid value of an ust_app.
76 */
77extern struct lttng_ht *ust_app_ht;
78
79/*
80 * Global applications HT used by the session daemon. This table is indexed by
81 * socket using the sock_n node and sock value of an ust_app.
82 *
83 * The 'sock' in question here is the 'command' socket.
84 */
85extern struct lttng_ht *ust_app_ht_by_sock;
86
87/*
88 * Global applications HT used by the session daemon. This table is indexed by
89 * socket using the notify_sock_n node and notify_sock value of an ust_app.
90 */
91extern struct lttng_ht *ust_app_ht_by_notify_sock;
92
93/* Stream list containing ust_app_stream. */
94struct ust_app_stream_list {
95 unsigned int count;
96 struct cds_list_head head;
97};
98
99struct ust_app_ctx {
100 int handle;
101 struct lttng_ust_context_attr ctx;
102 struct lttng_ust_abi_object_data *obj;
103 struct lttng_ht_node_ulong node;
104 struct cds_list_head list;
105};
106
107struct ust_app_event {
108 int enabled;
109 int handle;
110 struct lttng_ust_abi_object_data *obj;
111 struct lttng_ust_abi_event attr;
112 char name[LTTNG_UST_ABI_SYM_NAME_LEN];
113 struct lttng_ht_node_str node;
114 struct lttng_bytecode *filter;
115 struct lttng_event_exclusion *exclusion;
116};
117
118struct ust_app_event_notifier_rule {
119 int enabled;
120 uint64_t error_counter_index;
121 int handle;
122 struct lttng_ust_abi_object_data *obj;
123 /* Holds a strong reference. */
124 struct lttng_trigger *trigger;
125 /* Unique ID returned by the tracer to identify this event notifier. */
126 uint64_t token;
127 struct lttng_ht_node_u64 node;
128 /* The trigger object owns the filter. */
129 const struct lttng_bytecode *filter;
130 /* Owned by this. */
131 struct lttng_event_exclusion *exclusion;
132 /* For delayed reclaim. */
133 struct rcu_head rcu_head;
134};
135
136struct ust_app_stream {
137 int handle;
138 char pathname[PATH_MAX];
139 /* Format is %s_%d respectively channel name and CPU number. */
140 char name[DEFAULT_STREAM_NAME_LEN];
141 struct lttng_ust_abi_object_data *obj;
142 /* Using a list of streams to keep order. */
143 struct cds_list_head list;
144};
145
146struct ust_app_channel {
147 int enabled;
148 int handle;
149 /* Channel and streams were sent to the UST tracer. */
150 int is_sent;
151 /*
152 * Unique key used to identify the channel on the consumer side.
153 * 0 is a reserved 'invalid' value used to indicate that the consumer
154 * does not know about this channel (i.e. an error occurred).
155 */
156 uint64_t key;
157 /* Id of the tracing channel set on creation. */
158 uint64_t tracing_channel_id;
159 /* Number of stream that this channel is expected to receive. */
160 unsigned int expected_stream_count;
161 char name[LTTNG_UST_ABI_SYM_NAME_LEN];
162 struct lttng_ust_abi_object_data *obj;
163 struct lttng_ust_ctl_consumer_channel_attr attr;
164 struct ust_app_stream_list streams;
165 /* Session pointer that owns this object. */
166 struct ust_app_session *session;
167 /*
168 * Contexts are kept in a hash table for fast lookup and in an ordered list
169 * so we are able to enable them on the tracer side in the same order the
170 * user added them.
171 */
172 struct lttng_ht *ctx;
173 struct cds_list_head ctx_list;
174
175 struct lttng_ht *events;
176 uint64_t tracefile_size;
177 uint64_t tracefile_count;
178 uint64_t monitor_timer_interval;
179 /*
180 * Node indexed by channel name in the channels' hash table of a session.
181 */
182 struct lttng_ht_node_str node;
183 /*
184 * Node indexed by UST channel object descriptor (handle). Stored in the
185 * ust_objd hash table in the ust_app object.
186 */
187 struct lttng_ht_node_ulong ust_objd_node;
188 /* For delayed reclaim */
189 struct rcu_head rcu_head;
190};
191
192struct ust_app_session {
193 /*
194 * Lock protecting this session's ust app interaction. Held
195 * across command send/recv to/from app. Never nests within the
196 * session registry lock.
197 */
198 pthread_mutex_t lock;
199
200 int enabled;
201 /* started: has the session been in started state at any time ? */
202 int started; /* allows detection of start vs restart. */
203 int handle; /* used has unique identifier for app session */
204
205 bool deleted; /* Session deleted flag. Check with lock held. */
206
207 /*
208 * Tracing session ID. Multiple ust app session can have the same tracing
209 * session id making this value NOT unique to the object.
210 */
211 uint64_t tracing_id;
212 uint64_t id; /* Unique session identifier */
213 struct lttng_ht *channels; /* Registered channels */
214 struct lttng_ht_node_u64 node;
215 /*
216 * Node indexed by UST session object descriptor (handle). Stored in the
217 * ust_sessions_objd hash table in the ust_app object.
218 */
219 struct lttng_ht_node_ulong ust_objd_node;
220 /* Starts with 'ust'; no leading slash. */
221 char path[PATH_MAX];
222 /* UID/GID of the application owning the session */
223 struct lttng_credentials real_credentials;
224 /* Effective UID and GID. Same as the tracing session. */
225 struct lttng_credentials effective_credentials;
226 struct cds_list_head teardown_node;
227 /*
228 * Once at least *one* session is created onto the application, the
229 * corresponding consumer is set so we can use it on unregistration.
230 */
231 struct consumer_output *consumer;
232 enum lttng_buffer_type buffer_type;
233 /* ABI of the session. Same value as the application. */
234 uint32_t bits_per_long;
235 /* For delayed reclaim */
236 struct rcu_head rcu_head;
237 /* If the channel's streams have to be outputed or not. */
238 unsigned int output_traces;
239 unsigned int live_timer_interval; /* usec */
240
241 /* Metadata channel attributes. */
242 struct lttng_ust_ctl_consumer_channel_attr metadata_attr;
243
244 char root_shm_path[PATH_MAX];
245 char shm_path[PATH_MAX];
246};
247
248/*
249 * Registered traceable applications. Libust registers to the session daemon
250 * and a linked list is kept of all running traceable app.
251 */
252struct ust_app {
253 /*
254 * The lifetime of 'sock' holds a reference to the application; the
255 * application management thread will release a reference to the
256 * application if the application dies.
257 */
258 struct urcu_ref ref;
259 int sock;
260 pthread_mutex_t sock_lock; /* Protects sock protocol. */
261
262 int notify_sock;
263 pid_t pid;
264 pid_t ppid;
265 uid_t uid; /* User ID that owns the apps */
266 gid_t gid; /* Group ID that owns the apps */
267
268 /* App ABI */
269 uint32_t bits_per_long;
270 uint32_t uint8_t_alignment;
271 uint32_t uint16_t_alignment;
272 uint32_t uint32_t_alignment;
273 uint32_t uint64_t_alignment;
274 uint32_t long_alignment;
275 int byte_order; /* BIG_ENDIAN or LITTLE_ENDIAN */
276
277 int compatible; /* If the lttng-ust tracer version does not match the
278 supported version of the session daemon, this flag is
279 set to 0 (NOT compatible) else 1. */
280 struct lttng_ust_abi_tracer_version version;
281 uint32_t v_major; /* Version major number */
282 uint32_t v_minor; /* Version minor number */
283 /* Extra for the NULL byte. */
284 char name[UST_APP_PROCNAME_LEN + 1];
285 /* Type of buffer this application uses. */
286 enum lttng_buffer_type buffer_type;
287 struct lttng_ht *sessions;
288 struct lttng_ht_node_ulong pid_n;
289 struct lttng_ht_node_ulong sock_n;
290 struct lttng_ht_node_ulong notify_sock_n;
291 /*
292 * This is a list of ust app session that, once the app is going into
293 * teardown mode, in the RCU call, each node in this list is removed and
294 * deleted.
295 *
296 * Element of the list are added when an application unregisters after each
297 * ht_del of ust_app_session associated to this app. This list is NOT used
298 * when a session is destroyed.
299 */
300 struct cds_list_head teardown_head;
301 /*
302 * Hash table containing ust_app_channel indexed by channel objd.
303 */
304 struct lttng_ht *ust_objd;
305 /*
306 * Hash table containing ust_app_session indexed by objd.
307 */
308 struct lttng_ht *ust_sessions_objd;
309
310 /*
311 * If this application is of the agent domain and this is non negative then
312 * a lookup MUST be done to acquire a read side reference to the
313 * corresponding agent app object. If the lookup fails, this should be set
314 * to a negative value indicating that the agent application is gone.
315 */
316 int agent_app_sock;
317 /*
318 * Time at which the app is registred.
319 * Used for path creation
320 */
321 time_t registration_time;
322 /*
323 * Event notifier
324 */
325 struct {
326 /*
327 * Handle to the lttng_ust object representing the event
328 * notifier group.
329 */
330 struct lttng_ust_abi_object_data *object;
331 struct lttng_pipe *event_pipe;
332 struct lttng_ust_abi_object_data *counter;
333 struct lttng_ust_abi_object_data **counter_cpu;
334 int nr_counter_cpu;
335 } event_notifier_group;
336 /*
337 * Hashtable indexing the application's event notifier rule's
338 * (ust_app_event_notifier_rule) by their token's value.
339 */
340 struct lttng_ht *token_to_event_notifier_rule_ht;
341};
342
343#ifdef HAVE_LIBLTTNG_UST_CTL
344
345int ust_app_register(struct ust_register_msg *msg, int sock);
346int ust_app_register_done(struct ust_app *app);
347int ust_app_version(struct ust_app *app);
348void ust_app_unregister_by_socket(int sock);
349int ust_app_start_trace_all(struct ltt_ust_session *usess);
350int ust_app_stop_trace_all(struct ltt_ust_session *usess);
351int ust_app_destroy_trace_all(struct ltt_ust_session *usess);
352int ust_app_list_events(struct lttng_event **events);
353int ust_app_list_event_fields(struct lttng_event_field **fields);
354int ust_app_create_event_glb(struct ltt_ust_session *usess,
355 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent);
356int ust_app_disable_channel_glb(struct ltt_ust_session *usess,
357 struct ltt_ust_channel *uchan);
358int ust_app_enable_channel_glb(struct ltt_ust_session *usess,
359 struct ltt_ust_channel *uchan);
360int ust_app_enable_event_glb(struct ltt_ust_session *usess,
361 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent);
362int ust_app_disable_event_glb(struct ltt_ust_session *usess,
363 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent);
364int ust_app_add_ctx_channel_glb(struct ltt_ust_session *usess,
365 struct ltt_ust_channel *uchan, struct ltt_ust_context *uctx);
366void ust_app_global_update(struct ltt_ust_session *usess, struct ust_app *app);
367void ust_app_global_update_all(struct ltt_ust_session *usess);
368void ust_app_global_update_event_notifier_rules(struct ust_app *app);
369void ust_app_global_update_all_event_notifier_rules(void);
370
371void ust_app_clean_list(void);
372int ust_app_ht_alloc(void);
373struct ust_app *ust_app_find_by_pid(pid_t pid);
374struct ust_app_stream *ust_app_alloc_stream(void);
375int ust_app_recv_registration(int sock, struct ust_register_msg *msg);
376int ust_app_recv_notify(int sock);
377void ust_app_add(struct ust_app *app);
378struct ust_app *ust_app_create(struct ust_register_msg *msg, int sock);
379void ust_app_notify_sock_unregister(int sock);
380ssize_t ust_app_push_metadata(struct ust_registry_session *registry,
381 struct consumer_socket *socket, int send_zero_data);
382enum lttng_error_code ust_app_snapshot_record(
383 const struct ltt_ust_session *usess,
384 const struct consumer_output *output, int wait,
385 uint64_t nb_packets_per_stream);
386uint64_t ust_app_get_size_one_more_packet_per_stream(
387 const struct ltt_ust_session *usess, uint64_t cur_nr_packets);
388struct ust_app *ust_app_find_by_sock(int sock);
389int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id,
390 struct cds_list_head *buffer_reg_uid_list,
391 struct consumer_output *consumer, uint64_t uchan_id,
392 int overwrite, uint64_t *discarded, uint64_t *lost);
393int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session *usess,
394 struct ltt_ust_channel *uchan,
395 struct consumer_output *consumer,
396 int overwrite, uint64_t *discarded, uint64_t *lost);
397int ust_app_regenerate_statedump_all(struct ltt_ust_session *usess);
398enum lttng_error_code ust_app_rotate_session(struct ltt_session *session);
399enum lttng_error_code ust_app_create_channel_subdirectories(
400 const struct ltt_ust_session *session);
401int ust_app_release_object(struct ust_app *app,
402 struct lttng_ust_abi_object_data *data);
403enum lttng_error_code ust_app_clear_session(struct ltt_session *session);
404enum lttng_error_code ust_app_open_packets(struct ltt_session *session);
405
406int ust_app_setup_event_notifier_group(struct ust_app *app);
407
408static inline
409int ust_app_supported(void)
410{
411 return 1;
412}
413
414bool ust_app_supports_notifiers(const struct ust_app *app);
415bool ust_app_supports_counters(const struct ust_app *app);
416
417bool ust_app_get(struct ust_app *app);
418void ust_app_put(struct ust_app *app);
419
420#else /* HAVE_LIBLTTNG_UST_CTL */
421
422static inline
423int ust_app_destroy_trace_all(struct ltt_ust_session *usess)
424{
425 return 0;
426}
427static inline
428int ust_app_start_trace(struct ltt_ust_session *usess, struct ust_app *app)
429{
430 return 0;
431}
432static inline
433int ust_app_start_trace_all(struct ltt_ust_session *usess)
434{
435 return 0;
436}
437static inline
438int ust_app_stop_trace_all(struct ltt_ust_session *usess)
439{
440 return 0;
441}
442static inline
443int ust_app_list_events(struct lttng_event **events)
444{
445 return -ENOSYS;
446}
447static inline
448int ust_app_list_event_fields(struct lttng_event_field **fields)
449{
450 return -ENOSYS;
451}
452static inline
453int ust_app_register(struct ust_register_msg *msg, int sock)
454{
455 return -ENOSYS;
456}
457static inline
458int ust_app_register_done(struct ust_app *app)
459{
460 return -ENOSYS;
461}
462static inline
463int ust_app_version(struct ust_app *app)
464{
465 return -ENOSYS;
466}
467static inline
468void ust_app_unregister_by_socket(int sock)
469{
470}
471static inline
472void ust_app_lock_list(void)
473{
474}
475static inline
476void ust_app_unlock_list(void)
477{
478}
479static inline
480void ust_app_clean_list(void)
481{
482}
483static inline
484struct ust_app_list *ust_app_get_list(void)
485{
486 return NULL;
487}
488static inline
489struct ust_app *ust_app_get_by_pid(pid_t pid)
490{
491 return NULL;
492}
493static inline
494int ust_app_ht_alloc(void)
495{
496 return 0;
497}
498static inline
499void ust_app_global_update(struct ltt_ust_session *usess, struct ust_app *app)
500{}
501static inline
502void ust_app_global_update_event_notifier_rules(struct ust_app *app)
503{}
504static inline
505void ust_app_global_update_all_event_notifier_rules(void)
506{}
507static inline
508int ust_app_setup_event_notifier_group(struct ust_app *app)
509{
510 return 0;
511}
512static inline
513int ust_app_disable_channel_glb(struct ltt_ust_session *usess,
514 struct ltt_ust_channel *uchan)
515{
516 return 0;
517}
518static inline
519int ust_app_enable_channel_glb(struct ltt_ust_session *usess,
520 struct ltt_ust_channel *uchan)
521{
522 return 0;
523}
524static inline
525int ust_app_create_event_glb(struct ltt_ust_session *usess,
526 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
527{
528 return 0;
529}
530static inline
531int ust_app_disable_event_glb(struct ltt_ust_session *usess,
532 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
533{
534 return 0;
535}
536static inline
537int ust_app_enable_event_glb(struct ltt_ust_session *usess,
538 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent)
539{
540 return 0;
541}
542static inline
543int ust_app_add_ctx_channel_glb(struct ltt_ust_session *usess,
544 struct ltt_ust_channel *uchan, struct ltt_ust_context *uctx)
545{
546 return 0;
547}
548static inline
549int ust_app_enable_event_pid(struct ltt_ust_session *usess,
550 struct ltt_ust_channel *uchan, struct ltt_ust_event *uevent,
551 pid_t pid)
552{
553 return 0;
554}
555static inline
556int ust_app_recv_registration(int sock, struct ust_register_msg *msg)
557{
558 return 0;
559}
560static inline
561int ust_app_recv_notify(int sock)
562{
563 return 0;
564}
565static inline
566struct ust_app *ust_app_create(struct ust_register_msg *msg, int sock)
567{
568 return NULL;
569}
570static inline
571void ust_app_add(struct ust_app *app)
572{
573}
574static inline
575void ust_app_notify_sock_unregister(int sock)
576{
577}
578static inline
579ssize_t ust_app_push_metadata(struct ust_registry_session *registry,
580 struct consumer_socket *socket, int send_zero_data)
581{
582 return 0;
583}
584static inline
585enum lttng_error_code ust_app_snapshot_record(struct ltt_ust_session *usess,
586 const struct consumer_output *output, int wait, uint64_t max_stream_size)
587{
588 return 0;
589}
590static inline
591unsigned int ust_app_get_nb_stream(struct ltt_ust_session *usess)
592{
593 return 0;
594}
595static inline
596void ust_app_update_event_notifier_error_count(
597 struct lttng_trigger *lttng_trigger)
598{
599 return;
600}
601static inline
602int ust_app_supported(void)
603{
604 return 0;
605}
606static inline
607bool ust_app_supports_notifiers(const struct ust_app *app)
608{
609 return false;
610}
611static inline
612bool ust_app_supports_counters(const struct ust_app *app)
613{
614 return false;
615}
616static inline
617struct ust_app *ust_app_find_by_sock(int sock)
618{
619 return NULL;
620}
621static inline
622struct ust_app *ust_app_find_by_pid(pid_t pid)
623{
624 return NULL;
625}
626static inline
627uint64_t ust_app_get_size_one_more_packet_per_stream(
628 const struct ltt_ust_session *usess, uint64_t cur_nr_packets) {
629 return 0;
630}
631static inline
632int ust_app_uid_get_channel_runtime_stats(uint64_t ust_session_id,
633 struct cds_list_head *buffer_reg_uid_list,
634 struct consumer_output *consumer, int overwrite,
635 uint64_t uchan_id, uint64_t *discarded, uint64_t *lost)
636{
637 return 0;
638}
639
640static inline
641int ust_app_pid_get_channel_runtime_stats(struct ltt_ust_session *usess,
642 struct ltt_ust_channel *uchan,
643 struct consumer_output *consumer,
644 int overwrite, uint64_t *discarded, uint64_t *lost)
645{
646 return 0;
647}
648
649static inline
650int ust_app_regenerate_statedump_all(struct ltt_ust_session *usess)
651{
652 return 0;
653}
654
655static inline
656enum lttng_error_code ust_app_rotate_session(struct ltt_session *session)
657{
658 return 0;
659}
660
661static inline
662enum lttng_error_code ust_app_create_channel_subdirectories(
663 const struct ltt_ust_session *session)
664{
665 return 0;
666}
667
668static inline
669int ust_app_release_object(struct ust_app *app, struct lttng_ust_abi_object_data *data)
670{
671 return 0;
672}
673
674static inline
675enum lttng_error_code ust_app_clear_session(struct ltt_session *session)
676{
677 return 0;
678}
679
680static inline
681enum lttng_error_code ust_app_open_packets(struct ltt_session *session)
682{
683 return 0;
684}
685
686static inline
687void ust_app_get(struct ust_app *app)
688{
689}
690
691static inline
692void ust_app_put(struct ust_app *app)
693{
694}
695
696#endif /* HAVE_LIBLTTNG_UST_CTL */
697
698#endif /* _LTT_UST_APP_H */
This page took 0.024503 seconds and 4 git commands to generate.