1 /*-
2  * Copyright (c) 2007 Joerg Sonnenberger
3  * Copyright (c) 2012 Michihiro NAKAJIMA
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include "archive_platform.h"
28 __FBSDID("$FreeBSD: head/lib/libarchive/archive_write_set_compression_program.c 201104 2009-12-28 03:14:30Z kientzle $");
29 
30 #ifdef HAVE_SYS_WAIT_H
31 #  include <sys/wait.h>
32 #endif
33 #ifdef HAVE_ERRNO_H
34 #  include <errno.h>
35 #endif
36 #ifdef HAVE_FCNTL_H
37 #  include <fcntl.h>
38 #endif
39 #ifdef HAVE_STDLIB_H
40 #  include <stdlib.h>
41 #endif
42 #ifdef HAVE_STRING_H
43 #  include <string.h>
44 #endif
45 
46 #include "archive.h"
47 #include "archive_private.h"
48 #include "archive_string.h"
49 #include "archive_write_private.h"
50 #include "filter_fork.h"
51 
52 #if ARCHIVE_VERSION_NUMBER < 4000000
53 int
54 archive_write_set_compression_program(struct archive *a, const char *cmd)
55 {
56 	__archive_write_filters_free(a);
57 	return (archive_write_add_filter_program(a, cmd));
58 }
59 #endif
60 
61 struct archive_write_program_data {
62 #if defined(_WIN32) && !defined(__CYGWIN__)
63 	HANDLE		 child;
64 #else
65 	pid_t		 child;
66 #endif
67 	int		 child_stdin, child_stdout;
68 
69 	char		*child_buf;
70 	size_t		 child_buf_len, child_buf_avail;
71 	char		*program_name;
72 };
73 
74 struct private_data {
75 	struct archive_write_program_data *pdata;
76 	struct archive_string description;
77 	char		*cmd;
78 };
79 
80 static int archive_compressor_program_open(struct archive_write_filter *);
81 static int archive_compressor_program_write(struct archive_write_filter *,
82 		    const void *, size_t);
83 static int archive_compressor_program_close(struct archive_write_filter *);
84 static int archive_compressor_program_free(struct archive_write_filter *);
85 
86 /*
87  * Add a filter to this write handle that passes all data through an
88  * external program.
89  */
90 int
91 archive_write_add_filter_program(struct archive *_a, const char *cmd)
92 {
93 	struct archive_write_filter *f = __archive_write_allocate_filter(_a);
94 	struct private_data *data;
95 	static const char *prefix = "Program: ";
96 
97 	archive_check_magic(_a, ARCHIVE_WRITE_MAGIC,
98 	    ARCHIVE_STATE_NEW, "archive_write_add_filter_program");
99 
100 	f->data = calloc(1, sizeof(*data));
101 	if (f->data == NULL)
102 		goto memerr;
103 	data = (struct private_data *)f->data;
104 
105 	data->cmd = strdup(cmd);
106 	if (data->cmd == NULL)
107 		goto memerr;
108 
109 	data->pdata = __archive_write_program_allocate(cmd);
110 	if (data->pdata == NULL)
111 		goto memerr;
112 
113 	/* Make up a description string. */
114 	if (archive_string_ensure(&data->description,
115 	    strlen(prefix) + strlen(cmd) + 1) == NULL)
116 		goto memerr;
117 	archive_strcpy(&data->description, prefix);
118 	archive_strcat(&data->description, cmd);
119 
120 	f->name = data->description.s;
121 	f->code = ARCHIVE_FILTER_PROGRAM;
122 	f->open = archive_compressor_program_open;
123 	f->write = archive_compressor_program_write;
124 	f->close = archive_compressor_program_close;
125 	f->free = archive_compressor_program_free;
126 	return (ARCHIVE_OK);
127 memerr:
128 	archive_compressor_program_free(f);
129 	archive_set_error(_a, ENOMEM,
130 	    "Can't allocate memory for filter program");
131 	return (ARCHIVE_FATAL);
132 }
133 
134 static int
135 archive_compressor_program_open(struct archive_write_filter *f)
136 {
137 	struct private_data *data = (struct private_data *)f->data;
138 
139 	return __archive_write_program_open(f, data->pdata, data->cmd);
140 }
141 
142 static int
143 archive_compressor_program_write(struct archive_write_filter *f,
144     const void *buff, size_t length)
145 {
146 	struct private_data *data = (struct private_data *)f->data;
147 
148 	return __archive_write_program_write(f, data->pdata, buff, length);
149 }
150 
151 static int
152 archive_compressor_program_close(struct archive_write_filter *f)
153 {
154 	struct private_data *data = (struct private_data *)f->data;
155 
156 	return __archive_write_program_close(f, data->pdata);
157 }
158 
159 static int
160 archive_compressor_program_free(struct archive_write_filter *f)
161 {
162 	struct private_data *data = (struct private_data *)f->data;
163 
164 	if (data) {
165 		free(data->cmd);
166 		archive_string_free(&data->description);
167 		__archive_write_program_free(data->pdata);
168 		free(data);
169 		f->data = NULL;
170 	}
171 	return (ARCHIVE_OK);
172 }
173 
174 /*
175  * Allocate resources for executing an external program.
176  */
177 struct archive_write_program_data *
178 __archive_write_program_allocate(const char *program)
179 {
180 	struct archive_write_program_data *data;
181 
182 	data = calloc(1, sizeof(struct archive_write_program_data));
183 	if (data == NULL)
184 		return (data);
185 	data->child_stdin = -1;
186 	data->child_stdout = -1;
187 	data->program_name = strdup(program);
188 	return (data);
189 }
190 
191 /*
192  * Release the resources.
193  */
194 int
195 __archive_write_program_free(struct archive_write_program_data *data)
196 {
197 
198 	if (data) {
199 #if defined(_WIN32) && !defined(__CYGWIN__)
200 		if (data->child)
201 			CloseHandle(data->child);
202 #endif
203 		free(data->child_buf);
204 		free(data);
205 	}
206 	return (ARCHIVE_OK);
207 }
208 
209 int
210 __archive_write_program_open(struct archive_write_filter *f,
211     struct archive_write_program_data *data, const char *cmd)
212 {
213 	pid_t child;
214 	int ret;
215 
216 	ret = __archive_write_open_filter(f->next_filter);
217 	if (ret != ARCHIVE_OK)
218 		return (ret);
219 
220 	if (data->child_buf == NULL) {
221 		data->child_buf_len = 65536;
222 		data->child_buf_avail = 0;
223 		data->child_buf = malloc(data->child_buf_len);
224 
225 		if (data->child_buf == NULL) {
226 			archive_set_error(f->archive, ENOMEM,
227 			    "Can't allocate compression buffer");
228 			return (ARCHIVE_FATAL);
229 		}
230 	}
231 
232 	child = __archive_create_child(cmd, &data->child_stdin,
233 		    &data->child_stdout);
234 	if (child == -1) {
235 		archive_set_error(f->archive, EINVAL,
236 		    "Can't launch external program: %s", cmd);
237 		return (ARCHIVE_FATAL);
238 	}
239 #if defined(_WIN32) && !defined(__CYGWIN__)
240 	data->child = OpenProcess(PROCESS_QUERY_INFORMATION, FALSE, child);
241 	if (data->child == NULL) {
242 		close(data->child_stdin);
243 		data->child_stdin = -1;
244 		close(data->child_stdout);
245 		data->child_stdout = -1;
246 		archive_set_error(f->archive, EINVAL,
247 		    "Can't launch external program: %s", cmd);
248 		return (ARCHIVE_FATAL);
249 	}
250 #else
251 	data->child = child;
252 #endif
253 	return (ARCHIVE_OK);
254 }
255 
256 static ssize_t
257 child_write(struct archive_write_filter *f,
258     struct archive_write_program_data *data, const char *buf, size_t buf_len)
259 {
260 	ssize_t ret;
261 
262 	if (data->child_stdin == -1)
263 		return (-1);
264 
265 	if (buf_len == 0)
266 		return (-1);
267 
268 	for (;;) {
269 		do {
270 			ret = write(data->child_stdin, buf, buf_len);
271 		} while (ret == -1 && errno == EINTR);
272 
273 		if (ret > 0)
274 			return (ret);
275 		if (ret == 0) {
276 			close(data->child_stdin);
277 			data->child_stdin = -1;
278 			fcntl(data->child_stdout, F_SETFL, 0);
279 			return (0);
280 		}
281 		if (ret == -1 && errno != EAGAIN)
282 			return (-1);
283 
284 		if (data->child_stdout == -1) {
285 			fcntl(data->child_stdin, F_SETFL, 0);
286 			__archive_check_child(data->child_stdin,
287 				data->child_stdout);
288 			continue;
289 		}
290 
291 		do {
292 			ret = read(data->child_stdout,
293 			    data->child_buf + data->child_buf_avail,
294 			    data->child_buf_len - data->child_buf_avail);
295 		} while (ret == -1 && errno == EINTR);
296 
297 		if (ret == 0 || (ret == -1 && errno == EPIPE)) {
298 			close(data->child_stdout);
299 			data->child_stdout = -1;
300 			fcntl(data->child_stdin, F_SETFL, 0);
301 			continue;
302 		}
303 		if (ret == -1 && errno == EAGAIN) {
304 			__archive_check_child(data->child_stdin,
305 				data->child_stdout);
306 			continue;
307 		}
308 		if (ret == -1)
309 			return (-1);
310 
311 		data->child_buf_avail += ret;
312 
313 		ret = __archive_write_filter(f->next_filter,
314 		    data->child_buf, data->child_buf_avail);
315 		if (ret != ARCHIVE_OK)
316 			return (-1);
317 		data->child_buf_avail = 0;
318 	}
319 }
320 
321 /*
322  * Write data to the filter stream.
323  */
324 int
325 __archive_write_program_write(struct archive_write_filter *f,
326     struct archive_write_program_data *data, const void *buff, size_t length)
327 {
328 	ssize_t ret;
329 	const char *buf;
330 
331 	if (data->child == 0)
332 		return (ARCHIVE_OK);
333 
334 	buf = buff;
335 	while (length > 0) {
336 		ret = child_write(f, data, buf, length);
337 		if (ret == -1 || ret == 0) {
338 			archive_set_error(f->archive, EIO,
339 			    "Can't write to program: %s", data->program_name);
340 			return (ARCHIVE_FATAL);
341 		}
342 		length -= ret;
343 		buf += ret;
344 	}
345 	return (ARCHIVE_OK);
346 }
347 
348 /*
349  * Finish the filtering...
350  */
351 int
352 __archive_write_program_close(struct archive_write_filter *f,
353     struct archive_write_program_data *data)
354 {
355 	int ret, r1, status;
356 	ssize_t bytes_read;
357 
358 	if (data->child == 0)
359 		return __archive_write_close_filter(f->next_filter);
360 
361 	ret = 0;
362 	close(data->child_stdin);
363 	data->child_stdin = -1;
364 	fcntl(data->child_stdout, F_SETFL, 0);
365 
366 	for (;;) {
367 		do {
368 			bytes_read = read(data->child_stdout,
369 			    data->child_buf + data->child_buf_avail,
370 			    data->child_buf_len - data->child_buf_avail);
371 		} while (bytes_read == -1 && errno == EINTR);
372 
373 		if (bytes_read == 0 || (bytes_read == -1 && errno == EPIPE))
374 			break;
375 
376 		if (bytes_read == -1) {
377 			archive_set_error(f->archive, errno,
378 			    "Error reading from program: %s", data->program_name);
379 			ret = ARCHIVE_FATAL;
380 			goto cleanup;
381 		}
382 		data->child_buf_avail += bytes_read;
383 
384 		ret = __archive_write_filter(f->next_filter,
385 		    data->child_buf, data->child_buf_avail);
386 		if (ret != ARCHIVE_OK) {
387 			ret = ARCHIVE_FATAL;
388 			goto cleanup;
389 		}
390 		data->child_buf_avail = 0;
391 	}
392 
393 cleanup:
394 	/* Shut down the child. */
395 	if (data->child_stdin != -1)
396 		close(data->child_stdin);
397 	if (data->child_stdout != -1)
398 		close(data->child_stdout);
399 	while (waitpid(data->child, &status, 0) == -1 && errno == EINTR)
400 		continue;
401 #if defined(_WIN32) && !defined(__CYGWIN__)
402 	CloseHandle(data->child);
403 #endif
404 	data->child = 0;
405 
406 	if (status != 0) {
407 		archive_set_error(f->archive, EIO,
408 		    "Error closing program: %s", data->program_name);
409 		ret = ARCHIVE_FATAL;
410 	}
411 	r1 = __archive_write_close_filter(f->next_filter);
412 	return (r1 < ret ? r1 : ret);
413 }
414 
415