8987f72a30f3dc234cf2ca9d7b777c86b6197cf3
[lttng-ust.git] / libringbuffer / shm.h
1 #ifndef _LIBRINGBUFFER_SHM_H
2 #define _LIBRINGBUFFER_SHM_H
3
4 /*
5 * libringbuffer/shm.h
6 *
7 * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
8 *
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.
13 *
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.
18 *
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
22 */
23
24 #include <stdint.h>
25 #include <usterr-signal-safe.h>
26 #include <urcu/compiler.h>
27 #include "shm_types.h"
28
29 /*
30 * Pointer dereferencing. We don't trust the shm_ref, so we validate
31 * both the index and offset with known boundaries.
32 *
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.
37 */
38 static inline
39 char *_shmp_offset(struct shm_object_table *table, struct shm_ref *ref,
40 size_t idx, size_t elem_size)
41 {
42 struct shm_object *obj;
43 size_t objindex, ref_offset;
44
45 objindex = (size_t) ref->index;
46 if (caa_unlikely(objindex >= table->allocated_len))
47 return NULL;
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))
53 return NULL;
54 return &obj->memory_map[ref_offset];
55 }
56
57 #define shmp_index(handle, ref, index) \
58 ({ \
59 __typeof__((ref)._type) ____ptr_ret; \
60 ____ptr_ret = (__typeof__(____ptr_ret)) _shmp_offset((handle)->table, &(ref)._ref, index, sizeof(*____ptr_ret)); \
61 ____ptr_ret; \
62 })
63
64 #define shmp(handle, ref) shmp_index(handle, ref, 0)
65
66 static inline
67 void _set_shmp(struct shm_ref *ref, struct shm_ref src)
68 {
69 *ref = src;
70 }
71
72 #define set_shmp(ref, src) _set_shmp(&(ref)._ref, src)
73
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);
80
81 /*
82 * zalloc_shm - allocate memory within a shm object.
83 *
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.
87 */
88 struct shm_ref zalloc_shm(struct shm_object *obj, size_t len);
89 void align_shm(struct shm_object *obj, size_t align);
90
91 static inline
92 int shm_get_wakeup_fd(struct lttng_ust_shm_handle *handle, struct shm_ref *ref)
93 {
94 struct shm_object_table *table = handle->table;
95 struct shm_object *obj;
96 size_t index;
97
98 index = (size_t) ref->index;
99 if (caa_unlikely(index >= table->allocated_len))
100 return -EPERM;
101 obj = &table->objects[index];
102 return obj->wait_fd[1];
103
104 }
105
106 static inline
107 int shm_get_wait_fd(struct lttng_ust_shm_handle *handle, struct shm_ref *ref)
108 {
109 struct shm_object_table *table = handle->table;
110 struct shm_object *obj;
111 size_t index;
112
113 index = (size_t) ref->index;
114 if (caa_unlikely(index >= table->allocated_len))
115 return -EPERM;
116 obj = &table->objects[index];
117 return obj->wait_fd[0];
118 }
119
120 static inline
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)
123 {
124 struct shm_object_table *table = handle->table;
125 struct shm_object *obj;
126 size_t index;
127
128 index = (size_t) ref->index;
129 if (caa_unlikely(index >= table->allocated_len))
130 return -EPERM;
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;
135 return 0;
136 }
137
138 #endif /* _LIBRINGBUFFER_SHM_H */
This page took 0.031044 seconds and 3 git commands to generate.