1 #ifndef _LIBRINGBUFFER_SHM_H
2 #define _LIBRINGBUFFER_SHM_H
7 * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; only
12 * version 2.1 of the License.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 #include <usterr-signal-safe.h>
26 #include <urcu/compiler.h>
27 #include "shm_types.h"
30 * Pointer dereferencing. We don't trust the shm_ref, so we validate
31 * both the index and offset with known boundaries.
33 * "shmp" and "shmp_index" guarantee that it's safe to use the pointer
34 * target type, even in the occurrence of shm_ref modification by an
35 * untrusted process having write access to the shm_ref. We return a
36 * NULL pointer if the ranges are invalid.
39 char *_shmp_offset(struct shm_object_table
*table
, struct shm_ref
*ref
,
40 size_t idx
, size_t elem_size
)
42 struct shm_object
*obj
;
43 size_t objindex
, ref_offset
;
45 objindex
= (size_t) ref
->index
;
46 if (caa_unlikely(objindex
>= table
->allocated_len
))
48 obj
= &table
->objects
[objindex
];
49 ref_offset
= (size_t) ref
->offset
;
50 ref_offset
+= idx
* elem_size
;
51 /* Check if part of the element returned would exceed the limits. */
52 if (caa_unlikely(ref_offset
+ elem_size
> obj
->memory_map_size
))
54 return &obj
->memory_map
[ref_offset
];
57 #define shmp_index(handle, ref, index) \
59 __typeof__((ref)._type) ____ptr_ret; \
60 ____ptr_ret = (__typeof__(____ptr_ret)) _shmp_offset((handle)->table, &(ref)._ref, index, sizeof(*____ptr_ret)); \
64 #define shmp(handle, ref) shmp_index(handle, ref, 0)
67 void _set_shmp(struct shm_ref
*ref
, struct shm_ref src
)
72 #define set_shmp(ref, src) _set_shmp(&(ref)._ref, src)
74 struct shm_object_table
*shm_object_table_create(size_t max_nb_obj
);
75 struct shm_object
*shm_object_table_append_shadow(struct shm_object_table
*table
,
76 int shm_fd
, int wait_fd
, size_t memory_map_size
);
77 void shm_object_table_destroy(struct shm_object_table
*table
);
78 struct shm_object
*shm_object_table_append(struct shm_object_table
*table
,
79 size_t memory_map_size
);
82 * zalloc_shm - allocate memory within a shm object.
84 * Shared memory is already zeroed by shmget.
85 * *NOT* multithread-safe (should be protected by mutex).
86 * Returns a -1, -1 tuple on error.
88 struct shm_ref
zalloc_shm(struct shm_object
*obj
, size_t len
);
89 void align_shm(struct shm_object
*obj
, size_t align
);
92 int shm_get_wakeup_fd(struct lttng_ust_shm_handle
*handle
, struct shm_ref
*ref
)
94 struct shm_object_table
*table
= handle
->table
;
95 struct shm_object
*obj
;
98 index
= (size_t) ref
->index
;
99 if (caa_unlikely(index
>= table
->allocated_len
))
101 obj
= &table
->objects
[index
];
102 return obj
->wait_fd
[1];
107 int shm_get_wait_fd(struct lttng_ust_shm_handle
*handle
, struct shm_ref
*ref
)
109 struct shm_object_table
*table
= handle
->table
;
110 struct shm_object
*obj
;
113 index
= (size_t) ref
->index
;
114 if (caa_unlikely(index
>= table
->allocated_len
))
116 obj
= &table
->objects
[index
];
117 return obj
->wait_fd
[0];
121 int shm_get_object_data(struct lttng_ust_shm_handle
*handle
, struct shm_ref
*ref
,
122 int **shm_fd
, int **wait_fd
, uint64_t **memory_map_size
)
124 struct shm_object_table
*table
= handle
->table
;
125 struct shm_object
*obj
;
128 index
= (size_t) ref
->index
;
129 if (caa_unlikely(index
>= table
->allocated_len
))
131 obj
= &table
->objects
[index
];
132 *shm_fd
= &obj
->shm_fd
;
133 *wait_fd
= &obj
->wait_fd
[0];
134 *memory_map_size
= &obj
->allocated_len
;
138 #endif /* _LIBRINGBUFFER_SHM_H */
This page took 0.049627 seconds and 4 git commands to generate.