summaryrefslogtreecommitdiff
path: root/src/event_types/action_executed_event.c
blob: 4af8362a877ea968e7a0a0f5349120a4f71d9f9d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
/*
 * This file is part of faultd.
 *
 * Copyright © 2017 Samsung Electronics
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <errno.h>
#include <malloc.h>

#include "common.h"
#include "action_executed_event.h"

static int allocate_ae_event(struct faultd_event_type *type,
                             void *data, struct faultd_event **ev)
{
	struct action_executed_event *ae_ev;
	struct ae_event_data *ae_ev_data = data;
	int ret;

	ae_ev = calloc(1, sizeof(*ae_ev));
	if (!ae_ev)
		return -ENOMEM;

	ret = faultd_event_init_internal(type, &ae_ev->event);
	if (ret)
		goto free_ae_ev;

	ae_ev->action = strdup(ae_ev_data->action);
	ae_ev->action_impl = strdup(ae_ev_data->action_impl);

	if (!ae_ev->action || !ae_ev->action_impl) {
		ret = -ENOMEM;
		goto cleanup_strs;
	}

	ae_ev->action_log = faultd_object_ref(ae_ev_data->action_log);
	ae_ev->result  = ae_ev_data->result;

	ae_ev->reason = ae_ev_data->reason;
	if (ae_ev->reason)
		faultd_event_ref(ae_ev->reason);

	*ev = &ae_ev->event;
	return 0;
cleanup_strs:
	free(ae_ev->action);
	free(ae_ev->action_impl);

	faultd_event_cleanup_internal(&ae_ev->event);
free_ae_ev:
	free(ae_ev);

	return ret;
}

static void ae_event_release(struct faultd_event *ev)
{
	struct action_executed_event *ae_ev =
		to_action_executed_event(ev);

	if (ae_ev->reason)
		faultd_event_unref(ae_ev->reason);
	free(ae_ev->action);
	free(ae_ev->action_impl);
	faultd_object_unref(ae_ev->action_log);
	faultd_event_cleanup_internal(&ae_ev->event);
	free(ae_ev);
}

static char *ae_event_to_string(struct faultd_event *ev)
{
	struct action_executed_event *ae_ev =
		to_action_executed_event(ev);
	char *str;
	int ret;

	/* TODO: add serialization? 
	bson2json(ae_ev->action_log.data, &buf, &len); */

	ret = asprintf(&str, "Action Executed Event:"
				   " Action: %s"
				   " Impl: %s"
				   " Result: %d",
				   ae_ev->action,
				   ae_ev->action_impl,
				   ae_ev->result);

	return ret > 0 ? str : NULL;
}

static void ae_event_serialize(struct faultd_event *ev, struct faultd_object *out)
{
	struct action_executed_event *ae_ev =
		to_action_executed_event(ev);

	faultd_event_serialize_internal(ev, out);
	faultd_object_append_oid(out, AE_EV_REASON, &ae_ev->reason->oid);
	faultd_object_append_string(out, AE_EV_ACTION, ae_ev->action);
	faultd_object_append_string(out, AE_EV_ACTION_IMPL, ae_ev->action_impl);
	faultd_object_append_object(out, AE_EV_ACTION_LOG, ae_ev->action_log);
	faultd_object_append_int(out, AE_EV_RESULT, ae_ev->result);
}

static struct faultd_event_type action_executed_event_type = {
	.name = ACTION_EXECUTED_EVENT_ID,
	.default_ops = {
		.release = ae_event_release,
		.serialize = ae_event_serialize,
		.to_string = ae_event_to_string,
	},
	.allocate_event = allocate_ae_event,
	.node = LIST_HEAD_INIT(action_executed_event_type.node),
};

FAULTD_EVENT_TYPE_REGISTER(action_executed_event_type, action_executed_et)