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"
16 #include "ust-helper.h"
18 /* channel_handle_create - for UST. */
21 struct lttng_ust_shm_handle
*channel_handle_create(void *data
,
22 uint64_t memory_map_size
, int wakeup_fd
);
23 /* channel_handle_add_stream - for UST. */
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 unsigned int channel_handle_get_nr_streams(struct lttng_ust_shm_handle
*handle
);
33 void channel_destroy(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
37 * Pointer dereferencing. We don't trust the shm_ref, so we validate
38 * both the index and offset with known boundaries.
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.
46 char *_shmp_offset(struct shm_object_table
*table
, struct shm_ref
*ref
,
47 size_t idx
, size_t elem_size
)
49 struct shm_object
*obj
;
50 size_t objindex
, ref_offset
;
52 objindex
= (size_t) ref
->index
;
53 if (caa_unlikely(objindex
>= table
->allocated_len
))
55 obj
= &table
->objects
[objindex
];
56 ref_offset
= (size_t) ref
->offset
;
57 ref_offset
+= idx
* elem_size
;
58 /* Check if part of the element returned would exceed the limits. */
59 if (caa_unlikely(ref_offset
+ elem_size
> obj
->memory_map_size
))
61 return &obj
->memory_map
[ref_offset
];
64 #define shmp_index(handle, ref, index) \
66 __typeof__((ref)._type) ____ptr_ret; \
67 ____ptr_ret = (__typeof__(____ptr_ret)) _shmp_offset((handle)->table, &(ref)._ref, index, sizeof(*____ptr_ret)); \
71 #define shmp(handle, ref) shmp_index(handle, ref, 0)
74 void _set_shmp(struct shm_ref
*ref
, struct shm_ref src
)
79 #define set_shmp(ref, src) _set_shmp(&(ref)._ref, src)
82 struct shm_object_table
*shm_object_table_create(size_t max_nb_obj
);
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 struct 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(). */
95 struct shm_object
*shm_object_table_append_mem(struct shm_object_table
*table
,
96 void *mem
, size_t memory_map_size
, int wakeup_fd
);
98 void shm_object_table_destroy(struct shm_object_table
*table
, int consumer
);
101 * zalloc_shm - allocate memory within a shm object.
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.
108 struct shm_ref
zalloc_shm(struct shm_object
*obj
, size_t len
);
110 void align_shm(struct shm_object
*obj
, size_t align
);
113 int shm_get_wait_fd(struct lttng_ust_shm_handle
*handle
, struct shm_ref
*ref
)
115 struct shm_object_table
*table
= handle
->table
;
116 struct shm_object
*obj
;
119 index
= (size_t) ref
->index
;
120 if (caa_unlikely(index
>= table
->allocated_len
))
122 obj
= &table
->objects
[index
];
123 return obj
->wait_fd
[0];
127 int shm_get_wakeup_fd(struct lttng_ust_shm_handle
*handle
, struct shm_ref
*ref
)
129 struct shm_object_table
*table
= handle
->table
;
130 struct shm_object
*obj
;
133 index
= (size_t) ref
->index
;
134 if (caa_unlikely(index
>= table
->allocated_len
))
136 obj
= &table
->objects
[index
];
137 return obj
->wait_fd
[1];
141 int shm_close_wait_fd(struct lttng_ust_shm_handle
*handle
,
144 struct shm_object_table
*table
= handle
->table
;
145 struct shm_object
*obj
;
150 index
= (size_t) ref
->index
;
151 if (caa_unlikely(index
>= table
->allocated_len
))
153 obj
= &table
->objects
[index
];
154 wait_fd
= obj
->wait_fd
[0];
157 obj
->wait_fd
[0] = -1;
158 ret
= close(wait_fd
);
167 int shm_close_wakeup_fd(struct lttng_ust_shm_handle
*handle
,
170 struct shm_object_table
*table
= handle
->table
;
171 struct shm_object
*obj
;
176 index
= (size_t) ref
->index
;
177 if (caa_unlikely(index
>= table
->allocated_len
))
179 obj
= &table
->objects
[index
];
180 wakeup_fd
= obj
->wait_fd
[1];
183 obj
->wait_fd
[1] = -1;
184 ret
= close(wakeup_fd
);
193 int shm_get_shm_fd(struct lttng_ust_shm_handle
*handle
, struct shm_ref
*ref
)
195 struct shm_object_table
*table
= handle
->table
;
196 struct shm_object
*obj
;
199 index
= (size_t) ref
->index
;
200 if (caa_unlikely(index
>= table
->allocated_len
))
202 obj
= &table
->objects
[index
];
208 int shm_get_shm_size(struct lttng_ust_shm_handle
*handle
, struct shm_ref
*ref
,
211 struct shm_object_table
*table
= handle
->table
;
212 struct shm_object
*obj
;
215 index
= (size_t) ref
->index
;
216 if (caa_unlikely(index
>= table
->allocated_len
))
218 obj
= &table
->objects
[index
];
219 *size
= obj
->memory_map_size
;
223 #endif /* _LIBRINGBUFFER_SHM_H */
This page took 0.05148 seconds and 4 git commands to generate.