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