| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "sd-journal.h" |
| 4 |
#include "sd-varlink.h" |
| 5 |
|
| 6 |
#include "journal-internal.h" |
| 7 |
#include "journalctl.h" |
| 8 |
#include "journalctl-filter.h" |
| 9 |
#include "journalctl-varlink-server.h" |
| 10 |
#include "json-util.h" |
| 11 |
#include "logs-show.h" |
| 12 |
#include "output-mode.h" |
| 13 |
#include "runtime-scope.h" |
| 14 |
#include "strv.h" |
| 15 |
#include "unit-name.h" /* IWYU pragma: keep */ |
| 16 |
#include "user-util.h" |
| 17 |
|
| 18 |
typedef struct GetEntriesParameters { |
| 19 |
char **units; |
| 20 |
char **user_units; |
| 21 |
const char *namespace; |
| 22 |
uid_t uid; |
| 23 |
int priority; |
| 24 |
uint64_t limit; |
| 25 |
} GetEntriesParameters; |
| 26 |
|
| 27 |
static void get_entries_parameters_done(GetEntriesParameters *p) { |
| 28 |
assert(p); |
| 29 |
|
| 30 |
p->units = strv_free(p->units); |
| 31 |
p->user_units = strv_free(p->user_units); |
| 32 |
} |
| 33 |
|
| 34 |
int vl_method_get_entries(sd_varlink *link, sd_json_variant *parameters, sd_varlink_method_flags_t flags, void *userdata) { |
| 35 |
|
| 36 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 37 |
{ "units", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_strv, offsetof(GetEntriesParameters, units), 0 }, |
| 38 |
{ "uid", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uid_gid, offsetof(GetEntriesParameters, uid), 0 }, |
| 39 |
{ "userUnits", SD_JSON_VARIANT_ARRAY, sd_json_dispatch_strv, offsetof(GetEntriesParameters, user_units), 0 }, |
| 40 |
{ "namespace", SD_JSON_VARIANT_STRING, sd_json_dispatch_const_string, offsetof(GetEntriesParameters, namespace), 0 }, |
| 41 |
{ "priority", _SD_JSON_VARIANT_TYPE_INVALID, json_dispatch_log_level, offsetof(GetEntriesParameters, priority), 0 }, |
| 42 |
{ "limit", _SD_JSON_VARIANT_TYPE_INVALID, sd_json_dispatch_uint64, offsetof(GetEntriesParameters, limit), 0 }, |
| 43 |
{} |
| 44 |
}; |
| 45 |
|
| 46 |
_cleanup_(get_entries_parameters_done) GetEntriesParameters p = { |
| 47 |
.uid = UID_INVALID, |
| 48 |
.priority = -1, |
| 49 |
}; |
| 50 |
_cleanup_(sd_journal_closep) sd_journal *j = NULL; |
| 51 |
int r; |
| 52 |
|
| 53 |
assert(link); |
| 54 |
assert(FLAGS_SET(flags, SD_VARLINK_METHOD_MORE)); |
| 55 |
|
| 56 |
r = sd_varlink_dispatch(link, parameters, dispatch_table, &p); |
| 57 |
if (r != 0) |
| 58 |
return r; |
| 59 |
|
| 60 |
if (arg_varlink_runtime_scope == RUNTIME_SCOPE_SYSTEM && p.user_units && !uid_is_valid(p.uid)) |
| 61 |
return sd_varlink_error_invalid_parameter_name(link, "uid"); |
| 62 |
|
| 63 |
/* systemd ships with sensible defaults for the system/user services and the socket permissions so we |
| 64 |
* do not need to do extra sd_varlink_get_peer_uid() or policykit checks here */ |
| 65 |
r = sd_journal_open_namespace(&j, p.namespace, SD_JOURNAL_LOCAL_ONLY | SD_JOURNAL_ASSUME_IMMUTABLE); |
| 66 |
if (r < 0) |
| 67 |
return r; |
| 68 |
|
| 69 |
r = journal_add_unit_matches(j, MATCH_UNIT_ALL, /* mangle_flags= */ 0, p.units, p.uid, p.user_units); |
| 70 |
if (r == -ENODATA) |
| 71 |
return sd_varlink_error(link, "io.systemd.JournalAccess.NoMatches", NULL); |
| 72 |
if (r < 0) |
| 73 |
return r; |
| 74 |
|
| 75 |
if (p.priority >= 0) { |
| 76 |
for (int i = 0; i <= p.priority; i++) { |
| 77 |
r = journal_add_matchf(j, "PRIORITY=%d", i); |
| 78 |
if (r < 0) |
| 79 |
return r; |
| 80 |
} |
| 81 |
|
| 82 |
r = sd_journal_add_conjunction(j); |
| 83 |
if (r < 0) |
| 84 |
return r; |
| 85 |
} |
| 86 |
|
| 87 |
/* this simulates "journalctl -n $p.limit" */ |
| 88 |
r = sd_journal_seek_tail(j); |
| 89 |
if (r < 0) |
| 90 |
return r; |
| 91 |
|
| 92 |
/* FIXME: this restriction should be removed eventually */ |
| 93 |
if (p.limit > 10000) |
| 94 |
return sd_varlink_error_invalid_parameter_name(link, "limit"); |
| 95 |
|
| 96 |
uint64_t n = p.limit == 0 ? 100 : p.limit; |
| 97 |
|
| 98 |
r = sd_journal_previous_skip(j, n + 1); |
| 99 |
if (r < 0) |
| 100 |
return r; |
| 101 |
|
| 102 |
r = sd_varlink_set_sentinel(link, "io.systemd.JournalAccess.NoEntries"); |
| 103 |
if (r < 0) |
| 104 |
return r; |
| 105 |
|
| 106 |
for (uint64_t i = 0; i < n; i++) { |
| 107 |
_cleanup_(sd_json_variant_unrefp) sd_json_variant *entry = NULL; |
| 108 |
|
| 109 |
r = sd_journal_next(j); |
| 110 |
if (r < 0) |
| 111 |
return r; |
| 112 |
if (r == 0) |
| 113 |
break; |
| 114 |
|
| 115 |
r = journal_entry_to_json(j, OUTPUT_SHOW_ALL, /* output_fields= */ NULL, &entry); |
| 116 |
if (r < 0) |
| 117 |
return r; |
| 118 |
if (r == 0) |
| 119 |
continue; /* skip corrupted entry */ |
| 120 |
|
| 121 |
r = sd_varlink_replybo(link, SD_JSON_BUILD_PAIR_VARIANT("entry", entry)); |
| 122 |
if (r < 0) |
| 123 |
return r; |
| 124 |
} |
| 125 |
|
| 126 |
return 0; |
| 127 |
} |