chiark / gitweb /
unit: add ConditionACPower=
authorLennart Poettering <lennart@poettering.net>
Tue, 25 Dec 2012 15:29:51 +0000 (16:29 +0100)
committerLennart Poettering <lennart@poettering.net>
Mon, 31 Dec 2012 20:24:26 +0000 (21:24 +0100)
TODO
man/systemd.unit.xml
src/ac-power/ac-power.c
src/core/condition.c
src/core/condition.h
src/core/load-fragment-gperf.gperf.m4
src/shared/util.c
src/shared/util.h

diff --git a/TODO b/TODO
index 3ec87c5a968125b058cf462425276f90e11984ed..aa9292ec661dc2a3060dd9885f4ec7bb024abacc 100644 (file)
--- a/TODO
+++ b/TODO
@@ -57,8 +57,6 @@ Features:
 
 * When shutdown.target is queued begin with an asynchronous sync()?
 
 
 * When shutdown.target is queued begin with an asynchronous sync()?
 
-* Add ConditionBatteryPower= or ConditionACPower=? (but definitely not both)
-
 * add API to close/reopen/get fd for journal client fd in libsystemd-journal.
 
 * maybe add API to send pairs of iovecs via sd_journal_send
 * add API to close/reopen/get fd for journal client fd in libsystemd-journal.
 
 * maybe add API to send pairs of iovecs via sd_journal_send
@@ -240,8 +238,6 @@ Features:
 
 * system-wide seccomp filter
 
 
 * system-wide seccomp filter
 
-* ability to pass fds into systemd
-
 * system.conf should have controls for cgroups
 
 * bind mount read-only the cgroup tree higher than nspawn
 * system.conf should have controls for cgroups
 
 * bind mount read-only the cgroup tree higher than nspawn
index bf91b4eb137eb1a03297f9311d84f01729065b84..8570815ad47e07fdf577e9bf89f90591f82d55b8 100644 (file)
                                 <term><varname>ConditionSecurity=</varname></term>
                                 <term><varname>ConditionCapability=</varname></term>
                                 <term><varname>ConditionHost=</varname></term>
                                 <term><varname>ConditionSecurity=</varname></term>
                                 <term><varname>ConditionCapability=</varname></term>
                                 <term><varname>ConditionHost=</varname></term>
+                                <term><varname>ConditionACPower=</varname></term>
                                 <term><varname>ConditionNull=</varname></term>
 
                                 <listitem><para>Before starting a unit
                                 <term><varname>ConditionNull=</varname></term>
 
                                 <listitem><para>Before starting a unit
                                 The test may be negated by prepending
                                 an exclamation mark.</para>
 
                                 The test may be negated by prepending
                                 an exclamation mark.</para>
 
+                                <para><varname>ConditionACPower=</varname>
+                                may may be used to check whether the
+                                system has AC power, or is exlcusively
+                                battery powered at the time of
+                                activation of the unit. This takes a
+                                boolean argument. If set to
+                                <varname>true</varname> the condition
+                                will hold only if at least one AC
+                                connector of the system is connected
+                                to a power source, or if no AC
+                                connectors are known. Conversely, if
+                                set to <varname>false</varname> the
+                                condition will hold only if there is
+                                at least one AC connector known and
+                                all AC connectors are disconnected
+                                from a power source.</para>
+
                                 <para>Finally,
                                 <varname>ConditionNull=</varname> may
                                 be used to add a constant condition
                                 <para>Finally,
                                 <varname>ConditionNull=</varname> may
                                 be used to add a constant condition
index 37313cf144d2051c8d250de9824d068bf0eb33c2..bd1b6ecc72c780245373cffd2476473642888fad 100644 (file)
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
   along with systemd; If not, see <http://www.gnu.org/licenses/>.
 ***/
 
-#include <stdlib.h>
-#include <stdbool.h>
-#include <errno.h>
-#include <string.h>
-#include <libudev.h>
-
 #include "util.h"
 
 #include "util.h"
 
