lxc.c revision 5d15c21a4ed7a5dd8369f06ebad0827c7f21bb29
/*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
* USA
*/
#include <Python.h>
#include "structmember.h"
#include <lxc/lxccontainer.h>
#include <lxc/namespace.h>
#include <stdio.h>
/* Helper functions */
char**
int argc;
int i, j;
/* not a list or tuple */
return NULL;
}
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;
}
struct lxc_attach_python_payload {
};
static int lxc_attach_python_exec(void* _payload)
{
struct lxc_attach_python_payload *payload =
(struct lxc_attach_python_payload *)_payload;
if (!result) {
PyErr_Print();
return -1;
}
if (PyLong_Check(result))
return (int)PyLong_AsLong(result);
else
return -1;
}
{
"initial_cwd", "uid", "gid", "env_policy",
"extra_env_vars", "extra_keep_env", "stdin",
int temp_env_policy;
bool parse_result;
if (!options) {
return NULL;
}
/* we need some dummy variables because we can't be sure
* the data types match completely */
temp_uid = -1;
temp_gid = -1;
/* we need a dummy tuple */
dummy = PyTuple_New(0);
&stdin_obj, &stdout_obj,
&stderr_obj);
/* immediately get rid of the dummy tuple */
if (!parse_result) {
return NULL;
}
/* duplicate the string, so we don't depend on some random Python object */
if (initial_cwd_obj != NULL) {
}
/* do the type conversion from the types that match the parse string */
if (extra_env_vars_obj)
if (extra_keep_env_obj)
if (stdin_obj) {
return NULL;
}
}
if (stdout_obj) {
return NULL;
}
}
if (stderr_obj) {
return NULL;
}
}
return options;
}
{
int i;
if (!options)
return;
if (options->initial_cwd)
if (options->extra_env_vars) {
for (i = 0; options->extra_env_vars[i]; i++)
}
if (options->extra_keep_env) {
for (i = 0; options->extra_keep_env[i]; i++)
}
}
/* Module functions */
static PyObject *
{
long rv = -1;
char *str;
if (!PyUnicode_Check(arg)) {
return NULL;
}
if (!pystr)
return NULL;
if (!str)
goto out;
if (rv == -1)
out:
}
static PyObject *
{
int i, rv;
NULL, /* program */
NULL /* argv[] */
};
return NULL;
} else {
"attach_run_command must be a list.");
return NULL;
}
return NULL;
return PyLong_FromLong(rv);
}
static PyObject *
{
int rv;
return PyLong_FromLong(rv);
}
static PyObject *
{
return PyUnicode_FromString(lxc_get_default_config_path());
}
static PyObject *
{
return PyUnicode_FromString(lxc_get_version());
}
static PyObject *
{
int list_count = 0;
int list_active = 1;
int list_defined = 1;
char* config_path = NULL;
int i = 0;
&config_path, &vargs))
return NULL;
/* We default to listing everything */
list_active = 0;
}
list_defined = 0;
}
/* Call the right API function based on filters */
else if (list_active == 1)
else if (list_defined == 1)
/* Handle failure */
if (list_count < 0) {
return NULL;
}
/* Generate the tuple */
for (i = 0; i < list_count; i++) {
}
return list;
}
/* Base type and functions for Container */
typedef struct {
struct lxc_container *container;
} Container;
static void
{
}
static int
{
char *config_path = NULL;
&name,
return -1;
if (fs_config_path != NULL) {
}
return -1;
}
return 0;
}
static PyObject *
{
}
/* Container properties */
static PyObject *
{
return PyUnicode_FromString(
}
static PyObject *
{
}
}
static PyObject *
{
}
static PyObject *
{
}
static PyObject *
{
}
}
static PyObject *
{
}
/* Container Functions */
static PyObject *
{
long ret;
return NULL;
return NULL;
}
if (!options)
return NULL;
if (ret < 0)
goto out;
if (wait) {
/* handle case where attach fails */
ret = -1;
} else {
}
out:
return PyLong_FromLong(ret);
}
static PyObject *
{
}
static PyObject *
{
}
static PyObject *
{
}
static PyObject *
{
&key))
return NULL;
}
}
static PyObject *
{
"escape", NULL};
&escape))
return NULL;
}
}
static PyObject *
{
return NULL;
&masterfd) < 0) {
return NULL;
}
return PyLong_FromLong(masterfd);
}
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 i = 0;
char** interfaces = NULL;
/* Get the interfaces */
if (!interfaces)
return PyTuple_New(0);
/* Count the entries */
while (interfaces[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 (interfaces[i]) {
if (!unicode) {
break;
}
i++;
}
/* Free the list of IPs */
i = 0;
while (interfaces[i]) {
free(interfaces[i]);
i++;
}
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 *
{
}
}
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 *
{
int timeout = -1;
return NULL;
}
}
/* Function/Properties list */
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[] = {
"attach(run, payload) -> int\n"
"\n"
"Attach to the container. Returns the pid of the attached process."
},
"attach(run, payload) -> int\n"
"\n"
"Attach to the container. Returns the exit code of the process."
},
"clear_config()\n"
"\n"
"Clear any container configuration."
},
"clear_config_item(key) -> boolean\n"
"\n"
"Clear the current value of a config key."
},
"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."
},
"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_interface() -> tuple\n"
"\n"
"Get a tuple of interfaces for the container."
},
"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."
},
"reboot() -> boolean\n"
"\n"
"Ask the container to reboot."
},
"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."
},
};
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 process personality of the corresponding architecture"},
"Runs a command when attaching, to use as the run parameter for attach "
"or attach_wait"},
"Starts up a shell when attaching, to use as the run parameter for "
"attach or attach_wait"},
"Returns the current LXC config path"},
"Returns the current LXC library version"},
"Returns a list of container names or objects"},
};
static PyModuleDef _lxcmodule = {
"_lxc",
"Binding for liblxc in python",
-1,
};
PyInit__lxc(void)
{
PyObject* m;
PyObject* d;
if (PyType_Ready(&_lxc_ContainerType) < 0)
return NULL;
m = PyModule_Create(&_lxcmodule);
if (m == NULL)
return NULL;
/* add constants */
d = PyModule_GetDict(m);
#define PYLXC_EXPORT_CONST(c) \
PyDict_SetItemString(d, #c, PyLong_FromLong(c))
/* environment variable handling */
/* attach options */
/* namespace flags (no other python lib exports this) */
return m;
}
/*
* kate: space-indent on; indent-width 4; mixedindent off; indent-mode cstyle;
*/