| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- /* Kernel log pane source: follows /dev/kmsg from the moment it is opened. */
- #include <errno.h>
- #include <fcntl.h>
- #include <glib-unix.h>
- #include <string.h>
- #include <unistd.h>
- #include "app.h"
- struct log_kmsg {
- int fd;
- guint watch_id;
- mpdc_log_fn log;
- void *user;
- };
- static gboolean on_kmsg(gint fd, GIOCondition cond, gpointer data)
- {
- struct log_kmsg *k = data;
- char buf[4096];
- ssize_t n;
- (void)cond;
- for (;;) {
- char *text, *end;
- n = read(fd, buf, sizeof(buf) - 1);
- if (n < 0 && errno == EPIPE)
- continue; /* records were overwritten; carry on */
- if (n <= 0)
- break;
- buf[n] = '\0';
- /* "prio,seq,usec,flags;message\n" then optional " KEY=value" lines */
- text = strchr(buf, ';');
- if (!text)
- continue;
- text++;
- end = strchr(text, '\n');
- if (end)
- *end = '\0';
- k->log(k->user, text);
- }
- if (n == 0 || (n < 0 && errno != EAGAIN)) {
- k->watch_id = 0;
- return G_SOURCE_REMOVE;
- }
- return G_SOURCE_CONTINUE;
- }
- struct log_kmsg *log_kmsg_new(mpdc_log_fn log, void *user)
- {
- struct log_kmsg *k = g_new0(struct log_kmsg, 1);
- k->log = log;
- k->user = user;
- k->fd = open("/dev/kmsg", O_RDONLY | O_NONBLOCK | O_CLOEXEC);
- if (k->fd < 0) {
- log(user, "Nie można otworzyć /dev/kmsg");
- return k;
- }
- lseek(k->fd, 0, SEEK_END);
- k->watch_id = g_unix_fd_add(k->fd, G_IO_IN, on_kmsg, k);
- return k;
- }
- void log_kmsg_free(struct log_kmsg *k)
- {
- if (k->watch_id)
- g_source_remove(k->watch_id);
- if (k->fd >= 0)
- close(k->fd);
- g_free(k);
- }
|