| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include <sys/socket.h> /* IWYU pragma: keep */ |
| 4 |
|
| 5 |
#include "sd-messages.h" |
| 6 |
|
| 7 |
#include "alloc-util.h" |
| 8 |
#include "battery-util.h" |
| 9 |
#include "build.h" |
| 10 |
#include "fd-util.h" |
| 11 |
#include "format-table.h" |
| 12 |
#include "glyph-util.h" |
| 13 |
#include "log.h" |
| 14 |
#include "main-func.h" |
| 15 |
#include "options.h" |
| 16 |
#include "plymouth-util.h" |
| 17 |
#include "pretty-print.h" |
| 18 |
#include "proc-cmdline.h" |
| 19 |
#include "terminal-util.h" |
| 20 |
#include "time-util.h" |
| 21 |
|
| 22 |
#define BATTERY_LOW_MESSAGE \ |
| 23 |
"Battery level critically low. Please connect your charger or the system will power off in 10 seconds." |
| 24 |
#define BATTERY_RESTORED_MESSAGE \ |
| 25 |
"A.C. power restored, continuing." |
| 26 |
|
| 27 |
static bool arg_doit = true; |
| 28 |
|
| 29 |
static int help(void) { |
| 30 |
_cleanup_free_ char *link = NULL; |
| 31 |
_cleanup_(table_unrefp) Table *options = NULL; |
| 32 |
int r; |
| 33 |
|
| 34 |
r = terminal_urlify_man("systemd-battery-check", "8", &link); |
| 35 |
if (r < 0) |
| 36 |
return log_oom(); |
| 37 |
|
| 38 |
r = option_parser_get_help_table(&options); |
| 39 |
if (r < 0) |
| 40 |
return r; |
| 41 |
|
| 42 |
printf("%s\n\n" |
| 43 |
"%sCheck battery level to see whether there's enough charge.%s\n\n", |
| 44 |
program_invocation_short_name, |
| 45 |
ansi_highlight(), |
| 46 |
ansi_normal()); |
| 47 |
|
| 48 |
r = table_print_or_warn(options); |
| 49 |
if (r < 0) |
| 50 |
return r; |
| 51 |
|
| 52 |
printf("\nSee the %s for details.\n", link); |
| 53 |
return 0; |
| 54 |
} |
| 55 |
|
| 56 |
static int plymouth_send_message(const char *mode, const char *message) { |
| 57 |
_cleanup_free_ char *plymouth_message = NULL; |
| 58 |
int c, r; |
| 59 |
|
| 60 |
assert(mode); |
| 61 |
assert(message); |
| 62 |
|
| 63 |
c = asprintf(&plymouth_message, |
| 64 |
"C\x02%c%s%c" |
| 65 |
"M\x02%c%s%c", |
| 66 |
(int) strlen(mode) + 1, mode, '\x00', |
| 67 |
(int) strlen(message) + 1, message, '\x00'); |
| 68 |
if (c < 0) |
| 69 |
return log_oom(); |
| 70 |
|
| 71 |
/* We set SOCK_NONBLOCK here so that we rather drop the message than wait for plymouth */ |
| 72 |
r = plymouth_send_raw(plymouth_message, c, SOCK_NONBLOCK); |
| 73 |
if (r < 0) |
| 74 |
return log_full_errno(ERRNO_IS_NO_PLYMOUTH(r) ? LOG_DEBUG : LOG_WARNING, r, |
| 75 |
"Failed to communicate with plymouth: %m"); |
| 76 |
|
| 77 |
return 0; |
| 78 |
} |
| 79 |
|
| 80 |
static int parse_argv(int argc, char *argv[]) { |
| 81 |
assert(argc >= 0); |
| 82 |
assert(argv); |
| 83 |
|
| 84 |
OptionParser opts = { argc, argv }; |
| 85 |
|
| 86 |
FOREACH_OPTION_OR_RETURN(c, &opts) |
| 87 |
switch (c) { |
| 88 |
|
| 89 |
OPTION_COMMON_HELP: |
| 90 |
return help(); |
| 91 |
|
| 92 |
OPTION_COMMON_VERSION: |
| 93 |
return version(); |
| 94 |
} |
| 95 |
|
| 96 |
if (option_parser_get_n_args(&opts) != 0) |
| 97 |
return log_error_errno(SYNTHETIC_ERRNO(EINVAL), |
| 98 |
"%s takes no argument.", |
| 99 |
program_invocation_short_name); |
| 100 |
return 1; |
| 101 |
} |
| 102 |
|
| 103 |
static int run(int argc, char *argv[]) { |
| 104 |
_cleanup_free_ char *plymouth_message = NULL; |
| 105 |
_cleanup_close_ int fd = -EBADF; |
| 106 |
int r; |
| 107 |
|
| 108 |
log_setup(); |
| 109 |
|
| 110 |
r = parse_argv(argc, argv); |
| 111 |
if (r <= 0) |
| 112 |
return r; |
| 113 |
|
| 114 |
r = proc_cmdline_get_bool("systemd.battery_check", PROC_CMDLINE_STRIP_RD_PREFIX|PROC_CMDLINE_TRUE_WHEN_MISSING, &arg_doit); |
| 115 |
if (r < 0) |
| 116 |
log_warning_errno(r, "Failed to parse systemd.battery_check= kernel command line option, ignoring: %m"); |
| 117 |
|
| 118 |
if (!arg_doit) { |
| 119 |
log_info("Checking battery status and AC power existence is disabled by the kernel command line, skipping execution."); |
| 120 |
return 0; |
| 121 |
} |
| 122 |
|
| 123 |
r = battery_is_discharging_and_low(); |
| 124 |
if (r < 0) { |
| 125 |
log_warning_errno(r, "Failed to check battery status, ignoring: %m"); |
| 126 |
return 0; |
| 127 |
} |
| 128 |
if (r == 0) |
| 129 |
return 0; |
| 130 |
log_struct(LOG_EMERG, |
| 131 |
LOG_MESSAGE("%s " BATTERY_LOW_MESSAGE, glyph(GLYPH_LOW_BATTERY)), |
| 132 |
LOG_MESSAGE_ID(SD_MESSAGE_BATTERY_LOW_WARNING_STR)); |
| 133 |
|
| 134 |
fd = open_terminal("/dev/console", O_WRONLY|O_NOCTTY|O_CLOEXEC); |
| 135 |
if (fd < 0) |
| 136 |
log_warning_errno(fd, "Failed to open console, ignoring: %m"); |
| 137 |
else |
| 138 |
dprintf(fd, ANSI_HIGHLIGHT_RED "%s " BATTERY_LOW_MESSAGE ANSI_NORMAL "\n", |
| 139 |
glyph_full(GLYPH_LOW_BATTERY, /* force_utf= */ false)); |
| 140 |
|
| 141 |
if (asprintf(&plymouth_message, "%s " BATTERY_LOW_MESSAGE, |
| 142 |
glyph_full(GLYPH_LOW_BATTERY, /* force_utf= */ true)) < 0) |
| 143 |
return log_oom(); |
| 144 |
|
| 145 |
(void) plymouth_send_message("shutdown", plymouth_message); |
| 146 |
|
| 147 |
usleep_safe(10 * USEC_PER_SEC); |
| 148 |
|
| 149 |
r = battery_is_discharging_and_low(); |
| 150 |
if (r < 0) |
| 151 |
return log_warning_errno(r, "Failed to check battery status, assuming not charged yet, powering off: %m"); |
| 152 |
if (r > 0) { |
| 153 |
log_struct(LOG_EMERG, |
| 154 |
LOG_MESSAGE("Battery level critically low, powering off."), |
| 155 |
LOG_MESSAGE_ID(SD_MESSAGE_BATTERY_LOW_POWEROFF_STR)); |
| 156 |
return r; |
| 157 |
} |
| 158 |
|
| 159 |
log_info(BATTERY_RESTORED_MESSAGE); |
| 160 |
if (fd >= 0) |
| 161 |
dprintf(fd, BATTERY_RESTORED_MESSAGE "\n"); |
| 162 |
(void) plymouth_send_message("boot-up", BATTERY_RESTORED_MESSAGE); |
| 163 |
|
| 164 |
return 0; |
| 165 |
} |
| 166 |
|
| 167 |
DEFINE_MAIN_FUNCTION_WITH_POSITIVE_FAILURE(run); |