forked from acozzette/BUSE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythonbd.c
More file actions
280 lines (260 loc) · 8.5 KB
/
pythonbd.c
File metadata and controls
280 lines (260 loc) · 8.5 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
* pythonbd - example memory-based block device using BUSE
* Copyright (C) 2016 Geert Audenaert
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Python.h>
#include "buse.h"
static int xmpl_debug = 1;
PyObject *xmp_read_impl, *xmp_write_impl, *xmp_disc_impl, *xmp_flush_impl, *xmp_trim_impl, *xmp_size_impl;
int xmp_read_set = 0;
int xmp_write_set = 0;
int xmp_disc_set = 0;
int xmp_flush_set = 0;
int xmp_trim_set = 0;
int xmp_size_set = 0;
static int xmp_read(void *buf, u_int32_t len, u_int64_t offset, void *userdata)
{
if (*(int *)userdata)
fprintf(stderr, "R - %lu, %u\n", offset, len);
// def read(length, offset):
// return 'dddd'.encode() # Needs to return a bytes object
PyObject *pArgs = PyTuple_New(2);
PyTuple_SetItem(pArgs, 0, PyLong_FromUnsignedLong(len));
PyTuple_SetItem(pArgs, 1, PyLong_FromUnsignedLongLong(offset));
PyObject *pValue = PyObject_CallObject(xmp_read_impl, pArgs);
Py_DECREF(pArgs);
if ( pValue == NULL ) {
fprintf(stderr, "R Python implementation raised an exception.\n");
PyErr_Print();
return 1;
}
if ( ! PyBytes_Check(pValue) ) {
fprintf(stderr, "R Python implementation did not return a bytes object.\n");
Py_DECREF(pValue);
return 1;
}
if ( PyBytes_Size(pValue) != len ) {
fprintf(stderr, "R Python implementation did not return expected amount (%u) of bytes.\n", len);
Py_DECREF(pValue);
return 1;
}
char *data_returned = PyBytes_AsString(pValue);
memcpy(buf, data_returned, len);
Py_DECREF(pValue);
return 0;
}
static int xmp_write(const void *buf, u_int32_t len, u_int64_t offset, void *userdata)
{
if (*(int *)userdata)
fprintf(stderr, "W - %lu, %u\n", offset, len);
// def write(data, offset):
// pass
PyObject *buf_bytes = PyBytes_FromStringAndSize(buf, len);
if ( buf_bytes == NULL ) {
fprintf(stderr, "W Failed to convert buffer into bytes object.\n");
PyErr_Print();
return 1;
}
PyObject *pArgs = PyTuple_New(2);
PyTuple_SetItem(pArgs, 0, buf_bytes);
PyTuple_SetItem(pArgs, 1, PyLong_FromUnsignedLongLong(offset));
PyObject *pValue = PyObject_CallObject(xmp_write_impl, pArgs);
Py_DECREF(pArgs);
Py_DECREF(buf_bytes);
if ( pValue == NULL ) {
fprintf(stderr, "W Python implementation raised an exception.\n");
PyErr_Print();
return 1;
}
Py_DECREF(pValue);
return 0;
}
static void xmp_disc(void *userdata)
{
(void)(userdata);
fprintf(stderr, "Received a disconnect request.\n");
// def disc():
// pass
PyObject *pArgs = PyTuple_New(0);
PyObject *pValue = PyObject_CallObject(xmp_disc_impl, pArgs);
Py_DECREF(pArgs);
if ( pValue == NULL ) {
fprintf(stderr, "D Python implementation raised an exception.\n");
PyErr_Print();
}
Py_DECREF(pValue);
}
static int xmp_flush(void *userdata)
{
(void)(userdata);
fprintf(stderr, "Received a flush request.\n");
// def flush():
// pass
PyObject *pArgs = PyTuple_New(0);
PyObject *pValue = PyObject_CallObject(xmp_flush_impl, pArgs);
Py_DECREF(pArgs);
if ( pValue == NULL ) {
fprintf(stderr, "F Python implementation raised an exception.\n");
PyErr_Print();
return 1;
}
Py_DECREF(pValue);
return 0;
}
static int xmp_trim(u_int64_t from, u_int32_t len, void *userdata)
{
(void)(userdata);
fprintf(stderr, "T - %lu, %u\n", from, len);
// def trim(offset, length):
// pass
PyObject *pArgs = PyTuple_New(2);
PyTuple_SetItem(pArgs, 0, PyLong_FromUnsignedLongLong(from));
PyTuple_SetItem(pArgs, 1, PyLong_FromUnsignedLong(len));
PyObject *pValue = PyObject_CallObject(xmp_trim_impl, pArgs);
Py_DECREF(pArgs);
if ( pValue == NULL ) {
fprintf(stderr, "T Python implementation raised an exception.\n");
PyErr_Print();
return 1;
}
Py_DECREF(pValue);
return 0;
}
static struct buse_operations get_buse_operations(void)
{
struct buse_operations aop = {
.read = xmp_read,
.write = xmp_write,
.disc = xmp_disc,
.flush = xmp_flush,
.trim = xmp_trim,
};
// def size():
// pass
PyObject *pArgs = PyTuple_New(0);
PyObject *pValue = PyObject_CallObject(xmp_size_impl, pArgs);
Py_DECREF(pArgs);
if ( pValue == NULL ) {
fprintf(stderr, "S Python implementation raised an exception.\n");
PyErr_Print();
return aop;
}
if ( PyLong_Check(pValue) != 0 ) {
u_int64_t size = PyLong_AsUnsignedLongLong(pValue);
Py_DECREF(pValue);
aop.size = size;
return aop;
} else {
fprintf(stderr, "S Python implementation did not return an integer.\n");
Py_DECREF(pValue);
return aop;
}
}
int main(int argc, char *argv[])
{
if (argc != 3)
{
fprintf(stderr,
"Usage:\n"
" %s /dev/nbd0\n"
"Don't forget to load nbd kernel module (`modprobe nbd`) and\n"
"run example from root.\n", argv[0]);
return 1;
}
PyObject *pName, *pModule;
Py_Initialize();
PyRun_SimpleString("import sys;sys.path.append('.')\n");
pName = PyUnicode_DecodeFSDefault(argv[2]);
pModule = PyImport_Import(pName);
Py_DECREF(pName);
if (pModule == NULL) {
PyErr_Print();
fprintf(stderr, "Failed to load \"%s\"\n", argv[2]);
Py_Finalize();
return 1;
}
PyObject *list = PyObject_Dir(pModule);
for (int i=0,max=PyList_Size(list); i<max; i++) {
PyObject *attr_name = PyList_GetItem(list, i);
PyObject *attr_value = PyObject_GetAttr(pModule, attr_name);
int match = 0;
if (PyCallable_Check(attr_value) != 0) {
if ( PyUnicode_CompareWithASCIIString(attr_name, "read") == 0 ) {
xmp_read_impl = attr_value;
xmp_read_set = 1;
match = 1;
} else if ( PyUnicode_CompareWithASCIIString(attr_name, "write") == 0 ) {
xmp_write_impl = attr_value;
xmp_write_set = 1;
match = 1;
} else if ( PyUnicode_CompareWithASCIIString(attr_name, "disc") == 0 ) {
xmp_disc_impl = attr_value;
xmp_disc_set = 1;
match = 1;
} else if ( PyUnicode_CompareWithASCIIString(attr_name, "flush") == 0 ) {
xmp_flush_impl = attr_value;
xmp_flush_set = 1;
match = 1;
} else if ( PyUnicode_CompareWithASCIIString(attr_name, "trim") == 0 ) {
xmp_trim_impl = attr_value;
xmp_trim_set = 1;
match = 1;
} else if ( PyUnicode_CompareWithASCIIString(attr_name, "size") == 0 ) {
xmp_size_impl = attr_value;
xmp_size_set = 1;
match = 1;
}
}
if ( match == 0 ) {
Py_DECREF(attr_name);
if ( attr_value != NULL ) Py_DECREF(attr_value);
}
}
Py_DECREF(list);
if ( ! ( xmp_read_set == 1 && xmp_write_set == 1 &&
xmp_disc_set == 1 && xmp_flush_set == 1 &&
xmp_trim_set == 1 && xmp_size_set ) ) {
if ( xmp_read_set == 1) Py_DECREF(xmp_read_impl); else fprintf(stderr, "read function not found\n");
if ( xmp_write_set == 1 ) Py_DECREF(xmp_write_impl); else fprintf(stderr, "write function not found\n");
if ( xmp_disc_set == 1 ) Py_DECREF(xmp_disc_impl); else fprintf(stderr, "disc function not found\n");
if ( xmp_flush_set == 1 ) Py_DECREF(xmp_flush_impl); else fprintf(stderr, "flush function not found\n");
if ( xmp_trim_set == 1 ) Py_DECREF(xmp_trim_impl); else fprintf(stderr, "trim function not found\n");
if ( xmp_size_set == 1 ) Py_DECREF(xmp_size_impl); else fprintf(stderr, "size function not found\n");
fprintf(stderr, "Not all required functions are present in \"%s\"\n", argv[2]);
Py_DECREF(pModule);
Py_Finalize();
return 1;
}
struct buse_operations aop = get_buse_operations();
int retval = 0;
if ( aop.size != 0) {
retval = buse_main(argv[1], &aop, (void *)&xmpl_debug);
} else
retval = 1;
Py_DECREF(xmp_read_impl);
Py_DECREF(xmp_write_impl);
Py_DECREF(xmp_disc_impl);
Py_DECREF(xmp_flush_impl);
Py_DECREF(xmp_trim_impl);
Py_DECREF(xmp_size_impl);
Py_DECREF(pModule);
Py_Finalize();
return retval;
}