lxc.c revision dc23c1c817da5c13529432270e51d0f7f3b1e95e
/*
* python-lxc: Python bindings for LXC
*
* (C) Copyright Canonical Ltd. 2012-2013
*
* Authors:
* Stéphane Graber <stgraber@ubuntu.com>
*
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <Python.h>
#include "structmember.h"
#include <lxc/lxccontainer.h>
#include <stdio.h>
typedef struct {
struct lxc_container *container;
} Container;
char**
int i, j;
return NULL;
}
for (i = 0; i < argc; i++) {
if (!PyUnicode_Check(pyobj)) {
goto error;
}
if (!pystr) {
/* Maybe it wasn't UTF-8 encoded. An exception is already set. */
goto error;
}
if (!str) {
/* Maybe pystr wasn't a valid object. An exception is already set.
*/
goto error;
}
/* We must make a copy of str, because it points into internal memory
* which we do not own. Assume it's NULL terminated, otherwise we'd
* have to use PyUnicode_AsUTF8AndSize() and be explicit about copying
* the memory.
*/
/* Do not decref pyobj since we stole a reference by using
* PyTuple_GET_ITEM().
*/
goto error;
}
}
return result;
/* We can only iterate up to but not including i because malloc() does not
* initialize its memory. Thus if we got here, i points to the index
* after the last strdup'd entry in result.
*/
for (j = 0; j < i; j++)
return NULL;
}
static void
{
}
static PyObject *
{
}
static int
{
char *config_path = NULL;
&name,
return -1;
if (fs_config_path != NULL) {
}
return -1;
}
return 0;
}
static PyObject *
{
return PyUnicode_FromString(lxc_get_default_config_path());
}
static PyObject *
{
return PyUnicode_FromString(lxc_get_version());
}
// Container properties
static PyObject *
{
return PyUnicode_FromString(
}
static PyObject *
{
}
}
static PyObject *
{
}
static PyObject *
{
}
static PyObject *
{
}
}
static PyObject *
{
}
// Container Functions
static PyObject *
{
&key))
return NULL;
}
}
static PyObject *
{
char* template_name = NULL;
char** create_args = {NULL};
int i = 0;
&template_name, &vargs))
return NULL;
if (vargs) {
if (PyTuple_Check(vargs)) {
if (!create_args) {
return NULL;
}
}
else {
return NULL;
}
}
else
if (vargs) {
/* We cannot have gotten here unless vargs was given and create_args
* was successfully allocated.
*/
for (i = 0; i < PyTuple_GET_SIZE(vargs); i++)
free(create_args[i]);
}
return retval;
}
static PyObject *
{
}
}
static PyObject *
{
}
}
static PyObject *
{
int len = 0;
&key))
return NULL;
if (len < 0) {
return NULL;
}
return PyErr_NoMemory();
return NULL;
}
return ret;
}
static PyObject *
{
int len = 0;
&key))
return NULL;
if (len < 0) {
return NULL;
}
return PyErr_NoMemory();
return NULL;
}
return ret;
}
static PyObject *
{
return PyUnicode_FromString(
}
static PyObject *
{
int len = 0;
&key))
return NULL;
if (len < 0) {
return NULL;
}
return PyErr_NoMemory();
return NULL;
}
return ret;
}
static PyObject *
{
int scope = 0;
int i = 0;
return NULL;
/* Get the IPs */
if (!ips)
return PyTuple_New(0);
/* Count the entries */
while (ips[i])
i++;
/* Create the new tuple */
ret = PyTuple_New(i);
if (!ret)
return NULL;
/* Add the entries to the tuple and free the memory */
i = 0;
while (ips[i]) {
if (!unicode) {
break;
}
i++;
}
/* Free the list of IPs */
i = 0;
while (ips[i]) {
i++;
}
return ret;
}
static PyObject *
{
return NULL;
}
}
}
static PyObject *
{
return NULL;
}
}
}
static PyObject *
{
return NULL;
}
}
static PyObject *
{
return NULL;
}
}
static PyObject *
{
&path))
return NULL;
}
}
static PyObject *
{
int timeout = -1;
&timeout))
return NULL;
}
}
static PyObject *
{
int init_useinit = 0, i = 0;
return NULL;
init_useinit = 1;
}
if (!init_args) {
return NULL;
}
}
else
if (vargs) {
/* We cannot have gotten here unless vargs was given and create_args
* was successfully allocated.
*/
for (i = 0; i < PyTuple_GET_SIZE(vargs); i++)
}
return retval;
}
static PyObject *
{
}
}
static PyObject *
{
}
}
static PyObject *
{
&escape))
return NULL;
}
}
static PyObject *
{
return NULL;
return NULL;
}
return PyLong_FromLong(masterfd);
}
static PyObject *
{
int timeout = -1;
return NULL;
}
}
static PyGetSetDef Container_getseters[] = {
{"config_file_name",
"Path to the container configuration",
NULL},
{"defined",
"Boolean indicating whether the container configuration exists",
NULL},
{"init_pid",
"PID of the container's init process in the host's PID namespace",
NULL},
{"name",
"Container name",
NULL},
{"running",
"Boolean indicating whether the container is running or not",
NULL},
{"state",
"Container state",
NULL},
};
static PyMethodDef Container_methods[] = {
"clear_config_item(key) -> boolean\n"
"\n"
"Clear the current value of a config key."
},
"create(template, args = (,)) -> boolean\n"
"\n"
"Create a new rootfs for the container, using the given template "
"and passing some optional arguments to it."
},
"destroy() -> boolean\n"
"\n"
"Destroys the container."
},
"freeze() -> boolean\n"
"\n"
"Freezes the container and returns its return code."
},
"get_cgroup_item(key) -> string\n"
"\n"
"Get the current value of a cgroup entry."
},
"get_config_item(key) -> string\n"
"\n"
"Get the current value of a config key."
},
"get_config_path() -> string\n"
"\n"
"Return the LXC config path (where the containers are stored)."
},
"get_keys(key) -> string\n"
"\n"
"Get a list of valid sub-keys for a key."
},
"get_ips(interface, family, scope) -> tuple\n"
"\n"
"Get a tuple of IPs for the container."
},
"load_config(path = DEFAULT) -> boolean\n"
"\n"
"Read the container configuration from its default "
"location or from an alternative location if provided."
},
"save_config(path = DEFAULT) -> boolean\n"
"\n"
"Save the container configuration to its default "
"location or to an alternative location if provided."
},
"set_cgroup_item(key, value) -> boolean\n"
"\n"
"Set a cgroup entry to the provided value."
},
"set_config_item(key, value) -> boolean\n"
"\n"
"Set a config key to the provided value."
},
"set_config_path(path) -> boolean\n"
"\n"
"Set the LXC config path (where the containers are stored)."
},
"shutdown(timeout = -1) -> boolean\n"
"\n"
"Sends SIGPWR to the container and wait for it to shutdown "
"unless timeout is set to a positive value, in which case "
"the container will be killed when the timeout is reached."
},
"start(useinit = False, cmd = (,)) -> boolean\n"
"\n"
"Start the container, optionally using lxc-init and "
"an alternate init command, then returns its return code."
},
"stop() -> boolean\n"
"\n"
"Stop the container and returns its return code."
},
"unfreeze() -> boolean\n"
"\n"
"Unfreezes the container and returns its return code."
},
"wait(state, timeout = -1) -> boolean\n"
"\n"
"Wait for the container to reach a given state or timeout."
},
"console(ttynum = -1, stdinfd = 0, stdoutfd = 1, stderrfd = 2, escape = 0) -> boolean\n"
"\n"
"Attach to container's console."
},
"console(ttynum = -1) -> boolean\n"
"\n"
"Attach to container's console."
},
};
static PyTypeObject _lxc_ContainerType = {
"lxc.Container", /* tp_name */
sizeof(Container), /* tp_basicsize */
0, /* tp_itemsize */
0, /* tp_print */
0, /* tp_getattr */
0, /* tp_setattr */
0, /* tp_reserved */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
0, /* tp_as_mapping */
0, /* tp_hash */
0, /* tp_call */
0, /* tp_str */
0, /* tp_getattro */
0, /* tp_setattro */
0, /* tp_as_buffer */
Py_TPFLAGS_BASETYPE, /* tp_flags */
"Container objects", /* tp_doc */
0, /* tp_traverse */
0, /* tp_clear */
0, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
Container_methods, /* tp_methods */
0, /* tp_members */
Container_getseters, /* tp_getset */
0, /* tp_base */
0, /* tp_dict */
0, /* tp_descr_get */
0, /* tp_descr_set */
0, /* tp_dictoffset */
0, /* tp_alloc */
Container_new, /* tp_new */
};
static PyMethodDef LXC_methods[] = {
"Returns the current LXC config path"},
"Returns the current LXC library version"},
};
static PyModuleDef _lxcmodule = {
"_lxc",
"Binding for liblxc in python",
-1,
};
PyInit__lxc(void)
{
PyObject* m;
if (PyType_Ready(&_lxc_ContainerType) < 0)
return NULL;
m = PyModule_Create(&_lxcmodule);
if (m == NULL)
return NULL;
return m;
}