chiark / gitweb /
units: activate wall agent automatically if something is dropped in /dev/.systemd...
[elogind.git] / src / ac-power.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
8   systemd is free software; you can redistribute it and/or modify it
9   under the terms of the GNU General Public License as published by
10   the Free Software Foundation; either version 2 of the License, or
11   (at your option) any later version.
12
13   systemd is distributed in the hope that it will be useful, but
14   WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16   General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <stdlib.h>
23 #include <stdbool.h>
24 #include <errno.h>
25 #include <string.h>
26 #include <libudev.h>
27
28 #include "util.h"
29 #include "ac-power.h"
30
31 int on_ac_power(void) {
32         int r;
33
34         struct udev *udev;
35         struct udev_enumerate *e = NULL;
36         struct udev_list_entry *item = NULL, *first = NULL;
37         bool found_offline = false, found_online = false;
38
39         if (!(udev = udev_new())) {
40                 r = -ENOMEM;
41                 goto finish;
42         }
43
44         if (!(e = udev_enumerate_new(udev))) {
45                 r = -ENOMEM;
46                 goto finish;
47         }
48
49         if (udev_enumerate_add_match_subsystem(e, "power_supply") < 0) {
50                 r = -EIO;
51                 goto finish;
52         }
53
54         if (udev_enumerate_scan_devices(e) < 0) {
55                 r = -EIO;
56                 goto finish;
57         }
58
59         first = udev_enumerate_get_list_entry(e);
60         udev_list_entry_foreach(item, first) {
61                 struct udev_device *d;
62                 const char *type, *online;
63
64                 if (!(d = udev_device_new_from_syspath(udev, udev_list_entry_get_name(item)))) {
65                         r = -ENOMEM;
66                         goto finish;
67                 }
68
69                 if (!(type = udev_device_get_sysattr_value(d, "type")))
70                         goto next;
71
72                 if (!streq(type, "Mains"))
73                         goto next;
74
75                 if (!(online = udev_device_get_sysattr_value(d, "online")))
76                         goto next;
77
78                 if (streq(online, "1")) {
79                         found_online = true;
80                         break;
81                 } else if (streq(online, "0"))
82                         found_offline = true;
83
84         next:
85                 udev_device_unref(d);
86         }
87
88         r = found_online || !found_offline;
89
90 finish:
91         if (e)
92                 udev_enumerate_unref(e);
93
94         if (udev)
95                 udev_unref(udev);
96
97         return r;
98 }