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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include "pmconfig.h"
#include <sys/mkdev.h>
#include <sys/syslog.h>
#include <sys/openpromio.h>
#include <sys/mnttab.h>
#include <sys/vtoc.h>
#include <sys/efi_partition.h>
#include <syslog.h>
#include <stdlib.h>
#include <sys/pm.h>
#include <kstat.h>
#include <sys/smbios.h>
#include <libzfs.h>
#define STRCPYLIM(dst, src, str) strcpy_limit(dst, src, sizeof (dst), str)
#define LASTBYTE(str) (str + strlen(str) - 1)
static char nerr_fmt[] = "number is out of range (%s)\n";
static char alloc_fmt[] = "cannot allocate space for \"%s\", %s\n";
static char set_thresh_fmt[] = "error setting threshold(s) for \"%s\", %s\n";
static char bad_thresh_fmt[] = "bad threshold(s)\n";
static char stat_fmt[] = "cannot stat \"%s\", %s\n";
static char always_on[] = "always-on";
#define PM_DEFAULT_ALGORITHM -1
/*
* When lines in a config file (usually "/etc/power.conf") start with
* a recognized keyword, a "handler" routine is called for specific
* CPR or PM -related action(s). Each routine returns a status code
* indicating whether all tasks were successful; if any errors occured,
* future CPR or PM updates are skipped. Following are the handler
* routines for all keywords:
*/
static char pm_cmd_string[32];
static char *
pm_map(int cmd)
{
pm_req_t req;
req.value = cmd;
req.data = (void *)pm_cmd_string;
req.datasize = sizeof (pm_cmd_string);
if (ioctl(pm_fd, PM_GET_CMD_NAME, &req) < 0) {
perror(gettext("PM_GET_CMD_NAME failed:"));
return ("??");
}
return (pm_cmd_string);
}
static int
isonlist(char *listname, const char *man, const char *prod)
{
pm_searchargs_t sl;
int ret;
sl.pms_listname = listname;
sl.pms_manufacturer = (char *)man;
sl.pms_product = (char *)prod;
ret = ioctl(pm_fd, PM_SEARCH_LIST, &sl);
mesg(MDEBUG, "PM_SEARCH_LIST %s for %s,%s returns %d\n",
listname, man, prod, ret);
return (ret == 0);
}
static int
do_ioctl(int ioctl_cmd, char *keyword, char *behavior, int suppress)
{
mesg(MDEBUG, "doing ioctl %s for %s ", pm_map(ioctl_cmd), keyword);
if (ioctl(pm_fd, ioctl_cmd, NULL) == -1) {
int suppressed = suppress == -1 || suppress == errno;
if (!suppressed) {
mesg(MERR, "%s %s failed, %s\n", keyword, behavior,
strerror(errno));
return (NOUP);
} else {
mesg(MDEBUG, "%s %s failed, %s\n", keyword, behavior,
strerror(errno));
return (OKUP);
}
}
mesg(MDEBUG, "succeeded\n");
return (OKUP);
}
/*
* Check for valid cpupm behavior and communicate it to the kernel.
*/
int
cpupm(void)
{
struct btoc {
char *behavior;
int cmd;
int Errno;
};
static struct btoc blist[] = {
"disable", PM_STOP_CPUPM, EINVAL,
"enable", PM_START_CPUPM, EBUSY,
NULL, 0, 0
};
struct btoc *bp;
char *behavior;
for (behavior = LINEARG(1), bp = blist; bp->cmd; bp++) {
if (strcmp(behavior, bp->behavior) == 0)
break;
}
if (bp->cmd == 0) {
mesg(MERR, "invalid cpupm behavior \"%s\"\n", behavior);
return (NOUP);
}
if (ioctl(pm_fd, bp->cmd, NULL) == -1 && errno != bp->Errno) {
mesg(MERR, "cpupm %s failed, %s\n",
behavior, strerror(errno));
return (NOUP);
}
return (OKUP);
}
/*
* Two decisions are identical except for the list names and ioctl commands
* inputs: whitelist, blacklist, yes, no
* if (! ("S3" kstat exists))
* return (no)
* if (SystemInformation.Manufacturer == "Sun Microsystems" &&
* (Pref_PM_Profile == Workstation || Pref_PM_Profile == Desktop)) {
* if (platform on blacklist)
* return (no)
* return (yes)
* } else {
* if (platform on whitelist)
* return (yes)
* return (no)
* }
*/
int
S3_helper(char *whitelist, char *blacklist, int yes, int no, char *keyword,
char *behavior, int *didyes, int suppress)
{
int oflags = SMB_O_NOCKSUM | SMB_O_NOVERS;
smbios_hdl_t *shp;
smbios_system_t sys;
id_t id;
int ret;
kstat_ctl_t *kc;
kstat_t *ksp;
kstat_named_t *dp;
smbios_info_t info;
int preferred_pm_profile = 0;
char yesstr[32], nostr[32]; /* DEBUG */
*didyes = 0;
strncpy(yesstr, pm_map(yes), sizeof (yesstr));
strncpy(nostr, pm_map(no), sizeof (nostr));
mesg(MDEBUG, "S3_helper(%s, %s, %s, %s, %s, %s)\n", whitelist,
blacklist, yesstr, nostr, keyword, behavior);
if ((kc = kstat_open()) == NULL) {
mesg(MDEBUG, "kstat_open failed\n");
return (OKUP);
}
ksp = kstat_lookup(kc, "acpi", -1, "acpi");
if (ksp == NULL) {
mesg(MDEBUG, "kstat_lookup 'acpi', -1, 'acpi' failed\n");
kstat_close(kc);
return (OKUP);
}
(void) kstat_read(kc, ksp, NULL);
dp = kstat_data_lookup(ksp, "S3");
if (dp == NULL || dp->value.l == 0) {
mesg(MDEBUG, "kstat_data_lookup 'S3' fails\n");
if (dp != NULL)
mesg(MDEBUG, "value.l %lx\n", dp->value.l);
kstat_close(kc);
return (do_ioctl(no, keyword, behavior, suppress));
}
mesg(MDEBUG, "kstat indicates S3 support (%lx)\n", dp->value.l);
if (!whitelist_only) {
/*
* We still have an ACPI ksp, search it again for
* 'preferred_pm_profile' (needs to be valid if we don't
* aren't only using a whitelist).
*/
dp = kstat_data_lookup(ksp, "preferred_pm_profile");
if (dp == NULL) {
mesg(MDEBUG, "kstat_data_lookup 'ppmp fails\n");
kstat_close(kc);
return (do_ioctl(no, keyword, behavior, suppress));
}
mesg(MDEBUG, "kstat indicates preferred_pm_profile is %lx\n",
dp->value.l);
preferred_pm_profile = dp->value.l;
}
kstat_close(kc);
if ((shp = smbios_open(NULL,
SMB_VERSION, oflags, &ret)) == NULL) {
/* we promised not to complain */
/* we bail leaving it to the kernel default */
mesg(MDEBUG, "smbios_open failed %d\n", errno);
return (OKUP);
}
if ((id = smbios_info_system(shp, &sys)) == SMB_ERR) {
mesg(MDEBUG, "smbios_info_system failed %d\n", errno);
smbios_close(shp);
return (OKUP);
}
if (smbios_info_common(shp, id, &info) == SMB_ERR) {
mesg(MDEBUG, "smbios_info_common failed %d\n", errno);
smbios_close(shp);
return (OKUP);
}
mesg(MDEBUG, "Manufacturer: %s\n", info.smbi_manufacturer);
mesg(MDEBUG, "Product: %s\n", info.smbi_product);
smbios_close(shp);
if (!whitelist_only) {
#define PPP_DESKTOP 1
#define PPP_WORKSTATION 3
if (strcmp(info.smbi_manufacturer, "Sun Microsystems") == 0 &&
(preferred_pm_profile == PPP_DESKTOP ||
preferred_pm_profile == PPP_WORKSTATION)) {
if (isonlist(blacklist,
info.smbi_manufacturer, info.smbi_product)) {
return (do_ioctl(no, keyword, behavior,
suppress));
} else {
ret = do_ioctl(yes, keyword, behavior,
suppress);
*didyes = (ret == OKUP);
return (ret);
}
}
}
if (isonlist(whitelist,
info.smbi_manufacturer, info.smbi_product)) {
ret = do_ioctl(yes, keyword, behavior, suppress);
*didyes = (ret == OKUP);
return (ret);
} else {
return (do_ioctl(no, keyword, behavior, suppress));
}
}
int
S3sup(void) /* S3-support keyword handler */
{
struct btoc {
char *behavior;
int cmd;
};
static struct btoc blist[] = {
"default", PM_DEFAULT_ALGORITHM,
"enable", PM_ENABLE_S3,
"disable", PM_DISABLE_S3,
NULL, 0
};
struct btoc *bp;
char *behavior;
int dontcare;
for (behavior = LINEARG(1), bp = blist; bp->cmd; bp++) {
if (strcmp(behavior, bp->behavior) == 0)
break;
}
if (bp->cmd == 0) {
mesg(MERR, "invalid S3-support behavior \"%s\"\n", behavior);
return (NOUP);
}
switch (bp->cmd) {
case PM_ENABLE_S3:
case PM_DISABLE_S3:
return (do_ioctl(bp->cmd, "S3-support", behavior, EBUSY));
case PM_DEFAULT_ALGORITHM:
/*
* we suppress errors in the "default" case because we
* already did an invisible default call, so we know we'll
* get EBUSY
*/
return (S3_helper("S3-support-enable", "S3-support-disable",
PM_ENABLE_S3, PM_DISABLE_S3, "S3-support", behavior,
&dontcare, EBUSY));
default:
mesg(MERR, "S3-support %s failed, %s\n", behavior,
strerror(errno));
return (NOUP);
}
}
/*
* Check for valid autoS3 behavior and save after ioctl success.
*/
int
autoS3(void)
{
struct btoc {
char *behavior;
int cmd;
};
static struct btoc blist[] = {
"default", PM_DEFAULT_ALGORITHM,
"disable", PM_STOP_AUTOS3,
"enable", PM_START_AUTOS3,
NULL, 0
};
struct btoc *bp;
char *behavior;
int dontcare;
for (behavior = LINEARG(1), bp = blist; bp->cmd; bp++) {
if (strcmp(behavior, bp->behavior) == 0)
break;
}
if (bp->cmd == 0) {
mesg(MERR, "invalid autoS3 behavior \"%s\"\n", behavior);
return (NOUP);
}
switch (bp->cmd) {
default:
mesg(MERR, "autoS3 %s failed, %s\n",
behavior, strerror(errno));
mesg(MDEBUG, "unknown command\n", bp->cmd);
return (OKUP);
case PM_STOP_AUTOS3:
case PM_START_AUTOS3:
return (do_ioctl(bp->cmd, "autoS3", behavior, EBUSY));
case PM_DEFAULT_ALGORITHM:
return (S3_helper("S3-autoenable", "S3-autodisable",
PM_START_AUTOS3, PM_STOP_AUTOS3, "autoS3", behavior,
&dontcare, EBUSY));
}
}
/*
* Check for valid autopm behavior and save after ioctl success.
*/
int
autopm(void)
{
struct btoc {
char *behavior;
int cmd, Errno, isdef;
};
static struct btoc blist[] = {
"default", PM_START_PM, -1, 1,
"disable", PM_STOP_PM, EINVAL, 0,
"enable", PM_START_PM, EBUSY, 0,
NULL, 0, 0, 0,
};
struct btoc *bp;
char *behavior;
for (behavior = LINEARG(1), bp = blist; bp->cmd; bp++) {
if (strcmp(behavior, bp->behavior) == 0)
break;
}
if (bp->cmd == 0) {
mesg(MERR, "invalid autopm behavior \"%s\"\n", behavior);
return (NOUP);
}
/*
* for "default" behavior, do not enable autopm if not ESTAR_V3
*/
#if defined(__sparc)
if (!bp->isdef || (estar_vers == ESTAR_V3)) {
if (ioctl(pm_fd, bp->cmd, NULL) == -1 && errno != bp->Errno) {
mesg(MERR, "autopm %s failed, %s\n",
behavior, strerror(errno));
return (NOUP);
}
}
(void) strcpy(new_cc.apm_behavior, behavior);
return (OKUP);
#endif
#if defined(__x86)
if (!bp->isdef) {
if (ioctl(pm_fd, bp->cmd, NULL) == -1 && errno != bp->Errno) {
mesg(MERR, "autopm %s failed, %s\n",
behavior, strerror(errno));
return (NOUP);
}
mesg(MDEBUG, "autopm %s succeeded\n", behavior);
return (OKUP);
} else {
int didenable;
int ret = S3_helper("autopm-enable", "autopm-disable",
PM_START_PM, PM_STOP_PM, "autopm", behavior, &didenable,
bp->Errno);
if (didenable) {
/* tell powerd to attach all devices */
new_cc.is_autopm_default = 1;
(void) strcpy(new_cc.apm_behavior, behavior);
}
return (ret);
}
#endif
}
static int
gethm(char *src, int *hour, int *min)
{
if (sscanf(src, "%d:%d", hour, min) != 2) {
mesg(MERR, "bad time format (%s)\n", src);
return (-1);
}
return (0);
}
static void
strcpy_limit(char *dst, char *src, size_t limit, char *info)
{
if (strlcpy(dst, src, limit) >= limit)
mesg(MEXIT, "%s is too long (%s)\n", info, src);
}
/*
* Convert autoshutdown idle and start/finish times;
* check and record autoshutdown behavior.
*/
int
autosd(void)
{
char **bp, *behavior;
char *unrec = gettext("unrecognized autoshutdown behavior");
static char *blist[] = {
"autowakeup", "default", "noshutdown",
"shutdown", "unconfigured", NULL
};
new_cc.as_idle = atoi(LINEARG(1));
if (gethm(LINEARG(2), &new_cc.as_sh, &new_cc.as_sm) ||
gethm(LINEARG(3), &new_cc.as_fh, &new_cc.as_fm))
return (NOUP);
mesg(MDEBUG, "idle %d, start %d:%02d, finish %d:%02d\n",
new_cc.as_idle, new_cc.as_sh, new_cc.as_sm,
new_cc.as_fh, new_cc.as_fm);
for (behavior = LINEARG(4), bp = blist; *bp; bp++) {
if (strcmp(behavior, *bp) == 0)
break;
}
if (*bp == NULL) {
mesg(MERR, "%s: \"%s\"\n", unrec, behavior);
return (NOUP);
}
STRCPYLIM(new_cc.as_behavior, *bp, unrec);
return (OKUP);
}
/*
* Check for a real device and try to resolve to a full path.
* The orig/resolved path may be modified into a prom pathname,
* and an allocated copy of the result is stored at *destp;
* the caller will need to free that space. Returns 1 for any
* error, otherwise 0; also sets *errp after an alloc error.
*/
static int
devpath(char **destp, char *src, int *errp)
{
struct stat stbuf;
char buf[PATH_MAX];
char *cp, *dstr;
int devok, dcs = 0;
size_t len;
/*
* When there's a real device, try to resolve the path
* and trim the leading "/devices" component.
*/
if ((devok = (stat(src, &stbuf) == 0 && stbuf.st_rdev)) != 0) {
if (realpath(src, buf) == NULL) {
mesg(MERR, "realpath cannot resolve \"%s\"\n",
src, strerror(errno));
return (1);
}
src = buf;
dstr = "/devices";
len = strlen(dstr);
dcs = (strncmp(src, dstr, len) == 0);
if (dcs)
src += len;
} else
mesg(MDEBUG, stat_fmt, src, strerror(errno));
/*
* When the path has ":anything", display an error for
* a non-device or truncate a resolved+modifed path.
*/
if (cp = strchr(src, ':')) {
if (devok == 0) {
mesg(MERR, "physical path may not contain "
"a minor string (%s)\n", src);
return (1);
} else if (dcs)
*cp = '\0';
}
if ((*destp = strdup(src)) == NULL) {
*errp = NOUP;
mesg(MERR, alloc_fmt, src, strerror(errno));
}
return (*destp == NULL);
}
/*
* Call pm ioctl request(s) to set property/device dependencies.
*/
static int
dev_dep_common(int isprop)
{
int cmd, argn, upval = OKUP;
char *src, *first, **destp;
pm_req_t pmreq;
bzero(&pmreq, sizeof (pmreq));
src = LINEARG(1);
if (isprop) {
cmd = PM_ADD_DEPENDENT_PROPERTY;
first = NULL;
pmreq.pmreq_kept = src;
} else {
cmd = PM_ADD_DEPENDENT;
if (devpath(&first, src, &upval))
return (upval);
pmreq.pmreq_kept = first;
}
destp = &pmreq.pmreq_keeper;
/*
* Now loop through any dependents.
*/
for (argn = 2; (src = LINEARG(argn)) != NULL; argn++) {
if (devpath(destp, src, &upval)) {
if (upval != OKUP)
return (upval);
break;
}
if ((upval = ioctl(pm_fd, cmd, &pmreq)) == -1) {
mesg(MDEBUG, "pm ioctl, cmd %d, errno %d\n"
"kept \"%s\", keeper \"%s\"\n",
cmd, errno, pmreq.pmreq_kept, pmreq.pmreq_keeper);
mesg(MERR, "cannot set \"%s\" dependency "
"for \"%s\", %s\n", pmreq.pmreq_keeper,
pmreq.pmreq_kept, strerror(errno));
}
free(*destp);
*destp = NULL;
if (upval != OKUP)
break;
}
free(first);
return (upval);
}
int
ddprop(void)
{
return (dev_dep_common(1));
}
int
devdep(void)
{
return (dev_dep_common(0));
}
/*
* Convert a numeric string (with a possible trailing scaling byte)
* into an integer. Returns a converted value and *nerrp unchanged,
* or 0 with *nerrp set to 1 for a conversion error.
*/
static int
get_scaled_value(char *str, int *nerrp)
{
longlong_t svalue = 0, factor = 1;
char *sp;
errno = 0;
svalue = strtol(str, &sp, 0);
if (errno || (*str != '-' && (*str < '0' || *str > '9')))
*nerrp = 1;
else if (sp && *sp != '\0') {
if (*sp == 'h')
factor = 3600;
else if (*sp == 'm')
factor = 60;
else if (*sp != 's')
*nerrp = 1;
}
/* any bytes following sp are ignored */
if (*nerrp == 0) {
svalue *= factor;
if (svalue < INT_MIN || svalue > INT_MAX)
*nerrp = 1;
}
if (*nerrp)
mesg(MERR, nerr_fmt, str);
mesg(MDEBUG, "got scaled value %d\n", (int)svalue);
return ((int)svalue);
}
/*
* Increment the count of threshold values,
* reallocate *vlistp and append another element.
* Returns 1 on error, otherwise 0.
*/
static int
vlist_append(int **vlistp, int *vcntp, int value)
{
(*vcntp)++;
if (*vlistp = realloc(*vlistp, *vcntp * sizeof (**vlistp)))
*(*vlistp + *vcntp - 1) = value;
else
mesg(MERR, alloc_fmt, "threshold list", strerror(errno));
return (*vlistp == NULL);
}
/*
* Convert a single threshold string or paren groups of thresh's as
* described below. All thresh's are saved to an allocated list at
* *vlistp; the caller will need to free that space. On return:
* *vcntp is the count of the vlist array, and vlist is either
* a single thresh or N groups of thresh's with a trailing zero:
* (cnt_1 thr_1a thr_1b [...]) ... (cnt_N thr_Na thr_Nb [...]) 0.
* Returns 0 when all conversions were OK, and 1 for any syntax,
* conversion, or alloc error.
*/
static int
get_thresh(int **vlistp, int *vcntp)
{
int argn, value, gci, grp_cnt = 0, paren = 0, nerr = 0;
char *rp, *src;
for (argn = 2; (src = LINEARG(argn)) != NULL; argn++) {
if (*src == LPAREN) {
gci = *vcntp;
if (nerr = vlist_append(vlistp, vcntp, 0))
break;
paren = 1;
src++;
}
if (*(rp = LASTBYTE(src)) == RPAREN) {
if (paren) {
grp_cnt = *vcntp - gci;
*(*vlistp + gci) = grp_cnt;
paren = 0;
*rp = '\0';
} else {
nerr = 1;
break;
}
}
value = get_scaled_value(src, &nerr);
if (nerr || (nerr = vlist_append(vlistp, vcntp, value)))
break;
}
if (nerr == 0 && grp_cnt)
nerr = vlist_append(vlistp, vcntp, 0);
return (nerr);
}
/*
* Set device thresholds from (3) formats:
* path "always-on"
* path time-spec: [0-9]+[{h,m,s}]
* path (ts1 ts2 ...)+
*/
int
devthr(void)
{
int cmd, upval = OKUP, nthresh = 0, *vlist = NULL;
pm_req_t pmreq;
bzero(&pmreq, sizeof (pmreq));
if (devpath(&pmreq.physpath, LINEARG(1), &upval))
return (upval);
if (strcmp(LINEARG(2), always_on) == 0) {
cmd = PM_SET_DEVICE_THRESHOLD;
pmreq.value = INT_MAX;
} else if (get_thresh(&vlist, &nthresh)) {
mesg(MERR, bad_thresh_fmt);
upval = NOUP;
} else if (nthresh == 1) {
pmreq.value = *vlist;
cmd = PM_SET_DEVICE_THRESHOLD;
} else {
pmreq.data = vlist;
pmreq.datasize = (nthresh * sizeof (*vlist));
cmd = PM_SET_COMPONENT_THRESHOLDS;
}
if (upval != NOUP && (upval = ioctl(pm_fd, cmd, &pmreq)) == -1)
mesg(MERR, set_thresh_fmt, pmreq.physpath, strerror(errno));
free(vlist);
free(pmreq.physpath);
return (upval);
}
static int
scan_int(char *src, int *dst)
{
long lval;
errno = 0;
lval = strtol(LINEARG(1), NULL, 0);
if (errno || lval > INT_MAX || lval < 0) {
mesg(MERR, nerr_fmt, src);
return (NOUP);
}
*dst = (int)lval;
return (OKUP);
}
static int
scan_float(char *src, float *dst)
{
float fval;
errno = 0;
fval = strtof(src, NULL);
if (errno || fval < 0.0) {
mesg(MERR, nerr_fmt, src);
return (NOUP);
}
*dst = fval;
return (OKUP);
}
int
dreads(void)
{
return (scan_int(LINEARG(1), &new_cc.diskreads_thold));
}
/*
* Set pathname for idlecheck;
* an overflowed pathname is treated as a fatal error.
*/
int
idlechk(void)
{
STRCPYLIM(new_cc.idlecheck_path, LINEARG(1), "idle path");
return (OKUP);
}
int
loadavg(void)
{
return (scan_float(LINEARG(1), &new_cc.loadaverage_thold));
}
int
nfsreq(void)
{
return (scan_int(LINEARG(1), &new_cc.nfsreqs_thold));
}
#ifdef sparc
static char open_fmt[] = "cannot open \"%s\", %s\n";
/*
* Verify the filesystem type for a regular statefile is "ufs"
* or verify a block device is not in use as a mounted filesytem.
* Returns 1 if any error, otherwise 0.
*/
static int
check_mount(char *sfile, dev_t sfdev, int ufs)
{
char *src, *err_fmt = NULL, *mnttab = MNTTAB;
int rgent, match = 0;
struct mnttab zroot = { 0 };
struct mnttab entry;
struct extmnttab ent;
FILE *fp;
if ((fp = fopen(mnttab, "r")) == NULL) {
mesg(MERR, open_fmt, mnttab, strerror(errno));
return (1);
}
if (ufs) {
zroot.mnt_mountp = "/";
zroot.mnt_fstype = "zfs";
if (getmntany(fp, &entry, &zroot) == 0) {
err_fmt = "ufs statefile with zfs root is not"
" supported\n";
mesg(MERR, err_fmt, sfile);
fclose(fp);
return (1);
}
resetmnttab(fp);
}
/*
* Search for a matching dev_t;
* ignore non-ufs filesystems for a regular statefile.
*/
while ((rgent = getextmntent(fp, &ent, sizeof (ent))) != -1) {
if (rgent > 0) {
mesg(MERR, "error reading \"%s\"\n", mnttab);
(void) fclose(fp);
return (1);
} else if (ufs && strcmp(ent.mnt_fstype, "ufs"))
continue;
else if (makedev(ent.mnt_major, ent.mnt_minor) == sfdev) {
match = 1;
break;
}
}
/*
* No match is needed for a block device statefile,
* a match is needed for a regular statefile.
*/
if (match == 0) {
if (new_cc.cf_type != CFT_UFS)
STRCPYLIM(new_cc.cf_devfs, sfile, "block statefile");
else
err_fmt = "cannot find ufs mount point for \"%s\"\n";
} else if (new_cc.cf_type == CFT_UFS) {
STRCPYLIM(new_cc.cf_fs, ent.mnt_mountp, "mnt entry");
STRCPYLIM(new_cc.cf_devfs, ent.mnt_special, "mnt special");
while (*(sfile + 1) == '/') sfile++;
src = sfile + strlen(ent.mnt_mountp);
while (*src == '/') src++;
STRCPYLIM(new_cc.cf_path, src, "statefile path");
} else
err_fmt = "statefile device \"%s\" is a mounted filesystem\n";
(void) fclose(fp);
if (err_fmt)
mesg(MERR, err_fmt, sfile);
return (err_fmt != NULL);
}
/*
* Convert a Unix device to a prom device and save on success,
* log any ioctl/conversion error.
*/
static int
utop(char *fs_name, char *prom_name)
{
union obpbuf {
char buf[OBP_MAXPATHLEN + sizeof (uint_t)];
struct openpromio oppio;
};
union obpbuf oppbuf;
struct openpromio *opp;
char *promdev = "/dev/openprom";
int fd, upval;
if ((fd = open(promdev, O_RDONLY)) == -1) {
mesg(MERR, open_fmt, promdev, strerror(errno));
return (NOUP);
}
opp = &oppbuf.oppio;
opp->oprom_size = OBP_MAXPATHLEN;
strcpy_limit(opp->oprom_array, fs_name,
OBP_MAXPATHLEN, "statefile device");
upval = ioctl(fd, OPROMDEV2PROMNAME, opp);
(void) close(fd);
if (upval == OKUP) {
strcpy_limit(prom_name, opp->oprom_array, OBP_MAXPATHLEN,
"prom device");
} else {
openlog("pmconfig", 0, LOG_DAEMON);
syslog(LOG_NOTICE,
gettext("cannot convert \"%s\" to prom device"),
fs_name);
closelog();
}
return (upval);
}
/*
* given the path to a zvol, return the cXtYdZ name
* returns < 0 on error, 0 if it isn't a zvol, > 1 on success
*/
static int
ztop(char *arg, char *diskname)
{
zpool_handle_t *zpool_handle;
nvlist_t *config, *nvroot;
nvlist_t **child;
uint_t children;
libzfs_handle_t *lzfs;
char *vname;
char *p;
char pool_name[MAXPATHLEN];
if (strncmp(arg, "/dev/zvol/dsk/", 14)) {
return (0);
}
arg += 14;
strncpy(pool_name, arg, MAXPATHLEN);
if (p = strchr(pool_name, '/'))
*p = '\0';
STRCPYLIM(new_cc.cf_fs, p + 1, "statefile path");
if ((lzfs = libzfs_init()) == NULL) {
mesg(MERR, "failed to initialize ZFS library\n");
return (-1);
}
if ((zpool_handle = zpool_open(lzfs, pool_name)) == NULL) {
mesg(MERR, "couldn't open pool '%s'\n", pool_name);
libzfs_fini(lzfs);
return (-1);
}
config = zpool_get_config(zpool_handle, NULL);
if (nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
&nvroot) != 0) {
zpool_close(zpool_handle);
libzfs_fini(lzfs);
return (-1);
}
verify(nvlist_lookup_nvlist_array(nvroot, ZPOOL_CONFIG_CHILDREN,
&child, &children) == 0);
if (children != 1) {
mesg(MERR, "expected one vdev, got %d\n", children);
zpool_close(zpool_handle);
libzfs_fini(lzfs);
return (-1);
}
vname = zpool_vdev_name(lzfs, zpool_handle, child[0]);
if (vname == NULL) {
mesg(MERR, "couldn't determine vdev name\n");
zpool_close(zpool_handle);
libzfs_fini(lzfs);
return (-1);
}
strcpy(diskname, "/dev/dsk/");
strcat(diskname, vname);
free(vname);
zpool_close(zpool_handle);
libzfs_fini(lzfs);
return (1);
}
/*
* returns NULL if the slice is good (e.g. does not start at block
* zero, or a string describing the error if it doesn't
*/
static boolean_t
is_good_slice(char *sfile, char **err)
{
int fd, rc;
struct vtoc vtoc;
dk_gpt_t *gpt;
char rdskname[MAXPATHLEN];
char *x, *y;
*err = NULL;
/* convert from dsk to rdsk */
STRCPYLIM(rdskname, sfile, "disk name");
x = strstr(rdskname, "dsk/");
y = strstr(sfile, "dsk/");
if (x != NULL) {
*x++ = 'r';
strcpy(x, y);
}
if ((fd = open(rdskname, O_RDONLY)) == -1) {
*err = "could not open '%s'\n";
} else if ((rc = read_vtoc(fd, &vtoc)) >= 0) {
/*
* we got a slice number; now check the block
* number where the slice starts
*/
if (vtoc.v_part[rc].p_start < 2)
*err = "using '%s' would clobber the disk label\n";
close(fd);
return (*err ? B_FALSE : B_TRUE);
} else if ((rc == VT_ENOTSUP) &&
(efi_alloc_and_read(fd, &gpt)) >= 0) {
/* EFI slices don't clobber the disk label */
free(gpt);
close(fd);
return (B_TRUE);
} else
*err = "could not read partition table from '%s'\n";
return (B_FALSE);
}
/*
* Check for a valid statefile pathname, inode and mount status.
*/
int
sfpath(void)
{
static int statefile;
char *err_fmt = NULL;
char *sfile, *sp, ch;
char diskname[256];
struct stat stbuf;
int dir = 0;
dev_t dev;
if (statefile) {
mesg(MERR, "ignored redundant statefile entry\n");
return (OKUP);
} else if (ua_err) {
if (ua_err != ENOTSUP)
mesg(MERR, "uadmin(A_FREEZE, A_CHECK, 0): %s\n",
strerror(ua_err));
return (NOUP);
}
/*
* Check for an absolute path and trim any trailing '/'.
*/
sfile = LINEARG(1);
if (*sfile != '/') {
mesg(MERR, "statefile requires an absolute path\n");
return (NOUP);
}
for (sp = sfile + strlen(sfile) - 1; sp > sfile && *sp == '/'; sp--)
*sp = '\0';
/*
* If the statefile doesn't exist, the leading path must be a dir.
*/
if (stat(sfile, &stbuf) == -1) {
if (errno == ENOENT) {
dir = 1;
if ((sp = strrchr(sfile, '/')) == sfile)
sp++;
ch = *sp;
*sp = '\0';
if (stat(sfile, &stbuf) == -1)
err_fmt = stat_fmt;
*sp = ch;
} else
err_fmt = stat_fmt;
if (err_fmt) {
mesg(MERR, err_fmt, sfile, strerror(errno));
return (NOUP);
}
}
/*
* Check for regular/dir/block types, set cf_type and dev.
*/
if (S_ISREG(stbuf.st_mode) || (dir && S_ISDIR(stbuf.st_mode))) {
new_cc.cf_type = CFT_UFS;
dev = stbuf.st_dev;
} else if (S_ISBLK(stbuf.st_mode)) {
if (is_good_slice(sfile, &err_fmt)) {
switch (ztop(sfile, diskname)) {
case 1:
new_cc.cf_type = CFT_ZVOL;
break;
case 0:
new_cc.cf_type = CFT_SPEC;
break;
case -1:
default:
return (NOUP);
}
dev = stbuf.st_rdev;
}
} else
err_fmt = "bad file type for \"%s\"\n"
"statefile must be a regular file or block device\n";
if (err_fmt) {
mesg(MERR, err_fmt, sfile);
return (NOUP);
}
if (check_mount(sfile, dev, (new_cc.cf_type == CFT_UFS)))
return (NOUP);
if (new_cc.cf_type == CFT_ZVOL) {
if (utop(diskname, new_cc.cf_dev_prom))
return (NOUP);
} else if (utop(new_cc.cf_devfs, new_cc.cf_dev_prom)) {
return (NOUP);
}
new_cc.cf_magic = CPR_CONFIG_MAGIC;
statefile = 1;
return (OKUP);
}
#endif /* sparc */
/*
* Common function to set a system or cpu threshold.
*/
static int
cmnthr(int req)
{
int value, nerr = 0, upval = OKUP;
char *thresh = LINEARG(1);
if (strcmp(thresh, always_on) == 0)
value = INT_MAX;
else if ((value = get_scaled_value(thresh, &nerr)) < 0 || nerr) {
mesg(MERR, "%s must be a positive value\n", LINEARG(0));
upval = NOUP;
}
if (upval == OKUP)
(void) ioctl(pm_fd, req, value);
return (upval);
}
/*
* Try setting system threshold.
*/
int
systhr(void)
{
return (cmnthr(PM_SET_SYSTEM_THRESHOLD));
}
/*
* Try setting cpu threshold.
*/
int
cputhr(void)
{
return (cmnthr(PM_SET_CPU_THRESHOLD));
}
int
tchars(void)
{
return (scan_int(LINEARG(1), &new_cc.ttychars_thold));
}
|