/***
This file is part of systemd.
- Copyright 2012 David Strauss
+ Copyright 2012 David Strauss <david@davidstrauss.net>
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
#include <Python.h>
+#include <alloca.h>
+
#define SD_JOURNAL_SUPPRESS_LOCATION
#include <systemd/sd-journal.h>
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 = alloca(argc * sizeof(PyObject*));
+ memset(encoded, 0, 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) {
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;
}
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;
}
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);
}
{ NULL, NULL, 0, NULL } /* Sentinel */
};
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wmissing-prototypes"
+
#if PY_MAJOR_VERSION < 3
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);
}
#else
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
};
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;
}
#endif
+
+#pragma GCC diagnostic pop