apm.c revision 1.11 1 /* $NetBSD: apm.c,v 1.11 2001/09/13 11:01:49 enami Exp $ */
2
3 /*-
4 * Copyright (c) 1996 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by John Kohl.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 #include <sys/types.h>
40 #include <sys/ioctl.h>
41 #include <sys/socket.h>
42 #include <sys/time.h>
43 #include <sys/un.h>
44
45 #include <machine/apmvar.h>
46
47 #include <err.h>
48 #include <errno.h>
49 #include <fcntl.h>
50 #include <stdio.h>
51 #include <stdlib.h>
52 #include <string.h>
53 #include <unistd.h>
54
55 #include "pathnames.h"
56 #include "apm-proto.h"
57
58 #define FALSE 0
59 #define TRUE 1
60
61 void usage(void);
62 void zzusage(void);
63 int do_zzz(const char *, enum apm_action);
64 int open_socket(const char *);
65 int send_command(int, struct apm_command *, struct apm_reply *);
66
67 void
68 usage(void)
69 {
70
71 fprintf(stderr,"usage: %s [-v] [-z | -S] [-slmba] [-f socket]\n",
72 getprogname());
73 exit(1);
74 }
75
76 void
77 zzusage(void)
78 {
79
80 fprintf(stderr,"usage: %s [-z | -S] [-f socket]\n",
81 getprogname());
82 exit(1);
83 }
84
85 int
86 send_command(int fd,
87 struct apm_command *cmd,
88 struct apm_reply *reply)
89 {
90
91 /* send a command to the apm daemon */
92 cmd->vno = APMD_VNO;
93
94 if (send(fd, cmd, sizeof(*cmd), 0) == sizeof(*cmd)) {
95 if (recv(fd, reply, sizeof(*reply), 0) != sizeof(*reply)) {
96 warn("invalid reply from APM daemon\n");
97 return (1);
98 }
99 } else {
100 warn("invalid send to APM daemon");
101 return (1);
102 }
103 return (0);
104 }
105
106 int
107 do_zzz(const char *pn, enum apm_action action)
108 {
109 struct apm_command command;
110 struct apm_reply reply;
111 int fd;
112
113 switch (action) {
114 case NONE:
115 case SUSPEND:
116 command.action = SUSPEND;
117 break;
118 case STANDBY:
119 command.action = STANDBY;
120 break;
121 default:
122 zzusage();
123 }
124
125 fd = open_socket(pn);
126 if (fd == -1)
127 err(1, "cannot open connection to APM daemon");
128 printf("Suspending system...\n");
129 exit(send_command(fd, &command, &reply));
130 }
131
132 int
133 open_socket(const char *sockname)
134 {
135 struct sockaddr_un s_un;
136 int sock, errr;
137
138 sock = socket(AF_LOCAL, SOCK_STREAM, 0);
139 if (sock == -1)
140 err(1, "cannot create local socket");
141
142 s_un.sun_family = AF_LOCAL;
143 strncpy(s_un.sun_path, sockname, sizeof(s_un.sun_path));
144 s_un.sun_len = SUN_LEN(&s_un);
145 if (connect(sock, (struct sockaddr *)&s_un, s_un.sun_len) == -1) {
146 errr = errno;
147 close(sock);
148 errno = errr;
149 return (-1);
150 }
151 return (sock);
152 }
153
154 int
155 main(int argc, char *argv[])
156 {
157 struct apm_command command;
158 struct apm_reply reply;
159 struct apm_power_info *api = &reply.batterystate;
160 char *sockname = _PATH_APM_SOCKET;
161 enum apm_action action = NONE;
162 int ch, doac, dobstate, domin, dopct, dostatus, fd, nodaemon,
163 rval, verbose;
164
165 doac = dobstate = domin = dopct = dostatus = nodaemon =
166 verbose = FALSE;
167 while ((ch = getopt(argc, argv, "lmbvadsSzf:d")) != -1)
168 switch (ch) {
169 case 'v':
170 verbose = TRUE;
171 break;
172 case 'f':
173 sockname = optarg;
174 break;
175 case 'z':
176 if (action != NONE)
177 usage();
178 action = SUSPEND;
179 break;
180 case 'S':
181 if (action != NONE)
182 usage();
183 action = STANDBY;
184 break;
185 case 's':
186 if (action != NONE && action != GETSTATUS)
187 usage();
188 dostatus = TRUE;
189 action = GETSTATUS;
190 break;
191 case 'b':
192 if (action != NONE && action != GETSTATUS)
193 usage();
194 dobstate = TRUE;
195 action = GETSTATUS;
196 break;
197 case 'l':
198 if (action != NONE && action != GETSTATUS)
199 usage();
200 dopct = TRUE;
201 action = GETSTATUS;
202 break;
203 case 'm':
204 if (action != NONE && action != GETSTATUS)
205 usage();
206 domin = TRUE;
207 action = GETSTATUS;
208 break;
209 case 'a':
210 if (action != NONE && action != GETSTATUS)
211 usage();
212 doac = TRUE;
213 action = GETSTATUS;
214 break;
215 case 'd':
216 nodaemon = TRUE;
217 break;
218 case '?':
219 default:
220 usage();
221 }
222
223 if (strcmp(getprogname(), "zzz") == 0)
224 exit(do_zzz(sockname, action));
225
226 if (nodaemon)
227 fd = -1;
228 else
229 fd = open_socket(sockname);
230
231 switch (action) {
232 case NONE:
233 verbose = doac = dopct = domin = dobstate = dostatus = TRUE;
234 action = GETSTATUS;
235 /* fallthrough */
236 case GETSTATUS:
237 if (fd == -1) {
238 /* open the device directly and get status */
239 fd = open(_PATH_APM_NORMAL, O_RDONLY);
240 if (fd == -1) {
241 err(1, "cannot contact APM daemon and "
242 "cannot open "
243 _PATH_APM_NORMAL);
244 }
245 if (ioctl(fd, APM_IOC_GETPOWER,
246 &reply.batterystate) == 0)
247 goto printval;
248 }
249 case SUSPEND:
250 case STANDBY:
251 if (nodaemon && fd == -1) {
252 fd = open(_PATH_APM_CTLDEV, O_RDWR);
253 if (fd == -1)
254 err(1, "cannot open APM control device "
255 _PATH_APM_CTLDEV);
256 sync();
257 sync();
258 sleep(1);
259 if (ioctl(fd, action == SUSPEND ?
260 APM_IOC_SUSPEND : APM_IOC_STANDBY, 0) == -1)
261 err(1, "cannot enter requested power state");
262 printf("System will enter %s in a moment.\n",
263 action == SUSPEND ? "suspend mode" :
264 "standby mode");
265 exit(0);
266 } else if (fd == -1)
267 err(1, "cannot contact APM daemon at socket "
268 _PATH_APM_SOCKET);
269 command.action = action;
270 break;
271 default:
272 usage();
273 }
274
275 if ((rval = send_command(fd, &command, &reply)) == 0) {
276 switch (action) {
277 case GETSTATUS:
278 printval:
279 if (verbose) {
280 if (dobstate)
281 printf("Battery charge state: %s\n",
282 battstate(api->battery_state));
283 if (dopct || domin) {
284 printf("Battery remaining: ");
285 if (dopct)
286 printf("%d percent",
287 api->battery_life);
288 if (dopct && domin)
289 printf(" (");
290 if (domin)
291 printf("%d minutes",
292 api->minutes_left);
293 if (dopct && domin)
294 printf(")");
295 printf("\n");
296 }
297 if (doac)
298 printf("A/C adapter state: %s\n",
299 ac_state(api->ac_state));
300 if (dostatus)
301 printf("Power management enabled\n");
302 if (api->nbattery) {
303 printf("Number of batteries: %u\n",
304 api->nbattery);
305 }
306 } else {
307 if (dobstate)
308 printf("%d\n", api->battery_state);
309 if (dopct)
310 printf("%d\n", api->battery_life);
311 if (domin)
312 printf("%d\n", api->minutes_left);
313 if (doac)
314 printf("%d\n", api->ac_state);
315 if (dostatus)
316 printf("1\n");
317 }
318 break;
319 default:
320 break;
321 }
322 switch (reply.newstate) {
323 case SUSPEND:
324 printf("System will enter suspend mode "
325 "in a moment.\n");
326 break;
327 case STANDBY:
328 printf("System will enter standby mode "
329 "in a moment.\n");
330 break;
331 default:
332 break;
333 }
334 } else
335 errx(rval, "cannot get reply from APM daemon\n");
336
337 exit(0);
338 }
339