summaryrefslogtreecommitdiff
path: root/usr/src/lib/libfsmgt/common/cmd.c
blob: 341a29caec35a74d70022922d0a3d96572dc05b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2003 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */

#pragma ident	"%Z%%M%	%I%	%E% SMI"

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <poll.h>
#include <sys/wait.h>
#include <errno.h>
#include <strings.h>
#include <sys/stropts.h>
#include "libfsmgt.h"

#define	MASKVAL (POLLIN | POLLPRI | POLLRDNORM | POLLRDBAND)
#define	STDOUT 1
#define	STDERR 2

/*
 * Public methods
 */

/*
 * Method: cmd_execute_command
 *
 * Description: Executes the given command and returns the output written to
 * stdout and stderr in two separate file descriptors to be read by the caller.
 * It is recommended that the caller use the cmd_retrieve_string method or
 * another polling method to read from the file descriptors especially in the
 * case that the command output is expected to be lengthy.
 *
 * Parameters:
 *	- char *cmd - The command to execute.
 *	- int *output_filedes - The file descriptor to which the stdout output
 *	is written.
 *	- int *err_filedes -  The file descriptor to which the stderr output
 *	is written.
 *
 * Returns:
 *	- int - This value will always be zero.  This was intended to be the
 *	the exit status of the executed command, but in the case of the
 *	execution of a command with a large amount of output (ex: ls of a large
 *	directory) we can't wait for the exec'd command to exit.  This is
 *	because of the way that file descriptors work.  When the child process,
 *	or the process executing the command, writes of 'x' amount of data to
 *	a file desciptor (fd), the fd reaches a threshold and will lock and wait
 *	for a reader to read before writing anymore data.  In this case, we
 *	don't have a reader since the caller reads from the file descriptors,
 *	not the parent process.
 *	The result is that the parent process cannot be allowed to wait for the
 *	child process to exit.  Hence, cannot get the exit status of the
 *	executed command.
 */
int
cmd_execute_command(char *cmd, int *output_filedes, int *err_filedes) {
	pid_t child_pid;
	int output[2];
	int error[2];
	int ret_val;

	if (pipe(output) == -1) {
		return (errno);
	}

	if (pipe(error) == -1) {
		return (errno);
	}

	if ((child_pid = fork()) == -1) {
		return (errno);
	}

	if (child_pid == 0) {
		/*
		 * We are in the child.
		 */

		/*
		 * Close the file descriptors we aren't using.
		 */
		close(output[0]);
		close(error[0]);

		/*
		 * Close stdout and dup to output[1]
		 */
		if (close(STDOUT) == -1) {
			exit(errno);
		}

		if (dup(output[1]) == -1) {
			exit(errno);
		}

		close(output[1]);

		/*
		 * Close stderr and dup to error[1]
		 */
		if (close(STDERR) == -1) {
			exit(errno);
		}

		if (dup(error[1]) == -1) {
			exit(errno);
		}

		close(error[1]);

		if (execl("/usr/bin/sh", "sh", "-c", cmd, (char *)0) == -1) {

			exit(errno);
		} else {
			exit(0);
		}
	}

	/*
	 * We are in the parent
	 */

	/*
	 * Close the file descriptors we aren't using.
	 */
	close(output[1]);
	close(error[1]);

	*output_filedes = output[0];
	*err_filedes = error[0];

	/*
	 * Do not wait for the child process to exit.  Just return.
	 */
	ret_val = 0;
	return (ret_val);

} /* cmd_execute_command */

/*
 * Method: cmd_execute_command_and_retrieve_string
 *
 * Description: Executes the given string and returns the output as it is
 * output as it is written to stdout and stderr in the return string.
 *
 * Parameters:
 *	- char *cmd - the command to execute.
 *	- int *errp - the error indicator.  This will be set to a non-zero
 *	upon error.
 *
 * Returns:
 *	char * - The output of the command to stderr and stdout.
 */
