just re-order
[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
7 static PyObject *
8 proc_mount(PyObject *self, PyObject *args)
9 {
10     int sts; 
11     sts = mount("none","/proc","proc",0,NULL);
12
13     return Py_BuildValue("i", sts);
14 }
15
16 static PyObject *
17 proc_umount(PyObject *self, PyObject *args)
18 {
19     int sts; 
20     sts = umount("/proc");
21
22     return Py_BuildValue("i", sts);
23
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 = -1;
38         goto out;
39     }
40     
41     if (setns(fd, 0)) {
42         sts = -1;
43     }
44     close(fd);
45
46 out:
47     return Py_BuildValue("i", sts);
48 }
49
50 static PyObject *
51 chcontext(PyObject *self, PyObject *args)
52 {
53     const char *filepath;
54     int sts;
55
56     if (!PyArg_ParseTuple(args, "s", &filepath))
57         return NULL;
58
59     int fd = open(filepath, O_RDONLY);
60     if (fd < 0) {
61         sts = -1;
62         goto out;
63     }
64     
65     if (setns(fd, 0)) {
66         sts = -1;
67     }
68     close(fd);
69
70 out:
71     return Py_BuildValue("i", sts);
72 }
73
74 static PyMethodDef SetnsMethods[] =
75 {
76          {"proc_mount", proc_mount, METH_VARARGS, "Mount a volume via the mount system call."},
77          {"proc_umount", proc_umount, METH_VARARGS, "Umount a volume via the umount system call."},
78          {"chcontext", chcontext, METH_VARARGS, "Switch into an lxc container."},
79          {"chfscontext", chfscontext, METH_VARARGS, "Switch into an lxc container."},
80               {NULL, NULL, 0, NULL}
81 };
82  
83 PyMODINIT_FUNC
84  
85 initsetns(void)
86 {
87          (void) Py_InitModule("setns", SetnsMethods);
88 }