-static int on_ac_power(void) {
-        int r;
-
-        struct udev *udev;
-        struct udev_enumerate *e = NULL;
-        struct udev_list_entry *item = NULL, *first = NULL;
-        bool found_offline = false, found_online = false;
-
-        if (!(udev = udev_new())) {
-                r = -ENOMEM;
-                goto finish;
-        }
-
-        if (!(e = udev_enumerate_new(udev))) {
-                r = -ENOMEM;
-                goto finish;
-        }
-
-        if (udev_enumerate_add_match_subsystem(e, "power_supply") < 0) {
-                r = -EIO;
-                goto finish;
-        }
-
-        if (udev_enumerate_scan_devices(e) < 0) {
-                r = -EIO;
-                goto finish;
-        }
-
-        first = udev_enumerate_get_list_entry(e);
-        udev_list_entry_foreach(item, first) {
-                struct udev_device *d;
-                const char *type, *online;
-
-                if (!(d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item)))) {
-                        r = -ENOMEM;
-                        goto finish;
-                }
-
-                if (!(type = udev_device_get_sysattr_value(d, "type")))
-                        goto next;
-
-                if (!streq(type, "Mains"))
-                        goto next;
-
-                if (!(online = udev_device_get_sysattr_value(d, "online")))
-                        goto next;
-
-                if (streq(online, "1")) {
-                        found_online = true;
-                        break;
-                } else if (streq(online, "0"))
-                        found_offline = true;
-
-        next:
-                udev_device_unref(d);
-        }
-
-        r = found_online || !found_offline;
-
-finish:
-        if (e)
-                udev_enumerate_unref(e);
-
-        if (udev)
-                udev_unref(udev);
-
-        return r;
-}
-
 int main(int argc, char *argv[]) {
         int r;
 
         /* This is mostly intended to be used for scripts which want
          * to detect whether AC power is plugged in or not. */
 
 int main(int argc, char *argv[]) {
         int r;
 
         /* This is mostly intended to be used for scripts which want
          * to detect whether AC power is plugged in or not. */
 
-        if ((r = on_ac_power()) < 0) {
+        r = on_ac_power();
+        if (r < 0) {
                 log_error("Failed to read AC status: %s", strerror(-r));
                 return EXIT_FAILURE;
         }
 
                 log_error("Failed to read AC status: %s", strerror(-r));
                 return EXIT_FAILURE;
         }
 
-        return r == 0;
+        return r != 0 ? EXIT_SUCCESS : EXIT_FAILURE;
 }
 }
index 32a37ccad61c514ff1807767535afad5462a134d..b3184922b8ecd6ed4c86a3d556996d00819e67e7 100644 (file)
@@ -221,6 +221,16 @@ static bool test_host(const char *parameter) {
         return b;
 }
 
         return b;
 }
 
