xref: /titanic_41/usr/src/cmd/isaexec/isaexec.c (revision 03831d35f7499c87d51205817c93e9a8d42c4bae)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <unistd.h>
32 #include <string.h>
33 #include <limits.h>
34 #include <locale.h>
35 #include <sys/types.h>
36 #include <sys/systeminfo.h>
37 
38 /*ARGSUSED*/
39 int
40 main(int argc, char **argv, char **envp)
41 {
42 	const char *execname;
43 	const char *fname;
44 	char *isalist;
45 	ssize_t isalen;
46 	char *pathname;
47 	ssize_t len;
48 	char scratch[1];
49 	char *str;
50 
51 #if !defined(TEXT_DOMAIN)		/* Should be defined by cc -D */
52 #define	TEXT_DOMAIN	"SYS_TEST"	/* Use this only if it wasn't */
53 #endif
54 	(void) setlocale(LC_ALL, "");
55 	(void) textdomain(TEXT_DOMAIN);
56 
57 	/*
58 	 * Get the isa list.
59 	 */
60 	if ((isalen = sysinfo(SI_ISALIST, scratch, 1)) == -1 ||
61 	    (isalist = malloc(isalen)) == NULL ||
62 	    sysinfo(SI_ISALIST, isalist, isalen) == -1) {
63 		(void) fprintf(stderr,
64 		    gettext("%s: cannot find the ISA list\n"),
65 		    argv[0]);
66 		return (1);
67 	}
68 
69 	/*
70 	 * Get the exec name.
71 	 */
72 	if ((execname = getexecname()) == NULL) {
73 		(void) fprintf(stderr,
74 		    gettext("%s: getexecname() failed\n"),
75 		    argv[0]);
76 		return (1);
77 	}
78 
79 	/*
80 	 * Allocate a path name buffer.  The sum of the lengths of the
81 	 * execname and isalist strings is guaranteed to be big enough.
82 	 */
83 	len = strlen(execname) + isalen;
84 	if ((pathname = malloc(len)) == NULL) {
85 		(void) fprintf(stderr,
86 		    gettext("%s: malloc(%d) failed\n"),
87 		    argv[0], (int)len);
88 		return (1);
89 	}
90 
91 	/*
92 	 * Break the exec name into directory and file name components.
93 	 */
94 	(void) strcpy(pathname, execname);
95 	if ((str = strrchr(pathname, '/')) != NULL) {
96 		*++str = '\0';
97 		fname = execname + (str - pathname);
98 	} else {
99 		fname = execname;
100 		*pathname = '\0';
101 	}
102 	len = strlen(pathname);
103 
104 	/*
105 	 * For each name in the isa list, look for an executable file
106 	 * with the given file name in the corresponding subdirectory.
107 	 */
108 	str = strtok(isalist, " ");
109 	do {
110 		(void) strcpy(pathname+len, str);
111 		(void) strcat(pathname+len, "/");
112 		(void) strcat(pathname+len, fname);
113 		if (access(pathname, X_OK) == 0) {
114 			/*
115 			 * File exists and is marked executable.  Attempt
116 			 * to execute the file from the subdirectory,
117 			 * using the user-supplied argv and envp.
118 			 */
119 			(void) execve(pathname, argv, envp);
120 			(void) fprintf(stderr,
121 			    gettext("%s: execve(\"%s\") failed\n"),
122 			    argv[0], pathname);
123 		}
124 	} while ((str = strtok(NULL, " ")) != NULL);
125 
126 	(void) fprintf(stderr,
127 	    gettext("%s: cannot find/execute \"%s\" in ISA subdirectories\n"),
128 	    argv[0], fname);
129 
130 	return (1);
131 }
132