| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "alloc-util.h" |
| 4 |
#include "extract-word.h" |
| 5 |
#include "log.h" |
| 6 |
#include "parse-util.h" |
| 7 |
#include "printk-util.h" |
| 8 |
#include "sysctl-util.h" |
| 9 |
|
| 10 |
int sysctl_printk_read(void) { |
| 11 |
int r; |
| 12 |
|
| 13 |
_cleanup_free_ char *sysctl_printk_vals = NULL; |
| 14 |
r = sysctl_read("kernel/printk", &sysctl_printk_vals); |
| 15 |
if (r < 0) |
| 16 |
return log_debug_errno(r, "Cannot read sysctl kernel.printk: %m"); |
| 17 |
|
| 18 |
_cleanup_free_ char *sysctl_printk_curr = NULL; |
| 19 |
const char *p = sysctl_printk_vals; |
| 20 |
r = extract_first_word(&p, &sysctl_printk_curr, NULL, 0); |
| 21 |
if (r < 0) |
| 22 |
return log_debug_errno(r, "Failed to split out kernel printk priority: %m"); |
| 23 |
if (r == 0) |
| 24 |
return log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "Short read while reading kernel.printk sysctl"); |
| 25 |
|
| 26 |
int current_lvl; |
| 27 |
r = safe_atoi(sysctl_printk_curr, ¤t_lvl); |
| 28 |
if (r < 0) |
| 29 |
return log_debug_errno(r, "Failed to parse kernel.printk sysctl: %s", sysctl_printk_vals); |
| 30 |
|
| 31 |
return current_lvl; |
| 32 |
} |
| 33 |
|
| 34 |
int sysctl_printk_write(int l) { |
| 35 |
int r; |
| 36 |
|
| 37 |
r = sysctl_writef("kernel/printk", "%i", l); |
| 38 |
if (r < 0) |
| 39 |
return log_debug_errno(r, "Failed to set kernel.printk to %i: %m", l); |
| 40 |
|
| 41 |
return 0; |
| 42 |
} |