]> git.sesse.net Git - vlc/blob - modules/stream_filter/decomp.c
decomp: inclde spawn.h only if used
[vlc] / modules / stream_filter / decomp.c
1 /*****************************************************************************
2  * decomp.c : Decompression module for vlc
3  *****************************************************************************
4  * Copyright © 2008 Rémi Denis-Courmont
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU Lesser General Public License as published by
8  * the Free Software Foundation; either version 2.1 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
19  *****************************************************************************/
20
21 #ifdef HAVE_CONFIG_H
22 # include "config.h"
23 #endif
24
25 #include <vlc_common.h>
26 #include <vlc_plugin.h>
27 #include <vlc_stream.h>
28 #include <vlc_network.h>
29 #include <unistd.h>
30 #ifndef _POSIX_SPAWN
31 # define _POSIX_SPAWN (-1)
32 #endif
33 #include <fcntl.h>
34 #if (_POSIX_SPAWN >= 0)
35 # include <spawn.h>
36 #endif
37 #include <sys/wait.h>
38 #include <sys/ioctl.h>
39 #if defined (__linux__) && defined (HAVE_VMSPLICE)
40 # include <sys/uio.h>
41 # include <sys/mman.h>
42 #else
43 # undef HAVE_VMSPLICE
44 #endif
45
46 static int  OpenGzip (vlc_object_t *);
47 static int  OpenBzip2 (vlc_object_t *);
48 static void Close (vlc_object_t *);
49
50 vlc_module_begin ()
51     set_description (N_("Decompression"))
52     set_category (CAT_INPUT)
53     set_subcategory (SUBCAT_INPUT_STREAM_FILTER)
54     set_capability ("stream_filter", 20)
55     set_callbacks (OpenBzip2, Close)
56     /* TODO: access shortnames for stream_UrlNew() */
57
58     add_submodule ()
59     set_callbacks (OpenGzip, Close)
60 vlc_module_end ()
61
62 struct stream_sys_t
63 {
64     block_t      *peeked;
65     uint64_t     offset;
66     vlc_thread_t thread;
67     pid_t        pid;
68     int          write_fd, read_fd;
69 };
70
71 static void cloexec (int fd)
72 {
73     int flags = fcntl (fd, F_GETFD);
74     fcntl (fd, F_SETFD, FD_CLOEXEC | ((flags != -1) ? flags : 0));
75 }
76
77 extern char **environ;
78
79 static const size_t bufsize = 65536;
80 #ifdef HAVE_VMSPLICE
81 static void cleanup_mmap (void *addr)
82 {
83     munmap (addr, bufsize);
84 }
85 #endif
86
87 static void *Thread (void *data)
88 {
89     stream_t *stream = data;
90     stream_sys_t *p_sys = stream->p_sys;
91 #ifdef HAVE_VMSPLICE
92     ssize_t page_mask = sysconf (_SC_PAGE_SIZE) - 1;
93 #endif
94     int fd = p_sys->write_fd;
95     bool error = false;
96
97     do
98     {
99         ssize_t len;
100         int canc = vlc_savecancel ();
101 #ifdef HAVE_VMSPLICE
102         unsigned char *buf = mmap (NULL, bufsize, PROT_READ|PROT_WRITE,
103                                    MAP_PRIVATE|MAP_ANONYMOUS, -1, 0);
104         vlc_cleanup_push (cleanup_mmap, buf);
105 #else
106         unsigned char buf[bufsize];
107 #endif
108
109         len = stream_Read (stream->p_source, buf, bufsize);
110         vlc_restorecancel (canc);
111         error = len <= 0;
112
113         for (ssize_t i = 0, j; i < len; i += j)
114         {
115 #ifdef HAVE_VMSPLICE
116             if ((len - i) <= page_mask) /* incomplete last page */
117                 j = write (fd, buf + i, len - i);
118             else
119             {
120                 struct iovec iov = { buf + i, (len - i) & ~page_mask, };
121                 j = vmsplice (fd, &iov, 1, SPLICE_F_GIFT);
122             }
123 #else
124             j = write (fd, buf + i, len - i);
125 #endif
126             if (j <= 0)
127             {
128                 if (j == 0)
129                     errno = EPIPE;
130                 msg_Err (stream, "cannot write data (%m)");
131                 error = true;
132                 break;
133             }
134         }
135 #ifdef HAVE_VMSPLICE
136         vlc_cleanup_run (); /* munmap (buf, bufsize) */
137 #endif
138     }
139     while (!error);
140
141     msg_Dbg (stream, "compressed stream at EOF");
142     return NULL;
143 }
144
145
146 #define MIN_BLOCK (1 << 10)
147 #define MAX_BLOCK (1 << 20)
148 /**
149  * Reads decompressed from the decompression program
150  * @return -1 for EAGAIN, 0 for EOF, byte count otherwise.
151  */
152 static int Read (stream_t *stream, void *buf, unsigned int buflen)
153 {
154     stream_sys_t *p_sys = stream->p_sys;
155     block_t *peeked;
156     size_t bonus = 0;
157     ssize_t length;
158
159     if ((peeked = p_sys->peeked) != NULL)
160     {
161         bonus = (buflen > peeked->i_buffer) ? peeked->i_buffer : buflen;
162         memcpy (buf, peeked->p_buffer, bonus);
163         peeked->p_buffer += bonus;
164         peeked->i_buffer -= bonus;
165         if (peeked->i_buffer == 0)
166         {
167             block_Release (peeked);
168             p_sys->peeked = NULL;
169         }
170     }
171
172     length = net_Read (stream, p_sys->read_fd, NULL, buf, buflen, false);
173     if (length < 0)
174         return 0;
175     length += bonus;
176     p_sys->offset += length;
177     return length;
178 }
179
180 /**
181  *
182  */
183 static int Peek (stream_t *stream, const uint8_t **pbuf, unsigned int len)
184 {
185     stream_sys_t *p_sys = stream->p_sys;
186     block_t *peeked = p_sys->peeked;
187     size_t curlen = 0;
188     int fd = p_sys->read_fd;
189
190     if (peeked == NULL)
191         peeked = block_Alloc (len);
192     else if ((curlen = peeked->i_buffer) < len)
193         peeked = block_Realloc (peeked, 0, len);
194
195     if ((p_sys->peeked = peeked) == NULL)
196         return 0;
197
198     if (curlen < len)
199     {
200         ssize_t val = net_Read (stream, fd, NULL, peeked->p_buffer + curlen,
201                                 len - curlen, true);
202         if (val >= 0)
203         {
204             curlen += val;
205             peeked->i_buffer = curlen;
206         }
207     }
208     *pbuf = peeked->p_buffer;
209     return curlen;
210 }
211
212 /**
213  *
214  */
215 static int Control (stream_t *stream, int query, va_list args)
216 {
217     stream_sys_t *p_sys = stream->p_sys;
218
219     switch (query)
220     {
221         case STREAM_CAN_SEEK:
222         case STREAM_CAN_FASTSEEK:
223             *(va_arg (args, bool *)) = false;
224             break;
225         case STREAM_GET_POSITION:
226             *(va_arg (args, int64_t *)) = p_sys->offset;
227             break;
228         case STREAM_GET_SIZE:
229             *(va_arg (args, int64_t *)) = 0;
230             break;
231         case STREAM_GET_MTU:
232             *(va_arg (args, int *)) = 0;
233             break;
234         default:
235             return VLC_EGENERIC;
236     }
237     return VLC_SUCCESS;
238 }
239
240 /**
241  * Pipe data through an external executable.
242  * @param stream the stream filter object.
243  * @param path path to the executable.
244  */
245 static int Open (stream_t *stream, const char *path)
246 {
247     stream_sys_t *p_sys = stream->p_sys = malloc (sizeof (*p_sys));
248     if (p_sys == NULL)
249         return VLC_ENOMEM;
250
251     stream->pf_read = Read;
252     stream->pf_peek = Peek;
253     stream->pf_control = Control;
254     p_sys->peeked = NULL;
255     p_sys->offset = 0;
256     p_sys->pid = -1;
257
258     /* I am not a big fan of the pyramid style, but I cannot think of anything
259      * better here. There are too many failure cases. */
260     int ret = VLC_EGENERIC;
261     int comp[2];
262
263     /* We use two pipes rather than one stream socket pair, so that we can
264      * use vmsplice() on Linux. */
265     if (pipe (comp) == 0)
266     {
267         cloexec (comp[1]);
268         p_sys->write_fd = comp[1];
269
270         int uncomp[2];
271         if (pipe (uncomp) == 0)
272         {
273             cloexec (uncomp[0]);
274             p_sys->read_fd = uncomp[0];
275
276 #if (_POSIX_SPAWN >= 0)
277             posix_spawn_file_actions_t actions;
278             if (posix_spawn_file_actions_init (&actions) == 0)
279             {
280                 char *const argv[] = { (char *)path, NULL };
281
282                 if (!posix_spawn_file_actions_adddup2 (&actions, comp[0], 0)
283                  && !posix_spawn_file_actions_addclose (&actions, comp[0])
284                  && !posix_spawn_file_actions_adddup2 (&actions, uncomp[1], 1)
285                  && !posix_spawn_file_actions_addclose (&actions, uncomp[1])
286                  && !posix_spawnp (&p_sys->pid, path, &actions, NULL, argv,
287                                    environ))
288                 {
289                     if (vlc_clone (&p_sys->thread, Thread, stream,
290                                    VLC_THREAD_PRIORITY_INPUT) == 0)
291                         ret = VLC_SUCCESS;
292                 }
293                 else
294                 {
295                     msg_Err (stream, "Cannot execute %s", path);
296                     p_sys->pid = -1;
297                 }
298                 posix_spawn_file_actions_destroy (&actions);
299             }
300 #else /* _POSIX_SPAWN */
301             switch (p_sys->pid = fork ())
302             {
303                 case -1:
304                     msg_Err (stream, "Cannot fork (%m)");
305                     break;
306                 case 0:
307                     dup2 (comp[0], 0);
308                     close (comp[0]);
309                     dup2 (uncomp[1], 1);
310                     close (uncomp[1]);
311                     execlp (path, path, (char *)NULL);
312                     exit (1); /* if we get, execlp() failed! */
313                 default:
314                     if (vlc_clone (&p_sys->thread, Thread, stream,
315                                    VLC_THREAD_PRIORITY_INPUT) == 0)
316                         ret = VLC_SUCCESS;
317             }
318 #endif /* _POSIX_SPAWN < 0 */
319             close (uncomp[1]);
320             if (ret != VLC_SUCCESS)
321                 close (uncomp[0]);
322         }
323         close (comp[0]);
324         if (ret != VLC_SUCCESS)
325         {
326             close (comp[1]);
327             if (p_sys->pid != -1)
328                 while (waitpid (p_sys->pid, &(int){ 0 }, 0) == -1);
329         }
330     }
331     return ret;
332 }
333
334
335 /**
336  * Releases allocate resources.
337  */
338 static void Close (vlc_object_t *obj)
339 {
340     stream_t *stream = (stream_t *)obj;
341     stream_sys_t *p_sys = stream->p_sys;
342     int status;
343
344     vlc_cancel (p_sys->thread);
345     close (p_sys->read_fd);
346     vlc_join (p_sys->thread, NULL);
347     close (p_sys->write_fd);
348
349     msg_Dbg (obj, "waiting for PID %u", (unsigned)p_sys->pid);
350     while (waitpid (p_sys->pid, &status, 0) == -1);
351     msg_Dbg (obj, "exit status %d", status);
352
353     if (p_sys->peeked)
354         block_Release (p_sys->peeked);
355     free (p_sys);
356 }
357
358
359 /**
360  * Detects gzip file format
361  */
362 static int OpenGzip (vlc_object_t *obj)
363 {
364     stream_t      *stream = (stream_t *)obj;
365     const uint8_t *peek;
366
367     if (stream_Peek (stream->p_source, &peek, 3) < 3)
368         return VLC_EGENERIC;
369
370     if (memcmp (peek, "\x1f\x8b\x08", 3))
371         return VLC_EGENERIC;
372
373     msg_Dbg (obj, "detected gzip compressed stream");
374     return Open (stream, "zcat");
375 }
376
377
378 /**
379  * Detects bzip2 file format
380  */
381 static int OpenBzip2 (vlc_object_t *obj)
382 {
383     stream_t      *stream = (stream_t *)obj;
384     const uint8_t *peek;
385
386     /* (Try to) parse the bzip2 header */
387     if (stream_Peek (stream->p_source, &peek, 10) < 10)
388         return VLC_EGENERIC;
389
390     if (memcmp (peek, "BZh", 3) || (peek[3] < '1') || (peek[3] > '9')
391      || memcmp (peek + 4, "\x31\x41\x59\x26\x53\x59", 6))
392         return VLC_EGENERIC;
393
394     msg_Dbg (obj, "detected bzip2 compressed stream");
395     return Open (stream, "bzcat");
396 }
397