source: trunk/packages/xen-3.1/xen-3.1/tools/python/xen/lowlevel/scf/scf.c @ 34

Last change on this file since 34 was 34, checked in by hartmans, 18 years ago

Add xen and xen-common

File size: 4.5 KB
Line 
1/*
2 * Permission is hereby granted, free of charge, to any person obtaining a copy
3 * of this software and associated documentation files (the "Software"), to
4 * deal in the Software without restriction, including without limitation the
5 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6 * sell copies of the Software, and to permit persons to whom the Software is
7 * furnished to do so, subject to the following conditions:
8 *
9 * The above copyright notice and this permission notice shall be included in
10 * all copies or substantial portions of the Software.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
18 * DEALINGS IN THE SOFTWARE.
19 *
20 * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
21 * Use is subject to license terms.
22 */
23
24#include <Python.h>
25
26#include <libscf.h>
27#include <stdio.h>
28
29#define XEND_FMRI "svc:/system/xctl/xend:default"
30#define XEND_PG "config"
31
32static PyObject *scf_exc;
33
34static void *
35scf_exception(const char *err, const char *value)
36{
37        int scferr = scf_error();
38        const char *scfstrerr = scf_strerror(scferr);
39        PyObject *obj = Py_BuildValue("(isss)", scferr, err, scfstrerr, value);
40        PyErr_SetObject(scf_exc, obj);
41        return (NULL);
42}
43
44static PyObject *
45pyscf_get_bool(PyObject *o, PyObject *args, PyObject *kwargs)
46{
47        static char *kwlist[] = { "name", NULL };
48        scf_simple_prop_t *prop;
49        uint8_t *val;
50        char *name;
51
52        if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &name))
53                return (NULL);
54
55        prop = scf_simple_prop_get(NULL, XEND_FMRI, XEND_PG, name);
56
57        if (prop == NULL)
58                return (scf_exception("scf_simple_prop_get() failed", name));
59
60        if ((val = scf_simple_prop_next_boolean(prop)) == NULL)
61                return (scf_exception("scf_simple_prop_next_boolean() failed",
62                    name));
63
64        if (*val) {
65                scf_simple_prop_free(prop);
66                Py_INCREF(Py_True);
67                return (Py_True);
68        }
69
70        scf_simple_prop_free(prop);
71        Py_INCREF(Py_False);
72        return (Py_False);
73}
74
75static PyObject *
76pyscf_get_int(PyObject *o, PyObject *args, PyObject *kwargs)
77{
78        static char *kwlist[] = { "name", NULL };
79        scf_simple_prop_t *prop;
80        PyObject *obj;
81        int64_t *val;
82        char *name;
83
84        if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &name))
85                return (NULL);
86
87        prop = scf_simple_prop_get(NULL, XEND_FMRI, XEND_PG, name);
88
89        if (prop == NULL)
90                return (scf_exception("scf_simple_prop_get() failed", name));
91
92        if ((val = scf_simple_prop_next_integer(prop)) == NULL)
93                return (scf_exception("scf_simple_prop_next_integer() failed",
94                    name));
95
96        obj = PyInt_FromLong((long)*val);
97        scf_simple_prop_free(prop);
98        return (obj);
99}
100
101static PyObject *
102pyscf_get_string(PyObject *o, PyObject *args, PyObject *kwargs)
103{
104        static char *kwlist[] = { "name", NULL };
105        scf_simple_prop_t *prop;
106        PyObject *obj;
107        char *name;
108        char *str;
109
110        if (!PyArg_ParseTupleAndKeywords(args, kwargs, "s", kwlist, &name))
111                return (NULL);
112
113        prop = scf_simple_prop_get(NULL, XEND_FMRI, XEND_PG, name);
114
115        if (prop == NULL)
116                return (scf_exception("scf_simple_prop_get() failed", name));
117
118        if ((str = scf_simple_prop_next_astring(prop)) == NULL) {
119                scf_simple_prop_free(prop);
120                return (scf_exception("scf_simple_prop_next_astring() failed",
121                    name));
122        }
123
124        obj = PyString_FromString(str);
125        scf_simple_prop_free(prop);
126        return (obj);
127}
128
129PyDoc_STRVAR(pyscf_get_bool__doc__,
130   "get_bool(name) - get the value of the named boolean property");
131PyDoc_STRVAR(pyscf_get_int__doc__,
132   "get_int(name) - get the value of the named integer property");
133PyDoc_STRVAR(pyscf_get_string__doc__,
134   "get_string(name) - get the value of the named string property");
135
136static struct PyMethodDef pyscf_module_methods[] = {
137        { "get_bool", (PyCFunction) pyscf_get_bool,
138          METH_VARARGS|METH_KEYWORDS, pyscf_get_bool__doc__ },
139        { "get_int", (PyCFunction) pyscf_get_int,
140          METH_VARARGS|METH_KEYWORDS, pyscf_get_int__doc__ },
141        { "get_string", (PyCFunction) pyscf_get_string,
142          METH_VARARGS|METH_KEYWORDS, pyscf_get_string__doc__ },
143        { NULL, NULL, 0, NULL } 
144};
145
146PyMODINIT_FUNC
147initscf(void)
148{
149        PyObject *m;
150        m = Py_InitModule("scf", pyscf_module_methods);
151
152        scf_exc = PyErr_NewException("scf.error", NULL, NULL);
153        Py_INCREF(scf_exc);
154        PyModule_AddObject(m, "error", scf_exc);
155        PyModule_AddIntConstant(m, "SCF_ERROR_NOT_FOUND", SCF_ERROR_NOT_FOUND);
156}
Note: See TracBrowser for help on using the repository browser.