4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
29 #pragma weak _isaexec = isaexec
32 #include <sys/types.h>
33 #include <sys/systeminfo.h>
42 * This is a utility routine to allow wrapper programs to simply
43 * implement the isalist exec algorithms. See PSARC/1997/220.
46 isaexec(const char *execname
, char *const *argv
, char *const *envp
)
53 size_t isalen
= 255; /* wild guess */
58 * Extract the isalist(5) for userland from the kernel.
60 isalist
= malloc(isalen
);
62 long ret
= sysinfo(SI_ISALIST
, isalist
, isalen
);
70 isalist
= realloc(isalist
, isalen
);
73 } while (isalist
!= NULL
);
75 if (isalist
== NULL
) {
77 * Then either a malloc or a realloc failed.
84 * Allocate a full pathname buffer. The sum of the lengths of the
85 * 'path' and isalist strings is guaranteed to be big enough.
87 len
= strlen(execname
) + isalen
;
88 if ((pathname
= malloc(len
)) == NULL
) {
95 * Break the exec name into directory and file name components.
97 (void) strcpy(pathname
, execname
);
98 if ((str
= strrchr(pathname
, '/')) != NULL
) {
100 fname
= execname
+ (str
- pathname
);
105 len
= strlen(pathname
);
108 * For each name in the isa list, look for an executable file
109 * with the given file name in the corresponding subdirectory.
110 * If it's there, exec it. If it's not there, or the exec
111 * fails, then run the next version ..
113 str
= strtok_r(isalist
, " ", &lasts
);
114 saved_errno
= ENOENT
;
116 (void) strcpy(pathname
+ len
, str
);
117 (void) strcat(pathname
+ len
, "/");
118 (void) strcat(pathname
+ len
, fname
);
119 if (access(pathname
, X_OK
) == 0) {
121 * File exists and is marked executable. Attempt
122 * to execute the file from the subdirectory,
123 * using the user-supplied argv and envp.
125 (void) execve(pathname
, argv
, envp
);
128 * If we failed to exec because of a temporary
129 * resource shortage, it's better to let our
130 * caller handle it (free memory, sleep for a while,
131 * or whatever before retrying) rather than drive
132 * on to run the "less capable" version.
134 if (errno
== EAGAIN
) {
139 } while ((str
= strtok_r(NULL
, " ", &lasts
)) != NULL
);