2 * Copyright (c) 2003, 2005 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Liam J. Foy <liamfoy@dragonflybsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $DragonFly: src/usr.sbin/battd/battd.c,v 1.3 2005/02/01 19:51:28 liamfoy Exp $
38 #include <sys/ioctl.h>
39 #include <sys/types.h>
42 #include <machine/apm_bios.h>
54 #define APMUNKNOWN 255 /* Unknown value. */
55 #define AC_LINE_IN 1 /* AC Line Status values. */
58 #define APM_DEVICE "/dev/apm" /* Default APM Device. */
59 #define ERR_OUT 1 /* Values for error writing. */
61 #define DEFAULT_ALERT 10 /* Default alert is 10%. */
64 int alert_per; /* Percentage to alert user on */
65 int alert_time; /* User can also alert when there is only X amount of minutes left */
66 int alert_status; /* Alert when battery is either high, low, critical */
67 const char *apm_dev; /* APM Device */
68 const char *exec_cmd; /* Command to execute if desired */
71 static int check_percent(int);
72 static int check_stat(int);
73 static int check_time(int);
74 static int get_apm_info(struct apm_info *, int, const char *, int);
75 static void execute_cmd(const char *, int *);
76 static void write_emerg(const char *, int *);
77 static void usage(void); __dead2
82 fprintf(stderr, "usage: battd [-dEhT] [-p percent] [-t minutes] [-s status]\n"
83 " [-f device] [-e command] [-c seconds]\n");
88 check_percent(int apm_per)
90 if (apm_per < 0 || apm_per >= APMUNKNOWN || apm_per > 100)
97 check_time(int apm_time)
106 check_stat(int apm_stat)
108 if (apm_stat > 3 || apm_stat < 0)
114 /* Fetch battery information */
116 get_apm_info(struct apm_info *ai, int fp_dev, const char *apm_dev, int err_to)
118 if (ioctl(fp_dev, APMIO_GETINFO, ai) == -1) {
120 err(1, "ioctl(APMIO_GETINFO) Device: %s", apm_dev);
122 syslog(LOG_ERR, "ioctl(APMIO_GETINFO) device: %s : %m ",
131 /* Execute command. */
133 execute_cmd(const char *exec_cmd, int *exec_cont)
138 if (exec_cmd != NULL) {
139 if ((pid = fork()) == -1) {
140 /* Here fork failed */
141 syslog(LOG_ERR, "fork failed: %m");
142 } else if (pid == 0) {
143 execl("/bin/sh", "/bin/sh", "-c", exec_cmd, NULL);
146 while (waitpid(pid, &status, 0) != pid)
148 if (WEXITSTATUS(status))
149 syslog(LOG_ERR, "child exited with code %d", status);
158 write_emerg(const char *eme_msg, int *warn_cont)
160 if (*warn_cont == 0) {
161 openlog("battd", LOG_EMERG, LOG_CONSOLE);
162 syslog(LOG_ERR, "%s\n", eme_msg);
167 /* Check given numerical arguments. */
169 getnum(const char *str)
175 val = strtol(str, &ep, 10);
177 err(1, "strtol failed: %s", str);
179 if (str == ep || *ep != '\0')
180 errx(1, "invalid value: %s", str);
182 if (val > INT_MAX || val < INT_MIN) {
184 errc(1, errno, "getnum failed:");
191 main(int argc, char **argv)
193 struct battd_conf battd_options, *opts;
195 int fp_device, exec_cont, f_debug;
196 int per_warn_cont, time_warn_cont, stat_warn_cont;
197 int check_sec, time_def_alert, def_warn_cont;
201 opts = &battd_options;
204 * As default, we sleep for 30 seconds before
205 * we next call get_apm_info and do the rounds.
206 * The lower the value, the more accurate. Very
207 * low values could cause a decrease in system
208 * performance. We recommend about 30 seconds.
213 exec_cont = f_debug = per_warn_cont = stat_warn_cont = 0;
214 time_warn_cont = time_def_alert = def_warn_cont = 0;
217 opts->alert_time = 0;
218 opts->alert_status = -1;
219 opts->exec_cmd = NULL;
220 opts->apm_dev = APM_DEVICE;
222 while ((c = getopt(argc, argv, "de:Ep:s:c:f:ht:T")) != -1) {
225 /* Parse the check battery interval. */
226 check_sec = getnum(optarg);
228 errx(1, "the interval for checking battery"
229 "status must be greater than 0.");
236 /* Command to execute. */
237 opts->exec_cmd = optarg;
240 /* Only execute once when any condition has been met. */
244 /* Don't use /dev/apm use optarg. */
245 opts->apm_dev = optarg;
248 /* Print usage and be done! */
253 * Parse percentage to alert on and enable
254 * battd to monitor the battery percentage.
256 opts->alert_per = getnum(optarg);
257 if (opts->alert_per <= 0 || opts->alert_per > 100)
258 errx(1, "Battery percentage to alert on must be "
259 "greater than 0 and less than 100.");
263 * Parse status to alert on and enable
264 * battd to monitor the battery status.
265 * We also accept 'high', 'HIGH' or 'HiGh'.
267 if (strcasecmp(optarg, "high") == 0)
268 opts->alert_status = 0; /* High */
269 else if (strcasecmp(optarg, "low") == 0)
270 opts->alert_status = 1; /* Low */
271 else if (strcasecmp(optarg, "critical") == 0)
272 opts->alert_status = 2; /* Critical (mental) */
274 /* No idea, see what we have. */
275 opts->alert_status = getnum(optarg);
276 if (opts->alert_status < 0 || opts->alert_status > 2)
277 errx(1, "Alert status must be between 0 and 2.");
282 * Parse time to alert on and enable
283 * battd to monitor the time percentage.
285 opts->alert_time = getnum(optarg);
286 if (opts->alert_time <= 0)
287 errx(1, "Alert time must be greater than zero.");
297 if ((fp_device = open(opts->apm_dev, O_RDONLY)) < 0)
298 err(1, "open failed: %s", opts->apm_dev);
301 * Before we become a daemon, first check whether
302 * the actual function requested is supported. If
303 * not, exit and let the user know.
307 get_apm_info(&ai, fp_device, opts->apm_dev, ERR_OUT);
309 if (opts->alert_per >= 0)
310 if (check_percent(ai.ai_batt_life))
311 errx(1, "invalid/unknown percentage(%d) returned from apm device",
314 if (opts->alert_time)
315 if (check_time(ai.ai_batt_time))
316 errx(1, "invalid/unknown time(%d) returned from apm device",
319 if (opts->alert_status)
320 if (check_stat(ai.ai_batt_stat))
321 errx(1, "invalid/unknown status(%d) returned from apm device",
326 if (daemon(0, 0) == -1)
327 err(1, "daemon failed");
331 if (get_apm_info(&ai, fp_device, opts->apm_dev,
332 f_debug ? ERR_OUT : SYSLOG_OUT))
333 /* Recoverable - sleep for check_sec seconds */
336 /* If we have power, reset the warning values. */
337 if (ai.ai_acline == AC_LINE_IN) {
345 * If the battery has main power (AC lead is plugged in)
346 * we skip and sleep for check_sec seconds.
348 if (ai.ai_acline != AC_LINE_IN) {
350 * Battery has no mains power. Time to do
355 * Main Processing loop
356 * --------------------
357 * 1. Check battery percentage if enabled.
358 * 2. Check battery time remaining if enabled.
359 * 3. Check battery status if enabled.
360 * 4. Deal with default alerts.
363 /* 1. Check battery percentage if enabled */
364 if (opts->alert_per) {
365 if (check_percent(ai.ai_batt_life)) {
367 printf("Invaild percentage (%d) recieved from %s.\n",
368 ai.ai_batt_life, opts->apm_dev);
370 syslog(LOG_ERR, "Invaild percentage recieved from %s.",
376 if (ai.ai_batt_life <= (u_int)opts->alert_per) {
377 tmp = (ai.ai_batt_life == (u_int)opts->alert_per);
378 snprintf(msg, sizeof(msg), "battery has %s %d%%\n",
379 tmp ? "reached" : "fell below",
381 execute_cmd(opts->exec_cmd, &exec_cont);
382 write_emerg(msg, &per_warn_cont);
386 /* 2. Check battery time remaining if enabled */
387 if (opts->alert_time) {
388 if (check_time(ai.ai_batt_time)) {
390 printf("Invaild time value (%d) recieved from %s.\n",
391 ai.ai_batt_time, opts->apm_dev);
393 syslog(LOG_ERR, "Invaild time value recieved from %s.",
399 if (ai.ai_batt_time <= (opts->alert_time * SECONDS)) {
401 char tmp_time[sizeof "tt:tt:tt" + 1];
407 snprintf(tmp_time, sizeof(tmp_time), "%d:%d:%d\n", h, m, s);
408 tmp = (ai.ai_batt_time == opts->alert_time);
409 snprintf(msg, sizeof(msg), "battery has %s %d(%s) minutes"
410 "remaining\n", tmp ? "reached" : "fell below",
411 ai.ai_batt_time / SECONDS, tmp_time);
412 execute_cmd(opts->exec_cmd, &exec_cont);
413 write_emerg(msg, &time_warn_cont);
417 /* 3. Check battery status if enabled */
418 if (opts->alert_status != -1) {
419 if (check_stat(ai.ai_batt_stat)) {
421 printf("Invaild status value (%d) recieved from %s.\n",
422 ai.ai_batt_life, opts->apm_dev);
424 syslog(LOG_ERR, "Invaild status value recieved from %s.",
430 if (ai.ai_batt_stat >= (u_int)opts->alert_status) {
431 const char *batt_status[] = {"'high'", "'low'", "'critical'"};
433 tmp = (ai.ai_batt_stat == (u_int)opts->alert_status);
434 snprintf(msg, sizeof(msg), "battery has %s '%s' status\n",
435 tmp ? "reached" : "fell below",
436 batt_status[ai.ai_batt_stat]);
437 execute_cmd(opts->exec_cmd, &exec_cont);
438 write_emerg(msg, &stat_warn_cont);
442 /* 4. Deal with default alerts. */
443 if (time_def_alert) {
444 if (check_time(ai.ai_batt_time)) {
445 if (ai.ai_batt_time <= DEFAULT_ALERT * SECONDS) {
446 snprintf(msg, sizeof(msg), "WARNING! battery only"
447 "has roughly %d minutes remaining!\n",
448 ai.ai_batt_time / SECONDS);
449 write_emerg(msg, NULL);
454 if (ai.ai_batt_life <= DEFAULT_ALERT) {
455 tmp = (ai.ai_batt_life == DEFAULT_ALERT);
456 snprintf(msg, sizeof(msg), "WARNING! battery has %s %d%%\n",
457 tmp ? "reached" : "fell below",
460 execute_cmd(opts->exec_cmd, &exec_cont);
461 write_emerg(msg, &def_warn_cont);
466 /* Sleep time! Default is 30 seconds */