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 5579df0adSaalok * Common Development and Distribution License (the "License"). 6579df0adSaalok * 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*7d82f0f8SDina K Nimeh * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 23579df0adSaalok * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #include <sys/types.h> 277c478bd9Sstevel@tonic-gate #include <sys/param.h> 287c478bd9Sstevel@tonic-gate #include <sys/stat.h> 297c478bd9Sstevel@tonic-gate #include <sys/statvfs.h> 307c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 317c478bd9Sstevel@tonic-gate #include <libintl.h> 327c478bd9Sstevel@tonic-gate #include <string.h> 337c478bd9Sstevel@tonic-gate #include <stdlib.h> 347c478bd9Sstevel@tonic-gate #include <stdarg.h> 357c478bd9Sstevel@tonic-gate #include <stdio.h> 367c478bd9Sstevel@tonic-gate #include <errno.h> 37579df0adSaalok #include <dlfcn.h> 38579df0adSaalok #include <link.h> 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gate #include "utils.h" 417c478bd9Sstevel@tonic-gate 42579df0adSaalok static void *lib_hdl = NULL; 43579df0adSaalok 447c478bd9Sstevel@tonic-gate static const char PNAME_FMT[] = "%s: "; 457c478bd9Sstevel@tonic-gate static const char ERRNO_FMT[] = ": %s\n"; 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate static const char *pname; 487c478bd9Sstevel@tonic-gate 497c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/ 50*7d82f0f8SDina K Nimeh void 517c478bd9Sstevel@tonic-gate warn(const char *format, ...) 527c478bd9Sstevel@tonic-gate { 537c478bd9Sstevel@tonic-gate int err = errno; 547c478bd9Sstevel@tonic-gate va_list alist; 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate if (pname != NULL) 577c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(PNAME_FMT), pname); 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate va_start(alist, format); 607c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, format, alist); 617c478bd9Sstevel@tonic-gate va_end(alist); 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate if (strchr(format, '\n') == NULL) 647c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERRNO_FMT), strerror(err)); 657c478bd9Sstevel@tonic-gate } 667c478bd9Sstevel@tonic-gate 677c478bd9Sstevel@tonic-gate /*PRINTFLIKE1*/ 687c478bd9Sstevel@tonic-gate void 697c478bd9Sstevel@tonic-gate die(const char *format, ...) 707c478bd9Sstevel@tonic-gate { 717c478bd9Sstevel@tonic-gate int err = errno; 727c478bd9Sstevel@tonic-gate va_list alist; 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate if (pname != NULL) 757c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(PNAME_FMT), pname); 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate va_start(alist, format); 787c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, format, alist); 797c478bd9Sstevel@tonic-gate va_end(alist); 807c478bd9Sstevel@tonic-gate 817c478bd9Sstevel@tonic-gate if (strchr(format, '\n') == NULL) 827c478bd9Sstevel@tonic-gate (void) fprintf(stderr, gettext(ERRNO_FMT), strerror(err)); 837c478bd9Sstevel@tonic-gate exit(E_ERROR); 847c478bd9Sstevel@tonic-gate } 857c478bd9Sstevel@tonic-gate 867c478bd9Sstevel@tonic-gate const char * 877c478bd9Sstevel@tonic-gate getpname(const char *arg0) 887c478bd9Sstevel@tonic-gate { 897c478bd9Sstevel@tonic-gate const char *p = strrchr(arg0, '/'); 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate if (p == NULL) 927c478bd9Sstevel@tonic-gate p = arg0; 937c478bd9Sstevel@tonic-gate else 947c478bd9Sstevel@tonic-gate p++; 957c478bd9Sstevel@tonic-gate 967c478bd9Sstevel@tonic-gate pname = p; 977c478bd9Sstevel@tonic-gate return (p); 987c478bd9Sstevel@tonic-gate } 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate int 1017c478bd9Sstevel@tonic-gate valid_abspath(const char *p) 1027c478bd9Sstevel@tonic-gate { 1037c478bd9Sstevel@tonic-gate if (p[0] != '/') { 1047c478bd9Sstevel@tonic-gate warn(gettext("pathname is not an absolute path -- %s\n"), p); 1057c478bd9Sstevel@tonic-gate return (0); 1067c478bd9Sstevel@tonic-gate } 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate if (strlen(p) > MAXPATHLEN) { 1097c478bd9Sstevel@tonic-gate warn(gettext("pathname is too long -- %s\n"), p); 1107c478bd9Sstevel@tonic-gate return (0); 1117c478bd9Sstevel@tonic-gate } 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate return (1); 1147c478bd9Sstevel@tonic-gate } 115579df0adSaalok 116579df0adSaalok /* 117579df0adSaalok * Wrapper for dlopen'ing a library. 118579df0adSaalok * The caller must call closelib() once 119579df0adSaalok * access to the library is no longer needed. 120579df0adSaalok */ 121579df0adSaalok void * 122579df0adSaalok openlib(const char *lib) 123579df0adSaalok { 124579df0adSaalok lib_hdl = dlopen(lib, RTLD_LAZY); 125579df0adSaalok return (lib_hdl); 126579df0adSaalok } 127579df0adSaalok 128579df0adSaalok void 129579df0adSaalok closelib() 130579df0adSaalok { 131579df0adSaalok if (lib_hdl != NULL) 132579df0adSaalok (void) dlclose(lib_hdl); 133579df0adSaalok } 134