Move to kernel style SPDX license identifiers
[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"
a6352fd4 16
74d81a6c
MD
17/* channel_handle_create - for UST. */
18extern
19struct lttng_ust_shm_handle *channel_handle_create(void *data,
ff0f5728 20 uint64_t memory_map_size, int wakeup_fd);
74d81a6c
MD
21/* channel_handle_add_stream - for UST. */
22extern
23int channel_handle_add_stream(struct lttng_ust_shm_handle *handle,
24 int shm_fd, int wakeup_fd, uint32_t stream_nr,
25 uint64_t memory_map_size);
26unsigned int channel_handle_get_nr_streams(struct lttng_ust_shm_handle *handle);
27extern
28void channel_destroy(struct channel *chan, struct lttng_ust_shm_handle *handle,
29 int consumer);
30
a6352fd4 31/*
1d498196
MD
32 * Pointer dereferencing. We don't trust the shm_ref, so we validate
33 * both the index and offset with known boundaries.
ed5426d3
MD
34 *
35 * "shmp" and "shmp_index" guarantee that it's safe to use the pointer
36 * target type, even in the occurrence of shm_ref modification by an
37 * untrusted process having write access to the shm_ref. We return a
38 * NULL pointer if the ranges are invalid.
a6352fd4 39 */
a6352fd4 40static inline
4746ae29 41char *_shmp_offset(struct shm_object_table *table, struct shm_ref *ref,
cba4b7a3 42 size_t idx, size_t elem_size)
a6352fd4 43{
1d498196 44 struct shm_object *obj;
cba4b7a3 45 size_t objindex, ref_offset;
a6352fd4 46
cba4b7a3 47 objindex = (size_t) ref->index;
b5a3dfa5 48 if (caa_unlikely(objindex >= table->allocated_len))
1d498196 49 return NULL;
cba4b7a3 50 obj = &table->objects[objindex];
4746ae29 51 ref_offset = (size_t) ref->offset;
cba4b7a3
MD
52 ref_offset += idx * elem_size;
53 /* Check if part of the element returned would exceed the limits. */
b5a3dfa5 54 if (caa_unlikely(ref_offset + elem_size > obj->memory_map_size))
a6352fd4 55 return NULL;
4746ae29 56 return &obj->memory_map[ref_offset];
a6352fd4
MD
57}
58
cba4b7a3 59#define shmp_index(handle, ref, index) \
1d498196
MD
60 ({ \
61 __typeof__((ref)._type) ____ptr_ret; \
cba4b7a3 62 ____ptr_ret = (__typeof__(____ptr_ret)) _shmp_offset((handle)->table, &(ref)._ref, index, sizeof(*____ptr_ret)); \
1d498196
MD
63 ____ptr_ret; \
64 })
65
4746ae29
MD
66#define shmp(handle, ref) shmp_index(handle, ref, 0)
67
431d5cf0 68static inline
1d498196 69void _set_shmp(struct shm_ref *ref, struct shm_ref src)
431d5cf0 70{
1d498196 71 *ref = src;
431d5cf0
MD
72}
73
1d498196
MD
74#define set_shmp(ref, src) _set_shmp(&(ref)._ref, src)
75
76struct shm_object_table *shm_object_table_create(size_t max_nb_obj);
74d81a6c
MD
77struct shm_object *shm_object_table_alloc(struct shm_object_table *table,
78 size_t memory_map_size,
a9ff648c 79 enum shm_object_type type,
4b68c31f
MD
80 const int stream_fd,
81 int cpu);
74d81a6c
MD
82struct shm_object *shm_object_table_append_shm(struct shm_object_table *table,
83 int shm_fd, int wakeup_fd, uint32_t stream_nr,
84 size_t memory_map_size);
85/* mem ownership is passed to shm_object_table_append_mem(). */
86struct shm_object *shm_object_table_append_mem(struct shm_object_table *table,
ff0f5728 87 void *mem, size_t memory_map_size, int wakeup_fd);
6548fca4 88void shm_object_table_destroy(struct shm_object_table *table, int consumer);
1d498196
MD
89
90/*
91 * zalloc_shm - allocate memory within a shm object.
92 *
93 * Shared memory is already zeroed by shmget.
94 * *NOT* multithread-safe (should be protected by mutex).
95 * Returns a -1, -1 tuple on error.
96 */
97struct shm_ref zalloc_shm(struct shm_object *obj, size_t len);
98void align_shm(struct shm_object *obj, size_t align);
99
74d81a6c
MD
100static inline
101int shm_get_wait_fd(struct lttng_ust_shm_handle *handle, struct shm_ref *ref)
102{
103 struct shm_object_table *table = handle->table;
104 struct shm_object *obj;
105 size_t index;
106
107 index = (size_t) ref->index;
108 if (caa_unlikely(index >= table->allocated_len))
109 return -EPERM;
110 obj = &table->objects[index];
111 return obj->wait_fd[0];
112}
113
5d61a504 114static inline
38fae1d3 115int shm_get_wakeup_fd(struct lttng_ust_shm_handle *handle, struct shm_ref *ref)
5d61a504
MD
116{
117 struct shm_object_table *table = handle->table;
118 struct shm_object *obj;
119 size_t index;
120
121 index = (size_t) ref->index;
b5a3dfa5 122 if (caa_unlikely(index >= table->allocated_len))
5d61a504
MD
123 return -EPERM;
124 obj = &table->objects[index];
125 return obj->wait_fd[1];
74d81a6c
MD
126}
127
128static inline
129int shm_close_wait_fd(struct lttng_ust_shm_handle *handle,
130 struct shm_ref *ref)
131{
132 struct shm_object_table *table = handle->table;
133 struct shm_object *obj;
c33ceb02 134 int wait_fd;
74d81a6c
MD
135 size_t index;
136 int ret;
5d61a504 137
74d81a6c
MD
138 index = (size_t) ref->index;
139 if (caa_unlikely(index >= table->allocated_len))
140 return -EPERM;
141 obj = &table->objects[index];
c33ceb02
MD
142 wait_fd = obj->wait_fd[0];
143 if (wait_fd < 0)
74d81a6c 144 return -ENOENT;
c33ceb02
MD
145 obj->wait_fd[0] = -1;
146 ret = close(wait_fd);
74d81a6c
MD
147 if (ret) {
148 ret = -errno;
149 return ret;
150 }
74d81a6c 151 return 0;
5d61a504
MD
152}
153
154static inline
74d81a6c
MD
155int shm_close_wakeup_fd(struct lttng_ust_shm_handle *handle,
156 struct shm_ref *ref)
5d61a504
MD
157{
158 struct shm_object_table *table = handle->table;
159 struct shm_object *obj;
c33ceb02 160 int wakeup_fd;
5d61a504 161 size_t index;
74d81a6c 162 int ret;
5d61a504
MD
163
164 index = (size_t) ref->index;
b5a3dfa5 165 if (caa_unlikely(index >= table->allocated_len))
5d61a504
MD
166 return -EPERM;
167 obj = &table->objects[index];
c33ceb02
MD
168 wakeup_fd = obj->wait_fd[1];
169 if (wakeup_fd < 0)
74d81a6c 170 return -ENOENT;
c33ceb02
MD
171 obj->wait_fd[1] = -1;
172 ret = close(wakeup_fd);
74d81a6c
MD
173 if (ret) {
174 ret = -errno;
175 return ret;
176 }
74d81a6c 177 return 0;
5d61a504
MD
178}
179
381c0f1e 180static inline
74d81a6c
MD
181int shm_get_shm_fd(struct lttng_ust_shm_handle *handle, struct shm_ref *ref)
182{
183 struct shm_object_table *table = handle->table;
184 struct shm_object *obj;
185 size_t index;
186
187 index = (size_t) ref->index;
188 if (caa_unlikely(index >= table->allocated_len))
189 return -EPERM;
190 obj = &table->objects[index];
191 return obj->shm_fd;
192}
193
194
195static inline
196int shm_get_shm_size(struct lttng_ust_shm_handle *handle, struct shm_ref *ref,
197 uint64_t *size)
381c0f1e
MD
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;
b5a3dfa5 204 if (caa_unlikely(index >= table->allocated_len))
381c0f1e
MD
205 return -EPERM;
206 obj = &table->objects[index];
74d81a6c 207 *size = obj->memory_map_size;
381c0f1e
MD
208 return 0;
209}
210
a6352fd4 211#endif /* _LIBRINGBUFFER_SHM_H */
This page took 0.040032 seconds and 4 git commands to generate.