xref: /illumos-gate/usr/src/lib/libvolmgt/common/volprivate.c (revision 18c2aff776a775d34a4c9893a4c72e0434d68e36)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*18c2aff7Sartem  * Common Development and Distribution License (the "License").
6*18c2aff7Sartem  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*18c2aff7Sartem  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23*18c2aff7Sartem  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
277c478bd9Sstevel@tonic-gate 
287c478bd9Sstevel@tonic-gate /*
297c478bd9Sstevel@tonic-gate  * routines in this module are meant to be called by other libvolmgt
307c478bd9Sstevel@tonic-gate  * routines only
317c478bd9Sstevel@tonic-gate  */
327c478bd9Sstevel@tonic-gate 
337c478bd9Sstevel@tonic-gate #include	<stdio.h>
347c478bd9Sstevel@tonic-gate #include	<string.h>
357c478bd9Sstevel@tonic-gate #include	<dirent.h>
367c478bd9Sstevel@tonic-gate #include	<string.h>
377c478bd9Sstevel@tonic-gate #include	<libintl.h>
387c478bd9Sstevel@tonic-gate #include	<limits.h>
397c478bd9Sstevel@tonic-gate #include	<unistd.h>
407c478bd9Sstevel@tonic-gate #include	<stdlib.h>
417c478bd9Sstevel@tonic-gate #include	<sys/types.h>
427c478bd9Sstevel@tonic-gate #include	<sys/stat.h>
437c478bd9Sstevel@tonic-gate #include	<sys/param.h>
447c478bd9Sstevel@tonic-gate #include	<sys/varargs.h>
457c478bd9Sstevel@tonic-gate #include	"volmgt_private.h"
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate /*
497c478bd9Sstevel@tonic-gate  * fix the getfull{raw,blk}name problem for the fd and diskette case
507c478bd9Sstevel@tonic-gate  *
517c478bd9Sstevel@tonic-gate  * return value is malloc'ed, and must be free'd
527c478bd9Sstevel@tonic-gate  *
537c478bd9Sstevel@tonic-gate  * no match gets a malloc'ed null string
547c478bd9Sstevel@tonic-gate  */
557c478bd9Sstevel@tonic-gate 
567c478bd9Sstevel@tonic-gate char *
577c478bd9Sstevel@tonic-gate volmgt_getfullblkname(char *n)
587c478bd9Sstevel@tonic-gate {
597c478bd9Sstevel@tonic-gate 	extern char	*getfullblkname(char *);
607c478bd9Sstevel@tonic-gate 	char		*rval;
617c478bd9Sstevel@tonic-gate 	char		namebuf[MAXPATHLEN+1];
627c478bd9Sstevel@tonic-gate 	char		*s;
637c478bd9Sstevel@tonic-gate 	char		c;
647c478bd9Sstevel@tonic-gate 	char		*res;
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate 	/* try to get full block-spcl device name */
697c478bd9Sstevel@tonic-gate 	rval = getfullblkname(n);
707c478bd9Sstevel@tonic-gate 	if ((rval != NULL) && (*rval != NULLC)) {
717c478bd9Sstevel@tonic-gate 		/* found it */
727c478bd9Sstevel@tonic-gate 		res = rval;
737c478bd9Sstevel@tonic-gate 		goto dun;
747c478bd9Sstevel@tonic-gate 	}
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate 	/* we have a null-string result */
777c478bd9Sstevel@tonic-gate 	if (rval != NULL) {
787c478bd9Sstevel@tonic-gate 		/* free null string */
797c478bd9Sstevel@tonic-gate 		free(rval);
807c478bd9Sstevel@tonic-gate 	}
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	/* ok, so we either have a bad device or a floppy */
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate 	/* try the rfd# or rdiskette forms */
857c478bd9Sstevel@tonic-gate 	if (((s = strstr(n, "/rfd")) != NULL) ||
867c478bd9Sstevel@tonic-gate 	    ((s = strstr(n, "/rdiskette")) != NULL) ||
877c478bd9Sstevel@tonic-gate 	    ((s = strstr(n, "/rdsk/")) != NULL)) {
887c478bd9Sstevel@tonic-gate 		/*
897c478bd9Sstevel@tonic-gate 		 * we do not have to check for room here, since we will
907c478bd9Sstevel@tonic-gate 		 * be making the string one shorter
917c478bd9Sstevel@tonic-gate 		 */
927c478bd9Sstevel@tonic-gate 		c = *++s;			/* save the first char */
937c478bd9Sstevel@tonic-gate 		*s = NULLC;			/* replace it with a null */
947c478bd9Sstevel@tonic-gate 		(void) strcpy(namebuf, n);	/* save first part of it */
957c478bd9Sstevel@tonic-gate 		*s++ = c;			/* give the first char back */
967c478bd9Sstevel@tonic-gate 		(void) strcat(namebuf, s);	/* copy the rest */
977c478bd9Sstevel@tonic-gate 		res = strdup(namebuf);
987c478bd9Sstevel@tonic-gate 		goto dun;
997c478bd9Sstevel@tonic-gate 	}
1007c478bd9Sstevel@tonic-gate 
1017c478bd9Sstevel@tonic-gate 	/* no match found */
1027c478bd9Sstevel@tonic-gate 	res = strdup("");
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate dun:
1057c478bd9Sstevel@tonic-gate 	return (res);
1067c478bd9Sstevel@tonic-gate }
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate char *
1107c478bd9Sstevel@tonic-gate volmgt_getfullrawname(char *n)
1117c478bd9Sstevel@tonic-gate {
1127c478bd9Sstevel@tonic-gate 	extern char	*getfullrawname(char *);
1137c478bd9Sstevel@tonic-gate 	char		*rval;
1147c478bd9Sstevel@tonic-gate 	char		namebuf[MAXPATHLEN+1];
1157c478bd9Sstevel@tonic-gate 	char		*s;
1167c478bd9Sstevel@tonic-gate 	char		c;
1177c478bd9Sstevel@tonic-gate 	char		*res;
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate #ifdef	DEBUG
1217c478bd9Sstevel@tonic-gate 	denter("volmgt_getfullrawname(%s): entering\n", n);
1227c478bd9Sstevel@tonic-gate #endif
1237c478bd9Sstevel@tonic-gate 	/* try to get full char-spcl device name */
1247c478bd9Sstevel@tonic-gate 	rval = getfullrawname(n);
1257c478bd9Sstevel@tonic-gate 	if ((rval != NULL) && (*rval != NULLC)) {
1267c478bd9Sstevel@tonic-gate 		/* found it */
1277c478bd9Sstevel@tonic-gate 		res = rval;
1287c478bd9Sstevel@tonic-gate 		goto dun;
1297c478bd9Sstevel@tonic-gate 	}
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate 	/* we have a null-string result */
1327c478bd9Sstevel@tonic-gate 	if (rval != NULL) {
1337c478bd9Sstevel@tonic-gate 		/* free null string */
1347c478bd9Sstevel@tonic-gate 		free(rval);
1357c478bd9Sstevel@tonic-gate 	}
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate 	/* ok, so we either have a bad device or a floppy */
1387c478bd9Sstevel@tonic-gate 
1397c478bd9Sstevel@tonic-gate 	/* try the "fd", "diskette", and the "dsk" form */
1407c478bd9Sstevel@tonic-gate 	if (((s = strstr(n, "/fd")) != NULL) ||
1417c478bd9Sstevel@tonic-gate 	    ((s = strstr(n, "/diskette")) != NULL) ||
1427c478bd9Sstevel@tonic-gate 	    ((s = strstr(n, "/dsk/")) != NULL)) {
1437c478bd9Sstevel@tonic-gate 		/*
1447c478bd9Sstevel@tonic-gate 		 * ensure we have room to add one more char
1457c478bd9Sstevel@tonic-gate 		 */
1467c478bd9Sstevel@tonic-gate 		if (strlen(n) < (MAXPATHLEN - 1)) {
1477c478bd9Sstevel@tonic-gate 			c = *++s;		/* save the first char */
1487c478bd9Sstevel@tonic-gate 			*s = NULLC;		/* replace it with a null */
1497c478bd9Sstevel@tonic-gate 			(void) strcpy(namebuf, n); /* save first part of str */
1507c478bd9Sstevel@tonic-gate 			*s = c;			/* put first charback */
1517c478bd9Sstevel@tonic-gate 			(void) strcat(namebuf, "r"); /* insert an 'r' */
1527c478bd9Sstevel@tonic-gate 			(void) strcat(namebuf, s); /* copy rest of str */
1537c478bd9Sstevel@tonic-gate 			res = strdup(namebuf);
1547c478bd9Sstevel@tonic-gate 			goto dun;
1557c478bd9Sstevel@tonic-gate 		}
1567c478bd9Sstevel@tonic-gate 	}
1577c478bd9Sstevel@tonic-gate 
1587c478bd9Sstevel@tonic-gate 	/* no match found */
1597c478bd9Sstevel@tonic-gate 	res = strdup("");
1607c478bd9Sstevel@tonic-gate dun:
1617c478bd9Sstevel@tonic-gate #ifdef	DEBUG
1627c478bd9Sstevel@tonic-gate 	dexit("volmgt_getfullrawname: returning %s\n",
1637c478bd9Sstevel@tonic-gate 	    res ? res : "<null ptr>");
1647c478bd9Sstevel@tonic-gate #endif
1657c478bd9Sstevel@tonic-gate 	return (res);
1667c478bd9Sstevel@tonic-gate }
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 
1697c478bd9Sstevel@tonic-gate #ifdef	DEBUG
1707c478bd9Sstevel@tonic-gate 
1717c478bd9Sstevel@tonic-gate /*
1727c478bd9Sstevel@tonic-gate  * debug print routines -- private to libvolmgt
1737c478bd9Sstevel@tonic-gate  */
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate #define	DEBUG_INDENT_SPACES	"  "
1767c478bd9Sstevel@tonic-gate 
1777c478bd9Sstevel@tonic-gate int	debug_level = 0;
1787c478bd9Sstevel@tonic-gate 
1797c478bd9Sstevel@tonic-gate 
1807c478bd9Sstevel@tonic-gate static void
1817c478bd9Sstevel@tonic-gate derrprint(char *fmt, va_list ap)
1827c478bd9Sstevel@tonic-gate {
1837c478bd9Sstevel@tonic-gate 	int		i;
1847c478bd9Sstevel@tonic-gate 	int		j;
1857c478bd9Sstevel@tonic-gate 	char		date_buf[256];
1867c478bd9Sstevel@tonic-gate 	time_t		t;
1877c478bd9Sstevel@tonic-gate 	struct tm	*tm;
1887c478bd9Sstevel@tonic-gate 
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	(void) time(&t);
1917c478bd9Sstevel@tonic-gate 	tm = localtime(&t);
1927c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, "%02d/%02d/%02d %02d:%02d:%02d ",
1937c478bd9Sstevel@tonic-gate 	    tm->tm_mon+1, tm->tm_mday, tm->tm_year % 100,
1947c478bd9Sstevel@tonic-gate 	    tm->tm_hour, tm->tm_min, tm->tm_sec);
1957c478bd9Sstevel@tonic-gate 	for (i = 0; i < debug_level; i++) {
1967c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, DEBUG_INDENT_SPACES);
1977c478bd9Sstevel@tonic-gate 	}
1987c478bd9Sstevel@tonic-gate 	(void) vfprintf(stderr, fmt, ap);
1997c478bd9Sstevel@tonic-gate }
2007c478bd9Sstevel@tonic-gate 
2017c478bd9Sstevel@tonic-gate /*
2027c478bd9Sstevel@tonic-gate  * denter -- do a derrprint(), then increment debug level
2037c478bd9Sstevel@tonic-gate  */
2047c478bd9Sstevel@tonic-gate void
2057c478bd9Sstevel@tonic-gate denter(char *fmt, ...)
2067c478bd9Sstevel@tonic-gate {
2077c478bd9Sstevel@tonic-gate 	va_list		ap;
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
2107c478bd9Sstevel@tonic-gate 	derrprint(fmt, ap);
2117c478bd9Sstevel@tonic-gate 	va_end(ap);
2127c478bd9Sstevel@tonic-gate 	debug_level++;
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate /*
2167c478bd9Sstevel@tonic-gate  * dexit -- decrement debug level then do a derrprint()
2177c478bd9Sstevel@tonic-gate  */
2187c478bd9Sstevel@tonic-gate void
2197c478bd9Sstevel@tonic-gate dexit(char *fmt, ...)
2207c478bd9Sstevel@tonic-gate {
2217c478bd9Sstevel@tonic-gate 	va_list		ap;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	if (--debug_level < 0) {
2247c478bd9Sstevel@tonic-gate 		debug_level = 0;
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
2277c478bd9Sstevel@tonic-gate 	derrprint(fmt, ap);
2287c478bd9Sstevel@tonic-gate 	va_end(ap);
2297c478bd9Sstevel@tonic-gate }
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate /*
2327c478bd9Sstevel@tonic-gate  * dprintf -- print debug info, indenting based on debug level
2337c478bd9Sstevel@tonic-gate  */
2347c478bd9Sstevel@tonic-gate void
2357c478bd9Sstevel@tonic-gate dprintf(char *fmt, ...)
2367c478bd9Sstevel@tonic-gate {
2377c478bd9Sstevel@tonic-gate 	va_list		ap;
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 	va_start(ap, fmt);
2407c478bd9Sstevel@tonic-gate 	derrprint(fmt, ap);
2417c478bd9Sstevel@tonic-gate 	va_end(ap);
2427c478bd9Sstevel@tonic-gate }
2437c478bd9Sstevel@tonic-gate 
2447c478bd9Sstevel@tonic-gate #endif	/* DEBUG */
245