aboutsummaryrefslogtreecommitdiff
path: root/src/main/extractor_ipc_w32.c
blob: 8749da950fdbecbcd779a5dabfd4c740b34a1b44 (plain) (blame)
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
/*
     This file is part of libextractor.
     Copyright (C) 2002, 2003, 2004, 2005, 2006, 2009, 2012 Vidyut Samanta and Christian Grothoff

     libextractor is free software; you can redistribute it and/or modify
     it under the terms of the GNU General Public License as published
     by the Free Software Foundation; either version 3, or (at your
     option) any later version.

     libextractor is distributed in the hope that it will be useful, but
     WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     General Public License for more details.

     You should have received a copy of the GNU General Public License
     along with libextractor; see the file COPYING.  If not, write to the
     Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
     Boston, MA 02110-1301, USA.
 */

#include "platform.h"
#include "plibc.h"
#include "extractor.h"
#include "extractor_datasource.h"
#include "extractor_plugin_main.h"
#include "extractor_plugins.h"
#include "extractor_ipc.h"
#include "extractor_logging.h"

/**
 */
struct EXTRACTOR_SharedMemory
{

  /**
   * W32 handle of the shm into which data is uncompressed
   */
  HANDLE map;

  /**
   * Name of the shm
   */
  char shm_name[MAX_SHM_NAME + 1];

  /**
   * Pointer to the mapped region of the shm (covers the whole shm)
   */
  void *ptr;

  /**
   * Position within shm
   */
  int64_t pos;

  /**
   * Allocated size of the shm
   */
  int64_t shm_size;

  /**
   * Number of bytes in shm (<= shm_size)
   */
  size_t shm_buf_size;

  size_t shm_map_size;

  /**
   * Reference counter describing how many references share this SHM.
   */
  unsigned int rc;
};


/**
 * Definition of an IPC communication channel with
 * some plugin.
 */
struct EXTRACTOR_Channel
{

  /**
   * Process ID of the child process for this plugin. 0 for none.
   */
  HANDLE hProcess;

  /**
   * Pipe used to communicate information to the plugin child process.
   * NULL if not initialized.
   */
  HANDLE cpipe_in;

  /**
   * Handle of the shm object
   */
  HANDLE map_handle;

  /**
   * Pipe used to read information about extracted meta data from
   * the plugin child process.  -1 if not initialized.
   */
  HANDLE cpipe_out;

  /**
   * A structure for overlapped reads on W32.
   */
  OVERLAPPED ov_read;

  /**
   * A structure for overlapped writes on W32.
   */
  OVERLAPPED ov_write;

  /**
   * A write buffer for overlapped writes on W32
   */
  unsigned char *ov_write_buffer;

  /**
   * The plugin this channel is to communicate with.
   */
  struct EXTRACTOR_PluginList *plugin;

  /**
   * Memory segment shared with this process.
   */
  struct EXTRACTOR_SharedMemory *shm;

  void *old_buf;

  /**
   * Buffer for reading data from the plugin.
   */
  char *mdata;

  /**
   * Size of the 'mdata' buffer.
   */
  size_t mdata_size;

  /**
   * Number of valid bytes in the channel's buffer.
   */
  size_t size;
};


/**
 * Create a shared memory area.
 *
 * @param size size of the shared area
 * @return NULL on error
 */
struct EXTRACTOR_SharedMemory *
EXTRACTOR_IPC_shared_memory_create_ (size_t size)
{
  struct EXTRACTOR_SharedMemory *shm;
  const char *tpath = "Local\\";

  if (NULL == (shm = malloc (sizeof (struct EXTRACTOR_SharedMemory))))
    return NULL;

  snprintf (shm->shm_name, MAX_SHM_NAME,
            "%slibextractor-shm-%u-%u",
            tpath, getpid (),
            (unsigned int) random ());
  shm->map = CreateFileMapping (INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0,
                                size, shm->shm_name);
  shm->ptr = MapViewOfFile (shm->map, FILE_MAP_WRITE, 0, 0, size);
  if (shm->ptr == NULL)
  {
    CloseHandle (shm->map);
    free (shm);
    return NULL;
  }
  shm->shm_size = size;
  shm->rc = 0;
  return shm;
}


/**
 * Change the reference counter for this shm instance.
 *
 * @param shm instance to update
 * @param delta value to change RC by
 * @return new RC
 */
