lxcsu proc bug fix
[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 proc_umount(PyObject *self, PyObject *args)
43 {
44     int sts; 
45     sts = umount("/proc");
46
47     return Py_BuildValue("i", sts);
48
49 }
50
51 static PyObject *
52 chcontext(PyObject *self, PyObject *args)
53 {
54     const char *filepath;
55     int sts;
56
57     if (!PyArg_ParseTuple(args, "s", &filepath))
58         return NULL;
59
60     int fd = open(filepath, O_RDONLY);
61     if (fd < 0) {
62 //      printf("Could not open ns file\n");
63         sts = -1;
64         goto out;
65     }
66     
67     if (setns(fd, 0)) {
68         sts = -1;
69     }
70     close(fd);
71
72 out:
73     return Py_BuildValue("i", sts);
74 }
75
76 static PyMethodDef SetnsMethods[] =
77 {
78          {"proc_mount", proc_mount, METH_VARARGS, "Mount a volume via the mount system call."},
79          {"proc_umount", proc_umount, METH_VARARGS, "Umount a volume via the umount system call."},
80          {"chcontext", chcontext, METH_VARARGS, "Switch into an lxc container."},
81          {"chfscontext", chfscontext, METH_VARARGS, "Switch into an lxc container."},
82               {NULL, NULL, 0, NULL}
83 };
84  
85 PyMODINIT_FUNC
86  
87 initsetns(void)
88 {
89          (void) Py_InitModule("setns", SetnsMethods);
90 }