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