summaryrefslogtreecommitdiff
path: root/usr/src/cmd/rmformat/rmf_misc.c
blob: be6aafb150cd4e1263df5dda63eefa1586be2809 (plain)
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
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
/*
 * 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.
 */

/*
 * rmf_misc.c :
 *	Miscelleneous routines for rmformat.
 */

#include <sys/types.h>
#include <stdio.h>
#include <sys/mnttab.h>
#include <volmgt.h>
#include <sys/dkio.h>
#include <sys/fdio.h>
#include <sys/vtoc.h>
#include <sys/termios.h>
#include <sys/mount.h>
#include <ctype.h>
#include <signal.h>
#include <sys/wait.h>
#include <dirent.h>
#include <priv_utils.h>
#include <stdarg.h>
#include "rmformat.h"

/*
 * Definitions.
 */
#define	SENSE_KEY(rqbuf)	(rqbuf[2] & 0xf) /* scsi error category */
#define	ASC(rqbuf)		(rqbuf[12])	/* additional sense code */
#define	ASCQ(rqbuf)		(rqbuf[13])	/* ASC qualifier */

#define	DEFAULT_SCSI_TIMEOUT	60
#define	INQUIRY_CMD		0x12
#define	RQBUFLEN		32
#define	CD_RW			1		/* CD_RW/CD-R	*/
#define	WRITE_10_CMD		0x2A
#define	READ_INFO_CMD		0x51
#define	SYNC_CACHE_CMD		0x35
#define	CLOSE_TRACK_CMD 	0x5B
#define	MODE_SENSE_10_CMD	0x5A
#define	DEVFS_PREFIX		"/devices"

int		uscsi_error;		 /* used for debugging failed uscsi */
char		rqbuf[RQBUFLEN];
static uint_t	total_retries;
static struct	uscsi_cmd uscmd;
static char	ucdb[16];
uchar_t 	uscsi_status, rqstatus, rqresid;
int		total_devices_found = 0;
int		removable_found = 0;

extern char	*global_intr_msg;
extern int	vol_running;
extern char	*dev_name;
extern int32_t	m_flag;

/*
 * ON-private functions from libvolmgt
 */
int	_dev_mounted(char *path);

/*
 * Function prototypes.
 */
static int		my_umount(char *mountp);
static int		my_volrmmount(char *real_name);
static int		vol_name_to_dev_node(char *vname, char *found);
static int		vol_lookup(char *supplied, char *found);
static device_t		*get_device(char *user_supplied, char *node);
static char		*get_physical_name(char *path);
static int		lookup_device(char *supplied, char *found);
static void		fini_device(device_t *dev);
static int		is_cd(char *node);
void			*my_zalloc(size_t size);
void			err_msg(char *fmt, ...);
int			inquiry(int fd, uchar_t *inq);
struct uscsi_cmd	*get_uscsi_cmd(void);
int			uscsi(int fd, struct uscsi_cmd *scmd);
int			get_mode_page(int fd, int page_no, int pc, int buf_len,
			    uchar_t *buffer);
int			mode_sense(int fd, uchar_t pc, int dbd, int page_len,
			    uchar_t *buffer);
uint16_t		read_scsi16(void *addr);
int			check_device(device_t *dev, int cond);
static void		get_media_info(device_t *t_dev, char *sdev,
			    char *pname, char *sn);

extern void		process_p_flag(smedia_handle_t handle, int32_t fd);

void
my_perror(char *err_string)
{

	int error_no;
	if (errno == 0)
		return;

	error_no = errno;
	(void) fprintf(stderr, "%s", err_string);
	(void) fprintf(stderr, gettext(" : "));
	errno = error_no;
	perror("");
}

int32_t
get_confirmation()
{
	char c;

	(void) fprintf(stderr, gettext("Do you want to continue? (y/n)"));
	c = getchar();
	if (c == 'y' || c == 'Y')
		return (1);
	else if (c == 'n' || c == 'N')
		return (0);
	else {
		(void) fprintf(stderr, gettext("Invalid choice\n"));
		return (0);
	}
}


void
get_passwd(struct smwp_state *wp, int32_t confirm)
{
	char passwd[256], re_passwd[256];
	int32_t len;
	struct termios tio;
	int32_t echo_off = 0;
	FILE *in, *out;
	char *buf;


	in = fopen("/dev/tty", "r+");
	if (in == NULL) {
		in = stdin;
		out = stderr;
	} else {
		out = in;
	}

	/* Turn echoing off if it is on now.  */

	if (tcgetattr(fileno(in), &tio) < 0) {
		PERROR("Echo off ioctl failed");
		exit(1);
	}
	if (tio.c_lflag & ECHO) {
		tio.c_lflag &= ~ECHO;
		/* echo_off = tcsetattr(fileno(in), TCSAFLUSH, &tio) == 0; */
		echo_off = tcsetattr(fileno(in), TCSAFLUSH, &tio) == 0;
		tio.c_lflag |= ECHO;
	}

	/* CONSTCOND */
	while (1) {
		(void) fputs(
		    gettext("Please enter password (32 chars maximum):"),
		    out);
		(void) fflush(out);
		buf = fgets(passwd, (size_t)256, in);
		rewind(in);
		if (buf == NULL) {
			PERROR("Error reading password");
			continue;
		}
		len = strlen(passwd);
		(void) fputc('\n', out);
		len--;	/* To offset the \n */
		if ((len <= 0) || (len > 32)) {
			(void) fprintf(stderr,
			    gettext("Invalid length of password \n"));
			(void) fputs("Try again\n", out);
			continue;
		}

		if (!confirm)
			break;

		(void) fputs("Please reenter password:", out);
		(void) fflush(out);
		buf = fgets(re_passwd, (size_t)256, in);
		rewind(in);
		(void) fputc('\n', out);
		if ((buf == NULL) || strcmp(passwd, re_passwd)) {
			(void) fputs("passwords did not match\n", out);
			(void) fputs("Try again\n", out);
		} else {
			break;
		}
	}
	wp->sm_passwd_len = len;
	(void) strncpy(wp->sm_passwd, passwd, wp->sm_passwd_len);
	wp->sm_version = SMWP_STATE_V_1;

	/* Restore echoing.  */
	if (echo_off)
		(void) tcsetattr(fileno(in), TCSAFLUSH, &tio);

}

