Cygwin: Introduce new LTTNG_UST_STREAM_PIPE command to open wakeup pipe
[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 #include <fcntl.h>
29
30 /*
31 * Pointer dereferencing. We don't trust the shm_ref, so we validate
32 * both the index and offset with known boundaries.
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.
38 */
39 static inline
40 char *_shmp_offset(struct shm_object_table *table, struct shm_ref *ref,
41 size_t idx, size_t elem_size)
42 {
43 struct shm_object *obj;
44 size_t objindex, ref_offset;
45
46 objindex = (size_t) ref->index;
47 if (caa_unlikely(objindex >= table->allocated_len))
48 return NULL;
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))
54 return NULL;
55 return &obj->memory_map[ref_offset];
56 }
57
58 #define shmp_index(handle, ref, index) \
59 ({ \
60 __typeof__((ref)._type) ____ptr_ret; \
61 ____ptr_ret = (__typeof__(____ptr_ret)) _shmp_offset((handle)->table, &(ref)._ref, index, sizeof(*____ptr_ret)); \
62 ____ptr_ret; \
63 })
64
65 #define shmp(handle, ref) shmp_index(handle, ref, 0)
66
67 static inline
68 void _set_shmp(struct shm_ref *ref, struct shm_ref src)
69 {
70 *ref = src;
71 }
72
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);
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);
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
92 static inline
93 int shm_get_wakeup_fd(struct lttng_ust_shm_handle *handle, struct shm_ref *ref)
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;
100 if (caa_unlikely(index >= table->allocated_len))
101 return -EPERM;
102 obj = &table->objects[index];
103
104 return obj->wait_fd[1];
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];
120
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;
134 }
135
136 static inline
137 int shm_get_wait_fd(struct lttng_ust_shm_handle *handle, struct shm_ref *ref)
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;
144 if (caa_unlikely(index >= table->allocated_len))
145 return -EPERM;
146 obj = &table->objects[index];
147 return obj->wait_fd[0];
148 }
149
150 static inline
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)
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;
161 if (caa_unlikely(index >= table->allocated_len))
162 return -EPERM;
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;
169 return 0;
170 }
171
172 #endif /* _LIBRINGBUFFER_SHM_H */
This page took 0.032091 seconds and 4 git commands to generate.