xref: /illumos-gate/usr/src/lib/libc/port/gen/isaexec.c (revision bbf215553c7233fbab8a0afdf1fac74c44781867)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57257d1b4Sraf  * Common Development and Distribution License (the "License").
67257d1b4Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217257d1b4Sraf 
227c478bd9Sstevel@tonic-gate /*
237257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27d1704f8eSAndy Fiddaman /*
28d1704f8eSAndy Fiddaman  * Copyright 2019 OmniOS Community Edition (OmniOSce) Association.
29d1704f8eSAndy Fiddaman  */
307c478bd9Sstevel@tonic-gate 
317257d1b4Sraf #pragma weak _isaexec = isaexec
327c478bd9Sstevel@tonic-gate 
337257d1b4Sraf #include "lint.h"
347c478bd9Sstevel@tonic-gate #include <sys/types.h>
357c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
367c478bd9Sstevel@tonic-gate #include <unistd.h>
377c478bd9Sstevel@tonic-gate #include <string.h>
387c478bd9Sstevel@tonic-gate #include <errno.h>
397c478bd9Sstevel@tonic-gate #include <limits.h>
407c478bd9Sstevel@tonic-gate #include <stdarg.h>
417c478bd9Sstevel@tonic-gate #include <stdlib.h>
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /*
447c478bd9Sstevel@tonic-gate  * This is a utility routine to allow wrapper programs to simply
457c478bd9Sstevel@tonic-gate  * implement the isalist exec algorithms.  See PSARC/1997/220.
467c478bd9Sstevel@tonic-gate  */
477c478bd9Sstevel@tonic-gate int
isaexec(const char * execname,char * const * argv,char * const * envp)487c478bd9Sstevel@tonic-gate isaexec(const char *execname, char *const *argv, char *const *envp)
497c478bd9Sstevel@tonic-gate {
507c478bd9Sstevel@tonic-gate 	const char *fname;
517c478bd9Sstevel@tonic-gate 	char *isalist;
527c478bd9Sstevel@tonic-gate 	char *pathname;
537c478bd9Sstevel@tonic-gate 	char *str;
547c478bd9Sstevel@tonic-gate 	char *lasts;
557c478bd9Sstevel@tonic-gate 	size_t isalen = 255;		/* wild guess */
567c478bd9Sstevel@tonic-gate 	size_t len;
577c478bd9Sstevel@tonic-gate 	int saved_errno;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 	/*
60*bbf21555SRichard Lowe 	 * Extract the isalist(7) for userland from the kernel.
617c478bd9Sstevel@tonic-gate 	 */
627c478bd9Sstevel@tonic-gate 	isalist = malloc(isalen);
637c478bd9Sstevel@tonic-gate 	do {
647c478bd9Sstevel@tonic-gate 		long ret = sysinfo(SI_ISALIST, isalist, isalen);
657c478bd9Sstevel@tonic-gate 		if (ret == -1l) {
667c478bd9Sstevel@tonic-gate 			free(isalist);
677c478bd9Sstevel@tonic-gate 			errno = ENOENT;
687c478bd9Sstevel@tonic-gate 			return (-1);
697c478bd9Sstevel@tonic-gate 		}
707c478bd9Sstevel@tonic-gate 		if (ret > isalen) {
717c478bd9Sstevel@tonic-gate 			isalen = ret;
72d1704f8eSAndy Fiddaman 			isalist = reallocf(isalist, isalen);
73d1704f8eSAndy Fiddaman 		} else {
747c478bd9Sstevel@tonic-gate 			break;
75d1704f8eSAndy Fiddaman 		}
767c478bd9Sstevel@tonic-gate 	} while (isalist != NULL);
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate 	if (isalist == NULL) {
797c478bd9Sstevel@tonic-gate 		/*
807c478bd9Sstevel@tonic-gate 		 * Then either a malloc or a realloc failed.
817c478bd9Sstevel@tonic-gate 		 */
827c478bd9Sstevel@tonic-gate 		errno = EAGAIN;
837c478bd9Sstevel@tonic-gate 		return (-1);
847c478bd9Sstevel@tonic-gate 	}
857c478bd9Sstevel@tonic-gate 
867c478bd9Sstevel@tonic-gate 	/*
877c478bd9Sstevel@tonic-gate 	 * Allocate a full pathname buffer.  The sum of the lengths of the
887c478bd9Sstevel@tonic-gate 	 * 'path' and isalist strings is guaranteed to be big enough.
897c478bd9Sstevel@tonic-gate 	 */
907c478bd9Sstevel@tonic-gate 	len = strlen(execname) + isalen;
917c478bd9Sstevel@tonic-gate 	if ((pathname = malloc(len)) == NULL) {
927c478bd9Sstevel@tonic-gate 		free(isalist);
937c478bd9Sstevel@tonic-gate 		errno = EAGAIN;
947c478bd9Sstevel@tonic-gate 		return (-1);
957c478bd9Sstevel@tonic-gate 	}
967c478bd9Sstevel@tonic-gate 
977c478bd9Sstevel@tonic-gate 	/*
987c478bd9Sstevel@tonic-gate 	 * Break the exec name into directory and file name components.
997c478bd9Sstevel@tonic-gate 	 */
1007c478bd9Sstevel@tonic-gate 	(void) strcpy(pathname, execname);
1017c478bd9Sstevel@tonic-gate 	if ((str = strrchr(pathname, '/')) != NULL) {
1027c478bd9Sstevel@tonic-gate 		*++str = '\0';
1037c478bd9Sstevel@tonic-gate 		fname = execname + (str - pathname);
1047c478bd9Sstevel@tonic-gate 	} else {
1057c478bd9Sstevel@tonic-gate 		fname = execname;
1067c478bd9Sstevel@tonic-gate 		*pathname = '\0';
1077c478bd9Sstevel@tonic-gate 	}
1087c478bd9Sstevel@tonic-gate 	len = strlen(pathname);
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	/*
1117c478bd9Sstevel@tonic-gate 	 * For each name in the isa list, look for an executable file
1127c478bd9Sstevel@tonic-gate 	 * with the given file name in the corresponding subdirectory.
1137c478bd9Sstevel@tonic-gate 	 * If it's there, exec it.  If it's not there, or the exec
1147c478bd9Sstevel@tonic-gate 	 * fails, then run the next version ..
1157c478bd9Sstevel@tonic-gate 	 */
1167c478bd9Sstevel@tonic-gate 	str = strtok_r(isalist, " ", &lasts);
1177c478bd9Sstevel@tonic-gate 	saved_errno = ENOENT;
1187c478bd9Sstevel@tonic-gate 	do {
1197c478bd9Sstevel@tonic-gate 		(void) strcpy(pathname + len, str);
1207c478bd9Sstevel@tonic-gate 		(void) strcat(pathname + len, "/");
1217c478bd9Sstevel@tonic-gate 		(void) strcat(pathname + len, fname);
1227c478bd9Sstevel@tonic-gate 		if (access(pathname, X_OK) == 0) {
1237c478bd9Sstevel@tonic-gate 			/*
1247c478bd9Sstevel@tonic-gate 			 * File exists and is marked executable.  Attempt
1257c478bd9Sstevel@tonic-gate 			 * to execute the file from the subdirectory,
1267c478bd9Sstevel@tonic-gate 			 * using the user-supplied argv and envp.
1277c478bd9Sstevel@tonic-gate 			 */
1287c478bd9Sstevel@tonic-gate 			(void) execve(pathname, argv, envp);
1297c478bd9Sstevel@tonic-gate 
1307c478bd9Sstevel@tonic-gate 			/*
1317c478bd9Sstevel@tonic-gate 			 * If we failed to exec because of a temporary
1327c478bd9Sstevel@tonic-gate 			 * resource shortage, it's better to let our
1337c478bd9Sstevel@tonic-gate 			 * caller handle it (free memory, sleep for a while,
1347c478bd9Sstevel@tonic-gate 			 * or whatever before retrying) rather than drive
1357c478bd9Sstevel@tonic-gate 			 * on to run the "less capable" version.
1367c478bd9Sstevel@tonic-gate 			 */
1377c478bd9Sstevel@tonic-gate 			if (errno == EAGAIN) {
1387c478bd9Sstevel@tonic-gate 				saved_errno = errno;
1397c478bd9Sstevel@tonic-gate 				break;
1407c478bd9Sstevel@tonic-gate 			}
1417c478bd9Sstevel@tonic-gate 		}
1427c478bd9Sstevel@tonic-gate 	} while ((str = strtok_r(NULL, " ", &lasts)) != NULL);
1437c478bd9Sstevel@tonic-gate 
1447c478bd9Sstevel@tonic-gate 	free(pathname);
1457c478bd9Sstevel@tonic-gate 	free(isalist);
1467c478bd9Sstevel@tonic-gate 
1477c478bd9Sstevel@tonic-gate 	errno = saved_errno;
1487c478bd9Sstevel@tonic-gate 	return (-1);
1497c478bd9Sstevel@tonic-gate }
150