Fix: sessiond: null pointer dereference on initial evaluation of session
[lttng-tools.git] / src / bin / lttng-sessiond / session.hpp
1 /*
2 * Copyright (C) 2011 EfficiOS Inc.
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #ifndef _LTT_SESSION_H
9 #define _LTT_SESSION_H
10
11 #include <limits.h>
12 #include <stdbool.h>
13 #include <urcu/list.h>
14
15 #include <common/hashtable/hashtable.hpp>
16 #include <common/dynamic-array.hpp>
17 #include <common/make-unique-wrapper.hpp>
18 #include <common/pthread-lock.hpp>
19 #include <lttng/rotation.h>
20 #include <lttng/location.h>
21 #include <lttng/lttng-error.h>
22
23 #include "snapshot.hpp"
24 #include "trace-kernel.hpp"
25 #include "consumer.hpp"
26
27 #define ASSERT_SESSION_LIST_LOCKED() LTTNG_ASSERT(session_trylock_list())
28
29 struct ltt_ust_session;
30
31 typedef void (*ltt_session_destroy_notifier)(const struct ltt_session *session,
32 void *user_data);
33 typedef void (*ltt_session_clear_notifier)(const struct ltt_session *session,
34 void *user_data);
35
36 namespace lttng {
37 namespace sessiond {
38 namespace details {
39 void locked_session_release(ltt_session *session);
40 } /* namespace details */
41 } /* namespace sessiond */
42 } /* namespace lttng */
43
44 /*
45 * Tracing session list
46 *
47 * Statically declared in session.c and can be accessed by using
48 * session_get_list() function that returns the pointer to the list.
49 */
50 struct ltt_session_list {
51 /*
52 * This lock protects any read/write access to the list and
53 * next_uuid. All public functions in session.c acquire this
54 * lock and release it before returning. If none of those
55 * functions are used, the lock MUST be acquired in order to
56 * iterate or/and do any actions on that list.
57 */
58 pthread_mutex_t lock;
59 /*
60 * This condition variable is signaled on every removal from
61 * the session list.
62 */
63 pthread_cond_t removal_cond;
64
65 /*
66 * Session unique ID generator. The session list lock MUST be
67 * upon update and read of this counter.
68 */
69 uint64_t next_uuid;
70
71 /* Linked list head */
72 struct cds_list_head head;
73 };
74
75 /*
76 * This data structure contains information needed to identify a tracing
77 * session for both LTTng and UST.
78 */
79 struct ltt_session {
80 using id_t = uint64_t;
81 using locked_ptr = std::unique_ptr<ltt_session,
82 lttng::details::create_unique_class<ltt_session,
83 lttng::sessiond::details::locked_session_release>::deleter>;
84 using sptr = std::shared_ptr<ltt_session>;
85
86 char name[NAME_MAX];
87 bool has_auto_generated_name;
88 bool name_contains_creation_time;
89 char hostname[LTTNG_HOST_NAME_MAX]; /* Local hostname. */
90 /* Path of the last closed chunk. */
91 char last_chunk_path[LTTNG_PATH_MAX];
92 time_t creation_time;
93 struct ltt_kernel_session *kernel_session;
94 struct ltt_ust_session *ust_session;
95 struct urcu_ref ref;
96 /*
97 * Protect any read/write on this session data structure. This lock must be
98 * acquired *before* using any public functions declared below. Use
99 * session_lock() and session_unlock() for that.
100 */
101 pthread_mutex_t lock;
102 struct cds_list_head list;
103 /* session unique identifier */
104 id_t id;
105 /* Indicates if the session has been added to the session list and ht.*/
106 bool published;
107 /* Indicates if a destroy command has been applied to this session. */
108 bool destroyed;
109 /* UID/GID of the user owning the session */
110 uid_t uid;
111 gid_t gid;
112 /*
113 * Network session handle. A value of 0 means that there is no remote
114 * session established.
115 */
116 uint64_t net_handle;
117 /*
118 * This consumer is only set when the create_session_uri call is made.
119 * This contains the temporary information for a consumer output. Upon
120 * creation of the UST or kernel session, this consumer, if available, is
121 * copied into those sessions.
122 */
123 struct consumer_output *consumer;
124 /*
125 * Indicates whether or not the user has specified an output directory
126 * or if it was configured using the default configuration.
127 */
128 bool has_user_specified_directory;
129 /* Did at least ONE start command has been triggered?. */
130 unsigned int has_been_started:1;
131 /*
132 * Is the session active? Start trace command sets this to 1 and the stop
133 * command reset it to 0.
134 */
135 unsigned int active:1;
136
137 /* Snapshot representation in a session. */
138 struct snapshot snapshot;
139 /* Indicate if the session has to output the traces or not. */
140 unsigned int output_traces;
141 /*
142 * This session is in snapshot mode. This means that channels enabled
143 * will be set in overwrite mode by default and must be in mmap
144 * output mode. Note that snapshots can be taken on a session that
145 * is not in "snapshot_mode". This parameter only affects channel
146 * creation defaults.
147 */
148 unsigned int snapshot_mode;
149 /*
150 * A session that has channels that don't use 'mmap' output can't be
151 * used to capture snapshots. This is set to true whenever a
152 * 'splice' kernel channel is enabled.
153 */
154 bool has_non_mmap_channel;
155 /*
156 * Timer set when the session is created for live reading.
157 */
158 unsigned int live_timer;
159 /*
160 * Path where to keep the shared memory files.
161 */
162 char shm_path[PATH_MAX];
163 /*
164 * Node in ltt_sessions_ht_by_id.
165 */
166 struct lttng_ht_node_u64 node;
167 /*
168 * Node in ltt_sessions_ht_by_name.
169 */
170 struct lttng_ht_node_str node_by_name;
171 /*
172 * Timer to check periodically if a relay and/or consumer has completed
173 * the last rotation.
174 */
175 bool rotation_pending_check_timer_enabled;
176 timer_t rotation_pending_check_timer;
177 /* Timer to periodically rotate a session. */
178 bool rotation_schedule_timer_enabled;
179 timer_t rotation_schedule_timer;
180 /* Value for periodic rotations, 0 if disabled. */
181 uint64_t rotate_timer_period;
182 /* Value for size-based rotations, 0 if disabled. */
183 uint64_t rotate_size;
184 /*
185 * Keep a state if this session was rotated after the last stop command.
186 * We only allow one rotation after a stop. At destroy, we also need to
187 * know if a rotation occurred since the last stop to rename the current
188 * chunk. After a stop followed by rotate, all subsequent clear
189 * (without prior start) will succeed, but will be effect-less.
190 */
191 bool rotated_after_last_stop;
192 /*
193 * Track whether the session was cleared after last stop. All subsequent
194 * clear (without prior start) will succeed, but will be effect-less. A
195 * subsequent rotate (without prior start) will return an error.
196 */
197 bool cleared_after_last_stop;
198 /*
199 * True if the session has had an explicit non-quiet rotation.
200 */
201 bool rotated;
202 /*
203 * Trigger for size-based rotations.
204 */
205 struct lttng_trigger *rotate_trigger;
206 LTTNG_OPTIONAL(uint64_t) most_recent_chunk_id;
207 struct lttng_trace_chunk *current_trace_chunk;
208 struct lttng_trace_chunk *chunk_being_archived;
209 /* Current state of a rotation. */
210 enum lttng_rotation_state rotation_state;
211 bool quiet_rotation;
212 char *last_archived_chunk_name;
213 LTTNG_OPTIONAL(uint64_t) last_archived_chunk_id;
214 struct lttng_dynamic_array destroy_notifiers;
215 struct lttng_dynamic_array clear_notifiers;
216 /* Session base path override. Set non-null. */
217 char *base_path;
218 };
219
220 enum lttng_error_code session_create(const char *name, uid_t uid, gid_t gid,
221 struct ltt_session **out_session);
222 void session_lock(struct ltt_session *session);
223 void session_unlock(struct ltt_session *session);
224
225 /*
226 * The session list lock covers more ground than its name implies. While
227 * it does protect against concurent mutations of the session list, it is
228 * also used as a multi-session lock when synchronizing newly-registered
229 * 'user space tracer' and 'agent' applications.
230 *
231 * In other words, it prevents tracer configurations from changing while they
232 * are being transmitted to the various applications.
233 */
234 void session_lock_list(void);
235 int session_trylock_list(void);
236 void session_unlock_list(void);
237
238 void session_destroy(struct ltt_session *session);
239 int session_add_destroy_notifier(struct ltt_session *session,
240 ltt_session_destroy_notifier notifier, void *user_data);
241
242 int session_add_clear_notifier(struct ltt_session *session,
243 ltt_session_clear_notifier notifier, void *user_data);
244 void session_notify_clear(struct ltt_session *session);
245
246 bool session_get(struct ltt_session *session);
247 void session_put(struct ltt_session *session);
248
249 enum consumer_dst_type session_get_consumer_destination_type(
250 const struct ltt_session *session);
251 const char *session_get_net_consumer_hostname(
252 const struct ltt_session *session);
253 void session_get_net_consumer_ports(
254 const struct ltt_session *session,
255 uint16_t *control_port, uint16_t *data_port);
256 struct lttng_trace_archive_location *session_get_trace_archive_location(
257 const struct ltt_session *session);
258
259 struct ltt_session *session_find_by_name(const char *name);
260 struct ltt_session *session_find_by_id(ltt_session::id_t id);
261
262 struct ltt_session_list *session_get_list(void);
263 void session_list_wait_empty(void);
264
265 bool session_access_ok(struct ltt_session *session, uid_t uid);
266
267 int session_reset_rotation_state(struct ltt_session *session,
268 enum lttng_rotation_state result);
269
270 /* Create a new trace chunk object from the session's configuration. */
271 struct lttng_trace_chunk *session_create_new_trace_chunk(
272 const struct ltt_session *session,
273 const struct consumer_output *consumer_output_override,
274 const char *session_base_path_override,
275 const char *chunk_name_override);
276
277 /*
278 * Set `new_trace_chunk` as the session's current trace chunk. A reference
279 * to `new_trace_chunk` is acquired by the session. The chunk is created
280 * on remote peers (consumer and relay daemons).
281 *
282 * A reference to the session's current trace chunk is returned through
283 * `current_session_trace_chunk` on success.
284 */
285 int session_set_trace_chunk(struct ltt_session *session,
286 struct lttng_trace_chunk *new_trace_chunk,
287 struct lttng_trace_chunk **current_session_trace_chunk);
288
289 /*
290 * Close a chunk on the remote peers of a session. Has no effect on the
291 * ltt_session itself.
292 */
293 int session_close_trace_chunk(struct ltt_session *session,
294 struct lttng_trace_chunk *trace_chunk,
295 enum lttng_trace_chunk_command_type close_command,
296 char *path);
297
298 /* Open a packet in all channels of a given session. */
299 enum lttng_error_code session_open_packets(struct ltt_session *session);
300
301 bool session_output_supports_trace_chunks(const struct ltt_session *session);
302
303 /*
304 * Sample the id of a session looked up via its name.
305 * Here the term "sampling" hint the caller that this return the id at a given
306 * point in time with no guarantee that the session for which the id was
307 * sampled still exist at that point.
308 *
309 * Return 0 when the session is not found,
310 * Return 1 when the session is found and set `id`.
311 */
312 bool sample_session_id_by_name(const char *name, uint64_t *id);
313
314 namespace lttng {
315 namespace sessiond {
316
317 /*
318 * Session list lock must be acquired by the caller.
319 * The caller must not keep the ownership of the returned locked session
320 * for longer than strictly necessary. If your intention is to acquire
321 * a reference to an ltt_session, see `find_session_by_id()`.
322 */
323 ltt_session::locked_ptr find_locked_session_by_id(ltt_session::id_t id);
324
325 /*
326 * Session list lock must be acquired by the caller.
327 * The caller must not keep the ownership of the returned locked session
328 * for longer than strictly necessary. If your intention is to acquire
329 * a reference to an ltt_session, see `find_session_by_id()`.
330 */
331 ltt_session::sptr find_session_by_id(ltt_session::id_t id);
332
333 } /* namespace sessiond */
334 } /* namespace lttng */
335
336 #endif /* _LTT_SESSION_H */
This page took 0.035933 seconds and 4 git commands to generate.