s3-pylibsmb: Factor out py_tevent_cond_signal
[Samba/gebeck_regimport.git] / source3 / libsmb / pylibsmb.c
blob778df0853020e27dc635727a4c95e6e003bdc3b1
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"
28 #include "trans2.h"
30 static PyTypeObject *get_pytype(const char *module, const char *type)
32 PyObject *mod;
33 PyTypeObject *result;
35 mod = PyImport_ImportModule(module);
36 if (mod == NULL) {
37 PyErr_Format(PyExc_RuntimeError,
38 "Unable to import %s to check type %s",
39 module, type);
40 return NULL;
42 result = (PyTypeObject *)PyObject_GetAttrString(mod, type);
43 Py_DECREF(mod);
44 if (result == NULL) {
45 PyErr_Format(PyExc_RuntimeError,
46 "Unable to find type %s in module %s",
47 module, type);
48 return NULL;
50 return result;
54 * We're using "const char **" for keywords,
55 * PyArg_ParseTupleAndKeywords expects a "char **". Confine the
56 * inevitable warnings to just one place.
58 static int ParseTupleAndKeywords(PyObject *args, PyObject *kw,
59 const char *format, const char **keywords,
60 ...)
62 va_list a;
63 int ret;
64 va_start(a, keywords);
65 ret = PyArg_VaParseTupleAndKeywords(args, kw, format,
66 (char **)keywords, a);
67 va_end(a);
68 return ret;
71 struct py_cli_thread;
73 struct py_cli_state {
74 PyObject_HEAD
75 struct cli_state *cli;
76 struct tevent_context *ev;
77 struct py_cli_thread *thread_state;
80 #if HAVE_PTHREAD
82 #include <pthread.h>
84 struct py_cli_thread {
87 * Pipe to make the poll thread wake up in our destructor, so
88 * that we can exit and join the thread.
90 int shutdown_pipe[2];
91 struct tevent_fd *shutdown_fde;
92 bool do_shutdown;
93 pthread_t id;
96 * Thread state to release the GIL during the poll(2) syscall
98 PyThreadState *py_threadstate;
101 static void *py_cli_state_poll_thread(void *private_data)
103 struct py_cli_state *self = (struct py_cli_state *)private_data;
104 struct py_cli_thread *t = self->thread_state;
105 PyGILState_STATE gstate;
107 gstate = PyGILState_Ensure();
109 while (!t->do_shutdown) {
110 int ret;
111 ret = tevent_loop_once(self->ev);
112 assert(ret == 0);
114 PyGILState_Release(gstate);
115 return NULL;
118 static void py_cli_state_trace_callback(enum tevent_trace_point point,
119 void *private_data)
121 struct py_cli_state *self = (struct py_cli_state *)private_data;
122 struct py_cli_thread *t = self->thread_state;
124 switch(point) {
125 case TEVENT_TRACE_BEFORE_WAIT:
126 assert(t->py_threadstate == NULL);
127 t->py_threadstate = PyEval_SaveThread();
128 break;
129 case TEVENT_TRACE_AFTER_WAIT:
130 assert(t->py_threadstate != NULL);
131 PyEval_RestoreThread(t->py_threadstate);
132 t->py_threadstate = NULL;
133 break;
134 default:
135 break;
139 static void py_cli_state_shutdown_handler(struct tevent_context *ev,
140 struct tevent_fd *fde,
141 uint16_t flags,
142 void *private_data)
144 struct py_cli_state *self = (struct py_cli_state *)private_data;
145 struct py_cli_thread *t = self->thread_state;
147 if ((flags & TEVENT_FD_READ) == 0) {
148 return;
150 TALLOC_FREE(t->shutdown_fde);
151 t->do_shutdown = true;
154 static int py_cli_thread_destructor(struct py_cli_thread *t)
156 char c = 0;
157 ssize_t written;
158 int ret;
160 do {
162 * This will wake the poll thread from the poll(2)
164 written = write(t->shutdown_pipe[1], &c, 1);
165 } while ((written == -1) && (errno == EINTR));
168 * Allow the poll thread to do its own cleanup under the GIL
170 Py_BEGIN_ALLOW_THREADS
171 ret = pthread_join(t->id, NULL);
172 Py_END_ALLOW_THREADS
173 assert(ret == 0);
175 if (t->shutdown_pipe[0] != -1) {
176 close(t->shutdown_pipe[0]);
177 t->shutdown_pipe[0] = -1;
179 if (t->shutdown_pipe[1] != -1) {
180 close(t->shutdown_pipe[1]);
181 t->shutdown_pipe[1] = -1;
183 return 0;
186 static bool py_cli_state_setup_ev(struct py_cli_state *self)
188 struct py_cli_thread *t = NULL;
189 int ret;
191 self->ev = tevent_context_init_byname(NULL, "poll_mt");
192 if (self->ev == NULL) {
193 goto fail;
195 tevent_set_trace_callback(self->ev, py_cli_state_trace_callback, self);
197 self->thread_state = talloc_zero(NULL, struct py_cli_thread);
198 if (self->thread_state == NULL) {
199 goto fail;
201 t = self->thread_state;
203 ret = pipe(t->shutdown_pipe);
204 if (ret == -1) {
205 goto fail;
207 t->shutdown_fde = tevent_add_fd(
208 self->ev, self->ev, t->shutdown_pipe[0], TEVENT_FD_READ,
209 py_cli_state_shutdown_handler, self);
210 if (t->shutdown_fde == NULL) {
211 goto fail;
214 PyEval_InitThreads();
216 ret = pthread_create(&t->id, NULL, py_cli_state_poll_thread, self);
217 if (ret != 0) {
218 goto fail;
220 talloc_set_destructor(self->thread_state, py_cli_thread_destructor);
221 return true;
223 fail:
224 if (t != NULL) {
225 TALLOC_FREE(t->shutdown_fde);
227 if (t->shutdown_pipe[0] != -1) {
228 close(t->shutdown_pipe[0]);
229 t->shutdown_pipe[0] = -1;
231 if (t->shutdown_pipe[1] != -1) {
232 close(t->shutdown_pipe[1]);
233 t->shutdown_pipe[1] = -1;
237 TALLOC_FREE(self->thread_state);
238 TALLOC_FREE(self->ev);
239 return false;
242 struct py_tevent_cond {
243 pthread_mutex_t mutex;
244 pthread_cond_t cond;
245 bool is_done;
248 static void py_tevent_signalme(struct tevent_req *req);
250 static int py_tevent_cond_wait(struct py_tevent_cond *cond)
252 int ret, result;
254 result = pthread_mutex_init(&cond->mutex, NULL);
255 if (result != 0) {
256 goto fail;
258 result = pthread_cond_init(&cond->cond, NULL);
259 if (result != 0) {
260 goto fail_mutex;
263 result = pthread_mutex_lock(&cond->mutex);
264 if (result != 0) {
265 goto fail_cond;
268 cond->is_done = false;
270 while (!cond->is_done) {
272 Py_BEGIN_ALLOW_THREADS
273 result = pthread_cond_wait(&cond->cond, &cond->mutex);
274 Py_END_ALLOW_THREADS
276 if (result != 0) {
277 goto fail_unlock;
281 fail_unlock:
282 ret = pthread_mutex_unlock(&cond->mutex);
283 assert(ret == 0);
284 fail_cond:
285 ret = pthread_cond_destroy(&cond->cond);
286 assert(ret == 0);
287 fail_mutex:
288 ret = pthread_mutex_destroy(&cond->mutex);
289 assert(ret == 0);
290 fail:
291 return result;
294 static int py_tevent_req_wait(struct tevent_context *ev,
295 struct tevent_req *req)
297 struct py_tevent_cond cond;
298 tevent_req_set_callback(req, py_tevent_signalme, &cond);
299 return py_tevent_cond_wait(&cond);
302 static void py_tevent_cond_signal(struct py_tevent_cond *cond)
304 int ret;
306 ret = pthread_mutex_lock(&cond->mutex);
307 assert(ret == 0);
309 cond->is_done = true;
311 ret = pthread_cond_signal(&cond->cond);
312 assert(ret == 0);
313 ret = pthread_mutex_unlock(&cond->mutex);
314 assert(ret == 0);
317 static void py_tevent_signalme(struct tevent_req *req)
319 struct py_tevent_cond *cond = (struct py_tevent_cond *)
320 tevent_req_callback_data_void(req);
322 py_tevent_cond_signal(cond);
325 #else
327 static bool py_cli_state_setup_ev(struct py_cli_state *self)
329 self->ev = tevent_context_init(NULL);
330 return (self->ev != NULL);
333 static int py_tevent_req_wait(struct tevent_context *ev,
334 struct tevent_req *req)
336 while (tevent_req_is_in_progress(req)) {
337 int ret;
339 ret = tevent_loop_once(ev);
340 if (ret != 0) {
341 return ret;
344 return 0;
347 #endif
349 static bool py_tevent_req_wait_exc(struct tevent_context *ev,
350 struct tevent_req *req)
352 int ret;
354 if (req == NULL) {
355 PyErr_NoMemory();
356 return false;
358 ret = py_tevent_req_wait(ev, req);
359 if (ret != 0) {
360 TALLOC_FREE(req);
361 errno = ret;
362 PyErr_SetFromErrno(PyExc_RuntimeError);
363 return false;
365 return true;
368 static PyObject *py_cli_state_new(PyTypeObject *type, PyObject *args,
369 PyObject *kwds)
371 struct py_cli_state *self;
373 self = (struct py_cli_state *)type->tp_alloc(type, 0);
374 if (self == NULL) {
375 return NULL;
377 self->cli = NULL;
378 self->ev = NULL;
379 self->thread_state = NULL;
380 return (PyObject *)self;
383 static int py_cli_state_init(struct py_cli_state *self, PyObject *args,
384 PyObject *kwds)
386 NTSTATUS status;
387 char *host, *share;
388 PyObject *creds;
389 struct cli_credentials *cli_creds;
390 bool ret;
392 static const char *kwlist[] = {
393 "host", "share", "credentials", NULL
396 PyTypeObject *py_type_Credentials = get_pytype(
397 "samba.credentials", "Credentials");
398 if (py_type_Credentials == NULL) {
399 return -1;
402 ret = ParseTupleAndKeywords(
403 args, kwds, "ss|O!", kwlist,
404 &host, &share, py_type_Credentials, &creds);
406 Py_DECREF(py_type_Credentials);
408 if (!ret) {
409 return -1;
412 if (!py_cli_state_setup_ev(self)) {
413 return -1;
416 cli_creds = cli_credentials_from_py_object(creds);
417 if (cli_creds == NULL) {
418 PyErr_SetString(PyExc_TypeError, "Expected credentials");
419 return -1;
422 status = cli_full_connection(
423 &self->cli, "myname", host, NULL, 0, share, "?????",
424 cli_credentials_get_username(cli_creds),
425 cli_credentials_get_domain(cli_creds),
426 cli_credentials_get_password(cli_creds),
427 0, 0);
428 if (!NT_STATUS_IS_OK(status)) {
429 PyErr_SetNTSTATUS(status);
430 return -1;
432 return 0;
435 static void py_cli_state_dealloc(struct py_cli_state *self)
437 TALLOC_FREE(self->thread_state);
438 TALLOC_FREE(self->ev);
440 if (self->cli != NULL) {
441 cli_shutdown(self->cli);
442 self->cli = NULL;
444 self->ob_type->tp_free((PyObject *)self);
447 static PyObject *py_cli_create(struct py_cli_state *self, PyObject *args,
448 PyObject *kwds)
450 char *fname;
451 unsigned CreateFlags = 0;
452 unsigned DesiredAccess = FILE_GENERIC_READ;
453 unsigned FileAttributes = 0;
454 unsigned ShareAccess = 0;
455 unsigned CreateDisposition = FILE_OPEN;
456 unsigned CreateOptions = 0;
457 unsigned SecurityFlags = 0;
458 uint16_t fnum;
459 struct tevent_req *req;
460 NTSTATUS status;
462 static const char *kwlist[] = {
463 "Name", "CreateFlags", "DesiredAccess", "FileAttributes",
464 "ShareAccess", "CreateDisposition", "CreateOptions",
465 "SecurityFlags", NULL };
467 if (!ParseTupleAndKeywords(
468 args, kwds, "s|IIIIIII", kwlist,
469 &fname, &CreateFlags, &DesiredAccess, &FileAttributes,
470 &ShareAccess, &CreateDisposition, &CreateOptions,
471 &SecurityFlags)) {
472 return NULL;
475 req = cli_ntcreate_send(NULL, self->ev, self->cli, fname, CreateFlags,
476 DesiredAccess, FileAttributes, ShareAccess,
477 CreateDisposition, CreateOptions,
478 SecurityFlags);
479 if (!py_tevent_req_wait_exc(self->ev, req)) {
480 return NULL;
482 status = cli_ntcreate_recv(req, &fnum);
483 TALLOC_FREE(req);
485 if (!NT_STATUS_IS_OK(status)) {
486 PyErr_SetNTSTATUS(status);
487 return NULL;
489 return Py_BuildValue("I", (unsigned)fnum);
492 static PyObject *py_cli_close(struct py_cli_state *self, PyObject *args)
494 struct tevent_req *req;
495 int fnum;
496 NTSTATUS status;
498 if (!PyArg_ParseTuple(args, "i", &fnum)) {
499 return NULL;
502 req = cli_close_send(NULL, self->ev, self->cli, fnum);
503 if (!py_tevent_req_wait_exc(self->ev, req)) {
504 return NULL;
506 status = cli_close_recv(req);
507 TALLOC_FREE(req);
509 if (!NT_STATUS_IS_OK(status)) {
510 PyErr_SetNTSTATUS(status);
511 return NULL;
513 Py_INCREF(Py_None);
514 return Py_None;
517 static PyObject *py_cli_write(struct py_cli_state *self, PyObject *args,
518 PyObject *kwds)
520 int fnum;
521 unsigned mode = 0;
522 char *buf;
523 int buflen;
524 unsigned long long offset;
525 struct tevent_req *req;
526 NTSTATUS status;
527 size_t written;
529 static const char *kwlist[] = {
530 "fnum", "buffer", "offset", "mode", NULL };
532 if (!ParseTupleAndKeywords(
533 args, kwds, "Is#K|I", kwlist,
534 &fnum, &buf, &buflen, &offset, &mode)) {
535 return NULL;
538 req = cli_write_andx_send(NULL, self->ev, self->cli, fnum, mode,
539 (uint8_t *)buf, offset, buflen);
540 if (!py_tevent_req_wait_exc(self->ev, req)) {
541 return NULL;
543 status = cli_write_andx_recv(req, &written);
544 TALLOC_FREE(req);
546 if (!NT_STATUS_IS_OK(status)) {
547 PyErr_SetNTSTATUS(status);
548 return NULL;
550 return Py_BuildValue("K", (unsigned long long)written);
553 static PyObject *py_cli_read(struct py_cli_state *self, PyObject *args,
554 PyObject *kwds)
556 int fnum;
557 unsigned long long offset;
558 unsigned size;
559 struct tevent_req *req;
560 NTSTATUS status;
561 uint8_t *buf;
562 ssize_t buflen;
563 PyObject *result;
565 static const char *kwlist[] = {
566 "fnum", "offset", "size", NULL };
568 if (!ParseTupleAndKeywords(
569 args, kwds, "IKI", kwlist, &fnum, &offset,
570 &size)) {
571 return NULL;
574 req = cli_read_andx_send(NULL, self->ev, self->cli, fnum,
575 offset, size);
576 if (!py_tevent_req_wait_exc(self->ev, req)) {
577 return NULL;
579 status = cli_read_andx_recv(req, &buflen, &buf);
581 if (!NT_STATUS_IS_OK(status)) {
582 TALLOC_FREE(req);
583 PyErr_SetNTSTATUS(status);
584 return NULL;
586 result = Py_BuildValue("s#", (char *)buf, (int)buflen);
587 TALLOC_FREE(req);
588 return result;
591 static PyObject *py_cli_ftruncate(struct py_cli_state *self, PyObject *args,
592 PyObject *kwds)
594 int fnum;
595 unsigned long long size;
596 struct tevent_req *req;
597 NTSTATUS status;
599 static const char *kwlist[] = {
600 "fnum", "size", NULL };
602 if (!ParseTupleAndKeywords(
603 args, kwds, "IK", kwlist, &fnum, &size)) {
604 return NULL;
607 req = cli_ftruncate_send(NULL, self->ev, self->cli, fnum, size);
608 if (!py_tevent_req_wait_exc(self->ev, req)) {
609 return NULL;
611 status = cli_ftruncate_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 PyObject *py_cli_delete_on_close(struct py_cli_state *self,
623 PyObject *args,
624 PyObject *kwds)
626 unsigned fnum, flag;
627 struct tevent_req *req;
628 NTSTATUS status;
630 static const char *kwlist[] = {
631 "fnum", "flag", NULL };
633 if (!ParseTupleAndKeywords(
634 args, kwds, "II", kwlist, &fnum, &flag)) {
635 return NULL;
638 req = cli_nt_delete_on_close_send(NULL, self->ev, self->cli, fnum,
639 flag);
640 if (!py_tevent_req_wait_exc(self->ev, req)) {
641 return NULL;
643 status = cli_nt_delete_on_close_recv(req);
644 TALLOC_FREE(req);
646 if (!NT_STATUS_IS_OK(status)) {
647 PyErr_SetNTSTATUS(status);
648 return NULL;
650 Py_INCREF(Py_None);
651 return Py_None;
654 static PyObject *py_cli_list(struct py_cli_state *self,
655 PyObject *args,
656 PyObject *kwds)
658 char *mask;
659 unsigned attribute =
660 FILE_ATTRIBUTE_DIRECTORY |
661 FILE_ATTRIBUTE_SYSTEM |
662 FILE_ATTRIBUTE_HIDDEN;
663 unsigned info_level = SMB_FIND_FILE_BOTH_DIRECTORY_INFO;
664 struct tevent_req *req;
665 NTSTATUS status;
666 struct file_info *finfos;
667 size_t i, num_finfos;
668 PyObject *result;
670 const char *kwlist[] = {
671 "mask", "attribute", "info_level", NULL
674 if (!ParseTupleAndKeywords(
675 args, kwds, "s|II", kwlist,
676 &mask, &attribute, &info_level)) {
677 return NULL;
680 req = cli_list_send(NULL, self->ev, self->cli, mask, attribute,
681 info_level);
682 if (!py_tevent_req_wait_exc(self->ev, req)) {
683 return NULL;
685 status = cli_list_recv(req, NULL, &finfos, &num_finfos);
686 TALLOC_FREE(req);
688 if (!NT_STATUS_IS_OK(status)) {
689 PyErr_SetNTSTATUS(status);
690 return NULL;
693 result = Py_BuildValue("[]");
694 if (result == NULL) {
695 return NULL;
698 for (i=0; i<num_finfos; i++) {
699 struct file_info *finfo = &finfos[i];
700 PyObject *file;
701 int ret;
703 file = Py_BuildValue(
704 "{s:s,s:i}",
705 "name", finfo->name,
706 "mode", (int)finfo->mode);
707 if (file == NULL) {
708 Py_XDECREF(result);
709 return NULL;
712 ret = PyList_Append(result, file);
713 if (ret == -1) {
714 Py_XDECREF(result);
715 return NULL;
719 return result;
722 static PyMethodDef py_cli_state_methods[] = {
723 { "create", (PyCFunction)py_cli_create, METH_VARARGS|METH_KEYWORDS,
724 "Open a file" },
725 { "close", (PyCFunction)py_cli_close, METH_VARARGS,
726 "Close a file handle" },
727 { "write", (PyCFunction)py_cli_write, METH_VARARGS|METH_KEYWORDS,
728 "Write to a file handle" },
729 { "read", (PyCFunction)py_cli_read, METH_VARARGS|METH_KEYWORDS,
730 "Read from a file handle" },
731 { "truncate", (PyCFunction)py_cli_ftruncate,
732 METH_VARARGS|METH_KEYWORDS,
733 "Truncate a file" },
734 { "delete_on_close", (PyCFunction)py_cli_delete_on_close,
735 METH_VARARGS|METH_KEYWORDS,
736 "Set/Reset the delete on close flag" },
737 { "readdir", (PyCFunction)py_cli_list,
738 METH_VARARGS|METH_KEYWORDS,
739 "List a directory" },
740 { NULL, NULL, 0, NULL }
743 static PyTypeObject py_cli_state_type = {
744 PyObject_HEAD_INIT(NULL)
745 .tp_name = "libsmb_samba_internal.Conn",
746 .tp_basicsize = sizeof(struct py_cli_state),
747 .tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,
748 .tp_doc = "libsmb connection",
749 .tp_new = py_cli_state_new,
750 .tp_init = (initproc)py_cli_state_init,
751 .tp_dealloc = (destructor)py_cli_state_dealloc,
752 .tp_methods = py_cli_state_methods,
755 static PyMethodDef py_libsmb_methods[] = {
756 { NULL },
759 void initlibsmb_samba_internal(void);
760 void initlibsmb_samba_internal(void)
762 PyObject *m;
764 talloc_stackframe();
766 m = Py_InitModule3("libsmb_samba_internal", py_libsmb_methods,
767 "libsmb wrapper");
769 if (PyType_Ready(&py_cli_state_type) < 0) {
770 return;
772 Py_INCREF(&py_cli_state_type);
773 PyModule_AddObject(m, "Conn", (PyObject *)&py_cli_state_type);