X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fpython-systemd%2F_journal.c;h=cbc661d837ad45c6786c2826dd465161547ba6e1;hp=c305b77ce298aefb276ebbf9fecd6144bfdcf087;hb=f0f2e63bb2d5cd27e6a2464ee46a670a3159c5da;hpb=0aee68ad028e696934367045e652a65865c0de52 diff --git a/src/python-systemd/_journal.c b/src/python-systemd/_journal.c index c305b77ce..cbc661d83 100644 --- a/src/python-systemd/_journal.c +++ b/src/python-systemd/_journal.c @@ -3,7 +3,7 @@ /*** This file is part of systemd. - Copyright 2012 David Strauss + Copyright 2012 David Strauss systemd is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by @@ -21,6 +21,9 @@ #include +#include +#include "util.h" + #define SD_JOURNAL_SUPPRESS_LOCATION #include @@ -36,20 +39,12 @@ static PyObject *journal_sendv(PyObject *self, PyObject *args) { PyObject *ret = NULL; PyObject **encoded; + /* Allocate an array for the argument strings */ argc = PyTuple_Size(args); - - encoded = calloc(argc, sizeof(PyObject*)); - if (!encoded) { - ret = PyErr_NoMemory(); - goto out1; - } + encoded = alloca0(argc * sizeof(PyObject*)); /* Allocate sufficient iovector space for the arguments. */ - iov = malloc(argc * sizeof(struct iovec)); - if (!iov) { - ret = PyErr_NoMemory(); - goto out; - } + iov = alloca(argc * sizeof(struct iovec)); /* Iterate through the Python arguments and fill the iovector. */ for (i = 0; i < argc; ++i) { @@ -70,17 +65,11 @@ static PyObject *journal_sendv(PyObject *self, PyObject *args) { iov[i].iov_len = length; } - /* Clear errno, because sd_journal_sendv will not set it by - itself, unless an error occurs in one of the system calls. */ - errno = 0; - /* Send the iovector to the journal. */ r = sd_journal_sendv(iov, argc); - if (r) { - if (errno) - PyErr_SetFromErrno(PyExc_IOError); - else - PyErr_SetString(PyExc_ValueError, "invalid message format"); + if (r < 0) { + errno = -r; + PyErr_SetFromErrno(PyExc_IOError); goto out; } @@ -92,13 +81,6 @@ out: for (i = 0; i < argc; ++i) Py_XDECREF(encoded[i]); - free(encoded); - -out1: - /* Free the iovector. The actual strings - are already managed by Python. */ - free(iov); - return ret; } @@ -117,8 +99,10 @@ static PyObject* journal_stream_fd(PyObject *self, PyObject *args) { return NULL; fd = sd_journal_stream_fd(identifier, priority, level_prefix); - if (fd < 0) + if (fd < 0) { + errno = -fd; return PyErr_SetFromErrno(PyExc_IOError); + } return PyLong_FromLong(fd); } @@ -131,9 +115,17 @@ static PyMethodDef methods[] = { #if PY_MAJOR_VERSION < 3 +DISABLE_WARNING_MISSING_PROTOTYPES; PyMODINIT_FUNC init_journal(void) { - (void) Py_InitModule("_journal", methods); + PyObject *m; + + m = Py_InitModule("_journal", methods); + if (m == NULL) + return; + + PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION); } +REENABLE_WARNING; #else @@ -141,12 +133,25 @@ static struct PyModuleDef module = { PyModuleDef_HEAD_INIT, "_journal", /* name of module */ NULL, /* module documentation, may be NULL */ - 0, /* size of per-interpreter state of the module */ + -1, /* size of per-interpreter state of the module */ methods }; +DISABLE_WARNING_MISSING_PROTOTYPES; PyMODINIT_FUNC PyInit__journal(void) { - return PyModule_Create(&module); + PyObject *m; + + m = PyModule_Create(&module); + if (m == NULL) + return NULL; + + if (PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION)) { + Py_DECREF(m); + return NULL; + } + + return m; } +REENABLE_WARNING; #endif