| 1 | #ifndef _LIBCOUNTER_SHM_H |
| 2 | #define _LIBCOUNTER_SHM_H |
| 3 | |
| 4 | /* |
| 5 | * libcounter/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 <stddef.h> |
| 25 | #include <stdint.h> |
| 26 | #include <unistd.h> |
| 27 | #include <usterr-signal-safe.h> |
| 28 | #include <urcu/compiler.h> |
| 29 | #include "shm_types.h" |
| 30 | |
| 31 | /* lttng_counter_handle_create - for UST. */ |
| 32 | extern |
| 33 | struct lttng_counter_shm_handle *lttng_counter_handle_create(void *data, |
| 34 | uint64_t memory_map_size, int wakeup_fd); |
| 35 | /* lttng_counter_handle_add_cpu - for UST. */ |
| 36 | extern |
| 37 | int lttng_counter_handle_add_cpu(struct lttng_counter_shm_handle *handle, |
| 38 | int shm_fd, uint32_t cpu_nr, |
| 39 | uint64_t memory_map_size); |
| 40 | unsigned int lttng_counter_handle_get_nr_cpus(struct lttng_counter_shm_handle *handle); |
| 41 | |
| 42 | /* |
| 43 | * Pointer dereferencing. We don't trust the shm_ref, so we validate |
| 44 | * both the index and offset with known boundaries. |
| 45 | * |
| 46 | * "shmp" and "shmp_index" guarantee that it's safe to use the pointer |
| 47 | * target type, even in the occurrence of shm_ref modification by an |
| 48 | * untrusted process having write access to the shm_ref. We return a |
| 49 | * NULL pointer if the ranges are invalid. |
| 50 | */ |
| 51 | static inline |
| 52 | char *_lttng_counter_shmp_offset(struct lttng_counter_shm_object_table *table, |
| 53 | struct lttng_counter_shm_ref *ref, |
| 54 | size_t idx, size_t elem_size) |
| 55 | { |
| 56 | struct lttng_counter_shm_object *obj; |
| 57 | size_t objindex, ref_offset; |
| 58 | |
| 59 | objindex = (size_t) ref->index; |
| 60 | if (caa_unlikely(objindex >= table->allocated_len)) |
| 61 | return NULL; |
| 62 | obj = &table->objects[objindex]; |
| 63 | ref_offset = (size_t) ref->offset; |
| 64 | ref_offset += idx * elem_size; |
| 65 | /* Check if part of the element returned would exceed the limits. */ |
| 66 | if (caa_unlikely(ref_offset + elem_size > obj->memory_map_size)) |
| 67 | return NULL; |
| 68 | return &obj->memory_map[ref_offset]; |
| 69 | } |
| 70 | |
| 71 | #define lttng_counter_shmp_index(handle, ref, index) \ |
| 72 | ({ \ |
| 73 | __typeof__((ref)._type) ____ptr_ret; \ |
| 74 | ____ptr_ret = (__typeof__(____ptr_ret)) _lttng_counter_shmp_offset((handle)->table, &(ref)._ref, index, sizeof(*____ptr_ret)); \ |
| 75 | ____ptr_ret; \ |
| 76 | }) |
| 77 | |
| 78 | #define lttng_counter_shmp(handle, ref) lttng_counter_shmp_index(handle, ref, 0) |
| 79 | |
| 80 | static inline |
| 81 | void _lttng_counter_set_shmp(struct lttng_counter_shm_ref *ref, struct lttng_counter_shm_ref src) |
| 82 | { |
| 83 | *ref = src; |
| 84 | } |
| 85 | |
| 86 | #define lttng_counter_set_shmp(ref, src) _lttng_counter_set_shmp(&(ref)._ref, src) |
| 87 | |
| 88 | struct lttng_counter_shm_object_table *lttng_counter_shm_object_table_create(size_t max_nb_obj); |
| 89 | struct lttng_counter_shm_object *lttng_counter_shm_object_table_alloc(struct lttng_counter_shm_object_table *table, |
| 90 | size_t memory_map_size, |
| 91 | enum lttng_counter_shm_object_type type, |
| 92 | const int cpu_fd, |
| 93 | int cpu); |
| 94 | struct lttng_counter_shm_object *lttng_counter_shm_object_table_append_shm(struct lttng_counter_shm_object_table *table, |
| 95 | int shm_fd, size_t memory_map_size); |
| 96 | /* mem ownership is passed to lttng_counter_shm_object_table_append_mem(). */ |
| 97 | struct lttng_counter_shm_object *lttng_counter_shm_object_table_append_mem(struct lttng_counter_shm_object_table *table, |
| 98 | void *mem, size_t memory_map_size); |
| 99 | void lttng_counter_shm_object_table_destroy(struct lttng_counter_shm_object_table *table, int consumer); |
| 100 | |
| 101 | /* |
| 102 | * lttng_counter_zalloc_shm - allocate memory within a shm object. |
| 103 | * |
| 104 | * Shared memory is already zeroed by shmget. |
| 105 | * *NOT* multithread-safe (should be protected by mutex). |
| 106 | * Returns a -1, -1 tuple on error. |
| 107 | */ |
| 108 | struct lttng_counter_shm_ref lttng_counter_zalloc_shm(struct lttng_counter_shm_object *obj, size_t len); |
| 109 | void lttng_counter_align_shm(struct lttng_counter_shm_object *obj, size_t align); |
| 110 | |
| 111 | static inline |
| 112 | int lttng_counter_shm_get_shm_fd(struct lttng_counter_shm_handle *handle, struct lttng_counter_shm_ref *ref) |
| 113 | { |
| 114 | struct lttng_counter_shm_object_table *table = handle->table; |
| 115 | struct lttng_counter_shm_object *obj; |
| 116 | size_t index; |
| 117 | |
| 118 | index = (size_t) ref->index; |
| 119 | if (caa_unlikely(index >= table->allocated_len)) |
| 120 | return -EPERM; |
| 121 | obj = &table->objects[index]; |
| 122 | return obj->shm_fd; |
| 123 | } |
| 124 | |
| 125 | |
| 126 | static inline |
| 127 | int lttng_counter_shm_get_shm_size(struct lttng_counter_shm_handle *handle, struct lttng_counter_shm_ref *ref, |
| 128 | uint64_t *size) |
| 129 | { |
| 130 | struct lttng_counter_shm_object_table *table = handle->table; |
| 131 | struct lttng_counter_shm_object *obj; |
| 132 | size_t index; |
| 133 | |
| 134 | index = (size_t) ref->index; |
| 135 | if (caa_unlikely(index >= table->allocated_len)) |
| 136 | return -EPERM; |
| 137 | obj = &table->objects[index]; |
| 138 | *size = obj->memory_map_size; |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | #endif /* _LIBCOUNTER_SHM_H */ |