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. */
19 struct lttng_ust_shm_handle
*channel_handle_create(void *data
,
20 uint64_t memory_map_size
, int wakeup_fd
);
21 /* channel_handle_add_stream - for UST. */
23 int 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
);
26 unsigned int channel_handle_get_nr_streams(struct lttng_ust_shm_handle
*handle
);
28 void channel_destroy(struct channel
*chan
, struct lttng_ust_shm_handle
*handle
,
32 * Pointer dereferencing. We don't trust the shm_ref, so we validate
33 * both the index and offset with known boundaries.
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.
41 char *_shmp_offset(struct shm_object_table
*table
, struct shm_ref
*ref
,
42 size_t idx
, size_t elem_size
)
44 struct shm_object
*obj
;
45 size_t objindex
, ref_offset
;
47 objindex
= (size_t) ref
->index
;
48 if (caa_unlikely(objindex
>= table
->allocated_len
))
50 obj
= &table
->objects
[objindex
];
51 ref_offset
= (size_t) ref
->offset
;
52 ref_offset
+= idx
* elem_size
;
53 /* Check if part of the element returned would exceed the limits. */
54 if (caa_unlikely(ref_offset
+ elem_size
> obj
->memory_map_size
))
56 return &obj
->memory_map
[ref_offset
];
59 #define shmp_index(handle, ref, index) \
61 __typeof__((ref)._type) ____ptr_ret; \
62 ____ptr_ret = (__typeof__(____ptr_ret)) _shmp_offset((handle)->table, &(ref)._ref, index, sizeof(*____ptr_ret)); \
66 #define shmp(handle, ref) shmp_index(handle, ref, 0)
69 void _set_shmp(struct shm_ref
*ref
, struct shm_ref src
)
74 #define set_shmp(ref, src) _set_shmp(&(ref)._ref, src)
76 struct shm_object_table
*shm_object_table_create(size_t max_nb_obj
);
77 struct shm_object
*shm_object_table_alloc(struct shm_object_table
*table
,
78 size_t memory_map_size
,
79 enum shm_object_type type
,
82 struct 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(). */
86 struct shm_object
*shm_object_table_append_mem(struct shm_object_table
*table
,
87 void *mem
, size_t memory_map_size
, int wakeup_fd
);
88 void shm_object_table_destroy(struct shm_object_table
*table
, int consumer
);
91 * zalloc_shm - allocate memory within a shm object.
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.
97 struct shm_ref
zalloc_shm(struct shm_object
*obj
, size_t len
);
98 void align_shm(struct shm_object
*obj
, size_t align
);
101 int shm_get_wait_fd(struct lttng_ust_shm_handle
*handle
, struct shm_ref
*ref
)
103 struct shm_object_table
*table
= handle
->table
;
104 struct shm_object
*obj
;
107 index
= (size_t) ref
->index
;
108 if (caa_unlikely(index
>= table
->allocated_len
))
110 obj
= &table
->objects
[index
];
111 return obj
->wait_fd
[0];
115 int shm_get_wakeup_fd(struct lttng_ust_shm_handle
*handle
, struct shm_ref
*ref
)
117 struct shm_object_table
*table
= handle
->table
;
118 struct shm_object
*obj
;
121 index
= (size_t) ref
->index
;
122 if (caa_unlikely(index
>= table
->allocated_len
))
124 obj
= &table
->objects
[index
];
125 return obj
->wait_fd
[1];
129 int shm_close_wait_fd(struct lttng_ust_shm_handle
*handle
,
132 struct shm_object_table
*table
= handle
->table
;
133 struct shm_object
*obj
;
138 index
= (size_t) ref
->index
;
139 if (caa_unlikely(index
>= table
->allocated_len
))
141 obj
= &table
->objects
[index
];
142 wait_fd
= obj
->wait_fd
[0];
145 obj
->wait_fd
[0] = -1;
146 ret
= close(wait_fd
);
155 int shm_close_wakeup_fd(struct lttng_ust_shm_handle
*handle
,
158 struct shm_object_table
*table
= handle
->table
;
159 struct shm_object
*obj
;
164 index
= (size_t) ref
->index
;
165 if (caa_unlikely(index
>= table
->allocated_len
))
167 obj
= &table
->objects
[index
];
168 wakeup_fd
= obj
->wait_fd
[1];
171 obj
->wait_fd
[1] = -1;
172 ret
= close(wakeup_fd
);
181 int shm_get_shm_fd(struct lttng_ust_shm_handle
*handle
, struct shm_ref
*ref
)
183 struct shm_object_table
*table
= handle
->table
;
184 struct shm_object
*obj
;
187 index
= (size_t) ref
->index
;
188 if (caa_unlikely(index
>= table
->allocated_len
))
190 obj
= &table
->objects
[index
];
196 int shm_get_shm_size(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
];
207 *size
= obj
->memory_map_size
;
211 #endif /* _LIBRINGBUFFER_SHM_H */
This page took 0.034056 seconds and 4 git commands to generate.