2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
2N/A */
2N/A
2N/A#include "lint.h"
2N/A#include <stdlib.h>
2N/A#include <stdarg.h>
2N/A#include <unistd.h>
2N/A#include <string.h>
2N/A#include <errno.h>
2N/A#include <limits.h>
2N/A#include <fcntl.h>
2N/A#include <sys/systeminfo.h>
2N/A#include <sys/execx.h>
2N/A
2N/A/*
2N/A * This is a utility routine to allow wrapper programs to simply
2N/A * implement the isalist exec algorithms. See PSARC/1997/220.
2N/A */
2N/Aint
2N/Aisaexec(const char *execname, char *const *argv, char *const *envp)
2N/A{
2N/A const char *fname;
2N/A char *isalist;
2N/A char *pathname;
2N/A char *str;
2N/A char *lasts;
2N/A size_t isalen = 255; /* wild guess */
2N/A size_t len;
2N/A int fd;
2N/A int saved_errno;
2N/A
2N/A /*
2N/A * Extract the isalist(5) for userland from the kernel.
2N/A */
2N/A isalist = malloc(isalen);
2N/A do {
2N/A long ret = sysinfo(SI_ISALIST, isalist, isalen);
2N/A if (ret == -1l) {
2N/A free(isalist);
2N/A errno = ENOENT;
2N/A return (-1);
2N/A }
2N/A if (ret > isalen) {
2N/A isalen = ret;
2N/A isalist = realloc(isalist, isalen);
2N/A } else
2N/A break;
2N/A } while (isalist != NULL);
2N/A
2N/A if (isalist == NULL) {
2N/A /*
2N/A * Then either a malloc or a realloc failed.
2N/A */
2N/A errno = EAGAIN;
2N/A return (-1);
2N/A }
2N/A
2N/A /*
2N/A * Allocate a full pathname buffer. The sum of the lengths of the
2N/A * 'path' and isalist strings is guaranteed to be big enough.
2N/A */
2N/A len = strlen(execname) + isalen;
2N/A if ((pathname = malloc(len)) == NULL) {
2N/A free(isalist);
2N/A errno = EAGAIN;
2N/A return (-1);
2N/A }
2N/A
2N/A /*
2N/A * Break the exec name into directory and file name components.
2N/A */
2N/A (void) strcpy(pathname, execname);
2N/A if ((str = strrchr(pathname, '/')) != NULL) {
2N/A *++str = '\0';
2N/A fname = execname + (str - pathname);
2N/A } else {
2N/A fname = execname;
2N/A *pathname = '\0';
2N/A }
2N/A len = strlen(pathname);
2N/A
2N/A /*
2N/A * For each name in the isa list, look for an executable file
2N/A * with the given file name in the corresponding subdirectory.
2N/A * If it's there, exec it. If it's not there, or the exec
2N/A * fails, then run the next version ..
2N/A */
2N/A str = strtok_r(isalist, " ", &lasts);
2N/A saved_errno = ENOENT;
2N/A do {
2N/A (void) strcpy(pathname + len, str);
2N/A (void) strcat(pathname + len, "/");
2N/A (void) strcat(pathname + len, fname);
2N/A if ((fd = open(pathname, O_EXEC)) >= 0) {
2N/A /*
2N/A * File exists and is open for O_EXEC. Attempt
2N/A * to execute the file from the subdirectory,
2N/A * using the user-supplied argv and envp.
2N/A * Retain the current name of the process
2N/A * (so shell scripts retain their own names
2N/A * and don't become, for example, "ksh93").
2N/A */
2N/A (void) execvex((uintptr_t)fd, argv, envp,
2N/A EXEC_DESCRIPTOR | EXEC_RETAINNAME);
2N/A
2N/A /*
2N/A * If we failed to exec because of a temporary
2N/A * resource shortage, it's better to let our
2N/A * caller handle it (free memory, sleep for a while,
2N/A * or whatever before retrying) rather than drive
2N/A * on to run the "less capable" version.
2N/A */
2N/A if (errno == EAGAIN) {
2N/A saved_errno = errno;
2N/A (void) close(fd);
2N/A break;
2N/A }
2N/A (void) close(fd);
2N/A }
2N/A } while ((str = strtok_r(NULL, " ", &lasts)) != NULL);
2N/A
2N/A free(pathname);
2N/A free(isalist);
2N/A
2N/A errno = saved_errno;
2N/A return (-1);
2N/A}