xref: /titanic_41/usr/src/cmd/sgs/moe/common/moe.c (revision 08278a5e91755ccdb5850c19d21d42fb2e16b50e)
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
57010c12aSrie  * Common Development and Distribution License (the "License").
67010c12aSrie  * 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  */
217010c12aSrie 
227c478bd9Sstevel@tonic-gate /*
23*08278a5eSRod Evans  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <dlfcn.h>
287c478bd9Sstevel@tonic-gate #include <link.h>
297c478bd9Sstevel@tonic-gate #include <stdio.h>
307c478bd9Sstevel@tonic-gate #include <string.h>
317c478bd9Sstevel@tonic-gate #include <stdarg.h>
327c478bd9Sstevel@tonic-gate #include <libintl.h>
337c478bd9Sstevel@tonic-gate #include <locale.h>
347c478bd9Sstevel@tonic-gate #include <conv.h>
357c478bd9Sstevel@tonic-gate #include <msg.h>
367c478bd9Sstevel@tonic-gate 
377c478bd9Sstevel@tonic-gate void
locale()387c478bd9Sstevel@tonic-gate locale()
397c478bd9Sstevel@tonic-gate {
407c478bd9Sstevel@tonic-gate 	static int	localeinit = 0;
417c478bd9Sstevel@tonic-gate 
427c478bd9Sstevel@tonic-gate 	/*
437c478bd9Sstevel@tonic-gate 	 * Defer localization overhead until a localized message, is required.
447c478bd9Sstevel@tonic-gate 	 * For successful specific (32-bit or 64-bit) commands, none of this
457c478bd9Sstevel@tonic-gate 	 * overhead should be incurred.
467c478bd9Sstevel@tonic-gate 	 */
477c478bd9Sstevel@tonic-gate 	if (localeinit++)
487c478bd9Sstevel@tonic-gate 		return;
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_MESSAGES, MSG_ORIG(MSG_STR_EMPTY));
517c478bd9Sstevel@tonic-gate 	(void) textdomain(MSG_ORIG(MSG_SUNW_OST_SGS));
527c478bd9Sstevel@tonic-gate }
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate const char *
_moe_msg(Msg mid)557c478bd9Sstevel@tonic-gate _moe_msg(Msg mid)
567c478bd9Sstevel@tonic-gate {
577c478bd9Sstevel@tonic-gate 	return (gettext(MSG_ORIG(mid)));
587c478bd9Sstevel@tonic-gate }
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /*
617c478bd9Sstevel@tonic-gate  * Error messages from ld.so.1 are formatted as:
627c478bd9Sstevel@tonic-gate  *
637c478bd9Sstevel@tonic-gate  *	ld.so.1: app-name: fatal: ....
647c478bd9Sstevel@tonic-gate  *
657c478bd9Sstevel@tonic-gate  * Here, we skip over these three components.  Thus the message a little less
667c478bd9Sstevel@tonic-gate  * hostile when displayed by moe().  Really, it would be nice to have some
677c478bd9Sstevel@tonic-gate  * flexibility over what ld.so.1 displays.
687c478bd9Sstevel@tonic-gate  */
697c478bd9Sstevel@tonic-gate static char *
trim_msg(char * str)707c478bd9Sstevel@tonic-gate trim_msg(char *str)
717c478bd9Sstevel@tonic-gate {
727c478bd9Sstevel@tonic-gate 	char	*ptr = str;
737c478bd9Sstevel@tonic-gate 	int	cnt = 0;
747c478bd9Sstevel@tonic-gate 
757c478bd9Sstevel@tonic-gate 	/*
767c478bd9Sstevel@tonic-gate 	 * Skip the first three components.
777c478bd9Sstevel@tonic-gate 	 */
787c478bd9Sstevel@tonic-gate 	while (*ptr) {
797c478bd9Sstevel@tonic-gate 		if (*ptr == ':') {
807c478bd9Sstevel@tonic-gate 			if (++cnt == 3)
817c478bd9Sstevel@tonic-gate 				break;
827c478bd9Sstevel@tonic-gate 		}
837c478bd9Sstevel@tonic-gate 		ptr++;
847c478bd9Sstevel@tonic-gate 	}
857c478bd9Sstevel@tonic-gate 	if (*ptr == '\0')
867c478bd9Sstevel@tonic-gate 		return (str);
877c478bd9Sstevel@tonic-gate 	else
887c478bd9Sstevel@tonic-gate 		return (ptr + 2);
897c478bd9Sstevel@tonic-gate }
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate #define	ONLY32	1
927c478bd9Sstevel@tonic-gate #define	ONLY64	2
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate static int
openlib(const char * prog,const char * name,int class,int silent,int verbose)957c478bd9Sstevel@tonic-gate openlib(const char *prog, const char *name, int class, int silent, int verbose)
967c478bd9Sstevel@tonic-gate {
977c478bd9Sstevel@tonic-gate 	void		*handle;
987c478bd9Sstevel@tonic-gate 	const char	*modestr;
997c478bd9Sstevel@tonic-gate 
1007c478bd9Sstevel@tonic-gate 	/*
1017c478bd9Sstevel@tonic-gate 	 * If the class of object is required, localize the prefix message.
1027c478bd9Sstevel@tonic-gate 	 */
1037c478bd9Sstevel@tonic-gate 	if (class) {
1047c478bd9Sstevel@tonic-gate 		locale();
1057c478bd9Sstevel@tonic-gate #if	defined(_LP64)
1067c478bd9Sstevel@tonic-gate 		modestr = MSG_INTL(MSG_PRE_64);
1077c478bd9Sstevel@tonic-gate #else
1087c478bd9Sstevel@tonic-gate 		modestr = MSG_INTL(MSG_PRE_32);
1097c478bd9Sstevel@tonic-gate #endif
1107c478bd9Sstevel@tonic-gate 	} else
1117c478bd9Sstevel@tonic-gate 		modestr = MSG_ORIG(MSG_STR_EMPTY);
1127c478bd9Sstevel@tonic-gate 
1137c478bd9Sstevel@tonic-gate 
1147c478bd9Sstevel@tonic-gate 	/*
1157c478bd9Sstevel@tonic-gate 	 * Open the optimal object, and determine its full name from the
1167c478bd9Sstevel@tonic-gate 	 * returned handle.  Borrow the internal mode, RTLD_CONFGEN, from
1177c478bd9Sstevel@tonic-gate 	 * crle(1).  This flag allows us to process incomplete objects, as
1187c478bd9Sstevel@tonic-gate 	 * would occur if the object couldn't find its dependencies or relocate
1197c478bd9Sstevel@tonic-gate 	 * itself.
1207c478bd9Sstevel@tonic-gate 	 */
1217c478bd9Sstevel@tonic-gate 	if ((handle = dlmopen(LM_ID_NEWLM, name,
1227c478bd9Sstevel@tonic-gate 	    (RTLD_FIRST | RTLD_CONFGEN | RTLD_LAZY))) == 0) {
123*08278a5eSRod Evans 		if (verbose) {
1247c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_ORIG(MSG_FMT_VERBOSE), prog,
1257c478bd9Sstevel@tonic-gate 			    modestr, trim_msg(dlerror()));
1267c478bd9Sstevel@tonic-gate 			(void) fflush(stderr);
127*08278a5eSRod Evans 		}
1287c478bd9Sstevel@tonic-gate 		return (1);
1297c478bd9Sstevel@tonic-gate 	}
130635216b6SRod Evans 	if (silent == 0) {
131635216b6SRod Evans 		Link_map	*lmp;
132635216b6SRod Evans 
1337c478bd9Sstevel@tonic-gate 		if (dlinfo(handle, RTLD_DI_LINKMAP, &lmp) == -1) {
134*08278a5eSRod Evans 			if (verbose) {
135635216b6SRod Evans 				(void) fprintf(stderr,
136635216b6SRod Evans 				    MSG_ORIG(MSG_FMT_VERBOSE), prog, modestr,
137635216b6SRod Evans 				    trim_msg(dlerror()));
1387c478bd9Sstevel@tonic-gate 				(void) fflush(stderr);
139*08278a5eSRod Evans 			}
1407c478bd9Sstevel@tonic-gate 			return (1);
1417c478bd9Sstevel@tonic-gate 		}
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 		if (verbose)
1447c478bd9Sstevel@tonic-gate 			(void) printf(MSG_ORIG(MSG_FMT_VERBOSE), prog, modestr,
1457c478bd9Sstevel@tonic-gate 			    lmp->l_name);
1467c478bd9Sstevel@tonic-gate 		else
1477c478bd9Sstevel@tonic-gate 			(void) printf(MSG_ORIG(MSG_FMT_SIMPLE), modestr,
1487c478bd9Sstevel@tonic-gate 			    lmp->l_name);
1497c478bd9Sstevel@tonic-gate 		(void) fflush(stdout);
1507c478bd9Sstevel@tonic-gate 	}
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate 	(void) dlclose(handle);
1537c478bd9Sstevel@tonic-gate 	return (0);
1547c478bd9Sstevel@tonic-gate }
1557c478bd9Sstevel@tonic-gate 
1567c478bd9Sstevel@tonic-gate int
1577c478bd9Sstevel@tonic-gate /* ARGSUSED2 */
main(int argc,char ** argv,char ** envp)1587c478bd9Sstevel@tonic-gate main(int argc, char **argv, char **envp)
1597c478bd9Sstevel@tonic-gate {
1607c478bd9Sstevel@tonic-gate 	int	var, verbose = 0, silent = 0, error = 0, mode = 0, class = 0;
1617c478bd9Sstevel@tonic-gate 	char	*prog;
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	if ((prog = strrchr(argv[0], '/')) == 0)
1647c478bd9Sstevel@tonic-gate 		prog = argv[0];
1657c478bd9Sstevel@tonic-gate 	else
1667c478bd9Sstevel@tonic-gate 		prog++;
1677c478bd9Sstevel@tonic-gate 
1687c478bd9Sstevel@tonic-gate 	opterr = 0;
1697c478bd9Sstevel@tonic-gate 	while ((var = getopt(argc, argv, MSG_ORIG(MSG_STR_OPTIONS))) != EOF) {
1707c478bd9Sstevel@tonic-gate 		switch (var) {
1717c478bd9Sstevel@tonic-gate 		case 'c':
1727c478bd9Sstevel@tonic-gate 			class++;
1737c478bd9Sstevel@tonic-gate 			break;
1747c478bd9Sstevel@tonic-gate 		case '3':
1757c478bd9Sstevel@tonic-gate #if	!defined(_LP64)
1767c478bd9Sstevel@tonic-gate 			if ((optarg[0] == '2') && (mode == 0))
1777c478bd9Sstevel@tonic-gate 				mode = ONLY32;
1787c478bd9Sstevel@tonic-gate 			else
1797c478bd9Sstevel@tonic-gate #endif
1807c478bd9Sstevel@tonic-gate 				error++;
1817c478bd9Sstevel@tonic-gate 			break;
1827c478bd9Sstevel@tonic-gate 		case '6':
1837c478bd9Sstevel@tonic-gate 			if ((optarg[0] == '4') && (mode == 0))
1847c478bd9Sstevel@tonic-gate 				mode = ONLY64;
1857c478bd9Sstevel@tonic-gate 			else
1867c478bd9Sstevel@tonic-gate 				error++;
1877c478bd9Sstevel@tonic-gate 			break;
1887c478bd9Sstevel@tonic-gate 		case 's':
1897c478bd9Sstevel@tonic-gate 			if (verbose == 0)
1907c478bd9Sstevel@tonic-gate 				silent++;
1917c478bd9Sstevel@tonic-gate 			else
1927c478bd9Sstevel@tonic-gate 				error++;
1937c478bd9Sstevel@tonic-gate 			break;
1947c478bd9Sstevel@tonic-gate 		case 'v':
1957c478bd9Sstevel@tonic-gate 			if (silent == 0)
1967c478bd9Sstevel@tonic-gate 				verbose++;
1977c478bd9Sstevel@tonic-gate 			else
1987c478bd9Sstevel@tonic-gate 				error++;
1997c478bd9Sstevel@tonic-gate 			break;
2007c478bd9Sstevel@tonic-gate 		case '?':
2017c478bd9Sstevel@tonic-gate 			error++;
2027c478bd9Sstevel@tonic-gate 			break;
2037c478bd9Sstevel@tonic-gate 		default:
2047c478bd9Sstevel@tonic-gate 			break;
2057c478bd9Sstevel@tonic-gate 		}
2067c478bd9Sstevel@tonic-gate 	}
2077c478bd9Sstevel@tonic-gate 	if (error || ((argc - optind) == 0)) {
2087c478bd9Sstevel@tonic-gate 		locale();
2097c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ARG_USAGE), prog);
2107c478bd9Sstevel@tonic-gate 		return (1);
2117c478bd9Sstevel@tonic-gate 	}
2127c478bd9Sstevel@tonic-gate 	if (silent)
2137c478bd9Sstevel@tonic-gate 		class = 0;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 	/*
2167c478bd9Sstevel@tonic-gate 	 * Process any 32-bit expansion.
2177c478bd9Sstevel@tonic-gate 	 */
2187c478bd9Sstevel@tonic-gate #if	!defined(_LP64)
2197c478bd9Sstevel@tonic-gate 	if (mode != ONLY64) {
2207c478bd9Sstevel@tonic-gate #endif
221635216b6SRod Evans 		if (openlib(prog, argv[optind], class, silent, verbose) != 0) {
2227c478bd9Sstevel@tonic-gate 			if (mode)
2237c478bd9Sstevel@tonic-gate 				error++;
224635216b6SRod Evans 		}
2257c478bd9Sstevel@tonic-gate #if	!defined(_LP64)
2267c478bd9Sstevel@tonic-gate 	}
2277c478bd9Sstevel@tonic-gate #endif
2287c478bd9Sstevel@tonic-gate 	if (mode == ONLY32)
229635216b6SRod Evans 		return (error);
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	/*
2327c478bd9Sstevel@tonic-gate 	 * Re-exec ourselves to process any 64-bit expansion.
2337c478bd9Sstevel@tonic-gate 	 */
2347c478bd9Sstevel@tonic-gate #if	!defined(__sparcv9) && !defined(__amd64)
2357010c12aSrie 	(void) conv_check_native(argv, envp);
2367c478bd9Sstevel@tonic-gate #endif
2377c478bd9Sstevel@tonic-gate 	return (error);
2387c478bd9Sstevel@tonic-gate }
239