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"
31 * Pointer dereferencing. We don't trust the shm_ref, so we validate
32 * both the index and offset with known boundaries.
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.
40 char *_shmp_offset(struct shm_object_table
*table
, struct shm_ref
*ref
,
41 size_t idx
, size_t elem_size
)
43 struct shm_object
*obj
;
44 size_t objindex
, ref_offset
;
46 objindex
= (size_t) ref
->index
;
47 if (caa_unlikely(objindex
>= table
->allocated_len
))
49 obj
= &table
->objects
[objindex
];
50 ref_offset
= (size_t) ref
->offset
;
51 ref_offset
+= idx
* elem_size
;
52 /* Check if part of the element returned would exceed the limits. */
53 if (caa_unlikely(ref_offset
+ elem_size
> obj
->memory_map_size
))
55 return &obj
->memory_map
[ref_offset
];
58 #define shmp_index(handle, ref, index) \
60 __typeof__((ref)._type) ____ptr_ret; \
61 ____ptr_ret = (__typeof__(____ptr_ret)) _shmp_offset((handle)->table, &(ref)._ref, index, sizeof(*____ptr_ret)); \
65 #define shmp(handle, ref) shmp_index(handle, ref, 0)
68 void _set_shmp(struct shm_ref
*ref
, struct shm_ref src
)
73 #define set_shmp(ref, src) _set_shmp(&(ref)._ref, src)
75 struct shm_object_table
*shm_object_table_create(size_t max_nb_obj
);
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
);
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
);
83 * zalloc_shm - allocate memory within a shm object.
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.
89 struct shm_ref
zalloc_shm(struct shm_object
*obj
, size_t len
);
90 void align_shm(struct shm_object
*obj
, size_t align
);
93 int shm_get_wakeup_fd(struct lttng_ust_shm_handle
*handle
, struct shm_ref
*ref
)
95 struct shm_object_table
*table
= handle
->table
;
96 struct shm_object
*obj
;
99 index
= (size_t) ref
->index
;
100 if (caa_unlikely(index
>= table
->allocated_len
))
102 obj
= &table
->objects
[index
];
104 return obj
->wait_fd
[1];
108 int shm_open_wakeup_pipe(struct lttng_ust_shm_handle
*handle
, struct shm_ref
*ref
)
110 struct shm_object_table
*table
= handle
->table
;
111 struct shm_object
*obj
;
115 index
= (size_t) ref
->index
;
116 if (caa_unlikely(index
>= table
->allocated_len
))
119 obj
= &table
->objects
[index
];
121 if (obj
->wait_fd
[1] < 0 && obj
->wait_pipe_path
!= NULL
) {
122 fd
= open(obj
->wait_pipe_path
, O_WRONLY
);
126 /* FIXME: What if fcntl fail? */
127 fcntl(fd
, F_SETFD
, FD_CLOEXEC
);
128 obj
->wait_fd
[1] = fd
;
137 int shm_get_wait_fd(struct lttng_ust_shm_handle
*handle
, struct shm_ref
*ref
)
139 struct shm_object_table
*table
= handle
->table
;
140 struct shm_object
*obj
;
143 index
= (size_t) ref
->index
;
144 if (caa_unlikely(index
>= table
->allocated_len
))
146 obj
= &table
->objects
[index
];
147 return obj
->wait_fd
[0];
151 int shm_get_object_data(struct lttng_ust_shm_handle
*handle
, struct shm_ref
*ref
,
152 int **shm_fd
, char **shm_path
,
153 int **wait_fd
, char **wait_pipe_path
,
154 uint64_t **memory_map_size
)
156 struct shm_object_table
*table
= handle
->table
;
157 struct shm_object
*obj
;
160 index
= (size_t) ref
->index
;
161 if (caa_unlikely(index
>= table
->allocated_len
))
163 obj
= &table
->objects
[index
];
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
;
168 *memory_map_size
= &obj
->allocated_len
;
172 #endif /* _LIBRINGBUFFER_SHM_H */
This page took 0.039639 seconds and 4 git commands to generate.