-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmountmodule.c
132 lines (100 loc) · 3.29 KB
/
mountmodule.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/* mountmodule.c - Python extension for Linux mount - by rbistolfi */
#include "Python.h"
#include <errno.h>
#include <string.h>
#include <sys/mount.h>
#include <linux/fs.h>
static PyObject *MountError;
static PyObject *
mount_mount(PyObject *self, PyObject *args)
{
const char *source;
const char *target;
const char *filesystemtype;
unsigned long mountflags;
const void *data;
int sts;
if (!PyArg_ParseTuple(args, "sssl|s", &source, &target, &filesystemtype,
&mountflags, &data))
return NULL;
sts = mount(source, target, filesystemtype, mountflags, data);
if (sts == -1)
{
PyErr_SetString(MountError, strerror(errno));
return NULL;
}
return Py_BuildValue("");
}
static PyObject *
mount_umount(PyObject *self, PyObject *args)
{
const char *target;
int sts;
if (!PyArg_ParseTuple(args, "s", &target))
return NULL;
sts = umount(target);
if (sts == -1)
{
PyErr_SetString(MountError, strerror(errno));
return NULL;
}
return Py_BuildValue("");
}
static PyObject *
mount_umount2(PyObject *self, PyObject *args)
{
const char *target;
int flags;
int sts;
if (!PyArg_ParseTuple(args, "si", &target, &flags))
return NULL;
sts = umount2(target, flags);
if (sts == -1)
{
PyErr_SetString(MountError, strerror(errno));
return NULL;
}
return Py_BuildValue("");
}
static PyMethodDef MountMethods[] = {
{"mount", mount_mount, METH_VARARGS, "Mount a filesystem"},
{"umount", mount_umount, METH_VARARGS, "Umount a filesystem"},
{"umount2", mount_umount2, METH_VARARGS, "Umount a filesystem"},
{NULL, NULL, 0, NULL}
};
PyMODINIT_FUNC
initmount(void)
{
PyObject *m;
m = Py_InitModule("mount", MountMethods);
if (m == NULL)
return;
MountError = PyErr_NewException("mount.MountError", NULL, NULL);
Py_INCREF(MountError);
PyModule_AddObject(m, "MountError", MountError);
/* mount flags */
PyModule_AddIntConstant(m, "MS_RDONLY", MS_RDONLY);
PyModule_AddIntConstant(m, "MS_NOSUID", MS_NOSUID);
PyModule_AddIntConstant(m, "MS_NODEV", MS_NODEV);
PyModule_AddIntConstant(m, "MS_NOEXEC", MS_NOEXEC);
PyModule_AddIntConstant(m, "MS_SYNCHRONOUS", MS_SYNCHRONOUS);
PyModule_AddIntConstant(m, "MS_REMOUNT", MS_REMOUNT);
PyModule_AddIntConstant(m, "MS_MANDLOCK", MS_MANDLOCK);
PyModule_AddIntConstant(m, "MS_NOATIME", MS_NOATIME);
PyModule_AddIntConstant(m, "MS_NODIRATIME", MS_NODIRATIME);
PyModule_AddIntConstant(m, "MS_BIND", MS_BIND);
PyModule_AddIntConstant(m, "MS_RMT_MASK", MS_RMT_MASK);
/* mount flags from linux/fs.h */
PyModule_AddIntConstant(m, "MS_SILENT", MS_SILENT);
PyModule_AddIntConstant(m, "MS_STRICTATIME", MS_STRICTATIME);
PyModule_AddIntConstant(m, "MS_DIRSYNC", MS_DIRSYNC);
PyModule_AddIntConstant(m, "MS_MOVE", MS_MOVE);
PyModule_AddIntConstant(m, "MS_RELATIME", MS_RELATIME);
/* the magic numbers */
PyModule_AddIntConstant(m, "MS_MGC_VAL", MS_MGC_VAL);
PyModule_AddIntConstant(m, "MS_MGC_MSK", MS_MGC_MSK);
/* flags for umount2 */
PyModule_AddIntConstant(m, "MNT_FORCE", MNT_FORCE);
PyModule_AddIntConstant(m, "MNT_DETACH", MNT_DETACH);
PyModule_AddIntConstant(m, "MNT_EXPIRE", MNT_EXPIRE);
}