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 5*7257d1b4Sraf * Common Development and Distribution License (the "License"). 6*7257d1b4Sraf * 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 */ 21*7257d1b4Sraf 227c478bd9Sstevel@tonic-gate /* 23*7257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 29*7257d1b4Sraf #pragma weak _isaexec = isaexec 307c478bd9Sstevel@tonic-gate 31*7257d1b4Sraf #include "lint.h" 327c478bd9Sstevel@tonic-gate #include <sys/types.h> 337c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h> 347c478bd9Sstevel@tonic-gate #include <unistd.h> 357c478bd9Sstevel@tonic-gate #include <string.h> 367c478bd9Sstevel@tonic-gate #include <errno.h> 377c478bd9Sstevel@tonic-gate #include <limits.h> 387c478bd9Sstevel@tonic-gate #include <stdarg.h> 397c478bd9Sstevel@tonic-gate #include <stdlib.h> 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate /* 427c478bd9Sstevel@tonic-gate * This is a utility routine to allow wrapper programs to simply 437c478bd9Sstevel@tonic-gate * implement the isalist exec algorithms. See PSARC/1997/220. 447c478bd9Sstevel@tonic-gate */ 457c478bd9Sstevel@tonic-gate int 467c478bd9Sstevel@tonic-gate isaexec(const char *execname, char *const *argv, char *const *envp) 477c478bd9Sstevel@tonic-gate { 487c478bd9Sstevel@tonic-gate const char *fname; 497c478bd9Sstevel@tonic-gate char *isalist; 507c478bd9Sstevel@tonic-gate char *pathname; 517c478bd9Sstevel@tonic-gate char *str; 527c478bd9Sstevel@tonic-gate char *lasts; 537c478bd9Sstevel@tonic-gate size_t isalen = 255; /* wild guess */ 547c478bd9Sstevel@tonic-gate size_t len; 557c478bd9Sstevel@tonic-gate int saved_errno; 567c478bd9Sstevel@tonic-gate 577c478bd9Sstevel@tonic-gate /* 587c478bd9Sstevel@tonic-gate * Extract the isalist(5) for userland from the kernel. 597c478bd9Sstevel@tonic-gate */ 607c478bd9Sstevel@tonic-gate isalist = malloc(isalen); 617c478bd9Sstevel@tonic-gate do { 627c478bd9Sstevel@tonic-gate long ret = sysinfo(SI_ISALIST, isalist, isalen); 637c478bd9Sstevel@tonic-gate if (ret == -1l) { 647c478bd9Sstevel@tonic-gate free(isalist); 657c478bd9Sstevel@tonic-gate errno = ENOENT; 667c478bd9Sstevel@tonic-gate return (-1); 677c478bd9Sstevel@tonic-gate } 687c478bd9Sstevel@tonic-gate if (ret > isalen) { 697c478bd9Sstevel@tonic-gate isalen = ret; 707c478bd9Sstevel@tonic-gate isalist = realloc(isalist, isalen); 717c478bd9Sstevel@tonic-gate } else 727c478bd9Sstevel@tonic-gate break; 737c478bd9Sstevel@tonic-gate } while (isalist != NULL); 747c478bd9Sstevel@tonic-gate 757c478bd9Sstevel@tonic-gate if (isalist == NULL) { 767c478bd9Sstevel@tonic-gate /* 777c478bd9Sstevel@tonic-gate * Then either a malloc or a realloc failed. 787c478bd9Sstevel@tonic-gate */ 797c478bd9Sstevel@tonic-gate errno = EAGAIN; 807c478bd9Sstevel@tonic-gate return (-1); 817c478bd9Sstevel@tonic-gate } 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate /* 847c478bd9Sstevel@tonic-gate * Allocate a full pathname buffer. The sum of the lengths of the 857c478bd9Sstevel@tonic-gate * 'path' and isalist strings is guaranteed to be big enough. 867c478bd9Sstevel@tonic-gate */ 877c478bd9Sstevel@tonic-gate len = strlen(execname) + isalen; 887c478bd9Sstevel@tonic-gate if ((pathname = malloc(len)) == NULL) { 897c478bd9Sstevel@tonic-gate free(isalist); 907c478bd9Sstevel@tonic-gate errno = EAGAIN; 917c478bd9Sstevel@tonic-gate return (-1); 927c478bd9Sstevel@tonic-gate } 937c478bd9Sstevel@tonic-gate 947c478bd9Sstevel@tonic-gate /* 957c478bd9Sstevel@tonic-gate * Break the exec name into directory and file name components. 967c478bd9Sstevel@tonic-gate */ 977c478bd9Sstevel@tonic-gate (void) strcpy(pathname, execname); 987c478bd9Sstevel@tonic-gate if ((str = strrchr(pathname, '/')) != NULL) { 997c478bd9Sstevel@tonic-gate *++str = '\0'; 1007c478bd9Sstevel@tonic-gate fname = execname + (str - pathname); 1017c478bd9Sstevel@tonic-gate } else { 1027c478bd9Sstevel@tonic-gate fname = execname; 1037c478bd9Sstevel@tonic-gate *pathname = '\0'; 1047c478bd9Sstevel@tonic-gate } 1057c478bd9Sstevel@tonic-gate len = strlen(pathname); 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate /* 1087c478bd9Sstevel@tonic-gate * For each name in the isa list, look for an executable file 1097c478bd9Sstevel@tonic-gate * with the given file name in the corresponding subdirectory. 1107c478bd9Sstevel@tonic-gate * If it's there, exec it. If it's not there, or the exec 1117c478bd9Sstevel@tonic-gate * fails, then run the next version .. 1127c478bd9Sstevel@tonic-gate */ 1137c478bd9Sstevel@tonic-gate str = strtok_r(isalist, " ", &lasts); 1147c478bd9Sstevel@tonic-gate saved_errno = ENOENT; 1157c478bd9Sstevel@tonic-gate do { 1167c478bd9Sstevel@tonic-gate (void) strcpy(pathname + len, str); 1177c478bd9Sstevel@tonic-gate (void) strcat(pathname + len, "/"); 1187c478bd9Sstevel@tonic-gate (void) strcat(pathname + len, fname); 1197c478bd9Sstevel@tonic-gate if (access(pathname, X_OK) == 0) { 1207c478bd9Sstevel@tonic-gate /* 1217c478bd9Sstevel@tonic-gate * File exists and is marked executable. Attempt 1227c478bd9Sstevel@tonic-gate * to execute the file from the subdirectory, 1237c478bd9Sstevel@tonic-gate * using the user-supplied argv and envp. 1247c478bd9Sstevel@tonic-gate */ 1257c478bd9Sstevel@tonic-gate (void) execve(pathname, argv, envp); 1267c478bd9Sstevel@tonic-gate 1277c478bd9Sstevel@tonic-gate /* 1287c478bd9Sstevel@tonic-gate * If we failed to exec because of a temporary 1297c478bd9Sstevel@tonic-gate * resource shortage, it's better to let our 1307c478bd9Sstevel@tonic-gate * caller handle it (free memory, sleep for a while, 1317c478bd9Sstevel@tonic-gate * or whatever before retrying) rather than drive 1327c478bd9Sstevel@tonic-gate * on to run the "less capable" version. 1337c478bd9Sstevel@tonic-gate */ 1347c478bd9Sstevel@tonic-gate if (errno == EAGAIN) { 1357c478bd9Sstevel@tonic-gate saved_errno = errno; 1367c478bd9Sstevel@tonic-gate break; 1377c478bd9Sstevel@tonic-gate } 1387c478bd9Sstevel@tonic-gate } 1397c478bd9Sstevel@tonic-gate } while ((str = strtok_r(NULL, " ", &lasts)) != NULL); 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate free(pathname); 1427c478bd9Sstevel@tonic-gate free(isalist); 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate errno = saved_errno; 1457c478bd9Sstevel@tonic-gate return (-1); 1467c478bd9Sstevel@tonic-gate } 147