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