unsigned int
EXTRACTOR_IPC_shared_memory_change_rc_ (struct EXTRACTOR_SharedMemory *shm,
                                        int delta)
{
  shm->rc += delta;
  return shm->rc;
}


/**
 * Destroy shared memory area.
 *
 * @param shm memory area to destroy
 * @return NULL on error
 */
void
EXTRACTOR_IPC_shared_memory_destroy_ (struct EXTRACTOR_SharedMemory *shm)
{
  if (shm->ptr != NULL)
    UnmapViewOfFile (shm->ptr);
  if (shm->map != 0)
    CloseHandle (shm->map);
  free (shm);
}


/**
 * Initialize shared memory area from data source.
 *
 * @param shm memory area to initialize
 * @param ds data source to use for initialization
 * @param off offset to use in data source
 * @param size number of bytes to copy
 * @return -1 on error, otherwise number of bytes copied
 */
ssize_t
EXTRACTOR_IPC_shared_memory_set_ (struct EXTRACTOR_SharedMemory *shm,
                                  struct EXTRACTOR_Datasource *ds,
                                  uint64_t off,
                                  size_t size)
{
  if (-1 ==
      EXTRACTOR_datasource_seek_ (ds, off, SEEK_SET))
  {
    LOG ("Failed to set IPC memory due to seek error\n");
    return -1;
  }
  if (size > shm->shm_size)
    size = shm->shm_size;
  return EXTRACTOR_datasource_read_ (ds,
                                     shm->ptr,
                                     size);
}


/**
 * Query datasource for current position
 *
 * @param ds data source to query
 * @return current position in the datasource or UINT_MAX on error
 */
uint64_t
EXTRACTOR_datasource_get_pos_ (struct EXTRACTOR_Datasource *ds)
{
  int64_t pos = EXTRACTOR_datasource_seek_ (ds, 0, SEEK_CUR);
  if (-1 == pos)
    return UINT_MAX;
  return pos;
}


#ifndef PIPE_BUF
#define PIPE_BUF 512
#endif

/* Copyright Bob Byrnes  <byrnes <at> curl.com>
   http://permalink.gmane.org/gmane.os.cygwin.patches/2121
*/
/* Create a pipe, and return handles to the read and write ends,
   just like CreatePipe, but ensure that the write end permits
   FILE_READ_ATTRIBUTES access, on later versions of win32 where
   this is supported.  This access is needed by NtQueryInformationFile,
   which is used to implement select and nonblocking writes.
   Note that the return value is either NO_ERROR or GetLastError,
   unlike CreatePipe, which returns a bool for success or failure.  */
static int
create_selectable_pipe (PHANDLE read_pipe_ptr, PHANDLE write_pipe_ptr,
                        LPSECURITY_ATTRIBUTES sa_ptr, DWORD psize,
                        DWORD dwReadMode, DWORD dwWriteMode)
{
  /* Default to error. */
  *read_pipe_ptr = *write_pipe_ptr = INVALID_HANDLE_VALUE;

  HANDLE read_pipe = INVALID_HANDLE_VALUE, write_pipe = INVALID_HANDLE_VALUE;

  /* Ensure that there is enough pipe buffer space for atomic writes.  */
  if (psize < PIPE_BUF)
    psize = PIPE_BUF;

  char pipename[MAX_PATH];

  /* Retry CreateNamedPipe as long as the pipe name is in use.
   * Retrying will probably never be necessary, but we want
   * to be as robust as possible.  */
  while (1)
  {
    static volatile LONG pipe_unique_id;

    snprintf (pipename, sizeof pipename, "\\\\.\\pipe\\gnunet-%d-%ld",
              getpid (), InterlockedIncrement ((LONG *) &pipe_unique_id));
    /* Use CreateNamedPipe instead of CreatePipe, because the latter
     * returns a write handle that does not permit FILE_READ_ATTRIBUTES
     * access, on versions of win32 earlier than WinXP SP2.
     * CreatePipe also stupidly creates a full duplex pipe, which is
     * a waste, since only a single direction is actually used.
     * It's important to only allow a single instance, to ensure that
     * the pipe was not created earlier by some other process, even if
     * the pid has been reused.  We avoid FILE_FLAG_FIRST_PIPE_INSTANCE
     * because that is only available for Win2k SP2 and WinXP.  */read_pipe = CreateNamedPipeA (pipename, PIPE_ACCESS_INBOUND | dwReadMode,
                                  PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, 1,                                               /* max instances */
                                  psize,        /* output buffer size */
                                  psize,        /* input buffer size */
                                  NMPWAIT_USE_DEFAULT_WAIT, sa_ptr);

    if (read_pipe != INVALID_HANDLE_VALUE)
    {
      break;
    }

    DWORD err = GetLastError ();

    switch (err)
    {
    case ERROR_PIPE_BUSY:
      /* The pipe is already open with compatible parameters.
       * Pick a new name and retry.  */
      continue;
    case ERROR_ACCESS_DENIED:
      /* The pipe is already open with incompatible parameters.
       * Pick a new name and retry.  */
      continue;
    case ERROR_CALL_NOT_IMPLEMENTED:
      /* We are on an older Win9x platform without named pipes.
       * Return an anonymous pipe as the best approximation.  */
      if (CreatePipe (read_pipe_ptr, write_pipe_ptr, sa_ptr, psize))
      {
        return 0;
      }
      err = GetLastError ();
      return err;
    default:
      return err;
    }
    /* NOTREACHED */
  }

  /* Open the named pipe for writing.
   * Be sure to permit FILE_READ_ATTRIBUTES access.  */
  write_pipe = CreateFileA (pipename, GENERIC_WRITE | FILE_READ_ATTRIBUTES, 0,  /* share mode */
                            sa_ptr, OPEN_EXISTING, dwWriteMode, /* flags and attributes */
                            0); /* handle to template file */

  if (write_pipe == INVALID_HANDLE_VALUE)
  {
    /* Failure. */
    DWORD err = GetLastError ();

    CloseHandle (read_pipe);
    return err;
  }

  /* Success. */
  *read_pipe_ptr = read_pipe;
  *write_pipe_ptr = write_pipe;
  return 0;
}


