| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <stdbool.h> |
| 4 |
|
| 5 |
#include "sd-bus.h" |
| 6 |
#include "sd-event.h" |
| 7 |
#include "sd-varlink.h" |
| 8 |
|
| 9 |
#include "alloc-util.h" |
| 10 |
#include "ansi-color.h" |
| 11 |
#include "build.h" |
| 12 |
#include "bus-error.h" |
| 13 |
#include "bus-locator.h" |
| 14 |
#include "bus-util.h" |
| 15 |
#include "daemon-util.h" |
| 16 |
#include "errno-util.h" |
| 17 |
#include "format-table.h" |
| 18 |
#include "log.h" |
| 19 |
#include "main-func.h" |
| 20 |
#include "options.h" |
| 21 |
#include "parse-argument.h" |
| 22 |
#include "pretty-print.h" |
| 23 |
#include "printk-util.h" |
| 24 |
#include "varlink-io.systemd.MuteConsole.h" |
| 25 |
#include "varlink-util.h" |
| 26 |
#include "virt.h" |
| 27 |
|
| 28 |
static bool arg_mute_pid1 = true; |
| 29 |
static bool arg_mute_kernel = true; |
| 30 |
static bool arg_varlink = false; |
| 31 |
|
| 32 |
static int help(void) { |
| 33 |
_cleanup_free_ char *link = NULL; |
| 34 |
_cleanup_(table_unrefp) Table *options = NULL; |
| 35 |
int r; |
| 36 |
|
| 37 |
r = terminal_urlify_man("systemd-mute-console", "1", &link); |
| 38 |
if (r < 0) |
| 39 |
return log_oom(); |
| 40 |
|
| 41 |
r = option_parser_get_help_table(&options); |
| 42 |
if (r < 0) |
| 43 |
return r; |
| 44 |
|
| 45 |
printf("%s [OPTIONS...]\n\n" |
| 46 |
"%sMute status output to the console.%s\n\n", |
| 47 |
program_invocation_short_name, |
| 48 |
ansi_highlight(), |
| 49 |
ansi_normal()); |
| 50 |
|
| 51 |
r = table_print_or_warn(options); |
| 52 |
if (r < 0) |
| 53 |
return r; |
| 54 |
|
| 55 |
printf("\nSee the %s for details.\n", link); |
| 56 |
return 0; |
| 57 |
} |
| 58 |
|
| 59 |
static int parse_argv(int argc, char *argv[]) { |
| 60 |
assert(argc >= 0); |
| 61 |
assert(argv); |
| 62 |
|
| 63 |
OptionParser opts = { argc, argv }; |
| 64 |
int r; |
| 65 |
|
| 66 |
FOREACH_OPTION_OR_RETURN(c, &opts) |
| 67 |
switch (c) { |
| 68 |
|
| 69 |
OPTION_COMMON_HELP: |
| 70 |
return help(); |
| 71 |
|
| 72 |
OPTION_COMMON_VERSION: |
| 73 |
return version(); |
| 74 |
|
| 75 |
OPTION_LONG("kernel", "BOOL", "Mute kernel log output"): |
| 76 |
r = parse_boolean_argument("--kernel=", opts.arg, &arg_mute_kernel); |
| 77 |
if (r < 0) |
| 78 |
return r; |
| 79 |
break; |
| 80 |
|
| 81 |
OPTION_LONG("pid1", "BOOL", "Mute PID 1 status output"): |
| 82 |
r = parse_boolean_argument("--pid1=", opts.arg, &arg_mute_pid1); |
| 83 |
if (r < 0) |
| 84 |
return r; |
| 85 |
break; |
| 86 |
} |
| 87 |
|
| 88 |
r = sd_varlink_invocation(SD_VARLINK_ALLOW_ACCEPT); |
| 89 |
if (r < 0) |
| 90 |
return log_error_errno(r, "Failed to check if invoked in Varlink mode: %m"); |
| 91 |
if (r > 0) |
| 92 |
arg_varlink = true; |
| 93 |
|
| 94 |
return 1; |
| 95 |
} |
| 96 |
|
| 97 |
static int set_show_status(const char *value) { |
| 98 |
int r; |
| 99 |
assert(value); |
| 100 |
|
| 101 |
_cleanup_(sd_bus_flush_close_unrefp) sd_bus *bus = NULL; |
| 102 |
r = bus_connect_system_systemd(&bus); |
| 103 |
if (r < 0) |
| 104 |
return log_error_errno(r, "Failed to connect to systemd: %m"); |
| 105 |
|
| 106 |
_cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
| 107 |
r = bus_call_method(bus, bus_systemd_mgr, "SetShowStatus", &error, /* ret_reply= */ NULL, "s", value); |
| 108 |
if (r < 0) |
| 109 |
return log_error_errno(r, "Failed to issue SetShowStatus() method call: %s", bus_error_message(&error, r)); |
| 110 |
|
| 111 |
return 0; |
| 112 |
} |
| 113 |
|
| 114 |
typedef struct Context { |
| 115 |
bool mute_pid1; |
| 116 |
bool mute_kernel; |
| 117 |
|
| 118 |
bool muted_pid1; |
| 119 |
int saved_kernel; |
| 120 |
|
| 121 |
sd_varlink *link; |
| 122 |
} Context; |
| 123 |
|
| 124 |
static int mute_pid1(Context *c) { |
| 125 |
int r; |
| 126 |
|
| 127 |
assert(c); |
| 128 |
|
| 129 |
if (!c->mute_pid1) { |
| 130 |
log_debug("Muting of PID 1 status console output disabled."); |
| 131 |
c->muted_pid1 = false; |
| 132 |
return 0; |
| 133 |
} |
| 134 |
|
| 135 |
r = set_show_status("no"); |
| 136 |
if (r < 0) |
| 137 |
return r; |
| 138 |
|
| 139 |
log_debug("Successfully muted PID 1 status console output."); |
| 140 |
|
| 141 |
c->muted_pid1 = true; |
| 142 |
return 0; |
| 143 |
} |
| 144 |
|
| 145 |
static int unmute_pid1(Context *c) { |
| 146 |
int r; |
| 147 |
|
| 148 |
assert(c); |
| 149 |
|
| 150 |
if (!c->muted_pid1) { |
| 151 |
if (c->mute_pid1) |
| 152 |
log_debug("Not restoring PID 1 status console output level."); |
| 153 |
return 0; |
| 154 |
} |
| 155 |
|
| 156 |
r = set_show_status(""); |
| 157 |
if (r < 0) |
| 158 |
return r; |
| 159 |
|
| 160 |
log_debug("Successfully unmuted PID 1 status console output."); |
| 161 |
c->muted_pid1 = false; |
| 162 |
return 0; |
| 163 |
} |
| 164 |
|
| 165 |
static int mute_kernel(Context *c) { |
| 166 |
int r; |
| 167 |
|
| 168 |
assert(c); |
| 169 |
|
| 170 |
if (!c->mute_kernel) { |
| 171 |
log_debug("Muting of kernel printk() console output disabled."); |
| 172 |
c->saved_kernel = -1; |
| 173 |
return 0; |
| 174 |
} |
| 175 |
|
| 176 |
if (detect_container() > 0) { |
| 177 |
log_debug("Skipping muting of printk() console output, because running in a container."); |
| 178 |
|
| 179 |
c->mute_kernel = false; |
| 180 |
c->saved_kernel = -1; |
| 181 |
return 0; |
| 182 |
} |
| 183 |
|
| 184 |
int level = sysctl_printk_read(); |
| 185 |
if (level < 0) |
| 186 |
return log_error_errno(level, "Failed to read kernel printk() console output level: %m"); |
| 187 |
|
| 188 |
if (level == 0) { |
| 189 |
log_info("Not muting kernel printk() console output, since it is already disabled."); |
| 190 |
c->saved_kernel = -1; /* don't bother with restoring */ |
| 191 |
} else { |
| 192 |
r = sysctl_printk_write(0); |
| 193 |
if (r < 0) |
| 194 |
return log_error_errno(r, "Failed to change kernel printk() console output level: %m"); |
| 195 |
|
| 196 |
log_debug("Successfully muted kernel printk() console output."); |
| 197 |
c->saved_kernel = level; |
| 198 |
} |
| 199 |
|
| 200 |
return 0; |
| 201 |
} |
| 202 |
|
| 203 |
static int unmute_kernel(Context *c) { |
| 204 |
int r; |
| 205 |
|
| 206 |
assert(c); |
| 207 |
|
| 208 |
if (c->saved_kernel < 0) { |
| 209 |
if (c->mute_kernel) |
| 210 |
log_debug("Not restoring kernel printk() console output level."); |
| 211 |
return 0; |
| 212 |
} |
| 213 |
|
| 214 |
int level = sysctl_printk_read(); |
| 215 |
if (level < 0) |
| 216 |
return log_error_errno(level, "Failed to read kernel printk() console output level: %m"); |
| 217 |
|
| 218 |
if (level != 0) { |
| 219 |
log_info("Not unmuting kernel printk() console output, since it has been changed externally in the meantime."); |
| 220 |
return 0; |
| 221 |
} |
| 222 |
|
| 223 |
r = sysctl_printk_write(c->saved_kernel); |
| 224 |
if (r < 0) |
| 225 |
return log_error_errno(r, "Failed to unmute kernel printk() console output level: %m"); |
| 226 |
|
| 227 |
log_debug("Successfully unmuted kernel printk() console output."); |
| 228 |
c->saved_kernel = -1; |
| 229 |
return 0; |
| 230 |
} |
| 231 |
|
| 232 |
static void vl_on_disconnect(sd_varlink_server *server, sd_varlink *link, void *userdata) { |
| 233 |
assert(link); |
| 234 |
|
| 235 |
Context *c = sd_varlink_get_userdata(link); |
| 236 |
if (!c) |
| 237 |
return; |
| 238 |
|
| 239 |
(void) unmute_pid1(c); |
| 240 |
(void) unmute_kernel(c); |
| 241 |
|
| 242 |
(void) sd_varlink_set_userdata(c->link, NULL); |
| 243 |
sd_varlink_flush_close_unref(c->link); |
| 244 |
|
| 245 |
free(c); |
| 246 |
} |
| 247 |
|
| 248 |
static int vl_method_mute( |
| 249 |
sd_varlink *link, |
| 250 |
sd_json_variant *parameters, |
| 251 |
sd_varlink_method_flags_t flags, |
| 252 |
void *userdata) { |
| 253 |
|
| 254 |
int r; |
| 255 |
|
| 256 |
assert(link); |
| 257 |
assert(FLAGS_SET(flags, SD_VARLINK_METHOD_MORE)); |
| 258 |
|
| 259 |
_cleanup_free_ Context *nc = new(Context, 1); |
| 260 |
if (!nc) |
| 261 |
return -ENOMEM; |
| 262 |
|
| 263 |
*nc = (Context) { |
| 264 |
.mute_pid1 = true, |
| 265 |
.mute_kernel = true, |
| 266 |
.saved_kernel = -1, |
| 267 |
}; |
| 268 |
|
| 269 |
static const sd_json_dispatch_field dispatch_table[] = { |
| 270 |
{ "kernel", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(Context, mute_kernel), 0 }, |
| 271 |
{ "pid1", SD_JSON_VARIANT_BOOLEAN, sd_json_dispatch_stdbool, offsetof(Context, mute_pid1), 0 }, |
| 272 |
{} |
| 273 |
}; |
| 274 |
|
| 275 |
r = sd_varlink_dispatch(link, parameters, dispatch_table, nc); |
| 276 |
if (r != 0) |
| 277 |
return r; |
| 278 |
|
| 279 |
r = sd_varlink_server_bind_disconnect(sd_varlink_get_server(link), vl_on_disconnect); |
| 280 |
if (r < 0) |
| 281 |
return r; |
| 282 |
|
| 283 |
(void) sd_varlink_set_userdata(link, nc); |
| 284 |
nc->link = sd_varlink_ref(link); |
| 285 |
Context *c = TAKE_PTR(nc); /* the Context object is now managed by the disconnect handler, not us anymore */ |
| 286 |
|
| 287 |
r = 0; |
| 288 |
RET_GATHER(r, mute_pid1(c)); |
| 289 |
RET_GATHER(r, mute_kernel(c)); |
| 290 |
if (r < 0) |
| 291 |
return r; |
| 292 |
|
| 293 |
/* Let client know we are muted now. We use sd_varlink_notify() here (rather than sd_varlink_reply()) |
| 294 |
* because we want to keep the method call open, as we want that the lifetime of the |
| 295 |
* connection/method call to determine how long we keep the console muted. */ |
| 296 |
r = sd_varlink_notify(link, /* parameters= */ NULL); |
| 297 |
if (r < 0) |
| 298 |
return r; |
| 299 |
|
| 300 |
return 0; |
| 301 |
} |
| 302 |
|
| 303 |
static int vl_server(void) { |
| 304 |
_cleanup_(sd_varlink_server_unrefp) sd_varlink_server *varlink_server = NULL; |
| 305 |
int r; |
| 306 |
|
| 307 |
/* Invocation as Varlink service */ |
| 308 |
|
| 309 |
r = varlink_server_new( |
| 310 |
&varlink_server, |
| 311 |
SD_VARLINK_SERVER_ROOT_ONLY | |
| 312 |
SD_VARLINK_SERVER_MYSELF_ONLY | |
| 313 |
SD_VARLINK_SERVER_HANDLE_SIGINT | |
| 314 |
SD_VARLINK_SERVER_HANDLE_SIGTERM, |
| 315 |
/* userdata= */ NULL); |
| 316 |
if (r < 0) |
| 317 |
return log_error_errno(r, "Failed to allocate Varlink server: %m"); |
| 318 |
|
| 319 |
r = sd_varlink_server_add_interface(varlink_server, &vl_interface_io_systemd_MuteConsole); |
| 320 |
if (r < 0) |
| 321 |
return log_error_errno(r, "Failed to add Varlink interface: %m"); |
| 322 |
|
| 323 |
r = sd_varlink_server_bind_method_many( |
| 324 |
varlink_server, |
| 325 |
"io.systemd.MuteConsole.Mute", vl_method_mute); |
| 326 |
if (r < 0) |
| 327 |
return log_error_errno(r, "Failed to bind Varlink methods: %m"); |
| 328 |
|
| 329 |
r = sd_varlink_server_loop_auto(varlink_server); |
| 330 |
if (r < 0) |
| 331 |
return log_error_errno(r, "Failed to run Varlink event loop: %m"); |
| 332 |
|
| 333 |
return 0; |
| 334 |
} |
| 335 |
|
| 336 |
static int run(int argc, char* argv[]) { |
| 337 |
int r; |
| 338 |
|
| 339 |
log_setup(); |
| 340 |
|
| 341 |
r = parse_argv(argc, argv); |
| 342 |
if (r <= 0) |
| 343 |
return r; |
| 344 |
|
| 345 |
if (arg_varlink) |
| 346 |
return vl_server(); |
| 347 |
|
| 348 |
if (!arg_mute_pid1 && !arg_mute_kernel) |
| 349 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), "Not asked to mute anything, refusing."); |
| 350 |
|
| 351 |
Context c = { |
| 352 |
.mute_pid1 = arg_mute_pid1, |
| 353 |
.mute_kernel = arg_mute_kernel, |
| 354 |
.saved_kernel = -1, |
| 355 |
}; |
| 356 |
|
| 357 |
_cleanup_(sd_event_unrefp) sd_event *event = NULL; |
| 358 |
r = sd_event_new(&event); |
| 359 |
if (r < 0) |
| 360 |
return log_error_errno(r, "Failed to get default event source: %m"); |
| 361 |
|
| 362 |
(void) sd_event_set_watchdog(event, true); |
| 363 |
(void) sd_event_set_signal_exit(event, true); |
| 364 |
|
| 365 |
int ret = 0; |
| 366 |
RET_GATHER(ret, mute_pid1(&c)); |
| 367 |
RET_GATHER(ret, mute_kernel(&c)); |
| 368 |
|
| 369 |
/* Now tell service manager we are ready to go */ |
| 370 |
_unused_ _cleanup_(notify_on_cleanup) const char *notify_message = |
| 371 |
notify_start("READY=1\n" |
| 372 |
"STATUS=Console status output muted temporarily.", |
| 373 |
"STOPPING=1\n" |
| 374 |
"STATUS=Console status output unmuted."); |
| 375 |
|
| 376 |
/* Now wait for SIGINT/SIGTERM */ |
| 377 |
r = sd_event_loop(event); |
| 378 |
if (r < 0) |
| 379 |
RET_GATHER(ret, log_error_errno(r, "Failed to run event loop: %m")); |
| 380 |
|
| 381 |
RET_GATHER(ret, unmute_pid1(&c)); |
| 382 |
RET_GATHER(ret, unmute_kernel(&c)); |
| 383 |
|
| 384 |
return ret; |
| 385 |
} |
| 386 |
|
| 387 |
DEFINE_MAIN_FUNCTION(run); |