xref: /onnv-gate/usr/src/cmd/sgs/moe/common/moe.c (revision 11827:d7ef53deac3f)
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
52647Srie  * Common Development and Distribution License (the "License").
62647Srie  * 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  */
212647Srie 
220Sstevel@tonic-gate /*
23*11827SRod.Evans@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #include <dlfcn.h>
280Sstevel@tonic-gate #include <link.h>
290Sstevel@tonic-gate #include <stdio.h>
300Sstevel@tonic-gate #include <string.h>
310Sstevel@tonic-gate #include <stdarg.h>
320Sstevel@tonic-gate #include <libintl.h>
330Sstevel@tonic-gate #include <locale.h>
340Sstevel@tonic-gate #include <conv.h>
350Sstevel@tonic-gate #include <msg.h>
360Sstevel@tonic-gate 
370Sstevel@tonic-gate void
locale()380Sstevel@tonic-gate locale()
390Sstevel@tonic-gate {
400Sstevel@tonic-gate 	static int	localeinit = 0;
410Sstevel@tonic-gate 
420Sstevel@tonic-gate 	/*
430Sstevel@tonic-gate 	 * Defer localization overhead until a localized message, is required.
440Sstevel@tonic-gate 	 * For successful specific (32-bit or 64-bit) commands, none of this
450Sstevel@tonic-gate 	 * overhead should be incurred.
460Sstevel@tonic-gate 	 */
470Sstevel@tonic-gate 	if (localeinit++)
480Sstevel@tonic-gate 		return;
490Sstevel@tonic-gate 
500Sstevel@tonic-gate 	(void) setlocale(LC_MESSAGES, MSG_ORIG(MSG_STR_EMPTY));
510Sstevel@tonic-gate 	(void) textdomain(MSG_ORIG(MSG_SUNW_OST_SGS));
520Sstevel@tonic-gate }
530Sstevel@tonic-gate 
540Sstevel@tonic-gate const char *
_moe_msg(Msg mid)550Sstevel@tonic-gate _moe_msg(Msg mid)
560Sstevel@tonic-gate {
570Sstevel@tonic-gate 	return (gettext(MSG_ORIG(mid)));
580Sstevel@tonic-gate }
590Sstevel@tonic-gate 
600Sstevel@tonic-gate /*
610Sstevel@tonic-gate  * Error messages from ld.so.1 are formatted as:
620Sstevel@tonic-gate  *
630Sstevel@tonic-gate  *	ld.so.1: app-name: fatal: ....
640Sstevel@tonic-gate  *
650Sstevel@tonic-gate  * Here, we skip over these three components.  Thus the message a little less
660Sstevel@tonic-gate  * hostile when displayed by moe().  Really, it would be nice to have some
670Sstevel@tonic-gate  * flexibility over what ld.so.1 displays.
680Sstevel@tonic-gate  */
690Sstevel@tonic-gate static char *
trim_msg(char * str)700Sstevel@tonic-gate trim_msg(char *str)
710Sstevel@tonic-gate {
720Sstevel@tonic-gate 	char	*ptr = str;
730Sstevel@tonic-gate 	int	cnt = 0;
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	/*
760Sstevel@tonic-gate 	 * Skip the first three components.
770Sstevel@tonic-gate 	 */
780Sstevel@tonic-gate 	while (*ptr) {
790Sstevel@tonic-gate 		if (*ptr == ':') {
800Sstevel@tonic-gate 			if (++cnt == 3)
810Sstevel@tonic-gate 				break;
820Sstevel@tonic-gate 		}
830Sstevel@tonic-gate 		ptr++;
840Sstevel@tonic-gate 	}
850Sstevel@tonic-gate 	if (*ptr == '\0')
860Sstevel@tonic-gate 		return (str);
870Sstevel@tonic-gate 	else
880Sstevel@tonic-gate 		return (ptr + 2);
890Sstevel@tonic-gate }
900Sstevel@tonic-gate 
910Sstevel@tonic-gate #define	ONLY32	1
920Sstevel@tonic-gate #define	ONLY64	2
930Sstevel@tonic-gate 
940Sstevel@tonic-gate static int
openlib(const char * prog,const char * name,int class,int silent,int verbose)950Sstevel@tonic-gate openlib(const char *prog, const char *name, int class, int silent, int verbose)
960Sstevel@tonic-gate {
970Sstevel@tonic-gate 	void		*handle;
980Sstevel@tonic-gate 	const char	*modestr;
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	/*
1010Sstevel@tonic-gate 	 * If the class of object is required, localize the prefix message.
1020Sstevel@tonic-gate 	 */
1030Sstevel@tonic-gate 	if (class) {
1040Sstevel@tonic-gate 		locale();
1050Sstevel@tonic-gate #if	defined(_LP64)
1060Sstevel@tonic-gate 		modestr = MSG_INTL(MSG_PRE_64);
1070Sstevel@tonic-gate #else
1080Sstevel@tonic-gate 		modestr = MSG_INTL(MSG_PRE_32);
1090Sstevel@tonic-gate #endif
1100Sstevel@tonic-gate 	} else
1110Sstevel@tonic-gate 		modestr = MSG_ORIG(MSG_STR_EMPTY);
1120Sstevel@tonic-gate 
1130Sstevel@tonic-gate 
1140Sstevel@tonic-gate 	/*
1150Sstevel@tonic-gate 	 * Open the optimal object, and determine its full name from the
1160Sstevel@tonic-gate 	 * returned handle.  Borrow the internal mode, RTLD_CONFGEN, from
1170Sstevel@tonic-gate 	 * crle(1).  This flag allows us to process incomplete objects, as
1180Sstevel@tonic-gate 	 * would occur if the object couldn't find its dependencies or relocate
1190Sstevel@tonic-gate 	 * itself.
1200Sstevel@tonic-gate 	 */
1210Sstevel@tonic-gate 	if ((handle = dlmopen(LM_ID_NEWLM, name,
1220Sstevel@tonic-gate 	    (RTLD_FIRST | RTLD_CONFGEN | RTLD_LAZY))) == 0) {
123*11827SRod.Evans@Sun.COM 		if (verbose) {
1240Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_ORIG(MSG_FMT_VERBOSE), prog,
1250Sstevel@tonic-gate 			    modestr, trim_msg(dlerror()));
1260Sstevel@tonic-gate 			(void) fflush(stderr);
127*11827SRod.Evans@Sun.COM 		}
1280Sstevel@tonic-gate 		return (1);
1290Sstevel@tonic-gate 	}
13010792SRod.Evans@Sun.COM 	if (silent == 0) {
13110792SRod.Evans@Sun.COM 		Link_map	*lmp;
1320Sstevel@tonic-gate 
13310792SRod.Evans@Sun.COM 		if (dlinfo(handle, RTLD_DI_LINKMAP, &lmp) == -1) {
134*11827SRod.Evans@Sun.COM 			if (verbose) {
13510792SRod.Evans@Sun.COM 				(void) fprintf(stderr,
13610792SRod.Evans@Sun.COM 				    MSG_ORIG(MSG_FMT_VERBOSE), prog, modestr,
13710792SRod.Evans@Sun.COM 				    trim_msg(dlerror()));
13810792SRod.Evans@Sun.COM 				(void) fflush(stderr);
139*11827SRod.Evans@Sun.COM 			}
14010792SRod.Evans@Sun.COM 			return (1);
14110792SRod.Evans@Sun.COM 		}
14210792SRod.Evans@Sun.COM 
1430Sstevel@tonic-gate 		if (verbose)
1440Sstevel@tonic-gate 			(void) printf(MSG_ORIG(MSG_FMT_VERBOSE), prog, modestr,
1450Sstevel@tonic-gate 			    lmp->l_name);
1460Sstevel@tonic-gate 		else
1470Sstevel@tonic-gate 			(void) printf(MSG_ORIG(MSG_FMT_SIMPLE), modestr,
1480Sstevel@tonic-gate 			    lmp->l_name);
1490Sstevel@tonic-gate 		(void) fflush(stdout);
1500Sstevel@tonic-gate 	}
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate 	(void) dlclose(handle);
1530Sstevel@tonic-gate 	return (0);
1540Sstevel@tonic-gate }
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate int
1570Sstevel@tonic-gate /* ARGSUSED2 */
main(int argc,char ** argv,char ** envp)1580Sstevel@tonic-gate main(int argc, char **argv, char **envp)
1590Sstevel@tonic-gate {
1600Sstevel@tonic-gate 	int	var, verbose = 0, silent = 0, error = 0, mode = 0, class = 0;
1610Sstevel@tonic-gate 	char	*prog;
1620Sstevel@tonic-gate 
1630Sstevel@tonic-gate 	if ((prog = strrchr(argv[0], '/')) == 0)
1640Sstevel@tonic-gate 		prog = argv[0];
1650Sstevel@tonic-gate 	else
1660Sstevel@tonic-gate 		prog++;
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate 	opterr = 0;
1690Sstevel@tonic-gate 	while ((var = getopt(argc, argv, MSG_ORIG(MSG_STR_OPTIONS))) != EOF) {
1700Sstevel@tonic-gate 		switch (var) {
1710Sstevel@tonic-gate 		case 'c':
1720Sstevel@tonic-gate 			class++;
1730Sstevel@tonic-gate 			break;
1740Sstevel@tonic-gate 		case '3':
1750Sstevel@tonic-gate #if	!defined(_LP64)
1760Sstevel@tonic-gate 			if ((optarg[0] == '2') && (mode == 0))
1770Sstevel@tonic-gate 				mode = ONLY32;
1780Sstevel@tonic-gate 			else
1790Sstevel@tonic-gate #endif
1800Sstevel@tonic-gate 				error++;
1810Sstevel@tonic-gate 			break;
1820Sstevel@tonic-gate 		case '6':
1830Sstevel@tonic-gate 			if ((optarg[0] == '4') && (mode == 0))
1840Sstevel@tonic-gate 				mode = ONLY64;
1850Sstevel@tonic-gate 			else
1860Sstevel@tonic-gate 				error++;
1870Sstevel@tonic-gate 			break;
1880Sstevel@tonic-gate 		case 's':
1890Sstevel@tonic-gate 			if (verbose == 0)
1900Sstevel@tonic-gate 				silent++;
1910Sstevel@tonic-gate 			else
1920Sstevel@tonic-gate 				error++;
1930Sstevel@tonic-gate 			break;
1940Sstevel@tonic-gate 		case 'v':
1950Sstevel@tonic-gate 			if (silent == 0)
1960Sstevel@tonic-gate 				verbose++;
1970Sstevel@tonic-gate 			else
1980Sstevel@tonic-gate 				error++;
1990Sstevel@tonic-gate 			break;
2000Sstevel@tonic-gate 		case '?':
2010Sstevel@tonic-gate 			error++;
2020Sstevel@tonic-gate 			break;
2030Sstevel@tonic-gate 		default:
2040Sstevel@tonic-gate 			break;
2050Sstevel@tonic-gate 		}
2060Sstevel@tonic-gate 	}
2070Sstevel@tonic-gate 	if (error || ((argc - optind) == 0)) {
2080Sstevel@tonic-gate 		locale();
2090Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ARG_USAGE), prog);
2100Sstevel@tonic-gate 		return (1);
2110Sstevel@tonic-gate 	}
2120Sstevel@tonic-gate 	if (silent)
2130Sstevel@tonic-gate 		class = 0;
2140Sstevel@tonic-gate 
2150Sstevel@tonic-gate 	/*
2160Sstevel@tonic-gate 	 * Process any 32-bit expansion.
2170Sstevel@tonic-gate 	 */
2180Sstevel@tonic-gate #if	!defined(_LP64)
2190Sstevel@tonic-gate 	if (mode != ONLY64) {
2200Sstevel@tonic-gate #endif
22110792SRod.Evans@Sun.COM 		if (openlib(prog, argv[optind], class, silent, verbose) != 0) {
2220Sstevel@tonic-gate 			if (mode)
2230Sstevel@tonic-gate 				error++;
22410792SRod.Evans@Sun.COM 		}
2250Sstevel@tonic-gate #if	!defined(_LP64)
2260Sstevel@tonic-gate 	}
2270Sstevel@tonic-gate #endif
2280Sstevel@tonic-gate 	if (mode == ONLY32)
22910792SRod.Evans@Sun.COM 		return (error);
2300Sstevel@tonic-gate 
2310Sstevel@tonic-gate 	/*
2320Sstevel@tonic-gate 	 * Re-exec ourselves to process any 64-bit expansion.
2330Sstevel@tonic-gate 	 */
2340Sstevel@tonic-gate #if	!defined(__sparcv9) && !defined(__amd64)
2352647Srie 	(void) conv_check_native(argv, envp);
2360Sstevel@tonic-gate #endif
2370Sstevel@tonic-gate 	return (error);
2380Sstevel@tonic-gate }
239