Commit | Line | Data |
---|---|---|
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> | |
11 | #include <common/error.h> | |
12 | #include <assert.h> | |
13 | ||
14 | LTTNG_HIDDEN | |
15 | bool lttng_trigger_validate(struct lttng_trigger *trigger) | |
16 | { | |
17 | bool valid; | |
18 | ||
19 | if (!trigger) { | |
20 | valid = false; | |
21 | goto end; | |
22 | } | |
23 | ||
24 | valid = lttng_condition_validate(trigger->condition) && | |
25 | lttng_action_validate(trigger->action); | |
26 | end: | |
27 | return valid; | |
28 | } | |
29 | ||
30 | struct lttng_trigger *lttng_trigger_create( | |
31 | struct lttng_condition *condition, | |
32 | struct lttng_action *action) | |
33 | { | |
34 | struct lttng_trigger *trigger = NULL; | |
35 | ||
36 | if (!condition || !action) { | |
37 | goto end; | |
38 | } | |
39 | ||
40 | trigger = zmalloc(sizeof(struct lttng_trigger)); | |
41 | if (!trigger) { | |
42 | goto end; | |
43 | } | |
44 | ||
45 | trigger->condition = condition; | |
46 | trigger->action = action; | |
47 | end: | |
48 | return trigger; | |
49 | } | |
50 | ||
51 | struct lttng_condition *lttng_trigger_get_condition( | |
52 | struct lttng_trigger *trigger) | |
53 | { | |
54 | return trigger ? trigger->condition : NULL; | |
55 | } | |
56 | ||
9b63a4aa JG |
57 | LTTNG_HIDDEN |
58 | const struct lttng_condition *lttng_trigger_get_const_condition( | |
59 | const struct lttng_trigger *trigger) | |
60 | { | |
61 | return trigger->condition; | |
62 | } | |
63 | ||
e2ba1c78 | 64 | struct lttng_action *lttng_trigger_get_action( |
a58c490f JG |
65 | struct lttng_trigger *trigger) |
66 | { | |
67 | return trigger ? trigger->action : NULL; | |
68 | } | |
69 | ||
9b63a4aa JG |
70 | LTTNG_HIDDEN |
71 | const struct lttng_action *lttng_trigger_get_const_action( | |
72 | const struct lttng_trigger *trigger) | |
73 | { | |
74 | return trigger->action; | |
75 | } | |
76 | ||
a58c490f JG |
77 | void lttng_trigger_destroy(struct lttng_trigger *trigger) |
78 | { | |
79 | if (!trigger) { | |
80 | return; | |
81 | } | |
82 | ||
83 | free(trigger); | |
84 | } | |
85 | ||
86 | LTTNG_HIDDEN | |
87 | ssize_t lttng_trigger_create_from_buffer( | |
88 | const struct lttng_buffer_view *src_view, | |
89 | struct lttng_trigger **trigger) | |
90 | { | |
91 | ssize_t ret, offset = 0, condition_size, action_size; | |
92 | struct lttng_condition *condition = NULL; | |
93 | struct lttng_action *action = NULL; | |
94 | const struct lttng_trigger_comm *trigger_comm; | |
95 | struct lttng_buffer_view condition_view; | |
96 | struct lttng_buffer_view action_view; | |
97 | ||
98 | if (!src_view || !trigger) { | |
99 | ret = -1; | |
100 | goto end; | |
101 | } | |
102 | ||
103 | /* lttng_trigger_comm header */ | |
104 | trigger_comm = (const struct lttng_trigger_comm *) src_view->data; | |
105 | offset += sizeof(*trigger_comm); | |
106 | ||
107 | condition_view = lttng_buffer_view_from_view(src_view, offset, -1); | |
108 | ||
109 | /* struct lttng_condition */ | |
110 | condition_size = lttng_condition_create_from_buffer(&condition_view, | |
111 | &condition); | |
112 | if (condition_size < 0) { | |
113 | ret = condition_size; | |
114 | goto end; | |
115 | } | |
116 | offset += condition_size; | |
117 | ||
118 | /* struct lttng_action */ | |
119 | action_view = lttng_buffer_view_from_view(src_view, offset, -1); | |
120 | action_size = lttng_action_create_from_buffer(&action_view, &action); | |
121 | if (action_size < 0) { | |
122 | ret = action_size; | |
123 | goto end; | |
124 | } | |
125 | offset += action_size; | |
126 | ||
127 | /* Unexpected size of inner-elements; the buffer is corrupted. */ | |
128 | if ((ssize_t) trigger_comm->length != condition_size + action_size) { | |
129 | ret = -1; | |
130 | goto error; | |
131 | } | |
132 | ||
133 | *trigger = lttng_trigger_create(condition, action); | |
134 | if (!*trigger) { | |
135 | ret = -1; | |
136 | goto error; | |
137 | } | |
138 | ret = offset; | |
139 | end: | |
140 | return ret; | |
141 | error: | |
142 | lttng_condition_destroy(condition); | |
143 | lttng_action_destroy(action); | |
144 | return ret; | |
145 | } | |
146 | ||
147 | /* | |
a58c490f JG |
148 | * Both elements are stored contiguously, see their "*_comm" structure |
149 | * for the detailed format. | |
150 | */ | |
151 | LTTNG_HIDDEN | |
3647288f JG |
152 | int lttng_trigger_serialize(struct lttng_trigger *trigger, |
153 | struct lttng_dynamic_buffer *buf) | |
a58c490f | 154 | { |
3647288f JG |
155 | int ret; |
156 | size_t header_offset, size_before_payload; | |
1065191b | 157 | struct lttng_trigger_comm trigger_comm = { 0 }; |
3647288f | 158 | struct lttng_trigger_comm *header; |
a58c490f | 159 | |
3647288f JG |
160 | header_offset = buf->size; |
161 | ret = lttng_dynamic_buffer_append(buf, &trigger_comm, | |
162 | sizeof(trigger_comm)); | |
163 | if (ret) { | |
a58c490f JG |
164 | goto end; |
165 | } | |
166 | ||
3647288f JG |
167 | size_before_payload = buf->size; |
168 | ret = lttng_condition_serialize(trigger->condition, buf); | |
169 | if (ret) { | |
a58c490f JG |
170 | goto end; |
171 | } | |
a58c490f | 172 | |
3647288f JG |
173 | ret = lttng_action_serialize(trigger->action, buf); |
174 | if (ret) { | |
a58c490f JG |
175 | goto end; |
176 | } | |
a58c490f | 177 | |
3647288f JG |
178 | /* Update payload size. */ |
179 | header = (struct lttng_trigger_comm *) ((char *) buf->data + header_offset); | |
180 | header->length = buf->size - size_before_payload; | |
a58c490f JG |
181 | end: |
182 | return ret; | |
183 | } |