-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbattmond.c
221 lines (197 loc) · 4.93 KB
/
battmond.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
* Battmond: A battery monitoring daemon.
*
* Author: Nikos Ntarmos <[email protected]>
* URL: https://github.com/ntarmos/battmond/
*
* $FreeBSD$
* $Id: battmond.c 54 2006-09-08 13:12:05Z ntarmos $
*/
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <grp.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sysexits.h>
#include <syslog.h>
#include <sys/ioctl.h>
#include <sys/param.h>
#include <unistd.h>
#include <libutil.h>
#include <dev/acpica/acpiio.h>
#define ACPIDEV "/dev/acpi"
char * acpidev = NULL;
#define BATT_WARN "Your battery power is running low. Please connect the \
power cord or save any unsaved work and halt the system.\n"
#define BATT_HALT "Your battery power is in critical level. \
Your system will now halt to preserve any unsaved work.\n"
#define BATT_SUSP "Your battery power is in critical level. \
Your system will now suspend to preserve any unsaved work.\n"
#ifdef DEBUG
#define logfunc fprintf
#define logtgt stderr
#else
#define logfunc syslog
#define logtgt LOG_ERR
#endif
static int have_warned = 0;
const char *pid_file = "/var/run/battmond.pid";
void oops(char * str)
{
if (errno)
logfunc(logtgt, "%s: %s", str, strerror(errno));
else
logfunc(logtgt, "%s", str);
if (acpidev != NULL)
free(acpidev);
exit(errno);
}
void _usage(char * argv0)
{
errno = 0;
fprintf(stderr, "Usage: %s [-pdiWHzh]\n", argv0);
exit(0);
}
#define usage() _usage(argv[0])
int main(int argc, char ** argv)
{
union acpi_battery_ioctl_arg battio;
int acpifd, ch;
int interval = 10; // Check every 10 seconds.
int warn = 10; // Percentage of charge to emit a warning.
int halt = 5; // Percentage of charge to halt the system.
char dosuspend = 0;
pid_t otherpid;
struct pidfh *pfh = NULL;
while ((ch = getopt(argc, argv, "p:d:i:W:H:zh")) != -1) {
switch (ch) {
case 'p':
pid_file = optarg;
break;
case 'd':
if (acpidev != NULL)
free(acpidev);
acpidev = strdup(optarg);
break;
case 'i':
interval = atoi(optarg);
if (interval <= 0)
oops("Error in interval value");
break;
case 'W':
warn = atoi(optarg);
if (warn < 0)
oops("Error in warning threshold value");
break;
case 'H':
halt = atoi(optarg);
if (halt < 0)
oops("Error in halt threshold value");
break;
case 'z':
dosuspend = 1;
break;
case 'h':
default:
usage();
}
}
if (acpidev == NULL)
acpidev = strdup(ACPIDEV);
if (warn <= halt)
oops ("Warning threshold is lower or equal to the halt threshold");
pfh = pidfile_open(pid_file, 0600, &otherpid);
if (pfh == NULL) {
if (errno == EEXIST) {
syslog(LOG_ERR, "%s already running, pid: %d",
getprogname(), otherpid);
exit(EX_OSERR);
}
syslog(LOG_WARNING, "pidfile_open() failed: %m");
}
#ifndef DEBUG
if (daemon(0, 0) < 0) {
pidfile_remove(pfh);
oops ("Can't spawn daemon process. Bailing out...");
}
#endif
if (pidfile_write(pfh) == -1) {
syslog(LOG_WARNING, "pidfile_write(): %m");
}
pidfile_close(pfh);
while (1) {
int total_cap = 0;
int units;
int unit = 0;
int num_discharging = 0, num_charging = 0;
if ((acpifd = open(acpidev, O_RDONLY)) == -1)
oops("Unable to open acpi device");
if (ioctl(acpifd, ACPIIO_BATT_GET_UNITS, &units) == -1) {
syslog(LOG_WARNING,
"Unable to retrieve battery count. Defaulting to probing approach...");
units = 5;
}
#ifdef DEBUG
fprintf(stderr, "%d battery unit%s detected\n", units, (units > 1) ? "s" : "");
#endif
for (unit = 0; unit < units; unit ++) {
bzero(&battio, sizeof(battio));
battio.unit = unit;
if (ioctl(acpifd, ACPIIO_BATT_GET_BATTINFO, &battio) != -1) {
#ifdef DEBUG
fprintf(stderr, "Battery unit %d: ", unit);
#endif
if (battio.battinfo.state != ACPI_BATT_STAT_NOT_PRESENT &&
battio.battinfo.cap != -1) {
#ifdef DEBUG
fprintf(stderr, "present%s (%d%%)\n", (battio.battinfo.state & ACPI_BATT_STAT_DISCHARG) ? "/discharging" : "", battio.battinfo.cap);
#endif
total_cap += battio.battinfo.cap;
if ((battio.battinfo.state & ACPI_BATT_STAT_DISCHARG)) {
num_discharging++;
} else {
num_charging++;
have_warned = 0;
}
} else {
#ifdef DEBUG
fprintf(stderr, "not present\n");
#endif
have_warned = 0;
}
}
}
#ifdef DEBUG
fprintf(stderr, "Total battery capacity: %d%%\n", total_cap);
#endif
if (num_discharging && !num_charging && total_cap > 0) {
if (total_cap <= halt) {
if (dosuspend) { // Suspend
syslog(LOG_EMERG, BATT_SUSP);
close(acpifd);
execl("/usr/sbin/acpiconf", "acpiconf", "-s3", NULL);
oops("execl");
} else { // Halt
syslog(LOG_EMERG, BATT_HALT);
close(acpifd);
execl("/sbin/halt", "halt", "-p", NULL);
oops("execl");
}
}
else if (total_cap <= warn) {
if (!have_warned) {
syslog(LOG_ALERT, BATT_WARN);
have_warned = 1;
}
} else {
have_warned = 0;
}
}
close(acpifd);
sleep(interval);
}
return 0;
}