Move notification API code to libcommon
[lttng-tools.git] / src / common / trigger.c
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 <assert.h>
22
23 LTTNG_HIDDEN
24 bool lttng_trigger_validate(struct lttng_trigger *trigger)
25 {
26 bool valid;
27
28 if (!trigger) {
29 valid = false;
30 goto end;
31 }
32
33 valid = lttng_condition_validate(trigger->condition) &&
34 lttng_action_validate(trigger->action);
35 end:
36 return valid;
37 }
38
39 struct lttng_trigger *lttng_trigger_create(
40 struct lttng_condition *condition,
41 struct lttng_action *action)
42 {
43 struct lttng_trigger *trigger = NULL;
44
45 if (!condition || !action) {
46 goto end;
47 }
48
49 trigger = zmalloc(sizeof(struct lttng_trigger));
50 if (!trigger) {
51 goto end;
52 }
53
54 trigger->condition = condition;
55 trigger->action = action;
56 end:
57 return trigger;
58 }
59
60 void lttng_trigger_destroy(struct lttng_trigger *trigger)
61 {
62 if (!trigger) {
63 return;
64 }
65
66 lttng_condition_destroy(trigger->condition);
67 lttng_action_destroy(trigger->action);
68 free(trigger);
69 }
70
71 LTTNG_HIDDEN
72 ssize_t lttng_trigger_create_from_buffer(const char *buf,
73 struct lttng_trigger **trigger)
74 {
75 ssize_t ret, trigger_size;
76 struct lttng_condition *condition = NULL;
77 struct lttng_action *action = NULL;
78
79 if (!buf || !trigger) {
80 ret = -1;
81 goto end;
82 }
83
84 ret = lttng_condition_create_from_buffer(buf, &condition);
85 if (ret < 0) {
86 goto end;
87 }
88
89 trigger_size = ret;
90 buf += ret;
91 ret = lttng_action_create_from_buffer(buf, &action);
92 if (ret < 0) {
93 goto end;
94 }
95
96 trigger_size += ret;
97 *trigger = lttng_trigger_create(condition, action);
98 if (!*trigger) {
99 goto error;
100 }
101 ret = trigger_size;
102 end:
103 return ret;
104 error:
105 lttng_condition_destroy(condition);
106 lttng_action_destroy(action);
107 return ret;
108 }
109
110 /*
111 * Returns the size of a trigger's condition and action.
112 * Both elements are stored contiguously, see their "*_comm" structure
113 * for the detailed format.
114 */
115 LTTNG_HIDDEN
116 ssize_t lttng_trigger_serialize(struct lttng_trigger *trigger, char *buf)
117 {
118 ssize_t action_size, condition_size, ret;
119
120 if (!trigger) {
121 ret = -1;
122 goto end;
123 }
124
125 condition_size = lttng_condition_serialize(trigger->condition, NULL);
126 if (condition_size < 0) {
127 ret = -1;
128 goto end;
129 }
130
131 action_size = lttng_action_serialize(trigger->action, NULL);
132 if (action_size < 0) {
133 ret = -1;
134 goto end;
135 }
136
137 ret = action_size + condition_size;
138 if (!buf) {
139 goto end;
140 }
141
142 condition_size = lttng_condition_serialize(trigger->condition, buf);
143 if (condition_size < 0) {
144 ret = -1;
145 goto end;
146 }
147
148 buf += condition_size;
149 action_size = lttng_action_serialize(trigger->action, buf);
150 if (action_size < 0) {
151 ret = -1;
152 goto end;
153 }
154 end:
155 return ret;
156 }
This page took 0.034687 seconds and 5 git commands to generate.