/**
 * Communicates plugin data (library name, options) to the plugin
 * process. This is only necessary on W32, where this information
 * is not inherited by the plugin, because it is not forked.
 *
 * @param plugin plugin context
 *
 * @return 0 on success, -1 on failure
 */
static int
write_plugin_data (struct EXTRACTOR_PluginList *plugin,
                   struct EXTRACTOR_Channel *channel)
{
  size_t libname_len, shortname_len, opts_len;
  DWORD len;
  char *str;
  size_t total_len = 0;
  unsigned char *buf, *ptr;
  ssize_t write_result;

  libname_len = strlen (plugin->libname) + 1;
  total_len += sizeof (size_t) + libname_len;
  shortname_len = strlen (plugin->short_libname) + 1;
  total_len += sizeof (size_t) + shortname_len;
  if (plugin->plugin_options != NULL)
  {
    opts_len = strlen (plugin->plugin_options) + 1;
    total_len += opts_len;
  }
  else
  {
    opts_len = 0;
  }
  total_len += sizeof (size_t);

  buf = malloc (total_len);
  if (buf == NULL)
    return -1;
  ptr = buf;
  memcpy (ptr, &libname_len, sizeof (size_t));
  ptr += sizeof (size_t);
  memcpy (ptr, plugin->libname, libname_len);
  ptr += libname_len;
  memcpy (ptr, &shortname_len, sizeof (size_t));
  ptr += sizeof (size_t);
  memcpy (ptr, plugin->short_libname, shortname_len);
  ptr += shortname_len;
  memcpy (ptr, &opts_len, sizeof (size_t));
  ptr += sizeof (size_t);
  if (opts_len > 0)
  {
    memcpy (ptr, plugin->plugin_options, opts_len);
    ptr += opts_len;
  }
  write_result = EXTRACTOR_IPC_channel_send_ (channel, buf, total_len);
  free (buf);
  return total_len == write_result;
}


/**
 * Create a channel to communicate with a process wrapping
 * the plugin of the given name.  Starts the process as well.
 *
 * @param plugin the plugin
 * @param shm memory to share with the process
 * @return NULL on error, otherwise IPC channel
 */
struct EXTRACTOR_Channel *
EXTRACTOR_IPC_channel_create_ (struct EXTRACTOR_PluginList *plugin,
                               struct EXTRACTOR_SharedMemory *shm)
{
  struct EXTRACTOR_Channel *channel;
  HANDLE p1[2];
  HANDLE p2[2];
  struct InitMessage *init;
  size_t slen;

  STARTUPINFOA startup;
  PROCESS_INFORMATION proc;
  char cmd[MAX_PATH + 1];
  char arg1[10], arg2[10];
  HANDLE p10_os_inh = INVALID_HANDLE_VALUE;
  HANDLE p21_os_inh = INVALID_HANDLE_VALUE;
  SECURITY_ATTRIBUTES sa;

