chiark / gitweb /
Revert "socket: add support for TCP fast Open"
[elogind.git] / src / python-systemd / _daemon.c
index c6b14d46652c0bdd4c16abaea4a3e53357869442..65cfec7ce8a804d163f58871b11e535f38c7189e 100644 (file)
@@ -29,8 +29,9 @@
 #include <assert.h>
 #include <sys/socket.h>
 
-#include <systemd/sd-daemon.h>
+#include "systemd/sd-daemon.h"
 #include "pyutil.h"
+#include "macro.h"
 
 PyDoc_STRVAR(module__doc__,
         "Python interface to the libsystemd-daemon library.\n\n"
@@ -40,28 +41,6 @@ PyDoc_STRVAR(module__doc__,
         "running under systemd."
 );
 
-
-#if PY_MAJOR_VERSION >=3 && PY_MINOR_VERSION >= 1
-static int Unicode_FSConverter(PyObject* obj, void *_result) {
-        PyObject **result = _result;
-
-        assert(result);
-
-        if (!obj)
-                /* cleanup: we don't return Py_CLEANUP_SUPPORTED, so
-                 * we can assume that it was PyUnicode_FSConverter. */
-                return PyUnicode_FSConverter(obj, result);
-
-        if (obj == Py_None) {
-                *result = NULL;
-                return 1;
-        }
-
-        return PyUnicode_FSConverter(obj, result);
-}
-#endif
-
-
 PyDoc_STRVAR(booted__doc__,
              "booted() -> bool\n\n"
              "Return True iff this system is running under systemd.\n"
@@ -73,7 +52,44 @@ static PyObject* booted(PyObject *self, PyObject *args) {
         assert(args == NULL);
 
         r = sd_booted();
-        if (set_error(r, NULL, NULL))
+        if (set_error(r, NULL, NULL) < 0)
+                return NULL;
+
+        return PyBool_FromLong(r);
+}
+
+PyDoc_STRVAR(notify__doc__,
+             "notify(status, unset_environment=False) -> bool\n\n"
+             "Send a message to the init system about a status change.\n"
+             "Wraps sd_notify(3).");
+
+static PyObject* notify(PyObject *self, PyObject *args, PyObject *keywds) {
+        int r;
+        const char* msg;
+        int unset = false;
+
+        static const char* const kwlist[] = {
+                "status",
+                "unset_environment",
+                NULL,
+        };
+#if PY_MAJOR_VERSION >=3 && PY_MINOR_VERSION >= 3
+        if (!PyArg_ParseTupleAndKeywords(args, keywds, "s|p:notify",
+                                         (char**) kwlist, &msg, &unset))
+                return NULL;
+#else
+        PyObject *obj = NULL;
+        if (!PyArg_ParseTupleAndKeywords(args, keywds, "s|O:notify",
+                                         (char**) kwlist, &msg, &obj))
+                return NULL;
+        if (obj != NULL)
+                unset = PyObject_IsTrue(obj);
+        if (unset < 0)
+                return NULL;
+#endif
+
+        r = sd_notify(unset, msg);
+        if (set_error(r, NULL, NULL) < 0)
                 return NULL;
 
         return PyBool_FromLong(r);
@@ -87,16 +103,19 @@ PyDoc_STRVAR(listen_fds__doc__,
              "Wraps sd_listen_fds(3)."
 );
 
-static PyObject* listen_fds(PyObject *self, PyObject *args) {
+static PyObject* listen_fds(PyObject *self, PyObject *args, PyObject *keywds) {
         int r;
         int unset = true;
 
+        static const char* const kwlist[] = {"unset_environment", NULL};
 #if PY_MAJOR_VERSION >=3 && PY_MINOR_VERSION >= 3
-        if (!PyArg_ParseTuple(args, "|p:_listen_fds", &unset))
+        if (!PyArg_ParseTupleAndKeywords(args, keywds, "|p:_listen_fds",
+                                         (char**) kwlist, &unset))
                 return NULL;
 #else
         PyObject *obj = NULL;
-        if (!PyArg_ParseTuple(args, "|O:_listen_fds", &obj))
+        if (!PyArg_ParseTupleAndKeywords(args, keywds, "|O:_listen_fds",
+                                         (char**) kwlist, &obj))
                 return NULL;
         if (obj != NULL)
                 unset = PyObject_IsTrue(obj);
@@ -105,7 +124,7 @@ static PyObject* listen_fds(PyObject *self, PyObject *args) {
 #endif
 
         r = sd_listen_fds(unset);
-        if (set_error(r, NULL, NULL))
+        if (set_error(r, NULL, NULL) < 0)
                 return NULL;
 
         return long_FromLong(r);
@@ -133,7 +152,7 @@ static PyObject* is_fifo(PyObject *self, PyObject *args) {
 #endif
 
         r = sd_is_fifo(fd, path);
-        if (set_error(r, path, NULL))
+        if (set_error(r, path, NULL) < 0)
                 return NULL;
 
         return PyBool_FromLong(r);
@@ -161,7 +180,7 @@ static PyObject* is_mq(PyObject *self, PyObject *args) {
 #endif
 
         r = sd_is_mq(fd, path);
-        if (set_error(r, path, NULL))
+        if (set_error(r, path, NULL) < 0)
                 return NULL;
 
         return PyBool_FromLong(r);
@@ -185,7 +204,7 @@ static PyObject* is_socket(PyObject *self, PyObject *args) {
                 return NULL;
 
         r = sd_is_socket(fd, family, type, listening);
-        if (set_error(r, NULL, NULL))
+        if (set_error(r, NULL, NULL) < 0)
                 return NULL;
 
         return PyBool_FromLong(r);
@@ -212,7 +231,7 @@ static PyObject* is_socket_inet(PyObject *self, PyObject *args) {
         }
 
         r = sd_is_socket_inet(fd, family, type, listening, (uint16_t) port);
-        if (set_error(r, NULL, NULL))
+        if (set_error(r, NULL, NULL) < 0)
                 return NULL;
 
         return PyBool_FromLong(r);
@@ -247,7 +266,7 @@ static PyObject* is_socket_unix(PyObject *self, PyObject *args) {
 #endif
 
         r = sd_is_socket_unix(fd, type, listening, path, length);
-        if (set_error(r, path, NULL))
+        if (set_error(r, path, NULL) < 0)
                 return NULL;
 
         return PyBool_FromLong(r);
@@ -256,7 +275,8 @@ static PyObject* is_socket_unix(PyObject *self, PyObject *args) {
 
 static PyMethodDef methods[] = {
         { "booted", booted, METH_NOARGS, booted__doc__},
-        { "_listen_fds", listen_fds, METH_VARARGS, listen_fds__doc__},
+        { "notify", (PyCFunction) notify, METH_VARARGS | METH_KEYWORDS, notify__doc__},
+        { "_listen_fds", (PyCFunction) listen_fds, METH_VARARGS | METH_KEYWORDS, listen_fds__doc__},
         { "_is_fifo", is_fifo, METH_VARARGS, is_fifo__doc__},
         { "_is_mq", is_mq, METH_VARARGS, is_mq__doc__},
         { "_is_socket", is_socket, METH_VARARGS, is_socket__doc__},
@@ -265,11 +285,9 @@ static PyMethodDef methods[] = {
         { NULL, NULL, 0, NULL }        /* Sentinel */
 };
 
-#pragma GCC diagnostic push
-#pragma GCC diagnostic ignored "-Wmissing-prototypes"
-
 #if PY_MAJOR_VERSION < 3
 
+DISABLE_WARNING_MISSING_PROTOTYPES;
 PyMODINIT_FUNC init_daemon(void) {
         PyObject *m;
 
@@ -280,6 +298,7 @@ PyMODINIT_FUNC init_daemon(void) {
         PyModule_AddIntConstant(m, "LISTEN_FDS_START", SD_LISTEN_FDS_START);
         PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION);
 }
+REENABLE_WARNING;
 
 #else
 
@@ -291,6 +310,7 @@ static struct PyModuleDef module = {
         methods
 };
 
+DISABLE_WARNING_MISSING_PROTOTYPES;
 PyMODINIT_FUNC PyInit__daemon(void) {
         PyObject *m;
 
@@ -306,7 +326,6 @@ PyMODINIT_FUNC PyInit__daemon(void) {
 
         return m;
 }
+REENABLE_WARNING;
 
 #endif
-
-#pragma GCC diagnostic pop