Commit | Line | Data |
---|---|---|
a6352fd4 MD |
1 | #ifndef _LIBRINGBUFFER_SHM_H |
2 | #define _LIBRINGBUFFER_SHM_H | |
3 | ||
4 | /* | |
5 | * libringbuffer/shm.h | |
6 | * | |
e92f3e28 | 7 | * Copyright (C) 2011-2012 Mathieu Desnoyers <mathieu.desnoyers@efficios.com> |
a6352fd4 | 8 | * |
e92f3e28 MD |
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 | |
a6352fd4 MD |
22 | */ |
23 | ||
24 | #include <stdint.h> | |
44c72f10 | 25 | #include <usterr-signal-safe.h> |
35897f8b | 26 | #include <urcu/compiler.h> |
1d498196 | 27 | #include "shm_types.h" |
616d3093 | 28 | #include <fcntl.h> |
a6352fd4 | 29 | |
a6352fd4 | 30 | /* |
1d498196 MD |
31 | * Pointer dereferencing. We don't trust the shm_ref, so we validate |
32 | * both the index and offset with known boundaries. | |
ed5426d3 MD |
33 | * |
34 | * "shmp" and "shmp_index" guarantee that it's safe to use the pointer | |
35 | * target type, even in the occurrence of shm_ref modification by an | |
36 | * untrusted process having write access to the shm_ref. We return a | |
37 | * NULL pointer if the ranges are invalid. | |
a6352fd4 | 38 | */ |
a6352fd4 | 39 | static inline |
4746ae29 | 40 | char *_shmp_offset(struct shm_object_table *table, struct shm_ref *ref, |
cba4b7a3 | 41 | size_t idx, size_t elem_size) |
a6352fd4 | 42 | { |
1d498196 | 43 | struct shm_object *obj; |
cba4b7a3 | 44 | size_t objindex, ref_offset; |
a6352fd4 | 45 | |
cba4b7a3 | 46 | objindex = (size_t) ref->index; |
b5a3dfa5 | 47 | if (caa_unlikely(objindex >= table->allocated_len)) |
1d498196 | 48 | return NULL; |
cba4b7a3 | 49 | obj = &table->objects[objindex]; |
4746ae29 | 50 | ref_offset = (size_t) ref->offset; |
cba4b7a3 MD |
51 | ref_offset += idx * elem_size; |
52 | /* Check if part of the element returned would exceed the limits. */ | |
b5a3dfa5 | 53 | if (caa_unlikely(ref_offset + elem_size > obj->memory_map_size)) |
a6352fd4 | 54 | return NULL; |
4746ae29 | 55 | return &obj->memory_map[ref_offset]; |
a6352fd4 MD |
56 | } |
57 | ||
cba4b7a3 | 58 | #define shmp_index(handle, ref, index) \ |
1d498196 MD |
59 | ({ \ |
60 | __typeof__((ref)._type) ____ptr_ret; \ | |
cba4b7a3 | 61 | ____ptr_ret = (__typeof__(____ptr_ret)) _shmp_offset((handle)->table, &(ref)._ref, index, sizeof(*____ptr_ret)); \ |
1d498196 MD |
62 | ____ptr_ret; \ |
63 | }) | |
64 | ||
4746ae29 MD |
65 | #define shmp(handle, ref) shmp_index(handle, ref, 0) |
66 | ||
431d5cf0 | 67 | static inline |
1d498196 | 68 | void _set_shmp(struct shm_ref *ref, struct shm_ref src) |
431d5cf0 | 69 | { |
1d498196 | 70 | *ref = src; |
431d5cf0 MD |
71 | } |
72 | ||
1d498196 MD |
73 | #define set_shmp(ref, src) _set_shmp(&(ref)._ref, src) |
74 | ||
75 | struct shm_object_table *shm_object_table_create(size_t max_nb_obj); | |
193183fb MD |
76 | struct shm_object *shm_object_table_append_shadow(struct shm_object_table *table, |
77 | int shm_fd, int wait_fd, size_t memory_map_size); | |
1d498196 MD |
78 | void shm_object_table_destroy(struct shm_object_table *table); |
79 | struct shm_object *shm_object_table_append(struct shm_object_table *table, | |
80 | size_t memory_map_size); | |
81 | ||
82 | /* | |
83 | * zalloc_shm - allocate memory within a shm object. | |
84 | * | |
85 | * Shared memory is already zeroed by shmget. | |
86 | * *NOT* multithread-safe (should be protected by mutex). | |
87 | * Returns a -1, -1 tuple on error. | |
88 | */ | |
89 | struct shm_ref zalloc_shm(struct shm_object *obj, size_t len); | |
90 | void align_shm(struct shm_object *obj, size_t align); | |
91 | ||
5d61a504 | 92 | static inline |
38fae1d3 | 93 | int shm_get_wakeup_fd(struct lttng_ust_shm_handle *handle, struct shm_ref *ref) |
5d61a504 MD |
94 | { |
95 | struct shm_object_table *table = handle->table; | |
96 | struct shm_object *obj; | |
97 | size_t index; | |
98 | ||
99 | index = (size_t) ref->index; | |
b5a3dfa5 | 100 | if (caa_unlikely(index >= table->allocated_len)) |
5d61a504 MD |
101 | return -EPERM; |
102 | obj = &table->objects[index]; | |
616d3093 | 103 | |
5d61a504 | 104 | return obj->wait_fd[1]; |
616d3093 CB |
105 | } |
106 | ||
107 | static inline | |
108 | int shm_open_wakeup_pipe(struct lttng_ust_shm_handle *handle, struct shm_ref *ref) | |
109 | { | |
110 | struct shm_object_table *table = handle->table; | |
111 | struct shm_object *obj; | |
112 | size_t index; | |
113 | int fd, ret = -1; | |
114 | ||
115 | index = (size_t) ref->index; | |
116 | if (caa_unlikely(index >= table->allocated_len)) | |
117 | return -EPERM; | |
118 | ||
119 | obj = &table->objects[index]; | |
5d61a504 | 120 | |
616d3093 CB |
121 | if (obj->wait_fd[1] < 0 && obj->wait_pipe_path != NULL) { |
122 | fd = open(obj->wait_pipe_path, O_WRONLY); | |
123 | if (fd < 0) { | |
124 | ret = -1; | |
125 | } else { | |
126 | /* FIXME: What if fcntl fail? */ | |
127 | fcntl(fd, F_SETFD, FD_CLOEXEC); | |
128 | obj->wait_fd[1] = fd; | |
129 | ret = 0; | |
130 | } | |
131 | } | |
132 | ||
133 | return ret; | |
5d61a504 MD |
134 | } |
135 | ||
136 | static inline | |
38fae1d3 | 137 | int shm_get_wait_fd(struct lttng_ust_shm_handle *handle, struct shm_ref *ref) |
5d61a504 MD |
138 | { |
139 | struct shm_object_table *table = handle->table; | |
140 | struct shm_object *obj; | |
141 | size_t index; | |
142 | ||
143 | index = (size_t) ref->index; | |
b5a3dfa5 | 144 | if (caa_unlikely(index >= table->allocated_len)) |
5d61a504 MD |
145 | return -EPERM; |
146 | obj = &table->objects[index]; | |
147 | return obj->wait_fd[0]; | |
148 | } | |
149 | ||
381c0f1e | 150 | static inline |
38fae1d3 | 151 | int shm_get_object_data(struct lttng_ust_shm_handle *handle, struct shm_ref *ref, |
bf5ff35e CB |
152 | int **shm_fd, char **shm_path, |
153 | int **wait_fd, char **wait_pipe_path, | |
154 | uint64_t **memory_map_size) | |
381c0f1e MD |
155 | { |
156 | struct shm_object_table *table = handle->table; | |
157 | struct shm_object *obj; | |
158 | size_t index; | |
159 | ||
160 | index = (size_t) ref->index; | |
b5a3dfa5 | 161 | if (caa_unlikely(index >= table->allocated_len)) |
381c0f1e MD |
162 | return -EPERM; |
163 | obj = &table->objects[index]; | |
bf5ff35e CB |
164 | *shm_fd = &obj->shm_fd; |
165 | *shm_path = obj->shm_path; | |
166 | *wait_fd = &obj->wait_fd[0]; | |
167 | *wait_pipe_path = obj->wait_pipe_path; | |
ef9ff354 | 168 | *memory_map_size = &obj->allocated_len; |
381c0f1e MD |
169 | return 0; |
170 | } | |
171 | ||
a6352fd4 | 172 | #endif /* _LIBRINGBUFFER_SHM_H */ |