  if (NULL == (channel = malloc (sizeof (struct EXTRACTOR_Channel))))
  {
    LOG_STRERROR ("malloc");
    return NULL;
  }
  memset (channel, 0, sizeof (struct EXTRACTOR_Channel));
  channel->mdata_size = 1024;
  if (NULL == (channel->mdata = malloc (channel->mdata_size)))
  {
    LOG_STRERROR ("malloc");
    free (channel);
    return NULL;
  }
  channel->shm = shm;
  channel->plugin = plugin;
  channel->size = 0;

  sa.nLength = sizeof (sa);
  sa.lpSecurityDescriptor = NULL;
  sa.bInheritHandle = FALSE;

  if (0 != create_selectable_pipe (&p1[0], &p1[1], &sa, 1024,
                                   FILE_FLAG_OVERLAPPED, FILE_FLAG_OVERLAPPED))
  {
    LOG_STRERROR ("pipe");
    free (channel);
    return NULL;
  }
  if (0 != create_selectable_pipe (&p2[0], &p2[1], &sa, 1024,
                                   FILE_FLAG_OVERLAPPED, FILE_FLAG_OVERLAPPED))
  {
    LOG_STRERROR ("pipe");
    CloseHandle (p1[0]);
    CloseHandle (p1[1]);
    free (channel);
    return NULL;
  }

  if (! DuplicateHandle (GetCurrentProcess (), p1[0], GetCurrentProcess (),
                         &p10_os_inh, 0, TRUE, DUPLICATE_SAME_ACCESS)
      || ! DuplicateHandle (GetCurrentProcess (), p2[1], GetCurrentProcess (),
                            &p21_os_inh, 0, TRUE, DUPLICATE_SAME_ACCESS))
  {
    LOG_STRERROR ("DuplicateHandle");
    if (p10_os_inh != INVALID_HANDLE_VALUE)
      CloseHandle (p10_os_inh);
    if (p21_os_inh != INVALID_HANDLE_VALUE)
      CloseHandle (p21_os_inh);
    CloseHandle (p1[0]);
    CloseHandle (p1[1]);
    CloseHandle (p2[0]);
    CloseHandle (p2[1]);
    CloseHandle (p1[0]);
    CloseHandle (p1[1]);
    free (channel);
    return NULL;
  }

  memset (&proc, 0, sizeof (PROCESS_INFORMATION));
  memset (&startup, 0, sizeof (STARTUPINFOA));

  /* TODO: write our own plugin-hosting executable? rundll32, for once, has smaller than usual stack size.
   * Also, users might freak out seeing over 9000 rundll32 processes (seeing over 9000 processes named
   * "libextractor_plugin_helper" is probably less confusing).
   */
  snprintf (cmd, MAX_PATH,
            "rundll32.exe libextractor-3.dll,RundllEntryPoint@16 %lu %lu",
            p10_os_inh, p21_os_inh);
  cmd[MAX_PATH] = '\0';
  startup.cb = sizeof (STARTUPINFOA);
  if (CreateProcessA (NULL, cmd, NULL, NULL, TRUE, CREATE_SUSPENDED, NULL, NULL,
                      &startup, &proc))
  {
    channel->hProcess = proc.hProcess;
    ResumeThread (proc.hThread);
    CloseHandle (proc.hThread);
  }
  else
  {
    LOG_STRERROR ("CreateProcess");
    CloseHandle (p1[0]);
    CloseHandle (p1[1]);
    CloseHandle (p2[0]);
    CloseHandle (p2[1]);
    free (channel);
    return NULL;
  }
  CloseHandle (p1[0]);
  CloseHandle (p2[1]);
  CloseHandle (p10_os_inh);
  CloseHandle (p21_os_inh);

  channel->cpipe_in = p1[1];
  channel->cpipe_out = p2[0];

  memset (&channel->ov_read, 0, sizeof (OVERLAPPED));
  memset (&channel->ov_write, 0, sizeof (OVERLAPPED));

  channel->ov_write_buffer = NULL;

  channel->ov_write.hEvent = CreateEvent (NULL, TRUE, TRUE, NULL);
  channel->ov_read.hEvent = CreateEvent (NULL, TRUE, TRUE, NULL);

