2 * Copyright (C) 2017 - Jérémie Galarneau <jeremie.galarneau@efficios.com>
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.
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
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
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>
25 bool lttng_trigger_validate(struct lttng_trigger
*trigger
)
34 valid
= lttng_condition_validate(trigger
->condition
) &&
35 lttng_action_validate(trigger
->action
);
40 struct lttng_trigger
*lttng_trigger_create(
41 struct lttng_condition
*condition
,
42 struct lttng_action
*action
)
44 struct lttng_trigger
*trigger
= NULL
;
46 if (!condition
|| !action
) {
50 trigger
= zmalloc(sizeof(struct lttng_trigger
));
55 trigger
->condition
= condition
;
56 trigger
->action
= action
;
61 struct lttng_condition
*lttng_trigger_get_condition(
62 struct lttng_trigger
*trigger
)
64 return trigger
? trigger
->condition
: NULL
;
67 extern struct lttng_action
*lttng_trigger_get_action(
68 struct lttng_trigger
*trigger
)
70 return trigger
? trigger
->action
: NULL
;
73 void lttng_trigger_destroy(struct lttng_trigger
*trigger
)
83 ssize_t
lttng_trigger_create_from_buffer(
84 const struct lttng_buffer_view
*src_view
,
85 struct lttng_trigger
**trigger
)
87 ssize_t ret
, offset
= 0, condition_size
, action_size
;
88 struct lttng_condition
*condition
= NULL
;
89 struct lttng_action
*action
= NULL
;
90 const struct lttng_trigger_comm
*trigger_comm
;
91 struct lttng_buffer_view condition_view
;
92 struct lttng_buffer_view action_view
;
94 if (!src_view
|| !trigger
) {
99 /* lttng_trigger_comm header */
100 trigger_comm
= (const struct lttng_trigger_comm
*) src_view
->data
;
101 offset
+= sizeof(*trigger_comm
);
103 condition_view
= lttng_buffer_view_from_view(src_view
, offset
, -1);
105 /* struct lttng_condition */
106 condition_size
= lttng_condition_create_from_buffer(&condition_view
,
108 if (condition_size
< 0) {
109 ret
= condition_size
;
112 offset
+= condition_size
;
114 /* struct lttng_action */
115 action_view
= lttng_buffer_view_from_view(src_view
, offset
, -1);
116 action_size
= lttng_action_create_from_buffer(&action_view
, &action
);
117 if (action_size
< 0) {
121 offset
+= action_size
;
123 /* Unexpected size of inner-elements; the buffer is corrupted. */
124 if ((ssize_t
) trigger_comm
->length
!= condition_size
+ action_size
) {
129 *trigger
= lttng_trigger_create(condition
, action
);
138 lttng_condition_destroy(condition
);
139 lttng_action_destroy(action
);
144 * Returns the size of a trigger (header + condition + action).
145 * Both elements are stored contiguously, see their "*_comm" structure
146 * for the detailed format.
149 ssize_t
lttng_trigger_serialize(struct lttng_trigger
*trigger
, char *buf
)
151 struct lttng_trigger_comm trigger_comm
= { 0 };
152 ssize_t action_size
, condition_size
, offset
= 0, ret
;
159 offset
+= sizeof(trigger_comm
);
160 condition_size
= lttng_condition_serialize(trigger
->condition
,
161 buf
? (buf
+ offset
) : NULL
);
162 if (condition_size
< 0) {
166 offset
+= condition_size
;
168 action_size
= lttng_action_serialize(trigger
->action
,
169 buf
? (buf
+ offset
) : NULL
);
170 if (action_size
< 0) {
174 offset
+= action_size
;
177 trigger_comm
.length
= (uint32_t) (condition_size
+ action_size
);
178 memcpy(buf
, &trigger_comm
, sizeof(trigger_comm
));