add --noslicehome option, and check /etc/lxcsu_default for default arguments
[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 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         sts = -1;
19         goto out;
20     }
21     
22     if (setns(fd, 0)) {
23         sts = -1;
24     }
25     close(fd);
26
27 out:
28     return Py_BuildValue("i", sts);
29 }
30
31 static PyObject *
32 proc_mount(PyObject *self, PyObject *args)
33 {
34     int sts; 
35     sts = mount("none","/proc","proc",0,NULL);
36
37     return Py_BuildValue("i", sts);
38 }
39
40 static PyObject *
41 proc_umount(PyObject *self, PyObject *args)
42 {
43     int sts; 
44     sts = umount("/proc");
45
46     return Py_BuildValue("i", sts);
47
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 }