actions: implement is_equal
[lttng-tools.git] / src / common / actions / action.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 <assert.h>
9 #include <common/error.h>
10 #include <lttng/action/action-internal.h>
11 #include <lttng/action/notify-internal.h>
12
13 static const char *lttng_action_type_string(enum lttng_action_type action_type)
14 {
15 switch (action_type) {
16 case LTTNG_ACTION_TYPE_UNKNOWN:
17 return "UNKNOWN";
18 case LTTNG_ACTION_TYPE_NOTIFY:
19 return "NOTIFY";
20 default:
21 return "???";
22 }
23 }
24
25 enum lttng_action_type lttng_action_get_type(struct lttng_action *action)
26 {
27 return action ? action->type : LTTNG_ACTION_TYPE_UNKNOWN;
28 }
29
30 LTTNG_HIDDEN
31 enum lttng_action_type lttng_action_get_type_const(
32 const struct lttng_action *action)
33 {
34 return action->type;
35 }
36
37 LTTNG_HIDDEN
38 void lttng_action_init(
39 struct lttng_action *action,
40 enum lttng_action_type type,
41 action_validate_cb validate,
42 action_serialize_cb serialize,
43 action_equal_cb equal,
44 action_destroy_cb destroy)
45 {
46 action->type = type;
47 action->validate = validate;
48 action->serialize = serialize;
49 action->equal = equal;
50 action->destroy = destroy;
51 }
52
53 void lttng_action_destroy(struct lttng_action *action)
54 {
55 if (!action) {
56 return;
57 }
58
59 assert(action->destroy);
60 action->destroy(action);
61 }
62
63 LTTNG_HIDDEN
64 bool lttng_action_validate(struct lttng_action *action)
65 {
66 bool valid;
67
68 if (!action) {
69 valid = false;
70 goto end;
71 }
72
73 if (!action->validate) {
74 /* Sub-class guarantees that it can never be invalid. */
75 valid = true;
76 goto end;
77 }
78
79 valid = action->validate(action);
80 end:
81 return valid;
82 }
83
84 LTTNG_HIDDEN
85 int lttng_action_serialize(struct lttng_action *action,
86 struct lttng_dynamic_buffer *buf)
87 {
88 int ret;
89 struct lttng_action_comm action_comm = {
90 .action_type = (int8_t) action->type,
91 };
92
93 ret = lttng_dynamic_buffer_append(buf, &action_comm,
94 sizeof(action_comm));
95 if (ret) {
96 goto end;
97 }
98
99 ret = action->serialize(action, buf);
100 if (ret) {
101 goto end;
102 }
103 end:
104 return ret;
105 }
106
107 LTTNG_HIDDEN
108 ssize_t lttng_action_create_from_buffer(const struct lttng_buffer_view *view,
109 struct lttng_action **action)
110 {
111 ssize_t consumed_len, specific_action_consumed_len;
112 const struct lttng_action_comm *action_comm;
113 action_create_from_buffer_cb create_from_buffer_cb;
114 struct lttng_buffer_view specific_action_view;
115
116 if (!view || !action) {
117 consumed_len = -1;
118 goto end;
119 }
120
121 action_comm = (const struct lttng_action_comm *) view->data;
122
123 DBG("Create action from buffer: action-type=%s",
124 lttng_action_type_string(action_comm->action_type));
125
126 switch (action_comm->action_type) {
127 case LTTNG_ACTION_TYPE_NOTIFY:
128 create_from_buffer_cb = lttng_action_notify_create_from_buffer;
129 break;
130 default:
131 ERR("Failed to create action from buffer, unhandled action type: action-type=%u (%s)",
132 action_comm->action_type,
133 lttng_action_type_string(
134 action_comm->action_type));
135 consumed_len = -1;
136 goto end;
137 }
138
139 /* Create buffer view for the action-type-specific data. */
140 specific_action_view = lttng_buffer_view_from_view(view,
141 sizeof(struct lttng_action_comm),
142 view->size - sizeof(struct lttng_action_comm));
143
144 specific_action_consumed_len =
145 create_from_buffer_cb(&specific_action_view, action);
146 if (specific_action_consumed_len < 0) {
147 ERR("Failed to create specific action from buffer.");
148 consumed_len = -1;
149 goto end;
150 }
151
152 assert(*action);
153
154 consumed_len = sizeof(struct lttng_action_comm) +
155 specific_action_consumed_len;
156
157 end:
158 return consumed_len;
159 }
160
161 LTTNG_HIDDEN
162 bool lttng_action_is_equal(const struct lttng_action *a,
163 const struct lttng_action *b)
164 {
165 bool is_equal = false;
166
167 if (!a || !b) {
168 goto end;
169 }
170
171 if (a->type != b->type) {
172 goto end;
173 }
174
175 if (a == b) {
176 is_equal = true;
177 goto end;
178 }
179
180 assert(a->equal);
181 is_equal = a->equal(a, b);
182 end:
183 return is_equal;
184 }
This page took 0.034555 seconds and 6 git commands to generate.