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