xref: /onnv-gate/usr/src/cmd/sgs/moe/common/moe.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
27*0Sstevel@tonic-gate 
28*0Sstevel@tonic-gate #include <dlfcn.h>
29*0Sstevel@tonic-gate #include <link.h>
30*0Sstevel@tonic-gate #include <stdio.h>
31*0Sstevel@tonic-gate #include <string.h>
32*0Sstevel@tonic-gate #include <stdarg.h>
33*0Sstevel@tonic-gate #include <libintl.h>
34*0Sstevel@tonic-gate #include <locale.h>
35*0Sstevel@tonic-gate #include <conv.h>
36*0Sstevel@tonic-gate #include <msg.h>
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate void
39*0Sstevel@tonic-gate locale()
40*0Sstevel@tonic-gate {
41*0Sstevel@tonic-gate 	static int	localeinit = 0;
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate 	/*
44*0Sstevel@tonic-gate 	 * Defer localization overhead until a localized message, is required.
45*0Sstevel@tonic-gate 	 * For successful specific (32-bit or 64-bit) commands, none of this
46*0Sstevel@tonic-gate 	 * overhead should be incurred.
47*0Sstevel@tonic-gate 	 */
48*0Sstevel@tonic-gate 	if (localeinit++)
49*0Sstevel@tonic-gate 		return;
50*0Sstevel@tonic-gate 
51*0Sstevel@tonic-gate 	(void) setlocale(LC_MESSAGES, MSG_ORIG(MSG_STR_EMPTY));
52*0Sstevel@tonic-gate 	(void) textdomain(MSG_ORIG(MSG_SUNW_OST_SGS));
53*0Sstevel@tonic-gate }
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate const char *
56*0Sstevel@tonic-gate _moe_msg(Msg mid)
57*0Sstevel@tonic-gate {
58*0Sstevel@tonic-gate 	return (gettext(MSG_ORIG(mid)));
59*0Sstevel@tonic-gate }
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate /*
62*0Sstevel@tonic-gate  * Error messages from ld.so.1 are formatted as:
63*0Sstevel@tonic-gate  *
64*0Sstevel@tonic-gate  *	ld.so.1: app-name: fatal: ....
65*0Sstevel@tonic-gate  *
66*0Sstevel@tonic-gate  * Here, we skip over these three components.  Thus the message a little less
67*0Sstevel@tonic-gate  * hostile when displayed by moe().  Really, it would be nice to have some
68*0Sstevel@tonic-gate  * flexibility over what ld.so.1 displays.
69*0Sstevel@tonic-gate  */
70*0Sstevel@tonic-gate static char *
71*0Sstevel@tonic-gate trim_msg(char *str)
72*0Sstevel@tonic-gate {
73*0Sstevel@tonic-gate 	char	*ptr = str;
74*0Sstevel@tonic-gate 	int	cnt = 0;
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate 	/*
77*0Sstevel@tonic-gate 	 * Skip the first three components.
78*0Sstevel@tonic-gate 	 */
79*0Sstevel@tonic-gate 	while (*ptr) {
80*0Sstevel@tonic-gate 		if (*ptr == ':') {
81*0Sstevel@tonic-gate 			if (++cnt == 3)
82*0Sstevel@tonic-gate 				break;
83*0Sstevel@tonic-gate 		}
84*0Sstevel@tonic-gate 		ptr++;
85*0Sstevel@tonic-gate 	}
86*0Sstevel@tonic-gate 	if (*ptr == '\0')
87*0Sstevel@tonic-gate 		return (str);
88*0Sstevel@tonic-gate 	else
89*0Sstevel@tonic-gate 		return (ptr + 2);
90*0Sstevel@tonic-gate }
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate #define	ONLY32	1
93*0Sstevel@tonic-gate #define	ONLY64	2
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate static int
96*0Sstevel@tonic-gate openlib(const char *prog, const char *name, int class, int silent, int verbose)
97*0Sstevel@tonic-gate {
98*0Sstevel@tonic-gate 	Link_map	*lmp;
99*0Sstevel@tonic-gate 	void		*handle;
100*0Sstevel@tonic-gate 	const char	*modestr;
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate 	/*
103*0Sstevel@tonic-gate 	 * If the class of object is required, localize the prefix message.
104*0Sstevel@tonic-gate 	 */
105*0Sstevel@tonic-gate 	if (class) {
106*0Sstevel@tonic-gate 		locale();
107*0Sstevel@tonic-gate #if	defined(_LP64)
108*0Sstevel@tonic-gate 		modestr = MSG_INTL(MSG_PRE_64);
109*0Sstevel@tonic-gate #else
110*0Sstevel@tonic-gate 		modestr = MSG_INTL(MSG_PRE_32);
111*0Sstevel@tonic-gate #endif
112*0Sstevel@tonic-gate 	} else
113*0Sstevel@tonic-gate 		modestr = MSG_ORIG(MSG_STR_EMPTY);
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 	/*
117*0Sstevel@tonic-gate 	 * Open the optimal object, and determine its full name from the
118*0Sstevel@tonic-gate 	 * returned handle.  Borrow the internal mode, RTLD_CONFGEN, from
119*0Sstevel@tonic-gate 	 * crle(1).  This flag allows us to process incomplete objects, as
120*0Sstevel@tonic-gate 	 * would occur if the object couldn't find its dependencies or relocate
121*0Sstevel@tonic-gate 	 * itself.
122*0Sstevel@tonic-gate 	 */
123*0Sstevel@tonic-gate 	if ((handle = dlmopen(LM_ID_NEWLM, name,
124*0Sstevel@tonic-gate 	    (RTLD_FIRST | RTLD_CONFGEN | RTLD_LAZY))) == 0) {
125*0Sstevel@tonic-gate 		if (verbose)
126*0Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_ORIG(MSG_FMT_VERBOSE), prog,
127*0Sstevel@tonic-gate 			    modestr, trim_msg(dlerror()));
128*0Sstevel@tonic-gate 			(void) fflush(stderr);
129*0Sstevel@tonic-gate 		return (1);
130*0Sstevel@tonic-gate 	}
131*0Sstevel@tonic-gate 	if (dlinfo(handle, RTLD_DI_LINKMAP, &lmp) == -1) {
132*0Sstevel@tonic-gate 		if (verbose)
133*0Sstevel@tonic-gate 			(void) fprintf(stderr, MSG_ORIG(MSG_FMT_VERBOSE), prog,
134*0Sstevel@tonic-gate 			    modestr, trim_msg(dlerror()));
135*0Sstevel@tonic-gate 			(void) fflush(stderr);
136*0Sstevel@tonic-gate 		return (1);
137*0Sstevel@tonic-gate 	}
138*0Sstevel@tonic-gate 
139*0Sstevel@tonic-gate 	if (silent == 0) {
140*0Sstevel@tonic-gate 		if (verbose)
141*0Sstevel@tonic-gate 			(void) printf(MSG_ORIG(MSG_FMT_VERBOSE), prog, modestr,
142*0Sstevel@tonic-gate 			    lmp->l_name);
143*0Sstevel@tonic-gate 		else
144*0Sstevel@tonic-gate 			(void) printf(MSG_ORIG(MSG_FMT_SIMPLE), modestr,
145*0Sstevel@tonic-gate 			    lmp->l_name);
146*0Sstevel@tonic-gate 		(void) fflush(stdout);
147*0Sstevel@tonic-gate 	}
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate 	(void) dlclose(handle);
150*0Sstevel@tonic-gate 	return (0);
151*0Sstevel@tonic-gate }
152*0Sstevel@tonic-gate 
153*0Sstevel@tonic-gate int
154*0Sstevel@tonic-gate /* ARGSUSED2 */
155*0Sstevel@tonic-gate main(int argc, char **argv, char **envp)
156*0Sstevel@tonic-gate {
157*0Sstevel@tonic-gate 	int	var, verbose = 0, silent = 0, error = 0, mode = 0, class = 0;
158*0Sstevel@tonic-gate 	char	*prog;
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate 	if ((prog = strrchr(argv[0], '/')) == 0)
161*0Sstevel@tonic-gate 		prog = argv[0];
162*0Sstevel@tonic-gate 	else
163*0Sstevel@tonic-gate 		prog++;
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate 	opterr = 0;
166*0Sstevel@tonic-gate 	while ((var = getopt(argc, argv, MSG_ORIG(MSG_STR_OPTIONS))) != EOF) {
167*0Sstevel@tonic-gate 		switch (var) {
168*0Sstevel@tonic-gate 		case 'c':
169*0Sstevel@tonic-gate 			class++;
170*0Sstevel@tonic-gate 			break;
171*0Sstevel@tonic-gate 		case '3':
172*0Sstevel@tonic-gate #if	!defined(_LP64)
173*0Sstevel@tonic-gate 			if ((optarg[0] == '2') && (mode == 0))
174*0Sstevel@tonic-gate 				mode = ONLY32;
175*0Sstevel@tonic-gate 			else
176*0Sstevel@tonic-gate #endif
177*0Sstevel@tonic-gate 				error++;
178*0Sstevel@tonic-gate 			break;
179*0Sstevel@tonic-gate 		case '6':
180*0Sstevel@tonic-gate 			if ((optarg[0] == '4') && (mode == 0))
181*0Sstevel@tonic-gate 				mode = ONLY64;
182*0Sstevel@tonic-gate 			else
183*0Sstevel@tonic-gate 				error++;
184*0Sstevel@tonic-gate 			break;
185*0Sstevel@tonic-gate 		case 's':
186*0Sstevel@tonic-gate 			if (verbose == 0)
187*0Sstevel@tonic-gate 				silent++;
188*0Sstevel@tonic-gate 			else
189*0Sstevel@tonic-gate 				error++;
190*0Sstevel@tonic-gate 			break;
191*0Sstevel@tonic-gate 		case 'v':
192*0Sstevel@tonic-gate 			if (silent == 0)
193*0Sstevel@tonic-gate 				verbose++;
194*0Sstevel@tonic-gate 			else
195*0Sstevel@tonic-gate 				error++;
196*0Sstevel@tonic-gate 			break;
197*0Sstevel@tonic-gate 		case '?':
198*0Sstevel@tonic-gate 			error++;
199*0Sstevel@tonic-gate 			break;
200*0Sstevel@tonic-gate 		default:
201*0Sstevel@tonic-gate 			break;
202*0Sstevel@tonic-gate 		}
203*0Sstevel@tonic-gate 	}
204*0Sstevel@tonic-gate 	if (error || ((argc - optind) == 0)) {
205*0Sstevel@tonic-gate 		locale();
206*0Sstevel@tonic-gate 		(void) fprintf(stderr, MSG_INTL(MSG_ARG_USAGE), prog);
207*0Sstevel@tonic-gate 		return (1);
208*0Sstevel@tonic-gate 	}
209*0Sstevel@tonic-gate 	if (silent)
210*0Sstevel@tonic-gate 		class = 0;
211*0Sstevel@tonic-gate 
212*0Sstevel@tonic-gate 	/*
213*0Sstevel@tonic-gate 	 * Process any 32-bit expansion.
214*0Sstevel@tonic-gate 	 */
215*0Sstevel@tonic-gate #if	!defined(_LP64)
216*0Sstevel@tonic-gate 	if (mode != ONLY64) {
217*0Sstevel@tonic-gate #endif
218*0Sstevel@tonic-gate 		if (openlib(prog, argv[optind], class, silent, verbose) != 0)
219*0Sstevel@tonic-gate 			if (mode)
220*0Sstevel@tonic-gate 				error++;
221*0Sstevel@tonic-gate #if	!defined(_LP64)
222*0Sstevel@tonic-gate 	}
223*0Sstevel@tonic-gate #endif
224*0Sstevel@tonic-gate 	if (mode == ONLY32)
225*0Sstevel@tonic-gate 		return (0);
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate 	/*
228*0Sstevel@tonic-gate 	 * Re-exec ourselves to process any 64-bit expansion.
229*0Sstevel@tonic-gate 	 */
230*0Sstevel@tonic-gate #if	!defined(__sparcv9) && !defined(__amd64)
231*0Sstevel@tonic-gate 	conv_check_native(argv, envp);
232*0Sstevel@tonic-gate #endif
233*0Sstevel@tonic-gate 	return (error);
234*0Sstevel@tonic-gate }
235