Sessiond: Implement cmd_clear_session
[lttng-tools.git] / src / common / trigger.c
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 end:
58 return trigger;
59 }
60
61 struct lttng_condition *lttng_trigger_get_condition(
62 struct lttng_trigger *trigger)
63 {
64 return trigger ? trigger->condition : NULL;
65 }
66
67 LTTNG_HIDDEN
68 const struct lttng_condition *lttng_trigger_get_const_condition(
69 const struct lttng_trigger *trigger)
70 {
71 return trigger->condition;
72 }
73
74 struct lttng_action *lttng_trigger_get_action(
75 struct lttng_trigger *trigger)
76 {
77 return trigger ? trigger->action : NULL;
78 }
79
80 LTTNG_HIDDEN
81 const struct lttng_action *lttng_trigger_get_const_action(
82 const struct lttng_trigger *trigger)
83 {
84 return trigger->action;
85 }
86
87 void lttng_trigger_destroy(struct lttng_trigger *trigger)
88 {
89 if (!trigger) {
90 return;
91 }
92
93 free(trigger);
94 }
95
96 LTTNG_HIDDEN
97 ssize_t lttng_trigger_create_from_buffer(
98 const struct lttng_buffer_view *src_view,
99 struct lttng_trigger **trigger)
100 {
101 ssize_t ret, offset = 0, condition_size, action_size;
102 struct lttng_condition *condition = NULL;
103 struct lttng_action *action = NULL;
104 const struct lttng_trigger_comm *trigger_comm;
105 struct lttng_buffer_view condition_view;
106 struct lttng_buffer_view action_view;
107
108 if (!src_view || !trigger) {
109 ret = -1;
110 goto end;
111 }
112
113 /* lttng_trigger_comm header */
114 trigger_comm = (const struct lttng_trigger_comm *) src_view->data;
115 offset += sizeof(*trigger_comm);
116
117 condition_view = lttng_buffer_view_from_view(src_view, offset, -1);
118
119 /* struct lttng_condition */
120 condition_size = lttng_condition_create_from_buffer(&condition_view,
121 &condition);
122 if (condition_size < 0) {
123 ret = condition_size;
124 goto end;
125 }
126 offset += condition_size;
127
128 /* struct lttng_action */
129 action_view = lttng_buffer_view_from_view(src_view, offset, -1);
130 action_size = lttng_action_create_from_buffer(&action_view, &action);
131 if (action_size < 0) {
132 ret = action_size;
133 goto end;
134 }
135 offset += action_size;
136
137 /* Unexpected size of inner-elements; the buffer is corrupted. */
138 if ((ssize_t) trigger_comm->length != condition_size + action_size) {
139 ret = -1;
140 goto error;
141 }
142
143 *trigger = lttng_trigger_create(condition, action);
144 if (!*trigger) {
145 ret = -1;
146 goto error;
147 }
148 ret = offset;
149 end:
150 return ret;
151 error:
152 lttng_condition_destroy(condition);
153 lttng_action_destroy(action);
154 return ret;
155 }
156
157 /*
158 * Both elements are stored contiguously, see their "*_comm" structure
159 * for the detailed format.
160 */
161 LTTNG_HIDDEN
162 int lttng_trigger_serialize(struct lttng_trigger *trigger,
163 struct lttng_dynamic_buffer *buf)
164 {
165 int ret;
166 size_t header_offset, size_before_payload;
167 struct lttng_trigger_comm trigger_comm = { 0 };
168 struct lttng_trigger_comm *header;
169
170 header_offset = buf->size;
171 ret = lttng_dynamic_buffer_append(buf, &trigger_comm,
172 sizeof(trigger_comm));
173 if (ret) {
174 goto end;
175 }
176
177 size_before_payload = buf->size;
178 ret = lttng_condition_serialize(trigger->condition, buf);
179 if (ret) {
180 goto end;
181 }
182
183 ret = lttng_action_serialize(trigger->action, buf);
184 if (ret) {
185 goto end;
186 }
187
188 /* Update payload size. */
189 header = (struct lttng_trigger_comm *) ((char *) buf->data + header_offset);
190 header->length = buf->size - size_before_payload;
191 end:
192 return ret;
193 }
This page took 0.0335 seconds and 5 git commands to generate.