actions: implement is_equal
[lttng-tools.git] / src / common / actions / action.c
CommitLineData
a58c490f 1/*
ab5be9fa 2 * Copyright (C) 2017 Jérémie Galarneau <jeremie.galarneau@efficios.com>
a58c490f 3 *
ab5be9fa 4 * SPDX-License-Identifier: LGPL-2.1-only
a58c490f 5 *
a58c490f
JG
6 */
7
14ec7e87
JR
8#include <assert.h>
9#include <common/error.h>
a58c490f
JG
10#include <lttng/action/action-internal.h>
11#include <lttng/action/notify-internal.h>
a58c490f 12
2666d352
SM
13static 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
a58c490f
JG
25enum lttng_action_type lttng_action_get_type(struct lttng_action *action)
26{
27 return action ? action->type : LTTNG_ACTION_TYPE_UNKNOWN;
28}
29
9b63a4aa
JG
30LTTNG_HIDDEN
31enum lttng_action_type lttng_action_get_type_const(
32 const struct lttng_action *action)
33{
34 return action->type;
35}
36
6acb3f46
SM
37LTTNG_HIDDEN
38void lttng_action_init(
39 struct lttng_action *action,
40 enum lttng_action_type type,
41 action_validate_cb validate,
42 action_serialize_cb serialize,
3dd04a6a 43 action_equal_cb equal,
6acb3f46
SM
44 action_destroy_cb destroy)
45{
46 action->type = type;
47 action->validate = validate;
48 action->serialize = serialize;
3dd04a6a 49 action->equal = equal;
6acb3f46
SM
50 action->destroy = destroy;
51}
52
a58c490f
JG
53void 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
63LTTNG_HIDDEN
64bool 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);
80end:
81 return valid;
82}
83
84LTTNG_HIDDEN
3647288f
JG
85int lttng_action_serialize(struct lttng_action *action,
86 struct lttng_dynamic_buffer *buf)
a58c490f 87{
3647288f
JG
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) {
a58c490f
JG
96 goto end;
97 }
98
3647288f
JG
99 ret = action->serialize(action, buf);
100 if (ret) {
a58c490f
JG
101 goto end;
102 }
a58c490f
JG
103end:
104 return ret;
105}
106
107LTTNG_HIDDEN
108ssize_t lttng_action_create_from_buffer(const struct lttng_buffer_view *view,
869a3c2d 109 struct lttng_action **action)
a58c490f 110{
869a3c2d 111 ssize_t consumed_len, specific_action_consumed_len;
a58c490f 112 const struct lttng_action_comm *action_comm;
869a3c2d
SM
113 action_create_from_buffer_cb create_from_buffer_cb;
114 struct lttng_buffer_view specific_action_view;
a58c490f 115
869a3c2d
SM
116 if (!view || !action) {
117 consumed_len = -1;
a58c490f
JG
118 goto end;
119 }
120
121 action_comm = (const struct lttng_action_comm *) view->data;
869a3c2d 122
2666d352
SM
123 DBG("Create action from buffer: action-type=%s",
124 lttng_action_type_string(action_comm->action_type));
125
a58c490f
JG
126 switch (action_comm->action_type) {
127 case LTTNG_ACTION_TYPE_NOTIFY:
869a3c2d 128 create_from_buffer_cb = lttng_action_notify_create_from_buffer;
a58c490f
JG
129 break;
130 default:
2666d352
SM
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));
869a3c2d 135 consumed_len = -1;
a58c490f
JG
136 goto end;
137 }
138
869a3c2d
SM
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;
a58c490f
JG
149 goto end;
150 }
869a3c2d
SM
151
152 assert(*action);
153
154 consumed_len = sizeof(struct lttng_action_comm) +
155 specific_action_consumed_len;
156
a58c490f 157end:
869a3c2d 158 return consumed_len;
a58c490f 159}
3dd04a6a
JR
160
161LTTNG_HIDDEN
162bool 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);
182end:
183 return is_equal;
184}
This page took 0.04734 seconds and 5 git commands to generate.