iron out argument parser usage, add --debug option, and get this to work again in...
[lxc-userspace.git] / setns.c
1 #include <Python.h>
2 #include <fcntl.h>
3 #include <stdio.h>
4 #include <asm-generic/unistd.h>
5
6 static PyObject *
7 chfscontext(PyObject *self, PyObject *args)
8 {
9     const char *filepath;
10     int sts;
11
12     if (!PyArg_ParseTuple(args, "s", &filepath))
13         return NULL;
14
15     int fd = open(filepath, O_RDONLY);
16     if (fd < 0) {
17             //printf("Could not open ns file\n");
18         sts = -1;
19         goto out;
20     }
21     
22     if (setns(fd, 666)) {
23         sts = -1;
24     }
25     close(fd);
26
27 out:
28     return Py_BuildValue("i", sts);
29 }
30 static PyObject *
31 chcontext(PyObject *self, PyObject *args)
32 {
33     const char *filepath;
34     int sts;
35
36     if (!PyArg_ParseTuple(args, "s", &filepath))
37         return NULL;
38
39     int fd = open(filepath, O_RDONLY);
40     if (fd < 0) {
41 //      printf("Could not open ns file\n");
42         sts = -1;
43         goto out;
44     }
45     
46     if (setns(fd, 0)) {
47         sts = -1;
48     }
49     close(fd);
50
51 out:
52     return Py_BuildValue("i", sts);
53 }
54
55 static PyMethodDef SetnsMethods[] =
56 {
57          {"chcontext", chcontext, METH_VARARGS, "Switch into an lxc container."},
58          {"chfscontext", chfscontext, METH_VARARGS, "Switch into an lxc container."},
59               {NULL, NULL, 0, NULL}
60 };
61  
62 PyMODINIT_FUNC
63  
64 initsetns(void)
65 {
66          (void) Py_InitModule("setns", SetnsMethods);
67 }