summaryrefslogtreecommitdiffstats
path: root/libkdenetwork/libgpgme-copy/assuan/assuan-pipe-connect.c
blob: ebae5ea3805acf7049959c6b8cbf2de85d6b5fec (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
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
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
/* assuan-pipe-connect.c - Establish a pipe connection (client) 
 * Copyright (C) 2001, 2002, 2003, 2005, 2006 Free Software Foundation, Inc.
 *
 * This file is part of Assuan.
 *
 * Assuan is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * Assuan 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
 * USA. 
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/types.h>
#ifndef HAVE_W32_SYSTEM
#include <sys/wait.h>
#else
#include <windows.h>
#endif

#include "assuan-defs.h"

/* Hacks for Slowaris.  */
#ifndef PF_LOCAL
# ifdef PF_UNIX
#  define PF_LOCAL PF_UNIX
# else
#  define PF_LOCAL AF_UNIX
# endif
#endif
#ifndef AF_LOCAL
# define AF_LOCAL AF_UNIX
#endif


#ifdef _POSIX_OPEN_MAX
#define MAX_OPEN_FDS _POSIX_OPEN_MAX
#else
#define MAX_OPEN_FDS 20
#endif

#ifdef HAVE_W32_SYSTEM
/* We assume that a HANDLE can be represented by an int which should
   be true for all i386 systems (HANDLE is defined as void *) and
   these are the only systems for which Windows is available.  Further
   we assume that -1 denotes an invalid handle.  */
#define fd_to_handle(a)  ((HANDLE)(a))
#define handle_to_fd(a)  ((int)(a))
#define pid_to_handle(a) ((HANDLE)(a))
#define handle_to_pid(a) ((int)(a))
#endif /*HAVE_W32_SYSTEM*/


/* This should be called to make sure that SIGPIPE gets ignored.  */
static void
fix_signals (void)
{
#ifndef _ASSUAN_NO_FIXED_SIGNALS
#ifndef HAVE_DOSISH_SYSTEM  /* No SIGPIPE for these systems.  */
  static int fixed_signals;

  if (!fixed_signals)
    { 
      struct sigaction act;
        
      sigaction (SIGPIPE, NULL, &act);
      if (act.sa_handler == SIG_DFL)
	{
	  act.sa_handler = SIG_IGN;
	  sigemptyset (&act.sa_tqmask);
	  act.sa_flags = 0;
	  sigaction (SIGPIPE, &act, NULL);
        }
      fixed_signals = 1;
      /* FIXME: This is not MT safe */
    }
#endif /*HAVE_DOSISH_SYSTEM*/
#endif /*!_ASSUAN_NO_FIXED_SIGNALS*/
}


#ifndef HAVE_W32_SYSTEM
static int
writen (int fd, const char *buffer, size_t length)
{
  while (length)
    {
      int nwritten = write (fd, buffer, length);
      
      if (nwritten < 0)
        {
          if (errno == EINTR)
            continue;
          return -1; /* write error */
        }
      length -= nwritten;
      buffer += nwritten;
    }
  return 0;  /* okay */
}
#endif

static int
do_finish (assuan_context_t ctx)
{
  if (ctx->inbound.fd != -1)
    {
      _assuan_close (ctx->inbound.fd);
      if (ctx->inbound.fd == ctx->outbound.fd)
        ctx->outbound.fd = -1;
      ctx->inbound.fd = -1;
    }
  if (ctx->outbound.fd != -1)
    {
      _assuan_close (ctx->outbound.fd);
      ctx->outbound.fd = -1;
    }
  if (ctx->pid != -1 && ctx->pid)
    {
#ifndef HAVE_W32_SYSTEM
#ifndef _ASSUAN_USE_DOUBLE_FORK
      if (!ctx->flags.no_waitpid)
        _assuan_waitpid (ctx->pid, NULL, 0); 
      ctx->pid = -1;
#endif
#endif /*!HAVE_W32_SYSTEM*/
    }
  return 0;
}

static void
do_deinit (assuan_context_t ctx)
{
  do_finish (ctx);
}


/* Helper for pipe_connect. */
static assuan_error_t
initial_handshake (assuan_context_t *ctx)
{
  int okay, off;
  assuan_error_t err;
  
  err = _assuan_read_from_server (*ctx, &okay, &off);
  if (err)
    _assuan_log_printf ("can't connect server: %s\n",
                        assuan_strerror (err));
  else if (okay != 1)
    {
      _assuan_log_printf ("can't connect server: `%s'\n",
                          (*ctx)->inbound.line);
      err = _assuan_error (ASSUAN_Connect_Failed);
    }

  if (err)
    {
      assuan_disconnect (*ctx);
      *ctx = NULL;
    }
  return err;
}


#ifndef HAVE_W32_SYSTEM
#define pipe_connect pipe_connect_unix
/* Unix version of the pipe connection code.  We use an extra macro to
   make ChangeLog entries easier. */
static assuan_error_t
pipe_connect_unix (assuan_context_t *ctx,
                   const char *name, const char *const argv[],
                   int *fd_child_list,
                   void (*atfork) (void *opaque, int reserved),
                   void *atforkvalue)
{
  assuan_error_t err;
  int rp[2];
  int wp[2];
  char mypidstr[50];

  if (!ctx || !name || !argv || !argv[0])
    return _assuan_error (ASSUAN_Invalid_Value);

  fix_signals ();

  sprintf (mypidstr, "%lu", (unsigned long)getpid ());

  if (pipe (rp) < 0)
    return _assuan_error (ASSUAN_General_Error);
  
  if (pipe (wp) < 0)
    {
      close (rp[0]);
      close (rp[1]);
      return _assuan_error (ASSUAN_General_Error);
    }

  err = _assuan_new_context (ctx);
  if (err)
    {
      close (rp[0]);
      close (rp[1]);
      close (wp[0]);
      close (wp[1]);
      return err;
    }
  (*ctx)->pipe_mode = 1;
  (*ctx)->inbound.fd  = rp[0];  /* Our inbound is read end of read pipe. */
  (*ctx)->outbound.fd = wp[1];  /* Our outbound is write end of write pipe. */
  (*ctx)->deinit_handler = do_deinit;
  (*ctx)->finish_handler = do_finish;

  /* FIXME: For GPGME we should better use _gpgme_io_spawn.  The PID
     stored here is actually soon useless.  */
  (*ctx)->pid = fork ();
  if ((*ctx)->pid < 0)
    {
      close (rp[0]);
      close (rp[1]);
      close (wp[0]);
      close (wp[1]);
      _assuan_release_context (*ctx); 
      return _assuan_error (ASSUAN_General_Error);
    }

  if ((*ctx)->pid == 0)
    {
#ifdef _ASSUAN_USE_DOUBLE_FORK      
      pid_t pid;

      if ((pid = fork ()) == 0)
#endif
	{
          int i, n;
          char errbuf[512];
          int *fdp;
          
          if (atfork)
            atfork (atforkvalue, 0);

          /* Dup handles to stdin/stdout. */
          if (rp[1] != STDOUT_FILENO)
            {
              if (dup2 (rp[1], STDOUT_FILENO) == -1)
                {
                  _assuan_log_printf ("dup2 failed in child: %s\n",
                                      strerror (errno));
                  _exit (4);
                }
            }
          if (wp[0] != STDIN_FILENO)
            {
              if (dup2 (wp[0], STDIN_FILENO) == -1)
                {
                  _assuan_log_printf ("dup2 failed in child: %s\n",
                                      strerror (errno));
                  _exit (4);
                }
            }

          /* Dup stderr to /dev/null unless it is in the list of FDs to be
             passed to the child. */
          fdp = fd_child_list;
          if (fdp)
            {
              for (; *fdp != -1 && *fdp != STDERR_FILENO; fdp++)
                ;
            }
          if (!fdp || *fdp == -1)
            {
              int fd = open ("/dev/null", O_WRONLY);
              if (fd == -1)
                {
                  _assuan_log_printf ("can't open `/dev/null': %s\n",
                                      strerror (errno));
                  _exit (4);
                }
              if (dup2 (fd, STDERR_FILENO) == -1)
                {
                  _assuan_log_printf ("dup2(dev/null, 2) failed: %s\n",
                                      strerror (errno));
                  _exit (4);
                }
            }


          /* Close all files which will not be duped and are not in the
             fd_child_list. */
          n = sysconf (_SC_OPEN_MAX);
          if (n < 0)
            n = MAX_OPEN_FDS;
          for (i=0; i < n; i++)
            {
              if ( i == STDIN_FILENO || i == STDOUT_FILENO
                   || i == STDERR_FILENO)
                continue;
              fdp = fd_child_list;
              if (fdp)
                {
                  while (*fdp != -1 && *fdp != i)
                    fdp++;
                }

              if (!(fdp && *fdp != -1))
                close(i);
            }
          errno = 0;

          /* We store our tqparents pid in the environment so that the
             execed assuan server is able to read the actual pid of the
             client.  The server can't use getppid because it might have
             been double forked before the assuan server has been
             initialized. */
          setenv ("_assuan_pipe_connect_pid", mypidstr, 1);

          /* Make sure that we never pass a connection fd variable
             when using a simple pipe.  */
          unsetenv ("_assuan_connection_fd");

          execv (name, (char *const *) argv); 
          /* oops - use the pipe to tell the tqparent about it */
          snprintf (errbuf, sizeof(errbuf)-1,
                    "ERR %d can't exec `%s': %.50s\n",
                    _assuan_error (ASSUAN_Problem_Starting_Server),
                    name, strerror (errno));
          errbuf[sizeof(errbuf)-1] = 0;
          writen (1, errbuf, strlen (errbuf));
          _exit (4);
        }
#ifdef _ASSUAN_USE_DOUBLE_FORK
      if (pid == -1)
	_exit (1);
      else
	_exit (0);
#endif
    }

#ifdef _ASSUAN_USE_DOUBLE_FORK
  _assuan_waitpid ((*ctx)->pid, NULL, 0);
  (*ctx)->pid = -1;
#endif

  close (rp[1]);
  close (wp[0]);

  return initial_handshake (ctx);
}
#endif /*!HAVE_W32_SYSTEM*/


#ifndef HAVE_W32_SYSTEM
/* This function is similar to pipe_connect but uses a socketpair and
   sets the I/O up to use sendmsg/recvmsg. */
static assuan_error_t
socketpair_connect (assuan_context_t *ctx,
                    const char *name, const char *const argv[],
                    int *fd_child_list,
                    void (*atfork) (void *opaque, int reserved),
                    void *atforkvalue)
{
  assuan_error_t err;
  int fds[2];
  char mypidstr[50];

  if (!ctx
      || (name && (!argv || !argv[0]))
      || (!name && argv))
    return _assuan_error (ASSUAN_Invalid_Value);

  fix_signals ();

  sprintf (mypidstr, "%lu", (unsigned long)getpid ());

  if ( socketpair (AF_LOCAL, SOCK_STREAM, 0, fds) )
    {
      _assuan_log_printf ("socketpair failed: %s\n", strerror (errno));
      return _assuan_error (ASSUAN_General_Error);
    }
  
  err = _assuan_new_context (ctx);
  if (err)
    {
      close (fds[0]);
      close (fds[1]);
      return err;
    }
  (*ctx)->pipe_mode = 1;
  (*ctx)->inbound.fd  = fds[0]; 
  (*ctx)->outbound.fd = fds[0]; 
  _assuan_init_uds_io (*ctx);
  (*ctx)->deinit_handler = _assuan_uds_deinit;
  (*ctx)->finish_handler = do_finish;

  (*ctx)->pid = fork ();
  if ((*ctx)->pid < 0)
    {
      close (fds[0]);
      close (fds[1]);
      _assuan_release_context (*ctx); 
      *ctx = NULL;
      return _assuan_error (ASSUAN_General_Error);
    }

  if ((*ctx)->pid == 0)
    {
#ifdef _ASSUAN_USE_DOUBLE_FORK      
      pid_t pid;

      if ((pid = fork ()) == 0)
#endif
	{
          int fd, i, n;
          char errbuf[512];
          int *fdp;
          
          if (atfork)
            atfork (atforkvalue, 0);

          /* Connect stdin and stdout to /dev/null. */
          fd = open ("/dev/null", O_RDONLY);
          if (fd == -1 || dup2 (fd, STDIN_FILENO) == -1)
            {
              _assuan_log_printf ("dup2(dev/null) failed: %s\n",
                                  strerror (errno));
              _exit (4);
            }
          fd = open ("/dev/null", O_WRONLY);
          if (fd == -1 || dup2 (fd, STDOUT_FILENO) == -1)
            {
              _assuan_log_printf ("dup2(dev/null) failed: %s\n",
                                  strerror (errno));
              _exit (4);
            }

          /* Dup stderr to /dev/null unless it is in the list of FDs to be
             passed to the child. */
          fdp = fd_child_list;
          if (fdp)
            {
              for (; *fdp != -1 && *fdp != STDERR_FILENO; fdp++)
                ;
            }
          if (!fdp || *fdp == -1)
            {
              fd = open ("/dev/null", O_WRONLY);
              if (fd == -1 || dup2 (fd, STDERR_FILENO) == -1)
                {
                  _assuan_log_printf ("dup2(dev/null) failed: %s\n",
                                      strerror (errno));
                  _exit (4);
                }
            }


          /* Close all files which will not be duped, are not in the
             fd_child_list and are not the connection fd. */
          n = sysconf (_SC_OPEN_MAX);
          if (n < 0)
            n = MAX_OPEN_FDS;
          for (i=0; i < n; i++)
            {
              if ( i == STDIN_FILENO || i == STDOUT_FILENO
                   || i == STDERR_FILENO || i == fds[1])
                continue;
              fdp = fd_child_list;
              if (fdp)
                {
                  while (*fdp != -1 && *fdp != i)
                    fdp++;
                }

              if (!(fdp && *fdp != -1))
                close(i);
            }
          errno = 0;

          /* We store our tqparents pid in the environment so that the
             execed assuan server is able to read the actual pid of the
             client.  The server can't use getppid becuase it might have
             been double forked before the assuan server has been
             initialized. */
          setenv ("_assuan_pipe_connect_pid", mypidstr, 1);

          /* Now set the environment variable used to convey the
             connection's file descriptor. */
          sprintf (mypidstr, "%d", fds[1]);
          if (setenv ("_assuan_connection_fd", mypidstr, 1))
            {
              _assuan_log_printf ("setenv failed: %s\n", strerror (errno));
              _exit (4);
            }

          if (!name && !argv)
            {
              /* No name and no args given, thus we don't do an exec
                 but continue the forked process.  */
              _assuan_release_context (*ctx);
              *ctx = NULL;
              return 0;
            }

          execv (name, (char *const *) argv); 
          /* oops - use the pipe to tell the tqparent about it */
          snprintf (errbuf, sizeof(errbuf)-1,
                    "ERR %d can't exec `%s': %.50s\n",
                    _assuan_error (ASSUAN_Problem_Starting_Server),
                    name, strerror (errno));
          errbuf[sizeof(errbuf)-1] = 0;
          writen (fds[1], errbuf, strlen (errbuf));
          _exit (4);
        }
#ifdef _ASSUAN_USE_DOUBLE_FORK
      if (pid == -1)
	_exit (1);
      else
	_exit (0);
#endif
    }


#ifdef _ASSUAN_USE_DOUBLE_FORK
  _assuan_waitpid ((*ctx)->pid, NULL, 0);
  (*ctx)->pid = -1;
#endif

  close (fds[1]);
  
  return initial_handshake (ctx);
}
#endif /*!HAVE_W32_SYSTEM*/



#ifdef HAVE_W32_SYSTEM
/* Build a command line for use with W32's CreateProcess.  On success
   CMDLINE gets the address of a newly allocated string.  */
static int
build_w32_commandline (char * const *argv, char **cmdline)
{
  int i, n;
  const char *s;
  char *buf, *p;

  *cmdline = NULL;
  n = 0;
  for (i=0; (s=argv[i]); i++)
    {
      n += strlen (s) + 1 + 2;  /* (1 space, 2 quoting */
      for (; *s; s++)
        if (*s == '\"')
          n++;  /* Need to double inner quotes.  */
    }
  n++;

  buf = p = xtrymalloc (n);
  if (!buf)
    return -1;

  for (i=0; argv[i]; i++) 
    {
      if (i)
        p = stpcpy (p, " ");
      if (!*argv[i]) /* Empty string. */
        p = stpcpy (p, "\"\"");
      else if (strpbrk (argv[i], " \t\n\v\f\""))
        {
          p = stpcpy (p, "\"");
          for (s=argv[i]; *s; s++)
            {
              *p++ = *s;
              if (*s == '\"')
                *p++ = *s;
            }
          *p++ = '\"';
          *p = 0;
        }
      else
        p = stpcpy (p, argv[i]);
    }

  *cmdline= buf;
  return 0;
}
#endif /*HAVE_W32_SYSTEM*/


#ifdef HAVE_W32_SYSTEM
/* Create pipe where one end end is inheritable.  */
static int
create_inheritable_pipe (int filedes[2], int for_write)
{
  HANDLE r, w, h;
  SECURITY_ATTRIBUTES sec_attr;

  memset (&sec_attr, 0, sizeof sec_attr );
  sec_attr.nLength = sizeof sec_attr;
  sec_attr.bInheritHandle = FALSE;
    
  if (!CreatePipe (&r, &w, &sec_attr, 0))
    {
      _assuan_log_printf ("CreatePipe failed: %s\n", w32_strerror (-1));
      return -1;
    }

  if (!DuplicateHandle (GetCurrentProcess(), for_write? r : w,
                        GetCurrentProcess(), &h, 0,
                        TRUE, DUPLICATE_SAME_ACCESS ))
    {
      _assuan_log_printf ("DuplicateHandle failed: %s\n", w32_strerror (-1));
      CloseHandle (r);
      CloseHandle (w);
      return -1;
    }
  if (for_write)
    {
      CloseHandle (r);
      r = h;
    }
  else
    {
      CloseHandle (w);
      w = h;
    }

  filedes[0] = handle_to_fd (r);
  filedes[1] = handle_to_fd (w);
  return 0;
}
#endif /*HAVE_W32_SYSTEM*/


#ifdef HAVE_W32_SYSTEM
#define pipe_connect pipe_connect_w32
/* W32 version of the pipe connection code. */
static assuan_error_t
pipe_connect_w32 (assuan_context_t *ctx,
                  const char *name, const char *const argv[],
                  int *fd_child_list,
                  void (*atfork) (void *opaque, int reserved),
                  void *atforkvalue)
{
  assuan_error_t err;
  int rp[2];
  int wp[2];
  char mypidstr[50];
  char *cmdline;
  SECURITY_ATTRIBUTES sec_attr;
  PROCESS_INFORMATION pi = 
    {
      NULL,      /* Returns process handle.  */
      0,         /* Returns primary thread handle.  */
      0,         /* Returns pid.  */
      0          /* Returns tid.  */
    };
  STARTUPINFO si;
  int fd, *fdp;
  HANDLE nullfd = INVALID_HANDLE_VALUE;

  if (!ctx || !name || !argv || !argv[0])
    return _assuan_error (ASSUAN_Invalid_Value);

  fix_signals ();

  sprintf (mypidstr, "%lu", (unsigned long)getpid ());

  /* Build the command line.  */
  if (build_w32_commandline (argv, &cmdline))
    return _assuan_error (ASSUAN_Out_Of_Core);

  /* Create thew two pipes. */
  if (create_inheritable_pipe (rp, 0))
    {
      xfree (cmdline);
      return _assuan_error (ASSUAN_General_Error);
    }
  
  if (create_inheritable_pipe (wp, 1))
    {
      CloseHandle (fd_to_handle (rp[0]));
      CloseHandle (fd_to_handle (rp[1]));
      xfree (cmdline);
      return _assuan_error (ASSUAN_General_Error);
    }

  
  err = _assuan_new_context (ctx);
  if (err)
    {
      CloseHandle (fd_to_handle (rp[0]));
      CloseHandle (fd_to_handle (rp[1]));
      CloseHandle (fd_to_handle (wp[0]));
      CloseHandle (fd_to_handle (wp[1]));
      xfree (cmdline);
      return _assuan_error (ASSUAN_General_Error);
    }

  (*ctx)->pipe_mode = 1;
  (*ctx)->inbound.fd  = rp[0];  /* Our inbound is read end of read pipe. */
  (*ctx)->outbound.fd = wp[1];  /* Our outbound is write end of write pipe. */
  (*ctx)->deinit_handler = do_deinit;
  (*ctx)->finish_handler = do_finish;


  /* fixme: Actually we should set the "_assuan_pipe_connect_pid" env
     variable.  However this requires us to write a full environment
     handler, because the strings are expected in sorted order.  The
     suggestion given in the MS Reference Library, to save the old
     value, changeit, create proces and restore it, is not thread
     safe.  */

  /* Start the process.  */
  memset (&sec_attr, 0, sizeof sec_attr );
  sec_attr.nLength = sizeof sec_attr;
  sec_attr.bInheritHandle = FALSE;
  
  memset (&si, 0, sizeof si);
  si.cb = sizeof (si);
  si.dwFlags = STARTF_USESTDHANDLES;
  si.hStdInput  = fd_to_handle (wp[0]);
  si.hStdOutput = fd_to_handle (rp[1]);

  /* Dup stderr to /dev/null unless it is in the list of FDs to be
     passed to the child. */
  fd = fileno (stderr);
  fdp = fd_child_list;
  if (fdp)
    {
      for (; *fdp != -1 && *fdp != fd; fdp++)
        ;
    }
  if (!fdp || *fdp == -1)
    {
      nullfd = CreateFile ("nul", GENERIC_WRITE,
                           FILE_SHARE_READ | FILE_SHARE_WRITE,
                           NULL, OPEN_EXISTING, 0, NULL);
      if (nullfd == INVALID_HANDLE_VALUE)
        {
          _assuan_log_printf ("can't open `nul': %s\n", w32_strerror (-1));
          CloseHandle (fd_to_handle (rp[0]));
          CloseHandle (fd_to_handle (rp[1]));
          CloseHandle (fd_to_handle (wp[0]));
          CloseHandle (fd_to_handle (wp[1]));
          xfree (cmdline);
          _assuan_release_context (*ctx); 
          return -1;
        }
      si.hStdError = nullfd;
    }
  else
    si.hStdError = fd_to_handle (_get_osfhandle (fd));


  /* Note: We inherit all handles flagged as inheritable.  This seems
     to be a security flaw but there seems to be no way of selecting
     handles to inherit. */
  /*   _assuan_log_printf ("CreateProcess, path=`%s' cmdline=`%s'\n", */
  /*                       name, cmdline); */
  if (!CreateProcess (name,                 /* Program to start.  */
                      cmdline,              /* Command line arguments.  */
                      &sec_attr,            /* Process security attributes.  */
                      &sec_attr,            /* Thread security attributes.  */
                      TRUE,                 /* Inherit handles.  */
                      (CREATE_DEFAULT_ERROR_MODE
                       | GetPriorityClass (GetCurrentProcess ())
                       | CREATE_SUSPENDED), /* Creation flags.  */
                      NULL,                 /* Environment.  */
                      NULL,                 /* Use current drive/directory.  */
                      &si,                  /* Startup information. */
                      &pi                   /* Returns process information.  */
                      ))
    {
      _assuan_log_printf ("CreateProcess failed: %s\n", w32_strerror (-1));
      CloseHandle (fd_to_handle (rp[0]));
      CloseHandle (fd_to_handle (rp[1]));
      CloseHandle (fd_to_handle (wp[0]));
      CloseHandle (fd_to_handle (wp[1]));
      if (nullfd != INVALID_HANDLE_VALUE)
        CloseHandle (nullfd);
      xfree (cmdline);
      _assuan_release_context (*ctx); 
      return _assuan_error (ASSUAN_General_Error);
    }
  xfree (cmdline);
  cmdline = NULL;
  if (nullfd != INVALID_HANDLE_VALUE)
    {
      CloseHandle (nullfd);
      nullfd = INVALID_HANDLE_VALUE;
    }

  CloseHandle (fd_to_handle (rp[1]));
  CloseHandle (fd_to_handle (wp[0]));

  /*   _assuan_log_printf ("CreateProcess ready: hProcess=%p hThread=%p" */
  /*                       " dwProcessID=%d dwThreadId=%d\n", */
  /*                       pi.hProcess, pi.hThread, */
  /*                       (int) pi.dwProcessId, (int) pi.dwThreadId); */

  ResumeThread (pi.hThread);
  CloseHandle (pi.hThread); 
  (*ctx)->pid = 0;  /* We don't use the PID. */
  CloseHandle (pi.hProcess); /* We don't need to wait for the process. */

  return initial_handshake (ctx);
}
#endif /*HAVE_W32_SYSTEM*/


/* Connect to a server over a pipe, creating the assuan context and
   returning it in CTX.  The server filename is NAME, the argument
   vector in ARGV.  FD_CHILD_LIST is a -1 terminated list of file
   descriptors not to close in the child.  */
assuan_error_t
assuan_pipe_connect (assuan_context_t *ctx, const char *name,
		     const char *const argv[], int *fd_child_list)
{
  return pipe_connect (ctx, name, argv, fd_child_list, NULL, NULL);
}



assuan_error_t
assuan_pipe_connect2 (assuan_context_t *ctx,
                      const char *name, const char *const argv[],
                      int *fd_child_list,
                      void (*atfork) (void *opaque, int reserved),
                      void *atforkvalue)
{
  return pipe_connect (ctx, name, argv, fd_child_list, atfork, atforkvalue);
}


/* Connect to a server over a full-duplex socket (i.e. created by
   socketpair), creating the assuan context and returning it in CTX.
   The server filename is NAME, the argument vector in ARGV.
   FD_CHILD_LIST is a -1 terminated list of file descriptors not to
   close in the child.  ATFORK is called in the child right after the
   fork; ATFORKVALUE is passed as the first argument and 0 is passed
   as the second argument. The ATFORK function should only act if the
   second value is 0.

   For now FLAGS may either take the value 0 to behave like
   assuan_pipe_connect2 or 1 to enable the described full-duplex
   socket behaviour.

   If NAME as well as ARGV are NULL, no exec is done but the same
   process is continued.  However all file descriptors are closed and
   some special environment variables are set. To let the caller
   detect whether the child or the tqparent continues, the child returns
   a CTX of NULL. */
assuan_error_t
assuan_pipe_connect_ext (assuan_context_t *ctx,
                         const char *name, const char *const argv[],
                         int *fd_child_list,
                         void (*atfork) (void *opaque, int reserved),
                         void *atforkvalue, unsigned int flags)
{
  if ((flags & 1))
    {
#ifdef HAVE_W32_SYSTEM
      return _assuan_error (ASSUAN_Not_Implemented);
#else
      return socketpair_connect (ctx, name, argv, fd_child_list,
                                 atfork, atforkvalue);
#endif
    }
  else
    return pipe_connect (ctx, name, argv, fd_child_list, atfork, atforkvalue);
}