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
;
57 CDS_INIT_LIST_HEAD(&trigger
->list_node
);
62 struct lttng_condition
*lttng_trigger_get_condition(
63 struct lttng_trigger
*trigger
)
65 return trigger
? trigger
->condition
: NULL
;
68 extern struct lttng_action
*lttng_trigger_get_action(
69 struct lttng_trigger
*trigger
)
71 return trigger
? trigger
->action
: NULL
;
74 void lttng_trigger_destroy(struct lttng_trigger
*trigger
)
80 lttng_condition_destroy(trigger
->condition
);
81 lttng_action_destroy(trigger
->action
);
86 ssize_t
lttng_trigger_create_from_buffer(const char *buf
,
87 struct lttng_trigger
**trigger
)
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
;
94 if (!buf
|| !trigger
) {
99 /* lttng_trigger_comm header */
100 trigger_comm
= (struct lttng_trigger_comm
*) buf
;
101 offset
+= sizeof(*trigger_comm
);
103 /* struct lttng_condition */
104 condition_size
= lttng_condition_create_from_buffer(buf
+ offset
,
106 if (condition_size
< 0) {
107 ret
= condition_size
;
110 offset
+= condition_size
;
112 /* struct lttng_action */
113 action_size
= lttng_action_create_from_buffer(buf
+ offset
, &action
);
114 if (action_size
< 0) {
118 offset
+= action_size
;
120 /* Unexpected size of inner-elements; the buffer is corrupted. */
121 if ((ssize_t
) trigger_comm
->length
!= condition_size
+ action_size
) {
126 *trigger
= lttng_trigger_create(condition
, action
);
134 lttng_condition_destroy(condition
);
135 lttng_action_destroy(action
);
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.
145 ssize_t
lttng_trigger_serialize(struct lttng_trigger
*trigger
, char *buf
)
147 struct lttng_trigger_comm trigger_comm
;
148 ssize_t action_size
, condition_size
, offset
= 0, ret
;
155 offset
+= sizeof(trigger_comm
);
156 condition_size
= lttng_condition_serialize(trigger
->condition
,
157 buf
? (buf
+ offset
) : NULL
);
158 if (condition_size
< 0) {
162 offset
+= condition_size
;
164 action_size
= lttng_action_serialize(trigger
->action
,
165 buf
? (buf
+ offset
) : NULL
);
166 if (action_size
< 0) {
170 offset
+= action_size
;
173 trigger_comm
.length
= (uint32_t) (condition_size
+ action_size
);
174 memcpy(buf
, &trigger_comm
, sizeof(trigger_comm
));