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