SoW-2019-0002: Dynamic Snapshot
[lttng-tools.git] / src / common / notification.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/notification/notification-internal.h>
9 #include <lttng/condition/condition-internal.h>
10 #include <lttng/condition/evaluation-internal.h>
11 #include <lttng/condition/condition.h>
12 #include <lttng/condition/evaluation.h>
13 #include <assert.h>
14
15 LTTNG_HIDDEN
16 struct lttng_notification *lttng_notification_create(
17 struct lttng_condition *condition,
18 struct lttng_evaluation *evaluation)
19 {
20 struct lttng_notification *notification = NULL;
21
22 if (!condition || !evaluation) {
23 goto end;
24 }
25
26 notification = zmalloc(sizeof(struct lttng_notification));
27 if (!notification) {
28 goto end;
29 }
30
31 notification->condition = condition;
32 notification->evaluation = evaluation;
33 end:
34 return notification;
35 }
36
37 LTTNG_HIDDEN
38 int lttng_notification_serialize(const struct lttng_notification *notification,
39 struct lttng_dynamic_buffer *buf)
40 {
41 int ret;
42 size_t header_offset, size_before_payload;
43 struct lttng_notification_comm notification_comm = { 0 };
44 struct lttng_notification_comm *header;
45
46 header_offset = buf->size;
47 ret = lttng_dynamic_buffer_append(buf, &notification_comm,
48 sizeof(notification_comm));
49 if (ret) {
50 goto end;
51 }
52
53 size_before_payload = buf->size;
54 ret = lttng_condition_serialize(notification->condition, buf, NULL);
55 if (ret) {
56 goto end;
57 }
58
59 ret = lttng_evaluation_serialize(notification->evaluation, buf);
60 if (ret) {
61 goto end;
62 }
63
64 /* Update payload size. */
65 header = (struct lttng_notification_comm *) ((char *) buf->data + header_offset);
66 header->length = (uint32_t) (buf->size - size_before_payload);
67 end:
68 return ret;
69
70 }
71
72 LTTNG_HIDDEN
73 ssize_t lttng_notification_create_from_buffer(
74 const struct lttng_buffer_view *src_view,
75 struct lttng_notification **notification)
76 {
77 ssize_t ret, notification_size = 0, condition_size, evaluation_size;
78 const struct lttng_notification_comm *notification_comm;
79 struct lttng_condition *condition;
80 struct lttng_evaluation *evaluation;
81 struct lttng_buffer_view condition_view;
82 struct lttng_buffer_view evaluation_view;
83
84 if (!src_view || !notification) {
85 ret = -1;
86 goto end;
87 }
88
89 notification_comm =
90 (const struct lttng_notification_comm *) src_view->data;
91 notification_size += sizeof(*notification_comm);
92
93 /* struct lttng_condition */
94 condition_view = lttng_buffer_view_from_view(src_view,
95 sizeof(*notification_comm), -1);
96 condition_size = lttng_condition_create_from_buffer(&condition_view,
97 &condition);
98 if (condition_size < 0) {
99 ret = condition_size;
100 goto end;
101 }
102 notification_size += condition_size;
103
104 /* struct lttng_evaluation */
105 evaluation_view = lttng_buffer_view_from_view(&condition_view,
106 condition_size, -1);
107 evaluation_size = lttng_evaluation_create_from_buffer(&evaluation_view,
108 &evaluation);
109 if (evaluation_size < 0) {
110 ret = evaluation_size;
111 goto end;
112 }
113 notification_size += evaluation_size;
114
115 /* Unexpected size of inner-elements; the buffer is corrupted. */
116 if ((ssize_t) notification_comm->length !=
117 condition_size + evaluation_size) {
118 ret = -1;
119 goto error;
120 }
121
122 *notification = lttng_notification_create(condition, evaluation);
123 if (!*notification) {
124 ret = -1;
125 goto error;
126 }
127 ret = notification_size;
128 end:
129 return ret;
130 error:
131 lttng_condition_destroy(condition);
132 lttng_evaluation_destroy(evaluation);
133 return ret;
134 }
135
136 void lttng_notification_destroy(struct lttng_notification *notification)
137 {
138 if (!notification) {
139 return;
140 }
141
142 lttng_condition_destroy(notification->condition);
143 lttng_evaluation_destroy(notification->evaluation);
144 free(notification);
145 }
146
147 const struct lttng_condition *lttng_notification_get_condition(
148 struct lttng_notification *notification)
149 {
150 return notification ? notification->condition : NULL;
151 }
152
153 const struct lttng_evaluation *lttng_notification_get_evaluation(
154 struct lttng_notification *notification)
155 {
156 return notification ? notification->evaluation : NULL;
157 }
This page took 0.032499 seconds and 5 git commands to generate.