int32_t
check_and_unmount_vold(char *device_name, int32_t flag)
{
	char *real_name;
	char *nm;
	char tmp_path_name[PATH_MAX];
	struct stat stat_buf;
	int32_t ret_val = 0;
	struct	mnttab	*mntp;
	FILE	*fp;
	int nl;

	DPRINTF1("Device name %s\n", device_name);

	if (volmgt_running() == 0) {
		DPRINTF("Vold not running\n");
		return (0);
	}
	if ((nm = volmgt_symname(device_name)) == NULL) {
		DPRINTF("path not managed\n");
		real_name = media_findname(device_name);
	} else {
		DPRINTF1("path managed as %s\n", nm);
		real_name = media_findname(nm);
		DPRINTF1("real name %s\n", real_name);
	}

	if (real_name == NULL)
		return (-1);

	/*
	 * To find out whether the device has been mounted by
	 * volume manager...
	 *
	 * Convert the real name to a block device address.
	 * Do a partial match with the mnttab entries.
	 * Make sure the match is in the beginning to avoid if
	 * anybody puts a label similiar to volume manager path names.
	 * Then use "volrmmount -e <dev_name>" if -U flag is set.
	 */

	nl = strlen("/vol/dev/");

	if (strncmp(real_name, "/vol/dev/", nl) != 0)
			return (0);
	if (real_name[nl] == 'r') {
		(void) snprintf(tmp_path_name, PATH_MAX, "%s%s", "/vol/dev/",
		    &real_name[nl + 1]);
	} else {
		(void) snprintf(tmp_path_name, PATH_MAX, "%s", real_name);
	}
	DPRINTF1("%s \n", tmp_path_name);
	ret_val = stat(tmp_path_name, &stat_buf);
	if (ret_val < 0) {
		PERROR("Could not stat");
		return (-1);
	}

	fp = fopen("/etc/mnttab", "r");

	if (fp == NULL) {
		PERROR("Could not open /etc/mnttab");
		return (-1);
	}

	mntp = (struct mnttab *)malloc(sizeof (struct mnttab));
	if (mntp == NULL) {
		PERROR("malloc failed");
		(void) fclose(fp);
		return (-1);
	}
	errno = 0;
	while (getmntent(fp, mntp) == 0) {
		if (errno != 0) {
			PERROR("Error with mnttab");
			(void) fclose(fp);
			return (-1);
		}
		/* Is it a probable entry? */
		DPRINTF1(" %s \n", mntp->mnt_special);
		if (strstr(mntp->mnt_special, tmp_path_name) !=
		    mntp->mnt_special) {
			/* Skip to next entry */
			continue;
		} else {
			DPRINTF1("Found!! %s\n", mntp->mnt_special);
			ret_val = 1;
			break;
		}
	}

	if (ret_val == 1) {
		if (flag) {
			if (my_volrmmount(real_name) < 0) {
				ret_val = -1;
			}
		} else {
			ret_val = -1;
		}
	}
	(void) fclose(fp);
	free(mntp);
	return (ret_val);
}

/*
 * This routine checks if a device has mounted partitions. The
 * device name is assumed to be /dev/rdsk/cNtNdNsN. So, this can
 * be used for SCSI and PCMCIA cards.
 * Returns
 *	 0 : if not mounted
 *	 1 : if successfully unmounted
 *	-1 : Any error or umount failed
 */

int32_t
check_and_unmount_scsi(char *device_name, int32_t flag)
{

	struct	mnttab	*mntrefp;
	struct	mnttab	*mntp;
	FILE	*fp;
	char block_dev_name[PATH_MAX];
	char tmp_name[PATH_MAX];
	int32_t  i, j;
	int32_t unmounted = 0;

	/*
	 * If the device name is not a character special, anyway we
	 * can not progress further
	 */

	if (strncmp(device_name, "/dev/rdsk/c", strlen("/dev/rdsk/c")) != 0)
		return (0);

	(void) snprintf(block_dev_name, PATH_MAX, "/dev/%s",
	    &device_name[strlen("/dev/r")]);
	fp = fopen("/etc/mnttab", "r");

	if (fp == NULL) {
		PERROR("Could not open /etc/mnttab");
		return (-1);
	}

	mntrefp = (struct mnttab *)malloc(sizeof (struct mnttab));
	if (mntrefp == NULL) {
		PERROR("malloc failed");
		(void) fclose(fp);
		return (-1);
	}

	mntp = (struct mnttab *)malloc(sizeof (struct mnttab));
	if (mntp == NULL) {
		PERROR("malloc failed");
		(void) fclose(fp);
		free(mntrefp);
		return (-1);
	}

	/* Try all the partitions */

	(void) snprintf(tmp_name, PATH_MAX, "/dev/%s",
	    &device_name[strlen("/dev/r")]);

	tmp_name[strlen("/dev/dsk/c0t0d0s")] = '\0';

	errno = 0;
	while (getmntent(fp, mntp) == 0) {
		if (errno != 0) {
			PERROR("Error with mnttab");
			(void) fclose(fp);
			return (-1);
		}
		/* Is it a probable entry? */
		if (strncmp(mntp->mnt_special, tmp_name, strlen(tmp_name))) {
			/* Skip to next entry */
			continue;
		}
		for (i = 0; i < NDKMAP; i++) {
			/* Check for ufs style mount devices */
			(void) snprintf(block_dev_name, PATH_MAX,
			    "%s%d", tmp_name, i);

			if (strcmp(mntp->mnt_special, block_dev_name) == 0) {
				if (flag) {
					if (my_umount(mntp->mnt_mountp) < 0) {
						(void) fclose(fp);
						return (-1);
					}
					unmounted = 1;
				} else {
					(void) fclose(fp);
					return (-1);
				}
				/* Skip to next entry */
				continue;
			}

			/* Try for :1 -> :24 for pcfs */

			for (j = 1; j < 24; j++) {
				(void) snprintf(block_dev_name, PATH_MAX,
				    "%s%d:%d", tmp_name, i, j);

				if (strcmp(mntp->mnt_special,
				    block_dev_name) == 0) {
					if (flag) {
						if (my_umount(mntp->mnt_mountp)
						    < 0) {
							(void) fclose(fp);
							return (-1);
						}
						unmounted = 1;
					} else {
						(void) fclose(fp);
						return (-1);
					}
					/* Skip to next entry */
					continue;
				}
				(void) snprintf(block_dev_name, PATH_MAX,
				    "%s%d:%c", tmp_name, i, 'b' + j);

				if (strcmp(mntp->mnt_special,
				    block_dev_name) == 0) {
					if (flag) {
						if (my_umount(mntp->mnt_mountp)
						    < 0) {
							(void) fclose(fp);
							return (-1);
						}
						unmounted = 1;
					} else {
						(void) fclose(fp);
						return (-1);
					}
					/* Skip to next entry */
					continue;
				}
			}
		}

	}

	if (unmounted)
		return (1);
	return (0);
}

/*
 * This routine checks if a device has mounted partitions. The
 * device name is assumed to be /dev/rdiskette. So, this can
 * be used for Floppy controllers
 * Returns
 *	 0 : if not mounted
 *	 1 : if successfully unmounted
 *	-1 : Any error or unmount failed
 */

int32_t
check_and_unmount_floppy(int32_t fd, int32_t flag)
{
	FILE	*fp = NULL;
	int32_t	mfd;
	struct dk_cinfo dkinfo, dkinfo_tmp;
	struct mnttab	mnt_record;
	struct mnttab	*mp = &mnt_record;
	struct stat	stbuf;
	char	raw_device[PATH_MAX];
	int32_t	found = 0;


	if (ioctl(fd, DKIOCINFO, &dkinfo) < 0) {
		return (-1);
	}

	if ((fp = fopen(MNTTAB, "r")) == NULL) {
		PERROR("Could not open /etc/mnttab");
		(void) close(fd);
		exit(3);
	}

	while (getmntent(fp, mp) == 0) {
		if (strstr(mp->mnt_special, "/dev/fd") == NULL &&
		    strstr(mp->mnt_special, "/dev/disket") == NULL &&
		    strstr(mp->mnt_special, "/dev/c") == NULL) {
			continue;
		}

		(void) strcpy(raw_device, "/dev/r");
		(void) strcat(raw_device, mp->mnt_special + strlen("/dev/"));


		/*
		 * Attempt to open the device.	If it fails, skip it.
		 */

		/* Turn on the privileges. */
		(void) __priv_bracket(PRIV_ON);

		mfd = open(raw_device, O_RDWR | O_NDELAY);

		/* Turn off the privileges. */
		(void) __priv_bracket(PRIV_OFF);

		if (mfd < 0) {
			continue;
		}

		/*
		 * Must be a character device
		 */
		if (fstat(mfd, &stbuf) < 0 || !S_ISCHR(stbuf.st_mode)) {
			(void) close(mfd);
			continue;
		}
		/*
		 * Attempt to read the configuration info on the disk.
		 */
		if (ioctl(mfd, DKIOCINFO, &dkinfo_tmp) < 0) {
			(void) close(mfd);
			continue;
		}
		/*
		 * Finished with the opened device
		 */
		(void) close(mfd);

		/*
		 * If it's not the disk we're interested in, it doesn't apply.
		 */
		if (dkinfo.dki_ctype != dkinfo_tmp.dki_ctype ||
		    dkinfo.dki_cnum != dkinfo_tmp.dki_cnum ||
		    dkinfo.dki_unit != dkinfo_tmp.dki_unit) {
				continue;
		}
		/*
		 * It's a mount on the disk we're checking.  If we are
		 * checking whole disk, then we found trouble.	We can
		 * quit searching.
		 */

		if (flag) {
			if (my_umount(mp->mnt_mountp) < 0) {
				return (-1);
			}
			found = 1;
		} else {
			return (-1);
		}
	}
	return (found);
}


