ia64/xen-unstable

view tools/python/xen/lowlevel/xs/xs.c @ 5448:a382b5e24996

bitkeeper revision 1.1713.1.5 (42ae0d44bPpuSBR0o475OuEOeDlNPg)

Fix xs Python module for Python <2.3.
Signed-off-by: Keir Fraser <keir@xensource.com>
author kaf24@firebug.cl.cam.ac.uk
date Mon Jun 13 22:48:36 2005 +0000 (2005-06-13)
parents d44caae57112
children dda0c275d413
line source
1 #include <Python.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <sys/types.h>
7 #include <sys/stat.h>
8 #include <fcntl.h>
10 #include "xs.h"
12 /** @file
13 * Python interface to the Xen Store Daemon (xs).
14 */
16 /* Needed for Python versions earlier than 2.3. */
17 #ifndef PyMODINIT_FUNC
18 #define PyMODINIT_FUNC DL_EXPORT(void)
19 #endif
21 #define PYPKG "xen.lowlevel.xs"
23 /** Python wrapper round an xs handle.
24 */
25 typedef struct XsHandle {
26 PyObject_HEAD;
27 struct xs_handle *xh;
28 } XsHandle;
30 static inline struct xs_handle *xshandle(PyObject *self)
31 {
32 struct xs_handle *xh = ((XsHandle*)self)->xh;
33 if (!xh)
34 PyErr_SetString(PyExc_RuntimeError, "invalid xenstore daemon handle");
35 return xh;
36 }
38 static inline PyObject *pyvalue_int(int val) {
39 return (val
40 ? PyInt_FromLong(val)
41 : PyErr_SetFromErrno(PyExc_RuntimeError));
42 }
44 static inline PyObject *pyvalue_str(char *val) {
45 return (val
46 ? PyString_FromString(val)
47 : PyErr_SetFromErrno(PyExc_RuntimeError));
48 }
50 static PyObject *xspy_write(PyObject *self, PyObject *args, PyObject *kwds)
51 {
52 static char *kwd_spec[] = { "path", "data", "create", "excl", NULL };
53 static char *arg_spec = "ss#|ii";
54 char *path = NULL;
55 char *data = NULL;
56 int data_n = 0;
57 int create = 0;
58 int excl = 0;
60 struct xs_handle *xh = xshandle(self);
61 PyObject *val = NULL;
62 int flags = 0;
63 int xsval = 0;
65 if (!xh)
66 goto exit;
67 if (!PyArg_ParseTupleAndKeywords(args, kwds, arg_spec, kwd_spec,
68 &path, &data, &data_n, &create, &excl))
69 goto exit;
70 if (create)
71 flags |= O_CREAT;
72 if (excl)
73 flags |= O_EXCL;
74 xsval = xs_write(xh, path, data, data_n, flags);
75 val = pyvalue_int(xsval);
76 exit:
77 return val;
78 }
80 static PyObject *xspy_read(PyObject *self, PyObject *args, PyObject *kwds)
81 {
82 static char *kwd_spec[] = { "path", NULL };
83 static char *arg_spec = "s|";
84 char *path = NULL;
86 struct xs_handle *xh = xshandle(self);
87 char *xsval = NULL;
88 unsigned int xsval_n = 0;
89 PyObject *val = NULL;
91 if (!xh)
92 goto exit;
93 if (!PyArg_ParseTupleAndKeywords(args, kwds, arg_spec, kwd_spec,
94 &path))
95 goto exit;
96 xsval = xs_read(xh, path, &xsval_n);
97 if (!xsval) {
98 val = pyvalue_int(0);
99 goto exit;
100 }
101 val = PyString_FromStringAndSize(xsval, xsval_n);
102 exit:
103 if (xsval)
104 free(xsval);
105 return val;
106 }
108 static PyObject *xspy_mkdir(PyObject *self, PyObject *args, PyObject *kwds)
109 {
110 static char *kwd_spec[] = { "path", NULL };
111 static char *arg_spec = "s|";
112 char *path = NULL;
114 struct xs_handle *xh = xshandle(self);
115 PyObject *val = NULL;
116 int xsval = 0;
118 if (!xh)
119 goto exit;
120 if (!PyArg_ParseTupleAndKeywords(args, kwds, arg_spec, kwd_spec, &path))
121 goto exit;
122 xsval = xs_mkdir(xh, path);
123 val = pyvalue_int(xsval);
124 exit:
125 return val;
126 }
128 static PyObject *xspy_ls(PyObject *self, PyObject *args, PyObject *kwds)
129 {
130 static char *kwd_spec[] = { "path", NULL };
131 static char *arg_spec = "s|";
132 char *path = NULL;
134 struct xs_handle *xh = xshandle(self);
135 PyObject *val = NULL;
136 char **xsval = NULL;
137 unsigned int xsval_n = 0;
138 int i;
140 if (!xh)
141 goto exit;
142 if (!PyArg_ParseTupleAndKeywords(args, kwds, arg_spec, kwd_spec, &path))
143 goto exit;
144 xsval = xs_directory(xh, path, &xsval_n);
145 if (!xsval) {
146 val = pyvalue_int(0);
147 goto exit;
148 }
149 val = PyList_New(xsval_n);
150 for (i = 0; i < xsval_n; i++)
151 PyList_SetItem(val, i, PyString_FromString(xsval[i]));
152 exit:
153 return val;
154 }
156 static PyObject *xspy_rm(PyObject *self, PyObject *args, PyObject *kwds)
157 {
158 static char *kwd_spec[] = { "path", NULL };
159 static char *arg_spec = "s|";
160 char *path = NULL;
162 struct xs_handle *xh = xshandle(self);
163 PyObject *val = NULL;
164 int xsval = 0;
166 if (!xh)
167 goto exit;
168 if (!PyArg_ParseTupleAndKeywords(args, kwds, arg_spec, kwd_spec, &path))
169 goto exit;
170 xsval = xs_rm(xh, path);
171 val = pyvalue_int(xsval);
172 exit:
173 return val;
174 }
176 static PyObject *xspy_get_permissions(PyObject *self, PyObject *args,
177 PyObject *kwds)
178 {
179 static char *kwd_spec[] = { "path", NULL };
180 static char *arg_spec = "s|";
181 char *path = NULL;
183 struct xs_handle *xh = xshandle(self);
184 PyObject *val = NULL;
185 struct xs_permissions *perms;
186 unsigned int perms_n = 0;
187 int i;
189 if (!xh)
190 goto exit;
191 if (!PyArg_ParseTupleAndKeywords(args, kwds, arg_spec, kwd_spec, &path))
192 goto exit;
193 perms = xs_get_permissions(xh, path, &perms_n);
194 if (!perms) {
195 PyErr_SetFromErrno(PyExc_RuntimeError);
196 goto exit;
197 }
198 val = PyList_New(perms_n);
199 for (i = 0; i < perms_n; i++, perms++) {
200 PyObject *p = Py_BuildValue("{s:i,s:i,s:i,s:i,s:i}",
201 "dom", perms->id,
202 "read", (perms->perms & XS_PERM_READ),
203 "write", (perms->perms & XS_PERM_WRITE),
204 "create", (perms->perms & XS_PERM_CREATE),
205 "owner", (perms->perms & XS_PERM_OWNER));
206 PyList_SetItem(val, i, p);
207 }
208 exit:
209 return val;
210 }
212 static PyObject *xspy_set_permissions(PyObject *self, PyObject *args,
213 PyObject *kwds)
214 {
215 static char *kwd_spec[] = { "path", "perms", NULL };
216 static char *arg_spec = "sO";
217 char *path = NULL;
218 PyObject *perms = NULL;
219 static char *perm_names[] = { "dom", "read", "write", "create", "owner",
220 NULL };
221 static char *perm_spec = "i|iiii";
223 struct xs_handle *xh = xshandle(self);
224 int i, xsval;
225 struct xs_permissions *xsperms = NULL;
226 int xsperms_n = 0;
227 PyObject *tuple0 = NULL;
228 PyObject *val = NULL;
230 if (!xh)
231 goto exit;
232 if (!PyArg_ParseTupleAndKeywords(args, kwds, arg_spec, kwd_spec,
233 &path, &perms))
234 goto exit;
235 if (!PyList_Check(perms)) {
236 PyErr_SetString(PyExc_RuntimeError, "perms must be a list");
237 goto exit;
238 }
239 xsperms_n = PyList_Size(perms);
240 xsperms = calloc(xsperms_n, sizeof(struct xs_permissions));
241 if (!xsperms) {
242 PyErr_SetString(PyExc_RuntimeError, "out of memory");
243 goto exit;
244 }
245 tuple0 = PyTuple_New(0);
246 if (!tuple0)
247 goto exit;
248 for (i = 0; i < xsperms_n; i++) {
249 /* Domain the permissions apply to. */
250 int dom = 0;
251 /* Read/write perms. Set these. */
252 int p_read = 0, p_write = 0;
253 /* Create/owner perms. Ignore them.
254 * This is so the output from get_permissions() can be used
255 * as input to set_permissions().
256 */
257 int p_create = 0, p_owner = 0;
258 PyObject *p = PyList_GetItem(perms, i);
259 if (!PyArg_ParseTupleAndKeywords(tuple0, p, perm_spec, perm_names,
260 &dom, &p_read, &p_write, &p_create,
261 &p_owner))
262 goto exit;
263 xsperms[i].id = dom;
264 if (p_read)
265 xsperms[i].perms |= XS_PERM_READ;
266 if (p_write)
267 xsperms[i].perms |= XS_PERM_WRITE;
268 }
269 xsval = xs_set_permissions(xh, path, xsperms, xsperms_n);
270 val = pyvalue_int(xsval);
271 exit:
272 Py_XDECREF(tuple0);
273 if (xsperms)
274 free(xsperms);
275 return val;
276 }
278 static PyObject *xspy_watch(PyObject *self, PyObject *args, PyObject *kwds)
279 {
280 static char *kwd_spec[] = { "path", "priority", NULL };
281 static char *arg_spec = "s|i";
282 char *path = NULL;
283 int priority = 0;
285 struct xs_handle *xh = xshandle(self);
286 PyObject *val = NULL;
287 int xsval = 0;
289 if (!xh)
290 goto exit;
291 if (!PyArg_ParseTupleAndKeywords(args, kwds, arg_spec, kwd_spec,
292 &path, &priority))
293 goto exit;
294 xsval = xs_watch(xh, path, priority);
295 val = pyvalue_int(xsval);
296 exit:
297 return val;
298 }
300 static PyObject *xspy_read_watch(PyObject *self, PyObject *args,
301 PyObject *kwds)
302 {
303 static char *kwd_spec[] = { NULL };
304 static char *arg_spec = "";
306 struct xs_handle *xh = xshandle(self);
307 PyObject *val = NULL;
308 char *xsval = NULL;
310 if (!xh)
311 goto exit;
312 if (!PyArg_ParseTupleAndKeywords(args, kwds, arg_spec, kwd_spec))
313 goto exit;
314 xsval = xs_read_watch(xh);
315 val = pyvalue_str(xsval);
316 exit:
317 if (xsval)
318 free(xsval);
319 return val;
320 }
322 static PyObject *xspy_acknowledge_watch(PyObject *self, PyObject *args,
323 PyObject *kwds)
324 {
325 static char *kwd_spec[] = { NULL };
326 static char *arg_spec = "";
328 struct xs_handle *xh = xshandle(self);
329 PyObject *val = NULL;
330 int xsval = 0;
332 if (!xh)
333 goto exit;
334 if (!PyArg_ParseTupleAndKeywords(args, kwds, arg_spec, kwd_spec))
335 goto exit;
336 xsval = xs_acknowledge_watch(xh);
337 val = pyvalue_int(xsval);
338 exit:
339 return val;
340 }
342 static PyObject *xspy_unwatch(PyObject *self, PyObject *args, PyObject *kwds)
343 {
344 static char *kwd_spec[] = { "path", NULL };
345 static char *arg_spec = "s|";
346 char *path = NULL;
348 struct xs_handle *xh = xshandle(self);
349 PyObject *val = NULL;
350 int xsval = 0;
352 if (!xh)
353 goto exit;
354 if (!PyArg_ParseTupleAndKeywords(args, kwds, arg_spec, kwd_spec, &path))
355 goto exit;
356 xsval = xs_unwatch(xh, path);
357 val = pyvalue_int(xsval);
358 exit:
359 return val;
360 }
362 static PyObject *xspy_transaction_start(PyObject *self, PyObject *args,
363 PyObject *kwds)
364 {
365 static char *kwd_spec[] = { "path", NULL };
366 static char *arg_spec = "s|";
367 char *path = NULL;
369 struct xs_handle *xh = xshandle(self);
370 PyObject *val = NULL;
371 int xsval = 0;
373 if (!xh)
374 goto exit;
375 if (!PyArg_ParseTupleAndKeywords(args, kwds, arg_spec, kwd_spec, &path))
376 goto exit;
377 xsval = xs_transaction_start(xh, path);
378 val = pyvalue_int(xsval);
379 exit:
380 return val;
381 }
383 static PyObject *xspy_transaction_end(PyObject *self, PyObject *args,
384 PyObject *kwds)
385 {
386 static char *kwd_spec[] = { "abort", NULL };
387 static char *arg_spec = "|i";
388 int abort = 0;
390 struct xs_handle *xh = xshandle(self);
391 PyObject *val = NULL;
392 int xsval = 0;
394 if (!xh)
395 goto exit;
396 if (!PyArg_ParseTupleAndKeywords(args, kwds, arg_spec, kwd_spec, &abort))
397 goto exit;
398 xsval = xs_transaction_end(xh, abort);
399 val = pyvalue_int(xsval);
400 exit:
401 return val;
402 }
404 static PyObject *xspy_introduce_domain(PyObject *self, PyObject *args,
405 PyObject *kwds)
406 {
407 static char *kwd_spec[] = { "dom", "page", "port", "path", NULL };
408 static char *arg_spec = "iiis|";
409 domid_t dom = 0;
410 unsigned long page = 0;
411 unsigned int port = 0;
412 char *path = NULL;
414 struct xs_handle *xh = xshandle(self);
415 PyObject *val = NULL;
416 int xsval = 0;
418 if (!xh)
419 goto exit;
420 if (!PyArg_ParseTupleAndKeywords(args, kwds, arg_spec, kwd_spec,
421 &dom, &page, &port, &path))
422 goto exit;
423 printf("%s> dom=%u page=0x%08lx port=%u path=%s\n", __FUNCTION__, dom,
424 page, port, path);
425 xsval = xs_introduce_domain(xh, dom, page, port, path);
426 printf("%s> xsval=%d\n", __FUNCTION__, xsval);
427 val = pyvalue_int(xsval);
428 exit:
429 return val;
430 }
432 static PyObject *xspy_release_domain(PyObject *self, PyObject *args,
433 PyObject *kwds)
434 {
435 static char *kwd_spec[] = { "dom", NULL };
436 static char *arg_spec = "i|";
437 domid_t dom;
439 struct xs_handle *xh = xshandle(self);
440 PyObject *val = NULL;
441 int xsval = 0;
443 if (!xh)
444 goto exit;
445 if (!PyArg_ParseTupleAndKeywords(args, kwds, arg_spec, kwd_spec,
446 &dom))
447 goto exit;
448 printf("%s> dom=%u\n", __FUNCTION__, dom);
449 xsval = xs_release_domain(xh, dom);
450 printf("%s> xsval=%d\n", __FUNCTION__, xsval);
451 val = pyvalue_int(xsval);
452 exit:
453 return val;
454 }
456 static PyObject *xspy_close(PyObject *self, PyObject *args, PyObject *kwds)
457 {
458 static char *kwd_spec[] = { NULL };
459 static char *arg_spec = "";
461 struct xs_handle *xh = xshandle(self);
462 PyObject *val = NULL;
463 int xsval = 1;
465 if (!xh)
466 goto exit;
467 if (!PyArg_ParseTupleAndKeywords(args, kwds, arg_spec, kwd_spec))
468 goto exit;
469 xs_daemon_close(xh);
470 ((XsHandle*)self)->xh = NULL;
471 val = pyvalue_int(xsval);
472 exit:
473 return val;
474 }
476 static PyObject *xspy_shutdown(PyObject *self, PyObject *args, PyObject *kwds)
477 {
478 static char *kwd_spec[] = { NULL };
479 static char *arg_spec = "";
481 struct xs_handle *xh = xshandle(self);
482 PyObject *val = NULL;
483 int xsval = 0;
485 if (!xh)
486 goto exit;
487 if (!PyArg_ParseTupleAndKeywords(args, kwds, arg_spec, kwd_spec))
488 goto exit;
489 xsval = xs_shutdown(xh);
490 val = pyvalue_int(xsval);
491 exit:
492 return val;
493 }
495 #define XSPY_METH(_name) \
496 #_name, \
497 (PyCFunction) xspy_ ## _name, \
498 (METH_VARARGS | METH_KEYWORDS)
499 // mtime
500 // ctime
502 static PyMethodDef xshandle_methods[] = {
503 { XSPY_METH(read),
504 "read(path) : read data\n" },
505 { XSPY_METH(write),
506 "write(path, data, [creat], [excl]): write data\n" },
507 { XSPY_METH(ls),
508 "ls(path): list directory.\n" },
509 { XSPY_METH(mkdir),
510 "mkdir(path): make a directory.\n" },
511 { XSPY_METH(rm),
512 "rm(path): remove a path (dir must be empty).\n" },
513 { XSPY_METH(get_permissions),
514 "get_permissions(path)\n" },
515 { XSPY_METH(set_permissions),
516 "set_permissions(path)\n" },
517 { XSPY_METH(watch),
518 "watch(path)\n" },
519 { XSPY_METH(read_watch),
520 "read_watch()\n" },
521 { XSPY_METH(acknowledge_watch),
522 "acknowledge_watch()\n" },
523 { XSPY_METH(unwatch),
524 "unwatch()\n" },
525 { XSPY_METH(transaction_start),
526 "transaction_start()\n" },
527 { XSPY_METH(transaction_end),
528 "transaction_end([abort])\n" },
529 { XSPY_METH(introduce_domain),
530 "introduce_domain(dom, page, port)\n" },
531 { XSPY_METH(release_domain),
532 "release_domain(dom)\n" },
533 { XSPY_METH(close),
534 "close()\n" },
535 { XSPY_METH(shutdown),
536 "shutdown()\n" },
537 { NULL, NULL, 0, NULL }
538 };
540 static PyObject *xshandle_getattr(PyObject *self, char *name)
541 {
542 PyObject *val = NULL;
543 if (strcmp(name, "fileno") == 0) {
544 struct xs_handle *xh = xshandle(self);
545 val = PyInt_FromLong((xh ? xs_fileno(xh) : -1));
546 } else
547 val = Py_FindMethod(xshandle_methods, self, name);
548 return val;
549 }
551 static void xshandle_dealloc(PyObject *self)
552 {
553 XsHandle *xh = (XsHandle*)self;
554 if (xh->xh) {
555 xs_daemon_close(xh->xh);
556 xh->xh = NULL;
557 }
558 PyObject_Del(self);
559 }
561 static PyTypeObject xshandle_type = {
562 PyObject_HEAD_INIT(&PyType_Type)
563 0,
564 "xshandle",
565 sizeof(XsHandle),
566 0,
567 xshandle_dealloc, /* tp_dealloc */
568 NULL, /* tp_print */
569 xshandle_getattr, /* tp_getattr */
570 NULL, /* tp_setattr */
571 NULL, /* tp_compare */
572 NULL, /* tp_repr */
573 NULL, /* tp_as_number */
574 NULL, /* tp_as_sequence */
575 NULL, /* tp_as_mapping */
576 NULL /* tp_hash */
577 };
579 static PyObject *xshandle_open(PyObject *self, PyObject *args, PyObject *kwds)
580 {
581 static char *kwd_spec[] = { "readonly", NULL };
582 static char *arg_spec = "|i";
583 int readonly = 0;
585 XsHandle *xsh = NULL;
586 PyObject *val = NULL;
588 if (!PyArg_ParseTupleAndKeywords(args, kwds, arg_spec, kwd_spec,
589 &readonly))
590 goto exit;
592 xsh = PyObject_New(XsHandle, &xshandle_type);
593 if (!xsh)
594 goto exit;
595 xsh->xh = (readonly ? xs_daemon_open_readonly() : xs_daemon_open());
596 if (!xsh->xh) {
597 PyObject_Del(xsh);
598 val = pyvalue_int(0);
599 goto exit;
600 }
601 val = (PyObject *)xsh;
602 exit:
603 return val;
604 }
606 static PyMethodDef xs_methods[] = {
607 { "open", (PyCFunction)xshandle_open, (METH_VARARGS | METH_KEYWORDS),
608 "Open a connection to the xenstore daemon.\n" },
609 { NULL, NULL, 0, NULL }
610 };
612 PyMODINIT_FUNC initxs (void)
613 {
614 PyObject *module;
616 module = Py_InitModule(PYPKG, xs_methods);
617 }