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