aboutsummaryrefslogtreecommitdiffstats
path: root/guest_compat.c
blob: fd4704b20b1698b6e72a4f8077eebc3dc2d13192 (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
#include "kvm/guest_compat.h"

#include "kvm/mutex.h"

#include <linux/kernel.h>
#include <linux/list.h>

struct compat_message {
	int id;
	char *title;
	char *desc;

	struct list_head list;
};

static int id;
static DEFINE_MUTEX(compat_mtx);
static LIST_HEAD(messages);

static void compat__free(struct compat_message *msg)
{
	free(msg->title);
	free(msg->desc);
	free(msg);
}

int compat__add_message(const char *title, const char *desc)
{
	struct compat_message *msg;
	int msg_id;

	msg = malloc(sizeof(*msg));
	if (msg == NULL)
		goto cleanup;

	msg->title = strdup(title);
	msg->desc = strdup(desc);

	if (msg->title == NULL || msg->desc == NULL)
		goto cleanup;

	mutex_lock(&compat_mtx);

	msg->id = msg_id = id++;
	list_add_tail(&msg->list, &messages);

	mutex_unlock(&compat_mtx);

	return msg_id;

cleanup:
	if (msg)
		compat__free(msg);

	return -ENOMEM;
}

int compat__remove_message(int id)
{
	struct compat_message *pos, *n;

	mutex_lock(&compat_mtx);

	list_for_each_entry_safe(pos, n, &messages, list) {
		if (pos->id == id) {
			list_del(&pos->list);
			compat__free(pos);

			mutex_unlock(&compat_mtx);

			return 0;
		}
	}

	mutex_unlock(&compat_mtx);

	return -ENOENT;
}

int compat__print_all_messages(void)
{
	mutex_lock(&compat_mtx);

	while (!list_empty(&messages)) {
		struct compat_message *msg;

		msg = list_first_entry(&messages, struct compat_message, list);

		printf("\n  # KVM compatibility warning.\n\t%s\n\t%s\n",
			msg->title, msg->desc);

		list_del(&msg->list);
		compat__free(msg);
	}

	mutex_unlock(&compat_mtx);

	return 0;
}