Various fixes for prototype branch
[lttng-tools.git] / src / common / trigger.c
CommitLineData
668ba131
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>
2a38b722
JG
19#include <lttng/condition/condition-internal.h>
20#include <lttng/action/action-internal.h>
45fdf591 21#include <common/error.h>
668ba131
JG
22#include <assert.h>
23
96f8e9b0
JG
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
668ba131
JG
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;
c254fb11 57 CDS_INIT_LIST_HEAD(&trigger->list_node);
668ba131
JG
58end:
59 return trigger;
60}
61
3c47cdaf
JG
62struct lttng_condition *lttng_trigger_get_condition(
63 struct lttng_trigger *trigger)
64{
65 return trigger ? trigger->condition : NULL;
66}
67
68extern struct lttng_action *lttng_trigger_get_action(
69 struct lttng_trigger *trigger)
70{
71 return trigger ? trigger->action : NULL;
72}
73
668ba131
JG
74void lttng_trigger_destroy(struct lttng_trigger *trigger)
75{
76 if (!trigger) {
77 return;
78 }
79
80 lttng_condition_destroy(trigger->condition);
81 lttng_action_destroy(trigger->action);
82 free(trigger);
83}
2a38b722 84
96f8e9b0
JG
85LTTNG_HIDDEN
86ssize_t lttng_trigger_create_from_buffer(const char *buf,
87 struct lttng_trigger **trigger)
88{
3c47cdaf 89 ssize_t ret, offset = 0, condition_size, action_size;
96f8e9b0
JG
90 struct lttng_condition *condition = NULL;
91 struct lttng_action *action = NULL;
3c47cdaf 92 struct lttng_trigger_comm *trigger_comm;
96f8e9b0
JG
93
94 if (!buf || !trigger) {
95 ret = -1;
96 goto end;
97 }
98
3c47cdaf
JG
99 /* lttng_trigger_comm header */
100 trigger_comm = (struct lttng_trigger_comm *) buf;
101 offset += sizeof(*trigger_comm);
102
103 /* struct lttng_condition */
104 condition_size = lttng_condition_create_from_buffer(buf + offset,
105 &condition);
106 if (condition_size < 0) {
107 ret = condition_size;
96f8e9b0
JG
108 goto end;
109 }
3c47cdaf 110 offset += condition_size;
96f8e9b0 111
3c47cdaf
JG
112 /* struct lttng_action */
113 action_size = lttng_action_create_from_buffer(buf + offset, &action);
114 if (action_size < 0) {
115 ret = action_size;
96f8e9b0
JG
116 goto end;
117 }
3c47cdaf
JG
118 offset += action_size;
119
120 /* Unexpected size of inner-elements; the buffer is corrupted. */
121 if ((ssize_t) trigger_comm->length != condition_size + action_size) {
122 ret = -1;
123 goto error;
124 }
96f8e9b0 125
96f8e9b0
JG
126 *trigger = lttng_trigger_create(condition, action);
127 if (!*trigger) {
128 goto error;
129 }
3c47cdaf 130 ret = offset;
96f8e9b0
JG
131end:
132 return ret;
133error:
134 lttng_condition_destroy(condition);
135 lttng_action_destroy(action);
136 return ret;
137}
138
139/*
3c47cdaf 140 * Returns the size of a trigger (header + condition + action).
96f8e9b0
JG
141 * Both elements are stored contiguously, see their "*_comm" structure
142 * for the detailed format.
143 */
144LTTNG_HIDDEN
2a38b722
JG
145ssize_t lttng_trigger_serialize(struct lttng_trigger *trigger, char *buf)
146{
3c47cdaf
JG
147 struct lttng_trigger_comm trigger_comm;
148 ssize_t action_size, condition_size, offset = 0, ret;
2a38b722
JG
149
150 if (!trigger) {
151 ret = -1;
152 goto end;
153 }
154
3c47cdaf
JG
155 offset += sizeof(trigger_comm);
156 condition_size = lttng_condition_serialize(trigger->condition,
157 buf ? (buf + offset) : NULL);
96f8e9b0
JG
158 if (condition_size < 0) {
159 ret = -1;
160 goto end;
161 }
3c47cdaf 162 offset += condition_size;
96f8e9b0 163
3c47cdaf
JG
164 action_size = lttng_action_serialize(trigger->action,
165 buf ? (buf + offset) : NULL);
96f8e9b0
JG
166 if (action_size < 0) {
167 ret = -1;
168 goto end;
169 }
3c47cdaf 170 offset += action_size;
96f8e9b0 171
3c47cdaf
JG
172 if (buf) {
173 trigger_comm.length = (uint32_t) (condition_size + action_size);
174 memcpy(buf, &trigger_comm, sizeof(trigger_comm));
2a38b722 175 }
3c47cdaf 176 ret = offset;
2a38b722
JG
177end:
178 return ret;
179}
This page took 0.044374 seconds and 5 git commands to generate.