int32_t
my_open(char *device_name, int32_t flags)
{
	char *real_name;
	char *nm;
	char tmp_path_name[PATH_MAX];
	struct stat stat_buf;
	int32_t ret_val;
	int32_t fd;
	int32_t have_read_priv = 0;
	DIR *dirp;
	struct dirent *dp;

	DPRINTF1("Device name %s\n", device_name);

	if ((nm = volmgt_symname(device_name)) == NULL) {
		DPRINTF("path not managed\n");
		real_name = media_findname(device_name);
	} else {
		DPRINTF1("path managed as %s\n", nm);
		real_name = media_findname(nm);
		DPRINTF1("real name %s\n", real_name);
	}

	if (real_name == NULL)
		return (-1);

	(void) strcpy(tmp_path_name, real_name);
	ret_val = stat(tmp_path_name, &stat_buf);
	if (ret_val < 0) {
		PERROR("Could not stat");
		return (-1);
	}
	if (S_ISDIR(stat_buf.st_mode)) {

		/*
		 * Open the directory and look for the
		 * first non '.' entry.
		 * Since raw_read and raw_writes are used, we don't
		 * need to access the backup slice.
		 * For PCMCIA Memory cards, raw_read and raw_writes are
		 * not supported, but that is not a problem as, only slice2
		 * is allowed on PCMCIA memory cards.
		 */

		/*
		 * First make sure we are operating with a /vol/....
		 * Otherwise it can dangerous,
		 * e.g. rmformat -s /dev/rdsk
		 * We should not look into the directory contents here.
		 */
		if (strncmp(tmp_path_name, "/vol/dev/", strlen("/vol/dev/"))
		    != 0) {
			(void) fprintf(stderr, gettext("The specified device \
is not a raw device.\n"));
			exit(1);
		}

		/* Turn on the privileges. */
		(void) __priv_bracket(PRIV_ON);

		dirp = opendir(tmp_path_name);

		/* Turn off the privileges. */
		(void) __priv_bracket(PRIV_OFF);

		if (dirp == NULL) {
			return (-1);
		}

		/* Turn on the privileges. */
		(void) __priv_bracket(PRIV_ON);
		have_read_priv = 1;

		while ((dp = readdir(dirp)) != NULL) {

			/* Turn off the privileges. */
			(void) __priv_bracket(PRIV_OFF);
			have_read_priv = 0;

			DPRINTF1("Found %s\n", dp->d_name);
			if ((strcmp(dp->d_name, ".") != 0) &&
			    (strcmp(dp->d_name, "..") != 0)) {
				(void) snprintf(tmp_path_name, PATH_MAX,
				    "%s/%s", tmp_path_name, dp->d_name);

				DPRINTF1("tmp_pathname is %s\n", tmp_path_name);
				break;
			}

			/* Turn on the privileges. */
			(void) __priv_bracket(PRIV_ON);
			have_read_priv = 1;
		}

		if (have_read_priv) {
			/* drop the file_dac_read privilege */
			(void) __priv_bracket(PRIV_OFF);
			have_read_priv = 0;
		}

		(void) closedir(dirp);
	}


	if (volmgt_running() == 0) {
		/* Turn on privileges. */
		(void) __priv_bracket(PRIV_ON);
		have_read_priv = 1;
	}

	fd = open(tmp_path_name, flags);

	if (have_read_priv) {
		/* Turn off privileges. */
		(void) __priv_bracket(PRIV_OFF);
		have_read_priv = 0;
	}

	DPRINTF1("path opened %s\n", tmp_path_name);

	return (fd);
}

uint64_t
my_atoll(char *ptr)
{
	char *tmp_ptr = ptr;
	int32_t base = 10;
	uint64_t ret_val;

	while (*tmp_ptr) {
		if (isdigit(*tmp_ptr))
			tmp_ptr++;
		else {
			base = 16;
			break;
		}
	}
	tmp_ptr = ptr;
	if (base == 16) {
		if (strlen(tmp_ptr) < 3) {
			return (-1);
		}
		if (*tmp_ptr++ != '0' || (*tmp_ptr != 'x' && *tmp_ptr != 'X')) {
			return (-1);
		}
		tmp_ptr++;
		while (*tmp_ptr) {
			if (isxdigit(*tmp_ptr))
				tmp_ptr++;
			else {
				return (-1);
			}
		}
	}
	ret_val = (uint64_t)strtoull(ptr, (char **)NULL, 0);
	return (ret_val);
}