char *
cmd_execute_command_and_retrieve_string(char *cmd, int *errp) {
	pid_t child_pid;
	int output[2];
	int err;
	int status;
	char *ret_val;

	*errp = 0;
	if (pipe(output) == -1) {
		*errp = errno;
		return (NULL);
	}

	if ((child_pid = fork()) == -1) {
		*errp = errno;
		return (NULL);
	}

	if (child_pid == 0) {
		/*
		 * We are in the child.
		 */

		/*
		 * Close the unused file descriptor.
		 */
		close(output[0]);

		/*
		 * Close stdout and dup to output[1]
		 */
		if (close(STDOUT) == -1) {
			*errp = errno;
			exit(*errp);
		}

		if (dup(output[1]) == -1) {
			*errp = errno;
			exit(*errp);
		}

		/*
		 * Close stderr and dup to output[1]
		 */
		if (close(STDERR) == -1) {
			*errp = errno;
			exit(*errp);
		}

		if (dup(output[1]) == -1) {
			*errp = errno;
			exit(*errp);
		}

		close(output[1]);

		if (execl("/usr/bin/sh", "sh", "-c", cmd, (char *)0) == -1) {

			*errp = errno;
			exit(*errp);
		} else {
			exit(0);
		}
	}

	/*
	 * We are in the parent
	 */

	/*
	 * Close the file descriptors we are not using.
	 */
	close(output[1]);

	/*
	 * Wait for the child process to exit.
	 */
	while ((wait(&status) != child_pid)) {
		ret_val = cmd_retrieve_string(output[0], &err);
	}

	/*
	 * Evaluate the wait status and set the evaluated value to
	 * the value of errp.
	 */
	*errp = WEXITSTATUS(status);

	ret_val = cmd_retrieve_string(output[0], &err);

	/*
	 * Caller must free space allocated for ret_val with free()
	 */
	return (ret_val);
} /* cmd_execute_command_and_retrieve_string */

/*
 * Method: cmd_retrieve_string
 *
 * Description: Returns the data written to the file descriptor passed in.
 *
 * Parameters:
 *	- int filedes - The file descriptor to be read.
 *	- int *errp - The error indicator.  This will be set to a non-zero
 *	value upon error.
 *
 * Returns:
 *	- char * - The data read from the file descriptor.
 */
char *
cmd_retrieve_string(int filedes, int *errp) {
	int returned_value = 0;
	int buffer_size = 1024;
	int len;
	char *ret_val;
	char *buffer;
	boolean_t stop_loop = B_FALSE;
	struct pollfd pollfds[1];

	*errp = 0;
	/*
	 * Read from the file descriptor passed into the function.  This
	 * will read data written to the file descriptor on a FIFO basis.
	 * Care must be taken to make sure to get all data from the file
	 * descriptor.
	 */

	ret_val = (char *)calloc((size_t)1, (size_t)sizeof (char));
	ret_val[0] = '\0';


	/*
	 * Set up the pollfd structure with appropriate information.
	 */
	pollfds[0].fd = filedes;
	pollfds[0].events = MASKVAL;
	pollfds[0].revents = 0;

	while (stop_loop == B_FALSE) {
		char *tmp_string;

		switch (poll(pollfds, 1, INFTIM)) {
			case -1:

			case 0:
				/*
				 * Nothing to read yet so continue.
				 */
				continue;
			default:
				buffer = (char *)calloc(
					(size_t)(buffer_size + 1),
					(size_t)sizeof (char));

				if (buffer == NULL) {
					/*
					 * Out of memory
					 */
					*errp = errno;
					return (NULL);
				}

				/*
				 * Call read to read from the filedesc.
				 */
				returned_value = read(filedes, buffer,
					buffer_size);
				if (returned_value <= 0) {
					/*
					 * Either we errored or didn't read any
					 * bytes of data.
					 * returned_value == -1 represents an
					 * error.
					 * returned value == 0 represents 0
					 * bytes read.
					 */
					stop_loop = B_TRUE;
					continue;
				}

				len = strlen(buffer);

				/*
				 * Allocate space for the new string.
				 */
				tmp_string =
				(char *)calloc((size_t)(len+strlen(ret_val)+1),
						(size_t)sizeof (char));

				if (tmp_string == NULL) {
					/*
					 * Out of memory
					 */

					*errp = errno;
					return (NULL);
				}

				/*
				 * Concatenate the the new string in 'buffer'
				 * with whatever is in the 'ret_val' buffer.
				 */
				snprintf(tmp_string, (size_t)(len +
					strlen(ret_val) + 1), "%s%s",
					ret_val, buffer);

				(void) free(ret_val);
				ret_val = strdup(tmp_string);

				if (ret_val == NULL) {
					/*
					 * Out of memory
					 */
					*errp = errno;
					return (NULL);
				}
				(void) free(tmp_string);
				(void) free(buffer);

		} /* switch (poll(pollfds, 1, INFTIM)) */

	} /* while (stop_loop == B_FALSE) */

	return (ret_val);
} /* cmd_retrieve_string */