more meaningful retcods for ch[fs]context
[lxc-userspace.git] / setns.c
1 #include <Python.h>
2 #include <fcntl.h>
3 #include <stdio.h>
4 #include <asm/unistd.h>
5 #include <sys/mount.h>
6 #include <errno.h>
7
8 static PyObject *
9 proc_mount(PyObject *self, PyObject *args)
10 {
11     int sts; 
12     sts = mount("none","/proc","proc",0,NULL);
13
14     return Py_BuildValue("i", sts);
15 }
16
17 static PyObject *
18 proc_umount(PyObject *self, PyObject *args)
19 {
20     int sts; 
21     sts = umount("/proc");
22
23     return Py_BuildValue("i", sts);
24 }
25
26 static PyObject *
27 chfscontext(PyObject *self, PyObject *args)
28 {
29     const char *filepath;
30     int sts;
31
32     if (!PyArg_ParseTuple(args, "s", &filepath))
33         return NULL;
34
35     int fd = open(filepath, O_RDONLY);
36     if (fd < 0) {
37         sts = -errno;
38         goto out;
39     }
40     
41     if (setns(fd, 0)) {
42         sts = -errno;
43     }
44     close(fd);
45     sts = 0;
46
47 out:
48     return Py_BuildValue("i", sts);
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         sts = -errno;
63         goto out;
64     }
65     
66     if (setns(fd, 0)) {
67         sts = -errno;
68     }
69     close(fd);
70     sts = 0;
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 }