Fix: remove invalid free
[lttng-ust.git] / libringbuffer / shm.h
CommitLineData
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. */
30extern
31struct 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. */
34extern
35int 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);
38unsigned int channel_handle_get_nr_streams(struct lttng_ust_shm_handle *handle);
39extern
40void 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 52static inline
4746ae29 53char *_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 80static inline
1d498196 81void _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
88struct shm_object_table *shm_object_table_create(size_t max_nb_obj);
74d81a6c
MD
89struct shm_object *shm_object_table_alloc(struct shm_object_table *table,
90 size_t memory_map_size,
a9ff648c 91 enum shm_object_type type,
5ea386c3 92 const int stream_fd);
74d81a6c
MD
93struct shm_object *shm_object_table_append_shm(struct shm_object_table *table,
94 int shm_fd, int wakeup_fd, uint32_t stream_nr,
95 size_t memory_map_size);
96/* mem ownership is passed to shm_object_table_append_mem(). */
97struct shm_object *shm_object_table_append_mem(struct shm_object_table *table,
ff0f5728 98 void *mem, size_t memory_map_size, int wakeup_fd);
1d498196 99void shm_object_table_destroy(struct shm_object_table *table);
1d498196
MD
100
101/*
102 * zalloc_shm - allocate memory within a shm object.
103 *
104 * Shared memory is already zeroed by shmget.
105 * *NOT* multithread-safe (should be protected by mutex).
106 * Returns a -1, -1 tuple on error.
107 */
108struct shm_ref zalloc_shm(struct shm_object *obj, size_t len);
109void align_shm(struct shm_object *obj, size_t align);
110
74d81a6c
MD
111static inline
112int shm_get_wait_fd(struct lttng_ust_shm_handle *handle, struct shm_ref *ref)
113{
114 struct shm_object_table *table = handle->table;
115 struct shm_object *obj;
116 size_t index;
117
118 index = (size_t) ref->index;
119 if (caa_unlikely(index >= table->allocated_len))
120 return -EPERM;
121 obj = &table->objects[index];
122 return obj->wait_fd[0];
123}
124
5d61a504 125static inline
38fae1d3 126int shm_get_wakeup_fd(struct lttng_ust_shm_handle *handle, struct shm_ref *ref)
5d61a504
MD
127{
128 struct shm_object_table *table = handle->table;
129 struct shm_object *obj;
130 size_t index;
131
132 index = (size_t) ref->index;
b5a3dfa5 133 if (caa_unlikely(index >= table->allocated_len))
5d61a504
MD
134 return -EPERM;
135 obj = &table->objects[index];
136 return obj->wait_fd[1];
74d81a6c
MD
137}
138
139static inline
140int shm_close_wait_fd(struct lttng_ust_shm_handle *handle,
141 struct shm_ref *ref)
142{
143 struct shm_object_table *table = handle->table;
144 struct shm_object *obj;
c33ceb02 145 int wait_fd;
74d81a6c
MD
146 size_t index;
147 int ret;
5d61a504 148
74d81a6c
MD
149 index = (size_t) ref->index;
150 if (caa_unlikely(index >= table->allocated_len))
151 return -EPERM;
152 obj = &table->objects[index];
c33ceb02
MD
153 wait_fd = obj->wait_fd[0];
154 if (wait_fd < 0)
74d81a6c 155 return -ENOENT;
c33ceb02
MD
156 obj->wait_fd[0] = -1;
157 ret = close(wait_fd);
74d81a6c
MD
158 if (ret) {
159 ret = -errno;
160 return ret;
161 }
74d81a6c 162 return 0;
5d61a504
MD
163}
164
165static inline
74d81a6c
MD
166int shm_close_wakeup_fd(struct lttng_ust_shm_handle *handle,
167 struct shm_ref *ref)
5d61a504
MD
168{
169 struct shm_object_table *table = handle->table;
170 struct shm_object *obj;
c33ceb02 171 int wakeup_fd;
5d61a504 172 size_t index;
74d81a6c 173 int ret;
5d61a504
MD
174
175 index = (size_t) ref->index;
b5a3dfa5 176 if (caa_unlikely(index >= table->allocated_len))
5d61a504
MD
177 return -EPERM;
178 obj = &table->objects[index];
c33ceb02
MD
179 wakeup_fd = obj->wait_fd[1];
180 if (wakeup_fd < 0)
74d81a6c 181 return -ENOENT;
c33ceb02
MD
182 obj->wait_fd[1] = -1;
183 ret = close(wakeup_fd);
74d81a6c
MD
184 if (ret) {
185 ret = -errno;
186 return ret;
187 }
74d81a6c 188 return 0;
5d61a504
MD
189}
190
381c0f1e 191static inline
74d81a6c
MD
192int shm_get_shm_fd(struct lttng_ust_shm_handle *handle, struct shm_ref *ref)
193{
194 struct shm_object_table *table = handle->table;
195 struct shm_object *obj;
196 size_t index;
197
198 index = (size_t) ref->index;
199 if (caa_unlikely(index >= table->allocated_len))
200 return -EPERM;
201 obj = &table->objects[index];
202 return obj->shm_fd;
203}
204
205
206static inline
207int shm_get_shm_size(struct lttng_ust_shm_handle *handle, struct shm_ref *ref,
208 uint64_t *size)
381c0f1e
MD
209{
210 struct shm_object_table *table = handle->table;
211 struct shm_object *obj;
212 size_t index;
213
214 index = (size_t) ref->index;
b5a3dfa5 215 if (caa_unlikely(index >= table->allocated_len))
381c0f1e
MD
216 return -EPERM;
217 obj = &table->objects[index];
74d81a6c 218 *size = obj->memory_map_size;
381c0f1e
MD
219 return 0;
220}
221
a6352fd4 222#endif /* _LIBRINGBUFFER_SHM_H */
This page took 0.050602 seconds and 4 git commands to generate.