Welcome to mirror list, hosted at ThFree Co, Russian Federation.

pipe.cc « fhandler « cygwin « winsup - cygwin.com/git/newlib-cygwin.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 1a97108b5d62c9f7e6f8287c6425aaa6faf731c3 (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
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
/* fhandler_pipe.cc: pipes for Cygwin.

This file is part of Cygwin.

This software is a copyrighted work licensed under the terms of the
Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
details. */

#include "winsup.h"
#include <stdlib.h>
#include <sys/socket.h>
#include "cygerrno.h"
#include "security.h"
#include "path.h"
#include "fhandler.h"
#include "dtable.h"
#include "cygheap.h"
#include "pinfo.h"
#include "shared_info.h"
#include "tls_pbuf.h"
#include <assert.h>

/* This is only to be used for writing.  When reading,
STATUS_PIPE_EMPTY simply means there's no data to be read. */
#define STATUS_PIPE_IS_CLOSED(status)	\
		({ NTSTATUS _s = (status); \
		   _s == STATUS_PIPE_CLOSING \
		   || _s == STATUS_PIPE_BROKEN \
		   || _s == STATUS_PIPE_EMPTY; })

fhandler_pipe_fifo::fhandler_pipe_fifo ()
  : fhandler_base (), pipe_buf_size (DEFAULT_PIPEBUFSIZE)
{
}


fhandler_pipe::fhandler_pipe ()
  : fhandler_pipe_fifo (), popen_pid (0)
{
  need_fork_fixup (true);
}

/* The following function is intended for fhandler_pipe objects
   created by the second version of fhandler_pipe::create below.  See
   the comment preceding the latter.

   In addition to setting the blocking mode of the pipe handle, it
   also sets the pipe's read mode to byte_stream unconditionally. */
void
fhandler_pipe::set_pipe_non_blocking (bool nonblocking)
{
  NTSTATUS status;
  IO_STATUS_BLOCK io;
  FILE_PIPE_INFORMATION fpi;

  fpi.ReadMode = FILE_PIPE_BYTE_STREAM_MODE;
  fpi.CompletionMode = nonblocking ? FILE_PIPE_COMPLETE_OPERATION
    : FILE_PIPE_QUEUE_OPERATION;
  status = NtSetInformationFile (get_handle (), &io, &fpi, sizeof fpi,
				 FilePipeInformation);
  if (!NT_SUCCESS (status))
    debug_printf ("NtSetInformationFile(FilePipeInformation): %y", status);
}

int
fhandler_pipe::init (HANDLE f, DWORD a, mode_t mode, int64_t uniq_id)
{
  /* FIXME: Have to clean this up someday
     FIXME: Do we have to check for both !get_win32_name() and
     !*get_win32_name()? */
  if ((!get_win32_name () || !*get_win32_name ()) && get_name ())
    {
      char *d;
      const char *s;
      char *hold_normalized_name = (char *) alloca (strlen (get_name ()) + 1);
      for (s = get_name (), d = hold_normalized_name; *s; s++, d++)
	if (*s == '/')
	  *d = '\\';
	else
	  *d = *s;
      *d = '\0';
      set_name (hold_normalized_name);
    }

  bool opened_properly = a & FILE_CREATE_PIPE_INSTANCE;
  a &= ~FILE_CREATE_PIPE_INSTANCE;
  fhandler_base::init (f, a, mode);
  close_on_exec (mode & O_CLOEXEC);
  set_ino (uniq_id);
  set_unique_id (uniq_id | !!(mode & GENERIC_WRITE));
  if (opened_properly)
    /* Set read pipe always nonblocking to allow signal handling
       even with FILE_SYNCHRONOUS_IO_NONALERT. */
    set_pipe_non_blocking (get_device () == FH_PIPER ?
			   true : is_nonblocking ());
  return 1;
}

extern "C" int sscanf (const char *, const char *, ...);

int
fhandler_pipe::open (int flags, mode_t mode)
{
  HANDLE proc, nio_hdl = NULL;
  int64_t uniq_id;
  fhandler_pipe *fh = NULL, *fhr = NULL, *fhw = NULL;
  size_t size;
  int pid, rwflags = (flags & O_ACCMODE);
  bool inh;
  bool got_one = false;

  if (sscanf (get_name (), "/proc/%d/fd/pipe:[%llu]",
	      &pid, (long long *) &uniq_id) < 2)
    {
      set_errno (ENOENT);
      return 0;
    }
  if (pid == myself->pid)
    {
      cygheap_fdenum cfd (true);
      while (cfd.next () >= 0)
	{
	  /* Windows doesn't allow to copy a pipe HANDLE with another access
	     mode.  So we check for read and write side of pipe and try to
	     find the one matching the requested access mode. */
	  if (cfd->get_unique_id () == uniq_id)
	    got_one = true;
	  else if (cfd->get_unique_id () == uniq_id + 1)
	    got_one = true;
	  else
	    continue;
	  if ((rwflags == O_RDONLY && !(cfd->get_access () & GENERIC_READ))
	      || (rwflags == O_WRONLY && !(cfd->get_access () & GENERIC_WRITE)))
	    continue;
	  copy_from (cfd);
	  set_handle (NULL);
	  pc.close_conv_handle ();
	  if (!cfd->dup (this, flags))
	    return 1;
	  return 0;
	}
      /* Found the pipe but access mode didn't match? EACCES.
	 Otherwise ENOENT */
      set_errno (got_one ? EACCES : ENOENT);
      return 0;
    }

  pinfo p (pid);
  if (!p)
    {
      set_errno (ESRCH);
      return 0;
    }
  if (!(proc = OpenProcess (PROCESS_DUP_HANDLE, false, p->dwProcessId)))
    {
      __seterrno ();
      return 0;
    }
  fhr = p->pipe_fhandler (uniq_id, size);
  if (fhr && rwflags == O_RDONLY)
    fh = fhr;
  else
    {
      fhw = p->pipe_fhandler (uniq_id + 1, size);
      if (fhw && rwflags == O_WRONLY)
	fh = fhw;
    }
  if (!fh)
    {
      /* Too bad, but Windows only allows the same access mode when dup'ing
	 the pipe. */
      set_errno (fhr || fhw ? EACCES : ENOENT);
      goto out;
    }
  inh = !(flags & O_CLOEXEC);
  if (!DuplicateHandle (proc, fh->get_handle (), GetCurrentProcess (),
			&nio_hdl, 0, inh, DUPLICATE_SAME_ACCESS))
    {
      __seterrno ();
      goto out;
    }
  init (nio_hdl, fh->get_access (), mode & O_TEXT ?: O_BINARY,
	fh->get_plain_ino ());
  cfree (fh);
  CloseHandle (proc);
  return 1;
out:
  if (nio_hdl)
    CloseHandle (nio_hdl);
  if (fh)
    free (fh);
  if (proc)
    CloseHandle (proc);
  return 0;
}

bool
fhandler_pipe::open_setup (int flags)
{
  bool read_mtx_created = false;

  if (!fhandler_base::open_setup (flags))
    goto err;
  if (get_dev () == FH_PIPER && !read_mtx)
    {
      SECURITY_ATTRIBUTES *sa = sec_none_cloexec (flags);
      read_mtx = CreateMutex (sa, FALSE, NULL);
      if (read_mtx)
	read_mtx_created = true;
      else
	{
	  debug_printf ("CreateMutex read_mtx failed: %E");
	  goto err;
	}
    }
  if (!hdl_cnt_mtx)
    {
      SECURITY_ATTRIBUTES *sa = sec_none_cloexec (flags);
      hdl_cnt_mtx = CreateMutex (sa, FALSE, NULL);
      if (!hdl_cnt_mtx)
	{
	  debug_printf ("CreateMutex hdl_cnt_mtx failed: %E");
	  goto err_close_read_mtx;
	}
    }
  return true;

err_close_read_mtx:
  if (read_mtx_created)
    CloseHandle (read_mtx);
err:
  return false;
}

off_t
fhandler_pipe::lseek (off_t offset, int whence)
{
  debug_printf ("(%D, %d)", offset, whence);
  set_errno (ESPIPE);
  return -1;
}

int
fhandler_pipe::fadvise (off_t offset, off_t length, int advice)
{
  return ESPIPE;
}

int
fhandler_pipe::fallocate (int mode, off_t offset, off_t length)
{
  return (mode & __FALLOC_FL_TRUNCATE) ? EINVAL : ESPIPE;
}

char *
fhandler_pipe::get_proc_fd_name (char *buf)
{
  __small_sprintf (buf, "pipe:[%U]", get_plain_ino ());
  return buf;
}

void
fhandler_pipe::release_select_sem (const char *from)
{
  LONG n_release;
  if (get_dev () == FH_PIPER) /* Number of select() and writer */
    n_release = get_obj_handle_count (select_sem)
      - get_obj_handle_count (read_mtx);
  else /* Number of select() call and reader */
    n_release = get_obj_handle_count (select_sem)
      - get_obj_handle_count (get_handle ());
  debug_printf("%s(%s) release %d", from,
	       get_dev () == FH_PIPER ? "PIPER" : "PIPEW", n_release);
  if (n_release)
    ReleaseSemaphore (select_sem, n_release, NULL);
}

void
fhandler_pipe::raw_read (void *ptr, size_t& len)
{
  size_t nbytes = 0;
  NTSTATUS status = STATUS_SUCCESS;
  IO_STATUS_BLOCK io;
  ULONGLONG t0 = GetTickCount64 (); /* Init timer */
  const ULONGLONG t0_threshold = 20;

  if (!len)
    return;

  DWORD timeout = is_nonblocking () ? 0 : INFINITE;
  DWORD waitret = cygwait (read_mtx, timeout);
  switch (waitret)
    {
    case WAIT_OBJECT_0:
      break;
    case WAIT_TIMEOUT:
      set_errno (EAGAIN);
      len = (size_t) -1;
      return;
    case WAIT_SIGNALED:
      set_errno (EINTR);
      len = (size_t) -1;
      return;
    case WAIT_CANCELED:
      pthread::static_cancel_self ();
      /* NOTREACHED */
    default:
      /* Should not reach here. */
      __seterrno ();
      len = (size_t) -1;
      return;
    }
  while (nbytes < len)
    {
      ULONG_PTR nbytes_now = 0;
      ULONG len1 = (ULONG) (len - nbytes);
      DWORD select_sem_timeout = 0;

      FILE_PIPE_LOCAL_INFORMATION fpli;
      status = NtQueryInformationFile (get_handle (), &io,
				       &fpli, sizeof (fpli),
				       FilePipeLocalInformation);
      if (NT_SUCCESS (status))
	{
	  if (fpli.ReadDataAvailable == 0 && nbytes != 0)
	    break;
	}
      else if (nbytes != 0)
	break;
      status = NtReadFile (get_handle (), NULL, NULL, NULL, &io, ptr,
			   len1, NULL, NULL);
      if (isclosed ())  /* A signal handler might have closed the fd. */
	{
	  set_errno (EBADF);
	  nbytes = (size_t) -1;
	}
      else if (NT_SUCCESS (status) || status == STATUS_BUFFER_OVERFLOW)
	{
	  nbytes_now = io.Information;
	  ptr = ((char *) ptr) + nbytes_now;
	  nbytes += nbytes_now;
	  if (select_sem && nbytes_now > 0)
	    release_select_sem ("raw_read");
	}
      else
	{
	  /* Some errors are not really errors.  Detect such cases here.  */
	  switch (status)
	    {
	    case STATUS_END_OF_FILE:
	    case STATUS_PIPE_BROKEN:
	      /* This is really EOF.  */
	      break;
	    case STATUS_PIPE_LISTENING:
	    case STATUS_PIPE_EMPTY:
	      if (nbytes != 0)
		break;
	      if (is_nonblocking ())
		{
		  set_errno (EAGAIN);
		  nbytes = (size_t) -1;
		  break;
		}
	      /* If the pipe is a non-cygwin pipe, select_sem trick
		 does not work. As a result, the following cygwait()
		 will return only after timeout occurs. This causes
		 performance degradation. However, setting timeout
		 to zero causes high CPU load. So, set timeout to
		 non-zero only when select_sem is valid or pipe is
		 not ready to read for more than t0_threshold.
		 This prevents both the performance degradation and
		 the high CPU load. */
	      if (select_sem || GetTickCount64 () - t0 > t0_threshold)
		select_sem_timeout = 1;
	      waitret = cygwait (select_sem, select_sem_timeout);
	      if (waitret == WAIT_CANCELED)
		pthread::static_cancel_self ();
	      else if (waitret == WAIT_SIGNALED)
		{
		  set_errno (EINTR);
		  nbytes = (size_t) -1;
		  break;
		}
	      continue;
	    default:
	      __seterrno_from_nt_status (status);
	      nbytes = (size_t) -1;
	      break;
	    }
	}

      if ((nbytes_now == 0 && !NT_SUCCESS (status))
	  || status == STATUS_BUFFER_OVERFLOW)
	break;
    }
  ReleaseMutex (read_mtx);
  len = nbytes;
}

bool
fhandler_pipe::reader_closed ()
{
  if (!query_hdl)
    return false;
  WaitForSingleObject (hdl_cnt_mtx, INFINITE);
  int n_reader = get_obj_handle_count (query_hdl);
  int n_writer = get_obj_handle_count (get_handle ());
  ReleaseMutex (hdl_cnt_mtx);
  return n_reader == n_writer;
}

ssize_t
fhandler_pipe_fifo::raw_write (const void *ptr, size_t len)
{
  size_t nbytes = 0;
  ULONG chunk;
  NTSTATUS status = STATUS_SUCCESS;
  IO_STATUS_BLOCK io;
  HANDLE evt;

  if (!len)
    return 0;

  if (reader_closed ())
    {
      set_errno (EPIPE);
      raise (SIGPIPE);
      return -1;
    }

  if (len <= pipe_buf_size || pipe_buf_size == 0)
    chunk = len;
  else if (is_nonblocking ())
    chunk = len = pipe_buf_size;
  else
    chunk = pipe_buf_size;

  if (!(evt = CreateEvent (NULL, false, false, NULL)))
    {
      __seterrno ();
      return -1;
    }

  /* Write in chunks, accumulating a total.  If there's an error, just
     return the accumulated total unless the first write fails, in
     which case return -1. */
  while (nbytes < len)
    {
      ULONG_PTR nbytes_now = 0;
      size_t left = len - nbytes;
      ULONG len1;
      DWORD waitret = WAIT_OBJECT_0;

      if (left > chunk)
	len1 = chunk;
      else
	len1 = (ULONG) left;
      /* NtWriteFile returns success with # of bytes written == 0 if writing
         on a non-blocking pipe fails because the pipe buffer doesn't have
	 sufficient space.

	 POSIX requires
	 - A write request for {PIPE_BUF} or fewer bytes shall have the
	   following effect: if there is sufficient space available in the
	   pipe, write() shall transfer all the data and return the number
	   of bytes requested. Otherwise, write() shall transfer no data and
	   return -1 with errno set to [EAGAIN].

	 - A write request for more than {PIPE_BUF} bytes shall cause one
	   of the following:

	  - When at least one byte can be written, transfer what it can and
	    return the number of bytes written. When all data previously
	    written to the pipe is read, it shall transfer at least {PIPE_BUF}
	    bytes.

	  - When no data can be written, transfer no data, and return -1 with
	    errno set to [EAGAIN]. */
      while (len1 > 0)
	{
	  status = NtWriteFile (get_handle (), evt, NULL, NULL, &io,
				(PVOID) ptr, len1, NULL, NULL);
	  if (status == STATUS_PENDING)
	    {
	      while (WAIT_TIMEOUT ==
		     (waitret = cygwait (evt, (DWORD) 0, cw_cancel | cw_sig)))
		{
		  if (reader_closed ())
		    {
		      CancelIo (get_handle ());
		      set_errno (EPIPE);
		      raise (SIGPIPE);
		      goto out;
		    }
		  else
		    cygwait (select_sem, 10);
		}
	      /* If io.Status is STATUS_CANCELLED after CancelIo, IO has
		 actually been cancelled and io.Information contains the
		 number of bytes processed so far.
		 Otherwise IO has been finished regulary and io.Status
		 contains valid success or error information. */
	      CancelIo (get_handle ());
	      if (waitret == WAIT_SIGNALED && io.Status != STATUS_CANCELLED)
		waitret = WAIT_OBJECT_0;

	      if (waitret == WAIT_CANCELED)
		status = STATUS_THREAD_CANCELED;
	      else if (waitret == WAIT_SIGNALED)
		status = STATUS_THREAD_SIGNALED;
	      else
		status = io.Status;
	    }
	  if (!is_nonblocking () || !NT_SUCCESS (status) || io.Information > 0
	      || len <= PIPE_BUF)
	    break;
	  len1 >>= 1;
	}
      if (isclosed ())  /* A signal handler might have closed the fd. */
	{
	  if (waitret == WAIT_OBJECT_0)
	    set_errno (EBADF);
	  else
	    __seterrno ();
	}
      else if (NT_SUCCESS (status)
	       || status == STATUS_THREAD_CANCELED
	       || status == STATUS_THREAD_SIGNALED)
	{
	  nbytes_now = io.Information;
	  ptr = ((char *) ptr) + nbytes_now;
	  nbytes += nbytes_now;
	  if (select_sem && nbytes_now > 0)
	    release_select_sem ("raw_write");
	  /* 0 bytes returned?  EAGAIN.  See above. */
	  if (NT_SUCCESS (status) && nbytes == 0)
	    set_errno (EAGAIN);
	}
      else if (STATUS_PIPE_IS_CLOSED (status))
	{
	  set_errno (EPIPE);
	  raise (SIGPIPE);
	}
      else
	__seterrno_from_nt_status (status);

      if (nbytes_now == 0)
	break;
    }
out:
  CloseHandle (evt);
  if (status == STATUS_THREAD_SIGNALED && nbytes == 0)
    set_errno (EINTR);
  else if (status == STATUS_THREAD_CANCELED)
    pthread::static_cancel_self ();
  return nbytes ?: -1;
}

void
fhandler_pipe::set_close_on_exec (bool val)
{
  fhandler_base::set_close_on_exec (val);
  if (read_mtx)
    set_no_inheritance (read_mtx, val);
  if (select_sem)
    set_no_inheritance (select_sem, val);
  if (query_hdl)
    set_no_inheritance (query_hdl, val);
  set_no_inheritance (hdl_cnt_mtx, val);
}

void
fhandler_pipe::fixup_after_fork (HANDLE parent)
{
  fork_fixup (parent, hdl_cnt_mtx, "hdl_cnt_mtx");
  WaitForSingleObject (hdl_cnt_mtx, INFINITE);
  if (read_mtx)
    fork_fixup (parent, read_mtx, "read_mtx");
  if (select_sem)
    fork_fixup (parent, select_sem, "select_sem");
  if (query_hdl)
    fork_fixup (parent, query_hdl, "query_hdl");
  if (query_hdl_close_req_evt)
    fork_fixup (parent, query_hdl_close_req_evt, "query_hdl_close_req_evt");

  fhandler_base::fixup_after_fork (parent);
  ReleaseMutex (hdl_cnt_mtx);
}

int
fhandler_pipe::dup (fhandler_base *child, int flags)
{
  fhandler_pipe *ftp = (fhandler_pipe *) child;
  ftp->set_popen_pid (0);

  int res = 0;
  WaitForSingleObject (hdl_cnt_mtx, INFINITE);
  if (fhandler_base::dup (child, flags))
    res = -1;
  else if (read_mtx &&
	   !DuplicateHandle (GetCurrentProcess (), read_mtx,
			     GetCurrentProcess (), &ftp->read_mtx,
			     0, !(flags & O_CLOEXEC), DUPLICATE_SAME_ACCESS))
    {
      __seterrno ();
      ftp->close ();
      res = -1;
    }
  else if (select_sem &&
	   !DuplicateHandle (GetCurrentProcess (), select_sem,
			     GetCurrentProcess (), &ftp->select_sem,
			     0, !(flags & O_CLOEXEC), DUPLICATE_SAME_ACCESS))
    {
      __seterrno ();
      ftp->close ();
      res = -1;
    }
  else if (query_hdl &&
	   !DuplicateHandle (GetCurrentProcess (), query_hdl,
			     GetCurrentProcess (), &ftp->query_hdl,
			     0, !(flags & O_CLOEXEC), DUPLICATE_SAME_ACCESS))
    {
      __seterrno ();
      ftp->close ();
      res = -1;
    }
  else if (!DuplicateHandle (GetCurrentProcess (), hdl_cnt_mtx,
			     GetCurrentProcess (), &ftp->hdl_cnt_mtx,
			     0, !(flags & O_CLOEXEC), DUPLICATE_SAME_ACCESS))
    {
      __seterrno ();
      ftp->close ();
      res = -1;
    }
  else if (query_hdl_close_req_evt &&
	   !DuplicateHandle (GetCurrentProcess (), query_hdl_close_req_evt,
			     GetCurrentProcess (),
			     &ftp->query_hdl_close_req_evt,
			     0, !(flags & O_CLOEXEC), DUPLICATE_SAME_ACCESS))
    {
      __seterrno ();
      ftp->close ();
      res = -1;
    }
  ReleaseMutex (hdl_cnt_mtx);

  debug_printf ("res %d", res);
  return res;
}

int
fhandler_pipe::close ()
{
  if (select_sem)
    {
      release_select_sem ("close");
      CloseHandle (select_sem);
    }
  if (read_mtx)
    CloseHandle (read_mtx);
  WaitForSingleObject (hdl_cnt_mtx, INFINITE);
  if (query_hdl)
    CloseHandle (query_hdl);
  if (query_hdl_close_req_evt)
    CloseHandle (query_hdl_close_req_evt);
  int ret = fhandler_base::close ();
  ReleaseMutex (hdl_cnt_mtx);
  CloseHandle (hdl_cnt_mtx);
  if (query_hdl_proc)
    CloseHandle (query_hdl_proc);
  return ret;
}

#define PIPE_INTRO "\\\\.\\pipe\\cygwin-"

/* 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 0 or GetLastError,
   unlike CreatePipe, which returns a bool for success or failure.  */
DWORD
fhandler_pipe::create (LPSECURITY_ATTRIBUTES sa_ptr, PHANDLE r, PHANDLE w,
		       DWORD psize, const char *name, DWORD open_mode,
		       int64_t *unique_id)
{
  /* Default to error. */
  if (r)
    *r = NULL;
  if (w)
    *w = NULL;

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

  char pipename[MAX_PATH];
  size_t len = __small_sprintf (pipename, PIPE_INTRO "%S-",
				      &cygheap->installation_key);
  DWORD pipe_mode = PIPE_READMODE_BYTE | PIPE_REJECT_REMOTE_CLIENTS;
  if (!name)
    pipe_mode |= pipe_byte ? PIPE_TYPE_BYTE : PIPE_TYPE_MESSAGE;
  else
    pipe_mode |= PIPE_TYPE_MESSAGE;

  if (!name || (open_mode & PIPE_ADD_PID))
    {
      len += __small_sprintf (pipename + len, "%u-", GetCurrentProcessId ());
      open_mode &= ~PIPE_ADD_PID;
    }

  if (name)
    len += __small_sprintf (pipename + len, "%s", name);

  open_mode |= PIPE_ACCESS_INBOUND | FILE_FLAG_FIRST_PIPE_INSTANCE;

  /* 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.  */
  DWORD err = 0;
  while (r && !*r)
    {
      static volatile ULONG pipe_unique_id;
      if (!name)
	{
	  LONG id = InterlockedIncrement ((LONG *) &pipe_unique_id);
	  __small_sprintf (pipename + len, "pipe-%p", id);
	  if (unique_id)
	    *unique_id = ((int64_t) id << 32 | GetCurrentProcessId ());
	}

      debug_printf ("name %s, size %u, mode %s", pipename, psize,
		    (pipe_mode & PIPE_TYPE_MESSAGE)
		    ? "PIPE_TYPE_MESSAGE" : "PIPE_TYPE_BYTE");

      /* 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.

	 Note that the write side of the pipe is opened as PIPE_TYPE_MESSAGE.
	 This *seems* to more closely mimic Linux pipe behavior and is
	 definitely required for pty handling since fhandler_pty_master
	 writes to the pipe in chunks, terminated by newline when CANON mode
	 is specified.  */
      *r = CreateNamedPipe (pipename, open_mode, pipe_mode, 1, psize,
			   psize, NMPWAIT_USE_DEFAULT_WAIT, sa_ptr);

      if (*r != INVALID_HANDLE_VALUE)
	{
	  debug_printf ("pipe read handle %p", *r);
	  err = 0;
	  break;
	}

      err = GetLastError ();
      switch (err)
	{
	case ERROR_PIPE_BUSY:
	  /* The pipe is already open with compatible parameters.
	     Pick a new name and retry.  */
	  debug_printf ("pipe busy", !name ? ", retrying" : "");
	  if (!name)
	    *r = NULL;
	  break;
	case ERROR_ACCESS_DENIED:
	  /* The pipe is already open with incompatible parameters.
	     Pick a new name and retry.  */
	  debug_printf ("pipe access denied%s", !name ? ", retrying" : "");
	  if (!name)
	    *r = NULL;
	  break;
	default:
	  {
	    err = GetLastError ();
	    debug_printf ("failed, %E");
	  }
	}
    }

  if (err)
    {
      *r = NULL;
      return err;
    }

  if (!w)
    debug_printf ("pipe write handle NULL");
  else
    {
      debug_printf ("CreateFile: name %s", pipename);

      /* Open the named pipe for writing.
	 Be sure to permit FILE_READ_ATTRIBUTES access.  */
      DWORD access = GENERIC_WRITE | FILE_READ_ATTRIBUTES;
      if ((open_mode & PIPE_ACCESS_DUPLEX) == PIPE_ACCESS_DUPLEX)
	access |= GENERIC_READ | FILE_WRITE_ATTRIBUTES;
      *w = CreateFile (pipename, access, 0, sa_ptr, OPEN_EXISTING,
		      open_mode & FILE_FLAG_OVERLAPPED, 0);

      if (!*w || *w == INVALID_HANDLE_VALUE)
	{
	  /* Failure. */
	  DWORD err = GetLastError ();
	  debug_printf ("CreateFile failed, r %p, %E", r);
	  if (r)
	    CloseHandle (*r);
	  *w = NULL;
	  return err;
	}

      debug_printf ("pipe write handle %p", *w);
    }

  /* Success. */
  return 0;
}

inline static bool
is_running_as_service (void)
{
  return check_token_membership (well_known_service_sid)
    || cygheap->user.saved_sid () == well_known_system_sid;
}

/* The next version of fhandler_pipe::create used to call the previous
   version.  But the read handle created by the latter doesn't have
   FILE_WRITE_ATTRIBUTES access unless the pipe is created with
   PIPE_ACCESS_DUPLEX, and it doesn't seem possible to add that access
   right.  This causes set_pipe_non_blocking to fail.

   To fix this we will define a helper function 'nt_create' that is
   similar to the above fhandler_pipe::create but uses
   NtCreateNamedPipeFile instead of CreateNamedPipe; this gives more
   flexibility in setting the access rights.  We will use this helper
   function in the version of fhandler_pipe::create below, which
   suffices for all of our uses of set_pipe_non_blocking.  For
   simplicity, nt_create will omit the 'open_mode' and 'name'
   parameters, which aren't needed for our purposes.  */

static int nt_create (LPSECURITY_ATTRIBUTES, HANDLE &, HANDLE &, DWORD,
		      int64_t *);

int
fhandler_pipe::create (fhandler_pipe *fhs[2], unsigned psize, int mode)
{
  HANDLE r, w;
  SECURITY_ATTRIBUTES *sa = sec_none_cloexec (mode);
  int res = -1;
  int64_t unique_id;

  int ret = nt_create (sa, r, w, psize, &unique_id);
  if (ret)
    {
      __seterrno_from_win_error (ret);
      goto out;
    }
  if ((fhs[0] = (fhandler_pipe *) build_fh_dev (*piper_dev)) == NULL)
    goto err_close_rw_handle;
  if ((fhs[1] = (fhandler_pipe *) build_fh_dev (*pipew_dev)) == NULL)
    goto err_delete_fhs0;
  mode |= mode & O_TEXT ?: O_BINARY;
  fhs[0]->init (r, FILE_CREATE_PIPE_INSTANCE | GENERIC_READ, mode, unique_id);
  fhs[1]->init (w, FILE_CREATE_PIPE_INSTANCE | GENERIC_WRITE, mode, unique_id);

  /* For the read side of the pipe, add a mutex.  See raw_read for the
     usage. */
  fhs[0]->read_mtx = CreateMutexW (sa, FALSE, NULL);
  if (!fhs[0]->read_mtx)
    goto err_delete_fhs1;

  fhs[0]->select_sem = CreateSemaphore (sa, 0, INT32_MAX, NULL);
  if (!fhs[0]->select_sem)
    goto err_close_read_mtx;
  if (!DuplicateHandle (GetCurrentProcess (), fhs[0]->select_sem,
			GetCurrentProcess (), &fhs[1]->select_sem,
			0, sa->bInheritHandle, DUPLICATE_SAME_ACCESS))
    goto err_close_select_sem0;

  if (is_running_as_service () &&
      !DuplicateHandle (GetCurrentProcess (), r,
			GetCurrentProcess (), &fhs[1]->query_hdl,
			FILE_READ_DATA, sa->bInheritHandle, 0))
    goto err_close_select_sem1;

  fhs[0]->hdl_cnt_mtx = CreateMutexW (sa, FALSE, NULL);
  if (!fhs[0]->hdl_cnt_mtx)
    goto err_close_query_hdl;
  if (!DuplicateHandle (GetCurrentProcess (), fhs[0]->hdl_cnt_mtx,
			GetCurrentProcess (), &fhs[1]->hdl_cnt_mtx,
			0, sa->bInheritHandle, DUPLICATE_SAME_ACCESS))
    goto err_close_hdl_cnt_mtx0;

  if (fhs[1]->query_hdl)
    {
      fhs[1]->query_hdl_close_req_evt = CreateEvent (sa, TRUE, FALSE, NULL);
      if (!fhs[1]->query_hdl_close_req_evt)
	goto err_close_hdl_cnt_mtx1;
    }

  res = 0;
  goto out;

err_close_hdl_cnt_mtx1:
  CloseHandle (fhs[1]->hdl_cnt_mtx);
err_close_hdl_cnt_mtx0:
  CloseHandle (fhs[0]->hdl_cnt_mtx);
err_close_query_hdl:
  if (fhs[1]->query_hdl)
    CloseHandle (fhs[1]->query_hdl);
err_close_select_sem1:
  CloseHandle (fhs[1]->select_sem);
err_close_select_sem0:
  CloseHandle (fhs[0]->select_sem);
err_close_read_mtx:
  CloseHandle (fhs[0]->read_mtx);
err_delete_fhs1:
  delete fhs[1];
err_delete_fhs0:
  delete fhs[0];
err_close_rw_handle:
  NtClose (r);
  NtClose (w);
out:
  debug_printf ("%R = pipe([%p, %p], %d, %y)",
		res, fhs[0], fhs[1], psize, mode);
  return res;
}

static int
nt_create (LPSECURITY_ATTRIBUTES sa_ptr, HANDLE &r, HANDLE &w,
		DWORD psize, int64_t *unique_id)
{
  NTSTATUS status;
  HANDLE npfsh;
  ACCESS_MASK access;
  OBJECT_ATTRIBUTES attr;
  IO_STATUS_BLOCK io;
  LARGE_INTEGER timeout;

  /* Default to error. */
  r = NULL;
  w = NULL;

  status = fhandler_base::npfs_handle (npfsh);
  if (!NT_SUCCESS (status))
    {
      __seterrno_from_nt_status (status);
      return GetLastError ();
    }

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

  UNICODE_STRING pipename;
  WCHAR pipename_buf[MAX_PATH];
  size_t len = __small_swprintf (pipename_buf, L"%S-%u-",
				 &cygheap->installation_key,
				 GetCurrentProcessId ());

  access = GENERIC_READ | FILE_WRITE_ATTRIBUTES | SYNCHRONIZE;
  access |= FILE_WRITE_EA; /* Add this right as a marker of cygwin read pipe */

  ULONG pipe_type = pipe_byte ? FILE_PIPE_BYTE_STREAM_TYPE
    : FILE_PIPE_MESSAGE_TYPE;

  /* Retry NtCreateNamedPipeFile as long as the pipe name is in use.
     Retrying will probably never be necessary, but we want
     to be as robust as possible.  */
  DWORD err = 0;
  while (!r)
    {
      static volatile ULONG pipe_unique_id;
      LONG id = InterlockedIncrement ((LONG *) &pipe_unique_id);
      __small_swprintf (pipename_buf + len, L"pipe-nt-%p", id);
      if (unique_id)
	*unique_id = ((int64_t) id << 32 | GetCurrentProcessId ());

      debug_printf ("name %W, size %u, mode %s", pipename_buf, psize,
		    (pipe_type & FILE_PIPE_MESSAGE_TYPE)
		    ? "PIPE_TYPE_MESSAGE" : "PIPE_TYPE_BYTE");

      RtlInitUnicodeString (&pipename, pipename_buf);

      InitializeObjectAttributes (&attr, &pipename,
				  sa_ptr->bInheritHandle ? OBJ_INHERIT : 0,
				  npfsh, sa_ptr->lpSecurityDescriptor);

      timeout.QuadPart = -500000;
      /* Set FILE_SYNCHRONOUS_IO_NONALERT flag so that native
	 C# programs work with cygwin pipe. */
      status = NtCreateNamedPipeFile (&r, access, &attr, &io,
				      FILE_SHARE_READ | FILE_SHARE_WRITE,
				      FILE_CREATE,
				      FILE_SYNCHRONOUS_IO_NONALERT, pipe_type,
				      FILE_PIPE_BYTE_STREAM_MODE,
				      0, 1, psize, psize, &timeout);

      if (NT_SUCCESS (status))
	{
	  debug_printf ("pipe read handle %p", r);
	  err = 0;
	  break;
	}

      switch (status)
	{
	case STATUS_PIPE_BUSY:
	case STATUS_INSTANCE_NOT_AVAILABLE:
	case STATUS_PIPE_NOT_AVAILABLE:
	  /* The pipe is already open with compatible parameters.
	     Pick a new name and retry.  */
	  debug_printf ("pipe busy, retrying");
	  r = NULL;
	  break;
	case STATUS_ACCESS_DENIED:
	  /* The pipe is already open with incompatible parameters.
	     Pick a new name and retry.  */
	  debug_printf ("pipe access denied, retrying");
	  r = NULL;
	  break;
	default:
	  {
	    __seterrno_from_nt_status (status);
	    err = GetLastError ();
	    debug_printf ("failed, %E");
	    r = NULL;
	  }
	}
    }

  if (err)
    {
      r = NULL;
      return err;
    }

  debug_printf ("NtOpenFile: name %S", &pipename);

  access = GENERIC_WRITE | FILE_READ_ATTRIBUTES | SYNCHRONIZE;
  status = NtOpenFile (&w, access, &attr, &io, 0, 0);
  if (!NT_SUCCESS (status))
    {
      DWORD err = GetLastError ();
      debug_printf ("NtOpenFile failed, r %p, %E", r);
      if (r)
	NtClose (r);
      w = NULL;
      return err;
    }

  /* Success. */
  return 0;
}

/* Called by dtable::init_std_file_from_handle for stdio handles
   inherited from non-Cygwin processes. */
void
fhandler_pipe::set_pipe_buf_size ()
{
  NTSTATUS status;
  IO_STATUS_BLOCK io;
  FILE_PIPE_LOCAL_INFORMATION fpli;

  status = NtQueryInformationFile (get_handle (), &io, &fpli, sizeof fpli,
				   FilePipeLocalInformation);
  if (NT_SUCCESS (status))
    pipe_buf_size = fpli.InboundQuota;
}

int
fhandler_pipe::ioctl (unsigned int cmd, void *p)
{
  int n;

  switch (cmd)
    {
    case FIONREAD:
      if (get_device () == FH_PIPEW)
	{
	  set_errno (EINVAL);
	  return -1;
	}
      if (!PeekNamedPipe (get_handle (), NULL, 0, NULL, (DWORD *) &n, NULL))
	{
	  __seterrno ();
	  return -1;
	}
      break;
    default:
      return fhandler_base::ioctl (cmd, p);
      break;
    }
  *(int *) p = n;
  return 0;
}

int
fhandler_pipe::fcntl (int cmd, intptr_t arg)
{
  if (cmd != F_SETFL)
    return fhandler_base::fcntl (cmd, arg);

  const bool was_nonblocking = is_nonblocking ();
  int res = fhandler_base::fcntl (cmd, arg);
  const bool now_nonblocking = is_nonblocking ();
  /* Do not set blocking mode for read pipe to allow signal handling
     even with FILE_SYNCHRONOUS_IO_NONALERT. */
  if (now_nonblocking != was_nonblocking && get_device () != FH_PIPER)
    set_pipe_non_blocking (now_nonblocking);
  return res;
}

int
fhandler_pipe::fstat (struct stat *buf)
{
  int ret = fhandler_base::fstat (buf);
  if (!ret)
    {
      buf->st_dev = FH_PIPE;
      if (!(buf->st_ino = get_plain_ino ()))
	sscanf (get_name (), "/proc/%*d/fd/pipe:[%llu]",
			     (long long *) &buf->st_ino);
    }
  return ret;
}

int
fhandler_pipe::fstatvfs (struct statvfs *sfs)
{
  set_errno (EBADF);
  return -1;
}

HANDLE
fhandler_pipe::temporary_query_hdl ()
{
  if (get_dev () != FH_PIPEW)
    return NULL;

  ULONG len;
  NTSTATUS status;
  tmp_pathbuf tp;
  OBJECT_NAME_INFORMATION *ntfn = (OBJECT_NAME_INFORMATION *) tp.w_get ();

  /* Try process handle opened and pipe handle value cached first
     in order to reduce overhead. */
  if (query_hdl_proc && query_hdl_value)
    {
      HANDLE h;
      if (!DuplicateHandle (query_hdl_proc, query_hdl_value,
			   GetCurrentProcess (), &h, FILE_READ_DATA, 0, 0))
	goto cache_err;
      /* Check name */
      status = NtQueryObject (h, ObjectNameInformation, ntfn, 65536, &len);
      if (!NT_SUCCESS (status) || !ntfn->Name.Buffer)
	goto hdl_err;
      ntfn->Name.Buffer[ntfn->Name.Length / sizeof (WCHAR)] = L'\0';
      uint64_t key;
      DWORD pid;
      LONG id;
      if (swscanf (ntfn->Name.Buffer,
		   L"\\Device\\NamedPipe\\%llx-%u-pipe-nt-0x%x",
		   &key, &pid, &id) == 3 &&
	  key == pipename_key && pid == pipename_pid && id == pipename_id)
	return h;
hdl_err:
      CloseHandle (h);
cache_err:
      CloseHandle (query_hdl_proc);
      query_hdl_proc = NULL;
      query_hdl_value = NULL;
    }

  status = NtQueryObject (get_handle (), ObjectNameInformation, ntfn,
			  65536, &len);
  if (!NT_SUCCESS (status) || !ntfn->Name.Buffer)
    return NULL; /* Non cygwin pipe? */
  WCHAR name[MAX_PATH];
  int namelen = min (ntfn->Name.Length / sizeof (WCHAR), MAX_PATH-1);
  memcpy (name, ntfn->Name.Buffer, namelen * sizeof (WCHAR));
  name[namelen] = L'\0';
  if (swscanf (name, L"\\Device\\NamedPipe\\%llx-%u-pipe-nt-0x%x",
	       &pipename_key, &pipename_pid, &pipename_id) != 3)
    return NULL; /* Non cygwin pipe? */

  return get_query_hdl_per_process (name, ntfn); /* Since Win8 */
}

HANDLE
fhandler_pipe::get_query_hdl_per_process (WCHAR *name,
					  OBJECT_NAME_INFORMATION *ntfn)
{
  NTSTATUS status;
  ULONG len;
  DWORD n_process = 256;
  PSYSTEM_PROCESS_INFORMATION spi;
  do
    { /* Enumerate processes */
      DWORD nbytes = n_process * sizeof (SYSTEM_PROCESS_INFORMATION);
      spi = (PSYSTEM_PROCESS_INFORMATION) HeapAlloc (GetProcessHeap (),
						     0, nbytes);
      if (!spi)
	return NULL;
      status = NtQuerySystemInformation (SystemProcessInformation,
					 spi, nbytes, &len);
      if (NT_SUCCESS (status))
	break;
      HeapFree (GetProcessHeap (), 0, spi);
      n_process *= 2;
    }
  while (n_process < (1L<<20) && status == STATUS_INFO_LENGTH_MISMATCH);
  if (!NT_SUCCESS (status))
    return NULL;

  /* In most cases, it is faster to check the processes in reverse order.
     To do this, store PIDs into an array. */
  DWORD *proc_pids = (DWORD *) HeapAlloc (GetProcessHeap (), 0,
					  n_process * sizeof (DWORD));
  if (!proc_pids)
    {
      HeapFree (GetProcessHeap (), 0, spi);
      return NULL;
    }
  PSYSTEM_PROCESS_INFORMATION p = spi;
  n_process = 0;
  while (true)
    {
      proc_pids[n_process++] = (DWORD)(intptr_t) p->UniqueProcessId;
      if (!p->NextEntryOffset)
	break;
      p = (PSYSTEM_PROCESS_INFORMATION) ((char *) p + p->NextEntryOffset);
    }
  HeapFree (GetProcessHeap (), 0, spi);

  for (LONG i = (LONG) n_process - 1; i >= 0; i--)
    {
      HANDLE proc = OpenProcess (PROCESS_DUP_HANDLE
				 | PROCESS_QUERY_INFORMATION,
				 0, proc_pids[i]);
      if (!proc)
	continue;

      /* Retrieve process handles */
      DWORD n_handle = 256;
      PPROCESS_HANDLE_SNAPSHOT_INFORMATION phi;
      do
	{
	  DWORD nbytes = 2 * sizeof (ULONG_PTR) +
	    n_handle * sizeof (PROCESS_HANDLE_TABLE_ENTRY_INFO);
	  phi = (PPROCESS_HANDLE_SNAPSHOT_INFORMATION)
	    HeapAlloc (GetProcessHeap (), 0, nbytes);
	  if (!phi)
	    goto close_proc;
	  /* NtQueryInformationProcess can return STATUS_SUCCESS with
	     invalid handle data for certain processes.  See
	     https://github.com/processhacker/processhacker/blob/05f5e9fa477dcaa1709d9518170d18e1b3b8330d/phlib/native.c#L5754.
	     We need to ensure that NumberOfHandles is zero in this
	     case to avoid a crash in the for loop below. */
	  phi->NumberOfHandles = 0;
	  status = NtQueryInformationProcess (proc, ProcessHandleInformation,
					      phi, nbytes, &len);
	  if (NT_SUCCESS (status))
	    break;
	  HeapFree (GetProcessHeap (), 0, phi);
	  n_handle *= 2;
	}
      while (n_handle < (1L<<20) && status == STATUS_INFO_LENGTH_MISMATCH);
      if (!NT_SUCCESS (status))
	goto close_proc;

      /* Sanity check in case Microsoft changes
	 NtQueryInformationProcess and the initialization of
	 NumberOfHandles above is no longer sufficient. */
      assert (phi->NumberOfHandles <= n_handle);
      for (ULONG j = 0; j < phi->NumberOfHandles; j++)
	{
	  /* Check for the peculiarity of cygwin read pipe */
	  const ULONG access = FILE_READ_DATA | FILE_READ_EA
	    | FILE_WRITE_EA /* marker */
	    | FILE_READ_ATTRIBUTES | FILE_WRITE_ATTRIBUTES
	    | READ_CONTROL | SYNCHRONIZE;
	  if (phi->Handles[j].GrantedAccess != access)
	    continue;

	  /* Retrieve handle */
	  HANDLE h = (HANDLE)(intptr_t) phi->Handles[j].HandleValue;
	  BOOL res = DuplicateHandle (proc, h, GetCurrentProcess (), &h,
				      FILE_READ_DATA, 0, 0);
	  if (!res)
	    continue;

	  /* Check object name */
	  status = NtQueryObject (h, ObjectNameInformation,
				  ntfn, 65536, &len);
	  if (!NT_SUCCESS (status) || !ntfn->Name.Buffer)
	    goto close_handle;
	  ntfn->Name.Buffer[ntfn->Name.Length / sizeof (WCHAR)] = L'\0';
	  if (wcscmp (name, ntfn->Name.Buffer) == 0)
	    {
	      query_hdl_proc = proc;
	      query_hdl_value = (HANDLE)(intptr_t) phi->Handles[j].HandleValue;
	      HeapFree (GetProcessHeap (), 0, phi);
	      HeapFree (GetProcessHeap (), 0, proc_pids);
	      return h;
	    }
close_handle:
	  CloseHandle (h);
	}
      HeapFree (GetProcessHeap (), 0, phi);
close_proc:
      CloseHandle (proc);
    }
  HeapFree (GetProcessHeap (), 0, proc_pids);
  return NULL;
}