+static bool test_ac_power(const char *parameter) {
+        int r;
+
+        r = parse_boolean(parameter);
+        if (r < 0)
+                return true;
+
+        return (on_ac_power() != 0) == !!r;
+}
+
 bool condition_test(Condition *c) {
         assert(c);
 
 bool condition_test(Condition *c) {
         assert(c);
 
@@ -294,6 +304,9 @@ bool condition_test(Condition *c) {
         case CONDITION_HOST:
                 return test_host(c->parameter) == !c->negate;
 
         case CONDITION_HOST:
                 return test_host(c->parameter) == !c->negate;
 
+        case CONDITION_AC_POWER:
+                return test_ac_power(c->parameter) == !c->negate;
+
         case CONDITION_NULL:
                 return !c->negate;
 
         case CONDITION_NULL:
                 return !c->negate;
 
@@ -364,6 +377,7 @@ static const char* const condition_type_table[_CONDITION_TYPE_MAX] = {
         [CONDITION_VIRTUALIZATION] = "ConditionVirtualization",
         [CONDITION_SECURITY] = "ConditionSecurity",
         [CONDITION_HOST] = "ConditionHost",
         [CONDITION_VIRTUALIZATION] = "ConditionVirtualization",
         [CONDITION_SECURITY] = "ConditionSecurity",
         [CONDITION_HOST] = "ConditionHost",
+        [CONDITION_AC_POWER] = "ConditionACPower",
         [CONDITION_NULL] = "ConditionNull"
 };
 
         [CONDITION_NULL] = "ConditionNull"
 };
 
index 03954e40b3c00160d1917161ca408df1aacef4a9..1797385d26aeb2a04e694403cce779fdef17c3e8 100644 (file)
@@ -40,6 +40,7 @@ typedef enum ConditionType {
         CONDITION_SECURITY,
         CONDITION_CAPABILITY,
         CONDITION_HOST,
         CONDITION_SECURITY,
         CONDITION_CAPABILITY,
         CONDITION_HOST,
+        CONDITION_AC_POWER,
         CONDITION_NULL,
         _CONDITION_TYPE_MAX,
         _CONDITION_TYPE_INVALID = -1
         CONDITION_NULL,
         _CONDITION_TYPE_MAX,
         _CONDITION_TYPE_INVALID = -1
index 7212053ca7ca451a25df8126b3616bf31b453d31..7fba0cfb772bc4244bb4dfbe4301b47c24c4e11d 100644 (file)
@@ -137,6 +137,7 @@ Unit.ConditionVirtualization,    config_parse_unit_condition_string, CONDITION_V
 Unit.ConditionSecurity,          config_parse_unit_condition_string, CONDITION_SECURITY,            0
 Unit.ConditionCapability,        config_parse_unit_condition_string, CONDITION_CAPABILITY,          0
 Unit.ConditionHost,              config_parse_unit_condition_string, CONDITION_HOST,                0
 Unit.ConditionSecurity,          config_parse_unit_condition_string, CONDITION_SECURITY,            0
 Unit.ConditionCapability,        config_parse_unit_condition_string, CONDITION_CAPABILITY,          0
 Unit.ConditionHost,              config_parse_unit_condition_string, CONDITION_HOST,                0
+Unit.ConditionACPower,           config_parse_unit_condition_string, CONDITION_AC_POWER,            0
 Unit.ConditionNull,              config_parse_unit_condition_null,   0,                             0
 m4_dnl
 Service.PIDFile,                 config_parse_unit_path_printf,      0,                             offsetof(Service, pid_file)
 Unit.ConditionNull,              config_parse_unit_condition_null,   0,                             0
 m4_dnl
 Service.PIDFile,                 config_parse_unit_path_printf,      0,                             offsetof(Service, pid_file)
index 1779625c77916ccb10b1d8ceca99d3b40688acc9..3f00db7f2c2ed8fc521b892069827d1d0d28392f 100644 (file)
@@ -5763,3 +5763,81 @@ char *strip_tab_ansi(char **ibuf, size_t *_isz) {
 
         return obuf;
 }
 
         return obuf;
 }
+
+int on_ac_power(void) {
+        bool found_offline = false, found_online = false;
+        _cleanup_closedir_ DIR *d = NULL;
+
+        d = opendir("/sys/class/power_supply");
+        if (!d)
+                return -errno;
+
+        for (;;) {
+                struct dirent *de;
+                union dirent_storage buf;
+                _cleanup_free_ char *p = NULL;
+                _cleanup_close_ int fd = -1, device = -1;
+                char contents[6];
+                ssize_t n;
+                int k;
+
+                k = readdir_r(d, &buf.de, &de);
+                if (k != 0)
+                        return -k;
+
+                if (!de)
+                        break;
+
+                if (ignore_file(de->d_name))
+                        continue;
+
+                device = openat(dirfd(d), de->d_name, O_DIRECTORY|O_RDONLY|O_CLOEXEC|O_NOCTTY);
+                if (device < 0) {
+                        if (errno == ENOENT || errno == ENOTDIR)
+                                continue;
+
+                        return -errno;
+                }
+
+                fd = openat(device, "type", O_RDONLY|O_CLOEXEC|O_NOCTTY);
+                if (fd < 0) {
+                        if (errno == ENOENT)
+                                continue;
+
+                        return -errno;
+                }
+
+                n = read(fd, contents, sizeof(contents));
+                if (n < 0)
+                        return -errno;
+
+                if (n != 6 || memcmp(contents, "Mains\n", 6))
+                        continue;
+
+                close_nointr_nofail(fd);
+                fd = openat(device, "online", O_RDONLY|O_CLOEXEC|O_NOCTTY);
+                if (fd < 0) {
+                        if (errno == ENOENT)
+                                continue;
+
+                        return -errno;
+                }
+
+                n = read(fd, contents, sizeof(contents));
+                if (n < 0)
+                        return -errno;
+
+                if (n != 2 || contents[1] != '\n')
+                        return -EIO;
+
+                if (contents[0] == '1') {
+                        found_online = true;
+                        break;
+                } else if (contents[0] == '0')
+                        found_offline = true;
+                else
+                        return -EIO;
+        }
+
+        return found_online || !found_offline;
+}
index 25b349a17a0c1ff9be109963635b7a69ee521186..bb6602fb243636109f8cd774c0d6be9ff72c535f 100644 (file)
@@ -561,3 +561,5 @@ const char *draw_special_char(DrawSpecialChar ch);
 char *strreplace(const char *text, const char *old_string, const char *new_string);
 
 char *strip_tab_ansi(char **p, size_t *l);
 char *strreplace(const char *text, const char *old_string, const char *new_string);
 
 char *strip_tab_ansi(char **p, size_t *l);
+
+int on_ac_power(void);