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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
|
/* $OpenBSD: apm.c,v 1.28 2014/10/17 07:43:33 jmc Exp $ */
/*
* Copyright (c) 1996 John T. Kohl
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/ioctl.h>
#include <machine/apmvar.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <err.h>
#include <string.h>
#include "pathnames.h"
#include "apm-proto.h"
#define FALSE 0
#define TRUE 1
extern char *__progname;
void usage(void);
void zzusage(void);
int do_zzz(int, enum apm_action action);
int open_socket(const char *pn);
int send_command(int fd, struct apm_command *cmd, struct apm_reply *reply);
void
usage(void)
{
fprintf(stderr,"usage: %s [-AabHLlmPSvZz] [-f sockname]\n",
__progname);
exit(1);
}
void
zzusage(void)
{
fprintf(stderr,"usage: %s [-SZz] [-f sockname]\n",
__progname);
exit(1);
}
int
send_command(int fd, struct apm_command *cmd, struct apm_reply *reply)
{
/* send a command to the apm daemon */
cmd->vno = APMD_VNO;
if (send(fd, cmd, sizeof(*cmd), 0) == sizeof(*cmd)) {
if (recv(fd, reply, sizeof(*reply), 0) != sizeof(*reply)) {
warn("invalid reply from APM daemon");
return (1);
}
} else {
warn("invalid send to APM daemon");
return (1);
}
return (0);
}
int
do_zzz(int fd, enum apm_action action)
{
struct apm_command command;
struct apm_reply reply;
char *msg;
switch (action) {
case NONE:
case SUSPEND:
command.action = SUSPEND;
msg = "Suspending system";
break;
case STANDBY:
command.action = STANDBY;
msg = "System standing by";
break;
case HIBERNATE:
command.action = HIBERNATE;
msg = "Hibernating system";
break;
default:
zzusage();
}
printf("%s...\n", msg);
exit(send_command(fd, &command, &reply));
}
int
open_socket(const char *sockname)
{
int sock, errr;
struct sockaddr_un s_un;
sock = socket(AF_UNIX, SOCK_STREAM, 0);
if (sock == -1)
err(1, "cannot create local socket");
s_un.sun_family = AF_UNIX;
strncpy(s_un.sun_path, sockname, sizeof(s_un.sun_path));
s_un.sun_len = SUN_LEN(&s_un);
if (connect(sock, (struct sockaddr *)&s_un, s_un.sun_len) == -1) {
errr = errno;
close(sock);
errno = errr;
sock = -1;
}
return (sock);
}
int
main(int argc, char *argv[])
{
const char *sockname = _PATH_APM_SOCKET;
int doac = FALSE;
int dopct = FALSE;
int dobstate = FALSE;
int domin = FALSE;
int doperf = FALSE;
int verbose = FALSE;
int ch, fd, rval;
enum apm_action action = NONE;
struct apm_command command;
struct apm_reply reply;
int cpuspeed_mib[] = { CTL_HW, HW_CPUSPEED }, cpuspeed;
size_t cpuspeed_sz = sizeof(cpuspeed);
while ((ch = getopt(argc, argv, "ACHLlmbvaPSzZf:")) != -1) {
switch (ch) {
case 'v':
verbose = TRUE;
break;
case 'f':
sockname = optarg;
break;
case 'z':
if (action != NONE)
usage();
action = SUSPEND;
break;
case 'S':
if (action != NONE)
usage();
action = STANDBY;
break;
case 'Z':
if (action != NONE)
usage();
action = HIBERNATE;
break;
case 'A':
if (action != NONE)
usage();
action = SETPERF_AUTO;
break;
case 'C':
if (action != NONE)
usage();
action = SETPERF_COOL;
break;
case 'H':
if (action != NONE)
usage();
action = SETPERF_HIGH;
break;
case 'L':
if (action != NONE)
usage();
action = SETPERF_LOW;
break;
case 'b':
if (action != NONE && action != GETSTATUS)
usage();
dobstate = TRUE;
action = GETSTATUS;
break;
case 'l':
if (action != NONE && action != GETSTATUS)
usage();
dopct = TRUE;
action = GETSTATUS;
break;
case 'm':
if (action != NONE && action != GETSTATUS)
usage();
domin = TRUE;
action = GETSTATUS;
break;
case 'a':
if (action != NONE && action != GETSTATUS)
usage();
doac = TRUE;
action = GETSTATUS;
break;
case 'P':
if (action != NONE && action != GETSTATUS)
usage();
doperf = TRUE;
action = GETSTATUS;
break;
default:
if (!strcmp(__progname, "zzz") || !strcmp(__progname, "ZZZ"))
zzusage();
else
usage();
}
}
fd = open_socket(sockname);
if (!strcmp(__progname, "zzz")) {
if (fd < 0)
err(1, "cannot connect to apmd");
else
return (do_zzz(fd, action));
} else if (!strcmp(__progname, "ZZZ")) {
if (fd < 0)
err(1, "cannot connect to apmd");
else
return (do_zzz(fd, HIBERNATE));
}
bzero(&reply, sizeof reply);
reply.batterystate.battery_state = APM_BATT_UNKNOWN;
reply.batterystate.ac_state = APM_AC_UNKNOWN;
reply.perfmode = PERF_MANUAL;
if (sysctl(cpuspeed_mib, 2, &cpuspeed, &cpuspeed_sz, NULL, 0) < 0)
reply.cpuspeed = 0;
else
reply.cpuspeed = cpuspeed;
switch (action) {
case SETPERF_LOW:
case SETPERF_HIGH:
case SETPERF_AUTO:
case SETPERF_COOL:
if (fd == -1)
errx(1, "cannot connect to apmd, "
"not changing performance adjustment mode");
goto balony;
case NONE:
action = GETSTATUS;
verbose = doac = dopct = dobstate = domin = doperf = TRUE;
/* FALLTHROUGH */
case GETSTATUS:
if (fd == -1) {
/* open the device directly and get status */
fd = open(_PATH_APM_NORMAL, O_RDONLY);
if (ioctl(fd, APM_IOC_GETPOWER,
&reply.batterystate) == 0)
goto printval;
}
/* FALLTHROUGH */
balony:
case SUSPEND:
case STANDBY:
case HIBERNATE:
command.action = action;
break;
default:
usage();
}
if (fd != -1 && (rval = send_command(fd, &command, &reply)) != 0)
errx(rval, "cannot get reply from APM daemon");
switch (action) {
case GETSTATUS:
printval:
if (!verbose) {
if (dobstate)
printf("%d\n",
reply.batterystate.battery_state);
if (dopct)
printf("%d\n",
reply.batterystate.battery_life);
if (domin) {
if (reply.batterystate.minutes_left ==
(u_int)-1)
printf("unknown\n");
else
printf("%d\n",
reply.batterystate.minutes_left);
}
if (doac)
printf("%d\n",
reply.batterystate.ac_state);
if (doperf)
printf("%d\n", reply.perfmode);
break;
}
if (dobstate) {
printf("Battery state: %s",
battstate(reply.batterystate.battery_state));
if (!dopct && !domin)
printf("\n");
}
if (dopct && !dobstate)
printf("Battery remaining: %d percent",
reply.batterystate.battery_life);
else if (dopct)
printf(", %d%% remaining",
reply.batterystate.battery_life);
if (dopct && !domin)
printf("\n");
if (domin && !dobstate && !dopct) {
#ifdef __powerpc__
if (reply.batterystate.battery_state ==
APM_BATT_CHARGING)
printf("Remaining battery recharge "
"time estimate: %d minutes\n",
reply.batterystate.minutes_left);
else if (reply.batterystate.minutes_left == 0 &&
reply.batterystate.battery_life > 10)
printf("Battery life estimate: "
"not available\n");
else
#endif
{
printf("Battery life estimate: ");
if (reply.batterystate.minutes_left ==
(u_int)-1)
printf("unknown\n");
else
printf("%d minutes\n",
reply.batterystate.minutes_left);
}
} else if (domin) {
#ifdef __powerpc__
if (reply.batterystate.battery_state ==
APM_BATT_CHARGING)
printf(", %d minutes recharge time estimate\n",
reply.batterystate.minutes_left);
else if (reply.batterystate.minutes_left == 0 &&
reply.batterystate.battery_life > 10)
printf(", unknown life estimate\n");
else
#endif
{
if (reply.batterystate.minutes_left ==
(u_int)-1)
printf(", unknown");
else
printf(", %d minutes",
reply.batterystate.minutes_left);
printf(" life estimate\n");
}
}
if (doac)
printf("A/C adapter state: %s\n",
ac_state(reply.batterystate.ac_state));
if (doperf)
printf("Performance adjustment mode: %s (%d MHz)\n",
perf_mode(reply.perfmode), reply.cpuspeed);
break;
default:
break;
}
switch (reply.newstate) {
case SUSPEND:
printf("System will enter suspend mode momentarily.\n");
break;
case STANDBY:
printf("System will enter standby mode momentarily.\n");
break;
case HIBERNATE:
printf("System will enter hibernate mode momentarily.\n");
break;
default:
break;
}
return (0);
}
|