xref: /titanic_50/usr/src/cmd/rmformat/rmf_misc.c (revision 2a8164df8a5f42c8a00f10c67d7bc84f80ae9c41)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*
28  * rmf_misc.c :
29  *	Miscelleneous routines for rmformat.
30  */
31 
32 #include <sys/types.h>
33 #include <stdio.h>
34 #include <sys/mnttab.h>
35 #include <volmgt.h>
36 #include <sys/dkio.h>
37 #include <sys/fdio.h>
38 #include <sys/vtoc.h>
39 #include <sys/termios.h>
40 #include <sys/mount.h>
41 #include <ctype.h>
42 #include <signal.h>
43 #include <sys/wait.h>
44 #include <dirent.h>
45 #include <priv_utils.h>
46 #include <stdarg.h>
47 #include "rmformat.h"
48 /*
49  * These defines are from the PCMCIA memory driver driver
50  * header files (pcramio.h/pcramvar.h) and they are in
51  * the Platform Specific (PS) train.
52  */
53 #ifndef PCRAM_PROBESIZE
54 #define	PCRAMIOC	('P' << 8)
55 #define	PCRAM_PROBESIZE (PCRAMIOC|22)	/* Probe memory card size */
56 #endif
57 
58 /*
59  * Definitions.
60  */
61 #define	SENSE_KEY(rqbuf)	(rqbuf[2] & 0xf) /* scsi error category */
62 #define	ASC(rqbuf)		(rqbuf[12])	/* additional sense code */
63 #define	ASCQ(rqbuf)		(rqbuf[13])	/* ASC qualifier */
64 
65 #define	DEFAULT_SCSI_TIMEOUT	60
66 #define	INQUIRY_CMD		0x12
67 #define	RQBUFLEN		32
68 #define	CD_RW			1		/* CD_RW/CD-R	*/
69 #define	WRITE_10_CMD		0x2A
70 #define	READ_INFO_CMD		0x51
71 #define	SYNC_CACHE_CMD		0x35
72 #define	CLOSE_TRACK_CMD 	0x5B
73 #define	MODE_SENSE_10_CMD	0x5A
74 #define	DEVFS_PREFIX		"/devices"
75 
76 int		uscsi_error;		 /* used for debugging failed uscsi */
77 char		rqbuf[RQBUFLEN];
78 static uint_t	total_retries;
79 static struct	uscsi_cmd uscmd;
80 static char	ucdb[16];
81 uchar_t 	uscsi_status, rqstatus, rqresid;
82 int		total_devices_found = 0;
83 int		removable_found = 0;
84 
85 extern char	*global_intr_msg;
86 extern int	vol_running;
87 extern char	*dev_name;
88 extern int32_t	m_flag;
89 
90 /*
91  * ON-private functions from libvolmgt
92  */
93 int	_dev_mounted(char *path);
94 
95 /*
96  * Function prototypes.
97  */
98 static int		my_umount(char *mountp);
99 static int		my_volrmmount(char *real_name);
100 static int		vol_name_to_dev_node(char *vname, char *found);
101 static int		vol_lookup(char *supplied, char *found);
102 static device_t		*get_device(char *user_supplied, char *node);
103 static char		*get_physical_name(char *path);
104 static int		lookup_device(char *supplied, char *found);
105 static void		fini_device(device_t *dev);
106 static int		is_cd(char *node);
107 void			*my_zalloc(size_t size);
108 void			err_msg(char *fmt, ...);
109 int			inquiry(int fd, uchar_t *inq);
110 struct uscsi_cmd	*get_uscsi_cmd(void);
111 int			uscsi(int fd, struct uscsi_cmd *scmd);
112 int			get_mode_page(int fd, int page_no, int pc, int buf_len,
113 			    uchar_t *buffer);
114 int			mode_sense(int fd, uchar_t pc, int dbd, int page_len,
115 			    uchar_t *buffer);
116 uint16_t		read_scsi16(void *addr);
117 int			check_device(device_t *dev, int cond);
118 static void		get_media_info(device_t *t_dev, char *sdev,
119 			    char *pname, char *sn);
120 
121 extern void		process_p_flag(smedia_handle_t handle, int32_t fd);
122 
123 void
124 my_perror(char *err_string)
125 {
126 
127 	int error_no;
128 	if (errno == 0)
129 		return;
130 
131 	error_no = errno;
132 	(void) fprintf(stderr, "%s", err_string);
133 	(void) fprintf(stderr, gettext(" : "));
134 	errno = error_no;
135 	perror("");
136 }
137 
138 int32_t
139 get_confirmation()
140 {
141 	char c;
142 
143 	(void) fprintf(stderr, gettext("Do you want to continue? (y/n)"));
144 	c = getchar();
145 	if (c == 'y' || c == 'Y')
146 		return (1);
147 	else if (c == 'n' || c == 'N')
148 		return (0);
149 	else {
150 		(void) fprintf(stderr, gettext("Invalid choice\n"));
151 		return (0);
152 	}
153 }
154 
155 
156 void
157 get_passwd(struct smwp_state *wp, int32_t confirm)
158 {
159 	char passwd[256], re_passwd[256];
160 	int32_t len;
161 	struct termios tio;
162 	int32_t echo_off = 0;
163 	FILE *in, *out;
164 	char *buf;
165 
166 
167 	in = fopen("/dev/tty", "r+");
168 	if (in == NULL) {
169 		in = stdin;
170 		out = stderr;
171 	} else {
172 		out = in;
173 	}
174 
175 	/* Turn echoing off if it is on now.  */
176 
177 	if (tcgetattr(fileno(in), &tio) < 0) {
178 		PERROR("Echo off ioctl failed");
179 		exit(1);
180 	}
181 	if (tio.c_lflag & ECHO) {
182 		tio.c_lflag &= ~ECHO;
183 		/* echo_off = tcsetattr(fileno(in), TCSAFLUSH, &tio) == 0; */
184 		echo_off = tcsetattr(fileno(in), TCSAFLUSH, &tio) == 0;
185 		tio.c_lflag |= ECHO;
186 	}
187 
188 	/* CONSTCOND */
189 	while (1) {
190 		(void) fputs(
191 			gettext("Please enter password (32 chars maximum):"),
192 				out);
193 		(void) fflush(out);
194 		buf = fgets(passwd, (size_t)256, in);
195 		rewind(in);
196 		if (buf == NULL) {
197 			PERROR("Error reading password");
198 			continue;
199 		}
200 		len = strlen(passwd);
201 		(void) fputc('\n', out);
202 		len--;	/* To offset the \n */
203 		if ((len <= 0) || (len > 32)) {
204 			(void) fprintf(stderr,
205 				gettext("Invalid length of password \n"));
206 			(void) fputs("Try again\n", out);
207 			continue;
208 		}
209 
210 		if (!confirm)
211 			break;
212 
213 		(void) fputs("Please reenter password:", out);
214 		(void) fflush(out);
215 		buf = fgets(re_passwd, (size_t)256, in);
216 		rewind(in);
217 		(void) fputc('\n', out);
218 		if ((buf == NULL) || strcmp(passwd, re_passwd)) {
219 			(void) fputs("passwords did not match\n", out);
220 			(void) fputs("Try again\n", out);
221 		} else {
222 			break;
223 		}
224 	}
225 	wp->sm_passwd_len = len;
226 	(void) strncpy(wp->sm_passwd, passwd, wp->sm_passwd_len);
227 	wp->sm_version = SMWP_STATE_V_1;
228 
229 	/* Restore echoing.  */
230 	if (echo_off)
231 		(void) tcsetattr(fileno(in), TCSAFLUSH, &tio);
232 
233 }
234 
235 int32_t
236 check_and_unmount_vold(char *device_name, int32_t flag)
237 {
238 	char *real_name;
239 	char *nm;
240 	char tmp_path_name[PATH_MAX];
241 	struct stat stat_buf;
242 	int32_t ret_val = 0;
243 	struct	mnttab	*mntp;
244 	FILE	*fp;
245 	int nl;
246 
247 	DPRINTF1("Device name %s\n", device_name);
248 
249 	if (volmgt_running() == 0) {
250 		DPRINTF("Vold not running\n");
251 		return (0);
252 	}
253 	if ((nm = volmgt_symname(device_name)) == NULL) {
254 		DPRINTF("path not managed\n");
255 		real_name = media_findname(device_name);
256 	} else {
257 		DPRINTF1("path managed as %s\n", nm);
258 		real_name = media_findname(nm);
259 		DPRINTF1("real name %s\n", real_name);
260 	}
261 
262 	if (real_name == NULL)
263 		return (-1);
264 
265 	/*
266 	 * To find out whether the device has been mounted by
267 	 * volume manager...
268 	 *
269 	 * Convert the real name to a block device address.
270 	 * Do a partial match with the mnttab entries.
271 	 * Make sure the match is in the beginning to avoid if
272 	 * anybody puts a label similiar to volume manager path names.
273 	 * Then use "volrmmount -e <dev_name>" if -U flag is set.
274 	 */
275 
276 	nl = strlen("/vol/dev/");
277 
278 	if (strncmp(real_name, "/vol/dev/", nl) != 0)
279 			return (0);
280 	if (real_name[nl] == 'r') {
281 		(void) snprintf(tmp_path_name, PATH_MAX, "%s%s", "/vol/dev/",
282 			&real_name[nl + 1]);
283 	} else {
284 		(void) snprintf(tmp_path_name, PATH_MAX, "%s", real_name);
285 	}
286 	DPRINTF1("%s \n", tmp_path_name);
287 	ret_val = stat(tmp_path_name, &stat_buf);
288 	if (ret_val < 0) {
289 		PERROR("Could not stat");
290 		return (-1);
291 	}
292 
293 	fp = fopen("/etc/mnttab", "r");
294 
295 	if (fp == NULL) {
296 		PERROR("Could not open /etc/mnttab");
297 		return (-1);
298 	}
299 
300 	mntp = (struct mnttab *)malloc(sizeof (struct mnttab));
301 	if (mntp == NULL) {
302 		PERROR("malloc failed");
303 		(void) fclose(fp);
304 		return (-1);
305 	}
306 	errno = 0;
307 	while (getmntent(fp, mntp) == 0) {
308 		if (errno != 0) {
309 			PERROR("Error with mnttab");
310 			(void) fclose(fp);
311 			return (-1);
312 		}
313 		/* Is it a probable entry? */
314 		DPRINTF1(" %s \n", mntp->mnt_special);
315 		if (strstr(mntp->mnt_special, tmp_path_name) !=
316 			mntp->mnt_special) {
317 			/* Skip to next entry */
318 			continue;
319 		} else {
320 			DPRINTF1("Found!! %s\n", mntp->mnt_special);
321 			ret_val = 1;
322 			break;
323 		}
324 	}
325 
326 	if (ret_val == 1) {
327 		if (flag) {
328 			if (my_volrmmount(real_name) < 0) {
329 				ret_val = -1;
330 			}
331 		} else {
332 			ret_val = -1;
333 		}
334 	}
335 	(void) fclose(fp);
336 	free(mntp);
337 	return (ret_val);
338 }
339 
340 /*
341  * This routine checks if a device has mounted partitions. The
342  * device name is assumed to be /dev/rdsk/cNtNdNsN. So, this can
343  * be used for SCSI and PCMCIA cards.
344  * Returns
345  *	 0 : if not mounted
346  *	 1 : if successfully unmounted
347  *	-1 : Any error or umount failed
348  */
349 
350 int32_t
351 check_and_unmount_scsi(char *device_name, int32_t flag)
352 {
353 
354 	struct	mnttab	*mntrefp;
355 	struct	mnttab	*mntp;
356 	FILE	*fp;
357 	char block_dev_name[PATH_MAX];
358 	char tmp_name[PATH_MAX];
359 	int32_t  i, j;
360 	int32_t unmounted = 0;
361 
362 	/*
363 	 * If the device name is not a character special, anyway we
364 	 * can not progress further
365 	 */
366 
367 	if (strncmp(device_name, "/dev/rdsk/c", strlen("/dev/rdsk/c")) != 0)
368 		return (0);
369 
370 	(void) snprintf(block_dev_name, PATH_MAX, "/dev/%s",
371 		&device_name[strlen("/dev/r")]);
372 	fp = fopen("/etc/mnttab", "r");
373 
374 	if (fp == NULL) {
375 		PERROR("Could not open /etc/mnttab");
376 		return (-1);
377 	}
378 
379 	mntrefp = (struct mnttab *)malloc(sizeof (struct mnttab));
380 	if (mntrefp == NULL) {
381 		PERROR("malloc failed");
382 		(void) fclose(fp);
383 		return (-1);
384 	}
385 
386 	mntp = (struct mnttab *)malloc(sizeof (struct mnttab));
387 	if (mntp == NULL) {
388 		PERROR("malloc failed");
389 		(void) fclose(fp);
390 		free(mntrefp);
391 		return (-1);
392 	}
393 
394 	/* Try all the partitions */
395 
396 	(void) snprintf(tmp_name, PATH_MAX, "/dev/%s",
397 	    &device_name[strlen("/dev/r")]);
398 
399 	tmp_name[strlen("/dev/dsk/c0t0d0s")] = '\0';
400 
401 	errno = 0;
402 	while (getmntent(fp, mntp) == 0) {
403 		if (errno != 0) {
404 			PERROR("Error with mnttab");
405 			(void) fclose(fp);
406 			return (-1);
407 		}
408 		/* Is it a probable entry? */
409 		if (strncmp(mntp->mnt_special, tmp_name, strlen(tmp_name))) {
410 			/* Skip to next entry */
411 			continue;
412 		}
413 		for (i = 0; i < NDKMAP; i++) {
414 			/* Check for ufs style mount devices */
415 			(void) snprintf(block_dev_name, PATH_MAX,
416 			    "%s%d", tmp_name, i);
417 
418 			if (strcmp(mntp->mnt_special, block_dev_name) == 0) {
419 				if (flag) {
420 					if (my_umount(mntp->mnt_mountp) < 0) {
421 						(void) fclose(fp);
422 						return (-1);
423 					}
424 					unmounted = 1;
425 				} else {
426 					(void) fclose(fp);
427 					return (-1);
428 				}
429 				/* Skip to next entry */
430 				continue;
431 			}
432 
433 			/* Try for :1 -> :24 for pcfs */
434 
435 			for (j = 1; j < 24; j++) {
436 				(void) snprintf(block_dev_name, PATH_MAX,
437 				    "%s%d:%d", tmp_name, i, j);
438 
439 				if (strcmp(mntp->mnt_special,
440 					block_dev_name) == 0) {
441 					if (flag) {
442 						if (my_umount(mntp->mnt_mountp)
443 									< 0) {
444 							(void) fclose(fp);
445 							return (-1);
446 						}
447 						unmounted = 1;
448 					} else {
449 						(void) fclose(fp);
450 						return (-1);
451 					}
452 					/* Skip to next entry */
453 					continue;
454 				}
455 				(void) snprintf(block_dev_name, PATH_MAX,
456 				    "%s%d:%c", tmp_name, i, 'b' + j);
457 
458 				if (strcmp(mntp->mnt_special,
459 					block_dev_name) == 0) {
460 					if (flag) {
461 						if (my_umount(mntp->mnt_mountp)
462 									< 0) {
463 							(void) fclose(fp);
464 							return (-1);
465 						}
466 						unmounted = 1;
467 					} else {
468 						(void) fclose(fp);
469 						return (-1);
470 					}
471 					/* Skip to next entry */
472 					continue;
473 				}
474 			}
475 		}
476 
477 	}
478 
479 	if (unmounted)
480 		return (1);
481 	return (0);
482 }
483 
484 /*
485  * This routine checks if a device has mounted partitions. The
486  * device name is assumed to be /dev/rdiskette. So, this can
487  * be used for Floppy controllers
488  * Returns
489  *	 0 : if not mounted
490  *	 1 : if successfully unmounted
491  *	-1 : Any error or unmount failed
492  */
493 
494 int32_t
495 check_and_unmount_floppy(int32_t fd, int32_t flag)
496 {
497 	FILE	*fp = NULL;
498 	int32_t	mfd;
499 	struct dk_cinfo dkinfo, dkinfo_tmp;
500 	struct mnttab	mnt_record;
501 	struct mnttab	*mp = &mnt_record;
502 	struct stat	stbuf;
503 	char	raw_device[PATH_MAX];
504 	int32_t	found = 0;
505 
506 
507 	if (ioctl(fd, DKIOCINFO, &dkinfo) < 0) {
508 		return (-1);
509 	}
510 
511 	if ((fp = fopen(MNTTAB, "r")) == NULL) {
512 		PERROR("Could not open /etc/mnttab");
513 		(void) close(fd);
514 		exit(3);
515 	}
516 
517 	while (getmntent(fp, mp) == 0) {
518 		if (strstr(mp->mnt_special, "/dev/fd") == NULL &&
519 		    strstr(mp->mnt_special, "/dev/disket") == NULL &&
520 		    strstr(mp->mnt_special, "/dev/c") == NULL) {
521 			continue;
522 		}
523 
524 		(void) strcpy(raw_device, "/dev/r");
525 		(void) strcat(raw_device, mp->mnt_special + strlen("/dev/"));
526 
527 
528 		/*
529 		 * Attempt to open the device.	If it fails, skip it.
530 		 */
531 
532 		/* Turn on the privileges. */
533 		(void) __priv_bracket(PRIV_ON);
534 
535 		mfd = open(raw_device, O_RDWR | O_NDELAY);
536 
537 		/* Turn off the privileges. */
538 		(void) __priv_bracket(PRIV_OFF);
539 
540 		if (mfd < 0) {
541 			continue;
542 		}
543 
544 		/*
545 		 * Must be a character device
546 		 */
547 		if (fstat(mfd, &stbuf) < 0 || !S_ISCHR(stbuf.st_mode)) {
548 			(void) close(mfd);
549 			continue;
550 		}
551 		/*
552 		 * Attempt to read the configuration info on the disk.
553 		 */
554 		if (ioctl(mfd, DKIOCINFO, &dkinfo_tmp) < 0) {
555 			(void) close(mfd);
556 			continue;
557 		}
558 		/*
559 		 * Finished with the opened device
560 		 */
561 		(void) close(mfd);
562 
563 		/*
564 		 * If it's not the disk we're interested in, it doesn't apply.
565 		 */
566 		if (dkinfo.dki_ctype != dkinfo_tmp.dki_ctype ||
567 			dkinfo.dki_cnum != dkinfo_tmp.dki_cnum ||
568 			dkinfo.dki_unit != dkinfo_tmp.dki_unit) {
569 				continue;
570 		}
571 		/*
572 		 * It's a mount on the disk we're checking.  If we are
573 		 * checking whole disk, then we found trouble.	We can
574 		 * quit searching.
575 		 */
576 
577 		if (flag) {
578 			if (my_umount(mp->mnt_mountp) < 0) {
579 				return (-1);
580 			}
581 			found = 1;
582 		} else {
583 			return (-1);
584 		}
585 	}
586 	return (found);
587 }
588 
589 
590 int32_t
591 my_open(char *device_name, int32_t flags)
592 {
593 	char *real_name;
594 	char *nm;
595 	char tmp_path_name[PATH_MAX];
596 	struct stat stat_buf;
597 	int32_t ret_val;
598 	int32_t fd;
599 	int32_t have_read_priv = 0;
600 	DIR *dirp;
601 	struct dirent *dp;
602 
603 	DPRINTF1("Device name %s\n", device_name);
604 
605 	if ((nm = volmgt_symname(device_name)) == NULL) {
606 		DPRINTF("path not managed\n");
607 		real_name = media_findname(device_name);
608 	} else {
609 		DPRINTF1("path managed as %s\n", nm);
610 		real_name = media_findname(nm);
611 		DPRINTF1("real name %s\n", real_name);
612 	}
613 
614 	if (real_name == NULL)
615 		return (-1);
616 
617 	(void) strcpy(tmp_path_name, real_name);
618 	ret_val = stat(tmp_path_name, &stat_buf);
619 	if (ret_val < 0) {
620 		PERROR("Could not stat");
621 		return (-1);
622 	}
623 	if (S_ISDIR(stat_buf.st_mode)) {
624 
625 		/*
626 		 * Open the directory and look for the
627 		 * first non '.' entry.
628 		 * Since raw_read and raw_writes are used, we don't
629 		 * need to access the backup slice.
630 		 * For PCMCIA Memory cards, raw_read and raw_writes are
631 		 * not supported, but that is not a problem as, only slice2
632 		 * is allowed on PCMCIA memory cards.
633 		 */
634 
635 		/*
636 		 * First make sure we are operating with a /vol/....
637 		 * Otherwise it can dangerous,
638 		 * e.g. rmformat -s /dev/rdsk
639 		 * We should not look into the directory contents here.
640 		 */
641 		if (strncmp(tmp_path_name, "/vol/dev/", strlen("/vol/dev/"))
642 								!= 0) {
643 			(void) fprintf(stderr, gettext("The specified device \
644 is not a raw device.\n"));
645 			exit(1);
646 		}
647 
648 		/* Turn on the privileges. */
649 		(void) __priv_bracket(PRIV_ON);
650 
651 		dirp = opendir(tmp_path_name);
652 
653 		/* Turn off the privileges. */
654 		(void) __priv_bracket(PRIV_OFF);
655 
656 		if (dirp == NULL) {
657 			return (-1);
658 		}
659 
660 		/* Turn on the privileges. */
661 		(void) __priv_bracket(PRIV_ON);
662 		have_read_priv = 1;
663 
664 		while ((dp = readdir(dirp)) != NULL) {
665 
666 			/* Turn off the privileges. */
667 			(void) __priv_bracket(PRIV_OFF);
668 			have_read_priv = 0;
669 
670 			DPRINTF1("Found %s\n", dp->d_name);
671 			if ((strcmp(dp->d_name, ".") != 0) &&
672 			    (strcmp(dp->d_name, "..") != 0)) {
673 				(void) snprintf(tmp_path_name, PATH_MAX,
674 				    "%s/%s", tmp_path_name, dp->d_name);
675 
676 				DPRINTF1("tmp_pathname is %s\n", tmp_path_name);
677 				break;
678 			}
679 
680 			/* Turn on the privileges. */
681 			(void) __priv_bracket(PRIV_ON);
682 			have_read_priv = 1;
683 		}
684 
685 		if (have_read_priv) {
686 			/* drop the file_dac_read privilege */
687 			(void) __priv_bracket(PRIV_OFF);
688 			have_read_priv = 0;
689 		}
690 
691 		(void) closedir(dirp);
692 	}
693 
694 
695 	if (volmgt_running() == 0) {
696 		/* Turn on privileges. */
697 		(void) __priv_bracket(PRIV_ON);
698 		have_read_priv = 1;
699 	}
700 
701 	fd = open(tmp_path_name, flags);
702 
703 	if (have_read_priv) {
704 		/* Turn off privileges. */
705 		(void) __priv_bracket(PRIV_OFF);
706 		have_read_priv = 0;
707 	}
708 
709 	DPRINTF1("path opened %s\n", tmp_path_name);
710 
711 	return (fd);
712 }
713 
714 uint64_t
715 my_atoll(char *ptr)
716 {
717 	char *tmp_ptr = ptr;
718 	int32_t base = 10;
719 	uint64_t ret_val;
720 
721 	while (*tmp_ptr) {
722 		if (isdigit(*tmp_ptr))
723 			tmp_ptr++;
724 		else {
725 			base = 16;
726 			break;
727 		}
728 	}
729 	tmp_ptr = ptr;
730 	if (base == 16) {
731 		if (strlen(tmp_ptr) < 3) {
732 			return (-1);
733 		}
734 		if (*tmp_ptr++ != '0' || (*tmp_ptr != 'x' && *tmp_ptr != 'X')) {
735 			return (-1);
736 		}
737 		tmp_ptr++;
738 		while (*tmp_ptr) {
739 			if (isxdigit(*tmp_ptr))
740 				tmp_ptr++;
741 			else {
742 				return (-1);
743 			}
744 		}
745 	}
746 	ret_val = (uint64_t)strtoull(ptr, (char **)NULL, 0);
747 	return (ret_val);
748 }
749 
750 int32_t
751 write_sunos_label(int32_t fd, int32_t media_type)
752 {
753 
754 	struct extvtoc v_toc;
755 	int32_t ret;
756 
757 	(void) memset(&v_toc, 0, sizeof (struct extvtoc));
758 
759 	/* Initialize the vtoc information */
760 
761 	if (media_type == SM_FLOPPY) {
762 		struct fd_char fdchar;
763 		int32_t mult_factor;
764 
765 		if (ioctl(fd, FDIOGCHAR, &fdchar) < 0) {
766 			PERROR("FDIOGCHAR failed");
767 			return (-1);
768 		}
769 
770 		/* SPARC and x86 fd drivers use fdc_medium differently */
771 #if defined(__sparc)
772 		mult_factor = (fdchar.fdc_medium) ? 2 : 1;
773 #elif defined(__x86)
774 		mult_factor = (fdchar.fdc_medium == 5) ? 2 : 1;
775 #else
776 #error  No Platform defined
777 #endif /* defined(__sparc) */
778 
779 		/* initialize the vtoc structure */
780 		v_toc.v_nparts = 3;
781 
782 		v_toc.v_part[0].p_start = 0;
783 		v_toc.v_part[0].p_size = (fdchar.fdc_ncyl - 1) * 2 *
784 			fdchar.fdc_secptrack * mult_factor;
785 		v_toc.v_part[1].p_start = (fdchar.fdc_ncyl - 1) * 2 *
786 			fdchar.fdc_secptrack * mult_factor;
787 		v_toc.v_part[1].p_size = 2 * fdchar.fdc_secptrack * mult_factor;
788 
789 		v_toc.v_part[2].p_start = 0;
790 		v_toc.v_part[2].p_size = fdchar.fdc_ncyl * 2 *
791 			fdchar.fdc_secptrack * mult_factor;
792 
793 	} else if (media_type == SM_PCMCIA_MEM) {
794 
795 		static	struct dk_geom	dkg;
796 
797 		/*  Get card cyl/head/secptrack info  */
798 		if (ioctl(fd, DKIOCGGEOM, &dkg) < 0) {
799 			/*
800 			 * Card doesn't have a CIS. So, ask driver
801 			 * to probe card size info
802 			 */
803 			if (ioctl(fd, PCRAM_PROBESIZE, &dkg) < 0) {
804 				(void) fprintf(stderr,
805 				    gettext(
806 				"Could not get card size information\n"));
807 				(void) close(fd);
808 				exit(3);
809 			}
810 		}
811 
812 
813 		v_toc.v_part[2].p_start = 0;
814 		v_toc.v_part[2].p_size =  dkg.dkg_ncyl * dkg.dkg_nhead *
815 			dkg.dkg_nsect;
816 
817 		/* v_nparts was 1 in fdformat. But write vtoc failes */
818 		v_toc.v_nparts = 3;
819 
820 	} else if (media_type == SM_SCSI_FLOPPY) {
821 
822 		smedia_handle_t handle;
823 		smmedium_prop_t med_info;
824 		struct dk_geom dkgeom;
825 
826 
827 		/*
828 		 * call smedia_get_medium_property to get the
829 		 * correct media information, since DKIOCGMEDIAINFO
830 		 * may fail for unformatted media.
831 		 */
832 
833 		handle = smedia_get_handle(fd);
834 		if (handle == NULL) {
835 			(void) fprintf(stderr,
836 			gettext("Failed to get libsmedia handle.\n"));
837 
838 			(void) close(fd);
839 			return (-1);
840 		}
841 
842 
843 		if (smedia_get_medium_property(handle, &med_info) < 0) {
844 			(void) fprintf(stderr,
845 			    gettext("Get medium property failed \n"));
846 
847 			(void) smedia_release_handle(handle);
848 			(void) close(fd);
849 			return (-1);
850 		}
851 
852 		/* Fill in our own geometry information */
853 
854 		dkgeom.dkg_pcyl = med_info.sm_pcyl;
855 		dkgeom.dkg_ncyl = med_info.sm_pcyl;
856 		dkgeom.dkg_nhead = med_info.sm_nhead;
857 		dkgeom.dkg_nsect = med_info.sm_nsect;
858 		dkgeom.dkg_acyl = 0;
859 		dkgeom.dkg_bcyl = 0;
860 		dkgeom.dkg_intrlv = 0;
861 		dkgeom.dkg_apc = 0;
862 
863 		/*
864 		 * Try to set vtoc, if not successful we will
865 		 * continue to use the faked geometry information.
866 		 */
867 
868 		(void) ioctl(fd, DKIOCSGEOM, &dkgeom);
869 
870 		(void) smedia_release_handle(handle);
871 
872 		/* we want the same partitioning as used for normal floppies */
873 
874 		v_toc.v_part[0].p_start = 0;
875 		v_toc.v_part[0].p_size =  (diskaddr_t)(dkgeom.dkg_ncyl - 1) *
876 		    dkgeom.dkg_nhead * dkgeom.dkg_nsect;
877 
878 		v_toc.v_part[1].p_start = (diskaddr_t)(dkgeom.dkg_ncyl - 1) *
879 		    dkgeom.dkg_nhead * dkgeom.dkg_nsect;
880 		v_toc.v_part[1].p_size =  dkgeom.dkg_nhead * dkgeom.dkg_nsect;
881 
882 		v_toc.v_part[2].p_start = 0;
883 		v_toc.v_part[2].p_size = (diskaddr_t)dkgeom.dkg_ncyl *
884 		    dkgeom.dkg_nhead * dkgeom.dkg_nsect;
885 
886 		/* both write_vtoc and DKIOCSVTOC require V_NUMPAR partitions */
887 		v_toc.v_nparts = V_NUMPAR;
888 
889 	} else {
890 
891 		return (0);
892 	}
893 
894 	v_toc.v_sanity = VTOC_SANE;
895 	v_toc.v_version = V_VERSION;
896 
897 	/*
898 	 * The label structure is set up for DEV_BSIZE(512 byte) blocks,
899 	 * even though a medium density diskette has 1024 byte blocks
900 	 * See dklabel.h for more details.
901 	 */
902 	v_toc.v_sectorsz = DEV_BSIZE;
903 
904 	/* let the fd driver finish constructing the label and writing it. */
905 
906 
907 	/* Turn on the privileges. */
908 	(void) __priv_bracket(PRIV_ON);
909 
910 	ret = write_extvtoc(fd, &v_toc);
911 
912 	/* Turn off the privileges. */
913 	(void) __priv_bracket(PRIV_OFF);
914 
915 	if (ret < 0) {
916 		PERROR("Write vtoc");
917 		DPRINTF1("Write vtoc failed errno:%d\n", errno);
918 		return (-1);
919 	}
920 
921 	return (0);
922 }
923 
924 static void
925 intr_sig_handler()
926 {
927 	char c;
928 
929 	(void) fprintf(stderr, gettext(global_intr_msg));
930 	(void) fprintf(stderr,
931 	    gettext("\nDo you want to stop formatting?(y/n)"));
932 	(void) fflush(stdout);
933 	rewind(stdin);
934 	while ((c = getchar()) == -1);
935 	if (c == 'y' || c == 'Y') {
936 		(void) fprintf(stderr, gettext("Format interrupted\n"));
937 		exit(1);
938 	} else if (c == 'n' || c == 'N')
939 		return;
940 	else {
941 		(void) fprintf(stderr, gettext("Did not interrupt\n"));
942 		return;
943 	}
944 }
945 
946 static struct sigaction act, oact;
947 void
948 trap_SIGINT()
949 {
950 
951 	act.sa_handler = intr_sig_handler;
952 	(void) memset(&act.sa_mask, 0, sizeof (sigset_t));
953 	act.sa_flags = SA_RESTART; /* | SA_NODEFER; */
954 	if (sigaction(SIGINT, &act, &oact) < 0) {
955 		DPRINTF("sigset failed\n");
956 		return;
957 	}
958 }
959 
960 void
961 release_SIGINT()
962 {
963 	if (sigaction(SIGINT, &oact, (struct sigaction *)NULL) < 0) {
964 		DPRINTF("sigunset failed\n");
965 		return;
966 	}
967 }
968 
969 int32_t
970 verify(smedia_handle_t handle, int32_t fd, diskaddr_t start_sector,
971 	uint32_t nblocks, char *buf,
972 	int32_t flag, int32_t blocksize, int32_t no_raw_rw)
973 {
974 	uint64_t ret;
975 
976 	DPRINTF("ANALYSE MEDIA \n");
977 
978 
979 	if ((flag == VERIFY_READ) && (!no_raw_rw)) {
980 
981 		/* Turn on the privileges. */
982 		(void) __priv_bracket(PRIV_ON);
983 
984 		ret = smedia_raw_read(handle, start_sector, buf, nblocks *
985 			blocksize);
986 
987 		/* Turn off the privileges. */
988 		(void) __priv_bracket(PRIV_OFF);
989 
990 		if (ret != (nblocks * blocksize))
991 			return (-1);
992 		return (0);
993 
994 	} else if ((flag == VERIFY_WRITE) && (!no_raw_rw)) {
995 
996 		/* Turn on privileges. */
997 		(void) __priv_bracket(PRIV_ON);
998 
999 		ret = smedia_raw_write(handle, start_sector, buf, nblocks *
1000 			blocksize);
1001 
1002 		/* Turn off the privileges. */
1003 		(void) __priv_bracket(PRIV_OFF);
1004 
1005 		if (ret != (blocksize * nblocks))
1006 			return (-1);
1007 		return (0);
1008 
1009 	} else if ((flag == VERIFY_READ) && (no_raw_rw)) {
1010 		ret = llseek(fd, start_sector * blocksize, SEEK_SET);
1011 		if (ret != start_sector * blocksize) {
1012 			(void) fprintf(stderr, gettext("Seek failed\n"));
1013 			return (-2);
1014 		}
1015 
1016 		/* Turn on the privileges. */
1017 		(void) __priv_bracket(PRIV_ON);
1018 
1019 		ret = read(fd, buf, nblocks * blocksize);
1020 
1021 		/* Turn off the privileges. */
1022 		(void) __priv_bracket(PRIV_OFF);
1023 
1024 		if (ret != nblocks * blocksize) {
1025 			return (-1);
1026 		}
1027 		return (0);
1028 	} else if ((flag == VERIFY_WRITE) && (no_raw_rw)) {
1029 		ret = llseek(fd, start_sector * blocksize, SEEK_SET);
1030 		if (ret != start_sector * blocksize) {
1031 			(void) fprintf(stderr, gettext("Seek failed\n"));
1032 			return (-2);
1033 		}
1034 
1035 		/* Turn on the privileges. */
1036 		(void) __priv_bracket(PRIV_ON);
1037 
1038 		ret = write(fd, buf, nblocks * blocksize);
1039 
1040 		/* Turn off the privileges. */
1041 		(void) __priv_bracket(PRIV_OFF);
1042 
1043 		if (ret != nblocks * blocksize) {
1044 			return (-1);
1045 		}
1046 		return (0);
1047 	} else {
1048 		DPRINTF("Illegal parameter to verify_analysis!\n");
1049 		return (-1);
1050 	}
1051 }
1052 
1053 static int
1054 my_umount(char *mountp)
1055 {
1056 	pid_t	pid;	/* forked proc's pid */
1057 	int	rval;	/* proc's return value */
1058 
1059 
1060 	/* create a child to unmount the path */
1061 
1062 	/* Turn on the privileges */
1063 	(void) __priv_bracket(PRIV_ON);
1064 
1065 	pid = fork();
1066 
1067 	/* Turn off the privileges. */
1068 	(void) __priv_bracket(PRIV_OFF);
1069 
1070 	if (pid < 0) {
1071 		PERROR("fork failed");
1072 		exit(0);
1073 	}
1074 
1075 	if (pid == 0) {
1076 		/* the child */
1077 		/* get rid of those nasty err messages */
1078 		DPRINTF1("call_unmount_prog: calling %s \n", mountp);
1079 
1080 		/* Turn on the priviliges. */
1081 		(void) __priv_bracket(PRIV_ON);
1082 
1083 		if (execl("/usr/sbin/umount", "/usr/sbin/umount", mountp,
1084 			NULL) < 0) {
1085 			perror("exec failed");
1086 			/* Turn off the privileges */
1087 			(void) __priv_bracket(PRIV_OFF);
1088 			exit(-1);
1089 		}
1090 	}
1091 
1092 	/* wait for the umount command to exit */
1093 	rval = 0;
1094 	if (waitpid(pid, &rval, 0) == pid) {
1095 		if (WIFEXITED(rval)) {
1096 			if (WEXITSTATUS(rval) == 0) {
1097 				DPRINTF("umount : Success\n");
1098 				return (1);
1099 			}
1100 		}
1101 	}
1102 	return (-1);
1103 }
1104 
1105 static int
1106 my_volrmmount(char *real_name)
1107 {
1108 	int pid, rval;
1109 
1110 	/* Turn on the privileges. */
1111 	(void) __priv_bracket(PRIV_ON);
1112 
1113 	pid = fork();
1114 
1115 	/* Turn off the privileges. */
1116 	(void) __priv_bracket(PRIV_OFF);
1117 
1118 	/* create a child to unmount the path */
1119 	if (pid < 0) {
1120 		PERROR("fork failed");
1121 		exit(0);
1122 	}
1123 
1124 	if (pid == 0) {
1125 		/* the child */
1126 		/* get rid of those nasty err messages */
1127 		DPRINTF1("call_unmount_prog: calling %s \n",
1128 					"/usr/bin/volrmmount");
1129 
1130 		/* Turn on the privileges. */
1131 		(void) __priv_bracket(PRIV_ON);
1132 		if (execl("/usr/bin/volrmmount", "/usr/bin/volrmmount", "-e",
1133 			real_name, NULL) < 0) {
1134 			PERROR("volrmmount exec failed");
1135 			/* Turn off the privileges */
1136 			(void) __priv_bracket(PRIV_OFF);
1137 			exit(-1);
1138 		}
1139 	} else if (waitpid(pid, &rval, 0) == pid) {
1140 		if (WIFEXITED(rval)) {
1141 			if (WEXITSTATUS(rval) == 0) {
1142 				DPRINTF("volrmmount: Success\n");
1143 				return (1);
1144 			}
1145 		}
1146 	}
1147 	return (-1);
1148 }
1149 
1150 int
1151 find_device(int defer, char *tmpstr)
1152 {
1153 	DIR *dir;
1154 	struct dirent *dirent;
1155 	char sdev[PATH_MAX], dev[PATH_MAX], *pname;
1156 	device_t *t_dev;
1157 	int removable = 0;
1158 	int device_type = 0;
1159 	int hotpluggable = 0;
1160 	struct dk_minfo mediainfo;
1161 	static int found = 0;
1162 
1163 	dir = opendir("/dev/rdsk");
1164 	if (dir == NULL)
1165 		return (-1);
1166 
1167 	total_devices_found = 0;
1168 	while ((dirent = readdir(dir)) != NULL) {
1169 		if (dirent->d_name[0] == '.') {
1170 			continue;
1171 		}
1172 		(void) snprintf(sdev, PATH_MAX, "/dev/rdsk/%s",
1173 		    dirent->d_name);
1174 #ifdef sparc
1175 		if (!strstr(sdev, "s2")) {
1176 			continue;
1177 		}
1178 #else /* x86 */
1179 		if (vol_running) {
1180 			if (!(strstr(sdev, "s2") || strstr(sdev, "p0"))) {
1181 				continue;
1182 			}
1183 		} else {
1184 			if (!strstr(sdev, "p0")) {
1185 				continue;
1186 			}
1187 		}
1188 #endif
1189 		if (!lookup_device(sdev, dev)) {
1190 			continue;
1191 		}
1192 		if ((t_dev = get_device(NULL, dev)) == NULL) {
1193 			continue;
1194 		}
1195 		total_devices_found++;
1196 
1197 		if ((!defer) && !found) {
1198 			char *sn, *tmpbuf;
1199 			/*
1200 			 * dev_name is an optional command line input.
1201 			 */
1202 			if (dev_name) {
1203 				if (strstr(dirent->d_name, tmpstr)) {
1204 					found = 1;
1205 				} else if (!vol_running) {
1206 					continue;
1207 				}
1208 			}
1209 			/*
1210 			 * volmgt_symname() returns NULL if the device
1211 			 * is not managed by volmgt.
1212 			 */
1213 			sn = volmgt_symname(sdev);
1214 
1215 			if (vol_running && (sn != NULL)) {
1216 				if (strstr(sn, "dev") == NULL) {
1217 					tmpbuf = (char *)my_zalloc(PATH_MAX);
1218 					(void) strcpy(tmpbuf,
1219 					    "/vol/dev/aliases/");
1220 					(void) strcat(tmpbuf, sn);
1221 					free(sn);
1222 					sn = tmpbuf;
1223 				}
1224 				if (dev_name && !found) {
1225 					if (!strstr(tmpbuf, tmpstr)) {
1226 						continue;
1227 					} else {
1228 						found = 1;
1229 					}
1230 				}
1231 			}
1232 
1233 			/*
1234 			 * Get device type information for CD/DVD devices.
1235 			 */
1236 			if (is_cd(dev)) {
1237 				if (check_device(t_dev,
1238 				    CHECK_DEVICE_IS_DVD_WRITABLE)) {
1239 					device_type = DK_DVDR;
1240 				} else if (check_device(t_dev,
1241 				    CHECK_DEVICE_IS_DVD_READABLE)) {
1242 					device_type = DK_DVDROM;
1243 				} else if (check_device(t_dev,
1244 				    CHECK_DEVICE_IS_CD_WRITABLE)) {
1245 					device_type = DK_CDR;
1246 				} else {
1247 					device_type = DK_CDROM;
1248 				}
1249 			} else {
1250 				device_type = ioctl(t_dev->d_fd,
1251 				    DKIOCGMEDIAINFO, &mediainfo);
1252 				if (device_type < 0)
1253 					device_type = 0;
1254 				else
1255 					device_type = mediainfo.dki_media_type;
1256 			}
1257 
1258 			if (!ioctl(t_dev->d_fd, DKIOCREMOVABLE, &removable) &&
1259 			    !ioctl(t_dev->d_fd, DKIOCHOTPLUGGABLE,
1260 			    &hotpluggable)) {
1261 				if (removable || hotpluggable) {
1262 					removable_found++;
1263 					pname = get_physical_name(sdev);
1264 					if (sn) {
1265 						(void) printf("  %4d. "
1266 						    "Volmgt Node: %s\n",
1267 						    removable_found, sn);
1268 						(void) printf("        "
1269 						    "Logical Node: %s\n", sdev);
1270 						(void) printf("        "
1271 						    "Physical Node: %s\n",
1272 						    pname);
1273 					} else {
1274 						(void) printf("  %4d. "
1275 						    "Logical Node: %s\n",
1276 						    removable_found, sdev);
1277 						(void) printf("        "
1278 						    "Physical Node: %s\n",
1279 						    pname);
1280 					}
1281 					(void) printf("        Connected "
1282 					    "Device: %-8.8s %-16.16s "
1283 					    "%-4.4s\n",
1284 					    &t_dev->d_inq[8],
1285 					    &t_dev->d_inq[16],
1286 					    &t_dev->d_inq[32]);
1287 					(void) printf("        Device "
1288 					    "Type: ");
1289 				} else
1290 					continue;
1291 			} else
1292 				continue;
1293 
1294 			switch (device_type) {
1295 				case DK_CDROM:
1296 					(void) printf("CD Reader\n");
1297 					break;
1298 				case DK_CDR:
1299 				case DK_CDRW:
1300 					(void) printf("CD Reader/Writer\n");
1301 					break;
1302 				case DK_DVDROM:
1303 					(void) printf("DVD Reader\n");
1304 					break;
1305 				case DK_DVDR:
1306 				case DK_DVDRAM:
1307 					(void) printf("DVD Reader/Writer\n");
1308 					break;
1309 				case DK_FIXED_DISK:
1310 					if (strstr((const char *)
1311 					    &t_dev->d_inq[16], "FD") ||
1312 					    strstr((const char *)
1313 					    &t_dev->d_inq[16], "LS-120"))
1314 						(void) printf("Floppy "
1315 						    "drive\n");
1316 					else
1317 						(void) printf("Removable\n");
1318 					break;
1319 				case DK_FLOPPY:
1320 					(void) printf("Floppy drive\n");
1321 					break;
1322 				case DK_ZIP:
1323 					(void) printf("Zip drive\n");
1324 					break;
1325 				case DK_JAZ:
1326 					(void) printf("Jaz drive\n");
1327 					break;
1328 				default:
1329 					(void) printf("<Unknown>\n");
1330 					DPRINTF1("\t   %d\n", device_type);
1331 					break;
1332 			}
1333 			get_media_info(t_dev, sdev, pname, sn);
1334 		}
1335 		fini_device(t_dev);
1336 	}
1337 
1338 	(void) closedir(dir);
1339 	return (removable_found);
1340 }
1341 
1342 /*
1343  * Returns a device_t handle for a node returned by lookup_device()
1344  * and takes the user supplied name and stores it inside the node.
1345  */
1346 static device_t *
1347 get_device(char *user_supplied, char *node)
1348 {
1349 	device_t *dev;
1350 	int fd;
1351 	char devnode[PATH_MAX];
1352 	int size;
1353 
1354 	/*
1355 	 * we need to resolve any link paths to avoid fake files
1356 	 * such as /dev/rdsk/../../export/file.
1357 	 */
1358 	size = resolvepath(node, devnode, PATH_MAX);
1359 	if ((size <= 0) || (size >= (PATH_MAX - 1)))
1360 		return (NULL);
1361 
1362 	/* resolvepath may not return a null terminated string */
1363 	devnode[size] = '\0';
1364 
1365 
1366 	/* the device node must be in /devices/ or /vol/dev/rdsk */
1367 
1368 	if ((strncmp(devnode, "/devices/", 9) != 0) &&
1369 	    (strncmp(devnode, "/vol/dev/rdsk", 13) != 0))
1370 		return (NULL);
1371 
1372 	/* Turn on the privileges. */
1373 	(void) __priv_bracket(PRIV_ON);
1374 
1375 	/*
1376 	 * Since we are currently running with the user euid it is
1377 	 * safe to try to open the file without checking access.
1378 	 */
1379 
1380 	fd = open(devnode, O_RDONLY|O_NDELAY);
1381 
1382 	/* Turn off the privileges. */
1383 	(void) __priv_bracket(PRIV_OFF);
1384 
1385 	if (fd < 0) {
1386 		return (NULL);
1387 	}
1388 
1389 	dev = (device_t *)my_zalloc(sizeof (device_t));
1390 
1391 	dev->d_node = (char *)my_zalloc(strlen(devnode) + 1);
1392 	(void) strcpy(dev->d_node, devnode);
1393 
1394 	dev->d_fd = fd;
1395 
1396 	dev->d_inq = (uchar_t *)my_zalloc(INQUIRY_DATA_LENGTH);
1397 
1398 	/* Turn on privileges. */
1399 	(void) __priv_bracket(PRIV_ON);
1400 	if (!inquiry(fd, dev->d_inq)) {
1401 		DPRINTF1("USCSI ioctl failed %d\n",
1402 		    uscsi_error);
1403 		free(dev->d_inq);
1404 		free(dev->d_node);
1405 		(void) close(dev->d_fd);
1406 		free(dev);
1407 		/* Turn off privileges. */
1408 		(void) __priv_bracket(PRIV_OFF);
1409 		return (NULL);
1410 	}
1411 	/* Turn off privileges. */
1412 	(void) __priv_bracket(PRIV_OFF);
1413 
1414 	if (user_supplied) {
1415 		dev->d_name = (char *)my_zalloc(strlen(user_supplied) + 1);
1416 		(void) strcpy(dev->d_name, user_supplied);
1417 	}
1418 	return (dev);
1419 }
1420 
1421 /*
1422  * Check for device specific characteristics.
1423  */
1424 int
1425 check_device(device_t *dev, int cond)
1426 {
1427 	uchar_t page_code[4];
1428 
1429 	/* Look at the capabilities page for this information */
1430 	if (cond & CHECK_DEVICE_IS_CD_WRITABLE) {
1431 		if (get_mode_page(dev->d_fd, 0x2a, 0, 4, page_code) &&
1432 		    (page_code[3] & 1)) {
1433 			return (1);
1434 		}
1435 	}
1436 
1437 	if (cond & CHECK_DEVICE_IS_DVD_WRITABLE) {
1438 		if (get_mode_page(dev->d_fd, 0x2a, 0, 4, page_code) &&
1439 		    (page_code[3] & 0x10)) {
1440 			return (1);
1441 		}
1442 	}
1443 
1444 	if (cond & CHECK_DEVICE_IS_DVD_READABLE) {
1445 		if (get_mode_page(dev->d_fd, 0x2a, 0, 4, page_code) &&
1446 		    (page_code[2] & 0x8)) {
1447 			return (1);
1448 		}
1449 	}
1450 
1451 	return (0);
1452 }
1453 
1454 /*
1455  * Builds an open()able device path from a user supplied node which can be
1456  * of the * form of /dev/[r]dsk/cxtxdx[sx] or cxtxdx[sx] or volmgt-name like
1457  * cdrom[n].
1458  * Returns the path found in 'found' and returns 1. Otherwise returns 0.
1459  */
1460 int
1461 lookup_device(char *supplied, char *found)
1462 {
1463 	struct stat statbuf;
1464 	int fd;
1465 	char tmpstr[PATH_MAX];
1466 
1467 	/* Turn on privileges */
1468 	(void) __priv_bracket(PRIV_ON);
1469 
1470 	/* If everything is fine and proper, no need to analyze */
1471 	if ((stat(supplied, &statbuf) == 0) && S_ISCHR(statbuf.st_mode) &&
1472 	    ((fd = open(supplied, O_RDONLY|O_NDELAY)) >= 0)) {
1473 		(void) close(fd);
1474 		(void) strlcpy(found, supplied, PATH_MAX);
1475 		/* Turn off privilege */
1476 		(void) __priv_bracket(PRIV_OFF);
1477 		return (1);
1478 	}
1479 
1480 	/* Turn off privileges. */
1481 	(void) __priv_bracket(PRIV_OFF);
1482 
1483 	if (strncmp(supplied, "/dev/rdsk/", 10) == 0)
1484 		return (vol_lookup(supplied, found));
1485 	if (strncmp(supplied, "/dev/dsk/", 9) == 0) {
1486 		(void) snprintf(tmpstr, PATH_MAX, "/dev/rdsk/%s",
1487 		    (char *)strrchr(supplied, '/'));
1488 
1489 		if ((fd = open(tmpstr, O_RDONLY|O_NDELAY)) >= 0) {
1490 			(void) close(fd);
1491 			(void) strlcpy(found, supplied, PATH_MAX);
1492 			return (1);
1493 		}
1494 		if ((access(tmpstr, F_OK) == 0) && vol_running)
1495 			return (vol_lookup(tmpstr, found));
1496 		else
1497 			return (0);
1498 	}
1499 	if ((strncmp(supplied, "cdrom", 5) != 0) &&
1500 	    (strlen(supplied) < 32)) {
1501 		(void) snprintf(tmpstr, sizeof (tmpstr), "/dev/rdsk/%s",
1502 		    supplied);
1503 		if (access(tmpstr, F_OK) < 0) {
1504 			(void) strcat(tmpstr, "s2");
1505 		}
1506 		if ((fd = open(tmpstr, O_RDONLY|O_NDELAY)) >= 0) {
1507 			(void) close(fd);
1508 			(void) strlcpy(found, tmpstr, PATH_MAX);
1509 			return (1);
1510 		}
1511 		if ((access(tmpstr, F_OK) == 0) && vol_running)
1512 			return (vol_lookup(tmpstr, found));
1513 	}
1514 	return (vol_name_to_dev_node(supplied, found));
1515 }
1516 
1517 int
1518 is_cd(char *node)
1519 {
1520 	int fd;
1521 	struct dk_cinfo cinfo;
1522 
1523 	fd = open(node, O_RDONLY|O_NDELAY);
1524 	if (fd < 0)
1525 		return (0);
1526 	if (ioctl(fd, DKIOCINFO, &cinfo) < 0) {
1527 		(void) close(fd);
1528 		return (0);
1529 	}
1530 	if (cinfo.dki_ctype != DKC_CDROM)
1531 		return (0);
1532 	return (1);
1533 }
1534 
1535 void
1536 print_header(void)
1537 {
1538 	/* l10n_NOTE : Column spacing should be kept same */
1539 	(void) printf(gettext("    Node 		       "
1540 	    "Connected Device"));
1541 	/* l10n_NOTE : Column spacing should be kept same */
1542 	(void) printf(gettext(" 		Device type\n"));
1543 	(void) printf(
1544 	    "---------------------------+---------------------------");
1545 	(void) printf("-----+----------------\n");
1546 }
1547 
1548 void
1549 print_divider(void)
1550 {
1551 	(void) printf(
1552 	    "---------------------------+---------------------------");
1553 	(void) printf("-----+----------------\n");
1554 }
1555 
1556 static void
1557 fini_device(device_t *dev)
1558 {
1559 	free(dev->d_inq);
1560 	free(dev->d_node);
1561 	(void) close(dev->d_fd);
1562 	if (dev->d_name)
1563 		free(dev->d_name);
1564 	free(dev);
1565 }
1566 
1567 void *
1568 my_zalloc(size_t size)
1569 {
1570 	void *ret;
1571 
1572 	ret = malloc(size);
1573 	if (ret == NULL) {
1574 
1575 		/* Lets wait a sec. and try again */
1576 		if (errno == EAGAIN) {
1577 			(void) sleep(1);
1578 			ret = malloc(size);
1579 		}
1580 
1581 		if (ret == NULL) {
1582 			(void) err_msg("%s\n", gettext(strerror(errno)));
1583 			(void) err_msg(gettext(
1584 			    "Memory allocation failure, Exiting...\n"));
1585 			exit(1);
1586 		}
1587 	}
1588 	(void) memset(ret, 0, size);
1589 	return (ret);
1590 }
1591 
1592 static int
1593 vol_name_to_dev_node(char *vname, char *found)
1594 {
1595 	struct stat statbuf;
1596 	char *p1;
1597 	int i;
1598 
1599 	if (vname == NULL)
1600 		return (0);
1601 	if (vol_running)
1602 		(void) volmgt_check(vname);
1603 	p1 = media_findname(vname);
1604 	if (p1 == NULL)
1605 		return (0);
1606 	if (stat(p1, &statbuf) < 0) {
1607 		free(p1);
1608 		return (0);
1609 	}
1610 	if (S_ISDIR(statbuf.st_mode)) {
1611 		for (i = 0; i < 16; i++) {
1612 			(void) snprintf(found, PATH_MAX, "%s/s%d", p1, i);
1613 			if (access(found, F_OK) >= 0)
1614 				break;
1615 		}
1616 		if (i == 16) {
1617 			free(p1);
1618 			return (0);
1619 		}
1620 	} else {
1621 		(void) strlcpy(found, p1, PATH_MAX);
1622 	}
1623 	free(p1);
1624 	return (1);
1625 }
1626 
1627 /*
1628  * Searches for volume manager's equivalent char device for the
1629  * supplied pathname which is of the form of /dev/rdsk/cxtxdxsx
1630  */
1631 static int
1632 vol_lookup(char *supplied, char *found)
1633 {
1634 	char tmpstr[PATH_MAX], tmpstr1[PATH_MAX], *p;
1635 	int i, ret;
1636 
1637 	(void) strlcpy(tmpstr, supplied, PATH_MAX);
1638 	if ((p = volmgt_symname(tmpstr)) == NULL) {
1639 		if (strstr(tmpstr, "s2") != NULL) {
1640 			*((char *)(strrchr(tmpstr, 's') + 1)) = 0;
1641 			for (i = 0; i < 16; i++) {
1642 				(void) snprintf(tmpstr1, PATH_MAX, "%s%d",
1643 				    tmpstr, i);
1644 				if ((p = volmgt_symname(tmpstr1)) != NULL)
1645 					break;
1646 			}
1647 		} else if (strstr(tmpstr, "p0") != NULL) {
1648 			*((char *)(strrchr(tmpstr, 'p') + 1)) = 0;
1649 			for (i = 0; i < 5; i++) {
1650 				(void) snprintf(tmpstr1, PATH_MAX, "%s%d",
1651 					tmpstr, i);
1652 				if ((p = volmgt_symname(tmpstr1)) != NULL)
1653 					break;
1654 			}
1655 		} else
1656 			return (0);
1657 		if (p == NULL)
1658 			return (0);
1659 	}
1660 
1661 	ret = vol_name_to_dev_node(p, found);
1662 	free(p);
1663 	return (ret);
1664 }
1665 
1666 /*PRINTFLIKE1*/
1667 void
1668 err_msg(char *fmt, ...)
1669 {
1670 	va_list ap;
1671 
1672 	va_start(ap, fmt);
1673 	(void) vfprintf(stderr, fmt, ap);
1674 	va_end(ap);
1675 }
1676 
1677 int
1678 inquiry(int fd, uchar_t *inq)
1679 {
1680 	struct uscsi_cmd *scmd;
1681 
1682 	scmd = get_uscsi_cmd();
1683 	scmd->uscsi_flags = USCSI_READ|USCSI_SILENT;
1684 	scmd->uscsi_timeout = DEFAULT_SCSI_TIMEOUT;
1685 	scmd->uscsi_cdb[0] = INQUIRY_CMD;
1686 	scmd->uscsi_cdb[4] = INQUIRY_DATA_LENGTH;
1687 	scmd->uscsi_cdblen = 6;
1688 	scmd->uscsi_bufaddr = (char *)inq;
1689 	scmd->uscsi_buflen = INQUIRY_DATA_LENGTH;
1690 	if ((uscsi_error = uscsi(fd, scmd)) < 0)
1691 		return (0);
1692 	return (1);
1693 }
1694 
1695 struct uscsi_cmd *
1696 get_uscsi_cmd(void)
1697 {
1698 	(void) memset(&uscmd, 0, sizeof (uscmd));
1699 	(void) memset(ucdb, 0, 16);
1700 	uscmd.uscsi_cdb = ucdb;
1701 	return (&uscmd);
1702 }
1703 
1704 int
1705 uscsi(int fd, struct uscsi_cmd *scmd)
1706 {
1707 	int ret, global_rqsense;
1708 	int retries, max_retries = 5;
1709 	int i;
1710 
1711 	/* set up for request sense extensions */
1712 	if (!(scmd->uscsi_flags & USCSI_RQENABLE)) {
1713 		scmd->uscsi_flags |= USCSI_RQENABLE;
1714 		scmd->uscsi_rqlen = RQBUFLEN;
1715 		scmd->uscsi_rqbuf = rqbuf;
1716 		global_rqsense = 1;
1717 	} else {
1718 		global_rqsense = 0;
1719 	}
1720 
1721 	/*
1722 	 * The device may be busy or slow and fail with a not ready status.
1723 	 * we'll allow a limited number of retries to give the drive time
1724 	 * to recover.
1725 	 */
1726 	for (retries = 0; retries < max_retries; retries++) {
1727 
1728 		scmd->uscsi_status = 0;
1729 
1730 		if (global_rqsense)
1731 			(void) memset(rqbuf, 0, RQBUFLEN);
1732 
1733 		DPRINTF("cmd:[");
1734 		for (i = 0; i < scmd->uscsi_cdblen; i++)
1735 			DPRINTF1("0x%02x ",
1736 			    (uchar_t)scmd->uscsi_cdb[i]);
1737 		DPRINTF("]\n");
1738 
1739 		/*
1740 		 * We need to have root privledges in order to use
1741 		 * uscsi commands on the device.
1742 		 */
1743 
1744 		ret = ioctl(fd, USCSICMD, scmd);
1745 
1746 		/* maintain consistency in case of sgen */
1747 		if ((ret == 0) && (scmd->uscsi_status == 2)) {
1748 			ret = -1;
1749 			errno = EIO;
1750 		}
1751 
1752 		/* if error and extended request sense, retrieve errors */
1753 		if (global_rqsense && (ret < 0) && (scmd->uscsi_status == 2)) {
1754 			/*
1755 			 * The drive is not ready to recieve commands but
1756 			 * may be in the process of becoming ready.
1757 			 * sleep for a short time then retry command.
1758 			 * SENSE/ASC = 2/4 : not ready
1759 			 * ASCQ = 0  Not Reportable.
1760 			 * ASCQ = 1  Becoming ready.
1761 			 */
1762 			if ((SENSE_KEY(rqbuf) == 2) && (ASC(rqbuf) == 4) &&
1763 			    ((ASCQ(rqbuf) == 0) || (ASCQ(rqbuf) == 1))) {
1764 				total_retries++;
1765 				(void) sleep(3);
1766 				continue;
1767 			}
1768 
1769 			/*
1770 			 * Device is not ready to transmit or a device reset
1771 			 * has occurred. wait for a short period of time then
1772 			 * retry the command.
1773 			 */
1774 			if ((SENSE_KEY(rqbuf) == 6) && ((ASC(rqbuf) == 0x28) ||
1775 			    (ASC(rqbuf) == 0x29))) {
1776 				(void) sleep(3);
1777 				total_retries++;
1778 				continue;
1779 			}
1780 
1781 			DPRINTF3("cmd: 0x%02x ret:%i status:%02x ",
1782 			    (uchar_t)scmd->uscsi_cdb[0], ret,
1783 			    scmd->uscsi_status);
1784 			DPRINTF3(" sense: %02x ASC: %02x ASCQ:%02x\n",
1785 			    (uchar_t)SENSE_KEY(rqbuf),
1786 			    (uchar_t)ASC(rqbuf), (uchar_t)ASCQ(rqbuf));
1787 		}
1788 
1789 		/* no errors we'll return */
1790 		break;
1791 	}
1792 
1793 	/* store the error status for later debug printing */
1794 	if ((ret < 0) && (global_rqsense)) {
1795 		uscsi_status = scmd->uscsi_status;
1796 		rqstatus = scmd->uscsi_rqstatus;
1797 		rqresid = scmd->uscsi_rqresid;
1798 
1799 	}
1800 
1801 	DPRINTF1("total retries: %d\n", total_retries);
1802 
1803 	return (ret);
1804 }
1805 
1806 /*
1807  * will get the mode page only i.e. will strip off the header.
1808  */
1809 int
1810 get_mode_page(int fd, int page_no, int pc, int buf_len, uchar_t *buffer)
1811 {
1812 	int ret;
1813 	uchar_t byte2, *buf;
1814 	uint_t header_len, page_len, copy_cnt;
1815 
1816 	byte2 = (uchar_t)(((pc << 6) & 0xC0) | (page_no & 0x3f));
1817 	buf = (uchar_t *)my_zalloc(256);
1818 
1819 	/* Ask 254 bytes only to make our IDE driver happy */
1820 	ret = mode_sense(fd, byte2, 1, 254, buf);
1821 	if (ret == 0) {
1822 		free(buf);
1823 		return (0);
1824 	}
1825 
1826 	header_len = 8 + read_scsi16(&buf[6]);
1827 	page_len = buf[header_len + 1] + 2;
1828 
1829 	copy_cnt = (page_len > buf_len) ? buf_len : page_len;
1830 	(void) memcpy(buffer, &buf[header_len], copy_cnt);
1831 	free(buf);
1832 
1833 	return (1);
1834 }
1835 
1836 int
1837 mode_sense(int fd, uchar_t pc, int dbd, int page_len, uchar_t *buffer)
1838 {
1839 	struct uscsi_cmd *scmd;
1840 
1841 	scmd = get_uscsi_cmd();
1842 	scmd->uscsi_flags = USCSI_READ|USCSI_SILENT;
1843 	scmd->uscsi_buflen = page_len;
1844 	scmd->uscsi_bufaddr = (char *)buffer;
1845 	scmd->uscsi_timeout = DEFAULT_SCSI_TIMEOUT;
1846 	scmd->uscsi_cdblen = 0xa;
1847 	scmd->uscsi_cdb[0] = MODE_SENSE_10_CMD;
1848 	if (dbd) {
1849 		/* don't return any block descriptors */
1850 		scmd->uscsi_cdb[1] = 0x8;
1851 	}
1852 	/* the page code we want */
1853 	scmd->uscsi_cdb[2] = pc;
1854 	/* allocation length */
1855 	scmd->uscsi_cdb[7] = (page_len >> 8) & 0xff;
1856 	scmd->uscsi_cdb[8] = page_len & 0xff;
1857 
1858 	if ((uscsi_error = uscsi(fd, scmd)) < 0)
1859 		return (0);
1860 	return (1);
1861 }
1862 
1863 uint16_t
1864 read_scsi16(void *addr)
1865 {
1866 	uchar_t *ad = (uchar_t *)addr;
1867 	uint16_t ret;
1868 
1869 	ret = ((((uint16_t)ad[0]) << 8) | ad[1]);
1870 	return (ret);
1871 }
1872 
1873 /*
1874  * Allocate space for and return a pointer to a string
1875  * on the stack.  If the string is null, create
1876  * an empty string.
1877  * Use destroy_data() to free when no longer used.
1878  */
1879 char *
1880 alloc_string(s)
1881 	char    *s;
1882 {
1883 	char    *ns;
1884 
1885 	if (s == (char *)NULL) {
1886 		ns = (char *)my_zalloc(1);
1887 	} else {
1888 		ns = (char *)my_zalloc(strlen(s) + 1);
1889 		(void) strcpy(ns, s);
1890 	}
1891 	return (ns);
1892 }
1893 
1894 /*
1895  * Follow symbolic links from the logical device name to
1896  * the /devfs physical device name.  To be complete, we
1897  * handle the case of multiple links.  This function
1898  * either returns NULL (no links, or some other error),
1899  * or the physical device name, alloc'ed on the heap.
1900  *
1901  * Note that the standard /devices prefix is stripped from
1902  * the final pathname, if present.  The trailing options
1903  * are also removed (":c, raw").
1904  */
1905 static char *
1906 get_physical_name(char *path)
1907 {
1908 	struct stat	stbuf;
1909 	int		i;
1910 	int		level;
1911 	char		*p;
1912 	char		s[MAXPATHLEN];
1913 	char		buf[MAXPATHLEN];
1914 	char		dir[MAXPATHLEN];
1915 	char		savedir[MAXPATHLEN];
1916 	char		*result = NULL;
1917 
1918 	if (getcwd(savedir, sizeof (savedir)) == NULL) {
1919 		DPRINTF1("getcwd() failed - %s\n", strerror(errno));
1920 		return (NULL);
1921 	}
1922 
1923 	(void) strcpy(s, path);
1924 	if ((p = strrchr(s, '/')) != NULL) {
1925 		*p = 0;
1926 	}
1927 	if (s[0] == 0) {
1928 		(void) strcpy(s, "/");
1929 	}
1930 	if (chdir(s) == -1) {
1931 		DPRINTF2("cannot chdir() to %s - %s\n",
1932 			s, strerror(errno));
1933 		goto exit;
1934 	}
1935 
1936 	level = 0;
1937 	(void) strcpy(s, path);
1938 	for (;;) {
1939 		/*
1940 		 * See if there's a real file out there.  If not,
1941 		 * we have a dangling link and we ignore it.
1942 		 */
1943 		if (stat(s, &stbuf) == -1) {
1944 			goto exit;
1945 		}
1946 		if (lstat(s, &stbuf) == -1) {
1947 			DPRINTF2("%s: lstat() failed - %s\n",
1948 			    s, strerror(errno));
1949 			goto exit;
1950 		}
1951 		/*
1952 		 * If the file is not a link, we're done one
1953 		 * way or the other.  If there were links,
1954 		 * return the full pathname of the resulting
1955 		 * file.
1956 		 */
1957 		if (!S_ISLNK(stbuf.st_mode)) {
1958 			if (level > 0) {
1959 				/*
1960 				 * Strip trailing options from the
1961 				 * physical device name
1962 				 */
1963 				if ((p = strrchr(s, ':')) != NULL) {
1964 					*p = 0;
1965 				}
1966 				/*
1967 				 * Get the current directory, and
1968 				 * glue the pieces together.
1969 				 */
1970 				if (getcwd(dir, sizeof (dir)) == NULL) {
1971 					DPRINTF1("getcwd() failed - %s\n",
1972 						strerror(errno));
1973 					goto exit;
1974 				}
1975 				(void) strcat(dir, "/");
1976 				(void) strcat(dir, s);
1977 				/*
1978 				 * If we have the standard fixed
1979 				 * /devices prefix, remove it.
1980 				 */
1981 				p = (strstr(dir, DEVFS_PREFIX) == dir) ?
1982 					dir+strlen(DEVFS_PREFIX) : dir;
1983 				result = alloc_string(p);
1984 			}
1985 			goto exit;
1986 		}
1987 		i = readlink(s, buf, sizeof (buf));
1988 		if (i == -1) {
1989 			DPRINTF2("%s: readlink() failed - %s\n",
1990 				s, strerror(errno));
1991 			goto exit;
1992 		}
1993 		level++;
1994 		buf[i] = 0;
1995 
1996 		/*
1997 		 * Break up the pathname into the directory
1998 		 * reference, if applicable and simple filename.
1999 		 * chdir()'ing to the directory allows us to
2000 		 * handle links with relative pathnames correctly.
2001 		 */
2002 		(void) strcpy(dir, buf);
2003 		if ((p = strrchr(dir, '/')) != NULL) {
2004 			*p = 0;
2005 			if (chdir(dir) == -1) {
2006 				DPRINTF2("cannot chdir() to %s - %s\n",
2007 					dir, strerror(errno));
2008 				goto exit;
2009 			}
2010 			(void) strcpy(s, p+1);
2011 		} else {
2012 			(void) strcpy(s, buf);
2013 		}
2014 	}
2015 
2016 exit:
2017 	if (chdir(savedir) == -1) {
2018 		(void) printf("cannot chdir() to %s - %s\n",
2019 		    savedir, strerror(errno));
2020 	}
2021 
2022 	return (result);
2023 }
2024 
2025 static void
2026 get_media_info(device_t *t_dev, char *sdev, char *pname, char *sn)
2027 {
2028 	struct dk_cinfo cinfo;
2029 	struct extvtoc vtocinfo;
2030 	float size;
2031 	int32_t fd;
2032 	smedia_handle_t handle;
2033 	struct dk_minfo mediainfo;
2034 	int device_type;
2035 
2036 	device_type = ioctl(t_dev->d_fd, DKIOCGMEDIAINFO, &mediainfo);
2037 
2038 	/*
2039 	 * Determine bus type.
2040 	 */
2041 	if (!ioctl(t_dev->d_fd, DKIOCINFO, &cinfo)) {
2042 		if (strstr(cinfo.dki_cname, "usb") || strstr(pname, "usb")) {
2043 			(void) printf("\tBus: USB\n");
2044 		} else if (strstr(cinfo.dki_cname, "firewire") ||
2045 		    strstr(pname, "firewire")) {
2046 			(void) printf("\tBus: Firewire\n");
2047 		} else if (strstr(cinfo.dki_cname, "ide") ||
2048 		    strstr(pname, "ide")) {
2049 			(void) printf("\tBus: IDE\n");
2050 		} else if (strstr(cinfo.dki_cname, "scsi") ||
2051 		    strstr(pname, "scsi")) {
2052 			(void) printf("\tBus: SCSI\n");
2053 		} else {
2054 			(void) printf("\tBus: <Unknown>\n");
2055 		}
2056 	} else {
2057 		(void) printf("\tBus: <Unknown>\n");
2058 	}
2059 
2060 	/*
2061 	 * Calculate size of media.
2062 	 */
2063 	if (!device_type &&
2064 	    (!ioctl(t_dev->d_fd, DKIOCGMEDIAINFO, &mediainfo))) {
2065 		size = (mediainfo.dki_lbsize*
2066 		    mediainfo.dki_capacity)/(1024.0*1024.0);
2067 		if (size < 1000) {
2068 			(void) printf("\tSize: %.1f MB\n", size);
2069 		} else {
2070 			size = size/1000;
2071 			(void) printf("\tSize: %.1f GB\n", size);
2072 		}
2073 	} else {
2074 		(void) printf("\tSize: <Unknown>\n");
2075 	}
2076 
2077 	/*
2078 	 * Print label.
2079 	 */
2080 	if (!device_type && (read_extvtoc(t_dev->d_fd,  &vtocinfo) >= 0)) {
2081 		if (*vtocinfo.v_volume) {
2082 			(void) printf("\tLabel: %s\n", vtocinfo.v_volume);
2083 		} else {
2084 			(void) printf("\tLabel: <None>\n");
2085 		}
2086 	} else {
2087 		(void) printf("\tLabel: <Unknown>\n");
2088 	}
2089 
2090 	/*
2091 	 * Acess permissions.
2092 	 */
2093 	if (device_type) {
2094 		(void) printf("\tAccess permissions: <Unknown>\n");
2095 		return;
2096 	}
2097 
2098 	(void) fprintf(stderr, gettext("\tAccess permissions: "));
2099 	if (sn) {
2100 		/*
2101 		 * Set dev_name for process_p_flag().
2102 		 */
2103 		dev_name = sn;
2104 		fd = my_open(sn, O_RDONLY|O_NDELAY);
2105 	} else {
2106 		dev_name = sdev;
2107 		fd = my_open(sdev, O_RDONLY|O_NDELAY);
2108 	}
2109 	if (fd < 0)  {
2110 		(void) printf("<Unknown>\n");
2111 		DPRINTF("Could not open device.\n");
2112 		(void) close(fd);
2113 	} else {
2114 		/* register the fd with the libsmedia */
2115 		handle = smedia_get_handle(fd);
2116 		if (handle == NULL) {
2117 			(void) printf("<Unknown>\n");
2118 			DPRINTF("Failed to get libsmedia handle.\n");
2119 			(void) close(fd);
2120 		} else {
2121 			process_p_flag(handle, fd);
2122 		}
2123 	}
2124 	/* Clear dev_name */
2125 	dev_name = NULL;
2126 }
2127