| 1 | /* |
| 2 | * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com> |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU Lesser General Public License, version 2.1 only, |
| 6 | * as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This library is distributed in the hope that it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 11 | * for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU Lesser General Public License |
| 14 | * along with this library; if not, write to the Free Software Foundation, |
| 15 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 16 | */ |
| 17 | |
| 18 | #include <lttng/trigger/trigger-internal.h> |
| 19 | #include <lttng/condition/condition-internal.h> |
| 20 | #include <lttng/action/action-internal.h> |
| 21 | #include <common/error.h> |
| 22 | #include <assert.h> |
| 23 | |
| 24 | LTTNG_HIDDEN |
| 25 | bool lttng_trigger_validate(struct lttng_trigger *trigger) |
| 26 | { |
| 27 | bool valid; |
| 28 | |
| 29 | if (!trigger) { |
| 30 | valid = false; |
| 31 | goto end; |
| 32 | } |
| 33 | |
| 34 | valid = lttng_condition_validate(trigger->condition) && |
| 35 | lttng_action_validate(trigger->action); |
| 36 | end: |
| 37 | return valid; |
| 38 | } |
| 39 | |
| 40 | struct lttng_trigger *lttng_trigger_create( |
| 41 | struct lttng_condition *condition, |
| 42 | struct lttng_action *action) |
| 43 | { |
| 44 | struct lttng_trigger *trigger = NULL; |
| 45 | |
| 46 | if (!condition || !action) { |
| 47 | goto end; |
| 48 | } |
| 49 | |
| 50 | trigger = zmalloc(sizeof(struct lttng_trigger)); |
| 51 | if (!trigger) { |
| 52 | goto end; |
| 53 | } |
| 54 | |
| 55 | trigger->condition = condition; |
| 56 | trigger->action = action; |
| 57 | CDS_INIT_LIST_HEAD(&trigger->list_node); |
| 58 | end: |
| 59 | return trigger; |
| 60 | } |
| 61 | |
| 62 | struct lttng_condition *lttng_trigger_get_condition( |
| 63 | struct lttng_trigger *trigger) |
| 64 | { |
| 65 | return trigger ? trigger->condition : NULL; |
| 66 | } |
| 67 | |
| 68 | extern struct lttng_action *lttng_trigger_get_action( |
| 69 | struct lttng_trigger *trigger) |
| 70 | { |
| 71 | return trigger ? trigger->action : NULL; |
| 72 | } |
| 73 | |
| 74 | void lttng_trigger_destroy(struct lttng_trigger *trigger) |
| 75 | { |
| 76 | if (!trigger) { |
| 77 | return; |
| 78 | } |
| 79 | |
| 80 | lttng_condition_destroy(trigger->condition); |
| 81 | lttng_action_destroy(trigger->action); |
| 82 | free(trigger); |
| 83 | } |
| 84 | |
| 85 | LTTNG_HIDDEN |
| 86 | ssize_t lttng_trigger_create_from_buffer(const char *buf, |
| 87 | struct lttng_trigger **trigger) |
| 88 | { |
| 89 | ssize_t ret, offset = 0, condition_size, action_size; |
| 90 | struct lttng_condition *condition = NULL; |
| 91 | struct lttng_action *action = NULL; |
| 92 | struct lttng_trigger_comm *trigger_comm; |
| 93 | |
| 94 | if (!buf || !trigger) { |
| 95 | ret = -1; |
| 96 | goto end; |
| 97 | } |
| 98 | |
| 99 | /* lttng_trigger_comm header */ |
| 100 | trigger_comm = (struct lttng_trigger_comm *) buf; |
| 101 | offset += sizeof(*trigger_comm); |
| 102 | |
| 103 | /* struct lttng_condition */ |
| 104 | condition_size = lttng_condition_create_from_buffer(buf + offset, |
| 105 | &condition); |
| 106 | if (condition_size < 0) { |
| 107 | ret = condition_size; |
| 108 | goto end; |
| 109 | } |
| 110 | offset += condition_size; |
| 111 | |
| 112 | /* struct lttng_action */ |
| 113 | action_size = lttng_action_create_from_buffer(buf + offset, &action); |
| 114 | if (action_size < 0) { |
| 115 | ret = action_size; |
| 116 | goto end; |
| 117 | } |
| 118 | offset += action_size; |
| 119 | |
| 120 | /* Unexpected size of inner-elements; the buffer is corrupted. */ |
| 121 | if ((ssize_t) trigger_comm->length != condition_size + action_size) { |
| 122 | ret = -1; |
| 123 | goto error; |
| 124 | } |
| 125 | |
| 126 | *trigger = lttng_trigger_create(condition, action); |
| 127 | if (!*trigger) { |
| 128 | goto error; |
| 129 | } |
| 130 | ret = offset; |
| 131 | end: |
| 132 | return ret; |
| 133 | error: |
| 134 | lttng_condition_destroy(condition); |
| 135 | lttng_action_destroy(action); |
| 136 | return ret; |
| 137 | } |
| 138 | |
| 139 | /* |
| 140 | * Returns the size of a trigger (header + condition + action). |
| 141 | * Both elements are stored contiguously, see their "*_comm" structure |
| 142 | * for the detailed format. |
| 143 | */ |
| 144 | LTTNG_HIDDEN |
| 145 | ssize_t lttng_trigger_serialize(struct lttng_trigger *trigger, char *buf) |
| 146 | { |
| 147 | struct lttng_trigger_comm trigger_comm; |
| 148 | ssize_t action_size, condition_size, offset = 0, ret; |
| 149 | |
| 150 | if (!trigger) { |
| 151 | ret = -1; |
| 152 | goto end; |
| 153 | } |
| 154 | |
| 155 | offset += sizeof(trigger_comm); |
| 156 | condition_size = lttng_condition_serialize(trigger->condition, |
| 157 | buf ? (buf + offset) : NULL); |
| 158 | if (condition_size < 0) { |
| 159 | ret = -1; |
| 160 | goto end; |
| 161 | } |
| 162 | offset += condition_size; |
| 163 | |
| 164 | action_size = lttng_action_serialize(trigger->action, |
| 165 | buf ? (buf + offset) : NULL); |
| 166 | if (action_size < 0) { |
| 167 | ret = -1; |
| 168 | goto end; |
| 169 | } |
| 170 | offset += action_size; |
| 171 | |
| 172 | if (buf) { |
| 173 | trigger_comm.length = (uint32_t) (condition_size + action_size); |
| 174 | memcpy(buf, &trigger_comm, sizeof(trigger_comm)); |
| 175 | } |
| 176 | ret = offset; |
| 177 | end: |
| 178 | return ret; |
| 179 | } |