Fix: lttng-destroy: missing newline on session destruction message
[lttng-tools.git] / src / common / trigger.c
1 /*
2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8 #include <lttng/trigger/trigger-internal.h>
9 #include <lttng/condition/condition-internal.h>
10 #include <lttng/action/action-internal.h>
11 #include <common/error.h>
12 #include <assert.h>
13
14 LTTNG_HIDDEN
15 bool lttng_trigger_validate(struct lttng_trigger *trigger)
16 {
17 bool valid;
18
19 if (!trigger) {
20 valid = false;
21 goto end;
22 }
23
24 valid = lttng_condition_validate(trigger->condition) &&
25 lttng_action_validate(trigger->action);
26 end:
27 return valid;
28 }
29
30 struct lttng_trigger *lttng_trigger_create(
31 struct lttng_condition *condition,
32 struct lttng_action *action)
33 {
34 struct lttng_trigger *trigger = NULL;
35
36 if (!condition || !action) {
37 goto end;
38 }
39
40 trigger = zmalloc(sizeof(struct lttng_trigger));
41 if (!trigger) {
42 goto end;
43 }
44
45 trigger->condition = condition;
46 trigger->action = action;
47 end:
48 return trigger;
49 }
50
51 struct lttng_condition *lttng_trigger_get_condition(
52 struct lttng_trigger *trigger)
53 {
54 return trigger ? trigger->condition : NULL;
55 }
56
57 LTTNG_HIDDEN
58 const struct lttng_condition *lttng_trigger_get_const_condition(
59 const struct lttng_trigger *trigger)
60 {
61 return trigger->condition;
62 }
63
64 struct lttng_action *lttng_trigger_get_action(
65 struct lttng_trigger *trigger)
66 {
67 return trigger ? trigger->action : NULL;
68 }
69
70 LTTNG_HIDDEN
71 const struct lttng_action *lttng_trigger_get_const_action(
72 const struct lttng_trigger *trigger)
73 {
74 return trigger->action;
75 }
76
77 void lttng_trigger_destroy(struct lttng_trigger *trigger)
78 {
79 if (!trigger) {
80 return;
81 }
82
83 free(trigger);
84 }
85
86 LTTNG_HIDDEN
87 ssize_t lttng_trigger_create_from_buffer(
88 const struct lttng_buffer_view *src_view,
89 struct lttng_trigger **trigger)
90 {
91 ssize_t ret, offset = 0, condition_size, action_size;
92 struct lttng_condition *condition = NULL;
93 struct lttng_action *action = NULL;
94 const struct lttng_trigger_comm *trigger_comm;
95 struct lttng_buffer_view condition_view;
96 struct lttng_buffer_view action_view;
97
98 if (!src_view || !trigger) {
99 ret = -1;
100 goto end;
101 }
102
103 /* lttng_trigger_comm header */
104 trigger_comm = (const struct lttng_trigger_comm *) src_view->data;
105 offset += sizeof(*trigger_comm);
106
107 condition_view = lttng_buffer_view_from_view(src_view, offset, -1);
108
109 /* struct lttng_condition */
110 condition_size = lttng_condition_create_from_buffer(&condition_view,
111 &condition);
112 if (condition_size < 0) {
113 ret = condition_size;
114 goto end;
115 }
116 offset += condition_size;
117
118 /* struct lttng_action */
119 action_view = lttng_buffer_view_from_view(src_view, offset, -1);
120 action_size = lttng_action_create_from_buffer(&action_view, &action);
121 if (action_size < 0) {
122 ret = action_size;
123 goto end;
124 }
125 offset += action_size;
126
127 /* Unexpected size of inner-elements; the buffer is corrupted. */
128 if ((ssize_t) trigger_comm->length != condition_size + action_size) {
129 ret = -1;
130 goto error;
131 }
132
133 *trigger = lttng_trigger_create(condition, action);
134 if (!*trigger) {
135 ret = -1;
136 goto error;
137 }
138 ret = offset;
139 end:
140 return ret;
141 error:
142 lttng_condition_destroy(condition);
143 lttng_action_destroy(action);
144 return ret;
145 }
146
147 /*
148 * Both elements are stored contiguously, see their "*_comm" structure
149 * for the detailed format.
150 */
151 LTTNG_HIDDEN
152 int lttng_trigger_serialize(struct lttng_trigger *trigger,
153 struct lttng_dynamic_buffer *buf)
154 {
155 int ret;
156 size_t header_offset, size_before_payload;
157 struct lttng_trigger_comm trigger_comm = { 0 };
158 struct lttng_trigger_comm *header;
159
160 header_offset = buf->size;
161 ret = lttng_dynamic_buffer_append(buf, &trigger_comm,
162 sizeof(trigger_comm));
163 if (ret) {
164 goto end;
165 }
166
167 size_before_payload = buf->size;
168 ret = lttng_condition_serialize(trigger->condition, buf);
169 if (ret) {
170 goto end;
171 }
172
173 ret = lttng_action_serialize(trigger->action, buf);
174 if (ret) {
175 goto end;
176 }
177
178 /* Update payload size. */
179 header = (struct lttng_trigger_comm *) ((char *) buf->data + header_offset);
180 header->length = buf->size - size_before_payload;
181 end:
182 return ret;
183 }
This page took 0.073682 seconds and 4 git commands to generate.