]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
util: move on_ac_power() from util.c -> udev-util.c
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 4 Jan 2022 23:05:26 +0000 (08:05 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Thu, 6 Jan 2022 09:06:22 +0000 (18:06 +0900)
src/ac-power/ac-power.c
src/basic/util.c
src/basic/util.h
src/shared/condition.c
src/shared/udev-util.c
src/shared/udev-util.h
src/test/test-condition.c

index c4bfe7cb18effc8dcdc2b10b6bf79d5207b864f3..12379df344f26c02a8b38aaf4866a082ac5e437a 100644 (file)
@@ -3,7 +3,7 @@
 #include <getopt.h>
 
 #include "main-func.h"
-#include "util.h"
+#include "udev-util.h"
 
 static bool arg_verbose = false;
 
index 3aecb22fc4dc18eb49b12c7def1d72bc6a9613c3..d7ef382737e913a035e00f25d77642e90c4c4974 100644 (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;
index f5434c9641831e5326d1f5249bf4c4fe43972af8..94804f28e3f7256afb282a4b6162e931d984f1e8 100644 (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)
index 44e26775db8f692cbc01a1bc4270b6e41c9ef08c..68fbbf643a9197a2081f0a393e4831351b7a238d 100644 (file)
@@ -50,9 +50,9 @@
 #include "string-table.h"
 #include "string-util.h"
 #include "tomoyo-util.h"
+#include "udev-util.h"
 #include "uid-alloc-range.h"
 #include "user-util.h"
-#include "util.h"
 #include "virt.h"
 
 Condition* condition_new(ConditionType type, const char *parameter, bool trigger, bool negate) {
index 27dfd11a6a7cc1518406b934274e11352ff12874..73ca886fb301d9dccc643286032966f84697c302 100644 (file)
@@ -2,6 +2,7 @@
 
 #include <ctype.h>
 #include <errno.h>
+#include <fcntl.h>
 #include <sys/inotify.h>
 #include <unistd.h>
 
 #include "device-nodes.h"
 #include "device-private.h"
 #include "device-util.h"
+#include "dirent-util.h"
 #include "env-file.h"
 #include "errno-util.h"
 #include "escape.h"
 #include "fd-util.h"
+#include "fileio.h"
 #include "log.h"
 #include "macro.h"
 #include "parse-util.h"
@@ -579,3 +582,62 @@ int udev_queue_init(void) {
 
         return TAKE_FD(fd);
 }
+
+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;
+}
index 276686da80864bb3c8d373baf7d1f7a09883e44e..8d21dc43647b3fd80e60dc3e2f6c2249ed587395 100644 (file)
@@ -53,6 +53,8 @@ int udev_resolve_subsys_kernel(const char *string, char *result, size_t maxsize,
 int udev_queue_is_empty(void);
 int udev_queue_init(void);
 
+int on_ac_power(void);
+
 #if HAVE_SYS_SDT_H
 
 /* Each trace point can have different number of additional arguments. Note that when the macro is used only
index 4b22784d17897b3bc50bacf2145281d32f2b87f5..c872a6c2b9dd80a12ed32b41c803e893ef53fe8e 100644 (file)
@@ -33,6 +33,7 @@
 #include "strv.h"
 #include "tests.h"
 #include "tomoyo-util.h"
+#include "udev-util.h"
 #include "uid-alloc-range.h"
 #include "user-util.h"
 #include "virt.h"