Setting tag lxc-userspace-1.0-1
[lxc-userspace.git] / setns.c
1 #include <Python.h>
2 #include <fcntl.h>
3 #include <stdio.h>
4 #include <asm-generic/unistd.h>
5 #include <sys/mount.h>
6
7 static PyObject *
8 chfscontext(PyObject *self, PyObject *args)
9 {
10     const char *filepath;
11     int sts;
12
13     if (!PyArg_ParseTuple(args, "s", &filepath))
14         return NULL;
15
16     int fd = open(filepath, O_RDONLY);
17     if (fd < 0) {
18             //printf("Could not open ns file\n");
19         sts = -1;
20         goto out;
21     }
22     
23     if (setns(fd, 666)) {
24         sts = -1;
25     }
26     close(fd);
27
28 out:
29     return Py_BuildValue("i", sts);
30 }
31
32 static PyObject *
33 proc_mount(PyObject *self, PyObject *args)
34 {
35     int sts; 
36     sts = mount("none","/proc","proc",0,NULL);
37
38     return Py_BuildValue("i", sts);
39 }
40
41 static PyObject *
42 chcontext(PyObject *self, PyObject *args)
43 {
44     const char *filepath;
45     int sts;
46
47     if (!PyArg_ParseTuple(args, "s", &filepath))
48         return NULL;
49
50     int fd = open(filepath, O_RDONLY);
51     if (fd < 0) {
52 //      printf("Could not open ns file\n");
53         sts = -1;
54         goto out;
55     }
56     
57     if (setns(fd, 0)) {
58         sts = -1;
59     }
60     close(fd);
61
62 out:
63     return Py_BuildValue("i", sts);
64 }
65
66 static PyMethodDef SetnsMethods[] =
67 {
68          {"proc_mount", proc_mount, METH_VARARGS, "Mount a volume via the mount system call."},
69          {"chcontext", chcontext, METH_VARARGS, "Switch into an lxc container."},
70          {"chfscontext", chfscontext, METH_VARARGS, "Switch into an lxc container."},
71               {NULL, NULL, 0, NULL}
72 };
73  
74 PyMODINIT_FUNC
75  
76 initsetns(void)
77 {
78          (void) Py_InitModule("setns", SetnsMethods);
79 }