xref: /onnv-gate/usr/src/cmd/lofiadm/utils.c (revision 8313:88e485ada83a)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
55719Saalok  * Common Development and Distribution License (the "License").
65719Saalok  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*8313SDina.Nimeh@Sun.Com  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
235719Saalok  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <sys/param.h>
280Sstevel@tonic-gate #include <sys/stat.h>
290Sstevel@tonic-gate #include <sys/statvfs.h>
300Sstevel@tonic-gate #include <sys/sysmacros.h>
310Sstevel@tonic-gate #include <libintl.h>
320Sstevel@tonic-gate #include <string.h>
330Sstevel@tonic-gate #include <stdlib.h>
340Sstevel@tonic-gate #include <stdarg.h>
350Sstevel@tonic-gate #include <stdio.h>
360Sstevel@tonic-gate #include <errno.h>
375719Saalok #include <dlfcn.h>
385719Saalok #include <link.h>
390Sstevel@tonic-gate 
400Sstevel@tonic-gate #include "utils.h"
410Sstevel@tonic-gate 
425719Saalok static void *lib_hdl = NULL;
435719Saalok 
440Sstevel@tonic-gate static const char PNAME_FMT[] = "%s: ";
450Sstevel@tonic-gate static const char ERRNO_FMT[] = ": %s\n";
460Sstevel@tonic-gate 
470Sstevel@tonic-gate static const char *pname;
480Sstevel@tonic-gate 
490Sstevel@tonic-gate /*PRINTFLIKE1*/
50*8313SDina.Nimeh@Sun.Com void
warn(const char * format,...)510Sstevel@tonic-gate warn(const char *format, ...)
520Sstevel@tonic-gate {
530Sstevel@tonic-gate 	int err = errno;
540Sstevel@tonic-gate 	va_list alist;
550Sstevel@tonic-gate 
560Sstevel@tonic-gate 	if (pname != NULL)
570Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(PNAME_FMT), pname);
580Sstevel@tonic-gate 
590Sstevel@tonic-gate 	va_start(alist, format);
600Sstevel@tonic-gate 	(void) vfprintf(stderr, format, alist);
610Sstevel@tonic-gate 	va_end(alist);
620Sstevel@tonic-gate 
630Sstevel@tonic-gate 	if (strchr(format, '\n') == NULL)
640Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERRNO_FMT), strerror(err));
650Sstevel@tonic-gate }
660Sstevel@tonic-gate 
670Sstevel@tonic-gate /*PRINTFLIKE1*/
680Sstevel@tonic-gate void
die(const char * format,...)690Sstevel@tonic-gate die(const char *format, ...)
700Sstevel@tonic-gate {
710Sstevel@tonic-gate 	int err = errno;
720Sstevel@tonic-gate 	va_list alist;
730Sstevel@tonic-gate 
740Sstevel@tonic-gate 	if (pname != NULL)
750Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(PNAME_FMT), pname);
760Sstevel@tonic-gate 
770Sstevel@tonic-gate 	va_start(alist, format);
780Sstevel@tonic-gate 	(void) vfprintf(stderr, format, alist);
790Sstevel@tonic-gate 	va_end(alist);
800Sstevel@tonic-gate 
810Sstevel@tonic-gate 	if (strchr(format, '\n') == NULL)
820Sstevel@tonic-gate 		(void) fprintf(stderr, gettext(ERRNO_FMT), strerror(err));
830Sstevel@tonic-gate 	exit(E_ERROR);
840Sstevel@tonic-gate }
850Sstevel@tonic-gate 
860Sstevel@tonic-gate const char *
getpname(const char * arg0)870Sstevel@tonic-gate getpname(const char *arg0)
880Sstevel@tonic-gate {
890Sstevel@tonic-gate 	const char *p = strrchr(arg0, '/');
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	if (p == NULL)
920Sstevel@tonic-gate 		p = arg0;
930Sstevel@tonic-gate 	else
940Sstevel@tonic-gate 		p++;
950Sstevel@tonic-gate 
960Sstevel@tonic-gate 	pname = p;
970Sstevel@tonic-gate 	return (p);
980Sstevel@tonic-gate }
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate int
valid_abspath(const char * p)1010Sstevel@tonic-gate valid_abspath(const char *p)
1020Sstevel@tonic-gate {
1030Sstevel@tonic-gate 	if (p[0] != '/') {
1040Sstevel@tonic-gate 		warn(gettext("pathname is not an absolute path -- %s\n"), p);
1050Sstevel@tonic-gate 		return (0);
1060Sstevel@tonic-gate 	}
1070Sstevel@tonic-gate 
1080Sstevel@tonic-gate 	if (strlen(p) > MAXPATHLEN) {
1090Sstevel@tonic-gate 		warn(gettext("pathname is too long -- %s\n"), p);
1100Sstevel@tonic-gate 		return (0);
1110Sstevel@tonic-gate 	}
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 	return (1);
1140Sstevel@tonic-gate }
1155719Saalok 
1165719Saalok /*
1175719Saalok  * Wrapper for dlopen'ing a library.
1185719Saalok  * The caller must call closelib() once
1195719Saalok  * access to the library is no longer needed.
1205719Saalok  */
1215719Saalok void *
openlib(const char * lib)1225719Saalok openlib(const char *lib)
1235719Saalok {
1245719Saalok 	lib_hdl = dlopen(lib, RTLD_LAZY);
1255719Saalok 	return (lib_hdl);
1265719Saalok }
1275719Saalok 
1285719Saalok void
closelib()1295719Saalok closelib()
1305719Saalok {
1315719Saalok 	if (lib_hdl != NULL)
1325719Saalok 		(void) dlclose(lib_hdl);
1335719Saalok }
134