| 1 |
/* sysprof-journald-source.c |
| 2 |
* |
| 3 |
* Copyright 2023 Christian Hergert <[email protected]> |
| 4 |
* |
| 5 |
* This program is free software: you can redistribute it and/or modify |
| 6 |
* it under the terms of the GNU General Public License as published by |
| 7 |
* the Free Software Foundation, either version 3 of the License, or |
| 8 |
* (at your option) any later version. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, |
| 11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
* GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License |
| 16 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
* |
| 18 |
* SPDX-License-Identifier: GPL-3.0-or-later |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "config.h" |
| 22 |
|
| 23 |
#include <systemd/sd-journal.h> |
| 24 |
|
| 25 |
#include "sysprof-journald-source.h" |
| 26 |
|
| 27 |
typedef struct _SysprofJournaldSource |
| 28 |
{ |
| 29 |
GSource parent_instance; |
| 30 |
sd_journal *journal; |
| 31 |
SysprofJournaldSourceFunc func; |
| 32 |
gpointer func_data; |
| 33 |
GDestroyNotify func_data_destroy; |
| 34 |
} SysprofJournaldSource; |
| 35 |
|
| 36 |
static void |
| 37 |
sysprof_journald_source_finalize (GSource *source) |
| 38 |
{ |
| 39 |
SysprofJournaldSource *self = (SysprofJournaldSource *)source; |
| 40 |
|
| 41 |
if (self->func_data_destroy) |
| 42 |
self->func_data_destroy (self->func_data); |
| 43 |
|
| 44 |
g_clear_pointer (&self->journal, sd_journal_close); |
| 45 |
} |
| 46 |
|
| 47 |
static gboolean |
| 48 |
sysprof_journald_source_dispatch (GSource *source, |
| 49 |
GSourceFunc callback, |
| 50 |
gpointer user_data) |
| 51 |
{ |
| 52 |
SysprofJournaldSource *self = (SysprofJournaldSource *)source; |
| 53 |
|
| 54 |
if (sd_journal_process (self->journal) == SD_JOURNAL_APPEND) |
| 55 |
{ |
| 56 |
if (sd_journal_next (self->journal) > 0) |
| 57 |
return self->func (self->func_data, self->journal); |
| 58 |
} |
| 59 |
|
| 60 |
return G_SOURCE_CONTINUE; |
| 61 |
} |
| 62 |
|
| 63 |
static GSourceFuncs sysprof_journald_source_funcs = { |
| 64 |
.dispatch = sysprof_journald_source_dispatch, |
| 65 |
.finalize = sysprof_journald_source_finalize, |
| 66 |
}; |
| 67 |
|
| 68 |
GSource * |
| 69 |
sysprof_journald_source_new (SysprofJournaldSourceFunc func, |
| 70 |
gpointer func_data, |
| 71 |
GDestroyNotify func_data_destroy) |
| 72 |
{ |
| 73 |
SysprofJournaldSource *self; |
| 74 |
sd_journal *journal = NULL; |
| 75 |
int fd; |
| 76 |
|
| 77 |
if (sd_journal_open (&journal, 0) < 0) |
| 78 |
return NULL; |
| 79 |
|
| 80 |
if (sd_journal_seek_tail (journal) < 0) |
| 81 |
goto failure; |
| 82 |
|
| 83 |
if (sd_journal_previous (journal) < 0) |
| 84 |
goto failure; |
| 85 |
|
| 86 |
if (sd_journal_next (journal) < 0) |
| 87 |
goto failure; |
| 88 |
|
| 89 |
if (-1 == (fd = sd_journal_get_fd (journal))) |
| 90 |
goto failure; |
| 91 |
|
| 92 |
self = (SysprofJournaldSource *)g_source_new (&sysprof_journald_source_funcs, |
| 93 |
sizeof (SysprofJournaldSource)); |
| 94 |
self->journal = journal; |
| 95 |
self->func = func; |
| 96 |
self->func_data = func_data; |
| 97 |
self->func_data_destroy = func_data_destroy; |
| 98 |
|
| 99 |
g_source_add_unix_fd ((GSource *)self, fd, sd_journal_get_events (journal)); |
| 100 |
|
| 101 |
return (GSource *)self; |
| 102 |
|
| 103 |
failure: |
| 104 |
g_clear_pointer (&journal, sd_journal_close); |
| 105 |
|
| 106 |
return NULL; |
| 107 |
} |