int32_t
write_sunos_label(int32_t fd, int32_t media_type)
{

	struct extvtoc v_toc;
	int32_t ret;

	(void) memset(&v_toc, 0, sizeof (struct extvtoc));

	/* Initialize the vtoc information */

	if (media_type == SM_FLOPPY) {
		struct fd_char fdchar;
		int32_t mult_factor;

		if (ioctl(fd, FDIOGCHAR, &fdchar) < 0) {
			PERROR("FDIOGCHAR failed");
			return (-1);
		}

		/* SPARC and x86 fd drivers use fdc_medium differently */
#if defined(__sparc)
		mult_factor = (fdchar.fdc_medium) ? 2 : 1;
#elif defined(__x86)
		mult_factor = (fdchar.fdc_medium == 5) ? 2 : 1;
#else
#error  No Platform defined
#endif /* defined(__sparc) */

		/* initialize the vtoc structure */
		v_toc.v_nparts = 3;

		v_toc.v_part[0].p_start = 0;
		v_toc.v_part[0].p_size = (fdchar.fdc_ncyl - 1) * 2 *
		    fdchar.fdc_secptrack * mult_factor;
		v_toc.v_part[1].p_start = (fdchar.fdc_ncyl - 1) * 2 *
		    fdchar.fdc_secptrack * mult_factor;
		v_toc.v_part[1].p_size = 2 * fdchar.fdc_secptrack * mult_factor;

		v_toc.v_part[2].p_start = 0;
		v_toc.v_part[2].p_size = fdchar.fdc_ncyl * 2 *
		    fdchar.fdc_secptrack * mult_factor;

	} else if (media_type == SM_SCSI_FLOPPY) {

		smedia_handle_t handle;
		smmedium_prop_t med_info;
		struct dk_geom dkgeom;


		/*
		 * call smedia_get_medium_property to get the
		 * correct media information, since DKIOCGMEDIAINFO
		 * may fail for unformatted media.
		 */

		handle = smedia_get_handle(fd);
		if (handle == NULL) {
			(void) fprintf(stderr,
			gettext("Failed to get libsmedia handle.\n"));

			(void) close(fd);
			return (-1);
		}


		if (smedia_get_medium_property(handle, &med_info) < 0) {
			(void) fprintf(stderr,
			    gettext("Get medium property failed \n"));

			(void) smedia_release_handle(handle);
			(void) close(fd);
			return (-1);
		}

		/* Fill in our own geometry information */

		dkgeom.dkg_pcyl = med_info.sm_pcyl;
		dkgeom.dkg_ncyl = med_info.sm_pcyl;
		dkgeom.dkg_nhead = med_info.sm_nhead;
		dkgeom.dkg_nsect = med_info.sm_nsect;
		dkgeom.dkg_acyl = 0;
		dkgeom.dkg_bcyl = 0;
		dkgeom.dkg_intrlv = 0;
		dkgeom.dkg_apc = 0;

		/*
		 * Try to set vtoc, if not successful we will
		 * continue to use the faked geometry information.
		 */

		(void) ioctl(fd, DKIOCSGEOM, &dkgeom);

		(void) smedia_release_handle(handle);

		/* we want the same partitioning as used for normal floppies */

		v_toc.v_part[0].p_start = 0;
		v_toc.v_part[0].p_size =  (diskaddr_t)(dkgeom.dkg_ncyl - 1) *
		    dkgeom.dkg_nhead * dkgeom.dkg_nsect;

		v_toc.v_part[1].p_start = (diskaddr_t)(dkgeom.dkg_ncyl - 1) *
		    dkgeom.dkg_nhead * dkgeom.dkg_nsect;
		v_toc.v_part[1].p_size =  dkgeom.dkg_nhead * dkgeom.dkg_nsect;

		v_toc.v_part[2].p_start = 0;
		v_toc.v_part[2].p_size = (diskaddr_t)dkgeom.dkg_ncyl *
		    dkgeom.dkg_nhead * dkgeom.dkg_nsect;

		/* both write_vtoc and DKIOCSVTOC require V_NUMPAR partitions */
		v_toc.v_nparts = V_NUMPAR;

	} else {

		return (0);
	}

	v_toc.v_sanity = VTOC_SANE;
	v_toc.v_version = V_VERSION;

	/*
	 * The label structure is set up for DEV_BSIZE(512 byte) blocks,
	 * even though a medium density diskette has 1024 byte blocks
	 * See dklabel.h for more details.
	 */
	v_toc.v_sectorsz = DEV_BSIZE;

	/* let the fd driver finish constructing the label and writing it. */


	/* Turn on the privileges. */
	(void) __priv_bracket(PRIV_ON);

	ret = write_extvtoc(fd, &v_toc);

	/* Turn off the privileges. */
	(void) __priv_bracket(PRIV_OFF);

	if (ret < 0) {
		PERROR("Write vtoc");
		DPRINTF1("Write vtoc failed errno:%d\n", errno);
		return (-1);
	}

	return (0);
}

static void
intr_sig_handler()
{
	char c;

	(void) fprintf(stderr, gettext(global_intr_msg));
	(void) fprintf(stderr,
	    gettext("\nDo you want to stop formatting?(y/n)"));
	(void) fflush(stdout);
	rewind(stdin);
	while ((c = getchar()) == -1)
		;
	if (c == 'y' || c == 'Y') {
		(void) fprintf(stderr, gettext("Format interrupted\n"));
		exit(1);
	} else if (c == 'n' || c == 'N')
		return;
	else {
		(void) fprintf(stderr, gettext("Did not interrupt\n"));
		return;
	}
}

static struct sigaction act, oact;
void
trap_SIGINT()
{

	act.sa_handler = intr_sig_handler;
	(void) memset(&act.sa_mask, 0, sizeof (sigset_t));
	act.sa_flags = SA_RESTART; /* | SA_NODEFER; */
	if (sigaction(SIGINT, &act, &oact) < 0) {
		DPRINTF("sigset failed\n");
		return;
	}
}

void
release_SIGINT()
{
	if (sigaction(SIGINT, &oact, (struct sigaction *)NULL) < 0) {
		DPRINTF("sigunset failed\n");
		return;
	}
}

int32_t
verify(smedia_handle_t handle, int32_t fd, diskaddr_t start_sector,
	uint32_t nblocks, char *buf,
	int32_t flag, int32_t blocksize, int32_t no_raw_rw)
{
	uint64_t ret;

	DPRINTF("ANALYSE MEDIA \n");


	if ((flag == VERIFY_READ) && (!no_raw_rw)) {

		/* Turn on the privileges. */
		(void) __priv_bracket(PRIV_ON);

		ret = smedia_raw_read(handle, start_sector, buf, nblocks *
		    blocksize);

		/* Turn off the privileges. */
		(void) __priv_bracket(PRIV_OFF);

		if (ret != (nblocks * blocksize))
			return (-1);
		return (0);

	} else if ((flag == VERIFY_WRITE) && (!no_raw_rw)) {

		/* Turn on privileges. */
		(void) __priv_bracket(PRIV_ON);

		ret = smedia_raw_write(handle, start_sector, buf, nblocks *
		    blocksize);

		/* Turn off the privileges. */
		(void) __priv_bracket(PRIV_OFF);

		if (ret != (blocksize * nblocks))
			return (-1);
		return (0);

	} else if ((flag == VERIFY_READ) && (no_raw_rw)) {
		ret = llseek(fd, start_sector * blocksize, SEEK_SET);
		if (ret != start_sector * blocksize) {
			(void) fprintf(stderr, gettext("Seek failed\n"));
			return (-2);
		}

		/* Turn on the privileges. */
		(void) __priv_bracket(PRIV_ON);

		ret = read(fd, buf, nblocks * blocksize);

		/* Turn off the privileges. */
		(void) __priv_bracket(PRIV_OFF);

		if (ret != nblocks * blocksize) {
			return (-1);
		}
		return (0);
	} else if ((flag == VERIFY_WRITE) && (no_raw_rw)) {
		ret = llseek(fd, start_sector * blocksize, SEEK_SET);
		if (ret != start_sector * blocksize) {
			(void) fprintf(stderr, gettext("Seek failed\n"));
			return (-2);
		}

		/* Turn on the privileges. */
		(void) __priv_bracket(PRIV_ON);

		ret = write(fd, buf, nblocks * blocksize);

		/* Turn off the privileges. */
		(void) __priv_bracket(PRIV_OFF);

		if (ret != nblocks * blocksize) {
			return (-1);
		}
		return (0);
	} else {
		DPRINTF("Illegal parameter to verify_analysis!\n");
		return (-1);
	}
}

static int
my_umount(char *mountp)
{
	pid_t	pid;	/* forked proc's pid */
	int	rval;	/* proc's return value */


	/* create a child to unmount the path */

	/* Turn on the privileges */
	(void) __priv_bracket(PRIV_ON);

	pid = fork();

	/* Turn off the privileges. */
	(void) __priv_bracket(PRIV_OFF);

	if (pid < 0) {
		PERROR("fork failed");
		exit(0);
	}

	if (pid == 0) {
		/* the child */
		/* get rid of those nasty err messages */
		DPRINTF1("call_unmount_prog: calling %s \n", mountp);

		/* Turn on the priviliges. */
		(void) __priv_bracket(PRIV_ON);

		if (execl("/usr/sbin/umount", "/usr/sbin/umount", mountp,
		    NULL) < 0) {
			perror("exec failed");
			/* Turn off the privileges */
			(void) __priv_bracket(PRIV_OFF);
			exit(-1);
		}
	}

	/* wait for the umount command to exit */
	rval = 0;
	if (waitpid(pid, &rval, 0) == pid) {
		if (WIFEXITED(rval)) {
			if (WEXITSTATUS(rval) == 0) {
				DPRINTF("umount : Success\n");
				return (1);
			}
		}
	}
	return (-1);
}

