summaryrefslogtreecommitdiff
path: root/src/ac-power/ac-power.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2012-12-25 16:29:51 +0100
committerLennart Poettering <lennart@poettering.net>2012-12-31 21:24:26 +0100
commit240dbaa44f8e5ad51775c776fc3ce9cd2f19f037 (patch)
tree607e02cd59aef350e3b32698742cd1e00bc71d7c /src/ac-power/ac-power.c
parent3ec90c030081111900597ec5475d880893f10a76 (diff)
unit: add ConditionACPower=
Diffstat (limited to 'src/ac-power/ac-power.c')
-rw-r--r--src/ac-power/ac-power.c80
1 files changed, 3 insertions, 77 deletions
diff --git a/src/ac-power/ac-power.c b/src/ac-power/ac-power.c
index 37313cf144..bd1b6ecc72 100644
--- a/src/ac-power/ac-power.c
+++ b/src/ac-power/ac-power.c
@@ -19,93 +19,19 @@
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"
-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. */
- 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;
}
- return r == 0;
+ return r != 0 ? EXIT_SUCCESS : EXIT_FAILURE;
}