dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / lib / libc / port / gen / isaexec.c
blob9939b695f6858af9d91d83f4032dca593e2f76ed
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
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
31 #include "lint.h"
32 #include <sys/types.h>
33 #include <sys/systeminfo.h>
34 #include <unistd.h>
35 #include <string.h>
36 #include <errno.h>
37 #include <limits.h>
38 #include <stdarg.h>
39 #include <stdlib.h>
42 * This is a utility routine to allow wrapper programs to simply
43 * implement the isalist exec algorithms. See PSARC/1997/220.
45 int
46 isaexec(const char *execname, char *const *argv, char *const *envp)
48 const char *fname;
49 char *isalist;
50 char *pathname;
51 char *str;
52 char *lasts;
53 size_t isalen = 255; /* wild guess */
54 size_t len;
55 int saved_errno;
58 * Extract the isalist(5) for userland from the kernel.
60 isalist = malloc(isalen);
61 do {
62 long ret = sysinfo(SI_ISALIST, isalist, isalen);
63 if (ret == -1l) {
64 free(isalist);
65 errno = ENOENT;
66 return (-1);
68 if (ret > isalen) {
69 isalen = ret;
70 isalist = realloc(isalist, isalen);
71 } else
72 break;
73 } while (isalist != NULL);
75 if (isalist == NULL) {
77 * Then either a malloc or a realloc failed.
79 errno = EAGAIN;
80 return (-1);
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) {
89 free(isalist);
90 errno = EAGAIN;
91 return (-1);
95 * Break the exec name into directory and file name components.
97 (void) strcpy(pathname, execname);
98 if ((str = strrchr(pathname, '/')) != NULL) {
99 *++str = '\0';
100 fname = execname + (str - pathname);
101 } else {
102 fname = execname;
103 *pathname = '\0';
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;
115 do {
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) {
135 saved_errno = errno;
136 break;
139 } while ((str = strtok_r(NULL, " ", &lasts)) != NULL);
141 free(pathname);
142 free(isalist);
144 errno = saved_errno;
145 return (-1);