Add client notification API
[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
67extern struct lttng_action *lttng_trigger_get_action(
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/*
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.
147 */
148LTTNG_HIDDEN
149ssize_t lttng_trigger_serialize(struct lttng_trigger *trigger, char *buf)
150{
151 struct lttng_trigger_comm trigger_comm;
152 ssize_t action_size, condition_size, offset = 0, ret;
153
154 if (!trigger) {
155 ret = -1;
156 goto end;
157 }
158
159 offset += sizeof(trigger_comm);
160 condition_size = lttng_condition_serialize(trigger->condition,
161 buf ? (buf + offset) : NULL);
162 if (condition_size < 0) {
163 ret = -1;
164 goto end;
165 }
166 offset += condition_size;
167
168 action_size = lttng_action_serialize(trigger->action,
169 buf ? (buf + offset) : NULL);
170 if (action_size < 0) {
171 ret = -1;
172 goto end;
173 }
174 offset += action_size;
175
176 if (buf) {
177 trigger_comm.length = (uint32_t) (condition_size + action_size);
178 memcpy(buf, &trigger_comm, sizeof(trigger_comm));
179 }
180 ret = offset;
181end:
182 return ret;
183}
This page took 0.041285 seconds and 4 git commands to generate.