Move LTTng-UST buffer ownership from application to consumer
[lttng-ust.git] / libringbuffer / shm.h
1 #ifndef _LIBRINGBUFFER_SHM_H
2 #define _LIBRINGBUFFER_SHM_H
3
4 /*
5 * libringbuffer/shm.h
6 *
7 * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 *
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
22 */
23
24 #include <stdint.h>
25 #include <usterr-signal-safe.h>
26 #include <urcu/compiler.h>
27 #include "shm_types.h"
28
29 /* channel_handle_create - for UST. */
30 extern
31 struct lttng_ust_shm_handle *channel_handle_create(void *data,
32 uint64_t memory_map_size);
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
43 /*
44 * Pointer dereferencing. We don't trust the shm_ref, so we validate
45 * both the index and offset with known boundaries.
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.
51 */
52 static inline
53 char *_shmp_offset(struct shm_object_table *table, struct shm_ref *ref,
54 size_t idx, size_t elem_size)
55 {
56 struct shm_object *obj;
57 size_t objindex, ref_offset;
58
59 objindex = (size_t) ref->index;
60 if (caa_unlikely(objindex >= table->allocated_len))
61 return NULL;
62 obj = &table->objects[objindex];
63 ref_offset = (size_t) ref->offset;
64 ref_offset += idx * elem_size;
65 /* Check if part of the element returned would exceed the limits. */
66 if (caa_unlikely(ref_offset + elem_size > obj->memory_map_size))
67 return NULL;
68 return &obj->memory_map[ref_offset];
69 }
70
71 #define shmp_index(handle, ref, index) \
72 ({ \
73 __typeof__((ref)._type) ____ptr_ret; \
74 ____ptr_ret = (__typeof__(____ptr_ret)) _shmp_offset((handle)->table, &(ref)._ref, index, sizeof(*____ptr_ret)); \
75 ____ptr_ret; \
76 })
77
78 #define shmp(handle, ref) shmp_index(handle, ref, 0)
79
80 static inline
81 void _set_shmp(struct shm_ref *ref, struct shm_ref src)
82 {
83 *ref = src;
84 }
85
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);
89 struct shm_object *shm_object_table_alloc(struct shm_object_table *table,
90 size_t memory_map_size,
91 enum shm_object_type type);
92 struct shm_object *shm_object_table_append_shm(struct shm_object_table *table,
93 int shm_fd, int wakeup_fd, uint32_t stream_nr,
94 size_t memory_map_size);
95 /* mem ownership is passed to shm_object_table_append_mem(). */
96 struct shm_object *shm_object_table_append_mem(struct shm_object_table *table,
97 void *mem, size_t memory_map_size);
98 void shm_object_table_destroy(struct shm_object_table *table);
99
100 /*
101 * zalloc_shm - allocate memory within a shm object.
102 *
103 * Shared memory is already zeroed by shmget.
104 * *NOT* multithread-safe (should be protected by mutex).
105 * Returns a -1, -1 tuple on error.
106 */
107 struct shm_ref zalloc_shm(struct shm_object *obj, size_t len);
108 void align_shm(struct shm_object *obj, size_t align);
109
110 static inline
111 int shm_get_wait_fd(struct lttng_ust_shm_handle *handle, struct shm_ref *ref)
112 {
113 struct shm_object_table *table = handle->table;
114 struct shm_object *obj;
115 size_t index;
116
117 index = (size_t) ref->index;
118 if (caa_unlikely(index >= table->allocated_len))
119 return -EPERM;
120 obj = &table->objects[index];
121 return obj->wait_fd[0];
122 }
123
124 static inline
125 int shm_get_wakeup_fd(struct lttng_ust_shm_handle *handle, struct shm_ref *ref)
126 {
127 struct shm_object_table *table = handle->table;
128 struct shm_object *obj;
129 size_t index;
130
131 index = (size_t) ref->index;
132 if (caa_unlikely(index >= table->allocated_len))
133 return -EPERM;
134 obj = &table->objects[index];
135 return obj->wait_fd[1];
136 }
137
138 static inline
139 int shm_close_wait_fd(struct lttng_ust_shm_handle *handle,
140 struct shm_ref *ref)
141 {
142 struct shm_object_table *table = handle->table;
143 struct shm_object *obj;
144 size_t index;
145 int ret;
146
147 index = (size_t) ref->index;
148 if (caa_unlikely(index >= table->allocated_len))
149 return -EPERM;
150 obj = &table->objects[index];
151 if (obj->wait_fd[0] < 0)
152 return -ENOENT;
153 ret = close(obj->wait_fd[0]);
154 if (ret) {
155 ret = -errno;
156 return ret;
157 }
158 obj->wait_fd[0] = -1;
159 return 0;
160 }
161
162 static inline
163 int shm_close_wakeup_fd(struct lttng_ust_shm_handle *handle,
164 struct shm_ref *ref)
165 {
166 struct shm_object_table *table = handle->table;
167 struct shm_object *obj;
168 size_t index;
169 int ret;
170
171 index = (size_t) ref->index;
172 if (caa_unlikely(index >= table->allocated_len))
173 return -EPERM;
174 obj = &table->objects[index];
175 if (obj->wait_fd[1] < 0)
176 return -ENOENT;
177 ret = close(obj->wait_fd[1]);
178 if (ret) {
179 ret = -errno;
180 return ret;
181 }
182 obj->wait_fd[1] = -1;
183 return 0;
184 }
185
186 static inline
187 int shm_get_shm_fd(struct lttng_ust_shm_handle *handle, struct shm_ref *ref)
188 {
189 struct shm_object_table *table = handle->table;
190 struct shm_object *obj;
191 size_t index;
192
193 index = (size_t) ref->index;
194 if (caa_unlikely(index >= table->allocated_len))
195 return -EPERM;
196 obj = &table->objects[index];
197 return obj->shm_fd;
198 }
199
200
201 static inline
202 int shm_get_shm_size(struct lttng_ust_shm_handle *handle, struct shm_ref *ref,
203 uint64_t *size)
204 {
205 struct shm_object_table *table = handle->table;
206 struct shm_object *obj;
207 size_t index;
208
209 index = (size_t) ref->index;
210 if (caa_unlikely(index >= table->allocated_len))
211 return -EPERM;
212 obj = &table->objects[index];
213 *size = obj->memory_map_size;
214 return 0;
215 }
216
217 #endif /* _LIBRINGBUFFER_SHM_H */
This page took 0.036 seconds and 5 git commands to generate.