static int
my_volrmmount(char *real_name)
{
	int pid, rval;

	/* Turn on the privileges. */
	(void) __priv_bracket(PRIV_ON);

	pid = fork();

	/* Turn off the privileges. */
	(void) __priv_bracket(PRIV_OFF);

	/* create a child to unmount the path */
	if (pid < 0) {
		PERROR("fork failed");
		exit(0);
	}

	if (pid == 0) {
		/* the child */
		/* get rid of those nasty err messages */
		DPRINTF1("call_unmount_prog: calling %s \n",
		    "/usr/bin/volrmmount");

		/* Turn on the privileges. */
		(void) __priv_bracket(PRIV_ON);
		if (execl("/usr/bin/volrmmount", "/usr/bin/volrmmount", "-e",
		    real_name, NULL) < 0) {
			PERROR("volrmmount exec failed");
			/* Turn off the privileges */
			(void) __priv_bracket(PRIV_OFF);
			exit(-1);
		}
	} else if (waitpid(pid, &rval, 0) == pid) {
		if (WIFEXITED(rval)) {
			if (WEXITSTATUS(rval) == 0) {
				DPRINTF("volrmmount: Success\n");
				return (1);
			}
		}
	}
	return (-1);
}

int
find_device(int defer, char *tmpstr)
{
	DIR *dir;
	struct dirent *dirent;
	char sdev[PATH_MAX], dev[PATH_MAX], *pname;
	device_t *t_dev;
	int removable = 0;
	int device_type = 0;
	int hotpluggable = 0;
	struct dk_minfo mediainfo;
	static int found = 0;

	dir = opendir("/dev/rdsk");
	if (dir == NULL)
		return (-1);

	total_devices_found = 0;
	while ((dirent = readdir(dir)) != NULL) {
		if (dirent->d_name[0] == '.') {
			continue;
		}
		(void) snprintf(sdev, PATH_MAX, "/dev/rdsk/%s",
		    dirent->d_name);
#ifdef sparc
		if (!strstr(sdev, "s2")) {
			continue;
		}
#else /* x86 */
		if (vol_running) {
			if (!(strstr(sdev, "s2") || strstr(sdev, "p0"))) {
				continue;
			}
		} else {
			if (!strstr(sdev, "p0")) {
				continue;
			}
		}
#endif
		if (!lookup_device(sdev, dev)) {
			continue;
		}
		if ((t_dev = get_device(NULL, dev)) == NULL) {
			continue;
		}
		total_devices_found++;

		if ((!defer) && !found) {
			char *sn, *tmpbuf;
			/*
			 * dev_name is an optional command line input.
			 */
			if (dev_name) {
				if (strstr(dirent->d_name, tmpstr)) {
					found = 1;
				} else if (!vol_running) {
					continue;
				}
			}
			/*
			 * volmgt_symname() returns NULL if the device
			 * is not managed by volmgt.
			 */
			sn = volmgt_symname(sdev);

			if (vol_running && (sn != NULL)) {
				if (strstr(sn, "dev") == NULL) {
					tmpbuf = (char *)my_zalloc(PATH_MAX);
					(void) strcpy(tmpbuf,
					    "/vol/dev/aliases/");
					(void) strcat(tmpbuf, sn);
					free(sn);
					sn = tmpbuf;
				}
				if (dev_name && !found) {
					if (!strstr(tmpbuf, tmpstr)) {
						continue;
					} else {
						found = 1;
					}
				}
			}

			/*
			 * Get device type information for CD/DVD devices.
			 */
			if (is_cd(dev)) {
				if (check_device(t_dev,
				    CHECK_DEVICE_IS_DVD_WRITABLE)) {
					device_type = DK_DVDR;
				} else if (check_device(t_dev,
				    CHECK_DEVICE_IS_DVD_READABLE)) {
					device_type = DK_DVDROM;
				} else if (check_device(t_dev,
				    CHECK_DEVICE_IS_CD_WRITABLE)) {
					device_type = DK_CDR;
				} else {
					device_type = DK_CDROM;
				}
			} else {
				device_type = ioctl(t_dev->d_fd,
				    DKIOCGMEDIAINFO, &mediainfo);
				if (device_type < 0)
					device_type = 0;
				else
					device_type = mediainfo.dki_media_type;
			}

			if (!ioctl(t_dev->d_fd, DKIOCREMOVABLE, &removable) &&
			    !ioctl(t_dev->d_fd, DKIOCHOTPLUGGABLE,
			    &hotpluggable)) {
				if (removable || hotpluggable) {
					removable_found++;
					pname = get_physical_name(sdev);
					if (sn) {
						(void) printf("  %4d. "
						    "Volmgt Node: %s\n",
						    removable_found, sn);
						(void) printf("        "
						    "Logical Node: %s\n", sdev);
						(void) printf("        "
						    "Physical Node: %s\n",
						    pname);
					} else {
						(void) printf("  %4d. "
						    "Logical Node: %s\n",
						    removable_found, sdev);
						(void) printf("        "
						    "Physical Node: %s\n",
						    pname);
					}
					(void) printf("        Connected "
					    "Device: %-8.8s %-16.16s "
					    "%-4.4s\n",
					    &t_dev->d_inq[8],
					    &t_dev->d_inq[16],
					    &t_dev->d_inq[32]);
					(void) printf("        Device "
					    "Type: ");
				} else
					continue;
			} else
				continue;

			switch (device_type) {
				case DK_CDROM:
					(void) printf("CD Reader\n");
					break;
				case DK_CDR:
				case DK_CDRW:
					(void) printf("CD Reader/Writer\n");
					break;
				case DK_DVDROM:
					(void) printf("DVD Reader\n");
					break;
				case DK_DVDR:
				case DK_DVDRAM:
					(void) printf("DVD Reader/Writer\n");
					break;
				case DK_FIXED_DISK:
					if (strstr((const char *)
					    &t_dev->d_inq[16], "FD") ||
					    strstr((const char *)
					    &t_dev->d_inq[16], "LS-120"))
						(void) printf("Floppy "
						    "drive\n");
					else
						(void) printf("Removable\n");
					break;
				case DK_FLOPPY:
					(void) printf("Floppy drive\n");
					break;
				case DK_ZIP:
					(void) printf("Zip drive\n");
					break;
				case DK_JAZ:
					(void) printf("Jaz drive\n");
					break;
				default:
					(void) printf("<Unknown>\n");
					DPRINTF1("\t   %d\n", device_type);
					break;
			}
			get_media_info(t_dev, sdev, pname, sn);
		}
		fini_device(t_dev);
	}

	(void) closedir(dir);
	return (removable_found);
}

/*
 * Returns a device_t handle for a node returned by lookup_device()
 * and takes the user supplied name and stores it inside the node.
 */
