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