2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
7 #ifndef _LIBRINGBUFFER_SHM_H
8 #define _LIBRINGBUFFER_SHM_H
13 #include <usterr-signal-safe.h>
14 #include <urcu/compiler.h>
15 #include "shm_types.h"
17 /* channel_handle_create - for UST. */
18 __attribute__((visibility("hidden")))
20 struct lttng_ust_shm_handle
*channel_handle_create(void *data
,
21 uint64_t memory_map_size
, int wakeup_fd
);
23 /* channel_handle_add_stream - for UST. */
24 __attribute__((visibility("hidden")))
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
);
30 __attribute__((visibility("hidden")))
31 unsigned int channel_handle_get_nr_streams(struct lttng_ust_shm_handle
*handle
);
33 __attribute__((visibility("hidden")))
35 void channel_destroy(struct lttng_ust_lib_ring_buffer_channel
*chan
,
36 struct lttng_ust_shm_handle
*handle
,
40 * Pointer dereferencing. We don't trust the shm_ref, so we validate
41 * both the index and offset with known boundaries.
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.
49 char *_shmp_offset(struct shm_object_table
*table
, struct shm_ref
*ref
,
50 size_t idx
, size_t elem_size
)
52 struct shm_object
*obj
;
53 size_t objindex
, ref_offset
;
55 objindex
= (size_t) ref
->index
;
56 if (caa_unlikely(objindex
>= table
->allocated_len
))
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
))
64 return &obj
->memory_map
[ref_offset
];
67 #define shmp_index(handle, ref, index) \
68 ((__typeof__((ref)._type)) _shmp_offset((handle)->table, &(ref)._ref, index, sizeof(*((ref)._type))))
70 #define shmp(handle, ref) shmp_index(handle, ref, 0)
73 void _set_shmp(struct shm_ref
*ref
, struct shm_ref src
)
78 #define set_shmp(ref, src) _set_shmp(&(ref)._ref, src)
80 __attribute__((visibility("hidden")))
81 struct shm_object_table
*shm_object_table_create(size_t max_nb_obj
);
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
,
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
);
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
);
100 __attribute__((visibility("hidden")))
101 void shm_object_table_destroy(struct shm_object_table
*table
, int consumer
);
104 * zalloc_shm - allocate memory within a shm object.
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.
110 __attribute__((visibility("hidden")))
111 struct shm_ref
zalloc_shm(struct shm_object
*obj
, size_t len
);
113 __attribute__((visibility("hidden")))
114 void align_shm(struct shm_object
*obj
, size_t align
);
117 int shm_get_wait_fd(struct lttng_ust_shm_handle
*handle
, struct shm_ref
*ref
)
119 struct shm_object_table
*table
= handle
->table
;
120 struct shm_object
*obj
;
123 index
= (size_t) ref
->index
;
124 if (caa_unlikely(index
>= table
->allocated_len
))
126 obj
= &table
->objects
[index
];
127 return obj
->wait_fd
[0];
131 int shm_get_wakeup_fd(struct lttng_ust_shm_handle
*handle
, struct shm_ref
*ref
)
133 struct shm_object_table
*table
= handle
->table
;
134 struct shm_object
*obj
;
137 index
= (size_t) ref
->index
;
138 if (caa_unlikely(index
>= table
->allocated_len
))
140 obj
= &table
->objects
[index
];
141 return obj
->wait_fd
[1];
145 int shm_close_wait_fd(struct lttng_ust_shm_handle
*handle
,
148 struct shm_object_table
*table
= handle
->table
;
149 struct shm_object
*obj
;
154 index
= (size_t) ref
->index
;
155 if (caa_unlikely(index
>= table
->allocated_len
))
157 obj
= &table
->objects
[index
];
158 wait_fd
= obj
->wait_fd
[0];
161 obj
->wait_fd
[0] = -1;
162 ret
= close(wait_fd
);
171 int shm_close_wakeup_fd(struct lttng_ust_shm_handle
*handle
,
174 struct shm_object_table
*table
= handle
->table
;
175 struct shm_object
*obj
;
180 index
= (size_t) ref
->index
;
181 if (caa_unlikely(index
>= table
->allocated_len
))
183 obj
= &table
->objects
[index
];
184 wakeup_fd
= obj
->wait_fd
[1];
187 obj
->wait_fd
[1] = -1;
188 ret
= close(wakeup_fd
);
197 int shm_get_shm_fd(struct lttng_ust_shm_handle
*handle
, struct shm_ref
*ref
)
199 struct shm_object_table
*table
= handle
->table
;
200 struct shm_object
*obj
;
203 index
= (size_t) ref
->index
;
204 if (caa_unlikely(index
>= table
->allocated_len
))
206 obj
= &table
->objects
[index
];
212 int shm_get_shm_size(struct lttng_ust_shm_handle
*handle
, struct shm_ref
*ref
,
215 struct shm_object_table
*table
= handle
->table
;
216 struct shm_object
*obj
;
219 index
= (size_t) ref
->index
;
220 if (caa_unlikely(index
>= table
->allocated_len
))
222 obj
= &table
->objects
[index
];
223 *size
= obj
->memory_map_size
;
227 #endif /* _LIBRINGBUFFER_SHM_H */