s3:smb2_create: don't take 'state->te' as indication for "was_deferred" (bug #9196)
[Samba.git] / source3 / libsmb / pylibsmb.c
blobd8e64b35682c6fd2e6a96e46ffb3f84e310573ff
1 /*
2 * Unix SMB/CIFS implementation.
3 * Samba-internal work in progress Python binding for libsmbclient
5 * Copyright (C) Volker Lendecke 2012
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <Python.h>
22 #include "includes.h"
23 #include "libsmb/libsmb.h"
24 #include "libcli/security/security.h"
25 #include "system/select.h"
26 #include "source4/libcli/util/pyerrors.h"
27 #include "auth/credentials/pycredentials.h"
29 static PyTypeObject *get_pytype(const char *module, const char *type)
31 PyObject *mod;
32 PyTypeObject *result;
34 mod = PyImport_ImportModule(module);
35 if (mod == NULL) {
36 PyErr_Format(PyExc_RuntimeError,
37 "Unable to import %s to check type %s",
38 module, type);
39 return NULL;
41 result = (PyTypeObject *)PyObject_GetAttrString(mod, type);
42 Py_DECREF(mod);
43 if (result == NULL) {
44 PyErr_Format(PyExc_RuntimeError,
45 "Unable to find type %s in module %s",
46 module, type);
47 return NULL;
49 return result;
52 struct py_cli_thread;
54 struct py_cli_state {
55 PyObject_HEAD
56 struct cli_state *cli;
57 struct tevent_context *ev;
58 struct py_cli_thread *thread_state;
61 #if HAVE_PTHREAD
63 #include <pthread.h>
65 struct py_cli_thread {
68 * Pipe to make the poll thread wake up in our destructor, so
69 * that we can exit and join the thread.
71 int shutdown_pipe[2];
72 struct tevent_fd *shutdown_fde;
73 bool do_shutdown;
74 pthread_t id;
77 * Thread state to release the GIL during the poll(2) syscall
79 PyThreadState *py_threadstate;
82 static void *py_cli_state_poll_thread(void *private_data)
84 struct py_cli_state *self = (struct py_cli_state *)private_data;
85 struct py_cli_thread *t = self->thread_state;
86 PyGILState_STATE gstate;
88 gstate = PyGILState_Ensure();
90 while (!t->do_shutdown) {
91 int ret;
92 ret = tevent_loop_once(self->ev);
93 assert(ret == 0);
95 PyGILState_Release(gstate);
96 return NULL;
99 static void py_cli_state_trace_callback(enum tevent_trace_point point,
100 void *private_data)
102 struct py_cli_state *self = (struct py_cli_state *)private_data;
103 struct py_cli_thread *t = self->thread_state;
105 switch(point) {
106 case TEVENT_TRACE_BEFORE_WAIT:
107 assert(t->py_threadstate == NULL);
108 t->py_threadstate = PyEval_SaveThread();
109 break;
110 case TEVENT_TRACE_AFTER_WAIT:
111 assert(t->py_threadstate != NULL);
112 PyEval_RestoreThread(t->py_threadstate);
113 t->py_threadstate = NULL;
114 break;
115 default:
116 break;
120 static void py_cli_state_shutdown_handler(struct tevent_context *ev,
121 struct tevent_fd *fde,
122 uint16_t flags,
123 void *private_data)
125 struct py_cli_state *self = (struct py_cli_state *)private_data;
126 struct py_cli_thread *t = self->thread_state;
128 if ((flags & TEVENT_FD_READ) == 0) {
129 return;
131 TALLOC_FREE(t->shutdown_fde);
132 t->do_shutdown = true;
135 static int py_cli_thread_destructor(struct py_cli_thread *t)
137 char c = 0;
138 ssize_t written;
139 int ret;
141 do {
143 * This will wake the poll thread from the poll(2)
145 written = write(t->shutdown_pipe[1], &c, 1);
146 } while ((written == -1) && (errno == EINTR));
149 * Allow the poll thread to do its own cleanup under the GIL
151 Py_BEGIN_ALLOW_THREADS
152 ret = pthread_join(t->id, NULL);
153 Py_END_ALLOW_THREADS
154 assert(ret == 0);
156 if (t->shutdown_pipe[0] != -1) {
157 close(t->shutdown_pipe[0]);
158 t->shutdown_pipe[0] = -1;
160 if (t->shutdown_pipe[1] != -1) {
161 close(t->shutdown_pipe[1]);
162 t->shutdown_pipe[1] = -1;
164 return 0;
167 static bool py_cli_state_setup_ev(struct py_cli_state *self)
169 struct py_cli_thread *t;
170 int ret;
172 self->ev = tevent_context_init_byname(NULL, "poll_mt");
173 if (self->ev == NULL) {
174 goto fail;
176 tevent_set_trace_callback(self->ev, py_cli_state_trace_callback, self);
178 self->thread_state = talloc_zero(NULL, struct py_cli_thread);
179 if (self->thread_state == NULL) {
180 goto fail;
182 t = self->thread_state;
184 ret = pipe(t->shutdown_pipe);
185 if (ret == -1) {
186 goto fail;
188 t->shutdown_fde = tevent_add_fd(
189 self->ev, self->ev, t->shutdown_pipe[0], TEVENT_FD_READ,
190 py_cli_state_shutdown_handler, self);
191 if (t->shutdown_fde == NULL) {
192 goto fail;
195 PyEval_InitThreads();
197 ret = pthread_create(&t->id, NULL, py_cli_state_poll_thread, self);
198 if (ret != 0) {
199 goto fail;
201 talloc_set_destructor(self->thread_state, py_cli_thread_destructor);
202 return true;
204 fail:
205 TALLOC_FREE(t->shutdown_fde);
207 if (t->shutdown_pipe[0] != -1) {
208 close(t->shutdown_pipe[0]);
209 t->shutdown_pipe[0] = -1;
211 if (t->shutdown_pipe[1] != -1) {
212 close(t->shutdown_pipe[1]);
213 t->shutdown_pipe[1] = -1;
216 TALLOC_FREE(self->thread_state);
217 TALLOC_FREE(self->ev);
218 return false;
221 struct py_tevent_cond {
222 pthread_mutex_t mutex;
223 pthread_cond_t cond;
224 bool is_done;
227 static void py_tevent_signalme(struct tevent_req *req);
229 static int py_tevent_req_wait(struct tevent_context *ev,
230 struct tevent_req *req)
232 struct py_tevent_cond cond;
233 int ret, result;
235 result = pthread_mutex_init(&cond.mutex, NULL);
236 if (result != 0) {
237 goto fail;
239 result = pthread_cond_init(&cond.cond, NULL);
240 if (result != 0) {
241 goto fail_mutex;
244 cond.is_done = false;
245 tevent_req_set_callback(req, py_tevent_signalme, &cond);
247 result = pthread_mutex_lock(&cond.mutex);
248 if (result != 0) {
249 goto fail_cond;
252 while (!cond.is_done) {
254 Py_BEGIN_ALLOW_THREADS
255 result = pthread_cond_wait(&cond.cond, &cond.mutex);
256 Py_END_ALLOW_THREADS
258 if (result != 0) {
259 goto fail_unlock;
263 fail_unlock:
264 ret = pthread_mutex_unlock(&cond.mutex);
265 assert(ret == 0);
266 fail_cond:
267 ret = pthread_cond_destroy(&cond.cond);
268 assert(ret == 0);
269 fail_mutex:
270 ret = pthread_mutex_destroy(&cond.mutex);
271 assert(ret == 0);
272 fail:
273 return result;
276 static void py_tevent_signalme(struct tevent_req *req)
278 struct py_tevent_cond *cond = (struct py_tevent_cond *)
279 tevent_req_callback_data_void(req);
280 int ret;
282 ret = pthread_mutex_lock(&cond->mutex);
283 assert(ret == 0);
285 cond->is_done = true;
287 ret = pthread_cond_signal(&cond->cond);
288 assert(ret == 0);
289 ret = pthread_mutex_unlock(&cond->mutex);
290 assert(ret == 0);
293 #else
295 static bool py_cli_state_setup_ev(struct py_cli_state *self)
297 self->ev = tevent_context_init(NULL);
298 return (self->ev != NULL);
301 static int py_tevent_req_wait(struct tevent_context *ev,
302 struct tevent_req *req)
304 while (tevent_req_is_in_progress(req)) {
305 int ret;
307 ret = tevent_loop_once(ev);
308 if (ret != 0) {
309 return ret;
312 return 0;
315 #endif
317 static PyObject *py_cli_state_new(PyTypeObject *type, PyObject *args,
318 PyObject *kwds)
320 struct py_cli_state *self;
322 self = (struct py_cli_state *)type->tp_alloc(type, 0);
323 if (self == NULL) {
324 return NULL;
326 self->cli = NULL;
327 self->ev = NULL;
328 self->thread_state = NULL;
329 return (PyObject *)self;
332 static int py_cli_state_init(struct py_cli_state *self, PyObject *args,
333 PyObject *kwds)
335 NTSTATUS status;
336 char *host, *share;
337 PyObject *creds;
338 struct cli_credentials *cli_creds;
339 bool ret;
341 static const char *kwlist[] = {
342 "host", "share", "credentials", NULL
345 PyTypeObject *py_type_Credentials = get_pytype(
346 "samba.credentials", "Credentials");
347 if (py_type_Credentials == NULL) {
348 return -1;
351 ret = PyArg_ParseTupleAndKeywords(
352 args, kwds, "ss|O!", (char **)kwlist,
353 &host, &share, py_type_Credentials, &creds);
355 Py_DECREF(py_type_Credentials);
357 if (!ret) {
358 return -1;
361 if (!py_cli_state_setup_ev(self)) {
362 return -1;
365 cli_creds = cli_credentials_from_py_object(creds);
366 if (cli_creds == NULL) {
367 PyErr_SetString(PyExc_TypeError, "Expected credentials");
368 return -1;
371 status = cli_full_connection(
372 &self->cli, "myname", host, NULL, 0, share, "?????",
373 cli_credentials_get_username(cli_creds),
374 cli_credentials_get_domain(cli_creds),
375 cli_credentials_get_password(cli_creds),
376 0, 0);
377 if (!NT_STATUS_IS_OK(status)) {
378 PyErr_SetNTSTATUS(status);
379 return -1;
381 return 0;
384 static void py_cli_state_dealloc(struct py_cli_state *self)
386 TALLOC_FREE(self->thread_state);
387 TALLOC_FREE(self->ev);
389 if (self->cli != NULL) {
390 cli_shutdown(self->cli);
391 self->cli = NULL;
393 self->ob_type->tp_free((PyObject *)self);
396 static bool py_tevent_req_wait_exc(struct tevent_context *ev,
397 struct tevent_req *req)
399 int ret;
401 if (req == NULL) {
402 PyErr_NoMemory();
403 return false;
405 ret = py_tevent_req_wait(ev, req);
406 if (ret != 0) {
407 TALLOC_FREE(req);
408 errno = ret;
409 PyErr_SetFromErrno(PyExc_RuntimeError);
410 return false;
412 return true;
415 static PyObject *py_cli_create(struct py_cli_state *self, PyObject *args,
416 PyObject *kwds)
418 char *fname;
419 unsigned CreateFlags = 0;
420 unsigned DesiredAccess = FILE_GENERIC_READ;
421 unsigned FileAttributes = 0;
422 unsigned ShareAccess = 0;
423 unsigned CreateDisposition = FILE_OPEN;
424 unsigned CreateOptions = 0;
425 unsigned SecurityFlags = 0;
426 uint16_t fnum;
427 struct tevent_req *req;
428 NTSTATUS status;
430 static const char *kwlist[] = {
431 "Name", "CreateFlags", "DesiredAccess", "FileAttributes",
432 "ShareAccess", "CreateDisposition", "CreateOptions",
433 "SecurityFlags", NULL };
435 if (!PyArg_ParseTupleAndKeywords(
436 args, kwds, "s|IIIIIII", (char **)kwlist,
437 &fname, &CreateFlags, &DesiredAccess, &FileAttributes,
438 &ShareAccess, &CreateDisposition, &CreateOptions,
439 &SecurityFlags)) {
440 return NULL;
443 req = cli_ntcreate_send(NULL, self->ev, self->cli, fname, CreateFlags,
444 DesiredAccess, FileAttributes, ShareAccess,
445 CreateDisposition, CreateOptions,
446 SecurityFlags);
447 if (!py_tevent_req_wait_exc(self->ev, req)) {
448 return NULL;
450 status = cli_ntcreate_recv(req, &fnum);
451 TALLOC_FREE(req);
453 if (!NT_STATUS_IS_OK(status)) {
454 PyErr_SetNTSTATUS(status);
455 return NULL;
457 return Py_BuildValue("I", (unsigned)fnum);
460 static PyObject *py_cli_close(struct py_cli_state *self, PyObject *args)
462 struct tevent_req *req;
463 int fnum;
464 NTSTATUS status;
466 if (!PyArg_ParseTuple(args, "i", &fnum)) {
467 return NULL;
470 req = cli_close_send(NULL, self->ev, self->cli, fnum);
471 if (!py_tevent_req_wait_exc(self->ev, req)) {
472 return NULL;
474 status = cli_close_recv(req);
475 TALLOC_FREE(req);
477 if (!NT_STATUS_IS_OK(status)) {
478 PyErr_SetNTSTATUS(status);
479 return NULL;
481 Py_INCREF(Py_None);
482 return Py_None;
485 static PyObject *py_cli_write(struct py_cli_state *self, PyObject *args,
486 PyObject *kwds)
488 int fnum;
489 unsigned mode = 0;
490 char *buf;
491 int buflen;
492 unsigned long long offset;
493 struct tevent_req *req;
494 NTSTATUS status;
495 size_t written;
497 static const char *kwlist[] = {
498 "fnum", "buffer", "offset", "mode", NULL };
500 if (!PyArg_ParseTupleAndKeywords(
501 args, kwds, "Is#K|I", (char **)kwlist,
502 &fnum, &buf, &buflen, &offset, &mode)) {
503 return NULL;
506 req = cli_write_andx_send(NULL, self->ev, self->cli, fnum, mode,
507 (uint8_t *)buf, offset, buflen);
508 if (!py_tevent_req_wait_exc(self->ev, req)) {
509 return NULL;
511 status = cli_write_andx_recv(req, &written);
512 TALLOC_FREE(req);
514 if (!NT_STATUS_IS_OK(status)) {
515 PyErr_SetNTSTATUS(status);
516 return NULL;
518 return Py_BuildValue("K", (unsigned long long)written);
521 static PyObject *py_cli_read(struct py_cli_state *self, PyObject *args,
522 PyObject *kwds)
524 int fnum;
525 unsigned long long offset;
526 unsigned size;
527 struct tevent_req *req;
528 NTSTATUS status;
529 uint8_t *buf;
530 ssize_t buflen;
531 PyObject *result;
533 static const char *kwlist[] = {
534 "fnum", "offset", "size", NULL };
536 if (!PyArg_ParseTupleAndKeywords(
537 args, kwds, "IKI", (char **)kwlist, &fnum, &offset,
538 &size)) {
539 return NULL;
542 req = cli_read_andx_send(NULL, self->ev, self->cli, fnum,
543 offset, size);
544 if (!py_tevent_req_wait_exc(self->ev, req)) {
545 return NULL;
547 status = cli_read_andx_recv(req, &buflen, &buf);
549 if (!NT_STATUS_IS_OK(status)) {
550 TALLOC_FREE(req);
551 PyErr_SetNTSTATUS(status);
552 return NULL;
554 result = Py_BuildValue("s#", (char *)buf, (int)buflen);
555 TALLOC_FREE(req);
556 return result;
559 static PyObject *py_cli_ftruncate(struct py_cli_state *self, PyObject *args,
560 PyObject *kwds)
562 int fnum;
563 unsigned long long size;
564 struct tevent_req *req;
565 NTSTATUS status;
567 static const char *kwlist[] = {
568 "fnum", "size", NULL };
570 if (!PyArg_ParseTupleAndKeywords(
571 args, kwds, "IK", (char **)kwlist, &fnum, &size)) {
572 return NULL;
575 req = cli_ftruncate_send(NULL, self->ev, self->cli, fnum, size);
576 if (!py_tevent_req_wait_exc(self->ev, req)) {
577 return NULL;
579 status = cli_ftruncate_recv(req);
580 TALLOC_FREE(req);
582 if (!NT_STATUS_IS_OK(status)) {
583 PyErr_SetNTSTATUS(status);
584 return NULL;
586 Py_INCREF(Py_None);
587 return Py_None;
590 static PyObject *py_cli_delete_on_close(struct py_cli_state *self,
591 PyObject *args,
592 PyObject *kwds)
594 unsigned fnum, flag;
595 struct tevent_req *req;
596 NTSTATUS status;
598 static const char *kwlist[] = {
599 "fnum", "flag", NULL };
601 if (!PyArg_ParseTupleAndKeywords(
602 args, kwds, "II", (char **)kwlist, &fnum, &flag)) {
603 return NULL;
606 req = cli_nt_delete_on_close_send(NULL, self->ev, self->cli, fnum,
607 flag);
608 if (!py_tevent_req_wait_exc(self->ev, req)) {
609 return NULL;
611 status = cli_nt_delete_on_close_recv(req);
612 TALLOC_FREE(req);
614 if (!NT_STATUS_IS_OK(status)) {
615 PyErr_SetNTSTATUS(status);
616 return NULL;
618 Py_INCREF(Py_None);
619 return Py_None;
622 static PyMethodDef py_cli_state_methods[] = {
623 { "create", (PyCFunction)py_cli_create, METH_VARARGS|METH_KEYWORDS,
624 "Open a file" },
625 { "close", (PyCFunction)py_cli_close, METH_VARARGS,
626 "Close a file handle" },
627 { "write", (PyCFunction)py_cli_write, METH_VARARGS|METH_KEYWORDS,
628 "Write to a file handle" },
629 { "read", (PyCFunction)py_cli_read, METH_VARARGS|METH_KEYWORDS,
630 "Read from a file handle" },
631 { "truncate", (PyCFunction)py_cli_ftruncate,
632 METH_VARARGS|METH_KEYWORDS,
633 "Truncate a file" },
634 { "delete_on_close", (PyCFunction)py_cli_delete_on_close,
635 METH_VARARGS|METH_KEYWORDS,
636 "Set/Reset the delete on close flag" },
637 { NULL, NULL, 0, NULL }
640 static PyTypeObject py_cli_state_type = {
641 PyObject_HEAD_INIT(NULL)
642 .tp_name = "libsmb_samba_internal.Conn",
643 .tp_basicsize = sizeof(struct py_cli_state),
644 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
645 .tp_doc = "libsmb connection",
646 .tp_new = py_cli_state_new,
647 .tp_init = (initproc)py_cli_state_init,
648 .tp_dealloc = (destructor)py_cli_state_dealloc,
649 .tp_methods = py_cli_state_methods,
652 static PyMethodDef py_libsmb_methods[] = {
653 { NULL },
656 void initlibsmb_samba_internal(void);
657 void initlibsmb_samba_internal(void)
659 PyObject *m;
661 talloc_stackframe();
663 m = Py_InitModule3("libsmb_samba_internal", py_libsmb_methods,
664 "libsmb wrapper");
666 if (PyType_Ready(&py_cli_state_type) < 0) {
667 return;
669 Py_INCREF(&py_cli_state_type);
670 PyModule_AddObject(m, "Conn", (PyObject *)&py_cli_state_type);