Fix: add session_info object to sessions_ht
[lttng-tools.git] / src / common / trigger.c
CommitLineData
a58c490f
JG
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
24LTTNG_HIDDEN
25bool 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);
36end:
37 return valid;
38}
39
40struct 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;
57end:
58 return trigger;
59}
60
61struct lttng_condition *lttng_trigger_get_condition(
62 struct lttng_trigger *trigger)
63{
64 return trigger ? trigger->condition : NULL;
65}
66
e2ba1c78 67struct lttng_action *lttng_trigger_get_action(
a58c490f
JG
68 struct lttng_trigger *trigger)
69{
70 return trigger ? trigger->action : NULL;
71}
72
73void lttng_trigger_destroy(struct lttng_trigger *trigger)
74{
75 if (!trigger) {
76 return;
77 }
78
79 free(trigger);
80}
81
82LTTNG_HIDDEN
83ssize_t lttng_trigger_create_from_buffer(
84 const struct lttng_buffer_view *src_view,
85 struct lttng_trigger **trigger)
86{
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;
93
94 if (!src_view || !trigger) {
95 ret = -1;
96 goto end;
97 }
98
99 /* lttng_trigger_comm header */
100 trigger_comm = (const struct lttng_trigger_comm *) src_view->data;
101 offset += sizeof(*trigger_comm);
102
103 condition_view = lttng_buffer_view_from_view(src_view, offset, -1);
104
105 /* struct lttng_condition */
106 condition_size = lttng_condition_create_from_buffer(&condition_view,
107 &condition);
108 if (condition_size < 0) {
109 ret = condition_size;
110 goto end;
111 }
112 offset += condition_size;
113
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) {
118 ret = action_size;
119 goto end;
120 }
121 offset += action_size;
122
123 /* Unexpected size of inner-elements; the buffer is corrupted. */
124 if ((ssize_t) trigger_comm->length != condition_size + action_size) {
125 ret = -1;
126 goto error;
127 }
128
129 *trigger = lttng_trigger_create(condition, action);
130 if (!*trigger) {
131 ret = -1;
132 goto error;
133 }
134 ret = offset;
135end:
136 return ret;
137error:
138 lttng_condition_destroy(condition);
139 lttng_action_destroy(action);
140 return ret;
141}
142
143/*
a58c490f
JG
144 * Both elements are stored contiguously, see their "*_comm" structure
145 * for the detailed format.
146 */
147LTTNG_HIDDEN
3647288f
JG
148int lttng_trigger_serialize(struct lttng_trigger *trigger,
149 struct lttng_dynamic_buffer *buf)
a58c490f 150{
3647288f
JG
151 int ret;
152 size_t header_offset, size_before_payload;
1065191b 153 struct lttng_trigger_comm trigger_comm = { 0 };
3647288f 154 struct lttng_trigger_comm *header;
a58c490f 155
3647288f
JG
156 header_offset = buf->size;
157 ret = lttng_dynamic_buffer_append(buf, &trigger_comm,
158 sizeof(trigger_comm));
159 if (ret) {
a58c490f
JG
160 goto end;
161 }
162
3647288f
JG
163 size_before_payload = buf->size;
164 ret = lttng_condition_serialize(trigger->condition, buf);
165 if (ret) {
a58c490f
JG
166 goto end;
167 }
a58c490f 168
3647288f
JG
169 ret = lttng_action_serialize(trigger->action, buf);
170 if (ret) {
a58c490f
JG
171 goto end;
172 }
a58c490f 173
3647288f
JG
174 /* Update payload size. */
175 header = (struct lttng_trigger_comm *) ((char *) buf->data + header_offset);
176 header->length = buf->size - size_before_payload;
a58c490f
JG
177end:
178 return ret;
179}
This page took 0.031197 seconds and 4 git commands to generate.