  if (! write_plugin_data (plugin, channel))
  {
    LOG_STRERROR ("write_plugin_data");
    EXTRACTOR_IPC_channel_destroy_ (channel);
    return NULL;
  }

  slen = strlen (shm->shm_name) + 1;
  if (NULL == (init = malloc (sizeof (struct InitMessage) + slen)))
  {
    LOG_STRERROR ("malloc");
    EXTRACTOR_IPC_channel_destroy_ (channel);
    return NULL;
  }
  init->opcode = MESSAGE_INIT_STATE;
  init->reserved = 0;
  init->reserved2 = 0;
  init->shm_name_length = slen;
  init->shm_map_size = shm->shm_size;
  memcpy (&init[1], shm->shm_name, slen);
  if (sizeof (struct InitMessage) + slen !=
      EXTRACTOR_IPC_channel_send_ (channel, init,
                                   sizeof (struct InitMessage) + slen))
  {
    LOG ("Failed to send INIT_STATE message to plugin\n");
    EXTRACTOR_IPC_channel_destroy_ (channel);
    free (init);
    return NULL;
  }
  free (init);
  return channel;
}


/**
 * Destroy communication channel with a plugin/process.  Also
 * destroys the process.
 *
 * @param channel channel to communicate with the plugin
 */
void
EXTRACTOR_IPC_channel_destroy_ (struct EXTRACTOR_Channel *channel)
{
  int status;

  CloseHandle (channel->cpipe_out);
  CloseHandle (channel->cpipe_in);
  CloseHandle (channel->ov_read.hEvent);
  CloseHandle (channel->ov_write.hEvent);
  if (channel->ov_write_buffer != NULL)
  {
    free (channel->ov_write_buffer);
    channel->ov_write_buffer = NULL;
  }
  if (NULL != channel->plugin)
    channel->plugin->channel = NULL;
  free (channel->mdata);
  WaitForSingleObject (channel->hProcess, 1000);
  TerminateProcess (channel->hProcess, 0);
  CloseHandle (channel->hProcess);
  free (channel);
}


/**
 * Send data via the given IPC channel (blocking).
 *
 * @param channel channel to communicate with the plugin
 * @param buf data to send
 * @param size number of bytes in buf to send
 * @return -1 on error, number of bytes sent on success
 *           (never does partial writes)
 */
ssize_t
EXTRACTOR_IPC_channel_send_ (struct EXTRACTOR_Channel *channel,
                             const void *data,
                             size_t size)
{
  DWORD written;
  DWORD err;
  BOOL bresult;
  const char *cdata = data;

  if (WAIT_OBJECT_0 != WaitForSingleObject (channel->ov_write.hEvent, INFINITE))
    return -1;

  ResetEvent (channel->ov_write.hEvent);

  if (channel->old_buf != NULL)
    free (channel->old_buf);

  channel->old_buf = malloc (size);
  if (channel->old_buf == NULL)
    return -1;

  memcpy (channel->old_buf, data, size);
  written = 0;
  channel->ov_write.Offset = 0;
  channel->ov_write.OffsetHigh = 0;
  channel->ov_write.Pointer = 0;
  channel->ov_write.Internal = 0;
  channel->ov_write.InternalHigh = 0;
  bresult = WriteFile (channel->cpipe_in, channel->old_buf, size, &written,
                       &channel->ov_write);

  if (bresult == TRUE)
  {
    SetEvent (channel->ov_write.hEvent);
    free (channel->old_buf);
    channel->old_buf = NULL;
    return written;
  }

  err = GetLastError ();
  if (err == ERROR_IO_PENDING)
    return size;
  SetEvent (channel->ov_write.hEvent);
  free (channel->old_buf);
  channel->old_buf = NULL;
  SetLastError (err);
  return -1;
}


/**
 * Receive data from any of the given IPC channels (blocking).
 * Wait for one of the plugins to reply.
 * Selects on plugin output pipes, runs 'receive_reply'
 * on each activated pipe until it gets a seek request
 * or a done message. Called repeatedly by the user until all pipes are dry or
 * broken.
 *
 * @param channels array of channels, channels that break may be set to NULL
 * @param num_channels length of the 'channels' array
 * @param proc function to call to process messages (may be called
 *             more than once)
 * @param proc_cls closure for 'proc'
 * @return -1 on error, 1 on success
 */
