2N/A * The contents of this file are subject to the terms of the 2N/A * Common Development and Distribution License, Version 1.0 only 2N/A * (the "License"). You may not use this file except in compliance 2N/A * See the License for the specific language governing permissions 2N/A * and limitations under the License. 2N/A * When distributing Covered Code, include this CDDL HEADER in each 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 * Copyright 2003 Sun Microsystems, Inc. All rights reserved. 2N/A * Use is subject to license terms. 2N/A#
pragma ident "%Z%%M% %I% %E% SMI" 2N/A * Method: cmd_execute_command 2N/A * Description: Executes the given command and returns the output written to 2N/A * stdout and stderr in two separate file descriptors to be read by the caller. 2N/A * It is recommended that the caller use the cmd_retrieve_string method or 2N/A * another polling method to read from the file descriptors especially in the 2N/A * case that the command output is expected to be lengthy. 2N/A * - char *cmd - The command to execute. 2N/A * - int *output_filedes - The file descriptor to which the stdout output 2N/A * - int *err_filedes - The file descriptor to which the stderr output 2N/A * - int - This value will always be zero. This was intended to be the 2N/A * the exit status of the executed command, but in the case of the 2N/A * execution of a command with a large amount of output (ex: ls of a large 2N/A * directory) we can't wait for the exec'd command to exit. This is 2N/A * because of the way that file descriptors work. When the child process, 2N/A * or the process executing the command, writes of 'x' amount of data to 2N/A * a file desciptor (fd), the fd reaches a threshold and will lock and wait 2N/A * for a reader to read before writing anymore data. In this case, we 2N/A * don't have a reader since the caller reads from the file descriptors, 2N/A * not the parent process. 2N/A * The result is that the parent process cannot be allowed to wait for the 2N/A * child process to exit. Hence, cannot get the exit status of the 2N/A * We are in the child. 2N/A * Close the file descriptors we aren't using. 2N/A * Close stdout and dup to output[1] 2N/A * Close stderr and dup to error[1] 2N/A * We are in the parent 2N/A * Close the file descriptors we aren't using. 2N/A * Do not wait for the child process to exit. Just return. 2N/A}
/* cmd_execute_command */ 2N/A * Method: cmd_execute_command_and_retrieve_string 2N/A * Description: Executes the given string and returns the output as it is 2N/A * output as it is written to stdout and stderr in the return string. 2N/A * - char *cmd - the command to execute. 2N/A * - int *errp - the error indicator. This will be set to a non-zero 2N/A * char * - The output of the command to stderr and stdout. 2N/A * We are in the child. 2N/A * Close the unused file descriptor. 2N/A * Close stdout and dup to output[1] 2N/A * Close stderr and dup to output[1] 2N/A * We are in the parent 2N/A * Close the file descriptors we are not using. 2N/A * Wait for the child process to exit. 2N/A * Evaluate the wait status and set the evaluated value to 2N/A * the value of errp. 2N/A * Caller must free space allocated for ret_val with free() 2N/A}
/* cmd_execute_command_and_retrieve_string */ 2N/A * Method: cmd_retrieve_string 2N/A * Description: Returns the data written to the file descriptor passed in. 2N/A * - int filedes - The file descriptor to be read. 2N/A * - int *errp - The error indicator. This will be set to a non-zero 2N/A * - char * - The data read from the file descriptor. 2N/A * Read from the file descriptor passed into the function. This 2N/A * will read data written to the file descriptor on a FIFO basis. 2N/A * Care must be taken to make sure to get all data from the file 2N/A * Set up the pollfd structure with appropriate information. 2N/A * Nothing to read yet so continue. 2N/A * Call read to read from the filedesc. 2N/A * Either we errored or didn't read any 2N/A * returned_value == -1 represents an 2N/A * returned value == 0 represents 0 2N/A * Allocate space for the new string. 2N/A * Concatenate the the new string in 'buffer' 2N/A * with whatever is in the 'ret_val' buffer. 2N/A }
/* switch (poll(pollfds, 1, INFTIM)) */ 2N/A }
/* while (stop_loop == B_FALSE) */ 2N/A}
/* cmd_retrieve_string */