util: move on_ac_power() from util.c -> udev-util.c

This commit is contained in:
Yu Watanabe
2022-01-06 18:06:22 +09:00
parent cfd4c84add
commit 06795b02e3
7 changed files with 67 additions and 64 deletions
-60
View File
@@ -6,7 +6,6 @@
#include "alloc-util.h"
#include "build.h"
#include "dirent-util.h"
#include "env-file.h"
#include "env-util.h"
#include "fd-util.h"
@@ -115,65 +114,6 @@ void in_initrd_force(bool value) {
saved_in_initrd = value;
}
int on_ac_power(void) {
bool found_offline = false, found_online = false;
_cleanup_closedir_ DIR *d = NULL;
int r;
d = opendir("/sys/class/power_supply");
if (!d)
return errno == ENOENT ? true : -errno;
FOREACH_DIRENT(de, d, return -errno) {
_cleanup_close_ int device_fd = -1;
_cleanup_free_ char *contents = NULL;
unsigned v;
device_fd = openat(dirfd(d), de->d_name, O_DIRECTORY|O_RDONLY|O_CLOEXEC);
if (device_fd < 0) {
if (IN_SET(errno, ENOENT, ENOTDIR))
continue;
return -errno;
}
r = read_virtual_file_at(device_fd, "type", SIZE_MAX, &contents, NULL);
if (r == -ENOENT)
continue;
if (r < 0)
return r;
delete_trailing_chars(contents, NEWLINE);
/* We assume every power source is AC, except for batteries. See
* https://github.com/torvalds/linux/blob/4eef766b7d4d88f0b984781bc1bcb574a6eafdc7/include/linux/power_supply.h#L176
* for defined power source types. Also see:
* https://www.kernel.org/doc/Documentation/ABI/testing/sysfs-class-power */
if (streq(contents, "Battery"))
continue;
contents = mfree(contents);
r = read_virtual_file_at(device_fd, "online", SIZE_MAX, &contents, NULL);
if (r == -ENOENT)
continue;
if (r < 0)
return r;
delete_trailing_chars(contents, NEWLINE);
r = safe_atou(contents, &v);
if (r < 0)
return r;
if (v > 0) /* At least 1 and 2 are defined as different types of 'online' */
found_online = true;
else
found_offline = true;
}
return found_online || !found_offline;
}
int container_get_leader(const char *machine, pid_t *pid) {
_cleanup_free_ char *s = NULL, *class = NULL;
const char *p;
-2
View File
@@ -20,8 +20,6 @@ int prot_from_flags(int flags) _const_;
bool in_initrd(void);
void in_initrd_force(bool value);
int on_ac_power(void);
/* Note: log2(0) == log2(1) == 0 here and below. */
#define CONST_LOG2ULL(x) ((x) > 1 ? (unsigned) __builtin_clzll(x) ^ 63U : 0)