Commit | Line | Data |
---|---|---|
a6352fd4 MD |
1 | #ifndef _LIBRINGBUFFER_SHM_H |
2 | #define _LIBRINGBUFFER_SHM_H | |
3 | ||
4 | /* | |
5 | * libringbuffer/shm.h | |
6 | * | |
e92f3e28 | 7 | * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
a6352fd4 | 8 | * |
e92f3e28 MD |
9 | * This library is free software; you can redistribute it and/or |
10 | * modify it under the terms of the GNU Lesser General Public | |
11 | * License as published by the Free Software Foundation; only | |
12 | * version 2.1 of the License. | |
13 | * | |
14 | * This library is distributed in the hope that it will be useful, | |
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
17 | * Lesser General Public License for more details. | |
18 | * | |
19 | * You should have received a copy of the GNU Lesser General Public | |
20 | * License along with this library; if not, write to the Free Software | |
21 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
a6352fd4 MD |
22 | */ |
23 | ||
b4051ad8 | 24 | #include <stddef.h> |
a6352fd4 | 25 | #include <stdint.h> |
4e79769f | 26 | #include <unistd.h> |
44c72f10 | 27 | #include <usterr-signal-safe.h> |
35897f8b | 28 | #include <urcu/compiler.h> |
1d498196 | 29 | #include "shm_types.h" |
a6352fd4 | 30 | |
74d81a6c MD |
31 | /* channel_handle_create - for UST. */ |
32 | extern | |
33 | struct lttng_ust_shm_handle *channel_handle_create(void *data, | |
ff0f5728 | 34 | uint64_t memory_map_size, int wakeup_fd); |
74d81a6c MD |
35 | /* channel_handle_add_stream - for UST. */ |
36 | extern | |
37 | int channel_handle_add_stream(struct lttng_ust_shm_handle *handle, | |
38 | int shm_fd, int wakeup_fd, uint32_t stream_nr, | |
39 | uint64_t memory_map_size); | |
40 | unsigned int channel_handle_get_nr_streams(struct lttng_ust_shm_handle *handle); | |
41 | extern | |
42 | void channel_destroy(struct channel *chan, struct lttng_ust_shm_handle *handle, | |
43 | int consumer); | |
44 | ||
a6352fd4 | 45 | /* |
1d498196 MD |
46 | * Pointer dereferencing. We don't trust the shm_ref, so we validate |
47 | * both the index and offset with known boundaries. | |
ed5426d3 MD |
48 | * |
49 | * "shmp" and "shmp_index" guarantee that it's safe to use the pointer | |
50 | * target type, even in the occurrence of shm_ref modification by an | |
51 | * untrusted process having write access to the shm_ref. We return a | |
52 | * NULL pointer if the ranges are invalid. | |
a6352fd4 | 53 | */ |
a6352fd4 | 54 | static inline |
4746ae29 | 55 | char *_shmp_offset(struct shm_object_table *table, struct shm_ref *ref, |
cba4b7a3 | 56 | size_t idx, size_t elem_size) |
a6352fd4 | 57 | { |
1d498196 | 58 | struct shm_object *obj; |
cba4b7a3 | 59 | size_t objindex, ref_offset; |
a6352fd4 | 60 | |
cba4b7a3 | 61 | objindex = (size_t) ref->index; |
b5a3dfa5 | 62 | if (caa_unlikely(objindex >= table->allocated_len)) |
1d498196 | 63 | return NULL; |
cba4b7a3 | 64 | obj = &table->objects[objindex]; |
4746ae29 | 65 | ref_offset = (size_t) ref->offset; |
cba4b7a3 MD |
66 | ref_offset += idx * elem_size; |
67 | /* Check if part of the element returned would exceed the limits. */ | |
b5a3dfa5 | 68 | if (caa_unlikely(ref_offset + elem_size > obj->memory_map_size)) |
a6352fd4 | 69 | return NULL; |
4746ae29 | 70 | return &obj->memory_map[ref_offset]; |
a6352fd4 MD |
71 | } |
72 | ||
cba4b7a3 | 73 | #define shmp_index(handle, ref, index) \ |
1d498196 MD |
74 | ({ \ |
75 | __typeof__((ref)._type) ____ptr_ret; \ | |
cba4b7a3 | 76 | ____ptr_ret = (__typeof__(____ptr_ret)) _shmp_offset((handle)->table, &(ref)._ref, index, sizeof(*____ptr_ret)); \ |
1d498196 MD |
77 | ____ptr_ret; \ |
78 | }) | |
79 | ||
4746ae29 MD |
80 | #define shmp(handle, ref) shmp_index(handle, ref, 0) |
81 | ||
431d5cf0 | 82 | static inline |
1d498196 | 83 | void _set_shmp(struct shm_ref *ref, struct shm_ref src) |
431d5cf0 | 84 | { |
1d498196 | 85 | *ref = src; |
431d5cf0 MD |
86 | } |
87 | ||
1d498196 MD |
88 | #define set_shmp(ref, src) _set_shmp(&(ref)._ref, src) |
89 | ||
90 | struct shm_object_table *shm_object_table_create(size_t max_nb_obj); | |
74d81a6c MD |
91 | struct shm_object *shm_object_table_alloc(struct shm_object_table *table, |
92 | size_t memory_map_size, | |
a9ff648c | 93 | enum shm_object_type type, |
4b68c31f MD |
94 | const int stream_fd, |
95 | int cpu); | |
74d81a6c MD |
96 | struct shm_object *shm_object_table_append_shm(struct shm_object_table *table, |
97 | int shm_fd, int wakeup_fd, uint32_t stream_nr, | |
98 | size_t memory_map_size); | |
99 | /* mem ownership is passed to shm_object_table_append_mem(). */ | |
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); |
6548fca4 | 102 | void shm_object_table_destroy(struct shm_object_table *table, int consumer); |
1d498196 MD |
103 | |
104 | /* | |
105 | * zalloc_shm - allocate memory within a shm object. | |
106 | * | |
107 | * Shared memory is already zeroed by shmget. | |
108 | * *NOT* multithread-safe (should be protected by mutex). | |
109 | * Returns a -1, -1 tuple on error. | |
110 | */ | |
111 | struct shm_ref zalloc_shm(struct shm_object *obj, size_t len); | |
112 | void align_shm(struct shm_object *obj, size_t align); | |
113 | ||
74d81a6c MD |
114 | static inline |
115 | int shm_get_wait_fd(struct lttng_ust_shm_handle *handle, struct shm_ref *ref) | |
116 | { | |
117 | struct shm_object_table *table = handle->table; | |
118 | struct shm_object *obj; | |
119 | size_t index; | |
120 | ||
121 | index = (size_t) ref->index; | |
122 | if (caa_unlikely(index >= table->allocated_len)) | |
123 | return -EPERM; | |
124 | obj = &table->objects[index]; | |
125 | return obj->wait_fd[0]; | |
126 | } | |
127 | ||
5d61a504 | 128 | static inline |
38fae1d3 | 129 | int shm_get_wakeup_fd(struct lttng_ust_shm_handle *handle, struct shm_ref *ref) |
5d61a504 MD |
130 | { |
131 | struct shm_object_table *table = handle->table; | |
132 | struct shm_object *obj; | |
133 | size_t index; | |
134 | ||
135 | index = (size_t) ref->index; | |
b5a3dfa5 | 136 | if (caa_unlikely(index >= table->allocated_len)) |
5d61a504 MD |
137 | return -EPERM; |
138 | obj = &table->objects[index]; | |
139 | return obj->wait_fd[1]; | |
74d81a6c MD |
140 | } |
141 | ||
142 | static inline | |
143 | int shm_close_wait_fd(struct lttng_ust_shm_handle *handle, | |
144 | struct shm_ref *ref) | |
145 | { | |
146 | struct shm_object_table *table = handle->table; | |
147 | struct shm_object *obj; | |
c33ceb02 | 148 | int wait_fd; |
74d81a6c MD |
149 | size_t index; |
150 | int ret; | |
5d61a504 | 151 | |
74d81a6c MD |
152 | index = (size_t) ref->index; |
153 | if (caa_unlikely(index >= table->allocated_len)) | |
154 | return -EPERM; | |
155 | obj = &table->objects[index]; | |
c33ceb02 MD |
156 | wait_fd = obj->wait_fd[0]; |
157 | if (wait_fd < 0) | |
74d81a6c | 158 | return -ENOENT; |
c33ceb02 MD |
159 | obj->wait_fd[0] = -1; |
160 | ret = close(wait_fd); | |
74d81a6c MD |
161 | if (ret) { |
162 | ret = -errno; | |
163 | return ret; | |
164 | } | |
74d81a6c | 165 | return 0; |
5d61a504 MD |
166 | } |
167 | ||
168 | static inline | |
74d81a6c MD |
169 | int shm_close_wakeup_fd(struct lttng_ust_shm_handle *handle, |
170 | struct shm_ref *ref) | |
5d61a504 MD |
171 | { |
172 | struct shm_object_table *table = handle->table; | |
173 | struct shm_object *obj; | |
c33ceb02 | 174 | int wakeup_fd; |
5d61a504 | 175 | size_t index; |
74d81a6c | 176 | int ret; |
5d61a504 MD |
177 | |
178 | index = (size_t) ref->index; | |
b5a3dfa5 | 179 | if (caa_unlikely(index >= table->allocated_len)) |
5d61a504 MD |
180 | return -EPERM; |
181 | obj = &table->objects[index]; | |
c33ceb02 MD |
182 | wakeup_fd = obj->wait_fd[1]; |
183 | if (wakeup_fd < 0) | |
74d81a6c | 184 | return -ENOENT; |
c33ceb02 MD |
185 | obj->wait_fd[1] = -1; |
186 | ret = close(wakeup_fd); | |
74d81a6c MD |
187 | if (ret) { |
188 | ret = -errno; | |
189 | return ret; | |
190 | } | |
74d81a6c | 191 | return 0; |
5d61a504 MD |
192 | } |
193 | ||
381c0f1e | 194 | static inline |
74d81a6c MD |
195 | int shm_get_shm_fd(struct lttng_ust_shm_handle *handle, struct shm_ref *ref) |
196 | { | |
197 | struct shm_object_table *table = handle->table; | |
198 | struct shm_object *obj; | |
199 | size_t index; | |
200 | ||
201 | index = (size_t) ref->index; | |
202 | if (caa_unlikely(index >= table->allocated_len)) | |
203 | return -EPERM; | |
204 | obj = &table->objects[index]; | |
205 | return obj->shm_fd; | |
206 | } | |
207 | ||
208 | ||
209 | static inline | |
210 | int shm_get_shm_size(struct lttng_ust_shm_handle *handle, struct shm_ref *ref, | |
211 | uint64_t *size) | |
381c0f1e MD |
212 | { |
213 | struct shm_object_table *table = handle->table; | |
214 | struct shm_object *obj; | |
215 | size_t index; | |
216 | ||
217 | index = (size_t) ref->index; | |
b5a3dfa5 | 218 | if (caa_unlikely(index >= table->allocated_len)) |
381c0f1e MD |
219 | return -EPERM; |
220 | obj = &table->objects[index]; | |
74d81a6c | 221 | *size = obj->memory_map_size; |
381c0f1e MD |
222 | return 0; |
223 | } | |
224 | ||
a6352fd4 | 225 | #endif /* _LIBRINGBUFFER_SHM_H */ |