5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * CDDL HEADER START
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * The contents of this file are subject to the terms of the
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Common Development and Distribution License (the "License").
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * You may not use this file except in compliance with the License.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * or http://www.opensolaris.org/os/licensing.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * See the License for the specific language governing permissions
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * and limitations under the License.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * When distributing Covered Code, include this CDDL HEADER in each
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * If applicable, add the following below this CDDL HEADER, with the
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * fields enclosed by brackets "[]" replaced with your own identifying
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * information: Portions Copyright [yyyy] [name of copyright owner]
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * CDDL HEADER END
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Use is subject to license terms.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland/* All Rights Reserved */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Private definitions
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland/* Maximum number of arguments to pkg_ExecCmdList */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland/* Size of buffer increments when reading from pipe */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Public Methods
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland pkg_gt("unable to create temp error file, errno=%d"),
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland (void) strlcpy(errfile, perrfile, sizeof (errfile));
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* flush standard i/o before creating new process */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * create new process to execute command in;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * vfork() is being used to avoid duplicating the parents
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * memory space - this means that the child process may
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * not modify any of the parents memory including the
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * standard i/o descriptors - all the child can do is
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * adjust interrupts and open files as a prelude to a
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * call to exec().
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * this is the child process
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* reset any signals to default */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland for (i = 0; i < NSIG; i++) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland i = open(errfile, O_WRONLY|O_CREAT|O_TRUNC, 0666);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (i >= 0) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* Close all open files except standard i/o */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* execute target executable */
4656d4747c8743290bfbe910c64cd75eb4e4af8dGarrett D'Amore (void) execl("/sbin/sh", "/sbin/sh", "-c", cmd, NULL);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland progerr(pkg_gt("exec of <%s> failed, errno=%d"), cmd, errno);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland } else if (pid < 0) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* fork failed! */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland logerr(pkg_gt("bad vfork(), errno=%d"), errno);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * this is the parent process
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /*FALLTHROUGH*/
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* terminated by a signal */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* cleanup previous errfile */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland pkg_gt("unable to create temp error file, errno=%d"),
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland return ((FILE *)0);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland if (strlcpy(errfile, perrfile, sizeof (errfile)) > sizeof (errfile)) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland progerr(pkg_gt("file name max length %d; name is too long: %s"),
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland return ((FILE *)0);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland buffer = (char *)calloc(len, sizeof (char));
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland progerr(pkg_gt("no memory in epopen(), errno=%d"), errno);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland return ((FILE *)0);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland alen = snprintf(buffer, len, "(%s) 2>%s", cmd, errfile);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland alen = snprintf(buffer, len, "%s 2>%s", cmd, errfile);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland progerr(pkg_gt("command max length %d; cmd is too long: %s"),
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland return ((FILE *)0);
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Name: e_ExecCmdArray
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Synopsis: Execute Unix command and return results
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Description: Execute a Unix command and return results and status
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * r_status - [RO, *RW] - (int *)
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Return (exit) status from Unix command:
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * == -1 : child terminated with a signal
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * != -1 : lower 8-bit value child passed to exit()
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * r_results - [RO, *RW] - (char **)
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Any output generated by the Unix command to stdout
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * and to stderr
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * == (char *)NULL if no output generated
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * a_inputFile - [RO, *RO] - (char *)
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Pointer to character string representing file to be
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * used as "standard input" for the command.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * == (char *)NULL to use "/dev/null" as standard input
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * a_cmd - [RO, *RO] - (char *)
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Pointer to character string representing the full path
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * of the Unix command to execute
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * char **a_args - [RO, *RO] - (char **)
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * List of character strings representing the arguments
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * to be passed to the Unix command. The list must be
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * terminated with an element that is (char *)NULL
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Returns: int
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * == 0 - Command executed
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Look at r_status for results of Unix command
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * != 0 - problems executing command
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * r_status and r_results have no meaning;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * r_status will be -1
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * r_results will be NULL
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * NOTE: Any results returned is placed in new storage for the
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * calling method. The caller must use 'free' to dispose
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * of the storage once the results are no longer needed.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * NOTE: If 0 is returned, 'r_status' must be queried to
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * determine the results of the Unix command.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * NOTE: The system "errno" value from immediately after waitpid() call
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * is preserved for the calling method to use to determine
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * the system reason why the operation failed.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlande_ExecCmdArray(int *r_status, char **r_results,
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland char *a_inputFile, char *a_cmd, char **a_args)
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* reset return results buffer pointer */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * See if command exists
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * See if input file exists
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland stdinfile = open("/dev/null", O_RDONLY); /* stdin = /dev/null */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Create a pipe to be used to capture the command output
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* flush standard i/o before creating new process */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * create new process to execute command in;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * vfork() is being used to avoid duplicating the parents
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * memory space - this means that the child process may
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * not modify any of the parents memory including the
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * standard i/o descriptors - all the child can do is
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * adjust interrupts and open files as a prelude to a
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * call to exec().
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * This is the forked (child) process ======================
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* reset any signals to default */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland for (i = 0; i < NSIG; i++) {
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* assign stdin, stdout, stderr as appropriate */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland (void) close(ipipe[0]); /* close out pipe reader side */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* Close all open files except standard i/o */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* execute target executable */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland perror(a_cmd); /* Emit error msg - ends up in callers buffer */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * This is the forking (parent) process ====================
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland (void) close(ipipe[1]); /* Close write side of pipe */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Spin reading data from the child into the buffer - when the read eofs
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * the child has exited
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* read as much child data as there is available buffer space */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland bytesRead = read(ipipe[0], buffer + bufferIndex,
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* break out of read loop if end-of-file encountered */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* if error, continue if recoverable, else break out of loop */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* try again: EAGAIN - insufficient resources */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* try again: EINTR - interrupted system call */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* break out of loop - error not recoverable */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* at least 1 byte read: expand buffer if at end */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland (void) close(ipipe[0]); /* Close read side of pipe */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* Get subprocess exit status */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* break loop if child process status reaped */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* break loop if not interrupted out of waitpid */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * If the child process terminated due to a call to exit(), then
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * set results equal to the 8-bit exit status of the child process;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * otherwise, set the exit status to "-1" indicating that the child
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * exited via a signal.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland *r_status = WIFEXITED(status) ? WEXITSTATUS(status) : -1;
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* return appropriate output */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* No contents in output buffer - discard */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* Not requested to return results - discard */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland /* have output and request to return: pass to calling method */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Name: e_ExecCmdList
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Synopsis: Execute Unix command and return results
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Description: Execute a Unix command and return results and status
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * r_status - [RO, *RW] - (int *)
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Return (exit) status from Unix command
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * r_results - [RO, *RW] - (char **)
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Any output generated by the Unix command to stdout
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * and to stderr
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * == (char *)NULL if no output generated
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * a_inputFile - [RO, *RO] - (char *)
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Pointer to character string representing file to be
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * used as "standard input" for the command.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * == (char *)NULL to use "/dev/null" as standard input
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * a_cmd - [RO, *RO] - (char *)
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Pointer to character string representing the full path
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * of the Unix command to execute
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * ... - [RO] (?)
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Zero or more arguments to the Unix command
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * The argument list must be ended with (void *)NULL
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Returns: int
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * == 0 - Command executed
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Look at r_status for results of Unix command
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * != 0 - problems executing command
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * r_status and r_results have no meaning
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * NOTE: Any results returned is placed in new storage for the
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * calling method. The caller must use 'free' to dispose
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * of the storage once the results are no longer needed.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * NOTE: If LU_SUCCESS is returned, 'r_status' must be queried to
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * determine the results of the Unix command.
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterlande_ExecCmdList(int *r_status, char **r_results,
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland va_list ap; /* references variable argument list */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland * Create argument array for exec system call
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland va_start(ap, a_cmd); /* Begin variable argument processing */
5c51f1241dbbdf2656d0e10011981411ed0c9673Moriah Waterland for (argno = 0; argno < MAX_EXEC_CMD_ARGS; argno++) {