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 (the "License").
6 * You may not use this file except in compliance with the License.
7 *
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.
12 *
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]
18 *
19 * CDDL HEADER END
20 */
21
22 /*
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
25 */
26
27 /*
28 * Copyright 2019 OmniOS Community Edition (OmniOSce) Association.
29 */
30
31 #pragma weak _isaexec = isaexec
32
33 #include "lint.h"
34 #include <sys/types.h>
35 #include <sys/systeminfo.h>
36 #include <unistd.h>
37 #include <string.h>
38 #include <errno.h>
39 #include <limits.h>
40 #include <stdarg.h>
41 #include <stdlib.h>
42
43 /*
44 * This is a utility routine to allow wrapper programs to simply
45 * implement the isalist exec algorithms. See PSARC/1997/220.
46 */
47 int
isaexec(const char * execname,char * const * argv,char * const * envp)48 isaexec(const char *execname, char *const *argv, char *const *envp)
49 {
50 const char *fname;
51 char *isalist;
52 char *pathname;
53 char *str;
54 char *lasts;
55 size_t isalen = 255; /* wild guess */
56 size_t len;
57 int saved_errno;
58
59 /*
60 * Extract the isalist(7) for userland from the kernel.
61 */
62 isalist = malloc(isalen);
63 do {
64 long ret = sysinfo(SI_ISALIST, isalist, isalen);
65 if (ret == -1l) {
66 free(isalist);
67 errno = ENOENT;
68 return (-1);
69 }
70 if (ret > isalen) {
71 isalen = ret;
72 isalist = reallocf(isalist, isalen);
73 } else {
74 break;
75 }
76 } while (isalist != NULL);
77
78 if (isalist == NULL) {
79 /*
80 * Then either a malloc or a realloc failed.
81 */
82 errno = EAGAIN;
83 return (-1);
84 }
85
86 /*
87 * Allocate a full pathname buffer. The sum of the lengths of the
88 * 'path' and isalist strings is guaranteed to be big enough.
89 */
90 len = strlen(execname) + isalen;
91 if ((pathname = malloc(len)) == NULL) {
92 free(isalist);
93 errno = EAGAIN;
94 return (-1);
95 }
96
97 /*
98 * Break the exec name into directory and file name components.
99 */
100 (void) strcpy(pathname, execname);
101 if ((str = strrchr(pathname, '/')) != NULL) {
102 *++str = '\0';
103 fname = execname + (str - pathname);
104 } else {
105 fname = execname;
106 *pathname = '\0';
107 }
108 len = strlen(pathname);
109
110 /*
111 * For each name in the isa list, look for an executable file
112 * with the given file name in the corresponding subdirectory.
113 * If it's there, exec it. If it's not there, or the exec
114 * fails, then run the next version ..
115 */
116 str = strtok_r(isalist, " ", &lasts);
117 saved_errno = ENOENT;
118 do {
119 (void) strcpy(pathname + len, str);
120 (void) strcat(pathname + len, "/");
121 (void) strcat(pathname + len, fname);
122 if (access(pathname, X_OK) == 0) {
123 /*
124 * File exists and is marked executable. Attempt
125 * to execute the file from the subdirectory,
126 * using the user-supplied argv and envp.
127 */
128 (void) execve(pathname, argv, envp);
129
130 /*
131 * If we failed to exec because of a temporary
132 * resource shortage, it's better to let our
133 * caller handle it (free memory, sleep for a while,
134 * or whatever before retrying) rather than drive
135 * on to run the "less capable" version.
136 */
137 if (errno == EAGAIN) {
138 saved_errno = errno;
139 break;
140 }
141 }
142 } while ((str = strtok_r(NULL, " ", &lasts)) != NULL);
143
144 free(pathname);
145 free(isalist);
146
147 errno = saved_errno;
148 return (-1);
149 }
150