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
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
|
/*
* 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 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <door.h>
#include <libnvpair.h>
#include <libhotplug.h>
#include <libhotplug_impl.h>
#include <sys/sunddi.h>
#include <sys/ddi_hp.h>
static void i_hp_dprintf(const char *fmt, ...);
static int i_hp_pack_branch(hp_node_t, char **, size_t *);
static int i_hp_pack_node(hp_node_t, char **, size_t *);
static int i_hp_unpack_node(char *, size_t, hp_node_t, hp_node_t *);
static int i_hp_unpack_branch(char *, size_t, hp_node_t, hp_node_t *);
static int i_hp_call_hotplugd(nvlist_t *, nvlist_t **);
static nvlist_t *i_hp_set_args(hp_cmd_t, const char *, const char *, uint_t,
const char *, int);
static int i_hp_parse_results(nvlist_t *, hp_node_t *, char **);
/*
* Global flag to enable debug features.
*/
int libhotplug_debug = 0;
/*
* hp_init()
*
* Initialize a hotplug information snapshot.
*/
hp_node_t
hp_init(const char *path, const char *connection, uint_t flags)
{
nvlist_t *args;
nvlist_t *results;
hp_node_t root = NULL;
int rv;
i_hp_dprintf("hp_init: path=%p, connection=%p, flags=0x%x\n",
(void *)path, (void *)connection, flags);
/* Check arguments */
if ((path == NULL) || !HP_INIT_FLAGS_VALID(flags)) {
i_hp_dprintf("hp_init: invalid arguments.\n");
errno = EINVAL;
return (NULL);
}
/* Build arguments for door call */
if ((args = i_hp_set_args(HP_CMD_GETINFO, path, connection, flags,
NULL, 0)) == NULL) {
i_hp_dprintf("hp_init: cannot build arguments nvlist.\n");
errno = ENOMEM;
return (NULL);
}
/* Make the door call to hotplugd */
rv = i_hp_call_hotplugd(args, &results);
/* Arguments no longer needed */
nvlist_free(args);
/* Parse additional results, if any */
if ((rv == 0) && (results != NULL)) {
rv = i_hp_parse_results(results, &root, NULL);
nvlist_free(results);
}
/* Check for errors */
if (rv != 0) {
i_hp_dprintf("hp_init: failure (%s).\n", strerror(rv));
if (root)
hp_fini(root);
errno = rv;
return (NULL);
}
/* Success requires an info snapshot */
if (root == NULL) {
i_hp_dprintf("hp_init: missing info snapshot.\n");
errno = EFAULT;
return (NULL);
}
/* Success */
return (root);
}
/*
* hp_fini()
*
* Terminate and clean-up a hotplug information snapshot.
*/
void
hp_fini(hp_node_t root)
{
hp_node_t node;
hp_node_t sibling;
char *basepath;
i_hp_dprintf("hp_fini: root=%p\n", (void *)root);
if (root == NULL) {
i_hp_dprintf("hp_fini: invalid arguments.\n");
return;
}
/* Extract and free base path */
if (root->hp_basepath) {
basepath = root->hp_basepath;
for (node = root; node != NULL; node = node->hp_sibling)
node->hp_basepath = NULL;
free(basepath);
}
/* Destroy the nodes */
node = root;
while (node) {
sibling = node->hp_sibling;
if (node->hp_child)
hp_fini(node->hp_child);
if (node->hp_name)
free(node->hp_name);
if (node->hp_usage)
free(node->hp_usage);
if (node->hp_description)
free(node->hp_description);
free(node);
node = sibling;
}
}
/*
* hp_traverse()
*
* Walk a graph of hotplug nodes, executing a callback on each node.
*/
int
hp_traverse(hp_node_t root, void *arg, int (*hp_callback)(hp_node_t, void *arg))
{
int rv;
hp_node_t node;
i_hp_dprintf("hp_traverse: root=%p, arg=%p, hp_callback=%p\n",
(void *)root, arg, (void *)hp_callback);
/* Check arguments */
if ((root == NULL) || (hp_callback == NULL)) {
i_hp_dprintf("hp_traverse: invalid arguments.\n");
errno = EINVAL;
return (-1);
}
for (node = root; node; node = node->hp_sibling) {
rv = hp_callback(node, arg);
if (rv == HP_WALK_TERMINATE) {
i_hp_dprintf("hp_traverse: walk terminated.\n");
return (HP_WALK_TERMINATE);
}
if (node->hp_child && (rv != HP_WALK_PRUNECHILD))
if (hp_traverse(node->hp_child, arg, hp_callback) ==
HP_WALK_TERMINATE) {
i_hp_dprintf("hp_traverse: walk terminated.\n");
return (HP_WALK_TERMINATE);
}
if (rv == HP_WALK_PRUNESIBLING)
break;
}
return (0);
}
/*
* hp_type()
*
* Return a node's type.
*/
int
hp_type(hp_node_t node)
{
i_hp_dprintf("hp_type: node=%p\n", (void *)node);
if (node == NULL) {
i_hp_dprintf("hp_type: invalid arguments.\n");
errno = EINVAL;
return (-1);
}
return (node->hp_type);
}
/*
* hp_name()
*
* Return a node's name.
*/
char *
hp_name(hp_node_t node)
{
i_hp_dprintf("hp_name: node=%p\n", (void *)node);
if (node == NULL) {
i_hp_dprintf("hp_name: invalid arguments.\n");
errno = EINVAL;
return (NULL);
}
if (node->hp_name == NULL) {
i_hp_dprintf("hp_name: missing name value.\n");
errno = EFAULT;
}
return (node->hp_name);
}
/*
* hp_state()
*
* Return a node's current state.
*/
int
hp_state(hp_node_t node)
{
i_hp_dprintf("hp_state: node=%p\n", (void *)node);
if (node == NULL) {
i_hp_dprintf("hp_state: invalid arguments.\n");
errno = EINVAL;
return (-1);
}
if ((node->hp_type != HP_NODE_CONNECTOR) &&
(node->hp_type != HP_NODE_PORT)) {
i_hp_dprintf("hp_state: operation not supported.\n");
errno = ENOTSUP;
return (-1);
}
return (node->hp_state);
}
/*
* hp_usage()
*
* Return a usage description for usage nodes.
*/
char *
hp_usage(hp_node_t node)
{
i_hp_dprintf("hp_usage: node=%p\n", (void *)node);
if (node == NULL) {
i_hp_dprintf("hp_usage: invalid arguments.\n");
errno = EINVAL;
return (NULL);
}
if (node->hp_type != HP_NODE_USAGE) {
i_hp_dprintf("hp_usage: operation not supported.\n");
errno = ENOTSUP;
return (NULL);
}
if (node->hp_usage == NULL) {
i_hp_dprintf("hp_usage: missing usage value.\n");
errno = EFAULT;
}
return (node->hp_usage);
}
/*
* hp_description()
*
* Return a type description (e.g. "PCI slot") for connection nodes.
*/
char *
hp_description(hp_node_t node)
{
i_hp_dprintf("hp_description: node=%p\n", (void *)node);
if (node == NULL) {
i_hp_dprintf("hp_description: invalid arguments.\n");
errno = EINVAL;
return (NULL);
}
if ((node->hp_type != HP_NODE_CONNECTOR) &&
(node->hp_type != HP_NODE_PORT)) {
i_hp_dprintf("hp_description: operation not supported.\n");
errno = ENOTSUP;
return (NULL);
}
if (node->hp_description == NULL) {
i_hp_dprintf("hp_description: missing description value.\n");
errno = EFAULT;
}
return (node->hp_description);
}
/*
* hp_last_change()
*
* Return when the state of a connection was last changed.
*/
time_t
hp_last_change(hp_node_t node)
{
i_hp_dprintf("hp_last_change: node=%p\n", (void *)node);
if (node == NULL) {
i_hp_dprintf("hp_last_change: invalid arguments.\n");
errno = EINVAL;
return (0);
}
if ((node->hp_type != HP_NODE_CONNECTOR) &&
(node->hp_type != HP_NODE_PORT)) {
i_hp_dprintf("hp_last_change: operation not supported.\n");
errno = ENOTSUP;
return (0);
}
return (node->hp_last_change);
}
/*
* hp_parent()
*
* Return a node's parent node.
*/
hp_node_t
hp_parent(hp_node_t node)
{
i_hp_dprintf("hp_parent: node=%p\n", (void *)node);
if (node == NULL) {
i_hp_dprintf("hp_parent: invalid arguments.\n");
errno = EINVAL;
return (NULL);
}
if (node->hp_parent == NULL) {
i_hp_dprintf("hp_parent: node has no parent.\n");
errno = ENXIO;
}
return (node->hp_parent);
}
/*
* hp_child()
*
* Return a node's first child node.
*/
hp_node_t
hp_child(hp_node_t node)
{
i_hp_dprintf("hp_child: node=%p\n", (void *)node);
if (node == NULL) {
i_hp_dprintf("hp_child: invalid arguments.\n");
errno = EINVAL;
return (NULL);
}
if (node->hp_child == NULL) {
i_hp_dprintf("hp_child: node has no child.\n");
errno = ENXIO;
}
return (node->hp_child);
}
/*
* hp_sibling()
*
* Return a node's next sibling node.
*/
hp_node_t
hp_sibling(hp_node_t node)
{
i_hp_dprintf("hp_sibling: node=%p\n", (void *)node);
if (node == NULL) {
i_hp_dprintf("hp_sibling: invalid arguments.\n");
errno = EINVAL;
return (NULL);
}
if (node->hp_sibling == NULL) {
i_hp_dprintf("hp_sibling: node has no sibling.\n");
errno = ENXIO;
}
return (node->hp_sibling);
}
/*
* hp_path()
*
* Return the path (and maybe connection name) of a node.
* The caller must supply two buffers, each MAXPATHLEN size.
*/
int
hp_path(hp_node_t node, char *path, char *connection)
{
hp_node_t root = NULL;
hp_node_t parent;
int i;
char *s;
char components[MAXPATHLEN];
i_hp_dprintf("hp_path: node=%p, path=%p, connection=%p\n", (void *)node,
(void *)path, (void *)connection);
if ((node == NULL) || (path == NULL) || (connection == NULL)) {
i_hp_dprintf("hp_path: invalid arguments.\n");
return (EINVAL);
}
(void) memset(path, 0, MAXPATHLEN);
(void) memset(connection, 0, MAXPATHLEN);
(void) memset(components, 0, MAXPATHLEN);
/* Set 'connection' only for connectors and ports */
if ((node->hp_type == HP_NODE_CONNECTOR) ||
(node->hp_type == HP_NODE_PORT))
(void) strlcpy(connection, node->hp_name, MAXPATHLEN);
/* Trace back to the root node, accumulating components */
for (parent = node; parent != NULL; parent = parent->hp_parent) {
if (parent->hp_type == HP_NODE_DEVICE) {
(void) strlcat(components, "/", MAXPATHLEN);
(void) strlcat(components, parent->hp_name, MAXPATHLEN);
}
if (parent->hp_parent == NULL)
root = parent;
}
/* Ensure the snapshot actually contains a base path */
if (root->hp_basepath == NULL) {
i_hp_dprintf("hp_path: missing base pathname.\n");
return (EFAULT);
}
/*
* Construct the path. Start with the base path from the root
* node, then append the accumulated components in reverse order.
*/
if (strcmp(root->hp_basepath, "/") != 0) {
(void) strlcat(path, root->hp_basepath, MAXPATHLEN);
if ((root->hp_type == HP_NODE_DEVICE) &&
((s = strrchr(path, '/')) != NULL))
*s = '\0';
}
for (i = strlen(components) - 1; i >= 0; i--) {
if (components[i] == '/') {
(void) strlcat(path, &components[i], MAXPATHLEN);
components[i] = '\0';
}
}
return (0);
}
/*
* hp_set_state()
*
* Initiate a state change operation on a node.
*/
int
hp_set_state(hp_node_t node, uint_t flags, int state, hp_node_t *resultsp)
{
hp_node_t root = NULL;
nvlist_t *args;
nvlist_t *results;
int rv;
char path[MAXPATHLEN];
char connection[MAXPATHLEN];
i_hp_dprintf("hp_set_state: node=%p, flags=0x%x, state=0x%x, "
"resultsp=%p\n", (void *)node, flags, state, (void *)resultsp);
/* Check arguments */
if ((node == NULL) || (resultsp == NULL) ||
!HP_SET_STATE_FLAGS_VALID(flags)) {
i_hp_dprintf("hp_set_state: invalid arguments.\n");
return (EINVAL);
}
/* Check node type */
if ((node->hp_type != HP_NODE_CONNECTOR) &&
(node->hp_type != HP_NODE_PORT)) {
i_hp_dprintf("hp_set_state: operation not supported.\n");
return (ENOTSUP);
}
/* Check that target state is valid */
switch (state) {
case DDI_HP_CN_STATE_PRESENT:
case DDI_HP_CN_STATE_POWERED:
case DDI_HP_CN_STATE_ENABLED:
if (node->hp_type != HP_NODE_CONNECTOR) {
i_hp_dprintf("hp_set_state: mismatched target.\n");
return (ENOTSUP);
}
break;
case DDI_HP_CN_STATE_PORT_PRESENT:
case DDI_HP_CN_STATE_OFFLINE:
case DDI_HP_CN_STATE_ONLINE:
if (node->hp_type != HP_NODE_PORT) {
i_hp_dprintf("hp_set_state: mismatched target.\n");
return (ENOTSUP);
}
break;
default:
i_hp_dprintf("hp_set_state: invalid target state.\n");
return (EINVAL);
}
/* Get path and connection of specified node */
if ((rv = hp_path(node, path, connection)) != 0)
return (rv);
/* Build arguments for door call */
if ((args = i_hp_set_args(HP_CMD_CHANGESTATE, path, connection, flags,
NULL, state)) == NULL)
return (ENOMEM);
/* Make the door call to hotplugd */
rv = i_hp_call_hotplugd(args, &results);
/* Arguments no longer needed */
nvlist_free(args);
/* Parse additional results, if any */
if ((rv == 0) && (results != NULL)) {
rv = i_hp_parse_results(results, &root, NULL);
nvlist_free(results);
*resultsp = root;
}
/* Done */
return (rv);
}
/*
* hp_set_private()
*
* Set bus private options on the hotplug connection
* indicated by the given hotplug information node.
*/
int
hp_set_private(hp_node_t node, const char *options, char **resultsp)
{
int rv;
nvlist_t *args;
nvlist_t *results;
char *values = NULL;
char path[MAXPATHLEN];
char connection[MAXPATHLEN];
i_hp_dprintf("hp_set_private: node=%p, options=%p, resultsp=%p\n",
(void *)node, (void *)options, (void *)resultsp);
/* Check arguments */
if ((node == NULL) || (options == NULL) || (resultsp == NULL)) {
i_hp_dprintf("hp_set_private: invalid arguments.\n");
return (EINVAL);
}
/* Check node type */
if (node->hp_type != HP_NODE_CONNECTOR) {
i_hp_dprintf("hp_set_private: operation not supported.\n");
return (ENOTSUP);
}
/* Initialize results */
*resultsp = NULL;
/* Get path and connection of specified node */
if ((rv = hp_path(node, path, connection)) != 0)
return (rv);
/* Build arguments for door call */
if ((args = i_hp_set_args(HP_CMD_SETPRIVATE, path, connection, 0,
options, 0)) == NULL)
return (ENOMEM);
/* Make the door call to hotplugd */
rv = i_hp_call_hotplugd(args, &results);
/* Arguments no longer needed */
nvlist_free(args);
/* Parse additional results, if any */
if ((rv == 0) && (results != NULL)) {
rv = i_hp_parse_results(results, NULL, &values);
nvlist_free(results);
*resultsp = values;
}
/* Done */
return (rv);
}
/*
* hp_get_private()
*
* Get bus private options on the hotplug connection
* indicated by the given hotplug information node.
*/
int
hp_get_private(hp_node_t node, const char *options, char **resultsp)
{
int rv;
nvlist_t *args;
nvlist_t *results;
char *values = NULL;
char path[MAXPATHLEN];
char connection[MAXPATHLEN];
i_hp_dprintf("hp_get_private: node=%p, options=%p, resultsp=%p\n",
(void *)node, (void *)options, (void *)resultsp);
/* Check arguments */
if ((node == NULL) || (options == NULL) || (resultsp == NULL)) {
i_hp_dprintf("hp_get_private: invalid arguments.\n");
return (EINVAL);
}
/* Check node type */
if (node->hp_type != HP_NODE_CONNECTOR) {
i_hp_dprintf("hp_get_private: operation not supported.\n");
return (ENOTSUP);
}
/* Initialize results */
*resultsp = NULL;
/* Get path and connection of specified node */
if ((rv = hp_path(node, path, connection)) != 0)
return (rv);
/* Build arguments for door call */
if ((args = i_hp_set_args(HP_CMD_GETPRIVATE, path, connection, 0,
options, 0)) == NULL)
return (ENOMEM);
/* Make the door call to hotplugd */
rv = i_hp_call_hotplugd(args, &results);
/* Arguments no longer needed */
nvlist_free(args);
/* Parse additional results, if any */
if ((rv == 0) && (results != NULL)) {
rv = i_hp_parse_results(results, NULL, &values);
nvlist_free(results);
*resultsp = values;
}
/* Done */
return (rv);
}
/*
* hp_pack()
*
* Given the root of a hotplug information snapshot, pack
* it into a contiguous byte array so that it is suitable
* for network transport.
*/
int
hp_pack(hp_node_t root, char **bufp, size_t *lenp)
{
hp_node_t node;
nvlist_t *nvl;
char *buf;
size_t len;
int rv;
i_hp_dprintf("hp_pack: root=%p, bufp=%p, lenp=%p\n", (void *)root,
(void *)bufp, (void *)lenp);
if ((root == NULL) || (bufp == NULL) || (lenp == NULL)) {
i_hp_dprintf("hp_pack: invalid arguments.\n");
return (EINVAL);
}
*lenp = 0;
*bufp = NULL;
if (nvlist_alloc(&nvl, 0, 0) != 0) {
i_hp_dprintf("hp_pack: nvlist_alloc() failed (%s).\n",
strerror(errno));
return (ENOMEM);
}
if (root->hp_basepath != NULL) {
rv = nvlist_add_string(nvl, HP_INFO_BASE, root->hp_basepath);
if (rv != 0) {
nvlist_free(nvl);
return (rv);
}
}
for (node = root; node != NULL; node = node->hp_sibling) {
if ((rv = i_hp_pack_branch(node, &buf, &len)) == 0) {
rv = nvlist_add_byte_array(nvl, HP_INFO_BRANCH,
(uchar_t *)buf, len);
free(buf);
}
if (rv != 0) {
nvlist_free(nvl);
return (rv);
}
}
len = 0;
buf = NULL;
if ((rv = nvlist_pack(nvl, &buf, &len, NV_ENCODE_NATIVE, 0)) == 0) {
*lenp = len;
*bufp = buf;
}
nvlist_free(nvl);
return (rv);
}
/*
* hp_unpack()
*
* Unpack a hotplug information snapshot for normal usage.
*/
int
hp_unpack(char *packed_buf, size_t packed_len, hp_node_t *retp)
{
hp_node_t root;
hp_node_t root_list = NULL;
hp_node_t prev_root = NULL;
nvlist_t *nvl = NULL;
nvpair_t *nvp;
char *basepath = NULL;
int rv;
i_hp_dprintf("hp_unpack: packed_buf=%p, packed_len=%u, retp=%p\n",
(void *)packed_buf, (uint32_t)packed_len, (void *)retp);
if ((packed_buf == NULL) || (packed_len == 0) || (retp == NULL)) {
i_hp_dprintf("hp_unpack: invalid arguments.\n");
return (EINVAL);
}
if ((rv = nvlist_unpack(packed_buf, packed_len, &nvl, 0)) != 0)
return (rv);
if (nvlist_next_nvpair(nvl, NULL) == NULL) {
nvlist_free(nvl);
errno = EINVAL;
return (0);
}
for (nvp = NULL; nvp = nvlist_next_nvpair(nvl, nvp); ) {
rv = EINVAL;
if (strcmp(nvpair_name(nvp), HP_INFO_BASE) == 0) {
char *val_string;
if ((rv = nvpair_value_string(nvp, &val_string)) == 0) {
if ((basepath = strdup(val_string)) == NULL)
rv = ENOMEM;
}
} else if (strcmp(nvpair_name(nvp), HP_INFO_BRANCH) == 0) {
size_t len = 0;
char *buf = NULL;
if ((rv = nvpair_value_byte_array(nvp,
(uchar_t **)&buf, (uint_t *)&len)) == 0) {
rv = i_hp_unpack_branch(buf, len, NULL, &root);
}
if (rv == 0) {
if (prev_root) {
prev_root->hp_sibling = root;
} else {
root_list = root;
}
prev_root = root;
}
}
if (rv != 0) {
if (basepath)
free(basepath);
nvlist_free(nvl);
hp_fini(root_list);
*retp = NULL;
return (rv);
}
}
/* Store the base path in each root node */
if (basepath) {
for (root = root_list; root; root = root->hp_sibling)
root->hp_basepath = basepath;
}
nvlist_free(nvl);
*retp = root_list;
return (0);
}
/*
* i_hp_dprintf()
*
* Print debug messages to stderr, but only when the debug flag
* (libhotplug_debug) is set.
*/
/*PRINTFLIKE1*/
static void
i_hp_dprintf(const char *fmt, ...)
{
va_list ap;
if (libhotplug_debug) {
va_start(ap, fmt);
(void) vfprintf(stderr, fmt, ap);
va_end(ap);
}
}
/*
* i_hp_pack_branch()
*
* Pack an individual branch of a hotplug information snapshot.
*/
static int
i_hp_pack_branch(hp_node_t root, char **bufp, size_t *lenp)
{
hp_node_t child;
nvlist_t *nvl;
char *buf;
size_t len;
int rv;
*lenp = 0;
*bufp = NULL;
/* Allocate an nvlist for this branch */
if (nvlist_alloc(&nvl, 0, 0) != 0)
return (ENOMEM);
/* Pack the root of the branch and add it to the nvlist */
if ((rv = i_hp_pack_node(root, &buf, &len)) == 0) {
rv = nvlist_add_byte_array(nvl, HP_INFO_NODE,
(uchar_t *)buf, len);
free(buf);
}
if (rv != 0) {
nvlist_free(nvl);
return (rv);
}
/* Pack each subordinate branch, and add it to the nvlist */
for (child = root->hp_child; child != NULL; child = child->hp_sibling) {
if ((rv = i_hp_pack_branch(child, &buf, &len)) == 0) {
rv = nvlist_add_byte_array(nvl, HP_INFO_BRANCH,
(uchar_t *)buf, len);
free(buf);
}
if (rv != 0) {
nvlist_free(nvl);
return (rv);
}
}
/* Pack the resulting nvlist into a single buffer */
len = 0;
buf = NULL;
if ((rv = nvlist_pack(nvl, &buf, &len, NV_ENCODE_NATIVE, 0)) == 0) {
*lenp = len;
*bufp = buf;
}
/* Free the nvlist */
nvlist_free(nvl);
return (rv);
}
/*
* i_hp_pack_node()
*
* Pack an individual node of a hotplug information snapshot.
*/
static int
i_hp_pack_node(hp_node_t node, char **bufp, size_t *lenp)
{
nvlist_t *nvl;
char *buf = NULL;
size_t len = 0;
int rv;
if (nvlist_alloc(&nvl, 0, 0) != 0)
return (ENOMEM);
if ((rv = nvlist_add_uint32(nvl, HP_INFO_TYPE,
(uint32_t)node->hp_type)) != 0)
goto fail;
if ((node->hp_name) &&
((rv = nvlist_add_string(nvl, HP_INFO_NAME, node->hp_name)) != 0))
goto fail;
if ((node->hp_usage) &&
((rv = nvlist_add_string(nvl, HP_INFO_USAGE, node->hp_usage)) != 0))
goto fail;
if ((node->hp_description) &&
((rv = nvlist_add_string(nvl, HP_INFO_DESC,
node->hp_description)) != 0))
goto fail;
if ((rv = nvlist_add_uint32(nvl, HP_INFO_STATE, node->hp_state)) != 0)
goto fail;
if ((node->hp_last_change != 0) &&
((rv = nvlist_add_uint32(nvl, HP_INFO_TIME,
node->hp_last_change)) != 0))
goto fail;
if ((rv = nvlist_pack(nvl, &buf, &len, NV_ENCODE_NATIVE, 0)) != 0)
goto fail;
*bufp = buf;
*lenp = len;
nvlist_free(nvl);
return (0);
fail:
*bufp = NULL;
*lenp = 0;
nvlist_free(nvl);
return (rv);
}
/*
* i_hp_unpack_branch()
*
* Unpack a branch of hotplug information nodes.
*/
static int
i_hp_unpack_branch(char *packed_buf, size_t packed_len, hp_node_t parent,
hp_node_t *retp)
{
hp_node_t node = NULL;
hp_node_t child;
hp_node_t prev_child = NULL;
nvlist_t *nvl = NULL;
nvpair_t *nvp;
char *buf;
size_t len;
int rv;
/* Initialize results */
*retp = NULL;
/* Unpack the nvlist for this branch */
if ((rv = nvlist_unpack(packed_buf, packed_len, &nvl, 0)) != 0)
return (rv);
/*
* Unpack the branch. The first item in the nvlist is
* always the root node. And zero or more subordinate
* branches may be packed afterward.
*/
for (nvp = NULL; nvp = nvlist_next_nvpair(nvl, nvp); ) {
len = 0;
buf = NULL;
if (strcmp(nvpair_name(nvp), HP_INFO_NODE) == 0) {
/* Check that there is only one root node */
if (node != NULL) {
hp_fini(node);
nvlist_free(nvl);
return (EFAULT);
}
if ((rv = nvpair_value_byte_array(nvp, (uchar_t **)&buf,
(uint_t *)&len)) == 0)
rv = i_hp_unpack_node(buf, len, parent, &node);
if (rv != 0) {
nvlist_free(nvl);
return (rv);
}
} else if (strcmp(nvpair_name(nvp), HP_INFO_BRANCH) == 0) {
if ((rv = nvpair_value_byte_array(nvp, (uchar_t **)&buf,
(uint_t *)&len)) == 0)
rv = i_hp_unpack_branch(buf, len, node, &child);
if (rv != 0) {
hp_fini(node);
nvlist_free(nvl);
return (rv);
}
if (prev_child) {
prev_child->hp_sibling = child;
} else {
node->hp_child = child;
}
prev_child = child;
}
}
nvlist_free(nvl);
*retp = node;
return (0);
}
/*
* i_hp_unpack_node()
*
* Unpack an individual hotplug information node.
*/
static int
i_hp_unpack_node(char *buf, size_t len, hp_node_t parent, hp_node_t *retp)
{
hp_node_t node;
nvlist_t *nvl;
nvpair_t *nvp;
uint32_t val_uint32;
char *val_string;
int rv = 0;
/* Initialize results */
*retp = NULL;
/* Unpack node into an nvlist */
if ((nvlist_unpack(buf, len, &nvl, 0) != 0))
return (EINVAL);
/* Allocate the new node */
if ((node = (hp_node_t)calloc(1, sizeof (struct hp_node))) == NULL) {
nvlist_free(nvl);
return (ENOMEM);
}
/* Iterate through nvlist, unpacking each field */
for (nvp = NULL; nvp = nvlist_next_nvpair(nvl, nvp); ) {
if ((strcmp(nvpair_name(nvp), HP_INFO_TYPE) == 0) &&
(nvpair_type(nvp) == DATA_TYPE_UINT32)) {
(void) nvpair_value_uint32(nvp, &val_uint32);
node->hp_type = val_uint32;
} else if ((strcmp(nvpair_name(nvp), HP_INFO_NAME) == 0) &&
(nvpair_type(nvp) == DATA_TYPE_STRING)) {
(void) nvpair_value_string(nvp, &val_string);
if ((node->hp_name = strdup(val_string)) == NULL) {
rv = ENOMEM;
break;
}
} else if ((strcmp(nvpair_name(nvp), HP_INFO_STATE) == 0) &&
(nvpair_type(nvp) == DATA_TYPE_UINT32)) {
(void) nvpair_value_uint32(nvp, &val_uint32);
node->hp_state = val_uint32;
} else if ((strcmp(nvpair_name(nvp), HP_INFO_USAGE) == 0) &&
(nvpair_type(nvp) == DATA_TYPE_STRING)) {
(void) nvpair_value_string(nvp, &val_string);
if ((node->hp_usage = strdup(val_string)) == NULL) {
rv = ENOMEM;
break;
}
} else if ((strcmp(nvpair_name(nvp), HP_INFO_DESC) == 0) &&
(nvpair_type(nvp) == DATA_TYPE_STRING)) {
(void) nvpair_value_string(nvp, &val_string);
if ((node->hp_description = strdup(val_string))
== NULL) {
rv = ENOMEM;
break;
}
} else if ((strcmp(nvpair_name(nvp), HP_INFO_TIME) == 0) &&
(nvpair_type(nvp) == DATA_TYPE_UINT32)) {
(void) nvpair_value_uint32(nvp, &val_uint32);
node->hp_last_change = (time_t)val_uint32;
} else {
i_hp_dprintf("i_hp_unpack_node: unrecognized: '%s'\n",
nvpair_name(nvp));
}
}
/* Unpacked nvlist no longer needed */
nvlist_free(nvl);
/* Check for errors */
if (rv != 0) {
hp_fini(node);
return (rv);
}
/* Success */
node->hp_parent = parent;
*retp = node;
return (0);
}
/*
* i_hp_call_hotplugd()
*
* Perform a door call to the hotplug daemon.
*/
static int
i_hp_call_hotplugd(nvlist_t *args, nvlist_t **resultsp)
{
door_arg_t door_arg;
nvlist_t *results = NULL;
char *buf = NULL;
size_t len = 0;
uint64_t seqnum;
int door_fd;
int rv;
/* Initialize results */
*resultsp = NULL;
/* Open door */
if ((door_fd = open(HOTPLUGD_DOOR, O_RDONLY)) < 0) {
i_hp_dprintf("i_hp_call_hotplugd: cannot open door (%s)\n",
strerror(errno));
return (EBADF);
}
/* Pack the nvlist of arguments */
if ((rv = nvlist_pack(args, &buf, &len, NV_ENCODE_NATIVE, 0)) != 0) {
i_hp_dprintf("i_hp_call_hotplugd: cannot pack arguments (%s)\n",
strerror(rv));
return (rv);
}
/* Set the door argument using the packed arguments */
door_arg.data_ptr = buf;
door_arg.data_size = len;
door_arg.desc_ptr = NULL;
door_arg.desc_num = 0;
door_arg.rbuf = (char *)(uintptr_t)&rv;
door_arg.rsize = sizeof (rv);
/* Attempt the door call */
if (door_call(door_fd, &door_arg) != 0) {
rv = errno;
i_hp_dprintf("i_hp_call_hotplugd: door call failed (%s)\n",
strerror(rv));
(void) close(door_fd);
free(buf);
return (rv);
}
/* The arguments are no longer needed */
free(buf);
/*
* If results are not in the original buffer provided,
* then check and process the new results buffer.
*/
if (door_arg.rbuf != (char *)(uintptr_t)&rv) {
/*
* First check that the buffer is valid. Then check for
* the simple case where a short result code was sent.
* The last case is a packed nvlist was returned, which
* needs to be unpacked.
*/
if ((door_arg.rbuf == NULL) ||
(door_arg.data_size < sizeof (rv))) {
i_hp_dprintf("i_hp_call_hotplugd: invalid results.\n");
rv = EFAULT;
} else if (door_arg.data_size == sizeof (rv)) {
rv = *(int *)(uintptr_t)door_arg.rbuf;
} else if ((rv = nvlist_unpack(door_arg.rbuf,
door_arg.data_size, &results, 0)) != 0) {
i_hp_dprintf("i_hp_call_hotplugd: "
"cannot unpack results (%s).\n", strerror(rv));
results = NULL;
rv = EFAULT;
}
/* Unmap the results buffer */
if (door_arg.rbuf != NULL)
(void) munmap(door_arg.rbuf, door_arg.rsize);
/*
* In the case of a packed nvlist, notify the daemon
* that it can free the result buffer from its heap.
*/
if ((results != NULL) &&
(nvlist_lookup_uint64(results, HPD_SEQNUM, &seqnum) == 0)) {
door_arg.data_ptr = (char *)(uintptr_t)&seqnum;
door_arg.data_size = sizeof (seqnum);
door_arg.desc_ptr = NULL;
door_arg.desc_num = 0;
door_arg.rbuf = NULL;
door_arg.rsize = 0;
(void) door_call(door_fd, &door_arg);
if (door_arg.rbuf != NULL)
(void) munmap(door_arg.rbuf, door_arg.rsize);
}
*resultsp = results;
}
(void) close(door_fd);
return (rv);
}
/*
* i_hp_set_args()
*
* Construct an nvlist of arguments for a hotplugd door call.
*/
static nvlist_t *
i_hp_set_args(hp_cmd_t cmd, const char *path, const char *connection,
uint_t flags, const char *options, int state)
{
nvlist_t *args;
/* Allocate a new nvlist */
if (nvlist_alloc(&args, NV_UNIQUE_NAME_TYPE, 0) != 0)
return (NULL);
/* Add common arguments */
if ((nvlist_add_int32(args, HPD_CMD, cmd) != 0) ||
(nvlist_add_string(args, HPD_PATH, path) != 0)) {
nvlist_free(args);
return (NULL);
}
/* Add connection, but only if defined */
if ((connection != NULL) && (connection[0] != '\0') &&
(nvlist_add_string(args, HPD_CONNECTION, connection) != 0)) {
nvlist_free(args);
return (NULL);
}
/* Add flags, but only if defined */
if ((flags != 0) && (nvlist_add_uint32(args, HPD_FLAGS, flags) != 0)) {
nvlist_free(args);
return (NULL);
}
/* Add options, but only if defined */
if ((options != NULL) &&
(nvlist_add_string(args, HPD_OPTIONS, options) != 0)) {
nvlist_free(args);
return (NULL);
}
/* Add state, but only for CHANGESTATE command */
if ((cmd == HP_CMD_CHANGESTATE) &&
(nvlist_add_int32(args, HPD_STATE, state) != 0)) {
nvlist_free(args);
return (NULL);
}
return (args);
}
/*
* i_hp_parse_results()
*
* Parse out individual fields of an nvlist of results from
* a hotplugd door call.
*/
static int
i_hp_parse_results(nvlist_t *results, hp_node_t *rootp, char **optionsp)
{
int rv;
/* Parse an information snapshot */
if (rootp) {
char *buf = NULL;
size_t len = 0;
*rootp = NULL;
if (nvlist_lookup_byte_array(results, HPD_INFO,
(uchar_t **)&buf, (uint_t *)&len) == 0) {
if ((rv = hp_unpack(buf, len, rootp)) != 0)
return (rv);
}
}
/* Parse a bus private option string */
if (optionsp) {
char *str;
*optionsp = NULL;
if ((nvlist_lookup_string(results, HPD_OPTIONS, &str) == 0) &&
((*optionsp = strdup(str)) == NULL)) {
return (ENOMEM);
}
}
/* Parse result code of the operation */
if (nvlist_lookup_int32(results, HPD_STATUS, &rv) != 0) {
i_hp_dprintf("i_hp_call_hotplugd: missing status.\n");
return (EFAULT);
}
return (rv);
}
|