Cygwin: Pass file paths instead of file descriptors over UNIX sockets
[lttng-ust.git] / libringbuffer / shm.h
CommitLineData
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"
a6352fd4 28
a6352fd4 29/*
1d498196
MD
30 * Pointer dereferencing. We don't trust the shm_ref, so we validate
31 * both the index and offset with known boundaries.
ed5426d3
MD
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.
a6352fd4 37 */
a6352fd4 38static inline
4746ae29 39char *_shmp_offset(struct shm_object_table *table, struct shm_ref *ref,
cba4b7a3 40 size_t idx, size_t elem_size)
a6352fd4 41{
1d498196 42 struct shm_object *obj;
cba4b7a3 43 size_t objindex, ref_offset;
a6352fd4 44
cba4b7a3 45 objindex = (size_t) ref->index;
b5a3dfa5 46 if (caa_unlikely(objindex >= table->allocated_len))
1d498196 47 return NULL;
cba4b7a3 48 obj = &table->objects[objindex];
4746ae29 49 ref_offset = (size_t) ref->offset;
cba4b7a3
MD
50 ref_offset += idx * elem_size;
51 /* Check if part of the element returned would exceed the limits. */
b5a3dfa5 52 if (caa_unlikely(ref_offset + elem_size > obj->memory_map_size))
a6352fd4 53 return NULL;
4746ae29 54 return &obj->memory_map[ref_offset];
a6352fd4
MD
55}
56
cba4b7a3 57#define shmp_index(handle, ref, index) \
1d498196
MD
58 ({ \
59 __typeof__((ref)._type) ____ptr_ret; \
cba4b7a3 60 ____ptr_ret = (__typeof__(____ptr_ret)) _shmp_offset((handle)->table, &(ref)._ref, index, sizeof(*____ptr_ret)); \
1d498196
MD
61 ____ptr_ret; \
62 })
63
4746ae29
MD
64#define shmp(handle, ref) shmp_index(handle, ref, 0)
65
431d5cf0 66static inline
1d498196 67void _set_shmp(struct shm_ref *ref, struct shm_ref src)
431d5cf0 68{
1d498196 69 *ref = src;
431d5cf0
MD
70}
71
1d498196
MD
72#define set_shmp(ref, src) _set_shmp(&(ref)._ref, src)
73
74struct shm_object_table *shm_object_table_create(size_t max_nb_obj);
193183fb
MD
75struct shm_object *shm_object_table_append_shadow(struct shm_object_table *table,
76 int shm_fd, int wait_fd, size_t memory_map_size);
1d498196
MD
77void shm_object_table_destroy(struct shm_object_table *table);
78struct 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 */
88struct shm_ref zalloc_shm(struct shm_object *obj, size_t len);
89void align_shm(struct shm_object *obj, size_t align);
90
5d61a504 91static inline
38fae1d3 92int shm_get_wakeup_fd(struct lttng_ust_shm_handle *handle, struct shm_ref *ref)
5d61a504
MD
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;
b5a3dfa5 99 if (caa_unlikely(index >= table->allocated_len))
5d61a504
MD
100 return -EPERM;
101 obj = &table->objects[index];
102 return obj->wait_fd[1];
103
104}
105
106static inline
38fae1d3 107int shm_get_wait_fd(struct lttng_ust_shm_handle *handle, struct shm_ref *ref)
5d61a504
MD
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;
b5a3dfa5 114 if (caa_unlikely(index >= table->allocated_len))
5d61a504
MD
115 return -EPERM;
116 obj = &table->objects[index];
117 return obj->wait_fd[0];
118}
119
381c0f1e 120static inline
38fae1d3 121int shm_get_object_data(struct lttng_ust_shm_handle *handle, struct shm_ref *ref,
bf5ff35e
CB
122 int **shm_fd, char **shm_path,
123 int **wait_fd, char **wait_pipe_path,
124 uint64_t **memory_map_size)
381c0f1e
MD
125{
126 struct shm_object_table *table = handle->table;
127 struct shm_object *obj;
128 size_t index;
129
130 index = (size_t) ref->index;
b5a3dfa5 131 if (caa_unlikely(index >= table->allocated_len))
381c0f1e
MD
132 return -EPERM;
133 obj = &table->objects[index];
bf5ff35e
CB
134 *shm_fd = &obj->shm_fd;
135 *shm_path = obj->shm_path;
136 *wait_fd = &obj->wait_fd[0];
137 *wait_pipe_path = obj->wait_pipe_path;
ef9ff354 138 *memory_map_size = &obj->allocated_len;
381c0f1e
MD
139 return 0;
140}
141
a6352fd4 142#endif /* _LIBRINGBUFFER_SHM_H */
This page took 0.0303 seconds and 4 git commands to generate.