int
EXTRACTOR_IPC_channel_recv_ (struct EXTRACTOR_Channel **channels,
                             unsigned int num_channels,
                             EXTRACTOR_ChannelMessageProcessor proc,
                             void *proc_cls)
{
  DWORD ms;
  DWORD first_ready;
  DWORD dwresult;
  DWORD bytes_read;
  BOOL bresult;
  unsigned int i;
  unsigned int c;
  char *ndata;
  HANDLE events[MAXIMUM_WAIT_OBJECTS];
  int closed_channel;

  c = 0;
  for (i = 0; i < num_channels; i++)
  {
    if (NULL == channels[i])
      continue;
    if (MAXIMUM_WAIT_OBJECTS == c)
      return -1;
    if (WaitForSingleObject (channels[i]->ov_read.hEvent, 0) == WAIT_OBJECT_0)
    {
      ResetEvent (channels[i]->ov_read.hEvent);
      bresult = ReadFile (channels[i]->cpipe_out, &i, 0, &bytes_read,
                          &channels[i]->ov_read);
      if (bresult == TRUE)
      {
        SetEvent (channels[i]->ov_read.hEvent);
      }
      else
      {
        DWORD err = GetLastError ();
        if (err != ERROR_IO_PENDING)
          SetEvent (channels[i]->ov_read.hEvent);
      }
    }
    events[c] = channels[i]->ov_read.hEvent;
    c++;
  }

  if (c == 0)
    return 1; /* nothing left to do! */

  ms = 500;
  first_ready = WaitForMultipleObjects (c, events, FALSE, ms);
  if ((first_ready == WAIT_TIMEOUT) || (first_ready == WAIT_FAILED))
  {
    /* an error or timeout -> something's wrong or all plugins hung up */
    closed_channel = 0;
    for (i = 0; i < num_channels; i++)
    {
      struct EXTRACTOR_Channel *channel = channels[i];
      if (NULL == channel)
        continue;
      if (-1 == channel->plugin->seek_request)
      {
        /* plugin blocked for too long, kill the channel */
        LOG ("Channel blocked, closing channel to %s\n",
             channel->plugin->libname);
        channel->plugin->channel = NULL;
        channel->plugin->round_finished = 1;
        EXTRACTOR_IPC_channel_destroy_ (channel);
        channels[i] = NULL;
        closed_channel = 1;
      }
    }
    if (1 == closed_channel)
      return 1;
    LOG_STRERROR ("WaitForMultipleObjects");
    return -1;
  }

  i = 0;
  for (i = 0; i < num_channels; i++)
  {
    if (NULL == channels[i])
      continue;
    dwresult = WaitForSingleObject (channels[i]->ov_read.hEvent, 0);
    if (dwresult == WAIT_OBJECT_0)
    {
      int ret;
      if (channels[i]->mdata_size == channels[i]->size)
      {
        /* not enough space, need to grow allocation (if allowed) */
        if (MAX_META_DATA == channels[i]->mdata_size)
        {
          LOG ("Inbound message from channel too large, aborting\n");
          EXTRACTOR_IPC_channel_destroy_ (channels[i]);
          channels[i] = NULL;
        }
        channels[i]->mdata_size *= 2;
        if (channels[i]->mdata_size > MAX_META_DATA)
          channels[i]->mdata_size = MAX_META_DATA;
        if (NULL == (ndata = realloc (channels[i]->mdata,
                                      channels[i]->mdata_size)))
        {
          LOG_STRERROR ("realloc");
          EXTRACTOR_IPC_channel_destroy_ (channels[i]);
          channels[i] = NULL;
        }
        channels[i]->mdata = ndata;
      }
      bresult = ReadFile (channels[i]->cpipe_out,
                          &channels[i]->mdata[channels[i]->size],
                          channels[i]->mdata_size - channels[i]->size,
                          &bytes_read, NULL);
      if (bresult)
        ret = EXTRACTOR_IPC_process_reply_ (channels[i]->plugin,
                                            channels[i]->mdata,
                                            channels[i]->size + bytes_read,
                                            proc, proc_cls);
      if (! bresult || (-1 == ret))
      {
        DWORD error = GetLastError ();
        SetErrnoFromWinError (error);
        if (! bresult)
          LOG_STRERROR ("ReadFile");
        EXTRACTOR_IPC_channel_destroy_ (channels[i]);
        channels[i] = NULL;
      }
      else
      {
        memmove (channels[i]->mdata, &channels[i]->mdata[ret],
                 channels[i]->size + bytes_read - ret);
        channels[i]->size = channels[i]->size + bytes_read - ret;
      }
    }
  }
  return 1;
}