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