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 | ||
24 | #include <stdint.h> | |
44c72f10 | 25 | #include <usterr-signal-safe.h> |
35897f8b | 26 | #include <urcu/compiler.h> |
1d498196 | 27 | #include "shm_types.h" |
a6352fd4 | 28 | |
74d81a6c MD |
29 | /* channel_handle_create - for UST. */ |
30 | extern | |
31 | struct lttng_ust_shm_handle *channel_handle_create(void *data, | |
ff0f5728 | 32 | uint64_t memory_map_size, int wakeup_fd); |
74d81a6c MD |
33 | /* channel_handle_add_stream - for UST. */ |
34 | extern | |
35 | int channel_handle_add_stream(struct lttng_ust_shm_handle *handle, | |
36 | int shm_fd, int wakeup_fd, uint32_t stream_nr, | |
37 | uint64_t memory_map_size); | |
38 | unsigned int channel_handle_get_nr_streams(struct lttng_ust_shm_handle *handle); | |
39 | extern | |
40 | void channel_destroy(struct channel *chan, struct lttng_ust_shm_handle *handle, | |
41 | int consumer); | |
42 | ||
a6352fd4 | 43 | /* |
1d498196 MD |
44 | * Pointer dereferencing. We don't trust the shm_ref, so we validate |
45 | * both the index and offset with known boundaries. | |
ed5426d3 MD |
46 | * |
47 | * "shmp" and "shmp_index" guarantee that it's safe to use the pointer | |
48 | * target type, even in the occurrence of shm_ref modification by an | |
49 | * untrusted process having write access to the shm_ref. We return a | |
50 | * NULL pointer if the ranges are invalid. | |
a6352fd4 | 51 | */ |
a6352fd4 | 52 | static inline |
4746ae29 | 53 | char *_shmp_offset(struct shm_object_table *table, struct shm_ref *ref, |
cba4b7a3 | 54 | size_t idx, size_t elem_size) |
a6352fd4 | 55 | { |
1d498196 | 56 | struct shm_object *obj; |
cba4b7a3 | 57 | size_t objindex, ref_offset; |
a6352fd4 | 58 | |
cba4b7a3 | 59 | objindex = (size_t) ref->index; |
b5a3dfa5 | 60 | if (caa_unlikely(objindex >= table->allocated_len)) |
1d498196 | 61 | return NULL; |
cba4b7a3 | 62 | obj = &table->objects[objindex]; |
4746ae29 | 63 | ref_offset = (size_t) ref->offset; |
cba4b7a3 MD |
64 | ref_offset += idx * elem_size; |
65 | /* Check if part of the element returned would exceed the limits. */ | |
b5a3dfa5 | 66 | if (caa_unlikely(ref_offset + elem_size > obj->memory_map_size)) |
a6352fd4 | 67 | return NULL; |
4746ae29 | 68 | return &obj->memory_map[ref_offset]; |
a6352fd4 MD |
69 | } |
70 | ||
cba4b7a3 | 71 | #define shmp_index(handle, ref, index) \ |
1d498196 MD |
72 | ({ \ |
73 | __typeof__((ref)._type) ____ptr_ret; \ | |
cba4b7a3 | 74 | ____ptr_ret = (__typeof__(____ptr_ret)) _shmp_offset((handle)->table, &(ref)._ref, index, sizeof(*____ptr_ret)); \ |
1d498196 MD |
75 | ____ptr_ret; \ |
76 | }) | |
77 | ||
4746ae29 MD |
78 | #define shmp(handle, ref) shmp_index(handle, ref, 0) |
79 | ||
431d5cf0 | 80 | static inline |
1d498196 | 81 | void _set_shmp(struct shm_ref *ref, struct shm_ref src) |
431d5cf0 | 82 | { |
1d498196 | 83 | *ref = src; |
431d5cf0 MD |
84 | } |
85 | ||
1d498196 MD |
86 | #define set_shmp(ref, src) _set_shmp(&(ref)._ref, src) |
87 | ||
88 | struct shm_object_table *shm_object_table_create(size_t max_nb_obj); | |
74d81a6c MD |
89 | struct shm_object *shm_object_table_alloc(struct shm_object_table *table, |
90 | size_t memory_map_size, | |
a9ff648c | 91 | enum shm_object_type type, |
4b68c31f MD |
92 | const int stream_fd, |
93 | int cpu); | |
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); | |
97 | /* mem ownership is passed to shm_object_table_append_mem(). */ | |
98 | struct shm_object *shm_object_table_append_mem(struct shm_object_table *table, | |
ff0f5728 | 99 | void *mem, size_t memory_map_size, int wakeup_fd); |
6548fca4 | 100 | void shm_object_table_destroy(struct shm_object_table *table, int consumer); |
1d498196 MD |
101 | |
102 | /* | |
103 | * zalloc_shm - allocate memory within a shm object. | |
104 | * | |
105 | * Shared memory is already zeroed by shmget. | |
106 | * *NOT* multithread-safe (should be protected by mutex). | |
107 | * Returns a -1, -1 tuple on error. | |
108 | */ | |
109 | struct shm_ref zalloc_shm(struct shm_object *obj, size_t len); | |
110 | void align_shm(struct shm_object *obj, size_t align); | |
111 | ||
74d81a6c MD |
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 | ||
5d61a504 | 126 | static inline |
38fae1d3 | 127 | int shm_get_wakeup_fd(struct lttng_ust_shm_handle *handle, struct shm_ref *ref) |
5d61a504 MD |
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; | |
b5a3dfa5 | 134 | if (caa_unlikely(index >= table->allocated_len)) |
5d61a504 MD |
135 | return -EPERM; |
136 | obj = &table->objects[index]; | |
137 | return obj->wait_fd[1]; | |
74d81a6c MD |
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; | |
c33ceb02 | 146 | int wait_fd; |
74d81a6c MD |
147 | size_t index; |
148 | int ret; | |
5d61a504 | 149 | |
74d81a6c MD |
150 | index = (size_t) ref->index; |
151 | if (caa_unlikely(index >= table->allocated_len)) | |
152 | return -EPERM; | |
153 | obj = &table->objects[index]; | |
c33ceb02 MD |
154 | wait_fd = obj->wait_fd[0]; |
155 | if (wait_fd < 0) | |
74d81a6c | 156 | return -ENOENT; |
c33ceb02 MD |
157 | obj->wait_fd[0] = -1; |
158 | ret = close(wait_fd); | |
74d81a6c MD |
159 | if (ret) { |
160 | ret = -errno; | |
161 | return ret; | |
162 | } | |
74d81a6c | 163 | return 0; |
5d61a504 MD |
164 | } |
165 | ||
166 | static inline | |
74d81a6c MD |
167 | int shm_close_wakeup_fd(struct lttng_ust_shm_handle *handle, |
168 | struct shm_ref *ref) | |
5d61a504 MD |
169 | { |
170 | struct shm_object_table *table = handle->table; | |
171 | struct shm_object *obj; | |
c33ceb02 | 172 | int wakeup_fd; |
5d61a504 | 173 | size_t index; |
74d81a6c | 174 | int ret; |
5d61a504 MD |
175 | |
176 | index = (size_t) ref->index; | |
b5a3dfa5 | 177 | if (caa_unlikely(index >= table->allocated_len)) |
5d61a504 MD |
178 | return -EPERM; |
179 | obj = &table->objects[index]; | |
c33ceb02 MD |
180 | wakeup_fd = obj->wait_fd[1]; |
181 | if (wakeup_fd < 0) | |
74d81a6c | 182 | return -ENOENT; |
c33ceb02 MD |
183 | obj->wait_fd[1] = -1; |
184 | ret = close(wakeup_fd); | |
74d81a6c MD |
185 | if (ret) { |
186 | ret = -errno; | |
187 | return ret; | |
188 | } | |
74d81a6c | 189 | return 0; |
5d61a504 MD |
190 | } |
191 | ||
381c0f1e | 192 | static inline |
74d81a6c MD |
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) | |
381c0f1e MD |
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; | |
b5a3dfa5 | 216 | if (caa_unlikely(index >= table->allocated_len)) |
381c0f1e MD |
217 | return -EPERM; |
218 | obj = &table->objects[index]; | |
74d81a6c | 219 | *size = obj->memory_map_size; |
381c0f1e MD |
220 | return 0; |
221 | } | |
222 | ||
a6352fd4 | 223 | #endif /* _LIBRINGBUFFER_SHM_H */ |