kvm tools, setup: Create private directory
[linux-2.6/next.git] / tools / kvm / guest_compat.c
blobc5bacb8a105365c719f1bef50f6d86d844ced500
1 #include "kvm/guest_compat.h"
3 #include "kvm/mutex.h"
5 #include <linux/kernel.h>
6 #include <linux/list.h>
8 struct compat_message {
9 int id;
10 char *title;
11 char *desc;
13 struct list_head list;
16 static int id;
17 static DEFINE_MUTEX(compat_mtx);
18 static LIST_HEAD(messages);
20 int compat__add_message(const char *title, const char *desc)
22 struct compat_message *msg;
24 mutex_lock(&compat_mtx);
25 msg = malloc(sizeof(*msg));
26 if (msg == NULL)
27 goto cleanup;
29 *msg = (struct compat_message) {
30 .id = id,
31 .title = strdup(title),
32 .desc = strdup(desc),
35 if (msg->title == NULL || msg->desc == NULL)
36 goto cleanup;
38 list_add_tail(&msg->list, &messages);
40 mutex_unlock(&compat_mtx);
42 return id++;
44 cleanup:
45 if (msg) {
46 free(msg->title);
47 free(msg->desc);
48 free(msg);
51 mutex_unlock(&compat_mtx);
53 return -ENOMEM;
56 static void compat__free(struct compat_message *msg)
58 free(msg->title);
59 free(msg->desc);
60 free(msg);
63 int compat__remove_message(int id)
65 struct compat_message *pos, *n;
67 mutex_lock(&compat_mtx);
69 list_for_each_entry_safe(pos, n, &messages, list) {
70 if (pos->id == id) {
71 list_del(&pos->list);
72 compat__free(pos);
74 mutex_unlock(&compat_mtx);
76 return 0;
80 mutex_unlock(&compat_mtx);
82 return -ENOENT;
85 int compat__print_all_messages(void)
87 mutex_lock(&compat_mtx);
89 while (!list_empty(&messages)) {
90 struct compat_message *msg;
92 msg = list_first_entry(&messages, struct compat_message, list);
94 printf("\n\n*** Compatability Warning ***\n\n\t%s\n\n%s\n",
95 msg->title, msg->desc);
97 list_del(&msg->list);
98 compat__free(msg);
101 mutex_unlock(&compat_mtx);
103 return 0;