Commit | Line | Data |
---|---|---|
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 |
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 | ||
668ba131 JG |
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; | |
c254fb11 | 57 | CDS_INIT_LIST_HEAD(&trigger->list_node); |
668ba131 JG |
58 | end: |
59 | return trigger; | |
60 | } | |
61 | ||
62 | void lttng_trigger_destroy(struct lttng_trigger *trigger) | |
63 | { | |
64 | if (!trigger) { | |
65 | return; | |
66 | } | |
67 | ||
68 | lttng_condition_destroy(trigger->condition); | |
69 | lttng_action_destroy(trigger->action); | |
70 | free(trigger); | |
71 | } | |
2a38b722 | 72 | |
96f8e9b0 JG |
73 | LTTNG_HIDDEN |
74 | ssize_t lttng_trigger_create_from_buffer(const char *buf, | |
75 | struct lttng_trigger **trigger) | |
76 | { | |
77 | ssize_t ret, trigger_size; | |
78 | struct lttng_condition *condition = NULL; | |
79 | struct lttng_action *action = NULL; | |
80 | ||
81 | if (!buf || !trigger) { | |
82 | ret = -1; | |
83 | goto end; | |
84 | } | |
85 | ||
45fdf591 | 86 | DBG("Deserializing trigger from buffer"); |
96f8e9b0 JG |
87 | ret = lttng_condition_create_from_buffer(buf, &condition); |
88 | if (ret < 0) { | |
89 | goto end; | |
90 | } | |
91 | ||
92 | trigger_size = ret; | |
93 | buf += ret; | |
94 | ret = lttng_action_create_from_buffer(buf, &action); | |
95 | if (ret < 0) { | |
96 | goto end; | |
97 | } | |
98 | ||
99 | trigger_size += ret; | |
100 | *trigger = lttng_trigger_create(condition, action); | |
101 | if (!*trigger) { | |
102 | goto error; | |
103 | } | |
104 | ret = trigger_size; | |
105 | end: | |
106 | return ret; | |
107 | error: | |
108 | lttng_condition_destroy(condition); | |
109 | lttng_action_destroy(action); | |
110 | return ret; | |
111 | } | |
112 | ||
113 | /* | |
114 | * Returns the size of a trigger's condition and action. | |
115 | * Both elements are stored contiguously, see their "*_comm" structure | |
116 | * for the detailed format. | |
117 | */ | |
118 | LTTNG_HIDDEN | |
2a38b722 JG |
119 | ssize_t lttng_trigger_serialize(struct lttng_trigger *trigger, char *buf) |
120 | { | |
121 | ssize_t action_size, condition_size, ret; | |
122 | ||
123 | if (!trigger) { | |
124 | ret = -1; | |
125 | goto end; | |
126 | } | |
127 | ||
96f8e9b0 JG |
128 | condition_size = lttng_condition_serialize(trigger->condition, NULL); |
129 | if (condition_size < 0) { | |
130 | ret = -1; | |
131 | goto end; | |
132 | } | |
133 | ||
134 | action_size = lttng_action_serialize(trigger->action, NULL); | |
135 | if (action_size < 0) { | |
136 | ret = -1; | |
137 | goto end; | |
138 | } | |
139 | ||
140 | ret = action_size + condition_size; | |
141 | if (!buf) { | |
142 | goto end; | |
143 | } | |
144 | ||
2a38b722 JG |
145 | condition_size = lttng_condition_serialize(trigger->condition, buf); |
146 | if (condition_size < 0) { | |
147 | ret = -1; | |
148 | goto end; | |
149 | } | |
150 | ||
96f8e9b0 | 151 | buf += condition_size; |
2a38b722 JG |
152 | action_size = lttng_action_serialize(trigger->action, buf); |
153 | if (action_size < 0) { | |
154 | ret = -1; | |
155 | goto end; | |
156 | } | |
2a38b722 JG |
157 | end: |
158 | return ret; | |
159 | } |