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.9 2005/06/15 21:29:21 liamfoy Exp $
36 * Dedicated to my grandfather Peter Foy. Goodnight...
40 #include <sys/ioctl.h>
41 #include <sys/types.h>
44 #include <machine/apm_bios.h>
55 #define APMUNKNOWN 255 /* Unknown value. */
56 #define AC_LINE_IN 1 /* AC Line Status values. */
59 #define APM_DEVICE "/dev/apm" /* Default APM Device. */
60 #define ERR_OUT 1 /* Values for error writing. */
62 #define DEFAULT_ALERT 10 /* Default alert is 10%. */
65 int alert_per; /* Percentage to alert user on */
66 int alert_time; /* User can also alert when there is only X amount of minutes left */
67 int alert_status; /* Alert when battery is either high, low, critical */
68 const char *apm_dev; /* APM Device */
69 const char *exec_cmd; /* Command to execute if desired */
76 static int check_percent(int);
77 static int check_stat(int);
78 static int check_time(int);
79 static int get_apm_info(struct apm_info *, int, const char *, int);
80 static void execute_cmd(const char *, int *);
81 static void write_emerg(const char *, int *);
82 static void usage(void); __dead2
88 fprintf(stderr, "usage: battd [-dEhT] [-p percent] [-t minutes] [-s status]\n"
89 " [-f device] [-e command] [-c seconds]\n");
91 fprintf(stderr, "usage: battd [-EhT] [-p percent] [-t minutes] [-s status]\n"
92 " [-f device] [-e command] [-c seconds]\n");
98 check_percent(int apm_per)
100 if (apm_per < 0 || apm_per >= APMUNKNOWN || apm_per > 100)
107 check_time(int apm_time)
116 check_stat(int apm_stat)
118 if (apm_stat > 3 || apm_stat < 0)
124 /* Fetch battery information */
126 get_apm_info(struct apm_info *ai, int fp_dev, const char *apm_dev, int err_to)
128 if (ioctl(fp_dev, APMIO_GETINFO, ai) == -1) {
130 err(1, "ioctl(APMIO_GETINFO) device: %s", apm_dev);
132 syslog(LOG_ERR, "ioctl(APMIO_GETINFO) device: %s: %m ",
141 /* Execute command. */
143 execute_cmd(const char *exec_cmd, int *exec_cont)
148 if (exec_cmd != NULL) {
149 if ((pid = fork()) == -1) {
150 /* Here fork failed */
156 syslog(LOG_ERR, "fork failed: %m");
157 } else if (pid == 0) {
158 execl("/bin/sh", "/bin/sh", "-c", exec_cmd, NULL);
161 while (waitpid(pid, &status, 0) != pid)
163 if (WEXITSTATUS(status)) {
166 warnx("child exited with code %d", status);
169 syslog(LOG_ERR, "child exited with code %d", status);
179 write_emerg(const char *eme_msg, int *warn_cont)
181 if (*warn_cont == 0) {
182 openlog("battd", LOG_EMERG, LOG_CONSOLE);
183 syslog(LOG_ERR, "%s\n", eme_msg);
188 /* Check given numerical arguments. */
190 getnum(const char *str)
196 val = strtol(str, &ep, 10);
198 err(1, "strtol failed: %s", str);
200 if (str == ep || *ep != '\0')
201 errx(1, "invalid value: %s", str);
203 if (val > INT_MAX || val < INT_MIN) {
205 errc(1, errno, "getnum failed:");
212 main(int argc, char **argv)
214 struct battd_conf battd_options, *opts;
216 int fp_device, exec_cont;
217 int per_warn_cont, time_warn_cont, stat_warn_cont;
218 int check_sec, time_def_alert, def_warn_cont;
222 opts = &battd_options;
225 * As default, we sleep for 30 seconds before
226 * we next call get_apm_info and do the rounds.
227 * The lower the value, the more accurate. Very
228 * low values could cause a decrease in system
229 * performance. We recommend about 30 seconds.
234 exec_cont = per_warn_cont = stat_warn_cont = 0;
235 time_warn_cont = time_def_alert = def_warn_cont = 0;
238 opts->alert_time = 0;
239 opts->alert_status = -1;
240 opts->exec_cmd = NULL;
241 opts->apm_dev = APM_DEVICE;
243 while ((c = getopt(argc, argv, "de:Ep:s:c:f:ht:T")) != -1) {
246 /* Parse the check battery interval. */
247 check_sec = getnum(optarg);
249 errx(1, "the interval for checking battery"
250 "status must be greater than 0.");
259 /* Command to execute. */
260 opts->exec_cmd = optarg;
263 /* Only execute once when any condition has been met. */
267 /* Don't use /dev/apm use optarg. */
268 opts->apm_dev = optarg;
271 /* Print usage and be done! */
276 * Parse percentage to alert on and enable
277 * battd to monitor the battery percentage.
279 opts->alert_per = getnum(optarg);
280 if (opts->alert_per <= 0 || opts->alert_per > 100)
281 errx(1, "Battery percentage to alert on must be "
282 "greater than 0 and less than 100.");
286 * Parse status to alert on and enable
287 * battd to monitor the battery status.
288 * We also accept 'high', 'HIGH' or 'HiGh'.
290 if (strcasecmp(optarg, "high") == 0)
291 opts->alert_status = 0; /* High */
292 else if (strcasecmp(optarg, "low") == 0)
293 opts->alert_status = 1; /* Low */
294 else if (strcasecmp(optarg, "critical") == 0)
295 opts->alert_status = 2; /* Critical (mental) */
297 /* No idea, see what we have. */
298 opts->alert_status = getnum(optarg);
299 if (opts->alert_status < 0 || opts->alert_status > 2)
300 errx(1, "Alert status must be between 0 and 2.");
305 * Parse time to alert on and enable
306 * battd to monitor the time percentage.
308 opts->alert_time = getnum(optarg);
309 if (opts->alert_time <= 0)
310 errx(1, "Alert time must be greater than 0 minutes.");
320 if ((fp_device = open(opts->apm_dev, O_RDONLY)) < 0)
321 err(1, "open failed: %s", opts->apm_dev);
324 * Before we become a daemon, first check whether
325 * the actual function requested is supported. If
326 * not, exit and let the user know.
330 get_apm_info(&ai, fp_device, opts->apm_dev, ERR_OUT);
332 if (opts->alert_per >= 0)
333 if (check_percent(ai.ai_batt_life))
334 errx(1, "invalid/unknown percentage(%d) returned from %s",
335 ai.ai_batt_life, opts->apm_dev);
337 if (opts->alert_time || time_def_alert)
338 if (check_time(ai.ai_batt_time))
339 errx(1, "invalid/unknown time(%d) returned from %s",
340 ai.ai_batt_time, opts->apm_dev);
342 if (opts->alert_status)
343 if (check_stat(ai.ai_batt_stat))
344 errx(1, "invalid/unknown status(%d) returned from %s",
345 ai.ai_batt_stat, opts->apm_dev);
351 if (daemon(0, 0) == -1)
352 err(1, "daemon failed");
358 if (get_apm_info(&ai, fp_device, opts->apm_dev,
360 f_debug ? ERR_OUT : SYSLOG_OUT))
364 /* Recoverable - sleep for check_sec seconds */
367 /* If we have power, reset the warning values. */
368 if (ai.ai_acline == AC_LINE_IN) {
376 * If the battery has main power (AC lead is plugged in)
377 * we skip and sleep for check_sec seconds.
379 if (ai.ai_acline != AC_LINE_IN) {
381 * Battery has no mains power. Time to do
386 * Main Processing loop
387 * --------------------
388 * 1. Check battery percentage if enabled.
389 * 2. Check battery time remaining if enabled.
390 * 3. Check battery status if enabled.
391 * 4. Deal with default alerts.
394 /* 1. Check battery percentage if enabled */
395 if (opts->alert_per) {
396 if (check_percent(ai.ai_batt_life)) {
399 printf("Invalid percentage (%d) received from %s.\n",
400 ai.ai_batt_life, opts->apm_dev);
403 syslog(LOG_ERR, "Invalid percentage received from %s.",
411 if (ai.ai_batt_life <= (u_int)opts->alert_per) {
412 tmp = (ai.ai_batt_life == (u_int)opts->alert_per);
413 snprintf(msg, sizeof(msg), "battery has %s %d%%\n",
414 tmp ? "reached" : "fallen below",
416 execute_cmd(opts->exec_cmd, &exec_cont);
417 write_emerg(msg, &per_warn_cont);
421 /* 2. Check battery time remaining if enabled */
422 if (opts->alert_time) {
423 if (check_time(ai.ai_batt_time)) {
426 printf("Invalid time value (%d) received from %s.\n",
427 ai.ai_batt_time, opts->apm_dev);
430 syslog(LOG_ERR, "Invalid time value received from %s.",
438 if (ai.ai_batt_time <= (opts->alert_time * SECONDS)) {
440 char tmp_time[sizeof "tt:tt:tt" + 1];
446 snprintf(tmp_time, sizeof(tmp_time), "%d:%d:%d\n", h, m, s);
447 tmp = (ai.ai_batt_time == opts->alert_time);
448 snprintf(msg, sizeof(msg), "battery has %s %d(%s) minutes"
449 "remaining\n", tmp ? "reached" : "fallen below",
450 ai.ai_batt_time / SECONDS, tmp_time);
451 execute_cmd(opts->exec_cmd, &exec_cont);
452 write_emerg(msg, &time_warn_cont);
456 /* 3. Check battery status if enabled */
457 if (opts->alert_status != -1) {
458 if (check_stat(ai.ai_batt_stat)) {
461 printf("Invalid status value (%d) received from %s.\n",
462 ai.ai_batt_life, opts->apm_dev);
465 syslog(LOG_ERR, "Invalid status value received from %s.",
473 if (ai.ai_batt_stat >= (u_int)opts->alert_status) {
474 const char *batt_status[] = {"high", "low", "critical"};
476 tmp = (ai.ai_batt_stat == (u_int)opts->alert_status);
477 snprintf(msg, sizeof(msg), "battery has %s '%s' status\n",
478 tmp ? "reached" : "fallen below",
479 batt_status[ai.ai_batt_stat]);
480 execute_cmd(opts->exec_cmd, &exec_cont);
481 write_emerg(msg, &stat_warn_cont);
485 /* 4. Deal with default alerts. */
486 if (time_def_alert) {
487 if (check_time(ai.ai_batt_time)) {
488 if (ai.ai_batt_time <= DEFAULT_ALERT * SECONDS) {
489 snprintf(msg, sizeof(msg), "WARNING! battery only"
490 "has roughly %d minutes remaining!\n",
491 ai.ai_batt_time / SECONDS);
492 write_emerg(msg, NULL);
497 if (ai.ai_batt_life <= DEFAULT_ALERT) {
498 tmp = (ai.ai_batt_life == DEFAULT_ALERT);
499 snprintf(msg, sizeof(msg), "WARNING! battery has %s %d%%\n",
500 tmp ? "reached" : "fallen below",
503 execute_cmd(opts->exec_cmd, &exec_cont);
504 write_emerg(msg, &def_warn_cont);
509 /* Sleep time! Default is 30 seconds */