1 /* pipe.cc: pipe for Cygwin.
3 Copyright 1996, 1998, 1999, 2000, 2001 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
13 #include <sys/fcntl.h>
24 static unsigned pipecount;
25 static const NO_COPY char pipeid_fmt[] = "stupid_pipe.%u.%u";
27 fhandler_pipe::fhandler_pipe (const char *name, DWORD devtype) :
28 fhandler_base (devtype, name),
29 guard (0), writepipe_exists(0), orig_pid (0), id (0)
31 set_cb (sizeof *this);
35 fhandler_pipe::lseek (off_t offset, int whence)
37 debug_printf ("(%d, %d)", offset, whence);
43 fhandler_pipe::set_close_on_exec (int val)
45 this->fhandler_base::set_close_on_exec (val);
47 set_inheritance (guard, val);
49 set_inheritance (writepipe_exists, val);
53 fhandler_pipe::read (void *in_ptr, size_t in_len)
55 int res = this->fhandler_base::read (in_ptr, in_len);
60 int fhandler_pipe::close ()
62 int res = this->fhandler_base::close ();
66 {debug_printf ("writepipe_exists closed");
67 CloseHandle (writepipe_exists);
73 fhandler_pipe::hit_eof ()
79 __small_sprintf (buf, pipeid_fmt, orig_pid, id);
80 if ((ev = OpenEvent (EVENT_ALL_ACCESS, FALSE, buf)))
82 debug_printf ("%s %p", buf, ev);
87 fhandler_pipe::dup (fhandler_base *child)
89 int res = this->fhandler_base::dup (child);
93 fhandler_pipe *ftp = (fhandler_pipe *) child;
97 else if (!DuplicateHandle (hMainProc, guard, hMainProc, &ftp->guard, 0, 1,
98 DUPLICATE_SAME_ACCESS))
101 if (writepipe_exists == NULL)
102 ftp->writepipe_exists = NULL;
103 else if (!DuplicateHandle (hMainProc, writepipe_exists, hMainProc,
104 &ftp->writepipe_exists, 0, 1,
105 DUPLICATE_SAME_ACCESS))
109 ftp->orig_pid = orig_pid;
115 make_pipe (int fildes[2], unsigned int psize, int mode)
117 SetResourceLock (LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "make_pipe");
120 int fdr = -1, fdw = -1;
121 SECURITY_ATTRIBUTES *sa = (mode & O_NOINHERIT) ? &sec_none_nih : &sec_none;
124 if ((fdr = cygheap->fdtab.find_unused_handle ()) < 0)
126 else if ((fdw = cygheap->fdtab.find_unused_handle (fdr + 1)) < 0)
128 else if (!CreatePipe (&r, &w, sa, psize))
132 fhandler_pipe *fhr = (fhandler_pipe *) cygheap->fdtab.build_fhandler (fdr, FH_PIPER, "/dev/piper");
133 fhandler_pipe *fhw = (fhandler_pipe *) cygheap->fdtab.build_fhandler (fdw, FH_PIPEW, "/dev/pipew");
135 int binmode = mode & O_TEXT ? 0 : 1;
136 fhr->init (r, GENERIC_READ, binmode);
137 fhw->init (w, GENERIC_WRITE, binmode);
138 if (mode & O_NOINHERIT)
140 fhr->set_close_on_exec_flag (1);
141 fhw->set_close_on_exec_flag (1);
148 fhr->create_guard (sa);
149 if (wincap.has_unreliable_pipes ())
152 int count = pipecount++; /* FIXME: Should this be InterlockedIncrement? */
153 __small_sprintf (buf, pipeid_fmt, myself->pid, count);
154 fhw->writepipe_exists = CreateEvent (sa, TRUE, FALSE, buf);
155 fhr->orig_pid = myself->pid;
160 syscall_printf ("%d = make_pipe ([%d, %d], %d, %p)", res, fdr, fdw, psize, mode);
161 ReleaseResourceLock(LOCK_FD_LIST, WRITE_LOCK | READ_LOCK, "make_pipe");
166 pipe (int filedes[2])
168 extern DWORD binmode;
169 return make_pipe (filedes, 16384, (!binmode || binmode == O_BINARY) ? O_BINARY : O_TEXT);
173 _pipe (int filedes[2], unsigned int psize, int mode)
175 int res = make_pipe (filedes, psize, mode);
176 /* This type of pipe is not interruptible so set the appropriate flag. */
178 cygheap->fdtab[filedes[0]]->set_r_no_interrupt (1);