aboutsummaryrefslogtreecommitdiffstats
path: root/extras/mini-os/lib/xs.c
blob: 324bd05d329b0ad7b5b2256a64a7ebe7df231dc7 (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
 * libxs-compatible layer
 *
 * Samuel Thibault <Samuel.Thibault@eu.citrix.net>, 2007-2008
 *
 * Mere wrapper around xenbus_*
 */

#ifdef HAVE_LIBC
#include <os.h>
#include <lib.h>
#include <xenstore.h>
#include <xenbus.h>
#include <stdlib.h>
#include <unistd.h>

static inline int _xs_fileno(struct xs_handle *h) {
    return (intptr_t) h;
}

struct xs_handle *xs_daemon_open()
{
    int fd = alloc_fd(FTYPE_XENBUS);
    files[fd].xenbus.events = NULL;
    printk("xs_daemon_open -> %d, %p\n", fd, &files[fd].xenbus.events);
    return (void*)(intptr_t) fd;
}

void xs_daemon_close(struct xs_handle *h)
{
    int fd = _xs_fileno(h);
    struct xenbus_event *event, *next;
    for (event = files[fd].xenbus.events; event; event = next)
    {
        next = event->next;
        free(event);
    }
    files[fd].type = FTYPE_NONE;
}

int xs_fileno(struct xs_handle *h)
{
    return _xs_fileno(h);
}

void *xs_read(struct xs_handle *h, xs_transaction_t t,
	     const char *path, unsigned int *len)
{
    char *value;
    char *msg;

    msg = xenbus_read(t, path, &value);
    if (msg) {
	printk("xs_read(%s): %s\n", path, msg);
	free(msg);
	return NULL;
    }

    if (len)
	*len = strlen(value);
    return value;
}

bool xs_write(struct xs_handle *h, xs_transaction_t t,
	      const char *path, const void *data, unsigned int len)
{
    char value[len + 1];
    char *msg;

    memcpy(value, data, len);
    value[len] = 0;

    msg = xenbus_write(t, path, value);
    if (msg) {
	printk("xs_write(%s): %s\n", path, msg);
	free(msg);
	return false;
    }
    return true;
}

static bool xs_bool(char *reply)
{
    if (!reply)
	return true;
    free(reply);
    return false;
}

bool xs_rm(struct xs_handle *h, xs_transaction_t t, const char *path)
{
    return xs_bool(xenbus_rm(t, path));
}

static void *xs_talkv(struct xs_handle *h, xs_transaction_t t,
		enum xsd_sockmsg_type type,
		struct write_req *iovec,
		unsigned int num_vecs,
		unsigned int *len)
{
    struct xsd_sockmsg *msg;
    void *ret;

    msg = xenbus_msg_reply(type, t, iovec, num_vecs);
    ret = malloc(msg->len);
    memcpy(ret, (char*) msg + sizeof(*msg), msg->len);
    if (len)
	*len = msg->len - 1;
    free(msg);
    return ret;
}

static void *xs_single(struct xs_handle *h, xs_transaction_t t,
		enum xsd_sockmsg_type type,
		const char *string,
		unsigned int *len)
{
    struct write_req iovec;

    iovec.data = (void *)string;
    iovec.len = strlen(string) + 1;

    return xs_talkv(h, t, type, &iovec, 1, len);
}

char *xs_get_domain_path(struct xs_handle *h, unsigned int domid)
{
    char domid_str[MAX_STRLEN(domid)];

    sprintf(domid_str, "%u", domid);

    return xs_single(h, XBT_NULL, XS_GET_DOMAIN_PATH, domid_str, NULL);
}

char **xs_directory(struct xs_handle *h, xs_transaction_t t,
		    const char *path, unsigned int *num)
{
    char *msg;
    char **entries, **res;
    char *entry;
    int i, n;
    int size;

    msg = xenbus_ls(t, path, &res);
    if (msg) {
	printk("xs_directory(%s): %s\n", path, msg);
	free(msg);
	return NULL;
    }

    size = 0;
    for (n = 0; res[n]; n++)
	size += strlen(res[n]) + 1;

    entries = malloc(n * sizeof(char *) + size);
    entry = (char *) (&entries[n]);

    for (i = 0; i < n; i++) {
	int l = strlen(res[i]) + 1;
	memcpy(entry, res[i], l);
	free(res[i]);
	entries[i] = entry;
	entry += l;
    }

    *num = n;
    free(res);
    return entries;
}

bool xs_watch(struct xs_handle *h, const char *path, const char *token)
{
    int fd = _xs_fileno(h);
    printk("xs_watch(%s, %s)\n", path, token);
    return xs_bool(xenbus_watch_path_token(XBT_NULL, path, token, &files[fd].xenbus.events));
}

char **xs_read_watch(struct xs_handle *h, unsigned int *num)
{
    int fd = _xs_fileno(h);
    struct xenbus_event *event;
    event = files[fd].xenbus.events;
    files[fd].xenbus.events = event->next;
    printk("xs_read_watch() -> %s %s\n", event->path, event->token);
    *num = 2;
    return (char **) &event->path;
}

bool xs_unwatch(struct xs_handle *h, const char *path, const char *token)
{
    printk("xs_unwatch(%s, %s)\n", path, token);
    return xs_bool(xenbus_unwatch_path_token(XBT_NULL, path, token));
}
#endif