static device_t *
get_device(char *user_supplied, char *node)
{
	device_t *dev;
	int fd;
	char devnode[PATH_MAX];
	int size;

	/*
	 * we need to resolve any link paths to avoid fake files
	 * such as /dev/rdsk/../../export/file.
	 */
	size = resolvepath(node, devnode, PATH_MAX);
	if ((size <= 0) || (size >= (PATH_MAX - 1)))
		return (NULL);

	/* resolvepath may not return a null terminated string */
	devnode[size] = '\0';


	/* the device node must be in /devices/ or /vol/dev/rdsk */

	if ((strncmp(devnode, "/devices/", 9) != 0) &&
	    (strncmp(devnode, "/vol/dev/rdsk", 13) != 0))
		return (NULL);

	/* Turn on the privileges. */
	(void) __priv_bracket(PRIV_ON);

	/*
	 * Since we are currently running with the user euid it is
	 * safe to try to open the file without checking access.
	 */

	fd = open(devnode, O_RDONLY|O_NDELAY);

	/* Turn off the privileges. */
	(void) __priv_bracket(PRIV_OFF);

	if (fd < 0) {
		return (NULL);
	}

	dev = (device_t *)my_zalloc(sizeof (device_t));

	dev->d_node = (char *)my_zalloc(strlen(devnode) + 1);
	(void) strcpy(dev->d_node, devnode);

	dev->d_fd = fd;

	dev->d_inq = (uchar_t *)my_zalloc(INQUIRY_DATA_LENGTH);

	/* Turn on privileges. */
	(void) __priv_bracket(PRIV_ON);
	if (!inquiry(fd, dev->d_inq)) {
		DPRINTF1("USCSI ioctl failed %d\n",
		    uscsi_error);
		free(dev->d_inq);
		free(dev->d_node);
		(void) close(dev->d_fd);
		free(dev);
		/* Turn off privileges. */
		(void) __priv_bracket(PRIV_OFF);
		return (NULL);
	}
	/* Turn off privileges. */
	(void) __priv_bracket(PRIV_OFF);

	if (user_supplied) {
		dev->d_name = (char *)my_zalloc(strlen(user_supplied) + 1);
		(void) strcpy(dev->d_name, user_supplied);
	}
	return (dev);
}

/*
 * Check for device specific characteristics.
 */
int
check_device(device_t *dev, int cond)
{
	uchar_t page_code[4];

	/* Look at the capabilities page for this information */
	if (cond & CHECK_DEVICE_IS_CD_WRITABLE) {
		if (get_mode_page(dev->d_fd, 0x2a, 0, 4, page_code) &&
		    (page_code[3] & 1)) {
			return (1);
		}
	}

	if (cond & CHECK_DEVICE_IS_DVD_WRITABLE) {
		if (get_mode_page(dev->d_fd, 0x2a, 0, 4, page_code) &&
		    (page_code[3] & 0x10)) {
			return (1);
		}
	}

	if (cond & CHECK_DEVICE_IS_DVD_READABLE) {
		if (get_mode_page(dev->d_fd, 0x2a, 0, 4, page_code) &&
		    (page_code[2] & 0x8)) {
			return (1);
		}
	}

	return (0);
}

/*
 * Builds an open()able device path from a user supplied node which can be
 * of the * form of /dev/[r]dsk/cxtxdx[sx] or cxtxdx[sx] or volmgt-name like
 * cdrom[n].
 * Returns the path found in 'found' and returns 1. Otherwise returns 0.
 */
int
lookup_device(char *supplied, char *found)
{
	struct stat statbuf;
	int fd;
	char tmpstr[PATH_MAX];

	/* Turn on privileges */
	(void) __priv_bracket(PRIV_ON);

	/* If everything is fine and proper, no need to analyze */
	if ((stat(supplied, &statbuf) == 0) && S_ISCHR(statbuf.st_mode) &&
	    ((fd = open(supplied, O_RDONLY|O_NDELAY)) >= 0)) {
		(void) close(fd);
		(void) strlcpy(found, supplied, PATH_MAX);
		/* Turn off privilege */
		(void) __priv_bracket(PRIV_OFF);
		return (1);
	}

	/* Turn off privileges. */
	(void) __priv_bracket(PRIV_OFF);

	if (strncmp(supplied, "/dev/rdsk/", 10) == 0)
		return (vol_lookup(supplied, found));
	if (strncmp(supplied, "/dev/dsk/", 9) == 0) {
		(void) snprintf(tmpstr, PATH_MAX, "/dev/rdsk/%s",
		    (char *)strrchr(supplied, '/'));

		if ((fd = open(tmpstr, O_RDONLY|O_NDELAY)) >= 0) {
			(void) close(fd);
			(void) strlcpy(found, supplied, PATH_MAX);
			return (1);
		}
		if ((access(tmpstr, F_OK) == 0) && vol_running)
			return (vol_lookup(tmpstr, found));
		else
			return (0);
	}
	if ((strncmp(supplied, "cdrom", 5) != 0) &&
	    (strlen(supplied) < 32)) {
		(void) snprintf(tmpstr, sizeof (tmpstr), "/dev/rdsk/%s",
		    supplied);
		if (access(tmpstr, F_OK) < 0) {
			(void) strcat(tmpstr, "s2");
		}
		if ((fd = open(tmpstr, O_RDONLY|O_NDELAY)) >= 0) {
			(void) close(fd);
			(void) strlcpy(found, tmpstr, PATH_MAX);
			return (1);
		}
		if ((access(tmpstr, F_OK) == 0) && vol_running)
			return (vol_lookup(tmpstr, found));
	}
	return (vol_name_to_dev_node(supplied, found));
}

int
is_cd(char *node)
{
	int fd;
	struct dk_cinfo cinfo;

	fd = open(node, O_RDONLY|O_NDELAY);
	if (fd < 0)
		return (0);
	if (ioctl(fd, DKIOCINFO, &cinfo) < 0) {
		(void) close(fd);
		return (0);
	}
	if (cinfo.dki_ctype != DKC_CDROM)
		return (0);
	return (1);
}

void
print_header(void)
{
	/* l10n_NOTE : Column spacing should be kept same */
	(void) printf(gettext("    Node 		       "
	    "Connected Device"));
	/* l10n_NOTE : Column spacing should be kept same */
	(void) printf(gettext(" 		Device type\n"));
	(void) printf(
	    "---------------------------+---------------------------");
	(void) printf("-----+----------------\n");
}

void
print_divider(void)
{
	(void) printf(
	    "---------------------------+---------------------------");
	(void) printf("-----+----------------\n");
}

static void
fini_device(device_t *dev)
{
	free(dev->d_inq);
	free(dev->d_node);
	(void) close(dev->d_fd);
	if (dev->d_name)
		free(dev->d_name);
	free(dev);
}

void *
my_zalloc(size_t size)
{
	void *ret;

	ret = malloc(size);
	if (ret == NULL) {

		/* Lets wait a sec. and try again */
		if (errno == EAGAIN) {
			(void) sleep(1);
			ret = malloc(size);
		}

		if (ret == NULL) {
			(void) err_msg("%s\n", gettext(strerror(errno)));
			(void) err_msg(gettext(
			    "Memory allocation failure, Exiting...\n"));
			exit(1);
		}
	}
	(void) memset(ret, 0, size);
	return (ret);
}

static int
vol_name_to_dev_node(char *vname, char *found)
{
	struct stat statbuf;
	char *p1;
	int i;

	if (vname == NULL)
		return (0);
	if (vol_running)
		(void) volmgt_check(vname);
	p1 = media_findname(vname);
	if (p1 == NULL)
		return (0);
	if (stat(p1, &statbuf) < 0) {
		free(p1);
		return (0);
	}
	if (S_ISDIR(statbuf.st_mode)) {
		for (i = 0; i < 16; i++) {
			(void) snprintf(found, PATH_MAX, "%s/s%d", p1, i);
			if (access(found, F_OK) >= 0)
				break;
		}
		if (i == 16) {
			free(p1);
			return (0);
		}
	} else {
		(void) strlcpy(found, p1, PATH_MAX);
	}
	free(p1);
	return (1);
}

/*
 * Searches for volume manager's equivalent char device for the
 * supplied pathname which is of the form of /dev/rdsk/cxtxdxsx
 */
