Rename "tsc" to "timestamp"
[lttng-ust.git] / src / common / ringbuffer / 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>
9d315d6d 13#include "common/logging.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,
1d18d519
MJ
20 uint64_t memory_map_size, int wakeup_fd)
21 __attribute__((visibility("hidden")));
ddabe860 22
74d81a6c
MD
23/* channel_handle_add_stream - for UST. */
24extern
25int channel_handle_add_stream(struct lttng_ust_shm_handle *handle,
26 int shm_fd, int wakeup_fd, uint32_t stream_nr,
1d18d519
MJ
27 uint64_t memory_map_size)
28 __attribute__((visibility("hidden")));
ddabe860 29
1d18d519
MJ
30unsigned int channel_handle_get_nr_streams(struct lttng_ust_shm_handle *handle)
31 __attribute__((visibility("hidden")));
ddabe860 32
a6352fd4 33/*
1d498196
MD
34 * Pointer dereferencing. We don't trust the shm_ref, so we validate
35 * both the index and offset with known boundaries.
ed5426d3
MD
36 *
37 * "shmp" and "shmp_index" guarantee that it's safe to use the pointer
38 * target type, even in the occurrence of shm_ref modification by an
39 * untrusted process having write access to the shm_ref. We return a
40 * NULL pointer if the ranges are invalid.
a6352fd4 41 */
a6352fd4 42static inline
4746ae29 43char *_shmp_offset(struct shm_object_table *table, struct shm_ref *ref,
cba4b7a3 44 size_t idx, size_t elem_size)
a6352fd4 45{
1d498196 46 struct shm_object *obj;
cba4b7a3 47 size_t objindex, ref_offset;
a6352fd4 48
cba4b7a3 49 objindex = (size_t) ref->index;
b5a3dfa5 50 if (caa_unlikely(objindex >= table->allocated_len))
1d498196 51 return NULL;
cba4b7a3 52 obj = &table->objects[objindex];
4746ae29 53 ref_offset = (size_t) ref->offset;
cba4b7a3
MD
54 ref_offset += idx * elem_size;
55 /* Check if part of the element returned would exceed the limits. */
b5a3dfa5 56 if (caa_unlikely(ref_offset + elem_size > obj->memory_map_size))
a6352fd4 57 return NULL;
4746ae29 58 return &obj->memory_map[ref_offset];
a6352fd4
MD
59}
60
4041a8a7
MJ
61#define shmp_index(handle, ref, index) \
62 ((__typeof__((ref)._type)) _shmp_offset((handle)->table, &(ref)._ref, index, sizeof(*((ref)._type))))
1d498196 63
4746ae29
MD
64#define shmp(handle, ref) shmp_index(handle, ref, 0)
65
431d5cf0 66static inline
1d498196 67void _set_shmp(struct shm_ref *ref, struct shm_ref src)
431d5cf0 68{
1d498196 69 *ref = src;
431d5cf0
MD
70}
71
1d498196
MD
72#define set_shmp(ref, src) _set_shmp(&(ref)._ref, src)
73
97572c04 74struct shm_object_table *shm_object_table_create(size_t max_nb_obj, bool populate)
1d18d519 75 __attribute__((visibility("hidden")));
ddabe860 76
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 80 const int stream_fd,
97572c04 81 int cpu, bool populate)
1d18d519 82 __attribute__((visibility("hidden")));
ddabe860 83
74d81a6c
MD
84struct shm_object *shm_object_table_append_shm(struct shm_object_table *table,
85 int shm_fd, int wakeup_fd, uint32_t stream_nr,
97572c04 86 size_t memory_map_size, bool populate)
1d18d519 87 __attribute__((visibility("hidden")));
ddabe860 88
74d81a6c
MD
89/* mem ownership is passed to shm_object_table_append_mem(). */
90struct shm_object *shm_object_table_append_mem(struct shm_object_table *table,
1d18d519
MJ
91 void *mem, size_t memory_map_size, int wakeup_fd)
92 __attribute__((visibility("hidden")));
ddabe860 93
1d18d519
MJ
94void shm_object_table_destroy(struct shm_object_table *table, int consumer)
95 __attribute__((visibility("hidden")));
1d498196
MD
96
97/*
98 * zalloc_shm - allocate memory within a shm object.
99 *
100 * Shared memory is already zeroed by shmget.
101 * *NOT* multithread-safe (should be protected by mutex).
102 * Returns a -1, -1 tuple on error.
103 */
1d18d519
MJ
104struct shm_ref zalloc_shm(struct shm_object *obj, size_t len)
105 __attribute__((visibility("hidden")));
ddabe860 106
1d18d519
MJ
107void align_shm(struct shm_object *obj, size_t align)
108 __attribute__((visibility("hidden")));
1d498196 109
74d81a6c
MD
110static inline
111int 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
5d61a504 124static inline
38fae1d3 125int shm_get_wakeup_fd(struct lttng_ust_shm_handle *handle, struct shm_ref *ref)
5d61a504
MD
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;
b5a3dfa5 132 if (caa_unlikely(index >= table->allocated_len))
5d61a504
MD
133 return -EPERM;
134 obj = &table->objects[index];
135 return obj->wait_fd[1];
74d81a6c
MD
136}
137
138static inline
139int 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;
c33ceb02 144 int wait_fd;
74d81a6c
MD
145 size_t index;
146 int ret;
5d61a504 147
74d81a6c
MD
148 index = (size_t) ref->index;
149 if (caa_unlikely(index >= table->allocated_len))
150 return -EPERM;
151 obj = &table->objects[index];
c33ceb02
MD
152 wait_fd = obj->wait_fd[0];
153 if (wait_fd < 0)
74d81a6c 154 return -ENOENT;
c33ceb02
MD
155 obj->wait_fd[0] = -1;
156 ret = close(wait_fd);
74d81a6c
MD
157 if (ret) {
158 ret = -errno;
159 return ret;
160 }
74d81a6c 161 return 0;
5d61a504
MD
162}
163
164static inline
74d81a6c
MD
165int shm_close_wakeup_fd(struct lttng_ust_shm_handle *handle,
166 struct shm_ref *ref)
5d61a504
MD
167{
168 struct shm_object_table *table = handle->table;
169 struct shm_object *obj;
c33ceb02 170 int wakeup_fd;
5d61a504 171 size_t index;
74d81a6c 172 int ret;
5d61a504
MD
173
174 index = (size_t) ref->index;
b5a3dfa5 175 if (caa_unlikely(index >= table->allocated_len))
5d61a504
MD
176 return -EPERM;
177 obj = &table->objects[index];
c33ceb02
MD
178 wakeup_fd = obj->wait_fd[1];
179 if (wakeup_fd < 0)
74d81a6c 180 return -ENOENT;
c33ceb02
MD
181 obj->wait_fd[1] = -1;
182 ret = close(wakeup_fd);
74d81a6c
MD
183 if (ret) {
184 ret = -errno;
185 return ret;
186 }
74d81a6c 187 return 0;
5d61a504
MD
188}
189
381c0f1e 190static inline
74d81a6c
MD
191int shm_get_shm_fd(struct lttng_ust_shm_handle *handle, struct shm_ref *ref)
192{
193 struct shm_object_table *table = handle->table;
194 struct shm_object *obj;
195 size_t index;
196
197 index = (size_t) ref->index;
198 if (caa_unlikely(index >= table->allocated_len))
199 return -EPERM;
200 obj = &table->objects[index];
201 return obj->shm_fd;
202}
203
204
205static inline
206int shm_get_shm_size(struct lttng_ust_shm_handle *handle, struct shm_ref *ref,
207 uint64_t *size)
381c0f1e
MD
208{
209 struct shm_object_table *table = handle->table;
210 struct shm_object *obj;
211 size_t index;
212
213 index = (size_t) ref->index;
b5a3dfa5 214 if (caa_unlikely(index >= table->allocated_len))
381c0f1e
MD
215 return -EPERM;
216 obj = &table->objects[index];
74d81a6c 217 *size = obj->memory_map_size;
381c0f1e
MD
218 return 0;
219}
220
a6352fd4 221#endif /* _LIBRINGBUFFER_SHM_H */
This page took 0.048853 seconds and 4 git commands to generate.