Commit | Line | Data |
---|---|---|
a6352fd4 | 1 | /* |
c0c0989a | 2 | * SPDX-License-Identifier: LGPL-2.1-only |
a6352fd4 | 3 | * |
e92f3e28 | 4 | * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
a6352fd4 MD |
5 | */ |
6 | ||
c0c0989a MJ |
7 | #ifndef _LIBRINGBUFFER_SHM_H |
8 | #define _LIBRINGBUFFER_SHM_H | |
9 | ||
b4051ad8 | 10 | #include <stddef.h> |
a6352fd4 | 11 | #include <stdint.h> |
4e79769f | 12 | #include <unistd.h> |
44c72f10 | 13 | #include <usterr-signal-safe.h> |
35897f8b | 14 | #include <urcu/compiler.h> |
1d498196 | 15 | #include "shm_types.h" |
a6352fd4 | 16 | |
74d81a6c | 17 | /* channel_handle_create - for UST. */ |
ddabe860 | 18 | __attribute__((visibility("hidden"))) |
74d81a6c MD |
19 | extern |
20 | struct lttng_ust_shm_handle *channel_handle_create(void *data, | |
ff0f5728 | 21 | uint64_t memory_map_size, int wakeup_fd); |
ddabe860 | 22 | |
74d81a6c | 23 | /* channel_handle_add_stream - for UST. */ |
ddabe860 | 24 | __attribute__((visibility("hidden"))) |
74d81a6c MD |
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); | |
ddabe860 MJ |
29 | |
30 | __attribute__((visibility("hidden"))) | |
74d81a6c | 31 | unsigned int channel_handle_get_nr_streams(struct lttng_ust_shm_handle *handle); |
ddabe860 MJ |
32 | |
33 | __attribute__((visibility("hidden"))) | |
74d81a6c MD |
34 | extern |
35 | void channel_destroy(struct channel *chan, struct lttng_ust_shm_handle *handle, | |
36 | int consumer); | |
37 | ||
a6352fd4 | 38 | /* |
1d498196 MD |
39 | * Pointer dereferencing. We don't trust the shm_ref, so we validate |
40 | * both the index and offset with known boundaries. | |
ed5426d3 MD |
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. | |
a6352fd4 | 46 | */ |
a6352fd4 | 47 | static inline |
4746ae29 | 48 | char *_shmp_offset(struct shm_object_table *table, struct shm_ref *ref, |
cba4b7a3 | 49 | size_t idx, size_t elem_size) |
a6352fd4 | 50 | { |
1d498196 | 51 | struct shm_object *obj; |
cba4b7a3 | 52 | size_t objindex, ref_offset; |
a6352fd4 | 53 | |
cba4b7a3 | 54 | objindex = (size_t) ref->index; |
b5a3dfa5 | 55 | if (caa_unlikely(objindex >= table->allocated_len)) |
1d498196 | 56 | return NULL; |
cba4b7a3 | 57 | obj = &table->objects[objindex]; |
4746ae29 | 58 | ref_offset = (size_t) ref->offset; |
cba4b7a3 MD |
59 | ref_offset += idx * elem_size; |
60 | /* Check if part of the element returned would exceed the limits. */ | |
b5a3dfa5 | 61 | if (caa_unlikely(ref_offset + elem_size > obj->memory_map_size)) |
a6352fd4 | 62 | return NULL; |
4746ae29 | 63 | return &obj->memory_map[ref_offset]; |
a6352fd4 MD |
64 | } |
65 | ||
cba4b7a3 | 66 | #define shmp_index(handle, ref, index) \ |
1d498196 MD |
67 | ({ \ |
68 | __typeof__((ref)._type) ____ptr_ret; \ | |
cba4b7a3 | 69 | ____ptr_ret = (__typeof__(____ptr_ret)) _shmp_offset((handle)->table, &(ref)._ref, index, sizeof(*____ptr_ret)); \ |
1d498196 MD |
70 | ____ptr_ret; \ |
71 | }) | |
72 | ||
4746ae29 MD |
73 | #define shmp(handle, ref) shmp_index(handle, ref, 0) |
74 | ||
431d5cf0 | 75 | static inline |
1d498196 | 76 | void _set_shmp(struct shm_ref *ref, struct shm_ref src) |
431d5cf0 | 77 | { |
1d498196 | 78 | *ref = src; |
431d5cf0 MD |
79 | } |
80 | ||
1d498196 MD |
81 | #define set_shmp(ref, src) _set_shmp(&(ref)._ref, src) |
82 | ||
ddabe860 | 83 | __attribute__((visibility("hidden"))) |
1d498196 | 84 | struct shm_object_table *shm_object_table_create(size_t max_nb_obj); |
ddabe860 MJ |
85 | |
86 | __attribute__((visibility("hidden"))) | |
74d81a6c MD |
87 | struct shm_object *shm_object_table_alloc(struct shm_object_table *table, |
88 | size_t memory_map_size, | |
a9ff648c | 89 | enum shm_object_type type, |
4b68c31f MD |
90 | const int stream_fd, |
91 | int cpu); | |
ddabe860 MJ |
92 | |
93 | __attribute__((visibility("hidden"))) | |
74d81a6c MD |
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); | |
ddabe860 | 97 | |
74d81a6c | 98 | /* mem ownership is passed to shm_object_table_append_mem(). */ |
ddabe860 | 99 | __attribute__((visibility("hidden"))) |
74d81a6c | 100 | struct shm_object *shm_object_table_append_mem(struct shm_object_table *table, |
ff0f5728 | 101 | void *mem, size_t memory_map_size, int wakeup_fd); |
ddabe860 MJ |
102 | |
103 | __attribute__((visibility("hidden"))) | |
6548fca4 | 104 | void shm_object_table_destroy(struct shm_object_table *table, int consumer); |
1d498196 MD |
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 | */ | |
ddabe860 | 113 | __attribute__((visibility("hidden"))) |
1d498196 | 114 | struct shm_ref zalloc_shm(struct shm_object *obj, size_t len); |
ddabe860 MJ |
115 | |
116 | __attribute__((visibility("hidden"))) | |
1d498196 MD |
117 | void align_shm(struct shm_object *obj, size_t align); |
118 | ||
74d81a6c MD |
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 | ||
5d61a504 | 133 | static inline |
38fae1d3 | 134 | int shm_get_wakeup_fd(struct lttng_ust_shm_handle *handle, struct shm_ref *ref) |
5d61a504 MD |
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; | |
b5a3dfa5 | 141 | if (caa_unlikely(index >= table->allocated_len)) |
5d61a504 MD |
142 | return -EPERM; |
143 | obj = &table->objects[index]; | |
144 | return obj->wait_fd[1]; | |
74d81a6c MD |
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; | |
c33ceb02 | 153 | int wait_fd; |
74d81a6c MD |
154 | size_t index; |
155 | int ret; | |
5d61a504 | 156 | |
74d81a6c MD |
157 | index = (size_t) ref->index; |
158 | if (caa_unlikely(index >= table->allocated_len)) | |
159 | return -EPERM; | |
160 | obj = &table->objects[index]; | |
c33ceb02 MD |
161 | wait_fd = obj->wait_fd[0]; |
162 | if (wait_fd < 0) | |
74d81a6c | 163 | return -ENOENT; |
c33ceb02 MD |
164 | obj->wait_fd[0] = -1; |
165 | ret = close(wait_fd); | |
74d81a6c MD |
166 | if (ret) { |
167 | ret = -errno; | |
168 | return ret; | |
169 | } | |
74d81a6c | 170 | return 0; |
5d61a504 MD |
171 | } |
172 | ||
173 | static inline | |
74d81a6c MD |
174 | int shm_close_wakeup_fd(struct lttng_ust_shm_handle *handle, |
175 | struct shm_ref *ref) | |
5d61a504 MD |
176 | { |
177 | struct shm_object_table *table = handle->table; | |
178 | struct shm_object *obj; | |
c33ceb02 | 179 | int wakeup_fd; |
5d61a504 | 180 | size_t index; |
74d81a6c | 181 | int ret; |
5d61a504 MD |
182 | |
183 | index = (size_t) ref->index; | |
b5a3dfa5 | 184 | if (caa_unlikely(index >= table->allocated_len)) |
5d61a504 MD |
185 | return -EPERM; |
186 | obj = &table->objects[index]; | |
c33ceb02 MD |
187 | wakeup_fd = obj->wait_fd[1]; |
188 | if (wakeup_fd < 0) | |
74d81a6c | 189 | return -ENOENT; |
c33ceb02 MD |
190 | obj->wait_fd[1] = -1; |
191 | ret = close(wakeup_fd); | |
74d81a6c MD |
192 | if (ret) { |
193 | ret = -errno; | |
194 | return ret; | |
195 | } | |
74d81a6c | 196 | return 0; |
5d61a504 MD |
197 | } |
198 | ||
381c0f1e | 199 | static inline |
74d81a6c MD |
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) | |
381c0f1e MD |
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; | |
b5a3dfa5 | 223 | if (caa_unlikely(index >= table->allocated_len)) |
381c0f1e MD |
224 | return -EPERM; |
225 | obj = &table->objects[index]; | |
74d81a6c | 226 | *size = obj->memory_map_size; |
381c0f1e MD |
227 | return 0; |
228 | } | |
229 | ||
a6352fd4 | 230 | #endif /* _LIBRINGBUFFER_SHM_H */ |