Commit | Line | Data |
---|---|---|
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 | ||
8 | #include <lttng/action/action-internal.h> | |
9 | #include <lttng/action/notify-internal.h> | |
10 | #include <common/error.h> | |
11 | #include <assert.h> | |
12 | ||
13 | enum lttng_action_type lttng_action_get_type(struct lttng_action *action) | |
14 | { | |
15 | return action ? action->type : LTTNG_ACTION_TYPE_UNKNOWN; | |
16 | } | |
17 | ||
9b63a4aa JG |
18 | LTTNG_HIDDEN |
19 | enum lttng_action_type lttng_action_get_type_const( | |
20 | const struct lttng_action *action) | |
21 | { | |
22 | return action->type; | |
23 | } | |
24 | ||
a58c490f JG |
25 | void lttng_action_destroy(struct lttng_action *action) |
26 | { | |
27 | if (!action) { | |
28 | return; | |
29 | } | |
30 | ||
31 | assert(action->destroy); | |
32 | action->destroy(action); | |
33 | } | |
34 | ||
35 | LTTNG_HIDDEN | |
36 | bool lttng_action_validate(struct lttng_action *action) | |
37 | { | |
38 | bool valid; | |
39 | ||
40 | if (!action) { | |
41 | valid = false; | |
42 | goto end; | |
43 | } | |
44 | ||
45 | if (!action->validate) { | |
46 | /* Sub-class guarantees that it can never be invalid. */ | |
47 | valid = true; | |
48 | goto end; | |
49 | } | |
50 | ||
51 | valid = action->validate(action); | |
52 | end: | |
53 | return valid; | |
54 | } | |
55 | ||
56 | LTTNG_HIDDEN | |
3647288f JG |
57 | int lttng_action_serialize(struct lttng_action *action, |
58 | struct lttng_dynamic_buffer *buf) | |
a58c490f | 59 | { |
3647288f JG |
60 | int ret; |
61 | struct lttng_action_comm action_comm = { | |
62 | .action_type = (int8_t) action->type, | |
63 | }; | |
64 | ||
65 | ret = lttng_dynamic_buffer_append(buf, &action_comm, | |
66 | sizeof(action_comm)); | |
67 | if (ret) { | |
a58c490f JG |
68 | goto end; |
69 | } | |
70 | ||
3647288f JG |
71 | ret = action->serialize(action, buf); |
72 | if (ret) { | |
a58c490f JG |
73 | goto end; |
74 | } | |
a58c490f JG |
75 | end: |
76 | return ret; | |
77 | } | |
78 | ||
79 | LTTNG_HIDDEN | |
80 | ssize_t lttng_action_create_from_buffer(const struct lttng_buffer_view *view, | |
869a3c2d | 81 | struct lttng_action **action) |
a58c490f | 82 | { |
869a3c2d | 83 | ssize_t consumed_len, specific_action_consumed_len; |
a58c490f | 84 | const struct lttng_action_comm *action_comm; |
869a3c2d SM |
85 | action_create_from_buffer_cb create_from_buffer_cb; |
86 | struct lttng_buffer_view specific_action_view; | |
a58c490f | 87 | |
869a3c2d SM |
88 | if (!view || !action) { |
89 | consumed_len = -1; | |
a58c490f JG |
90 | goto end; |
91 | } | |
92 | ||
93 | action_comm = (const struct lttng_action_comm *) view->data; | |
869a3c2d | 94 | |
a58c490f JG |
95 | DBG("Deserializing action from buffer"); |
96 | switch (action_comm->action_type) { | |
97 | case LTTNG_ACTION_TYPE_NOTIFY: | |
869a3c2d | 98 | create_from_buffer_cb = lttng_action_notify_create_from_buffer; |
a58c490f JG |
99 | break; |
100 | default: | |
869a3c2d | 101 | consumed_len = -1; |
a58c490f JG |
102 | goto end; |
103 | } | |
104 | ||
869a3c2d SM |
105 | /* Create buffer view for the action-type-specific data. */ |
106 | specific_action_view = lttng_buffer_view_from_view(view, | |
107 | sizeof(struct lttng_action_comm), | |
108 | view->size - sizeof(struct lttng_action_comm)); | |
109 | ||
110 | specific_action_consumed_len = | |
111 | create_from_buffer_cb(&specific_action_view, action); | |
112 | if (specific_action_consumed_len < 0) { | |
113 | ERR("Failed to create specific action from buffer."); | |
114 | consumed_len = -1; | |
a58c490f JG |
115 | goto end; |
116 | } | |
869a3c2d SM |
117 | |
118 | assert(*action); | |
119 | ||
120 | consumed_len = sizeof(struct lttng_action_comm) + | |
121 | specific_action_consumed_len; | |
122 | ||
a58c490f | 123 | end: |
869a3c2d | 124 | return consumed_len; |
a58c490f | 125 | } |