static int
vol_lookup(char *supplied, char *found)
{
	char tmpstr[PATH_MAX], tmpstr1[PATH_MAX], *p;
	int i, ret;

	(void) strlcpy(tmpstr, supplied, PATH_MAX);
	if ((p = volmgt_symname(tmpstr)) == NULL) {
		if (strstr(tmpstr, "s2") != NULL) {
			*((char *)(strrchr(tmpstr, 's') + 1)) = 0;
			for (i = 0; i < 16; i++) {
				(void) snprintf(tmpstr1, PATH_MAX, "%s%d",
				    tmpstr, i);
				if ((p = volmgt_symname(tmpstr1)) != NULL)
					break;
			}
		} else if (strstr(tmpstr, "p0") != NULL) {
			*((char *)(strrchr(tmpstr, 'p') + 1)) = 0;
			for (i = 0; i < 5; i++) {
				(void) snprintf(tmpstr1, PATH_MAX, "%s%d",
				    tmpstr, i);
				if ((p = volmgt_symname(tmpstr1)) != NULL)
					break;
			}
		} else
			return (0);
		if (p == NULL)
			return (0);
	}

	ret = vol_name_to_dev_node(p, found);
	free(p);
	return (ret);
}

/*PRINTFLIKE1*/
void
err_msg(char *fmt, ...)
{
	va_list ap;

	va_start(ap, fmt);
	(void) vfprintf(stderr, fmt, ap);
	va_end(ap);
}

int
inquiry(int fd, uchar_t *inq)
{
	struct uscsi_cmd *scmd;

	scmd = get_uscsi_cmd();
	scmd->uscsi_flags = USCSI_READ|USCSI_SILENT;
	scmd->uscsi_timeout = DEFAULT_SCSI_TIMEOUT;
	scmd->uscsi_cdb[0] = INQUIRY_CMD;
	scmd->uscsi_cdb[4] = INQUIRY_DATA_LENGTH;
	scmd->uscsi_cdblen = 6;
	scmd->uscsi_bufaddr = (char *)inq;
	scmd->uscsi_buflen = INQUIRY_DATA_LENGTH;
	if ((uscsi_error = uscsi(fd, scmd)) < 0)
		return (0);
	return (1);
}

struct uscsi_cmd *
get_uscsi_cmd(void)
{
	(void) memset(&uscmd, 0, sizeof (uscmd));
	(void) memset(ucdb, 0, 16);
	uscmd.uscsi_cdb = ucdb;
	return (&uscmd);
}

int
uscsi(int fd, struct uscsi_cmd *scmd)
{
	int ret, global_rqsense;
	int retries, max_retries = 5;
	int i;

	/* set up for request sense extensions */
	if (!(scmd->uscsi_flags & USCSI_RQENABLE)) {
		scmd->uscsi_flags |= USCSI_RQENABLE;
		scmd->uscsi_rqlen = RQBUFLEN;
		scmd->uscsi_rqbuf = rqbuf;
		global_rqsense = 1;
	} else {
		global_rqsense = 0;
	}

	/*
	 * The device may be busy or slow and fail with a not ready status.
	 * we'll allow a limited number of retries to give the drive time
	 * to recover.
	 */
	for (retries = 0; retries < max_retries; retries++) {

		scmd->uscsi_status = 0;

		if (global_rqsense)
			(void) memset(rqbuf, 0, RQBUFLEN);

		DPRINTF("cmd:[");
		for (i = 0; i < scmd->uscsi_cdblen; i++)
			DPRINTF1("0x%02x ",
			    (uchar_t)scmd->uscsi_cdb[i]);
		DPRINTF("]\n");

		/*
		 * We need to have root privledges in order to use
		 * uscsi commands on the device.
		 */

		ret = ioctl(fd, USCSICMD, scmd);

		/* maintain consistency in case of sgen */
		if ((ret == 0) && (scmd->uscsi_status == 2)) {
			ret = -1;
			errno = EIO;
		}

		/* if error and extended request sense, retrieve errors */
		if (global_rqsense && (ret < 0) && (scmd->uscsi_status == 2)) {
			/*
			 * The drive is not ready to recieve commands but
			 * may be in the process of becoming ready.
			 * sleep for a short time then retry command.
			 * SENSE/ASC = 2/4 : not ready
			 * ASCQ = 0  Not Reportable.
			 * ASCQ = 1  Becoming ready.
			 */
			if ((SENSE_KEY(rqbuf) == 2) && (ASC(rqbuf) == 4) &&
			    ((ASCQ(rqbuf) == 0) || (ASCQ(rqbuf) == 1))) {
				total_retries++;
				(void) sleep(3);
				continue;
			}

			/*
			 * Device is not ready to transmit or a device reset
			 * has occurred. wait for a short period of time then
			 * retry the command.
			 */
			if ((SENSE_KEY(rqbuf) == 6) && ((ASC(rqbuf) == 0x28) ||
			    (ASC(rqbuf) == 0x29))) {
				(void) sleep(3);
				total_retries++;
				continue;
			}

			DPRINTF3("cmd: 0x%02x ret:%i status:%02x ",
			    (uchar_t)scmd->uscsi_cdb[0], ret,
			    scmd->uscsi_status);
			DPRINTF3(" sense: %02x ASC: %02x ASCQ:%02x\n",
			    (uchar_t)SENSE_KEY(rqbuf),
			    (uchar_t)ASC(rqbuf), (uchar_t)ASCQ(rqbuf));
		}

		/* no errors we'll return */
		break;
	}

	/* store the error status for later debug printing */
	if ((ret < 0) && (global_rqsense)) {
		uscsi_status = scmd->uscsi_status;
		rqstatus = scmd->uscsi_rqstatus;
		rqresid = scmd->uscsi_rqresid;

	}

	DPRINTF1("total retries: %d\n", total_retries);

	return (ret);
}

/*
 * will get the mode page only i.e. will strip off the header.
 */
int
get_mode_page(int fd, int page_no, int pc, int buf_len, uchar_t *buffer)
{
	int ret;
	uchar_t byte2, *buf;
	uint_t header_len, page_len, copy_cnt;

	byte2 = (uchar_t)(((pc << 6) & 0xC0) | (page_no & 0x3f));
	buf = (uchar_t *)my_zalloc(256);

	/* Ask 254 bytes only to make our IDE driver happy */
	ret = mode_sense(fd, byte2, 1, 254, buf);
	if (ret == 0) {
		free(buf);
		return (0);
	}

	header_len = 8 + read_scsi16(&buf[6]);
	page_len = buf[header_len + 1] + 2;

	copy_cnt = (page_len > buf_len) ? buf_len : page_len;
	(void) memcpy(buffer, &buf[header_len], copy_cnt);
	free(buf);

	return (1);
}

int
mode_sense(int fd, uchar_t pc, int dbd, int page_len, uchar_t *buffer)
{
	struct uscsi_cmd *scmd;

	scmd = get_uscsi_cmd();
	scmd->uscsi_flags = USCSI_READ|USCSI_SILENT;
	scmd->uscsi_buflen = page_len;
	scmd->uscsi_bufaddr = (char *)buffer;
	scmd->uscsi_timeout = DEFAULT_SCSI_TIMEOUT;
	scmd->uscsi_cdblen = 0xa;
	scmd->uscsi_cdb[0] = MODE_SENSE_10_CMD;
	if (dbd) {
		/* don't return any block descriptors */
		scmd->uscsi_cdb[1] = 0x8;
	}
	/* the page code we want */
	scmd->uscsi_cdb[2] = pc;
	/* allocation length */
	scmd->uscsi_cdb[7] = (page_len >> 8) & 0xff;
	scmd->uscsi_cdb[8] = page_len & 0xff;

	if ((uscsi_error = uscsi(fd, scmd)) < 0)
		return (0);
	return (1);
}

