Hide libringbuffer private symbols
[lttng-ust.git] / libringbuffer / shm.h
CommitLineData
a6352fd4 1/*
c0c0989a 2 * SPDX-License-Identifier: LGPL-2.1-only
a6352fd4 3 *
e92f3e28 4 * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
a6352fd4
MD
5 */
6
c0c0989a
MJ
7#ifndef _LIBRINGBUFFER_SHM_H
8#define _LIBRINGBUFFER_SHM_H
9
b4051ad8 10#include <stddef.h>
a6352fd4 11#include <stdint.h>
4e79769f 12#include <unistd.h>
44c72f10 13#include <usterr-signal-safe.h>
35897f8b 14#include <urcu/compiler.h>
1d498196 15#include "shm_types.h"
071dec43 16#include "ust-helper.h"
a6352fd4 17
74d81a6c 18/* channel_handle_create - for UST. */
071dec43 19LTTNG_HIDDEN
74d81a6c
MD
20extern
21struct lttng_ust_shm_handle *channel_handle_create(void *data,
ff0f5728 22 uint64_t memory_map_size, int wakeup_fd);
74d81a6c 23/* channel_handle_add_stream - for UST. */
071dec43 24LTTNG_HIDDEN
74d81a6c
MD
25extern
26int 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);
071dec43 29LTTNG_HIDDEN
74d81a6c 30unsigned int channel_handle_get_nr_streams(struct lttng_ust_shm_handle *handle);
071dec43 31LTTNG_HIDDEN
74d81a6c
MD
32extern
33void channel_destroy(struct channel *chan, struct lttng_ust_shm_handle *handle,
34 int consumer);
35
a6352fd4 36/*
1d498196
MD
37 * Pointer dereferencing. We don't trust the shm_ref, so we validate
38 * both the index and offset with known boundaries.
ed5426d3
MD
39 *
40 * "shmp" and "shmp_index" guarantee that it's safe to use the pointer
41 * target type, even in the occurrence of shm_ref modification by an
42 * untrusted process having write access to the shm_ref. We return a
43 * NULL pointer if the ranges are invalid.
a6352fd4 44 */
a6352fd4 45static inline
4746ae29 46char *_shmp_offset(struct shm_object_table *table, struct shm_ref *ref,
cba4b7a3 47 size_t idx, size_t elem_size)
a6352fd4 48{
1d498196 49 struct shm_object *obj;
cba4b7a3 50 size_t objindex, ref_offset;
a6352fd4 51
cba4b7a3 52 objindex = (size_t) ref->index;
b5a3dfa5 53 if (caa_unlikely(objindex >= table->allocated_len))
1d498196 54 return NULL;
cba4b7a3 55 obj = &table->objects[objindex];
4746ae29 56 ref_offset = (size_t) ref->offset;
cba4b7a3
MD
57 ref_offset += idx * elem_size;
58 /* Check if part of the element returned would exceed the limits. */
b5a3dfa5 59 if (caa_unlikely(ref_offset + elem_size > obj->memory_map_size))
a6352fd4 60 return NULL;
4746ae29 61 return &obj->memory_map[ref_offset];
a6352fd4
MD
62}
63
cba4b7a3 64#define shmp_index(handle, ref, index) \
1d498196
MD
65 ({ \
66 __typeof__((ref)._type) ____ptr_ret; \
cba4b7a3 67 ____ptr_ret = (__typeof__(____ptr_ret)) _shmp_offset((handle)->table, &(ref)._ref, index, sizeof(*____ptr_ret)); \
1d498196
MD
68 ____ptr_ret; \
69 })
70
4746ae29
MD
71#define shmp(handle, ref) shmp_index(handle, ref, 0)
72
431d5cf0 73static inline
1d498196 74void _set_shmp(struct shm_ref *ref, struct shm_ref src)
431d5cf0 75{
1d498196 76 *ref = src;
431d5cf0
MD
77}
78
1d498196
MD
79#define set_shmp(ref, src) _set_shmp(&(ref)._ref, src)
80
071dec43 81LTTNG_HIDDEN
1d498196 82struct shm_object_table *shm_object_table_create(size_t max_nb_obj);
071dec43 83LTTNG_HIDDEN
74d81a6c
MD
84struct shm_object *shm_object_table_alloc(struct shm_object_table *table,
85 size_t memory_map_size,
a9ff648c 86 enum shm_object_type type,
4b68c31f
MD
87 const int stream_fd,
88 int cpu);
071dec43 89LTTNG_HIDDEN
74d81a6c
MD
90struct shm_object *shm_object_table_append_shm(struct shm_object_table *table,
91 int shm_fd, int wakeup_fd, uint32_t stream_nr,
92 size_t memory_map_size);
93/* mem ownership is passed to shm_object_table_append_mem(). */
071dec43 94LTTNG_HIDDEN
74d81a6c 95struct shm_object *shm_object_table_append_mem(struct shm_object_table *table,
ff0f5728 96 void *mem, size_t memory_map_size, int wakeup_fd);
071dec43 97LTTNG_HIDDEN
6548fca4 98void shm_object_table_destroy(struct shm_object_table *table, int consumer);
1d498196
MD
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 */
071dec43 107LTTNG_HIDDEN
1d498196 108struct shm_ref zalloc_shm(struct shm_object *obj, size_t len);
071dec43 109LTTNG_HIDDEN
1d498196
MD
110void align_shm(struct shm_object *obj, size_t align);
111
74d81a6c
MD
112static inline
113int 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 126static inline
38fae1d3 127int 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
140static inline
141int 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
166static inline
74d81a6c
MD
167int 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 192static inline
74d81a6c
MD
193int 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
207static inline
208int 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 */
This page took 0.039634 seconds and 4 git commands to generate.