1 /* pipe.cc: pipe for Cygwin.
3 Copyright 1996, 1998, 1999, 2000, 2001, 2002, 2003 Red Hat, Inc.
5 This file is part of Cygwin.
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
11 /* FIXME: Should this really be fhandler_pipe.cc? */
16 #include <sys/socket.h>
25 #include "cygthread.h"
27 static unsigned pipecount;
28 static const NO_COPY char pipeid_fmt[] = "stupid_pipe.%u.%u";
30 fhandler_pipe::fhandler_pipe (DWORD devtype)
31 : fhandler_base (devtype), guard (NULL), broken_pipe (false), writepipe_exists(0),
37 fhandler_pipe::lseek (_off64_t offset, int whence)
39 debug_printf ("(%d, %d)", offset, whence);
45 fhandler_pipe::set_close_on_exec (int val)
47 fhandler_base::set_close_on_exec (val);
49 set_inheritance (guard, val);
51 set_inheritance (writepipe_exists, val);
64 pipeargs *pi = (pipeargs *) arg;
65 pi->fh->fhandler_base::read (pi->ptr, *pi->len);
70 fhandler_pipe::read (void *in_ptr, size_t& in_len)
76 pipeargs pi = {this, in_ptr, &in_len};
77 ResetEvent (read_state);
78 cygthread *th = new cygthread (read_pipe, &pi, "read_pipe");
79 if (th->detach (read_state) && !in_len)
80 (ssize_t) in_len = -1; /* received a signal */
82 (void) ReleaseMutex (guard);
86 int fhandler_pipe::close ()
88 int res = fhandler_base::close ();
93 CloseHandle (writepipe_exists);
94 if (read_state && !cygheap->fdtab.in_vfork_cleanup ())
95 CloseHandle (read_state);
100 fhandler_pipe::hit_eof ()
108 __small_sprintf (buf, pipeid_fmt, orig_pid, id);
109 if ((ev = OpenEvent (EVENT_ALL_ACCESS, FALSE, buf)))
111 debug_printf ("%s %p", buf, ev);
116 fhandler_pipe::fixup_after_exec (HANDLE parent)
119 read_state = CreateEvent (&sec_none_nih, FALSE, FALSE, NULL);
123 fhandler_pipe::fixup_after_fork (HANDLE parent)
125 fhandler_base::fixup_after_fork (parent);
127 fork_fixup (parent, guard, "guard");
128 if (writepipe_exists)
129 fork_fixup (parent, writepipe_exists, "guard");
130 fixup_after_exec (parent);
134 fhandler_pipe::dup (fhandler_base *child)
138 int res = fhandler_base::dup (child);
143 fhandler_pipe *ftp = (fhandler_pipe *) child;
145 /* FIXME: This leaks handles in the failing condition */
148 else if (!DuplicateHandle (hMainProc, guard, hMainProc, &ftp->guard, 0, 1,
149 DUPLICATE_SAME_ACCESS))
151 debug_printf ("couldn't duplicate guard %p, %E", guard);
155 if (writepipe_exists == NULL)
156 ftp->writepipe_exists = NULL;
157 else if (!DuplicateHandle (hMainProc, writepipe_exists, hMainProc,
158 &ftp->writepipe_exists, 0, 1,
159 DUPLICATE_SAME_ACCESS))
161 debug_printf ("couldn't duplicate writepipe_exists %p, %E", writepipe_exists);
165 if (read_state == NULL)
166 ftp->read_state = NULL;
167 else if (!DuplicateHandle (hMainProc, read_state, hMainProc,
168 &ftp->read_state, 0, 1,
169 DUPLICATE_SAME_ACCESS))
171 debug_printf ("couldn't duplicate read_state %p, %E", writepipe_exists);
176 ftp->orig_pid = orig_pid;
181 make_pipe (int fildes[2], unsigned int psize, int mode)
184 SECURITY_ATTRIBUTES *sa = (mode & O_NOINHERIT) ? &sec_none_nih : &sec_none;
190 cygheap_fdnew fdw (fdr, false);
193 else if (!CreatePipe (&r, &w, sa, psize))
197 fhandler_pipe *fhr = (fhandler_pipe *) cygheap->fdtab.build_fhandler (fdr, FH_PIPER, "/dev/piper");
198 fhandler_pipe *fhw = (fhandler_pipe *) cygheap->fdtab.build_fhandler (fdw, FH_PIPEW, "/dev/pipew");
200 int binmode = mode & O_TEXT ?: O_BINARY;
201 fhr->init (r, GENERIC_READ, binmode);
202 fhw->init (w, GENERIC_WRITE, binmode);
203 if (mode & O_NOINHERIT)
205 fhr->set_close_on_exec_flag (1);
206 fhw->set_close_on_exec_flag (1);
211 fhr->read_state = CreateEvent (&sec_none_nih, FALSE, FALSE, NULL);
212 fhr->set_need_fork_fixup ();
215 fhr->create_guard (sa);
216 if (wincap.has_unreliable_pipes ())
219 int count = pipecount++; /* FIXME: Should this be InterlockedIncrement? */
220 __small_sprintf (buf, pipeid_fmt, myself->pid, count);
221 fhw->writepipe_exists = CreateEvent (sa, TRUE, FALSE, buf);
222 fhr->orig_pid = myself->pid;
228 syscall_printf ("%d = make_pipe ([%d, %d], %d, %p)", res, fildes[0],
229 fildes[1], psize, mode);
234 fhandler_pipe::ioctl (unsigned int cmd, void *p)
241 if (get_device () == FH_PIPEW)
246 if (!PeekNamedPipe (get_handle (), NULL, 0, NULL, (DWORD *) &n, NULL))
253 return fhandler_base::ioctl (cmd, p);
261 pipe (int filedes[2])
263 extern DWORD binmode;
264 return make_pipe (filedes, 16384, (!binmode || binmode == O_BINARY) ? O_BINARY : O_TEXT);
268 _pipe (int filedes[2], unsigned int psize, int mode)
270 int res = make_pipe (filedes, psize, mode);
271 /* This type of pipe is not interruptible so set the appropriate flag. */
273 cygheap->fdtab[filedes[0]]->set_r_no_interrupt (1);