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