xref: /onnv-gate/usr/src/lib/libc/port/gen/isaexec.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*6812Sraf  * Common Development and Distribution License (the "License").
6*6812Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*6812Sraf 
220Sstevel@tonic-gate /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
29*6812Sraf #pragma weak _isaexec = isaexec
300Sstevel@tonic-gate 
31*6812Sraf #include "lint.h"
320Sstevel@tonic-gate #include <sys/types.h>
330Sstevel@tonic-gate #include <sys/systeminfo.h>
340Sstevel@tonic-gate #include <unistd.h>
350Sstevel@tonic-gate #include <string.h>
360Sstevel@tonic-gate #include <errno.h>
370Sstevel@tonic-gate #include <limits.h>
380Sstevel@tonic-gate #include <stdarg.h>
390Sstevel@tonic-gate #include <stdlib.h>
400Sstevel@tonic-gate 
410Sstevel@tonic-gate /*
420Sstevel@tonic-gate  * This is a utility routine to allow wrapper programs to simply
430Sstevel@tonic-gate  * implement the isalist exec algorithms.  See PSARC/1997/220.
440Sstevel@tonic-gate  */
450Sstevel@tonic-gate int
isaexec(const char * execname,char * const * argv,char * const * envp)460Sstevel@tonic-gate isaexec(const char *execname, char *const *argv, char *const *envp)
470Sstevel@tonic-gate {
480Sstevel@tonic-gate 	const char *fname;
490Sstevel@tonic-gate 	char *isalist;
500Sstevel@tonic-gate 	char *pathname;
510Sstevel@tonic-gate 	char *str;
520Sstevel@tonic-gate 	char *lasts;
530Sstevel@tonic-gate 	size_t isalen = 255;		/* wild guess */
540Sstevel@tonic-gate 	size_t len;
550Sstevel@tonic-gate 	int saved_errno;
560Sstevel@tonic-gate 
570Sstevel@tonic-gate 	/*
580Sstevel@tonic-gate 	 * Extract the isalist(5) for userland from the kernel.
590Sstevel@tonic-gate 	 */
600Sstevel@tonic-gate 	isalist = malloc(isalen);
610Sstevel@tonic-gate 	do {
620Sstevel@tonic-gate 		long ret = sysinfo(SI_ISALIST, isalist, isalen);
630Sstevel@tonic-gate 		if (ret == -1l) {
640Sstevel@tonic-gate 			free(isalist);
650Sstevel@tonic-gate 			errno = ENOENT;
660Sstevel@tonic-gate 			return (-1);
670Sstevel@tonic-gate 		}
680Sstevel@tonic-gate 		if (ret > isalen) {
690Sstevel@tonic-gate 			isalen = ret;
700Sstevel@tonic-gate 			isalist = realloc(isalist, isalen);
710Sstevel@tonic-gate 		} else
720Sstevel@tonic-gate 			break;
730Sstevel@tonic-gate 	} while (isalist != NULL);
740Sstevel@tonic-gate 
750Sstevel@tonic-gate 	if (isalist == NULL) {
760Sstevel@tonic-gate 		/*
770Sstevel@tonic-gate 		 * Then either a malloc or a realloc failed.
780Sstevel@tonic-gate 		 */
790Sstevel@tonic-gate 		errno = EAGAIN;
800Sstevel@tonic-gate 		return (-1);
810Sstevel@tonic-gate 	}
820Sstevel@tonic-gate 
830Sstevel@tonic-gate 	/*
840Sstevel@tonic-gate 	 * Allocate a full pathname buffer.  The sum of the lengths of the
850Sstevel@tonic-gate 	 * 'path' and isalist strings is guaranteed to be big enough.
860Sstevel@tonic-gate 	 */
870Sstevel@tonic-gate 	len = strlen(execname) + isalen;
880Sstevel@tonic-gate 	if ((pathname = malloc(len)) == NULL) {
890Sstevel@tonic-gate 		free(isalist);
900Sstevel@tonic-gate 		errno = EAGAIN;
910Sstevel@tonic-gate 		return (-1);
920Sstevel@tonic-gate 	}
930Sstevel@tonic-gate 
940Sstevel@tonic-gate 	/*
950Sstevel@tonic-gate 	 * Break the exec name into directory and file name components.
960Sstevel@tonic-gate 	 */
970Sstevel@tonic-gate 	(void) strcpy(pathname, execname);
980Sstevel@tonic-gate 	if ((str = strrchr(pathname, '/')) != NULL) {
990Sstevel@tonic-gate 		*++str = '\0';
1000Sstevel@tonic-gate 		fname = execname + (str - pathname);
1010Sstevel@tonic-gate 	} else {
1020Sstevel@tonic-gate 		fname = execname;
1030Sstevel@tonic-gate 		*pathname = '\0';
1040Sstevel@tonic-gate 	}
1050Sstevel@tonic-gate 	len = strlen(pathname);
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	/*
1080Sstevel@tonic-gate 	 * For each name in the isa list, look for an executable file
1090Sstevel@tonic-gate 	 * with the given file name in the corresponding subdirectory.
1100Sstevel@tonic-gate 	 * If it's there, exec it.  If it's not there, or the exec
1110Sstevel@tonic-gate 	 * fails, then run the next version ..
1120Sstevel@tonic-gate 	 */
1130Sstevel@tonic-gate 	str = strtok_r(isalist, " ", &lasts);
1140Sstevel@tonic-gate 	saved_errno = ENOENT;
1150Sstevel@tonic-gate 	do {
1160Sstevel@tonic-gate 		(void) strcpy(pathname + len, str);
1170Sstevel@tonic-gate 		(void) strcat(pathname + len, "/");
1180Sstevel@tonic-gate 		(void) strcat(pathname + len, fname);
1190Sstevel@tonic-gate 		if (access(pathname, X_OK) == 0) {
1200Sstevel@tonic-gate 			/*
1210Sstevel@tonic-gate 			 * File exists and is marked executable.  Attempt
1220Sstevel@tonic-gate 			 * to execute the file from the subdirectory,
1230Sstevel@tonic-gate 			 * using the user-supplied argv and envp.
1240Sstevel@tonic-gate 			 */
1250Sstevel@tonic-gate 			(void) execve(pathname, argv, envp);
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 			/*
1280Sstevel@tonic-gate 			 * If we failed to exec because of a temporary
1290Sstevel@tonic-gate 			 * resource shortage, it's better to let our
1300Sstevel@tonic-gate 			 * caller handle it (free memory, sleep for a while,
1310Sstevel@tonic-gate 			 * or whatever before retrying) rather than drive
1320Sstevel@tonic-gate 			 * on to run the "less capable" version.
1330Sstevel@tonic-gate 			 */
1340Sstevel@tonic-gate 			if (errno == EAGAIN) {
1350Sstevel@tonic-gate 				saved_errno = errno;
1360Sstevel@tonic-gate 				break;
1370Sstevel@tonic-gate 			}
1380Sstevel@tonic-gate 		}
1390Sstevel@tonic-gate 	} while ((str = strtok_r(NULL, " ", &lasts)) != NULL);
1400Sstevel@tonic-gate 
1410Sstevel@tonic-gate 	free(pathname);
1420Sstevel@tonic-gate 	free(isalist);
1430Sstevel@tonic-gate 
1440Sstevel@tonic-gate 	errno = saved_errno;
1450Sstevel@tonic-gate 	return (-1);
1460Sstevel@tonic-gate }
147