1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2003 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate #include <stdio.h> 30*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 31*7c478bd9Sstevel@tonic-gate #include <unistd.h> 32*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 33*7c478bd9Sstevel@tonic-gate #include <poll.h> 34*7c478bd9Sstevel@tonic-gate #include <sys/wait.h> 35*7c478bd9Sstevel@tonic-gate #include <errno.h> 36*7c478bd9Sstevel@tonic-gate #include <strings.h> 37*7c478bd9Sstevel@tonic-gate #include <sys/stropts.h> 38*7c478bd9Sstevel@tonic-gate #include "libfsmgt.h" 39*7c478bd9Sstevel@tonic-gate 40*7c478bd9Sstevel@tonic-gate #define MASKVAL (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND) 41*7c478bd9Sstevel@tonic-gate #define STDOUT 1 42*7c478bd9Sstevel@tonic-gate #define STDERR 2 43*7c478bd9Sstevel@tonic-gate 44*7c478bd9Sstevel@tonic-gate /* 45*7c478bd9Sstevel@tonic-gate * Public methods 46*7c478bd9Sstevel@tonic-gate */ 47*7c478bd9Sstevel@tonic-gate 48*7c478bd9Sstevel@tonic-gate /* 49*7c478bd9Sstevel@tonic-gate * Method: cmd_execute_command 50*7c478bd9Sstevel@tonic-gate * 51*7c478bd9Sstevel@tonic-gate * Description: Executes the given command and returns the output written to 52*7c478bd9Sstevel@tonic-gate * stdout and stderr in two separate file descriptors to be read by the caller. 53*7c478bd9Sstevel@tonic-gate * It is recommended that the caller use the cmd_retrieve_string method or 54*7c478bd9Sstevel@tonic-gate * another polling method to read from the file descriptors especially in the 55*7c478bd9Sstevel@tonic-gate * case that the command output is expected to be lengthy. 56*7c478bd9Sstevel@tonic-gate * 57*7c478bd9Sstevel@tonic-gate * Parameters: 58*7c478bd9Sstevel@tonic-gate * - char *cmd - The command to execute. 59*7c478bd9Sstevel@tonic-gate * - int *output_filedes - The file descriptor to which the stdout output 60*7c478bd9Sstevel@tonic-gate * is written. 61*7c478bd9Sstevel@tonic-gate * - int *err_filedes - The file descriptor to which the stderr output 62*7c478bd9Sstevel@tonic-gate * is written. 63*7c478bd9Sstevel@tonic-gate * 64*7c478bd9Sstevel@tonic-gate * Returns: 65*7c478bd9Sstevel@tonic-gate * - int - This value will always be zero. This was intended to be the 66*7c478bd9Sstevel@tonic-gate * the exit status of the executed command, but in the case of the 67*7c478bd9Sstevel@tonic-gate * execution of a command with a large amount of output (ex: ls of a large 68*7c478bd9Sstevel@tonic-gate * directory) we can't wait for the exec'd command to exit. This is 69*7c478bd9Sstevel@tonic-gate * because of the way that file descriptors work. When the child process, 70*7c478bd9Sstevel@tonic-gate * or the process executing the command, writes of 'x' amount of data to 71*7c478bd9Sstevel@tonic-gate * a file desciptor (fd), the fd reaches a threshold and will lock and wait 72*7c478bd9Sstevel@tonic-gate * for a reader to read before writing anymore data. In this case, we 73*7c478bd9Sstevel@tonic-gate * don't have a reader since the caller reads from the file descriptors, 74*7c478bd9Sstevel@tonic-gate * not the parent process. 75*7c478bd9Sstevel@tonic-gate * The result is that the parent process cannot be allowed to wait for the 76*7c478bd9Sstevel@tonic-gate * child process to exit. Hence, cannot get the exit status of the 77*7c478bd9Sstevel@tonic-gate * executed command. 78*7c478bd9Sstevel@tonic-gate */ 79*7c478bd9Sstevel@tonic-gate int 80*7c478bd9Sstevel@tonic-gate cmd_execute_command(char *cmd, int *output_filedes, int *err_filedes) { 81*7c478bd9Sstevel@tonic-gate pid_t child_pid; 82*7c478bd9Sstevel@tonic-gate int output[2]; 83*7c478bd9Sstevel@tonic-gate int error[2]; 84*7c478bd9Sstevel@tonic-gate int ret_val; 85*7c478bd9Sstevel@tonic-gate 86*7c478bd9Sstevel@tonic-gate if (pipe(output) == -1) { 87*7c478bd9Sstevel@tonic-gate return (errno); 88*7c478bd9Sstevel@tonic-gate } 89*7c478bd9Sstevel@tonic-gate 90*7c478bd9Sstevel@tonic-gate if (pipe(error) == -1) { 91*7c478bd9Sstevel@tonic-gate return (errno); 92*7c478bd9Sstevel@tonic-gate } 93*7c478bd9Sstevel@tonic-gate 94*7c478bd9Sstevel@tonic-gate if ((child_pid = fork()) == -1) { 95*7c478bd9Sstevel@tonic-gate return (errno); 96*7c478bd9Sstevel@tonic-gate } 97*7c478bd9Sstevel@tonic-gate 98*7c478bd9Sstevel@tonic-gate if (child_pid == 0) { 99*7c478bd9Sstevel@tonic-gate /* 100*7c478bd9Sstevel@tonic-gate * We are in the child. 101*7c478bd9Sstevel@tonic-gate */ 102*7c478bd9Sstevel@tonic-gate 103*7c478bd9Sstevel@tonic-gate /* 104*7c478bd9Sstevel@tonic-gate * Close the file descriptors we aren't using. 105*7c478bd9Sstevel@tonic-gate */ 106*7c478bd9Sstevel@tonic-gate close(output[0]); 107*7c478bd9Sstevel@tonic-gate close(error[0]); 108*7c478bd9Sstevel@tonic-gate 109*7c478bd9Sstevel@tonic-gate /* 110*7c478bd9Sstevel@tonic-gate * Close stdout and dup to output[1] 111*7c478bd9Sstevel@tonic-gate */ 112*7c478bd9Sstevel@tonic-gate if (close(STDOUT) == -1) { 113*7c478bd9Sstevel@tonic-gate exit(errno); 114*7c478bd9Sstevel@tonic-gate } 115*7c478bd9Sstevel@tonic-gate 116*7c478bd9Sstevel@tonic-gate if (dup(output[1]) == -1) { 117*7c478bd9Sstevel@tonic-gate exit(errno); 118*7c478bd9Sstevel@tonic-gate } 119*7c478bd9Sstevel@tonic-gate 120*7c478bd9Sstevel@tonic-gate close(output[1]); 121*7c478bd9Sstevel@tonic-gate 122*7c478bd9Sstevel@tonic-gate /* 123*7c478bd9Sstevel@tonic-gate * Close stderr and dup to error[1] 124*7c478bd9Sstevel@tonic-gate */ 125*7c478bd9Sstevel@tonic-gate if (close(STDERR) == -1) { 126*7c478bd9Sstevel@tonic-gate exit(errno); 127*7c478bd9Sstevel@tonic-gate } 128*7c478bd9Sstevel@tonic-gate 129*7c478bd9Sstevel@tonic-gate if (dup(error[1]) == -1) { 130*7c478bd9Sstevel@tonic-gate exit(errno); 131*7c478bd9Sstevel@tonic-gate } 132*7c478bd9Sstevel@tonic-gate 133*7c478bd9Sstevel@tonic-gate close(error[1]); 134*7c478bd9Sstevel@tonic-gate 135*7c478bd9Sstevel@tonic-gate if (execl("/usr/bin/sh", "sh", "-c", cmd, (char *)0) == -1) { 136*7c478bd9Sstevel@tonic-gate 137*7c478bd9Sstevel@tonic-gate exit(errno); 138*7c478bd9Sstevel@tonic-gate } else { 139*7c478bd9Sstevel@tonic-gate exit(0); 140*7c478bd9Sstevel@tonic-gate } 141*7c478bd9Sstevel@tonic-gate } 142*7c478bd9Sstevel@tonic-gate 143*7c478bd9Sstevel@tonic-gate /* 144*7c478bd9Sstevel@tonic-gate * We are in the parent 145*7c478bd9Sstevel@tonic-gate */ 146*7c478bd9Sstevel@tonic-gate 147*7c478bd9Sstevel@tonic-gate /* 148*7c478bd9Sstevel@tonic-gate * Close the file descriptors we aren't using. 149*7c478bd9Sstevel@tonic-gate */ 150*7c478bd9Sstevel@tonic-gate close(output[1]); 151*7c478bd9Sstevel@tonic-gate close(error[1]); 152*7c478bd9Sstevel@tonic-gate 153*7c478bd9Sstevel@tonic-gate *output_filedes = output[0]; 154*7c478bd9Sstevel@tonic-gate *err_filedes = error[0]; 155*7c478bd9Sstevel@tonic-gate 156*7c478bd9Sstevel@tonic-gate /* 157*7c478bd9Sstevel@tonic-gate * Do not wait for the child process to exit. Just return. 158*7c478bd9Sstevel@tonic-gate */ 159*7c478bd9Sstevel@tonic-gate ret_val = 0; 160*7c478bd9Sstevel@tonic-gate return (ret_val); 161*7c478bd9Sstevel@tonic-gate 162*7c478bd9Sstevel@tonic-gate } /* cmd_execute_command */ 163*7c478bd9Sstevel@tonic-gate 164*7c478bd9Sstevel@tonic-gate /* 165*7c478bd9Sstevel@tonic-gate * Method: cmd_execute_command_and_retrieve_string 166*7c478bd9Sstevel@tonic-gate * 167*7c478bd9Sstevel@tonic-gate * Description: Executes the given string and returns the output as it is 168*7c478bd9Sstevel@tonic-gate * output as it is written to stdout and stderr in the return string. 169*7c478bd9Sstevel@tonic-gate * 170*7c478bd9Sstevel@tonic-gate * Parameters: 171*7c478bd9Sstevel@tonic-gate * - char *cmd - the command to execute. 172*7c478bd9Sstevel@tonic-gate * - int *errp - the error indicator. This will be set to a non-zero 173*7c478bd9Sstevel@tonic-gate * upon error. 174*7c478bd9Sstevel@tonic-gate * 175*7c478bd9Sstevel@tonic-gate * Returns: 176*7c478bd9Sstevel@tonic-gate * char * - The output of the command to stderr and stdout. 177*7c478bd9Sstevel@tonic-gate */ 178*7c478bd9Sstevel@tonic-gate char * 179*7c478bd9Sstevel@tonic-gate cmd_execute_command_and_retrieve_string(char *cmd, int *errp) { 180*7c478bd9Sstevel@tonic-gate pid_t child_pid; 181*7c478bd9Sstevel@tonic-gate int output[2]; 182*7c478bd9Sstevel@tonic-gate int err; 183*7c478bd9Sstevel@tonic-gate int status; 184*7c478bd9Sstevel@tonic-gate char *ret_val; 185*7c478bd9Sstevel@tonic-gate 186*7c478bd9Sstevel@tonic-gate *errp = 0; 187*7c478bd9Sstevel@tonic-gate if (pipe(output) == -1) { 188*7c478bd9Sstevel@tonic-gate *errp = errno; 189*7c478bd9Sstevel@tonic-gate return (NULL); 190*7c478bd9Sstevel@tonic-gate } 191*7c478bd9Sstevel@tonic-gate 192*7c478bd9Sstevel@tonic-gate if ((child_pid = fork()) == -1) { 193*7c478bd9Sstevel@tonic-gate *errp = errno; 194*7c478bd9Sstevel@tonic-gate return (NULL); 195*7c478bd9Sstevel@tonic-gate } 196*7c478bd9Sstevel@tonic-gate 197*7c478bd9Sstevel@tonic-gate if (child_pid == 0) { 198*7c478bd9Sstevel@tonic-gate /* 199*7c478bd9Sstevel@tonic-gate * We are in the child. 200*7c478bd9Sstevel@tonic-gate */ 201*7c478bd9Sstevel@tonic-gate 202*7c478bd9Sstevel@tonic-gate /* 203*7c478bd9Sstevel@tonic-gate * Close the unused file descriptor. 204*7c478bd9Sstevel@tonic-gate */ 205*7c478bd9Sstevel@tonic-gate close(output[0]); 206*7c478bd9Sstevel@tonic-gate 207*7c478bd9Sstevel@tonic-gate /* 208*7c478bd9Sstevel@tonic-gate * Close stdout and dup to output[1] 209*7c478bd9Sstevel@tonic-gate */ 210*7c478bd9Sstevel@tonic-gate if (close(STDOUT) == -1) { 211*7c478bd9Sstevel@tonic-gate *errp = errno; 212*7c478bd9Sstevel@tonic-gate exit(*errp); 213*7c478bd9Sstevel@tonic-gate } 214*7c478bd9Sstevel@tonic-gate 215*7c478bd9Sstevel@tonic-gate if (dup(output[1]) == -1) { 216*7c478bd9Sstevel@tonic-gate *errp = errno; 217*7c478bd9Sstevel@tonic-gate exit(*errp); 218*7c478bd9Sstevel@tonic-gate } 219*7c478bd9Sstevel@tonic-gate 220*7c478bd9Sstevel@tonic-gate /* 221*7c478bd9Sstevel@tonic-gate * Close stderr and dup to output[1] 222*7c478bd9Sstevel@tonic-gate */ 223*7c478bd9Sstevel@tonic-gate if (close(STDERR) == -1) { 224*7c478bd9Sstevel@tonic-gate *errp = errno; 225*7c478bd9Sstevel@tonic-gate exit(*errp); 226*7c478bd9Sstevel@tonic-gate } 227*7c478bd9Sstevel@tonic-gate 228*7c478bd9Sstevel@tonic-gate if (dup(output[1]) == -1) { 229*7c478bd9Sstevel@tonic-gate *errp = errno; 230*7c478bd9Sstevel@tonic-gate exit(*errp); 231*7c478bd9Sstevel@tonic-gate } 232*7c478bd9Sstevel@tonic-gate 233*7c478bd9Sstevel@tonic-gate close(output[1]); 234*7c478bd9Sstevel@tonic-gate 235*7c478bd9Sstevel@tonic-gate if (execl("/usr/bin/sh", "sh", "-c", cmd, (char *)0) == -1) { 236*7c478bd9Sstevel@tonic-gate 237*7c478bd9Sstevel@tonic-gate *errp = errno; 238*7c478bd9Sstevel@tonic-gate exit(*errp); 239*7c478bd9Sstevel@tonic-gate } else { 240*7c478bd9Sstevel@tonic-gate exit(0); 241*7c478bd9Sstevel@tonic-gate } 242*7c478bd9Sstevel@tonic-gate } 243*7c478bd9Sstevel@tonic-gate 244*7c478bd9Sstevel@tonic-gate /* 245*7c478bd9Sstevel@tonic-gate * We are in the parent 246*7c478bd9Sstevel@tonic-gate */ 247*7c478bd9Sstevel@tonic-gate 248*7c478bd9Sstevel@tonic-gate /* 249*7c478bd9Sstevel@tonic-gate * Close the file descriptors we are not using. 250*7c478bd9Sstevel@tonic-gate */ 251*7c478bd9Sstevel@tonic-gate close(output[1]); 252*7c478bd9Sstevel@tonic-gate 253*7c478bd9Sstevel@tonic-gate /* 254*7c478bd9Sstevel@tonic-gate * Wait for the child process to exit. 255*7c478bd9Sstevel@tonic-gate */ 256*7c478bd9Sstevel@tonic-gate while ((wait(&status) != child_pid)) { 257*7c478bd9Sstevel@tonic-gate ret_val = cmd_retrieve_string(output[0], &err); 258*7c478bd9Sstevel@tonic-gate } 259*7c478bd9Sstevel@tonic-gate 260*7c478bd9Sstevel@tonic-gate /* 261*7c478bd9Sstevel@tonic-gate * Evaluate the wait status and set the evaluated value to 262*7c478bd9Sstevel@tonic-gate * the value of errp. 263*7c478bd9Sstevel@tonic-gate */ 264*7c478bd9Sstevel@tonic-gate *errp = WEXITSTATUS(status); 265*7c478bd9Sstevel@tonic-gate 266*7c478bd9Sstevel@tonic-gate ret_val = cmd_retrieve_string(output[0], &err); 267*7c478bd9Sstevel@tonic-gate 268*7c478bd9Sstevel@tonic-gate /* 269*7c478bd9Sstevel@tonic-gate * Caller must free space allocated for ret_val with free() 270*7c478bd9Sstevel@tonic-gate */ 271*7c478bd9Sstevel@tonic-gate return (ret_val); 272*7c478bd9Sstevel@tonic-gate } /* cmd_execute_command_and_retrieve_string */ 273*7c478bd9Sstevel@tonic-gate 274*7c478bd9Sstevel@tonic-gate /* 275*7c478bd9Sstevel@tonic-gate * Method: cmd_retrieve_string 276*7c478bd9Sstevel@tonic-gate * 277*7c478bd9Sstevel@tonic-gate * Description: Returns the data written to the file descriptor passed in. 278*7c478bd9Sstevel@tonic-gate * 279*7c478bd9Sstevel@tonic-gate * Parameters: 280*7c478bd9Sstevel@tonic-gate * - int filedes - The file descriptor to be read. 281*7c478bd9Sstevel@tonic-gate * - int *errp - The error indicator. This will be set to a non-zero 282*7c478bd9Sstevel@tonic-gate * value upon error. 283*7c478bd9Sstevel@tonic-gate * 284*7c478bd9Sstevel@tonic-gate * Returns: 285*7c478bd9Sstevel@tonic-gate * - char * - The data read from the file descriptor. 286*7c478bd9Sstevel@tonic-gate */ 287*7c478bd9Sstevel@tonic-gate char * 288*7c478bd9Sstevel@tonic-gate cmd_retrieve_string(int filedes, int *errp) { 289*7c478bd9Sstevel@tonic-gate int returned_value = 0; 290*7c478bd9Sstevel@tonic-gate int buffer_size = 1024; 291*7c478bd9Sstevel@tonic-gate int len; 292*7c478bd9Sstevel@tonic-gate char *ret_val; 293*7c478bd9Sstevel@tonic-gate char *buffer; 294*7c478bd9Sstevel@tonic-gate boolean_t stop_loop = B_FALSE; 295*7c478bd9Sstevel@tonic-gate struct pollfd pollfds[1]; 296*7c478bd9Sstevel@tonic-gate 297*7c478bd9Sstevel@tonic-gate *errp = 0; 298*7c478bd9Sstevel@tonic-gate /* 299*7c478bd9Sstevel@tonic-gate * Read from the file descriptor passed into the function. This 300*7c478bd9Sstevel@tonic-gate * will read data written to the file descriptor on a FIFO basis. 301*7c478bd9Sstevel@tonic-gate * Care must be taken to make sure to get all data from the file 302*7c478bd9Sstevel@tonic-gate * descriptor. 303*7c478bd9Sstevel@tonic-gate */ 304*7c478bd9Sstevel@tonic-gate 305*7c478bd9Sstevel@tonic-gate ret_val = (char *)calloc((size_t)1, (size_t)sizeof (char)); 306*7c478bd9Sstevel@tonic-gate ret_val[0] = '\0'; 307*7c478bd9Sstevel@tonic-gate 308*7c478bd9Sstevel@tonic-gate 309*7c478bd9Sstevel@tonic-gate /* 310*7c478bd9Sstevel@tonic-gate * Set up the pollfd structure with appropriate information. 311*7c478bd9Sstevel@tonic-gate */ 312*7c478bd9Sstevel@tonic-gate pollfds[0].fd = filedes; 313*7c478bd9Sstevel@tonic-gate pollfds[0].events = MASKVAL; 314*7c478bd9Sstevel@tonic-gate pollfds[0].revents = 0; 315*7c478bd9Sstevel@tonic-gate 316*7c478bd9Sstevel@tonic-gate while (stop_loop == B_FALSE) { 317*7c478bd9Sstevel@tonic-gate char *tmp_string; 318*7c478bd9Sstevel@tonic-gate 319*7c478bd9Sstevel@tonic-gate switch (poll(pollfds, 1, INFTIM)) { 320*7c478bd9Sstevel@tonic-gate case -1: 321*7c478bd9Sstevel@tonic-gate 322*7c478bd9Sstevel@tonic-gate case 0: 323*7c478bd9Sstevel@tonic-gate /* 324*7c478bd9Sstevel@tonic-gate * Nothing to read yet so continue. 325*7c478bd9Sstevel@tonic-gate */ 326*7c478bd9Sstevel@tonic-gate continue; 327*7c478bd9Sstevel@tonic-gate default: 328*7c478bd9Sstevel@tonic-gate buffer = (char *)calloc( 329*7c478bd9Sstevel@tonic-gate (size_t)(buffer_size + 1), 330*7c478bd9Sstevel@tonic-gate (size_t)sizeof (char)); 331*7c478bd9Sstevel@tonic-gate 332*7c478bd9Sstevel@tonic-gate if (buffer == NULL) { 333*7c478bd9Sstevel@tonic-gate /* 334*7c478bd9Sstevel@tonic-gate * Out of memory 335*7c478bd9Sstevel@tonic-gate */ 336*7c478bd9Sstevel@tonic-gate *errp = errno; 337*7c478bd9Sstevel@tonic-gate return (NULL); 338*7c478bd9Sstevel@tonic-gate } 339*7c478bd9Sstevel@tonic-gate 340*7c478bd9Sstevel@tonic-gate /* 341*7c478bd9Sstevel@tonic-gate * Call read to read from the filedesc. 342*7c478bd9Sstevel@tonic-gate */ 343*7c478bd9Sstevel@tonic-gate returned_value = read(filedes, buffer, 344*7c478bd9Sstevel@tonic-gate buffer_size); 345*7c478bd9Sstevel@tonic-gate if (returned_value <= 0) { 346*7c478bd9Sstevel@tonic-gate /* 347*7c478bd9Sstevel@tonic-gate * Either we errored or didn't read any 348*7c478bd9Sstevel@tonic-gate * bytes of data. 349*7c478bd9Sstevel@tonic-gate * returned_value == -1 represents an 350*7c478bd9Sstevel@tonic-gate * error. 351*7c478bd9Sstevel@tonic-gate * returned value == 0 represents 0 352*7c478bd9Sstevel@tonic-gate * bytes read. 353*7c478bd9Sstevel@tonic-gate */ 354*7c478bd9Sstevel@tonic-gate stop_loop = B_TRUE; 355*7c478bd9Sstevel@tonic-gate continue; 356*7c478bd9Sstevel@tonic-gate } 357*7c478bd9Sstevel@tonic-gate 358*7c478bd9Sstevel@tonic-gate len = strlen(buffer); 359*7c478bd9Sstevel@tonic-gate 360*7c478bd9Sstevel@tonic-gate /* 361*7c478bd9Sstevel@tonic-gate * Allocate space for the new string. 362*7c478bd9Sstevel@tonic-gate */ 363*7c478bd9Sstevel@tonic-gate tmp_string = 364*7c478bd9Sstevel@tonic-gate (char *)calloc((size_t)(len+strlen(ret_val)+1), 365*7c478bd9Sstevel@tonic-gate (size_t)sizeof (char)); 366*7c478bd9Sstevel@tonic-gate 367*7c478bd9Sstevel@tonic-gate if (tmp_string == NULL) { 368*7c478bd9Sstevel@tonic-gate /* 369*7c478bd9Sstevel@tonic-gate * Out of memory 370*7c478bd9Sstevel@tonic-gate */ 371*7c478bd9Sstevel@tonic-gate 372*7c478bd9Sstevel@tonic-gate *errp = errno; 373*7c478bd9Sstevel@tonic-gate return (NULL); 374*7c478bd9Sstevel@tonic-gate } 375*7c478bd9Sstevel@tonic-gate 376*7c478bd9Sstevel@tonic-gate /* 377*7c478bd9Sstevel@tonic-gate * Concatenate the the new string in 'buffer' 378*7c478bd9Sstevel@tonic-gate * with whatever is in the 'ret_val' buffer. 379*7c478bd9Sstevel@tonic-gate */ 380*7c478bd9Sstevel@tonic-gate snprintf(tmp_string, (size_t)(len + 381*7c478bd9Sstevel@tonic-gate strlen(ret_val) + 1), "%s%s", 382*7c478bd9Sstevel@tonic-gate ret_val, buffer); 383*7c478bd9Sstevel@tonic-gate 384*7c478bd9Sstevel@tonic-gate (void) free(ret_val); 385*7c478bd9Sstevel@tonic-gate ret_val = strdup(tmp_string); 386*7c478bd9Sstevel@tonic-gate 387*7c478bd9Sstevel@tonic-gate if (ret_val == NULL) { 388*7c478bd9Sstevel@tonic-gate /* 389*7c478bd9Sstevel@tonic-gate * Out of memory 390*7c478bd9Sstevel@tonic-gate */ 391*7c478bd9Sstevel@tonic-gate *errp = errno; 392*7c478bd9Sstevel@tonic-gate return (NULL); 393*7c478bd9Sstevel@tonic-gate } 394*7c478bd9Sstevel@tonic-gate (void) free(tmp_string); 395*7c478bd9Sstevel@tonic-gate (void) free(buffer); 396*7c478bd9Sstevel@tonic-gate 397*7c478bd9Sstevel@tonic-gate } /* switch (poll(pollfds, 1, INFTIM)) */ 398*7c478bd9Sstevel@tonic-gate 399*7c478bd9Sstevel@tonic-gate } /* while (stop_loop == B_FALSE) */ 400*7c478bd9Sstevel@tonic-gate 401*7c478bd9Sstevel@tonic-gate return (ret_val); 402*7c478bd9Sstevel@tonic-gate } /* cmd_retrieve_string */ 403