s4:samba_upgradedns: delete dns-HOSTNAME account if using the internal dns server
[Samba/gbeck.git] / lib / tevent / pytevent.c
blob870f5aad6104bccfbde773f9c6b99463a1f8c8e1
1 /*
2 Unix SMB/CIFS implementation.
3 Python bindings for tevent
5 Copyright (C) Jelmer Vernooij 2010
7 ** NOTE! The following LGPL license applies to the tevent
8 ** library. This does NOT imply that all of Samba is released
9 ** under the LGPL
11 This library is free software; you can redistribute it and/or
12 modify it under the terms of the GNU Lesser General Public
13 License as published by the Free Software Foundation; either
14 version 3 of the License, or (at your option) any later version.
16 This library is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 Lesser General Public License for more details.
21 You should have received a copy of the GNU Lesser General Public
22 License along with this library; if not, see <http://www.gnu.org/licenses/>.
25 #include <Python.h>
26 #include <tevent.h>
28 void init_tevent(void);
30 typedef struct {
31 PyObject_HEAD
32 struct tevent_context *ev;
33 } TeventContext_Object;
35 typedef struct {
36 PyObject_HEAD
37 struct tevent_queue *queue;
38 } TeventQueue_Object;
40 typedef struct {
41 PyObject_HEAD
42 struct tevent_req *req;
43 } TeventReq_Object;
45 typedef struct {
46 PyObject_HEAD
47 struct tevent_signal *signal;
48 } TeventSignal_Object;
50 typedef struct {
51 PyObject_HEAD
52 struct tevent_timer *timer;
53 } TeventTimer_Object;
55 typedef struct {
56 PyObject_HEAD
57 struct tevent_fd *fd;
58 } TeventFd_Object;
60 staticforward PyTypeObject TeventContext_Type;
61 staticforward PyTypeObject TeventReq_Type;
62 staticforward PyTypeObject TeventQueue_Type;
63 staticforward PyTypeObject TeventSignal_Type;
64 staticforward PyTypeObject TeventTimer_Type;
65 staticforward PyTypeObject TeventFd_Type;
67 static int py_context_init(struct tevent_context *ev)
69 /* FIXME */
70 return 0;
73 static struct tevent_fd *py_add_fd(struct tevent_context *ev,
74 TALLOC_CTX *mem_ctx,
75 int fd, uint16_t flags,
76 tevent_fd_handler_t handler,
77 void *private_data,
78 const char *handler_name,
79 const char *location)
81 /* FIXME */
82 return NULL;
85 static void py_set_fd_close_fn(struct tevent_fd *fde,
86 tevent_fd_close_fn_t close_fn)
88 /* FIXME */
91 static uint16_t py_get_fd_flags(struct tevent_fd *fde)
93 /* FIXME */
94 return 0;
97 static void py_set_fd_flags(struct tevent_fd *fde, uint16_t flags)
99 /* FIXME */
102 /* timed_event functions */
103 static struct tevent_timer *py_add_timer(struct tevent_context *ev,
104 TALLOC_CTX *mem_ctx,
105 struct timeval next_event,
106 tevent_timer_handler_t handler,
107 void *private_data,
108 const char *handler_name,
109 const char *location)
111 /* FIXME */
112 return NULL;
115 /* immediate event functions */
116 static void py_schedule_immediate(struct tevent_immediate *im,
117 struct tevent_context *ev,
118 tevent_immediate_handler_t handler,
119 void *private_data,
120 const char *handler_name,
121 const char *location)
123 /* FIXME */
126 /* signal functions */
127 static struct tevent_signal *py_add_signal(struct tevent_context *ev,
128 TALLOC_CTX *mem_ctx,
129 int signum, int sa_flags,
130 tevent_signal_handler_t handler,
131 void *private_data,
132 const char *handler_name,
133 const char *location)
135 /* FIXME */
136 return NULL;
139 /* loop functions */
140 static int py_loop_once(struct tevent_context *ev, const char *location)
142 /* FIXME */
143 return 0;
146 static int py_loop_wait(struct tevent_context *ev, const char *location)
148 /* FIXME */
149 return 0;
152 const static struct tevent_ops py_tevent_ops = {
153 .context_init = py_context_init,
154 .add_fd = py_add_fd,
155 .set_fd_close_fn = py_set_fd_close_fn,
156 .get_fd_flags = py_get_fd_flags,
157 .set_fd_flags = py_set_fd_flags,
158 .add_timer = py_add_timer,
159 .schedule_immediate = py_schedule_immediate,
160 .add_signal = py_add_signal,
161 .loop_wait = py_loop_wait,
162 .loop_once = py_loop_once,
165 static PyObject *py_register_backend(PyObject *self, PyObject *args)
167 PyObject *name, *py_backend;
169 if (!PyArg_ParseTuple(args, "O", &py_backend))
170 return NULL;
172 name = PyObject_GetAttrString(py_backend, "name");
173 if (name == NULL) {
174 PyErr_SetNone(PyExc_AttributeError);
175 return NULL;
178 if (!PyString_Check(name)) {
179 PyErr_SetNone(PyExc_TypeError);
180 return NULL;
183 if (!tevent_register_backend(PyString_AsString(name), &py_tevent_ops)) { /* FIXME: What to do with backend */
184 PyErr_SetNone(PyExc_RuntimeError);
185 return NULL;
188 Py_RETURN_NONE;
191 static PyObject *py_tevent_context_reinitialise(TeventContext_Object *self)
193 int ret = tevent_re_initialise(self->ev);
194 if (ret != 0) {
195 PyErr_SetNone(PyExc_RuntimeError);
196 return NULL;
198 Py_RETURN_NONE;
201 static PyObject *py_tevent_queue_stop(TeventQueue_Object *self)
203 tevent_queue_stop(self->queue);
204 Py_RETURN_NONE;
207 static PyObject *py_tevent_queue_start(TeventQueue_Object *self)
209 tevent_queue_start(self->queue);
210 Py_RETURN_NONE;
213 static void py_queue_trigger(struct tevent_req *req, void *private_data)
215 PyObject *callback = private_data, *ret;
217 ret = PyObject_CallFunction(callback, "");
218 Py_XDECREF(ret);
221 static PyObject *py_tevent_queue_add(TeventQueue_Object *self, PyObject *args)
223 TeventContext_Object *py_ev;
224 TeventReq_Object *py_req;
225 PyObject *trigger;
226 bool ret;
228 if (!PyArg_ParseTuple(args, "O!O!O",
229 &TeventContext_Type, &py_ev,
230 &TeventReq_Type, &py_req,
231 &trigger))
232 return NULL;
234 Py_INCREF(trigger);
236 ret = tevent_queue_add(self->queue, py_ev->ev, py_req->req,
237 py_queue_trigger, trigger);
238 if (!ret) {
239 PyErr_SetString(PyExc_RuntimeError, "queue add failed");
240 Py_DECREF(trigger);
241 return NULL;
244 Py_RETURN_NONE;
247 static PyMethodDef py_tevent_queue_methods[] = {
248 { "stop", (PyCFunction)py_tevent_queue_stop, METH_NOARGS,
249 "S.stop()" },
250 { "start", (PyCFunction)py_tevent_queue_start, METH_NOARGS,
251 "S.start()" },
252 { "add", (PyCFunction)py_tevent_queue_add, METH_VARARGS,
253 "S.add(ctx, req, trigger, baton)" },
254 { NULL },
257 static PyObject *py_tevent_context_wakeup_send(PyObject *self, PyObject *args)
259 /* FIXME */
261 Py_RETURN_NONE;
264 static PyObject *py_tevent_context_loop_wait(TeventContext_Object *self)
266 if (tevent_loop_wait(self->ev) != 0) {
267 PyErr_SetNone(PyExc_RuntimeError);
268 return NULL;
270 Py_RETURN_NONE;
273 static PyObject *py_tevent_context_loop_once(TeventContext_Object *self)
275 if (tevent_loop_once(self->ev) != 0) {
276 PyErr_SetNone(PyExc_RuntimeError);
277 return NULL;
279 Py_RETURN_NONE;
282 #ifdef TEVENT_DEPRECATED
283 static bool py_tevent_finished(PyObject *callback)
285 PyObject *py_ret;
286 bool ret;
288 py_ret = PyObject_CallFunction(callback, "");
289 if (py_ret == NULL)
290 return true;
291 ret = PyObject_IsTrue(py_ret);
292 Py_DECREF(py_ret);
293 return ret;
296 static PyObject *py_tevent_context_loop_until(TeventContext_Object *self, PyObject *args)
298 PyObject *callback;
299 if (!PyArg_ParseTuple(args, "O", &callback))
300 return NULL;
302 if (tevent_loop_until(self->ev, py_tevent_finished, callback) != 0) {
303 PyErr_SetNone(PyExc_RuntimeError);
304 return NULL;
307 if (PyErr_Occurred())
308 return NULL;
310 Py_RETURN_NONE;
312 #endif
314 static void py_tevent_signal_handler(struct tevent_context *ev,
315 struct tevent_signal *se,
316 int signum,
317 int count,
318 void *siginfo,
319 void *private_data)
321 PyObject *callback = (PyObject *)private_data, *ret;
323 ret = PyObject_CallFunction(callback, "ii", signum, count);
324 Py_XDECREF(ret);
327 static void py_tevent_signal_dealloc(TeventSignal_Object *self)
329 talloc_free(self->signal);
330 PyObject_Del(self);
333 static PyTypeObject TeventSignal_Type = {
334 .tp_name = "tevent.Signal",
335 .tp_basicsize = sizeof(TeventSignal_Object),
336 .tp_dealloc = (destructor)py_tevent_signal_dealloc,
337 .tp_flags = Py_TPFLAGS_DEFAULT,
340 static PyObject *py_tevent_context_add_signal(TeventContext_Object *self, PyObject *args)
342 int signum, sa_flags;
343 PyObject *handler;
344 struct tevent_signal *sig;
345 TeventSignal_Object *ret;
347 if (!PyArg_ParseTuple(args, "iiO", &signum, &sa_flags, &handler))
348 return NULL;
350 Py_INCREF(handler);
351 sig = tevent_add_signal(self->ev, NULL, signum, sa_flags,
352 py_tevent_signal_handler, handler);
354 ret = PyObject_New(TeventSignal_Object, &TeventSignal_Type);
355 if (ret == NULL) {
356 PyErr_NoMemory();
357 talloc_free(sig);
358 return NULL;
361 ret->signal = sig;
363 return (PyObject *)ret;
366 static void py_timer_handler(struct tevent_context *ev,
367 struct tevent_timer *te,
368 struct timeval current_time,
369 void *private_data)
371 PyObject *callback = private_data, *ret;
372 ret = PyObject_CallFunction(callback, "l", te);
373 Py_XDECREF(ret);
376 static PyObject *py_tevent_context_add_timer(TeventContext_Object *self, PyObject *args)
378 TeventTimer_Object *ret;
379 struct timeval next_event;
380 struct tevent_timer *timer;
381 PyObject *handler;
382 if (!PyArg_ParseTuple(args, "lO", &next_event, &handler))
383 return NULL;
385 timer = tevent_add_timer(self->ev, NULL, next_event, py_timer_handler,
386 handler);
387 if (timer == NULL) {
388 PyErr_SetNone(PyExc_RuntimeError);
389 return NULL;
392 ret = PyObject_New(TeventTimer_Object, &TeventTimer_Type);
393 if (ret == NULL) {
394 PyErr_NoMemory();
395 talloc_free(timer);
396 return NULL;
398 ret->timer = timer;
400 return (PyObject *)ret;
403 static void py_fd_handler(struct tevent_context *ev,
404 struct tevent_fd *fde,
405 uint16_t flags,
406 void *private_data)
408 PyObject *callback = private_data, *ret;
410 ret = PyObject_CallFunction(callback, "i", flags);
411 Py_XDECREF(ret);
414 static PyObject *py_tevent_context_add_fd(TeventContext_Object *self, PyObject *args)
416 int fd, flags;
417 PyObject *handler;
418 struct tevent_fd *tfd;
419 TeventFd_Object *ret;
421 if (!PyArg_ParseTuple(args, "iiO", &fd, &flags, &handler))
422 return NULL;
424 tfd = tevent_add_fd(self->ev, NULL, fd, flags, py_fd_handler, handler);
425 if (tfd == NULL) {
426 PyErr_SetNone(PyExc_RuntimeError);
427 return NULL;
430 ret = PyObject_New(TeventFd_Object, &TeventFd_Type);
431 if (ret == NULL) {
432 talloc_free(tfd);
433 return NULL;
435 ret->fd = tfd;
437 return (PyObject *)ret;
440 #ifdef TEVENT_DEPRECATED
441 static PyObject *py_tevent_context_set_allow_nesting(TeventContext_Object *self)
443 tevent_loop_allow_nesting(self->ev);
444 Py_RETURN_NONE;
446 #endif
448 static PyMethodDef py_tevent_context_methods[] = {
449 { "reinitialise", (PyCFunction)py_tevent_context_reinitialise, METH_NOARGS,
450 "S.reinitialise()" },
451 { "wakeup_send", (PyCFunction)py_tevent_context_wakeup_send,
452 METH_VARARGS, "S.wakeup_send(wakeup_time) -> req" },
453 { "loop_wait", (PyCFunction)py_tevent_context_loop_wait,
454 METH_NOARGS, "S.loop_wait()" },
455 { "loop_once", (PyCFunction)py_tevent_context_loop_once,
456 METH_NOARGS, "S.loop_once()" },
457 #ifdef TEVENT_DEPRECATED
458 { "loop_until", (PyCFunction)py_tevent_context_loop_until,
459 METH_VARARGS, "S.loop_until(callback)" },
460 #endif
461 { "add_signal", (PyCFunction)py_tevent_context_add_signal,
462 METH_VARARGS, "S.add_signal(signum, sa_flags, handler) -> signal" },
463 { "add_timer", (PyCFunction)py_tevent_context_add_timer,
464 METH_VARARGS, "S.add_timer(next_event, handler) -> timer" },
465 { "add_fd", (PyCFunction)py_tevent_context_add_fd,
466 METH_VARARGS, "S.add_fd(fd, flags, handler) -> fd" },
467 #ifdef TEVENT_DEPRECATED
468 { "allow_nesting", (PyCFunction)py_tevent_context_set_allow_nesting,
469 METH_NOARGS, "Whether to allow nested tevent loops." },
470 #endif
471 { NULL },
474 static PyObject *py_tevent_req_wakeup_recv(PyObject *self)
476 /* FIXME */
477 Py_RETURN_NONE;
480 static PyObject *py_tevent_req_received(PyObject *self)
482 /* FIXME */
483 Py_RETURN_NONE;
486 static PyObject *py_tevent_req_is_error(PyObject *self)
488 /* FIXME */
489 Py_RETURN_NONE;
492 static PyObject *py_tevent_req_poll(PyObject *self)
494 /* FIXME */
495 Py_RETURN_NONE;
498 static PyObject *py_tevent_req_is_in_progress(PyObject *self)
500 /* FIXME */
501 Py_RETURN_NONE;
504 static PyGetSetDef py_tevent_req_getsetters[] = {
505 { "in_progress", (getter)py_tevent_req_is_in_progress, NULL,
506 "Whether the request is in progress" },
507 { NULL }
510 static PyObject *py_tevent_req_post(PyObject *self, PyObject *args)
512 /* FIXME */
513 Py_RETURN_NONE;
516 static PyObject *py_tevent_req_set_error(PyObject *self, PyObject *args)
518 /* FIXME */
519 Py_RETURN_NONE;
522 static PyObject *py_tevent_req_done(PyObject *self)
524 /* FIXME */
525 Py_RETURN_NONE;
528 static PyObject *py_tevent_req_notify_callback(PyObject *self)
530 /* FIXME */
531 Py_RETURN_NONE;
534 static PyObject *py_tevent_req_set_endtime(PyObject *self, PyObject *args)
536 /* FIXME */
537 Py_RETURN_NONE;
540 static PyObject *py_tevent_req_cancel(TeventReq_Object *self)
542 if (!tevent_req_cancel(self->req)) {
543 PyErr_SetNone(PyExc_RuntimeError);
544 return NULL;
546 Py_RETURN_NONE;
549 static PyMethodDef py_tevent_req_methods[] = {
550 { "wakeup_recv", (PyCFunction)py_tevent_req_wakeup_recv, METH_NOARGS,
551 "Wakeup received" },
552 { "received", (PyCFunction)py_tevent_req_received, METH_NOARGS,
553 "Receive finished" },
554 { "is_error", (PyCFunction)py_tevent_req_is_error, METH_NOARGS,
555 "is_error() -> (error, state)" },
556 { "poll", (PyCFunction)py_tevent_req_poll, METH_VARARGS,
557 "poll(ctx)" },
558 { "post", (PyCFunction)py_tevent_req_post, METH_VARARGS,
559 "post(ctx) -> req" },
560 { "set_error", (PyCFunction)py_tevent_req_set_error, METH_VARARGS,
561 "set_error(error)" },
562 { "done", (PyCFunction)py_tevent_req_done, METH_NOARGS,
563 "done()" },
564 { "notify_callback", (PyCFunction)py_tevent_req_notify_callback,
565 METH_NOARGS, "notify_callback()" },
566 { "set_endtime", (PyCFunction)py_tevent_req_set_endtime,
567 METH_VARARGS, "set_endtime(ctx, endtime)" },
568 { "cancel", (PyCFunction)py_tevent_req_cancel,
569 METH_NOARGS, "cancel()" },
570 { NULL }
573 static void py_tevent_req_dealloc(TeventReq_Object *self)
575 talloc_free(self->req);
576 PyObject_DEL(self);
579 static PyTypeObject TeventReq_Type = {
580 .tp_name = "tevent.Request",
581 .tp_basicsize = sizeof(TeventReq_Object),
582 .tp_methods = py_tevent_req_methods,
583 .tp_dealloc = (destructor)py_tevent_req_dealloc,
584 .tp_getset = py_tevent_req_getsetters,
585 /* FIXME: .tp_new = py_tevent_req_new, */
588 static PyObject *py_tevent_queue_get_length(TeventQueue_Object *self)
590 return PyInt_FromLong(tevent_queue_length(self->queue));
593 static PyGetSetDef py_tevent_queue_getsetters[] = {
594 { "length", (getter)py_tevent_queue_get_length,
595 NULL, "The number of elements in the queue." },
596 { NULL },
599 static void py_tevent_queue_dealloc(TeventQueue_Object *self)
601 talloc_free(self->queue);
602 PyObject_Del(self);
605 static PyTypeObject TeventQueue_Type = {
606 .tp_name = "tevent.Queue",
607 .tp_basicsize = sizeof(TeventQueue_Object),
608 .tp_dealloc = (destructor)py_tevent_queue_dealloc,
609 .tp_flags = Py_TPFLAGS_DEFAULT,
610 .tp_getset = py_tevent_queue_getsetters,
611 .tp_methods = py_tevent_queue_methods,
614 static PyObject *py_tevent_context_signal_support(PyObject *_self)
616 TeventContext_Object *self = (TeventContext_Object *)_self;
617 return PyBool_FromLong(tevent_signal_support(self->ev));
620 static PyGetSetDef py_tevent_context_getsetters[] = {
621 { "signal_support", (getter)py_tevent_context_signal_support,
622 NULL, "if this platform and tevent context support signal handling" },
623 { NULL }
626 static void py_tevent_context_dealloc(TeventContext_Object *self)
628 talloc_free(self->ev);
629 PyObject_Del(self);
632 static PyObject *py_tevent_context_new(PyTypeObject *type, PyObject *args, PyObject *kwargs)
634 const char * const kwnames[] = { "name", NULL };
635 char *name = NULL;
636 struct tevent_context *ev;
637 TeventContext_Object *ret;
639 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|s", kwnames, &name))
640 return NULL;
642 if (name == NULL) {
643 ev = tevent_context_init(NULL);
644 } else {
645 ev = tevent_context_init_byname(NULL, name);
648 if (ev == NULL) {
649 PyErr_SetNone(PyExc_RuntimeError);
650 return NULL;
653 ret = PyObject_New(TeventContext_Object, type);
654 if (ret == NULL) {
655 PyErr_NoMemory();
656 talloc_free(ev);
657 return NULL;
660 ret->ev = ev;
661 return (PyObject *)ret;
664 static PyTypeObject TeventContext_Type = {
665 .tp_name = "tevent.Context",
666 .tp_new = py_tevent_context_new,
667 .tp_basicsize = sizeof(TeventContext_Object),
668 .tp_dealloc = (destructor)py_tevent_context_dealloc,
669 .tp_methods = py_tevent_context_methods,
670 .tp_getset = py_tevent_context_getsetters,
671 .tp_flags = Py_TPFLAGS_DEFAULT,
674 static PyObject *py_set_default_backend(PyObject *self, PyObject *args)
676 char *backend_name;
677 if (!PyArg_ParseTuple(args, "s", &backend_name))
678 return NULL;
680 tevent_set_default_backend(backend_name);
682 Py_RETURN_NONE;
685 static PyObject *py_backend_list(PyObject *self)
687 PyObject *ret;
688 int i;
689 const char **backends;
691 ret = PyList_New(0);
692 if (ret == NULL) {
693 return NULL;
696 backends = tevent_backend_list(NULL);
697 if (backends == NULL) {
698 PyErr_SetNone(PyExc_RuntimeError);
699 Py_DECREF(ret);
700 return NULL;
702 for (i = 0; backends[i]; i++) {
703 PyList_Append(ret, PyString_FromString(backends[i]));
706 talloc_free(backends);
708 return ret;
711 static PyMethodDef tevent_methods[] = {
712 { "register_backend", (PyCFunction)py_register_backend, METH_VARARGS,
713 "register_backend(backend)" },
714 { "set_default_backend", (PyCFunction)py_set_default_backend,
715 METH_VARARGS, "set_default_backend(backend)" },
716 { "backend_list", (PyCFunction)py_backend_list,
717 METH_NOARGS, "backend_list() -> list" },
718 { NULL },
721 void init_tevent(void)
723 PyObject *m;
725 if (PyType_Ready(&TeventContext_Type) < 0)
726 return;
728 if (PyType_Ready(&TeventQueue_Type) < 0)
729 return;
731 if (PyType_Ready(&TeventReq_Type) < 0)
732 return;
734 if (PyType_Ready(&TeventSignal_Type) < 0)
735 return;
737 if (PyType_Ready(&TeventTimer_Type) < 0)
738 return;
740 if (PyType_Ready(&TeventFd_Type) < 0)
741 return;
743 m = Py_InitModule3("_tevent", tevent_methods, "Tevent integration for twisted.");
744 if (m == NULL)
745 return;
747 Py_INCREF(&TeventContext_Type);
748 PyModule_AddObject(m, "Context", (PyObject *)&TeventContext_Type);
750 Py_INCREF(&TeventQueue_Type);
751 PyModule_AddObject(m, "Queue", (PyObject *)&TeventQueue_Type);
753 Py_INCREF(&TeventReq_Type);
754 PyModule_AddObject(m, "Request", (PyObject *)&TeventReq_Type);
756 Py_INCREF(&TeventSignal_Type);
757 PyModule_AddObject(m, "Signal", (PyObject *)&TeventSignal_Type);
759 Py_INCREF(&TeventTimer_Type);
760 PyModule_AddObject(m, "Timer", (PyObject *)&TeventTimer_Type);
762 Py_INCREF(&TeventFd_Type);
763 PyModule_AddObject(m, "Fd", (PyObject *)&TeventFd_Type);
765 PyModule_AddObject(m, "__version__", PyString_FromString(PACKAGE_VERSION));