1 /* pipe.cc: pipe for Cygwin.
3 Copyright 1996, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 2008, 2009, 2010, 2011 Hat, Inc.
6 This file is part of Cygwin.
8 This software is a copyrighted work licensed under the terms of the
9 Cygwin license. Please consult the file "CYGWIN_LICENSE" for
12 /* FIXME: Should this really be fhandler_pipe.cc? */
16 #include <sys/socket.h>
24 #include "shared_info.h"
26 fhandler_pipe::fhandler_pipe ()
27 : fhandler_base_overlapped (), popen_pid (0)
29 max_atomic_write = DEFAULT_PIPEBUFSIZE;
30 need_fork_fixup (true);
34 fhandler_pipe::init (HANDLE f, DWORD a, mode_t mode)
36 /* FIXME: Have to clean this up someday
37 FIXME: Do we have to check for both !get_win32_name() and
38 !*get_win32_name()? */
39 if ((!get_win32_name () || !*get_win32_name ()) && get_name ())
43 char *hold_normalized_name = (char *) alloca (strlen (get_name ()) + 1);
44 for (s = get_name (), d = hold_normalized_name; *s; s++, d++)
50 set_name (hold_normalized_name);
53 bool opened_properly = a & FILE_CREATE_PIPE_INSTANCE;
54 a &= ~FILE_CREATE_PIPE_INSTANCE;
55 fhandler_base::init (f, a, mode);
56 close_on_exec (mode & O_CLOEXEC);
60 destroy_overlapped ();
64 extern "C" int sscanf (const char *, const char *, ...);
67 fhandler_pipe::open (int flags, mode_t mode)
69 HANDLE proc, pipe_hdl, nio_hdl = NULL;
70 fhandler_pipe *fh = NULL;
72 int pid, rwflags = (flags & O_ACCMODE);
75 sscanf (get_name (), "/proc/%d/fd/pipe:[%d]", &pid, (int *) &pipe_hdl);
76 if (pid == myself->pid)
78 cygheap_fdenum cfd (true);
79 while (cfd.next () >= 0)
81 if (cfd->get_handle () != pipe_hdl)
83 if ((rwflags == O_RDONLY && !(cfd->get_access () & GENERIC_READ))
84 || (rwflags == O_WRONLY && !(cfd->get_access () & GENERIC_WRITE)))
91 pc.reset_conv_handle ();
92 if (!cfd->dup (this, flags))
106 if (!(proc = OpenProcess (PROCESS_DUP_HANDLE, false, p->dwProcessId)))
111 if (!(fh = p->pipe_fhandler (pipe_hdl, size)) || !size)
116 /* Too bad, but Windows only allows the same access mode when dup'ing
118 if ((rwflags == O_RDONLY && !(fh->get_access () & GENERIC_READ))
119 || (rwflags == O_WRONLY && !(fh->get_access () & GENERIC_WRITE)))
124 inh = !(flags & O_CLOEXEC);
125 if (!DuplicateHandle (proc, pipe_hdl, GetCurrentProcess (), &nio_hdl,
126 0, inh, DUPLICATE_SAME_ACCESS))
131 init (nio_hdl, fh->get_access (), mode & O_TEXT ?: O_BINARY);
137 CloseHandle (nio_hdl);
146 fhandler_pipe::lseek (_off64_t offset, int whence)
148 debug_printf ("(%d, %d)", offset, whence);
154 fhandler_pipe::fadvise (_off64_t offset, _off64_t length, int advice)
161 fhandler_pipe::ftruncate (_off64_t length, bool allow_truncate)
163 set_errno (allow_truncate ? EINVAL : ESPIPE);
168 fhandler_pipe::get_proc_fd_name (char *buf)
170 __small_sprintf (buf, "pipe:[%d]", get_handle ());
175 fhandler_pipe::dup (fhandler_base *child, int flags)
177 fhandler_pipe *ftp = (fhandler_pipe *) child;
178 ftp->set_popen_pid (0);
181 if (get_handle () && fhandler_base_overlapped::dup (child, flags))
186 debug_printf ("res %d", res);
190 #define PIPE_INTRO "\\\\.\\pipe\\cygwin-"
192 /* Create a pipe, and return handles to the read and write ends,
193 just like CreatePipe, but ensure that the write end permits
194 FILE_READ_ATTRIBUTES access, on later versions of win32 where
195 this is supported. This access is needed by NtQueryInformationFile,
196 which is used to implement select and nonblocking writes.
197 Note that the return value is either 0 or GetLastError,
198 unlike CreatePipe, which returns a bool for success or failure. */
200 fhandler_pipe::create (LPSECURITY_ATTRIBUTES sa_ptr, PHANDLE r, PHANDLE w,
201 DWORD psize, const char *name, DWORD open_mode)
203 /* Default to error. */
209 /* Ensure that there is enough pipe buffer space for atomic writes. */
211 psize = DEFAULT_PIPEBUFSIZE;
213 char pipename[MAX_PATH];
214 const size_t len = __small_sprintf (pipename, PIPE_INTRO "%S-",
217 strcpy (pipename + len, name);
219 open_mode |= PIPE_ACCESS_INBOUND;
221 /* Retry CreateNamedPipe as long as the pipe name is in use.
222 Retrying will probably never be necessary, but we want
223 to be as robust as possible. */
227 static volatile ULONG pipe_unique_id;
229 __small_sprintf (pipename + len, "pipe-%p-%p", myself->pid,
230 InterlockedIncrement ((LONG *) &pipe_unique_id));
232 debug_printf ("CreateNamedPipe: name %s, size %lu", pipename, psize);
234 /* Use CreateNamedPipe instead of CreatePipe, because the latter
235 returns a write handle that does not permit FILE_READ_ATTRIBUTES
236 access, on versions of win32 earlier than WinXP SP2.
237 CreatePipe also stupidly creates a full duplex pipe, which is
238 a waste, since only a single direction is actually used.
239 It's important to only allow a single instance, to ensure that
240 the pipe was not created earlier by some other process, even if
241 the pid has been reused. We avoid FILE_FLAG_FIRST_PIPE_INSTANCE
242 because that is only available for Win2k SP2 and WinXP.
244 Note that the write side of the pipe is opened as PIPE_TYPE_MESSAGE.
245 This *seems* to more closely mimic Linux pipe behavior and is
246 definitely required for pty handling since fhandler_pty_master
247 writes to the pipe in chunks, terminated by newline when CANON mode
249 *r = CreateNamedPipe (pipename, open_mode,
250 PIPE_TYPE_MESSAGE | PIPE_READMODE_BYTE, 1, psize,
251 psize, NMPWAIT_USE_DEFAULT_WAIT, sa_ptr);
253 if (*r != INVALID_HANDLE_VALUE)
255 debug_printf ("pipe read handle %p", *r);
260 err = GetLastError ();
263 case ERROR_PIPE_BUSY:
264 /* The pipe is already open with compatible parameters.
265 Pick a new name and retry. */
266 debug_printf ("pipe busy", !name ? ", retrying" : "");
270 case ERROR_ACCESS_DENIED:
271 /* The pipe is already open with incompatible parameters.
272 Pick a new name and retry. */
273 debug_printf ("pipe access denied%s", !name ? ", retrying" : "");
279 err = GetLastError ();
280 debug_printf ("failed, %E");
292 debug_printf ("pipe write handle NULL");
295 debug_printf ("CreateFile: name %s", pipename);
297 /* Open the named pipe for writing.
298 Be sure to permit FILE_READ_ATTRIBUTES access. */
299 DWORD access = GENERIC_WRITE | FILE_READ_ATTRIBUTES;
300 if ((open_mode & PIPE_ACCESS_DUPLEX) == PIPE_ACCESS_DUPLEX)
301 access |= GENERIC_READ | FILE_WRITE_ATTRIBUTES;
302 *w = CreateFile (pipename, access, 0, sa_ptr, OPEN_EXISTING,
303 open_mode & FILE_FLAG_OVERLAPPED, 0);
305 if (!*w || *w == INVALID_HANDLE_VALUE)
308 DWORD err = GetLastError ();
309 debug_printf ("CreateFile failed, %E");
316 debug_printf ("pipe write handle %p", *w);
324 fhandler_pipe::create (fhandler_pipe *fhs[2], unsigned psize, int mode)
327 SECURITY_ATTRIBUTES *sa = sec_none_cloexec (mode);
330 int ret = create (sa, &r, &w, psize, NULL, FILE_FLAG_OVERLAPPED);
332 __seterrno_from_win_error (ret);
333 else if ((fhs[0] = (fhandler_pipe *) build_fh_dev (*piper_dev)) == NULL)
338 else if ((fhs[1] = (fhandler_pipe *) build_fh_dev (*pipew_dev)) == NULL)
345 mode |= mode & O_TEXT ?: O_BINARY;
346 fhs[0]->init (r, FILE_CREATE_PIPE_INSTANCE | GENERIC_READ, mode);
347 fhs[1]->init (w, FILE_CREATE_PIPE_INSTANCE | GENERIC_WRITE, mode);
351 debug_printf ("%R = pipe([%p, %p], %d, %p)", res, fhs[0], fhs[1], psize, mode);
356 fhandler_pipe::ioctl (unsigned int cmd, void *p)
363 if (get_device () == FH_PIPEW)
368 if (!PeekNamedPipe (get_handle (), NULL, 0, NULL, (DWORD *) &n, NULL))
375 return fhandler_base::ioctl (cmd, p);
383 fhandler_pipe::fstatvfs (struct statvfs *sfs)
389 static int __attribute__ ((regparm (3)))
390 pipe_worker (int filedes[2], unsigned int psize, int mode)
392 fhandler_pipe *fhs[2];
393 int res = fhandler_pipe::create (fhs, psize, mode);
397 cygheap_fdnew fdout (fdin, false);
398 char buf[sizeof ("/dev/fd/pipe:[2147483647]")];
399 __small_sprintf (buf, "/dev/fd/pipe:[%d]", (int) fdin);
400 fhs[0]->pc.set_normalized_path (buf);
401 __small_sprintf (buf, "pipe:[%d]", (int) fdout);
402 fhs[1]->pc.set_normalized_path (buf);
412 _pipe (int filedes[2], unsigned int psize, int mode)
414 int res = pipe_worker (filedes, psize, mode);
423 syscall_printf ("%R = _pipe([%d, %d], %u, %p)", res, read, write, psize, mode);
428 pipe (int filedes[2])
430 int res = pipe_worker (filedes, DEFAULT_PIPEBUFSIZE, O_BINARY);
439 syscall_printf ("%R = pipe([%d, %d])", res, read, write);
444 pipe2 (int filedes[2], int mode)
446 int res = pipe_worker (filedes, DEFAULT_PIPEBUFSIZE, mode);
455 syscall_printf ("%R = pipe2([%d, %d], %p)", res, read, write, mode);