Remove LTTNG_HIDDEN macro
[lttng-ust.git] / libringbuffer / shm.h
1 /*
2 * SPDX-License-Identifier: LGPL-2.1-only
3 *
4 * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
5 */
6
7 #ifndef _LIBRINGBUFFER_SHM_H
8 #define _LIBRINGBUFFER_SHM_H
9
10 #include <stddef.h>
11 #include <stdint.h>
12 #include <unistd.h>
13 #include <usterr-signal-safe.h>
14 #include <urcu/compiler.h>
15 #include "shm_types.h"
16
17 /* channel_handle_create - for UST. */
18 __attribute__((visibility("hidden")))
19 extern
20 struct lttng_ust_shm_handle *channel_handle_create(void *data,
21 uint64_t memory_map_size, int wakeup_fd);
22
23 /* channel_handle_add_stream - for UST. */
24 __attribute__((visibility("hidden")))
25 extern
26 int channel_handle_add_stream(struct lttng_ust_shm_handle *handle,
27 int shm_fd, int wakeup_fd, uint32_t stream_nr,
28 uint64_t memory_map_size);
29
30 __attribute__((visibility("hidden")))
31 unsigned int channel_handle_get_nr_streams(struct lttng_ust_shm_handle *handle);
32
33 __attribute__((visibility("hidden")))
34 extern
35 void channel_destroy(struct channel *chan, struct lttng_ust_shm_handle *handle,
36 int consumer);
37
38 /*
39 * Pointer dereferencing. We don't trust the shm_ref, so we validate
40 * both the index and offset with known boundaries.
41 *
42 * "shmp" and "shmp_index" guarantee that it's safe to use the pointer
43 * target type, even in the occurrence of shm_ref modification by an
44 * untrusted process having write access to the shm_ref. We return a
45 * NULL pointer if the ranges are invalid.
46 */
47 static inline
48 char *_shmp_offset(struct shm_object_table *table, struct shm_ref *ref,
49 size_t idx, size_t elem_size)
50 {
51 struct shm_object *obj;
52 size_t objindex, ref_offset;
53
54 objindex = (size_t) ref->index;
55 if (caa_unlikely(objindex >= table->allocated_len))
56 return NULL;
57 obj = &table->objects[objindex];
58 ref_offset = (size_t) ref->offset;
59 ref_offset += idx * elem_size;
60 /* Check if part of the element returned would exceed the limits. */
61 if (caa_unlikely(ref_offset + elem_size > obj->memory_map_size))
62 return NULL;
63 return &obj->memory_map[ref_offset];
64 }
65
66 #define shmp_index(handle, ref, index) \
67 ({ \
68 __typeof__((ref)._type) ____ptr_ret; \
69 ____ptr_ret = (__typeof__(____ptr_ret)) _shmp_offset((handle)->table, &(ref)._ref, index, sizeof(*____ptr_ret)); \
70 ____ptr_ret; \
71 })
72
73 #define shmp(handle, ref) shmp_index(handle, ref, 0)
74
75 static inline
76 void _set_shmp(struct shm_ref *ref, struct shm_ref src)
77 {
78 *ref = src;
79 }
80
81 #define set_shmp(ref, src) _set_shmp(&(ref)._ref, src)
82
83 __attribute__((visibility("hidden")))
84 struct shm_object_table *shm_object_table_create(size_t max_nb_obj);
85
86 __attribute__((visibility("hidden")))
87 struct shm_object *shm_object_table_alloc(struct shm_object_table *table,
88 size_t memory_map_size,
89 enum shm_object_type type,
90 const int stream_fd,
91 int cpu);
92
93 __attribute__((visibility("hidden")))
94 struct shm_object *shm_object_table_append_shm(struct shm_object_table *table,
95 int shm_fd, int wakeup_fd, uint32_t stream_nr,
96 size_t memory_map_size);
97
98 /* mem ownership is passed to shm_object_table_append_mem(). */
99 __attribute__((visibility("hidden")))
100 struct shm_object *shm_object_table_append_mem(struct shm_object_table *table,
101 void *mem, size_t memory_map_size, int wakeup_fd);
102
103 __attribute__((visibility("hidden")))
104 void shm_object_table_destroy(struct shm_object_table *table, int consumer);
105
106 /*
107 * zalloc_shm - allocate memory within a shm object.
108 *
109 * Shared memory is already zeroed by shmget.
110 * *NOT* multithread-safe (should be protected by mutex).
111 * Returns a -1, -1 tuple on error.
112 */
113 __attribute__((visibility("hidden")))
114 struct shm_ref zalloc_shm(struct shm_object *obj, size_t len);
115
116 __attribute__((visibility("hidden")))
117 void align_shm(struct shm_object *obj, size_t align);
118
119 static inline
120 int shm_get_wait_fd(struct lttng_ust_shm_handle *handle, struct shm_ref *ref)
121 {
122 struct shm_object_table *table = handle->table;
123 struct shm_object *obj;
124 size_t index;
125
126 index = (size_t) ref->index;
127 if (caa_unlikely(index >= table->allocated_len))
128 return -EPERM;
129 obj = &table->objects[index];
130 return obj->wait_fd[0];
131 }
132
133 static inline
134 int shm_get_wakeup_fd(struct lttng_ust_shm_handle *handle, struct shm_ref *ref)
135 {
136 struct shm_object_table *table = handle->table;
137 struct shm_object *obj;
138 size_t index;
139
140 index = (size_t) ref->index;
141 if (caa_unlikely(index >= table->allocated_len))
142 return -EPERM;
143 obj = &table->objects[index];
144 return obj->wait_fd[1];
145 }
146
147 static inline
148 int shm_close_wait_fd(struct lttng_ust_shm_handle *handle,
149 struct shm_ref *ref)
150 {
151 struct shm_object_table *table = handle->table;
152 struct shm_object *obj;
153 int wait_fd;
154 size_t index;
155 int ret;
156
157 index = (size_t) ref->index;
158 if (caa_unlikely(index >= table->allocated_len))
159 return -EPERM;
160 obj = &table->objects[index];
161 wait_fd = obj->wait_fd[0];
162 if (wait_fd < 0)
163 return -ENOENT;
164 obj->wait_fd[0] = -1;
165 ret = close(wait_fd);
166 if (ret) {
167 ret = -errno;
168 return ret;
169 }
170 return 0;
171 }
172
173 static inline
174 int shm_close_wakeup_fd(struct lttng_ust_shm_handle *handle,
175 struct shm_ref *ref)
176 {
177 struct shm_object_table *table = handle->table;
178 struct shm_object *obj;
179 int wakeup_fd;
180 size_t index;
181 int ret;
182
183 index = (size_t) ref->index;
184 if (caa_unlikely(index >= table->allocated_len))
185 return -EPERM;
186 obj = &table->objects[index];
187 wakeup_fd = obj->wait_fd[1];
188 if (wakeup_fd < 0)
189 return -ENOENT;
190 obj->wait_fd[1] = -1;
191 ret = close(wakeup_fd);
192 if (ret) {
193 ret = -errno;
194 return ret;
195 }
196 return 0;
197 }
198
199 static inline
200 int shm_get_shm_fd(struct lttng_ust_shm_handle *handle, struct shm_ref *ref)
201 {
202 struct shm_object_table *table = handle->table;
203 struct shm_object *obj;
204 size_t index;
205
206 index = (size_t) ref->index;
207 if (caa_unlikely(index >= table->allocated_len))
208 return -EPERM;
209 obj = &table->objects[index];
210 return obj->shm_fd;
211 }
212
213
214 static inline
215 int shm_get_shm_size(struct lttng_ust_shm_handle *handle, struct shm_ref *ref,
216 uint64_t *size)
217 {
218 struct shm_object_table *table = handle->table;
219 struct shm_object *obj;
220 size_t index;
221
222 index = (size_t) ref->index;
223 if (caa_unlikely(index >= table->allocated_len))
224 return -EPERM;
225 obj = &table->objects[index];
226 *size = obj->memory_map_size;
227 return 0;
228 }
229
230 #endif /* _LIBRINGBUFFER_SHM_H */
This page took 0.033563 seconds and 4 git commands to generate.