trigger: use condition and action ref counting to ease internal objects management
[lttng-tools.git] / src / common / trigger.c
CommitLineData
a58c490f 1/*
ab5be9fa 2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
a58c490f 3 *
ab5be9fa 4 * SPDX-License-Identifier: LGPL-2.1-only
a58c490f 5 *
a58c490f
JG
6 */
7
8#include <lttng/trigger/trigger-internal.h>
9#include <lttng/condition/condition-internal.h>
10#include <lttng/action/action-internal.h>
3da864a9 11#include <common/credentials.h>
9e620ea7
JG
12#include <common/payload.h>
13#include <common/payload-view.h>
a58c490f 14#include <common/error.h>
3da864a9 15#include <common/optional.h>
a58c490f
JG
16#include <assert.h>
17
18LTTNG_HIDDEN
19bool lttng_trigger_validate(struct lttng_trigger *trigger)
20{
21 bool valid;
22
23 if (!trigger) {
24 valid = false;
25 goto end;
26 }
27
28 valid = lttng_condition_validate(trigger->condition) &&
29 lttng_action_validate(trigger->action);
30end:
31 return valid;
32}
33
34struct lttng_trigger *lttng_trigger_create(
35 struct lttng_condition *condition,
36 struct lttng_action *action)
37{
38 struct lttng_trigger *trigger = NULL;
39
40 if (!condition || !action) {
41 goto end;
42 }
43
44 trigger = zmalloc(sizeof(struct lttng_trigger));
45 if (!trigger) {
46 goto end;
47 }
48
7ca172c1 49 lttng_condition_get(condition);
a58c490f 50 trigger->condition = condition;
7ca172c1
JR
51
52 lttng_action_get(action);
a58c490f 53 trigger->action = action;
3da864a9 54
a58c490f
JG
55end:
56 return trigger;
57}
58
7ca172c1
JR
59/*
60 * Note: the lack of reference counting 'get' on the condition object is normal.
61 * This API was exposed as such in 2.11. The client is not expected to call
62 * lttng_condition_destroy on the returned object.
63 */
a58c490f
JG
64struct lttng_condition *lttng_trigger_get_condition(
65 struct lttng_trigger *trigger)
66{
67 return trigger ? trigger->condition : NULL;
68}
69
9b63a4aa
JG
70LTTNG_HIDDEN
71const struct lttng_condition *lttng_trigger_get_const_condition(
72 const struct lttng_trigger *trigger)
73{
74 return trigger->condition;
75}
76
7ca172c1
JR
77
78/*
79 * Note: the lack of reference counting 'get' on the action object is normal.
80 * This API was exposed as such in 2.11. The client is not expected to call
81 * lttng_action_destroy on the returned object.
82 */
e2ba1c78 83struct lttng_action *lttng_trigger_get_action(
a58c490f
JG
84 struct lttng_trigger *trigger)
85{
86 return trigger ? trigger->action : NULL;
87}
88
9b63a4aa
JG
89LTTNG_HIDDEN
90const struct lttng_action *lttng_trigger_get_const_action(
91 const struct lttng_trigger *trigger)
92{
93 return trigger->action;
94}
95
a58c490f
JG
96void lttng_trigger_destroy(struct lttng_trigger *trigger)
97{
7ca172c1
JR
98 struct lttng_action *action = lttng_trigger_get_action(trigger);
99 struct lttng_condition *condition =
100 lttng_trigger_get_condition(trigger);
101
a58c490f
JG
102 if (!trigger) {
103 return;
104 }
105
7ca172c1
JR
106 assert(action);
107 assert(condition);
108
109 /* Release ownership. */
110 lttng_action_put(action);
111 lttng_condition_put(condition);
112
a58c490f
JG
113 free(trigger);
114}
115
116LTTNG_HIDDEN
c0a66c84
JG
117ssize_t lttng_trigger_create_from_payload(
118 struct lttng_payload_view *src_view,
a58c490f
JG
119 struct lttng_trigger **trigger)
120{
121 ssize_t ret, offset = 0, condition_size, action_size;
122 struct lttng_condition *condition = NULL;
123 struct lttng_action *action = NULL;
124 const struct lttng_trigger_comm *trigger_comm;
a58c490f
JG
125
126 if (!src_view || !trigger) {
127 ret = -1;
128 goto end;
129 }
130
131 /* lttng_trigger_comm header */
c0a66c84 132 trigger_comm = (typeof(trigger_comm)) src_view->buffer.data;
a58c490f 133 offset += sizeof(*trigger_comm);
c0a66c84
JG
134 {
135 /* struct lttng_condition */
136 struct lttng_payload_view condition_view =
137 lttng_payload_view_from_view(
138 src_view, offset, -1);
139
140 condition_size = lttng_condition_create_from_payload(&condition_view,
141 &condition);
142 }
a58c490f 143
a58c490f
JG
144 if (condition_size < 0) {
145 ret = condition_size;
146 goto end;
147 }
c0a66c84 148
a58c490f 149 offset += condition_size;
c0a66c84
JG
150 {
151 /* struct lttng_action */
152 struct lttng_payload_view action_view =
153 lttng_payload_view_from_view(
154 src_view, offset, -1);
155
156 action_size = lttng_action_create_from_payload(&action_view, &action);
157 }
a58c490f 158
a58c490f
JG
159 if (action_size < 0) {
160 ret = action_size;
161 goto end;
162 }
163 offset += action_size;
164
165 /* Unexpected size of inner-elements; the buffer is corrupted. */
166 if ((ssize_t) trigger_comm->length != condition_size + action_size) {
167 ret = -1;
168 goto error;
169 }
170
171 *trigger = lttng_trigger_create(condition, action);
172 if (!*trigger) {
173 ret = -1;
174 goto error;
175 }
c0a66c84 176
7ca172c1
JR
177 /*
178 * The trigger object owns references to the action and condition
179 * objects.
180 */
181 lttng_condition_put(condition);
182 condition = NULL;
183
184 lttng_action_put(action);
185 action = NULL;
186
a58c490f 187 ret = offset;
7ca172c1 188
a58c490f
JG
189error:
190 lttng_condition_destroy(condition);
191 lttng_action_destroy(action);
7ca172c1 192end:
a58c490f
JG
193 return ret;
194}
195
196/*
a58c490f
JG
197 * Both elements are stored contiguously, see their "*_comm" structure
198 * for the detailed format.
199 */
200LTTNG_HIDDEN
3647288f 201int lttng_trigger_serialize(struct lttng_trigger *trigger,
c0a66c84 202 struct lttng_payload *payload)
a58c490f 203{
3647288f
JG
204 int ret;
205 size_t header_offset, size_before_payload;
c0a66c84 206 struct lttng_trigger_comm trigger_comm = {};
3647288f 207 struct lttng_trigger_comm *header;
a58c490f 208
c0a66c84
JG
209 header_offset = payload->buffer.size;
210 ret = lttng_dynamic_buffer_append(&payload->buffer, &trigger_comm,
3647288f
JG
211 sizeof(trigger_comm));
212 if (ret) {
a58c490f
JG
213 goto end;
214 }
215
c0a66c84
JG
216 size_before_payload = payload->buffer.size;
217 ret = lttng_condition_serialize(trigger->condition, payload);
3647288f 218 if (ret) {
a58c490f
JG
219 goto end;
220 }
a58c490f 221
c0a66c84 222 ret = lttng_action_serialize(trigger->action, payload);
3647288f 223 if (ret) {
a58c490f
JG
224 goto end;
225 }
a58c490f 226
3647288f 227 /* Update payload size. */
c0a66c84
JG
228 header = (typeof(header)) (payload->buffer.data + header_offset);
229 header->length = payload->buffer.size - size_before_payload;
a58c490f
JG
230end:
231 return ret;
232}
3da864a9
JR
233
234LTTNG_HIDDEN
235const struct lttng_credentials *lttng_trigger_get_credentials(
236 const struct lttng_trigger *trigger)
237{
238 return LTTNG_OPTIONAL_GET_PTR(trigger->creds);
239}
240
241LTTNG_HIDDEN
242void lttng_trigger_set_credentials(
243 struct lttng_trigger *trigger,
244 const struct lttng_credentials *creds)
245{
246 assert(creds);
247 LTTNG_OPTIONAL_SET(&trigger->creds, *creds);
248}
This page took 0.070735 seconds and 4 git commands to generate.