uint16_t
read_scsi16(void *addr)
{
	uchar_t *ad = (uchar_t *)addr;
	uint16_t ret;

	ret = ((((uint16_t)ad[0]) << 8) | ad[1]);
	return (ret);
}

/*
 * Allocate space for and return a pointer to a string
 * on the stack.  If the string is null, create
 * an empty string.
 * Use destroy_data() to free when no longer used.
 */
char *
alloc_string(s)
	char    *s;
{
	char    *ns;

	if (s == (char *)NULL) {
		ns = (char *)my_zalloc(1);
	} else {
		ns = (char *)my_zalloc(strlen(s) + 1);
		(void) strcpy(ns, s);
	}
	return (ns);
}

/*
 * Follow symbolic links from the logical device name to
 * the /devfs physical device name.  To be complete, we
 * handle the case of multiple links.  This function
 * either returns NULL (no links, or some other error),
 * or the physical device name, alloc'ed on the heap.
 *
 * Note that the standard /devices prefix is stripped from
 * the final pathname, if present.  The trailing options
 * are also removed (":c, raw").
 */
static char *
get_physical_name(char *path)
{
	struct stat	stbuf;
	int		i;
	int		level;
	char		*p;
	char		s[MAXPATHLEN];
	char		buf[MAXPATHLEN];
	char		dir[MAXPATHLEN];
	char		savedir[MAXPATHLEN];
	char		*result = NULL;

	if (getcwd(savedir, sizeof (savedir)) == NULL) {
		DPRINTF1("getcwd() failed - %s\n", strerror(errno));
		return (NULL);
	}

	(void) strcpy(s, path);
	if ((p = strrchr(s, '/')) != NULL) {
		*p = 0;
	}
	if (s[0] == 0) {
		(void) strcpy(s, "/");
	}
	if (chdir(s) == -1) {
		DPRINTF2("cannot chdir() to %s - %s\n",
		    s, strerror(errno));
		goto exit;
	}

	level = 0;
	(void) strcpy(s, path);
	for (;;) {
		/*
		 * See if there's a real file out there.  If not,
		 * we have a dangling link and we ignore it.
		 */
		if (stat(s, &stbuf) == -1) {
			goto exit;
		}
		if (lstat(s, &stbuf) == -1) {
			DPRINTF2("%s: lstat() failed - %s\n",
			    s, strerror(errno));
			goto exit;
		}
		/*
		 * If the file is not a link, we're done one
		 * way or the other.  If there were links,
		 * return the full pathname of the resulting
		 * file.
		 */
		if (!S_ISLNK(stbuf.st_mode)) {
			if (level > 0) {
				/*
				 * Strip trailing options from the
				 * physical device name
				 */
				if ((p = strrchr(s, ':')) != NULL) {
					*p = 0;
				}
				/*
				 * Get the current directory, and
				 * glue the pieces together.
				 */
				if (getcwd(dir, sizeof (dir)) == NULL) {
					DPRINTF1("getcwd() failed - %s\n",
					    strerror(errno));
					goto exit;
				}
				(void) strcat(dir, "/");
				(void) strcat(dir, s);
				/*
				 * If we have the standard fixed
				 * /devices prefix, remove it.
				 */
				p = (strstr(dir, DEVFS_PREFIX) == dir) ?
				    dir+strlen(DEVFS_PREFIX) : dir;
				result = alloc_string(p);
			}
			goto exit;
		}
		i = readlink(s, buf, sizeof (buf));
		if (i == -1) {
			DPRINTF2("%s: readlink() failed - %s\n",
			    s, strerror(errno));
			goto exit;
		}
		level++;
		buf[i] = 0;

		/*
		 * Break up the pathname into the directory
		 * reference, if applicable and simple filename.
		 * chdir()'ing to the directory allows us to
		 * handle links with relative pathnames correctly.
		 */
		(void) strcpy(dir, buf);
		if ((p = strrchr(dir, '/')) != NULL) {
			*p = 0;
			if (chdir(dir) == -1) {
				DPRINTF2("cannot chdir() to %s - %s\n",
				    dir, strerror(errno));
				goto exit;
			}
			(void) strcpy(s, p+1);
		} else {
			(void) strcpy(s, buf);
		}
	}

exit:
	if (chdir(savedir) == -1) {
		(void) printf("cannot chdir() to %s - %s\n",
		    savedir, strerror(errno));
	}

	return (result);
}

static void
get_media_info(device_t *t_dev, char *sdev, char *pname, char *sn)
{
	struct dk_cinfo cinfo;
	struct extvtoc vtocinfo;
	float size;
	int32_t fd;
	smedia_handle_t handle;
	struct dk_minfo mediainfo;
	int device_type;

	device_type = ioctl(t_dev->d_fd, DKIOCGMEDIAINFO, &mediainfo);

	/*
	 * Determine bus type.
	 */
	if (!ioctl(t_dev->d_fd, DKIOCINFO, &cinfo)) {
		if (strstr(cinfo.dki_cname, "usb") || strstr(pname, "usb")) {
			(void) printf("\tBus: USB\n");
		} else if (strstr(cinfo.dki_cname, "firewire") ||
		    strstr(pname, "firewire")) {
			(void) printf("\tBus: Firewire\n");
		} else if (strstr(cinfo.dki_cname, "ide") ||
		    strstr(pname, "ide")) {
			(void) printf("\tBus: IDE\n");
		} else if (strstr(cinfo.dki_cname, "scsi") ||
		    strstr(pname, "scsi")) {
			(void) printf("\tBus: SCSI\n");
		} else {
			(void) printf("\tBus: <Unknown>\n");
		}
	} else {
		(void) printf("\tBus: <Unknown>\n");
	}

	/*
	 * Calculate size of media.
	 */
	if (!device_type &&
	    (!ioctl(t_dev->d_fd, DKIOCGMEDIAINFO, &mediainfo))) {
		size = (mediainfo.dki_lbsize*
		    mediainfo.dki_capacity)/(1024.0*1024.0);
		if (size < 1000) {
			(void) printf("\tSize: %.1f MB\n", size);
		} else {
			size = size/1000;
			(void) printf("\tSize: %.1f GB\n", size);
		}
	} else {
		(void) printf("\tSize: <Unknown>\n");
	}

	/*
	 * Print label.
	 */
	if (!device_type && (read_extvtoc(t_dev->d_fd,  &vtocinfo) >= 0)) {
		if (*vtocinfo.v_volume) {
			(void) printf("\tLabel: %s\n", vtocinfo.v_volume);
		} else {
			(void) printf("\tLabel: <None>\n");
		}
	} else {
		(void) printf("\tLabel: <Unknown>\n");
	}

	/*
	 * Acess permissions.
	 */
	if (device_type) {
		(void) printf("\tAccess permissions: <Unknown>\n");
		return;
	}

	(void) fprintf(stdout, gettext("\tAccess permissions: "));
	if (sn) {
		/*
		 * Set dev_name for process_p_flag().
		 */
		dev_name = sn;
		fd = my_open(sn, O_RDONLY|O_NDELAY);
	} else {
		dev_name = sdev;
		fd = my_open(sdev, O_RDONLY|O_NDELAY);
	}
	if (fd < 0)  {
		(void) printf("<Unknown>\n");
		DPRINTF("Could not open device.\n");
		(void) close(fd);
	} else {
		/* register the fd with the libsmedia */
		handle = smedia_get_handle(fd);
		if (handle == NULL) {
			(void) printf("<Unknown>\n");
			DPRINTF("Failed to get libsmedia handle.\n");
			(void) close(fd);
		} else {
			process_p_flag(handle, fd);
		}
	}
	/* Clear dev_name */
	dev_name = NULL;
}