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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (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 2005 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <fcntl.h>
#include <libdevinfo.h>
#include <stdio.h>
#include <sys/sunddi.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <libintl.h>
#include <locale.h>
#include <sys/debug.h>
#include "libdiskmgt.h"
#include "disks_private.h"
#include "partition.h"
extern char *getfullblkname();
extern dm_desc_type_t drive_assoc_types[];
extern dm_desc_type_t bus_assoc_types[];
extern dm_desc_type_t controller_assoc_types[];
extern dm_desc_type_t media_assoc_types[];
extern dm_desc_type_t slice_assoc_types[];
extern dm_desc_type_t partition_assoc_types[];
extern dm_desc_type_t path_assoc_types[];
extern dm_desc_type_t alias_assoc_types[];
static dm_descriptor_t *ptr_array_to_desc_array(descriptor_t **ptrs, int *errp);
static descriptor_t **desc_array_to_ptr_array(dm_descriptor_t *da, int *errp);
static int build_usage_string(char *dname, char *by, char *data, char **use,
int *found, int *errp);
void
dm_free_descriptor(dm_descriptor_t desc)
{
descriptor_t *dp;
if (desc == NULL) {
return;
}
dp = (descriptor_t *)(uintptr_t)desc;
cache_wlock();
cache_free_descriptor(dp);
cache_unlock();
}
void
dm_free_descriptors(dm_descriptor_t *desc_list)
{
descriptor_t **dp;
int error;
if (desc_list == NULL) {
return;
}
dp = desc_array_to_ptr_array(desc_list, &error);
if (error != 0) {
free(desc_list);
return;
}
cache_wlock();
cache_free_descriptors(dp);
cache_unlock();
}
/*ARGSUSED*/
void
dm_free_name(char *name)
{
free(name);
}
dm_descriptor_t *
dm_get_associated_descriptors(dm_descriptor_t desc, dm_desc_type_t type,
int *errp)
{
descriptor_t **descs = NULL;
descriptor_t *dp;
dp = (descriptor_t *)(uintptr_t)desc;
cache_wlock();
if (!cache_is_valid_desc(dp)) {
cache_unlock();
*errp = EBADF;
return (NULL);
}
/* verify that the descriptor is still valid */
if (dp->p.generic == NULL) {
cache_unlock();
*errp = ENODEV;
return (NULL);
}
switch (dp->type) {
case DM_DRIVE:
descs = drive_get_assoc_descriptors(dp, type, errp);
break;
case DM_BUS:
descs = bus_get_assoc_descriptors(dp, type, errp);
break;
case DM_CONTROLLER:
descs = controller_get_assoc_descriptors(dp, type, errp);
break;
case DM_MEDIA:
descs = media_get_assoc_descriptors(dp, type, errp);
break;
case DM_SLICE:
descs = slice_get_assoc_descriptors(dp, type, errp);
break;
case DM_PARTITION:
descs = partition_get_assoc_descriptors(dp, type, errp);
break;
case DM_PATH:
descs = path_get_assoc_descriptors(dp, type, errp);
break;
case DM_ALIAS:
descs = alias_get_assoc_descriptors(dp, type, errp);
break;
default:
*errp = EINVAL;
break;
}
cache_unlock();
return (ptr_array_to_desc_array(descs, errp));
}
dm_desc_type_t *
dm_get_associated_types(dm_desc_type_t type)
{
switch (type) {
case DM_DRIVE:
return (drive_assoc_types);
case DM_BUS:
return (bus_assoc_types);
case DM_CONTROLLER:
return (controller_assoc_types);
case DM_MEDIA:
return (media_assoc_types);
case DM_SLICE:
return (slice_assoc_types);
case DM_PARTITION:
return (partition_assoc_types);
case DM_PATH:
return (path_assoc_types);
case DM_ALIAS:
return (alias_assoc_types);
}
return (NULL);
}
nvlist_t *
dm_get_attributes(dm_descriptor_t desc, int *errp)
{
descriptor_t *dp;
nvlist_t *attrs = NULL;
dp = (descriptor_t *)(uintptr_t)desc;
cache_rlock();
if (!cache_is_valid_desc(dp)) {
cache_unlock();
*errp = EBADF;
return (NULL);
}
/* verify that the descriptor is still valid */
if (dp->p.generic == NULL) {
cache_unlock();
*errp = ENODEV;
return (NULL);
}
switch (dp->type) {
case DM_DRIVE:
attrs = drive_get_attributes(dp, errp);
break;
case DM_BUS:
attrs = bus_get_attributes(dp, errp);
break;
case DM_CONTROLLER:
attrs = controller_get_attributes(dp, errp);
break;
case DM_MEDIA:
attrs = media_get_attributes(dp, errp);
break;
case DM_SLICE:
attrs = slice_get_attributes(dp, errp);
break;
case DM_PARTITION:
attrs = partition_get_attributes(dp, errp);
break;
case DM_PATH:
attrs = path_get_attributes(dp, errp);
break;
case DM_ALIAS:
attrs = alias_get_attributes(dp, errp);
break;
default:
*errp = EINVAL;
break;
}
cache_unlock();
return (attrs);
}
dm_descriptor_t
dm_get_descriptor_by_name(dm_desc_type_t desc_type, char *name, int *errp)
{
dm_descriptor_t desc = NULL;
cache_wlock();
switch (desc_type) {
case DM_DRIVE:
desc = (uintptr_t)drive_get_descriptor_by_name(name, errp);
break;
case DM_BUS:
desc = (uintptr_t)bus_get_descriptor_by_name(name, errp);
break;
case DM_CONTROLLER:
desc = (uintptr_t)controller_get_descriptor_by_name(name,
errp);
break;
case DM_MEDIA:
desc = (uintptr_t)media_get_descriptor_by_name(name, errp);
break;
case DM_SLICE:
desc = (uintptr_t)slice_get_descriptor_by_name(name, errp);
break;
case DM_PARTITION:
desc = (uintptr_t)partition_get_descriptor_by_name(name,
errp);
break;
case DM_PATH:
desc = (uintptr_t)path_get_descriptor_by_name(name, errp);
break;
case DM_ALIAS:
desc = (uintptr_t)alias_get_descriptor_by_name(name, errp);
break;
default:
*errp = EINVAL;
break;
}
cache_unlock();
return (desc);
}
dm_descriptor_t *
dm_get_descriptors(dm_desc_type_t type, int filter[], int *errp)
{
descriptor_t **descs = NULL;
cache_wlock();
switch (type) {
case DM_DRIVE:
descs = drive_get_descriptors(filter, errp);
break;
case DM_BUS:
descs = bus_get_descriptors(filter, errp);
break;
case DM_CONTROLLER:
descs = controller_get_descriptors(filter, errp);
break;
case DM_MEDIA:
descs = media_get_descriptors(filter, errp);
break;
case DM_SLICE:
descs = slice_get_descriptors(filter, errp);
break;
case DM_PARTITION:
descs = partition_get_descriptors(filter, errp);
break;
case DM_PATH:
descs = path_get_descriptors(filter, errp);
break;
case DM_ALIAS:
descs = alias_get_descriptors(filter, errp);
break;
default:
*errp = EINVAL;
break;
}
cache_unlock();
return (ptr_array_to_desc_array(descs, errp));
}
char *
dm_get_name(dm_descriptor_t desc, int *errp)
{
descriptor_t *dp;
char *nm = NULL;
char *name = NULL;
dp = (descriptor_t *)(uintptr_t)desc;
cache_rlock();
if (!cache_is_valid_desc(dp)) {
cache_unlock();
*errp = EBADF;
return (NULL);
}
/* verify that the descriptor is still valid */
if (dp->p.generic == NULL) {
cache_unlock();
*errp = ENODEV;
return (NULL);
}
switch (dp->type) {
case DM_DRIVE:
nm = (drive_get_name(dp));
break;
case DM_BUS:
nm = (bus_get_name(dp));
break;
case DM_CONTROLLER:
nm = (controller_get_name(dp));
break;
case DM_MEDIA:
nm = (media_get_name(dp));
break;
case DM_SLICE:
nm = (slice_get_name(dp));
break;
case DM_PARTITION:
nm = (partition_get_name(dp));
break;
case DM_PATH:
nm = (path_get_name(dp));
break;
case DM_ALIAS:
nm = (alias_get_name(dp));
break;
}
cache_unlock();
*errp = 0;
if (nm != NULL) {
name = strdup(nm);
if (name == NULL) {
*errp = ENOMEM;
return (NULL);
}
return (name);
}
return (NULL);
}
nvlist_t *
dm_get_stats(dm_descriptor_t desc, int stat_type, int *errp)
{
descriptor_t *dp;
nvlist_t *stats = NULL;
dp = (descriptor_t *)(uintptr_t)desc;
cache_rlock();
if (!cache_is_valid_desc(dp)) {
cache_unlock();
*errp = EBADF;
return (NULL);
}
/* verify that the descriptor is still valid */
if (dp->p.generic == NULL) {
cache_unlock();
*errp = ENODEV;
return (NULL);
}
switch (dp->type) {
case DM_DRIVE:
stats = drive_get_stats(dp, stat_type, errp);
break;
case DM_BUS:
stats = bus_get_stats(dp, stat_type, errp);
break;
case DM_CONTROLLER:
stats = controller_get_stats(dp, stat_type, errp);
break;
case DM_MEDIA:
stats = media_get_stats(dp, stat_type, errp);
break;
case DM_SLICE:
if (stat_type == DM_SLICE_STAT_USE) {
/*
* If NOINUSE_CHECK is set, we do not perform
* the in use checking if the user has set stat_type
* DM_SLICE_STAT_USE
*/
if (getenv("NOINUSE_CHECK") != NULL) {
stats = NULL;
break;
}
}
stats = slice_get_stats(dp, stat_type, errp);
break;
case DM_PARTITION:
stats = partition_get_stats(dp, stat_type, errp);
break;
case DM_PATH:
stats = path_get_stats(dp, stat_type, errp);
break;
case DM_ALIAS:
stats = alias_get_stats(dp, stat_type, errp);
break;
default:
*errp = EINVAL;
break;
}
cache_unlock();
return (stats);
}
dm_desc_type_t
dm_get_type(dm_descriptor_t desc)
{
descriptor_t *dp;
dp = (descriptor_t *)(uintptr_t)desc;
cache_rlock();
if (!cache_is_valid_desc(dp)) {
cache_unlock();
return (-1);
}
cache_unlock();
return (dp->type);
}
/*
* Returns, via slices paramater, a dm_descriptor_t list of
* slices for the named disk drive.
*/
void
dm_get_slices(char *drive, dm_descriptor_t **slices, int *errp)
{
dm_descriptor_t alias;
dm_descriptor_t *media;
dm_descriptor_t *disk;
*slices = NULL;
*errp = 0;
if (drive == NULL) {
return;
}
alias = dm_get_descriptor_by_name(DM_ALIAS, drive, errp);
/*
* Errors must be handled by the caller. The dm_descriptor_t *
* values will be NULL if an error occured in these calls.
*/
if (alias != NULL) {
disk = dm_get_associated_descriptors(alias, DM_DRIVE, errp);
dm_free_descriptor(alias);
if (disk != NULL) {
media = dm_get_associated_descriptors(*disk,
DM_MEDIA, errp);
dm_free_descriptors(disk);
if (media != NULL) {
*slices = dm_get_associated_descriptors(*media,
DM_SLICE, errp);
dm_free_descriptors(media);
}
}
}
}
/*
* Convenience function to get slice stats
*/
void
dm_get_slice_stats(char *slice, nvlist_t **dev_stats, int *errp)
{
dm_descriptor_t devp;
*dev_stats = NULL;
*errp = 0;
if (slice == NULL) {
return;
}
/*
* Errors must be handled by the caller. The dm_descriptor_t *
* values will be NULL if an error occured in these calls.
*/
devp = dm_get_descriptor_by_name(DM_SLICE, slice, errp);
if (devp != NULL) {
*dev_stats = dm_get_stats(devp, DM_SLICE_STAT_USE,
errp);
dm_free_descriptor(devp);
}
}
/*
* Returns 'in use' details, if found, about a specific dev_name,
* based on the caller(who). It is important to note that it is possible
* for there to be more than one 'in use' statistic regarding a dev_name.
* The **msg parameter returns a list of 'in use' details. This message
* is formatted via gettext().
*/
int
dm_inuse(char *dev_name, char **msg, dm_who_type_t who, int *errp)
{
nvlist_t *dev_stats = NULL;
char *by, *data;
nvpair_t *nvwhat = NULL;
nvpair_t *nvdesc = NULL;
int found = 0;
char *dname = NULL;
*errp = 0;
*msg = NULL;
dname = getfullblkname(dev_name);
/*
* If we cannot find the block name, we cannot check the device
* for in use statistics. So, return found, which is == 0.
*/
if (dname == NULL || *dname == '\0') {
return (found);
}
dm_get_slice_stats(dname, &dev_stats, errp);
if (dev_stats == NULL) {
/*
* If there is an error, but it isn't a no device found error
* return the error as recorded. Otherwise, with a full
* block name, we might not be able to get the slice
* associated, and will get an ENODEV error. For example,
* an SVM metadevice will return a value from getfullblkname()
* but libdiskmgt won't be able to find this device for
* statistics gathering. This is expected and we should not
* report errnoneous errors.
*/
if (*errp) {
if (*errp == ENODEV) {
*errp = 0;
}
}
free(dname);
return (found);
}
for (;;) {
nvwhat = nvlist_next_nvpair(dev_stats, nvdesc);
nvdesc = nvlist_next_nvpair(dev_stats, nvwhat);
/*
* End of the list found.
*/
if (nvwhat == NULL || nvdesc == NULL) {
break;
}
/*
* Otherwise, we check to see if this client(who) cares
* about this in use scenario
*/
ASSERT(strcmp(nvpair_name(nvwhat), DM_USED_BY) == 0);
ASSERT(strcmp(nvpair_name(nvdesc), DM_USED_NAME) == 0);
/*
* If we error getting the string value continue on
* to the next pair(if there is one)
*/
if (nvpair_value_string(nvwhat, &by)) {
continue;
}
if (nvpair_value_string(nvdesc, &data)) {
continue;
}
switch (who) {
case DM_WHO_MKFS:
/*
* mkfs is not in use for these cases.
* All others are in use.
*/
if (strcmp(by, DM_USE_LU) == 0 ||
strcmp(by, DM_USE_FS) == 0) {
break;
}
if (build_usage_string(dname,
by, data, msg, &found, errp) != 0) {
if (*errp) {
goto out;
}
}
break;
case DM_WHO_SWAP:
/*
* Not in use for this.
*/
if (strcmp(by, DM_USE_DUMP) == 0 ||
strcmp(by, DM_USE_FS) == 0) {
break;
}
if (build_usage_string(dname,
by, data, msg, &found, errp) != 0) {
if (*errp) {
goto out;
}
}
break;
case DM_WHO_DUMP:
/*
* Not in use for this.
*/
if ((strcmp(by, DM_USE_MOUNT) == 0 &&
strcmp(data, "swap") == 0) ||
strcmp(by, DM_USE_DUMP) == 0 ||
strcmp(by, DM_USE_FS) == 0) {
break;
}
if (build_usage_string(dname,
by, data, msg, &found, errp)) {
if (*errp) {
goto out;
}
}
break;
case DM_WHO_FORMAT:
if (strcmp(by, DM_USE_FS) == 0)
break;
if (build_usage_string(dname,
by, data, msg, &found, errp) != 0) {
if (*errp) {
goto out;
}
}
break;
default:
/*
* nothing found in use for this client
* of libdiskmgt. Default is 'not in use'.
*/
break;
}
}
out:
if (dname != NULL)
free(dname);
if (dev_stats != NULL)
nvlist_free(dev_stats);
return (found);
}
void
dm_get_usage_string(char *what, char *how, char **usage_string)
{
if (usage_string == NULL || what == NULL) {
return;
}
*usage_string = NULL;
if (strcmp(what, DM_USE_MOUNT) == 0) {
if (strcmp(how, "swap") == 0) {
*usage_string = dgettext(TEXT_DOMAIN,
"%s is currently used by swap. Please see swap(1M)."
"\n");
} else {
*usage_string = dgettext(TEXT_DOMAIN,
"%s is currently mounted on %s."
" Please see umount(1M).\n");
}
} else if (strcmp(what, DM_USE_VFSTAB) == 0) {
*usage_string = dgettext(TEXT_DOMAIN,
"%s is normally mounted on %s according to /etc/vfstab. "
"Please remove this entry to use this device.\n");
} else if (strcmp(what, DM_USE_FS) == 0) {
*usage_string = dgettext(TEXT_DOMAIN,
"Warning: %s contains a %s filesystem.\n");
} else if (strcmp(what, DM_USE_SVM) == 0) {
if (strcmp(how, "mdb") == 0) {
*usage_string = dgettext(TEXT_DOMAIN,
"%s contains an SVM %s. Please see "
"metadb(1M).\n");
} else {
*usage_string = dgettext(TEXT_DOMAIN,
"%s is part of SVM volume %s. "
"Please see metaclear(1M).\n");
}
} else if (strcmp(what, DM_USE_VXVM) == 0) {
*usage_string = dgettext(TEXT_DOMAIN,
"%s is part of VxVM volume %s.\n");
} else if (strcmp(what, DM_USE_LU) == 0) {
*usage_string = dgettext(TEXT_DOMAIN,
"%s is in use for live upgrade %s. Please see ludelete(1M)."
"\n");
} else if (strcmp(what, DM_USE_DUMP) == 0) {
*usage_string = dgettext(TEXT_DOMAIN,
"%s is in use by %s. Please see dumpadm(1M)."
"\n");
} else if (strcmp(what, DM_USE_ZPOOL) == 0) {
*usage_string = dgettext(TEXT_DOMAIN,
"%s is in use by zpool %s. Please see zpool(1M)."
"\n");
}
}
void
libdiskmgt_add_str(nvlist_t *attrs, char *name, char *val, int *errp)
{
if (*errp == 0) {
*errp = nvlist_add_string(attrs, name, val);
}
}
descriptor_t **
libdiskmgt_empty_desc_array(int *errp)
{
descriptor_t **empty;
empty = (descriptor_t **)calloc(1, sizeof (descriptor_t *));
if (empty == NULL) {
*errp = ENOMEM;
return (NULL);
}
empty[0] = NULL;
*errp = 0;
return (empty);
}
void
libdiskmgt_init_debug()
{
char *valp;
if ((valp = getenv(DM_DEBUG)) != NULL) {
dm_debug = atoi(valp);
}
}
int
libdiskmgt_str_eq(char *nm1, char *nm2)
{
if (nm1 == NULL) {
if (dm_debug) {
(void) fprintf(stderr, "WARNING: str_eq nm1 NULL\n");
}
if (nm2 == NULL) {
return (1);
} else {
return (0);
}
}
/* nm1 != NULL */
if (nm2 == NULL) {
if (dm_debug) {
(void) fprintf(stderr, "WARNING: str_eq nm2 NULL\n");
}
return (0);
}
if (strcmp(nm1, nm2) == 0) {
return (1);
}
return (0);
}
/*ARGSUSED*/
static descriptor_t **
desc_array_to_ptr_array(dm_descriptor_t *descs, int *errp)
{
#ifdef _LP64
return ((descriptor_t **)descs);
#else
/* convert the 64 bit descriptors to 32 bit ptrs */
int cnt;
int i;
descriptor_t **da;
for (cnt = 0; descs[cnt]; cnt++);
da = (descriptor_t **)calloc(cnt + 1, sizeof (descriptor_t *));
if (da == NULL) {
*errp = ENOMEM;
return (NULL);
}
for (i = 0; descs[i]; i++) {
da[i] = (descriptor_t *)(uintptr_t)descs[i];
}
*errp = 0;
free(descs);
return (da);
#endif
}
/*ARGSUSED*/
static dm_descriptor_t *
ptr_array_to_desc_array(descriptor_t **ptrs, int *errp)
{
#ifdef _LP64
return ((dm_descriptor_t *)ptrs);
#else
/* convert the 32 bit ptrs to the 64 bit descriptors */
int cnt;
int i;
dm_descriptor_t *da;
if (*errp != 0 || ptrs == NULL) {
return (NULL);
}
for (cnt = 0; ptrs[cnt]; cnt++);
da = (dm_descriptor_t *)calloc(cnt + 1, sizeof (dm_descriptor_t));
if (da == NULL) {
*errp = ENOMEM;
return (NULL);
}
for (i = 0; ptrs[i]; i++) {
da[i] = (uintptr_t)ptrs[i];
}
*errp = 0;
free(ptrs);
return (da);
#endif
}
/*
* Build the usage string for the in use data. Return the build string in
* the msg parameter. This function takes care of reallocing all the memory
* for this usage string. Usage string is returned already formatted for
* localization.
*/
static int
build_usage_string(char *dname, char *by, char *data, char **msg,
int *found, int *errp)
{
int len0;
int len1;
char *use;
char *p;
*errp = 0;
dm_get_usage_string(by, data, &use);
if (!use) {
return (-1);
}
if (*msg)
len0 = strlen(*msg);
else
len0 = 0;
/* LINTED */
len1 = snprintf(NULL, 0, use, dname, data);
/*
* If multiple in use details they
* are listed 1 per line for ease of
* reading. dm_find_usage_string
* formats these appropriately.
*/
if ((p = realloc(*msg, len0 + len1 + 1)) == NULL) {
*errp = errno;
free(*msg);
return (-1);
}
*msg = p;
/* LINTED */
(void) snprintf(*msg + len0, len1 + 1, use, dname, data);
(*found)++;
return (0);
}
|