| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <unistd.h> |
| 4 |
|
| 5 |
#include "sd-journal.h" |
| 6 |
#include "sd-varlink.h" |
| 7 |
|
| 8 |
#include "errno-util.h" |
| 9 |
#include "hashmap.h" |
| 10 |
#include "journal-internal.h" |
| 11 |
#include "journal-vacuum.h" |
| 12 |
#include "journalctl.h" |
| 13 |
#include "journalctl-util.h" |
| 14 |
#include "journalctl-varlink.h" |
| 15 |
#include "log.h" |
| 16 |
#include "string-util.h" |
| 17 |
#include "varlink-util.h" |
| 18 |
|
| 19 |
int varlink_connect_journal(sd_varlink **ret) { |
| 20 |
_cleanup_(sd_varlink_flush_close_unrefp) sd_varlink *vl = NULL; |
| 21 |
const char *address; |
| 22 |
int r; |
| 23 |
|
| 24 |
assert(ret); |
| 25 |
|
| 26 |
address = arg_namespace ? |
| 27 |
strjoina("/run/systemd/journal.", arg_namespace, "/io.systemd.journal") : |
| 28 |
"/run/systemd/journal/io.systemd.journal"; |
| 29 |
|
| 30 |
r = sd_varlink_connect_address(&vl, address); |
| 31 |
if (r < 0) |
| 32 |
return r; |
| 33 |
|
| 34 |
(void) sd_varlink_set_description(vl, "journal"); |
| 35 |
(void) sd_varlink_set_relative_timeout(vl, USEC_INFINITY); |
| 36 |
|
| 37 |
*ret = TAKE_PTR(vl); |
| 38 |
return 0; |
| 39 |
} |
| 40 |
|
| 41 |
int action_flush_to_var(void) { |
| 42 |
_cleanup_(sd_varlink_flush_close_unrefp) sd_varlink *link = NULL; |
| 43 |
int r; |
| 44 |
|
| 45 |
assert(arg_action == ACTION_FLUSH); |
| 46 |
|
| 47 |
if (arg_machine || arg_namespace) |
| 48 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
| 49 |
"--flush is not supported in conjunction with %s.", |
| 50 |
arg_machine ? "--machine=" : "--namespace="); |
| 51 |
|
| 52 |
if (access("/run/systemd/journal/flushed", F_OK) >= 0) |
| 53 |
return 0; /* Already flushed, no need to contact journald */ |
| 54 |
if (errno != ENOENT) |
| 55 |
return log_error_errno(errno, "Unable to check for existence of /run/systemd/journal/flushed: %m"); |
| 56 |
|
| 57 |
r = varlink_connect_journal(&link); |
| 58 |
if (r < 0) |
| 59 |
return log_error_errno(r, "Failed to connect to Varlink socket: %m"); |
| 60 |
|
| 61 |
return varlink_call_and_log(link, "io.systemd.Journal.FlushToVar", /* parameters= */ NULL, /* ret_parameters= */ NULL); |
| 62 |
} |
| 63 |
|
| 64 |
int action_relinquish_var(void) { |
| 65 |
_cleanup_(sd_varlink_flush_close_unrefp) sd_varlink *link = NULL; |
| 66 |
int r; |
| 67 |
|
| 68 |
assert(IN_SET(arg_action, ACTION_RELINQUISH_VAR, ACTION_SMART_RELINQUISH_VAR)); |
| 69 |
|
| 70 |
if (arg_machine || arg_namespace) |
| 71 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
| 72 |
"--(smart-)relinquish-var is not supported in conjunction with %s.", |
| 73 |
arg_machine ? "--machine=" : "--namespace="); |
| 74 |
|
| 75 |
r = varlink_connect_journal(&link); |
| 76 |
if (r < 0) |
| 77 |
return log_error_errno(r, "Failed to connect to Varlink socket: %m"); |
| 78 |
|
| 79 |
return varlink_call_and_log(link, "io.systemd.Journal.RelinquishVar", /* parameters= */ NULL, /* ret_parameters= */ NULL); |
| 80 |
} |
| 81 |
|
| 82 |
int action_rotate(void) { |
| 83 |
_cleanup_(sd_varlink_flush_close_unrefp) sd_varlink *link = NULL; |
| 84 |
int r; |
| 85 |
|
| 86 |
assert(IN_SET(arg_action, ACTION_ROTATE, ACTION_ROTATE_AND_VACUUM)); |
| 87 |
|
| 88 |
if (arg_machine) |
| 89 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
| 90 |
"--rotate is not supported in conjunction with --machine=."); |
| 91 |
|
| 92 |
r = varlink_connect_journal(&link); |
| 93 |
if (r < 0) |
| 94 |
return log_error_errno(r, "Failed to connect to Varlink socket: %m"); |
| 95 |
|
| 96 |
return varlink_call_and_log(link, "io.systemd.Journal.Rotate", /* parameters= */ NULL, /* ret_parameters= */ NULL); |
| 97 |
} |
| 98 |
|
| 99 |
int action_vacuum(void) { |
| 100 |
_cleanup_(sd_journal_closep) sd_journal *j = NULL; |
| 101 |
Directory *d; |
| 102 |
int r, ret = 0; |
| 103 |
|
| 104 |
assert(IN_SET(arg_action, ACTION_VACUUM, ACTION_ROTATE_AND_VACUUM)); |
| 105 |
|
| 106 |
r = acquire_journal(&j); |
| 107 |
if (r < 0) |
| 108 |
return r; |
| 109 |
|
| 110 |
HASHMAP_FOREACH(d, j->directories_by_path) { |
| 111 |
r = journal_directory_vacuum(d->path, arg_vacuum_size, arg_vacuum_n_files, arg_vacuum_time, NULL, !arg_quiet); |
| 112 |
if (r < 0) |
| 113 |
RET_GATHER(ret, log_error_errno(r, "Failed to vacuum %s: %m", d->path)); |
| 114 |
} |
| 115 |
|
| 116 |
return ret; |
| 117 |
} |
| 118 |
|
| 119 |
int action_rotate_and_vacuum(void) { |
| 120 |
int r; |
| 121 |
|
| 122 |
assert(arg_action == ACTION_ROTATE_AND_VACUUM); |
| 123 |
|
| 124 |
r = action_rotate(); |
| 125 |
if (r < 0) |
| 126 |
return r; |
| 127 |
|
| 128 |
return action_vacuum(); |
| 129 |
} |
| 130 |
|
| 131 |
int action_sync(void) { |
| 132 |
_cleanup_(sd_varlink_flush_close_unrefp) sd_varlink *link = NULL; |
| 133 |
int r; |
| 134 |
|
| 135 |
assert(arg_action == ACTION_SYNC); |
| 136 |
|
| 137 |
if (arg_machine) |
| 138 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), |
| 139 |
"--sync is not supported in conjunction with --machine=."); |
| 140 |
|
| 141 |
r = varlink_connect_journal(&link); |
| 142 |
if (ERRNO_IS_NEG_DISCONNECT(r) && arg_namespace) |
| 143 |
/* If the namespaced sd-journald instance was shut down due to inactivity, it should already |
| 144 |
* be synchronized */ |
| 145 |
return 0; |
| 146 |
if (r < 0) |
| 147 |
return log_error_errno(r, "Failed to connect to Varlink socket: %m"); |
| 148 |
|
| 149 |
return varlink_call_and_log(link, "io.systemd.Journal.Synchronize", /* parameters= */ NULL, /* ret_parameters= */ NULL); |
| 150 |
} |