bb678fe4da4c75763759c9b2e6e1fc5aeb213b54
[lttng-tools.git] / src / bin / lttng-sessiond / ust-registry-session.cpp
1 /*
2 * Copyright (C) 2022 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 */
7
8 #include "field.hpp"
9 #include "lttng-sessiond.hpp"
10 #include "notification-thread-commands.hpp"
11 #include "session.hpp"
12 #include "trace-class.hpp"
13 #include "tsdl-trace-class-visitor.hpp"
14 #include "ust-app.hpp"
15 #include "ust-field-convert.hpp"
16 #include "ust-registry.hpp"
17
18 #include <common/compat/directory-handle.hpp>
19 #include <common/error.hpp>
20 #include <common/exception.hpp>
21 #include <common/format.hpp>
22 #include <common/macros.hpp>
23 #include <common/make-unique.hpp>
24 #include <common/pthread-lock.hpp>
25 #include <common/runas.hpp>
26 #include <common/time.hpp>
27 #include <common/urcu.hpp>
28
29 #include <fcntl.h>
30 #include <functional>
31 #include <mutex>
32 #include <sstream>
33 #include <string>
34
35 namespace ls = lttng::sessiond;
36 namespace lst = lttng::sessiond::trace;
37 namespace lsu = lttng::sessiond::ust;
38
39 namespace {
40 lttng_uuid generate_uuid_or_throw()
41 {
42 lttng_uuid new_uuid;
43
44 if (lttng_uuid_generate(new_uuid)) {
45 LTTNG_THROW_POSIX("Failed to generate UST uuid", errno);
46 }
47
48 return new_uuid;
49 }
50
51 int get_count_order(unsigned int count)
52 {
53 int order;
54
55 order = lttng_fls(count) - 1;
56 if (count & (count - 1)) {
57 order++;
58 }
59
60 LTTNG_ASSERT(order >= 0);
61 return order;
62 }
63
64 void clear_metadata_file(int fd)
65 {
66 const auto lseek_ret = lseek(fd, 0, SEEK_SET);
67 if (lseek_ret < 0) {
68 LTTNG_THROW_POSIX("Failed to seek to the beginning of the metadata file while clearing it", errno);
69 }
70
71 const auto ret = ftruncate(fd, 0);
72 if (ret < 0) {
73 LTTNG_THROW_POSIX("Failed to truncate the metadata file while clearing it", errno);
74 }
75 }
76
77 /*
78 * Validate that the id has reached the maximum allowed or not.
79 */
80 bool is_max_channel_id(uint32_t id)
81 {
82 return id == UINT32_MAX;
83 }
84
85 void destroy_channel_rcu(struct rcu_head *head)
86 {
87 DIAGNOSTIC_PUSH
88 DIAGNOSTIC_IGNORE_INVALID_OFFSETOF
89 lsu::registry_channel *chan =
90 caa_container_of(head, lsu::registry_channel, _rcu_head);
91 DIAGNOSTIC_POP
92
93 delete chan;
94 }
95
96 /*
97 * Destroy every element of the registry and free the memory. This does NOT
98 * free the registry pointer since it might not have been allocated before so
99 * it's the caller responsability.
100 */
101 void destroy_channel(lsu::registry_channel *chan, bool notify)
102 {
103 struct lttng_ht_iter iter;
104 lttng::sessiond::ust::registry_event *event;
105 enum lttng_error_code cmd_ret;
106
107 LTTNG_ASSERT(chan);
108
109 if (notify) {
110 cmd_ret = notification_thread_command_remove_channel(
111 the_notification_thread_handle,
112 chan->_consumer_key, LTTNG_DOMAIN_UST);
113 if (cmd_ret != LTTNG_OK) {
114 ERR("Failed to remove channel from notification thread");
115 }
116 }
117
118 if (chan->_events) {
119 lttng::urcu::read_lock_guard read_lock_guard;
120
121 /* Destroy all event associated with this registry. */
122 DIAGNOSTIC_PUSH
123 DIAGNOSTIC_IGNORE_INVALID_OFFSETOF
124 cds_lfht_for_each_entry(
125 chan->_events->ht, &iter.iter, event, _node.node) {
126 /* Delete the node from the ht and free it. */
127 ust_registry_channel_destroy_event(chan, event);
128 }
129 DIAGNOSTIC_POP
130 }
131
132 call_rcu(&chan->_rcu_head, destroy_channel_rcu);
133 }
134 } /* namespace */
135
136 void lsu::details::locked_registry_session_release(lsu::registry_session *session)
137 {
138 pthread_mutex_unlock(&session->_lock);
139 }
140
141 lsu::registry_session::registry_session(const struct lst::abi& in_abi,
142 uint32_t major,
143 uint32_t minor,
144 const char *root_shm_path,
145 const char *shm_path,
146 uid_t euid,
147 gid_t egid,
148 uint64_t tracing_id) :
149 lst::trace_class(in_abi, generate_uuid_or_throw()),
150 _uid{euid},
151 _gid{egid},
152 _app_tracer_version_major{major},
153 _app_tracer_version_minor{minor},
154 _tracing_id{tracing_id},
155 _metadata_generating_visitor{lttng::make_unique<ls::tsdl::trace_class_visitor>(
156 abi, [this](const std::string& fragment) {
157 _append_metadata_fragment(fragment);
158 })}
159 {
160 pthread_mutex_init(&_lock, NULL);
161 strncpy(_root_shm_path, root_shm_path, sizeof(_root_shm_path));
162 _root_shm_path[sizeof(_root_shm_path) - 1] = '\0';
163 if (shm_path[0]) {
164 strncpy(_shm_path, shm_path, sizeof(_shm_path));
165 _shm_path[sizeof(_shm_path) - 1] = '\0';
166 strncpy(_metadata_path, shm_path, sizeof(_metadata_path));
167 _metadata_path[sizeof(_metadata_path) - 1] = '\0';
168 strncat(_metadata_path, "/metadata",
169 sizeof(_metadata_path) - strlen(_metadata_path) - 1);
170 }
171
172 if (_shm_path[0]) {
173 if (run_as_mkdir_recursive(_shm_path, S_IRWXU | S_IRWXG, euid, egid)) {
174 LTTNG_THROW_POSIX("run_as_mkdir_recursive", errno);
175 }
176 }
177
178 if (_metadata_path[0]) {
179 /* Create metadata file. */
180 const int ret = run_as_open(_metadata_path, O_WRONLY | O_CREAT | O_EXCL,
181 S_IRUSR | S_IWUSR, euid, egid);
182
183 if (ret < 0) {
184 std::stringstream ss;
185
186 ss << "Opening metadata file '" << _metadata_path << "'";
187 LTTNG_THROW_POSIX(ss.str(), errno);
188 }
189
190 _metadata_fd = ret;
191 }
192
193 _enums.reset(lttng_ht_new(0, LTTNG_HT_TYPE_STRING));
194 if (!_enums) {
195 LTTNG_THROW_POSIX("Failed to create enums hash table", ENOMEM);
196 }
197
198 /* hash/match functions are specified at call site. */
199 _enums->match_fct = NULL;
200 _enums->hash_fct = NULL;
201
202 _channels.reset(lttng_ht_new(0, LTTNG_HT_TYPE_U64));
203 if (!_channels) {
204 LTTNG_THROW_POSIX("Failed to create channels hash table", ENOMEM);
205 }
206 }
207
208 lsu::registry_session::~registry_session()
209 {
210 int ret;
211 struct lttng_ht_iter iter;
212 lsu::registry_channel *chan;
213 lsu::registry_enum *reg_enum;
214
215 /* On error, EBUSY can be returned if lock. Code flow error. */
216 ret = pthread_mutex_destroy(&_lock);
217 LTTNG_ASSERT(!ret);
218
219 if (_channels) {
220 lttng::urcu::read_lock_guard read_lock_guard;
221
222 /* Destroy all event associated with this registry. */
223 DIAGNOSTIC_PUSH
224 DIAGNOSTIC_IGNORE_INVALID_OFFSETOF
225 cds_lfht_for_each_entry(_channels->ht, &iter.iter, chan, _node.node) {
226 /* Delete the node from the ht and free it. */
227 ret = lttng_ht_del(_channels.get(), &iter);
228 LTTNG_ASSERT(!ret);
229 destroy_channel(chan, true);
230 }
231 DIAGNOSTIC_POP
232 }
233
234 free(_metadata);
235 if (_metadata_fd >= 0) {
236 ret = close(_metadata_fd);
237 if (ret) {
238 PERROR("close");
239 }
240
241 ret = run_as_unlink(_metadata_path, _uid, _gid);
242 if (ret) {
243 PERROR("unlink");
244 }
245 }
246
247 if (_root_shm_path[0]) {
248 /* Try to delete the directory hierarchy. */
249 (void) run_as_rmdir_recursive(_root_shm_path, _uid, _gid,
250 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG);
251 }
252
253 /* Destroy the enum hash table */
254 if (_enums) {
255 rcu_read_lock();
256 /* Destroy all enum entries associated with this registry. */
257 DIAGNOSTIC_PUSH
258 DIAGNOSTIC_IGNORE_INVALID_OFFSETOF
259 cds_lfht_for_each_entry (_enums->ht, &iter.iter, reg_enum, node.node) {
260 ust_registry_destroy_enum(this, reg_enum);
261 }
262 DIAGNOSTIC_POP
263
264 rcu_read_unlock();
265 }
266 }
267
268 lsu::registry_session::locked_ptr lsu::registry_session::lock()
269 {
270 pthread_mutex_lock(&_lock);
271 return locked_ptr(this);
272 }
273
274 /*
275 * Initialize registry with default values.
276 */
277 void lsu::registry_session::add_channel(uint64_t key)
278 {
279 lttng::pthread::lock_guard session_lock_guard(_lock);
280
281 /*
282 * Assign a channel ID right now since the event notification comes
283 * *before* the channel notify so the ID needs to be set at this point so
284 * the metadata can be dumped for that event.
285 */
286 if (is_max_channel_id(_used_channel_id)) {
287 LTTNG_THROW_ERROR(fmt::format("Failed to allocate unique id for channel under session while adding channel"));
288 }
289
290 auto chan = new lsu::registry_channel(
291 _get_next_channel_id(),
292 /* Registered channel listener. */
293 [this](const lsu::registry_channel& registered_channel) {
294 /*
295 * Channel registration completed, serialize it's layout's
296 * description.
297 */
298 registered_channel.accept(*_metadata_generating_visitor);
299 },
300 /* Added event listener. */
301 [this](const lsu::registry_channel& channel,
302 const lsu::registry_event& added_event) {
303 /*
304 * The channel and its event classes will be dumped at once when
305 * it is registered. This check prevents event classes from being
306 * declared before their stream class.
307 */
308 if (channel.is_registered()) {
309 added_event.accept(*_metadata_generating_visitor);
310 }
311 });
312
313 lttng::urcu::read_lock_guard rcu_read_lock_guard;
314 lttng_ht_node_init_u64(&chan->_node, key);
315 lttng_ht_add_unique_u64(_channels.get(), &chan->_node);
316 }
317
318 lttng::sessiond::ust::registry_channel& lsu::registry_session::get_channel(
319 uint64_t channel_key) const
320 {
321 lttng::urcu::read_lock_guard read_lock_guard;
322 struct lttng_ht_node_u64 *node;
323 struct lttng_ht_iter iter;
324
325 ASSERT_LOCKED(_lock);
326
327 lttng_ht_lookup(_channels.get(), &channel_key, &iter);
328 node = lttng_ht_iter_get_node_u64(&iter);
329 if (!node) {
330 LTTNG_THROW_INVALID_ARGUMENT_ERROR(fmt::format(
331 "Invalid channel key provided: channel key = {}", channel_key));
332 }
333
334 DIAGNOSTIC_PUSH
335 DIAGNOSTIC_IGNORE_INVALID_OFFSETOF
336 auto chan = caa_container_of(node, lsu::registry_channel, _node);
337 DIAGNOSTIC_POP
338 return *chan;
339 }
340
341 void lsu::registry_session::remove_channel(uint64_t channel_key, bool notify)
342 {
343 struct lttng_ht_iter iter;
344 int ret;
345 lttng::urcu::read_lock_guard read_lock_guard;
346
347 ASSERT_LOCKED(_lock);
348 auto& channel = get_channel(channel_key);
349
350 iter.iter.node = &channel._node.node;
351 ret = lttng_ht_del(_channels.get(), &iter);
352 LTTNG_ASSERT(!ret);
353 destroy_channel(&channel, notify);
354 }
355
356 void lsu::registry_session::_visit_environment(
357 lttng::sessiond::trace::trace_class_visitor& visitor) const
358 {
359 ASSERT_LOCKED(_lock);
360
361 visitor.visit(lst::environment_field<const char *>("domain", "ust"));
362 visitor.visit(lst::environment_field<const char *>("tracer_name", "lttng-ust"));
363 visitor.visit(lst::environment_field<int64_t>("tracer_major", _app_tracer_version_major));
364 visitor.visit(lst::environment_field<int64_t>("tracer_minor", _app_tracer_version_minor));
365 visitor.visit(lst::environment_field<const char *>("tracer_buffering_scheme",
366 get_buffering_scheme() == LTTNG_BUFFER_PER_PID ? "pid" : "uid"));
367 visitor.visit(lst::environment_field<int64_t>("architecture_bit_width", abi.bits_per_long));
368
369 {
370 /* The caller already holds the session and session list locks. */
371 ASSERT_SESSION_LIST_LOCKED();
372 const auto session = lttng::sessiond::find_session_by_id(_tracing_id);
373
374 LTTNG_ASSERT(session);
375 ASSERT_LOCKED(session->lock);
376
377 visitor.visit(lst::environment_field<const char *>("trace_name",
378 session->has_auto_generated_name ? DEFAULT_SESSION_NAME :
379 session->name));
380 visitor.visit(lst::environment_field<std::string>("trace_creation_datetime",
381 lttng::utils::time_to_iso8601_str(session->creation_time)));
382 visitor.visit(lst::environment_field<const char *>("hostname", session->hostname));
383 }
384 }
385
386 void lsu::registry_session::_accept_on_clock_classes(lst::trace_class_visitor& visitor) const
387 {
388 ASSERT_LOCKED(_lock);
389 _clock.accept(visitor);
390 }
391
392 void lsu::registry_session::_accept_on_stream_classes(lst::trace_class_visitor& visitor) const
393 {
394 ASSERT_LOCKED(_lock);
395
396 std::vector<const lttng::sessiond::ust::registry_channel *> sorted_stream_classes;
397
398 {
399 lttng::urcu::read_lock_guard rcu_lock_guard;
400 const lsu::registry_channel *channel;
401 lttng_ht_iter channel_it;
402
403 DIAGNOSTIC_PUSH
404 DIAGNOSTIC_IGNORE_INVALID_OFFSETOF
405 cds_lfht_for_each_entry(_channels->ht, &channel_it.iter, channel, _node.node) {
406 sorted_stream_classes.emplace_back(channel);
407 }
408 DIAGNOSTIC_POP
409 }
410
411 std::sort(sorted_stream_classes.begin(), sorted_stream_classes.end(),
412 [](const lttng::sessiond::ust::registry_channel *a,
413 const lttng::sessiond::ust::registry_channel *b) {
414 return a->id < b->id;
415 });
416
417 for (const auto stream_class : sorted_stream_classes) {
418 stream_class->accept(visitor);
419 }
420 }
421
422 /*
423 * Return next available channel id and increment the used counter. The
424 * is_max_channel_id function MUST be called before in order to validate
425 * if the maximum number of IDs have been reached. If not, it is safe to call
426 * this function.
427 *
428 * Return a unique channel ID. If max is reached, the used_channel_id counter
429 * is returned.
430 */
431 uint32_t lsu::registry_session::_get_next_channel_id()
432 {
433 if (is_max_channel_id(_used_channel_id)) {
434 return _used_channel_id;
435 }
436
437 _used_channel_id++;
438 return _next_channel_id++;
439 }
440
441 void lsu::registry_session::_increase_metadata_size(size_t reservation_length)
442 {
443 const auto new_len = _metadata_len + reservation_length;
444 auto new_alloc_len = new_len;
445 const auto old_alloc_len = _metadata_alloc_len;
446
447 /* Rounding the new allocation length to the next power of 2 would overflow. */
448 if (new_alloc_len > (UINT32_MAX >> 1)) {
449 LTTNG_THROW_ERROR("Failed to reserve trace metadata storage as the new size would overflow");
450 }
451
452 /* The current allocation length is already the largest we can afford. */
453 if ((old_alloc_len << 1) > (UINT32_MAX >> 1)) {
454 LTTNG_THROW_ERROR("Failed to reserve trace metadata storage as the max size was already reached");
455 }
456
457 if (new_alloc_len > old_alloc_len) {
458 new_alloc_len = std::max<size_t>(
459 1U << get_count_order(new_alloc_len), old_alloc_len << 1);
460
461 auto newptr = (char *) realloc(_metadata, new_alloc_len);
462 if (!newptr) {
463 LTTNG_THROW_POSIX("Failed to allocate trace metadata storage", errno);
464 }
465
466 _metadata = newptr;
467
468 /* We zero directly the memory from start of allocation. */
469 memset(&_metadata[old_alloc_len], 0, new_alloc_len - old_alloc_len);
470 _metadata_alloc_len = new_alloc_len;
471 }
472
473 _metadata_len += reservation_length;
474 }
475
476 void lsu::registry_session::_append_metadata_fragment(const std::string& fragment)
477 {
478 const auto offset = _metadata_len;
479
480 _increase_metadata_size(fragment.size());
481 memcpy(&_metadata[offset], fragment.c_str(), fragment.size());
482
483 if (_metadata_fd >= 0) {
484 const auto bytes_written =
485 lttng_write(_metadata_fd, fragment.c_str(), fragment.size());
486
487 if (bytes_written != fragment.size()) {
488 LTTNG_THROW_POSIX("Failed to write trace metadata fragment to file",
489 errno);
490 }
491 }
492 }
493
494 void lsu::registry_session::_reset_metadata()
495 {
496 _metadata_len_sent = 0;
497 memset(_metadata, 0, _metadata_alloc_len);
498 _metadata_len = 0;
499
500 if (_metadata_fd > 0) {
501 /* Clear the metadata file's content. */
502 clear_metadata_file(_metadata_fd);
503 }
504 }
505
506 void lsu::registry_session::_generate_metadata()
507 {
508 accept(*_metadata_generating_visitor);
509 }
510
511 void lsu::registry_session::regenerate_metadata()
512 {
513 lttng::pthread::lock_guard registry_lock(_lock);
514
515 _metadata_version++;
516 _reset_metadata();
517 _generate_metadata();
518 }
This page took 0.04303 seconds and 3 git commands to generate.