2 * Copyright (C) 2022 Jérémie Galarneau <jeremie.galarneau@efficios.com>
4 * SPDX-License-Identifier: GPL-2.0-only
8 #include "ust-registry.hpp"
10 #include <common/compat/directory-handle.hpp>
11 #include <common/error.hpp>
12 #include <common/exception.hpp>
13 #include <common/runas.hpp>
19 ust_registry_session::ust_registry_session(uint32_t bits_per_long
,
20 uint32_t uint8_t_alignment
,
21 uint32_t uint16_t_alignment
,
22 uint32_t uint32_t_alignment
,
23 uint32_t uint64_t_alignment
,
24 uint32_t long_alignment
,
28 const char *root_shm_path
,
32 uint64_t tracing_id
) :
33 _bits_per_long
{bits_per_long
},
34 _uint8_t_alignment
{uint8_t_alignment
},
35 _uint16_t_alignment
{uint16_t_alignment
},
36 _uint32_t_alignment
{uint32_t_alignment
},
37 _uint64_t_alignment
{uint64_t_alignment
},
38 _long_alignment
{long_alignment
},
39 _byte_order
{byte_order
},
42 _app_tracer_version_major
{major
},
43 _app_tracer_version_minor
{minor
},
44 _tracing_id
{tracing_id
}
46 pthread_mutex_init(&_lock
, NULL
);
47 strncpy(_root_shm_path
, root_shm_path
, sizeof(_root_shm_path
));
48 _root_shm_path
[sizeof(_root_shm_path
) - 1] = '\0';
50 strncpy(_shm_path
, shm_path
, sizeof(_shm_path
));
51 _shm_path
[sizeof(_shm_path
) - 1] = '\0';
52 strncpy(_metadata_path
, shm_path
, sizeof(_metadata_path
));
53 _metadata_path
[sizeof(_metadata_path
) - 1] = '\0';
54 strncat(_metadata_path
, "/metadata",
55 sizeof(_metadata_path
) - strlen(_metadata_path
) - 1);
59 if (run_as_mkdir_recursive(_shm_path
, S_IRWXU
| S_IRWXG
, euid
, egid
)) {
60 LTTNG_THROW_POSIX("run_as_mkdir_recursive", errno
);
64 if (_metadata_path
[0]) {
65 /* Create metadata file. */
66 const int ret
= run_as_open(_metadata_path
, O_WRONLY
| O_CREAT
| O_EXCL
,
67 S_IRUSR
| S_IWUSR
, euid
, egid
);
72 ss
<< "Opening metadata file '" << _metadata_path
<< "'";
73 LTTNG_THROW_POSIX(ss
.str(), errno
);
79 _enums
.reset(lttng_ht_new(0, LTTNG_HT_TYPE_STRING
));
81 LTTNG_THROW_POSIX("Failed to create enums hash table", ENOMEM
);
84 /* hash/match functions are specified at call site. */
85 _enums
->match_fct
= NULL
;
86 _enums
->hash_fct
= NULL
;
88 _channels
.reset(lttng_ht_new(0, LTTNG_HT_TYPE_U64
));
90 LTTNG_THROW_POSIX("Failed to create channels hash table", ENOMEM
);
93 if (lttng_uuid_generate(_uuid
)) {
94 LTTNG_THROW_POSIX("Failed to generate UST uuid", errno
);
98 ust_registry_session::~ust_registry_session()
101 struct lttng_ht_iter iter
;
102 struct ust_registry_channel
*chan
;
103 struct ust_registry_enum
*reg_enum
;
105 /* On error, EBUSY can be returned if lock. Code flow error. */
106 ret
= pthread_mutex_destroy(&_lock
);
111 /* Destroy all event associated with this registry. */
112 cds_lfht_for_each_entry (_channels
->ht
, &iter
.iter
, chan
, node
.node
) {
113 /* Delete the node from the ht and free it. */
114 ret
= lttng_ht_del(_channels
.get(), &iter
);
116 ust_registry_channel_destroy(chan
, true);
123 if (_metadata_fd
>= 0) {
124 ret
= close(_metadata_fd
);
129 ret
= run_as_unlink(_metadata_path
, _uid
, _gid
);
135 if (_root_shm_path
[0]) {
136 /* Try to delete the directory hierarchy. */
137 (void) run_as_rmdir_recursive(_root_shm_path
, _uid
, _gid
,
138 LTTNG_DIRECTORY_HANDLE_SKIP_NON_EMPTY_FLAG
);
141 /* Destroy the enum hash table */
144 /* Destroy all enum entries associated with this registry. */
145 cds_lfht_for_each_entry (_enums
->ht
, &iter
.iter
, reg_enum
, node
.node
) {
146 ust_registry_destroy_enum(this, reg_enum
);
153 void ust_registry_session::statedump()
155 pthread_mutex_lock(&_lock
);
156 const int ret
= ust_metadata_session_statedump(this);
157 pthread_mutex_unlock(&_lock
);
160 "Failed to generate session metadata during registry session creation");