[systemd-commits] 5 commits - man/sd_journal_get_fd.xml src/journal src/python-systemd sysctl.d/coredump.conf.in
Zbigniew JÄdrzejewski-Szmek
zbyszek at kemper.freedesktop.org
Wed Mar 6 21:49:15 PST 2013
man/sd_journal_get_fd.xml | 8 +-
src/journal/journalctl.c | 2
src/journal/sd-journal.c | 2
src/python-systemd/_reader.c | 123 ++++++++++++++++++++----------------
src/python-systemd/docs/journal.rst | 21 ++++--
src/python-systemd/journal.py | 99 +++++++++++++++++-----------
sysctl.d/coredump.conf.in | 2
7 files changed, 154 insertions(+), 103 deletions(-)
New commits:
commit 99613ec5d7f0c6e30801457f60c2cab7d8e50d8d
Author: Zbigniew JÄdrzejewski-Szmek <zbyszek at in.waw.pl>
Date: Thu Mar 7 00:40:30 2013 -0500
libsystemd-journal: return 0 on success in get_data()
The man page says so. Right now 0 would be returned if the data was encrypted,
1 otherwise.
diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c
index cb93fea..c90fecd 100644
--- a/src/journal/journalctl.c
+++ b/src/journal/journalctl.c
@@ -1103,7 +1103,7 @@ int main(int argc, char *argv[]) {
int flags;
if (need_seek) {
- if(!arg_reverse)
+ if (!arg_reverse)
r = sd_journal_next(j);
else
r = sd_journal_previous(j);
diff --git a/src/journal/sd-journal.c b/src/journal/sd-journal.c
index 1912354..fa04bfd 100644
--- a/src/journal/sd-journal.c
+++ b/src/journal/sd-journal.c
@@ -1868,7 +1868,7 @@ _public_ int sd_journal_get_data(sd_journal *j, const char *field, const void **
*data = o->data.payload;
*size = t;
- return 1;
+ return 0;
}
r = journal_file_move_to_object(f, OBJECT_ENTRY, f->current_offset, &o);
commit f2e82cd5add31d704b39a91d570ab6649653fa49
Author: Zbigniew JÄdrzejewski-Szmek <zbyszek at in.waw.pl>
Date: Thu Mar 7 00:35:28 2013 -0500
systemd-python: export sd_j_get_fd, sd_j_reliable_fd, sd_j_close
sd_journal_get_fd(j) is called j.fileno(), for compatiblity with
Python conventions for file-like objects.
More importantly, those new .seek_head() and .seek_tail() do not
call .get_next(). This is better, if one wants to skip before
retrieving an entry.
diff --git a/src/python-systemd/_reader.c b/src/python-systemd/_reader.c
index e5733f0..c435dad 100644
--- a/src/python-systemd/_reader.c
+++ b/src/python-systemd/_reader.c
@@ -124,6 +124,47 @@ static int Reader_init(Reader *self, PyObject *args, PyObject *keywds)
return set_error(r, path, "Invalid flags or path");
}
+PyDoc_STRVAR(Reader_fileno__doc__,
+ "fileno() -> int\n\n"
+ "Get a file descriptor to poll for changes in the journal.\n"
+ "This method invokes sd_journal_get_fd().\n"
+ "See man:sd_journal_get_fd(3).");
+static PyObject* Reader_fileno(Reader *self, PyObject *args)
+{
+ int r;
+ r = sd_journal_get_fd(self->j);
+ set_error(r, NULL, NULL);
+ if (r < 0)
+ return NULL;
+ return long_FromLong(r);
+}
+
+PyDoc_STRVAR(Reader_reliable_fd__doc__,
+ "reliable_fd() -> bool\n\n"
+ "Returns True iff the journal can be polled reliably.\n"
+ "This method invokes sd_journal_reliable_fd().\n"
+ "See man:sd_journal_reliable_fd(3).");
+static PyObject* Reader_reliable_fd(Reader *self, PyObject *args)
+{
+ int r;
+ r = sd_journal_reliable_fd(self->j);
+ set_error(r, NULL, NULL);
+ if (r < 0)
+ return NULL;
+ return PyBool_FromLong(r);
+}
+
+PyDoc_STRVAR(Reader_close__doc__,
+ "reliable_fd() -> None\n\n"
+ "Free resources allocated by this Reader object.\n"
+ "This method invokes sd_journal_close().\n"
+ "See man:sd_journal_close(3).");
+static PyObject* Reader_close(Reader *self, PyObject *args)
+{
+ sd_journal_close(self->j);
+ Py_RETURN_NONE;
+}
+
PyDoc_STRVAR(Reader_get_next__doc__,
"get_next([skip]) -> dict\n\n"
"Return dictionary of the next log entry. Optional skip value will\n"
@@ -613,6 +654,9 @@ static PyGetSetDef Reader_getseters[] = {
};
static PyMethodDef Reader_methods[] = {
+ {"fileno", (PyCFunction) Reader_fileno, METH_NOARGS, Reader_fileno__doc__},
+ {"reliable_fd", (PyCFunction) Reader_reliable_fd, METH_NOARGS, Reader_reliable_fd__doc__},
+ {"close", (PyCFunction) Reader_close, METH_NOARGS, Reader_close__doc__},
{"get_next", (PyCFunction) Reader_get_next, METH_VARARGS, Reader_get_next__doc__},
{"get_previous", (PyCFunction) Reader_get_previous, METH_VARARGS, Reader_get_previous__doc__},
{"add_match", (PyCFunction) Reader_add_match, METH_VARARGS|METH_KEYWORDS, Reader_add_match__doc__},
diff --git a/src/python-systemd/docs/journal.rst b/src/python-systemd/docs/journal.rst
index faa2707..9dc495f 100644
--- a/src/python-systemd/docs/journal.rst
+++ b/src/python-systemd/docs/journal.rst
@@ -27,6 +27,22 @@ Accessing the Journal
.. autoattribute:: systemd.journal.DEFAULT_CONVERTERS
+Example: polling for journal events
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+
+This example shows that journal events can be waited for (using
+e.g. `poll`). This makes it easy to integrate Reader in an external
+event loop:
+
+ >>> import select
+ >>> from systemd import journal
+ >>> j = journal.Reader()
+ >>> j.seek_tail()
+ >>> p = select.poll()
+ >>> p.register(j, select.POLLIN)
+ >>> p.poll()
+ [(3, 1)]
+ >>> j.get_next()
Journal access types
commit 5c1c14b3a021fbf91f31018256b0c241ea1fe3f0
Author: Zbigniew JÄdrzejewski-Szmek <zbyszek at in.waw.pl>
Date: Thu Mar 7 00:26:24 2013 -0500
systemd-python: split .seek() into .seek_head() and .seek_tail()
This way python code follows the original interface more closely.
Also, .seek(0, journal.SEEK_END) was just to much to type.
diff --git a/src/python-systemd/_reader.c b/src/python-systemd/_reader.c
index 7013fcf..e5733f0 100644
--- a/src/python-systemd/_reader.c
+++ b/src/python-systemd/_reader.c
@@ -360,60 +360,34 @@ static PyObject* Reader_flush_matches(Reader *self, PyObject *args)
Py_RETURN_NONE;
}
-PyDoc_STRVAR(Reader_seek__doc__,
- "seek(offset[, whence]) -> None\n\n"
- "Jump `offset` entries in the journal. Argument\n"
- "`whence` defines what the offset is relative to:\n"
- "os.SEEK_SET (default) from first match in journal;\n"
- "os.SEEK_CUR from current position in journal;\n"
- "and os.SEEK_END is from last match in journal.");
-static PyObject* Reader_seek(Reader *self, PyObject *args, PyObject *keywds)
+PyDoc_STRVAR(Reader_seek_head__doc__,
+ "seek_head() -> None\n\n"
+ "Jump to the beginning of the journal.\n"
+ "This method invokes sd_journal_seek_head().\n"
+ "See man:sd_journal_seek_head(3).");
+static PyObject* Reader_seek_head(Reader *self, PyObject *args)
{
- int64_t offset;
- int whence = SEEK_SET;
- PyObject *result = NULL;
-
- static const char* const kwlist[] = {"offset", "whence", NULL};
- if (!PyArg_ParseTupleAndKeywords(args, keywds, "L|i", (char**) kwlist,
- &offset, &whence))
+ int r;
+ Py_BEGIN_ALLOW_THREADS
+ r = sd_journal_seek_head(self->j);
+ Py_END_ALLOW_THREADS
+ if (set_error(r, NULL, NULL))
return NULL;
+ Py_RETURN_NONE;
+}
- switch(whence) {
- case SEEK_SET: {
- int r;
- Py_BEGIN_ALLOW_THREADS
- r = sd_journal_seek_head(self->j);
- Py_END_ALLOW_THREADS
- if (set_error(r, NULL, NULL))
- return NULL;
-
- if (offset > 0LL)
- result = PyObject_CallMethod((PyObject *)self, (char*) "get_next",
- (char*) "L", offset);
- break;
- }
- case SEEK_CUR:
- result = PyObject_CallMethod((PyObject *)self, (char*) "get_next",
- (char*) "L", offset);
- break;
- case SEEK_END: {
- int r;
- Py_BEGIN_ALLOW_THREADS
- r = sd_journal_seek_tail(self->j);
- Py_END_ALLOW_THREADS
- if (set_error(r, NULL, NULL))
- return NULL;
-
- result = PyObject_CallMethod((PyObject *)self, (char*) "get_next",
- (char*) "L", offset < 0LL ? offset : -1LL);
- break;
- }
- default:
- PyErr_SetString(PyExc_ValueError, "Invalid value for whence");
- }
-
- Py_XDECREF(result);
- if (PyErr_Occurred())
+PyDoc_STRVAR(Reader_seek_tail__doc__,
+ "seek_tail() -> None\n\n"
+ "Jump to the beginning of the journal.\n"
+ "This method invokes sd_journal_seek_tail().\n"
+ "See man:sd_journal_seek_tail(3).");
+static PyObject* Reader_seek_tail(Reader *self, PyObject *args)
+{
+ int r;
+ Py_BEGIN_ALLOW_THREADS
+ r = sd_journal_seek_tail(self->j);
+ Py_END_ALLOW_THREADS
+ if (set_error(r, NULL, NULL))
return NULL;
Py_RETURN_NONE;
}
@@ -644,7 +618,8 @@ static PyMethodDef Reader_methods[] = {
{"add_match", (PyCFunction) Reader_add_match, METH_VARARGS|METH_KEYWORDS, Reader_add_match__doc__},
{"add_disjunction", (PyCFunction) Reader_add_disjunction, METH_NOARGS, Reader_add_disjunction__doc__},
{"flush_matches", (PyCFunction) Reader_flush_matches, METH_NOARGS, Reader_flush_matches__doc__},
- {"seek", (PyCFunction) Reader_seek, METH_VARARGS | METH_KEYWORDS, Reader_seek__doc__},
+ {"seek_head", (PyCFunction) Reader_seek_head, METH_NOARGS, Reader_seek_head__doc__},
+ {"seek_tail", (PyCFunction) Reader_seek_tail, METH_NOARGS, Reader_seek_tail__doc__},
{"seek_realtime", (PyCFunction) Reader_seek_realtime, METH_VARARGS, Reader_seek_realtime__doc__},
{"seek_monotonic", (PyCFunction) Reader_seek_monotonic, METH_VARARGS, Reader_seek_monotonic__doc__},
{"wait", (PyCFunction) Reader_wait, METH_VARARGS, Reader_wait__doc__},
diff --git a/src/python-systemd/docs/journal.rst b/src/python-systemd/docs/journal.rst
index 78b831a..faa2707 100644
--- a/src/python-systemd/docs/journal.rst
+++ b/src/python-systemd/docs/journal.rst
@@ -27,12 +27,7 @@ Accessing the Journal
.. autoattribute:: systemd.journal.DEFAULT_CONVERTERS
-Whence constants
-~~~~~~~~~~~~~~~~
-.. autoattribute:: systemd.journal.SEEK_SET
-.. autoattribute:: systemd.journal.SEEK_CUR
-.. autoattribute:: systemd.journal.SEEK_END
Journal access types
~~~~~~~~~~~~~~~~~~~~
diff --git a/src/python-systemd/journal.py b/src/python-systemd/journal.py
index 80299e6..e9c09e8 100644
--- a/src/python-systemd/journal.py
+++ b/src/python-systemd/journal.py
@@ -27,7 +27,6 @@ import functools as _functools
import uuid as _uuid
import traceback as _traceback
import os as _os
-from os import SEEK_SET, SEEK_CUR, SEEK_END
import logging as _logging
if _sys.version_info >= (3,):
from collections import ChainMap as _ChainMap
commit aaf080611894aa70af421380af3bca23ad998a8d
Author: Zbigniew JÄdrzejewski-Szmek <zbyszek at in.waw.pl>
Date: Wed Mar 6 22:15:46 2013 -0500
systemd-python: catch only ValueErrors in conversion code
First of all, 'try: ... except: ...' (with no exception specified) is
always a no-no, since it catches all BaseExceptions, which includes ^C
and other stuff which should almost never be caught.
Now the conversion is stricter, and only one conversion is attempted,
and only a ValueEror is caught. It seems reasonable to catch ValueErrors,
since the entries in the journal are not verified, and any erroneous
application might log a field which cannot be converted. The consumer
of events must only check if a field is an instance of bytes and can
otherwise assume that the conversion was performed correctly.
Order of arguments in Reader.__init__ has been changed to match order
in _Reader.__init__.
Conversions have been updated to work under Python 2 and 3.
diff --git a/src/python-systemd/journal.py b/src/python-systemd/journal.py
index 23e1d65..80299e6 100644
--- a/src/python-systemd/journal.py
+++ b/src/python-systemd/journal.py
@@ -43,13 +43,29 @@ if _sys.version_info >= (3,):
else:
Monotonic = tuple
-_MONOTONIC_CONVERTER = lambda p: Monotonic((_datetime.timedelta(microseconds=p[0]),
- _uuid.UUID(bytes=p[1])))
-_REALTIME_CONVERTER = lambda x: _datetime.datetime.fromtimestamp(x / 1E6)
+def _convert_monotonic(m):
+ return Monotonic((_datetime.timedelta(microseconds=m[0]),
+ _uuid.UUID(bytes=m[1])))
+
+def _convert_source_monotonic(s):
+ return _datetime.timedelta(microseconds=int(s))
+
+def _convert_realtime(t):
+ return _datetime.datetime.fromtimestamp(t / 1E6)
+
+def _convert_timestamp(s):
+ return _datetime.datetime.fromtimestamp(int(s) / 1E6)
+
+if _sys.version_info >= (3,):
+ def _convert_uuid(s):
+ return _uuid.UUID(s.decode())
+else:
+ _convert_uuid = _uuid.UUID
+
DEFAULT_CONVERTERS = {
- 'MESSAGE_ID': _uuid.UUID,
- '_MACHINE_ID': _uuid.UUID,
- '_BOOT_ID': _uuid.UUID,
+ 'MESSAGE_ID': _convert_uuid,
+ '_MACHINE_ID': _convert_uuid,
+ '_BOOT_ID': _convert_uuid,
'PRIORITY': int,
'LEADER': int,
'SESSION_ID': int,
@@ -68,55 +84,60 @@ DEFAULT_CONVERTERS = {
'CODE_LINE': int,
'ERRNO': int,
'EXIT_STATUS': int,
- '_SOURCE_REALTIME_TIMESTAMP': _REALTIME_CONVERTER,
- '__REALTIME_TIMESTAMP': _REALTIME_CONVERTER,
- '_SOURCE_MONOTONIC_TIMESTAMP': _MONOTONIC_CONVERTER,
- '__MONOTONIC_TIMESTAMP': _MONOTONIC_CONVERTER,
+ '_SOURCE_REALTIME_TIMESTAMP': _convert_timestamp,
+ '__REALTIME_TIMESTAMP': _convert_realtime,
+ '_SOURCE_MONOTONIC_TIMESTAMP': _convert_source_monotonic,
+ '__MONOTONIC_TIMESTAMP': _convert_monotonic,
'COREDUMP': bytes,
'COREDUMP_PID': int,
'COREDUMP_UID': int,
'COREDUMP_GID': int,
'COREDUMP_SESSION': int,
'COREDUMP_SIGNAL': int,
- 'COREDUMP_TIMESTAMP': _REALTIME_CONVERTER,
+ 'COREDUMP_TIMESTAMP': _convert_timestamp,
}
-if _sys.version_info >= (3,):
- _convert_unicode = _functools.partial(str, encoding='utf-8')
-else:
- _convert_unicode = _functools.partial(unicode, encoding='utf-8')
-
class Reader(_Reader):
"""Reader allows the access and filtering of systemd journal
entries. Note that in order to access the system journal, a
non-root user must be in the `adm` group.
- Example usage to print out all error or higher level messages
- for systemd-udevd for the boot:
+ Example usage to print out all informational or higher level
+ messages for systemd-udevd for this boot:
- >>> myjournal = journal.Reader()
- >>> myjournal.add_boot_match(journal.CURRENT_BOOT)
- >>> myjournal.add_loglevel_matches(journal.LOG_ERR)
- >>> myjournal.add_match(_SYSTEMD_UNIT="systemd-udevd.service")
- >>> for entry in myjournal:
+ >>> j = journal.Reader()
+ >>> j.this_boot()
+ >>> j.log_level(journal.LOG_INFO)
+ >>> j.add_match(_SYSTEMD_UNIT="systemd-udevd.service")
+ >>> for entry in j:
... print(entry['MESSAGE'])
See systemd.journal-fields(7) for more info on typical fields
found in the journal.
"""
- def __init__(self, converters=None, flags=LOCAL_ONLY, path=None):
+ def __init__(self, flags=LOCAL_ONLY, path=None, converters=None):
"""Create an instance of Reader, which allows filtering and
return of journal entries.
- Argument `converters` is a dictionary which updates the
- DEFAULT_CONVERTERS to convert journal field values.
+
Argument `flags` sets open flags of the journal, which can be one
of, or ORed combination of constants: LOCAL_ONLY (default) opens
journal on local machine only; RUNTIME_ONLY opens only
volatile journal files; and SYSTEM_ONLY opens only
journal files of system services and the kernel.
+
Argument `path` is the directory of journal files. Note that
currently flags are ignored when `path` is present as they are
currently not relevant.
+
+ Argument `converters` is a dictionary which updates the
+ DEFAULT_CONVERTERS to convert journal field values. Field
+ names are used as keys into this dictionary. The values must
+ be single argument functions, which take a `bytes` object and
+ return a converted value. When there's no entry for a field
+ name, then the default UTF-8 decoding will be attempted. If
+ the conversion fails with a ValueError, unconverted bytes
+ object will be returned. (Note that ValueEror is a superclass
+ of UnicodeDecodeError).
"""
super(Reader, self).__init__(flags, path)
if _sys.version_info >= (3,3):
@@ -130,18 +151,19 @@ class Reader(_Reader):
self.converters.update(converters)
def _convert_field(self, key, value):
- """Convert value based on callable from self.converters
- based of field/key"""
+ """Convert value using self.converters[key]
+
+ If `key` is not present in self.converters, a standard unicode
+ decoding will be attempted. If the conversion (either
+ key-specific or the default one) fails with a ValueError, the
+ original bytes object will be returned.
+ """
+ convert = self.converters.get(key, bytes.decode)
try:
- result = self.converters[key](value)
- except:
- # Default conversion in unicode
- try:
- result = _convert_unicode(value)
- except UnicodeDecodeError:
- # Leave in default bytes
- result = value
- return result
+ return convert(value)
+ except ValueError:
+ # Leave in default bytes
+ return value
def _convert_entry(self, entry):
"""Convert entire journal entry utilising _covert_field"""
@@ -217,7 +239,7 @@ class Reader(_Reader):
"""
if 0 <= level <= 7:
for i in range(level+1):
- self.add_match(PRIORITY="%s" % i)
+ self.add_match(PRIORITY="%d" % i)
else:
raise ValueError("Log level must be 0 <= level <= 7")
commit 1d98d9a62c16c8282d02942d80e025ceec962c9b
Author: Zbigniew JÄdrzejewski-Szmek <zbyszek at in.waw.pl>
Date: Wed Mar 6 17:07:42 2013 -0500
man: fix compilation of example
diff --git a/man/sd_journal_get_fd.xml b/man/sd_journal_get_fd.xml
index 189d213..3fc9c5f 100644
--- a/man/sd_journal_get_fd.xml
+++ b/man/sd_journal_get_fd.xml
@@ -212,7 +212,7 @@ int main(int argc, char *argv[]) {
return 1;
}
for (;;) {
- const char *d;
+ const void *d;
size_t l;
r = sd_journal_next(j);
if (r < 0) {
@@ -233,7 +233,7 @@ int main(int argc, char *argv[]) {
fprintf(stderr, "Failed to read message field: %s\n", strerror(-r));
continue;
}
- printf("%.*s\n", (int) l, d);
+ printf("%.*s\n", (int) l, (const char*) d);
}
sd_journal_close(j);
return 0;
@@ -248,9 +248,9 @@ int main(int argc, char *argv[]) {
int wait_for_changes(sd_journal *j) {
struct pollfd pollfd;
- pollfd.fd = sd_journal_get_fd();
+ pollfd.fd = sd_journal_get_fd(j);
pollfd.events = POLLIN;
- poll(&pollfd, 1, sd_journal_reliable_fd() > 0 ? -1 : 2000);
+ poll(&pollfd, 1, sd_journal_reliable_fd(j) > 0 ? -1 : 2000);
return sd_journal_process(j);
}
</programlisting>
diff --git a/sysctl.d/coredump.conf.in b/sysctl.d/coredump.conf.in
index 5c791b7..d5795a3 100644
--- a/sysctl.d/coredump.conf.in
+++ b/sysctl.d/coredump.conf.in
@@ -5,6 +5,6 @@
# the Free Software Foundation; either version 2.1 of the License, or
# (at your option) any later version.
-# See sysctl.d(5) for details
+# See sysctl.d(5) and core(5) for for details.
kernel.core_pattern=|@rootlibexecdir@/systemd-coredump %p %u %g %s %t %e
More information about the systemd-commits
mailing list