From owner-freebsd-bugs@FreeBSD.ORG Thu May 25 12:30:24 2006 Return-Path: X-Original-To: freebsd-bugs@hub.freebsd.org Delivered-To: freebsd-bugs@hub.freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id E146D16A41F for ; Thu, 25 May 2006 12:30:24 +0000 (UTC) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (freefall.freebsd.org [216.136.204.21]) by mx1.FreeBSD.org (Postfix) with ESMTP id 302F243D49 for ; Thu, 25 May 2006 12:30:24 +0000 (GMT) (envelope-from gnats@FreeBSD.org) Received: from freefall.freebsd.org (gnats@localhost [127.0.0.1]) by freefall.freebsd.org (8.13.4/8.13.4) with ESMTP id k4PCUO0i043846 for ; Thu, 25 May 2006 12:30:24 GMT (envelope-from gnats@freefall.freebsd.org) Received: (from gnats@localhost) by freefall.freebsd.org (8.13.4/8.13.4/Submit) id k4PCUN64043845; Thu, 25 May 2006 12:30:23 GMT (envelope-from gnats) Resent-Date: Thu, 25 May 2006 12:30:23 GMT Resent-Message-Id: <200605251230.k4PCUN64043845@freefall.freebsd.org> Resent-From: FreeBSD-gnats-submit@FreeBSD.org (GNATS Filer) Resent-To: freebsd-bugs@FreeBSD.org Resent-Reply-To: FreeBSD-gnats-submit@FreeBSD.org, Beat Gätzi Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id A6EC016A4CD for ; Thu, 25 May 2006 12:24:05 +0000 (UTC) (envelope-from beat@chruetertee.ch) Received: from mail21.bluewin.ch (mail21.bluewin.ch [195.186.18.66]) by mx1.FreeBSD.org (Postfix) with ESMTP id 4016243D4C for ; Thu, 25 May 2006 12:24:05 +0000 (GMT) (envelope-from beat@chruetertee.ch) Received: from [83.79.177.162] (83.79.177.162) by mail21.bluewin.ch (Bluewin 7.2.073) id 4461DC32003CBA75 for FreeBSD-gnats-submit@freebsd.org; Thu, 25 May 2006 12:24:03 +0000 Received: by _HOSTNAME_ (sSMTP sendmail emulation); Thu, 25 May 2006 14:24:01 +0200 Message-Id: <4461DC32003CBA75@mail21.bluewin.ch> (added by postmaster@bluewin.ch) Date: Thu, 25 May 2006 14:24:01 +0200 From: "Beat Gaetzi" To: FreeBSD-gnats-submit@FreeBSD.org X-Send-Pr-Version: 3.113 Cc: Subject: bin/97896: [patch] proper system shut down on low battery level X-BeenThere: freebsd-bugs@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: Beat Gätzi List-Id: Bug reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 25 May 2006 12:30:25 -0000 >Number: 97896 >Category: bin >Synopsis: [patch] proper system shut down on low battery level >Confidential: no >Severity: non-critical >Priority: low >Responsible: freebsd-bugs >State: open >Quarter: >Keywords: >Date-Required: >Class: change-request >Submitter-Id: current-users >Arrival-Date: Thu May 25 12:30:23 GMT 2006 >Closed-Date: >Last-Modified: >Originator: Beat Gätzi >Release: FreeBSD 6.1-RELEASE i386 >Organization: >Environment: System: FreeBSD daedalus.network.local 6.1-RELEASE FreeBSD 6.1-RELEASE #0: Sat May 6 12:37:33 CEST 2006 beat@daedalus.network.local:/usr/obj/usr/src/sys/BEASTIE i386 >Description: If the notebook battery is empty, the system switches off without a proper system shut down. The attached patch adds two new options to the powerd which give the user a warning if the battery level is low and shut down the system if the user doesn't care about the warning. >How-To-Repeat: >Fix: --- powerd.patch begins here --- diff -ruN /usr/src/usr.sbin/powerd.ori/powerd.8 /usr/src/usr.sbin/powerd/powerd.8 --- /usr/src/usr.sbin/powerd.ori/powerd.8 Thu May 25 13:16:20 2006 +++ /usr/src/usr.sbin/powerd/powerd.8 Thu May 25 13:04:53 2006 @@ -39,7 +39,9 @@ .Op Fl p Ar ival .Op Fl P Ar pidfile .Op Fl r Ar percent +.Op Fl s Ar percent .Op Fl v +.Op Fl w Ar percent .Sh DESCRIPTION The .Nm @@ -92,11 +94,17 @@ adaptive mode should consider the CPU running and increase performance. The default is 65% or lower. +.It Fl s Ar percent +Specifies the battery level on which a warning message will be sent to all +user terminals. .It Fl v Verbose mode. Messages about power changes will be printed to stdout and .Nm will operate in the foreground. +.It Fl w Ar percent +Specifies the battery level on which the system will be shut down +automatically. .El .Sh SEE ALSO .Xr acpi 4 , diff -ruN /usr/src/usr.sbin/powerd.ori/powerd.c /usr/src/usr.sbin/powerd/powerd.c --- /usr/src/usr.sbin/powerd.ori/powerd.c Thu May 25 13:16:20 2006 +++ /usr/src/usr.sbin/powerd/powerd.c Thu May 25 13:39:24 2006 @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include @@ -39,11 +40,14 @@ #include #include #include +#include #include +#include #include #include #include #include +#include #include #ifdef USE_APM @@ -357,7 +361,7 @@ { fprintf(stderr, -"usage: powerd [-v] [-a mode] [-b mode] [-i %%] [-n mode] [-p ival] [-r %%] [-P pidfile]\n"); +"usage: powerd [-v] [-a mode] [-b mode] [-i %%] [-n mode] [-p ival] [-r %%] [-s %%] [-w %%] [-P pidfile]\n"); exit(1); } @@ -368,26 +372,33 @@ fd_set fdset; int nfds; struct pidfh *pfh = NULL; + char wcmd[MAXPATHLEN + 4]; const char *pidfile = NULL; long idle, total; - int curfreq, *freqs, i, *mwatts, numfreqs; - int ch, mode, mode_ac, mode_battery, mode_none; + int batt, curfreq, *freqs, i, *mwatts, numfreqs; + int ch, mode, mode_ac, mode_battery, mode_none, sflag; + int batt_shutdown, batt_warning, shutdown_status; uint64_t mjoules_used; size_t len; + FILE *pf; /* Default mode for all AC states is adaptive. */ mode_ac = mode_battery = mode_none = MODE_ADAPTIVE; cpu_running_mark = DEFAULT_ACTIVE_PERCENT; cpu_idle_mark = DEFAULT_IDLE_PERCENT; poll_ival = DEFAULT_POLL_INTERVAL; + batt_shutdown = 0; + batt_warning = 0; mjoules_used = 0; + sflag = 0; + shutdown_status = 0; vflag = 0; /* User must be root to control frequencies. */ if (geteuid() != 0) errx(1, "must be root to run"); - while ((ch = getopt(argc, argv, "a:b:i:n:p:P:r:v")) != EOF) + while ((ch = getopt(argc, argv, "a:b:i:n:p:P:r:s:vw:")) != EOF) switch (ch) { case 'a': parse_mode(optarg, &mode_ac, ch); @@ -424,6 +435,28 @@ usage(); } break; + case 's': + batt_shutdown = atoi(optarg); + sflag = 1; + batt_warning = batt_shutdown + 2; + if (batt_shutdown < 0 || batt_shutdown > 100) { + warnx("%d is not a valid percent", + batt_shutdown); + usage(); + } + break; + case 'w': + batt_warning = atoi(optarg); + if (batt_warning < 0 || batt_warning> 100) { + warnx("%d is not a valid percent", + batt_warning); + usage(); + } + if (batt_warning <= batt_shutdown) { + warnx("battery warning value must be greater than battery shut down value"); + usage(); + } + break; case 'v': vflag = 1; break; @@ -446,6 +479,13 @@ len = 4; if (sysctlnametomib("dev.cpu.0.freq_levels", levels_mib, &len)) err(1, "lookup freq_levels"); + if (sflag == 1) { + len = sizeof(batt); + if (sysctlbyname("hw.acpi.battery.life", &batt, &len, NULL, 0)) { + err(1, "lookup battery life"); + sflag = 0; + } + } /* Check if we can read the idle time and supported freqs. */ if (read_usage_times(NULL, NULL)) @@ -519,6 +559,40 @@ break; default: errx(1, "invalid AC line status %d", acline_status); + } + + if (sflag == 1) { + if (acline_status == SRC_BATTERY) { + len = sizeof(batt); + if (sysctlbyname("hw.acpi.battery.life", &batt, &len, NULL, 0)) + err(1, "lookup battery life"); + if (batt <= batt_warning) { + if (shutdown_status == 0) { + if (vflag) + warnx("Running out of battery! System will shut down automatically"); + (void)snprintf(wcmd, sizeof(wcmd), "%s -n", _PATH_WALL); + if (!(pf = popen(wcmd, "w"))) { + syslog(LOG_ERR, "powerd: can't find %s: %m", + _PATH_WALL); + } + (void)fprintf(pf, + "\nReconnect power IMMEDIATELY or system will shut down\n\n"); + (void)pclose(pf); + shutdown_status = 1; + } + } + if (batt <= batt_shutdown) { + if (shutdown_status == 1) { + if (vflag) + warnx("The System is shutting down"); + sflag = 0; + syslog(LOG_ERR, + "system shut down by powerd because of low battery"); + system("shutdown -p now Automatic system shut down by powerd &"); + shutdown_status = 2; + } + } + } } /* Read the current frequency. */ --- powerd.patch ends here --- >Release-Note: >Audit-Trail: >Unformatted: