chiark / gitweb /
use memzero(foo, length); for all memset(foo, 0, length); calls
[elogind.git] / src / python-systemd / _journal.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2012 David Strauss <david@davidstrauss.net>
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU Lesser General Public License as published by
10   the Free Software Foundation; either version 2.1 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   Lesser General Public License for more details.
17
18   You should have received a copy of the GNU Lesser General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <Python.h>
23
24 #include <alloca.h>
25 #include "util.h"
26
27 #define SD_JOURNAL_SUPPRESS_LOCATION
28 #include <systemd/sd-journal.h>
29
30 PyDoc_STRVAR(journal_sendv__doc__,
31              "sendv('FIELD=value', 'FIELD=value', ...) -> None\n\n"
32              "Send an entry to the journal."
33 );
34
35 static PyObject *journal_sendv(PyObject *self, PyObject *args) {
36         struct iovec *iov = NULL;
37         int argc;
38         int i, r;
39         PyObject *ret = NULL;
40         PyObject **encoded;
41
42         /* Allocate an array for the argument strings */
43         argc = PyTuple_Size(args);
44         encoded = alloca(argc * sizeof(PyObject*));
45         memzero(encoded, argc * sizeof(PyObject*));
46
47         /* Allocate sufficient iovector space for the arguments. */
48         iov = alloca(argc * sizeof(struct iovec));
49
50         /* Iterate through the Python arguments and fill the iovector. */
51         for (i = 0; i < argc; ++i) {
52                 PyObject *item = PyTuple_GetItem(args, i);
53                 char *stritem;
54                 Py_ssize_t length;
55
56                 if (PyUnicode_Check(item)) {
57                         encoded[i] = PyUnicode_AsEncodedString(item, "utf-8", "strict");
58                         if (encoded[i] == NULL)
59                                 goto out;
60                         item = encoded[i];
61                 }
62                 if (PyBytes_AsStringAndSize(item, &stritem, &length))
63                         goto out;
64
65                 iov[i].iov_base = stritem;
66                 iov[i].iov_len = length;
67         }
68
69         /* Send the iovector to the journal. */
70         r = sd_journal_sendv(iov, argc);
71         if (r < 0) {
72                 errno = -r;
73                 PyErr_SetFromErrno(PyExc_IOError);
74                 goto out;
75         }
76
77         /* End with success. */
78         Py_INCREF(Py_None);
79         ret = Py_None;
80
81 out:
82         for (i = 0; i < argc; ++i)
83                 Py_XDECREF(encoded[i]);
84
85         return ret;
86 }
87
88 PyDoc_STRVAR(journal_stream_fd__doc__,
89              "stream_fd(identifier, priority, level_prefix) -> fd\n\n"
90              "Open a stream to journal by calling sd_journal_stream_fd(3)."
91 );
92
93 static PyObject* journal_stream_fd(PyObject *self, PyObject *args) {
94         const char* identifier;
95         int priority, level_prefix;
96         int fd;
97
98         if (!PyArg_ParseTuple(args, "sii:stream_fd",
99                               &identifier, &priority, &level_prefix))
100                 return NULL;
101
102         fd = sd_journal_stream_fd(identifier, priority, level_prefix);
103         if (fd < 0) {
104                 errno = -fd;
105                 return PyErr_SetFromErrno(PyExc_IOError);
106         }
107
108         return PyLong_FromLong(fd);
109 }
110
111 static PyMethodDef methods[] = {
112         { "sendv",  journal_sendv, METH_VARARGS, journal_sendv__doc__ },
113         { "stream_fd", journal_stream_fd, METH_VARARGS, journal_stream_fd__doc__ },
114         { NULL, NULL, 0, NULL }        /* Sentinel */
115 };
116
117 #pragma GCC diagnostic push
118 #pragma GCC diagnostic ignored "-Wmissing-prototypes"
119
120 #if PY_MAJOR_VERSION < 3
121
122 PyMODINIT_FUNC init_journal(void) {
123         PyObject *m;
124
125         m = Py_InitModule("_journal", methods);
126         if (m == NULL)
127                 return;
128
129         PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION);
130 }
131
132 #else
133
134 static struct PyModuleDef module = {
135         PyModuleDef_HEAD_INIT,
136         "_journal", /* name of module */
137         NULL, /* module documentation, may be NULL */
138         -1, /* size of per-interpreter state of the module */
139         methods
140 };
141
142 PyMODINIT_FUNC PyInit__journal(void) {
143         PyObject *m;
144
145         m = PyModule_Create(&module);
146         if (m == NULL)
147                 return NULL;
148
149         if (PyModule_AddStringConstant(m, "__version__", PACKAGE_VERSION)) {
150                 Py_DECREF(m);
151                 return NULL;
152         }
153
154         return m;
155 }
156
157 #endif
158
159 #pragma GCC diagnostic pop