OSDN Git Service

Break out more header info into separate files. Use appropriate header files
[pf3gnuchains/pf3gnuchains3x.git] / winsup / cygwin / pipe.cc
1 /* pipe.cc: pipe for Cygwin.
2
3    Copyright 1996, 1998, 1999, 2000 Cygnus Solutions.
4
5 This file is part of Cygwin.
6
7 This software is a copyrighted work licensed under the terms of the
8 Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
9 details. */
10
11 #include "winsup.h"
12 #include <unistd.h>
13 #include <sys/fcntl.h>
14 #include <errno.h>
15 #include "cygerrno.h"
16 #include "fhandler.h"
17 #include "dtable.h"
18 #include "thread.h"
19 #include "security.h"
20
21 static int
22 make_pipe (int fildes[2], unsigned int psize, int mode)
23 {
24   SetResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK," make_pipe");
25
26   HANDLE r, w;
27   int  fdr, fdw;
28   SECURITY_ATTRIBUTES *sa = (mode & O_NOINHERIT) ?  &sec_none_nih : &sec_none;
29
30   if ((fdr = fdtab.find_unused_handle ()) < 0)
31     set_errno (ENMFILE);
32   else if ((fdw = fdtab.find_unused_handle (fdr + 1)) < 0)
33     set_errno (ENMFILE);
34   else if (!CreatePipe (&r, &w, sa, psize))
35     __seterrno ();
36   else
37     {
38       fhandler_base *fhr = fdtab.build_fhandler (fdr, FH_PIPER, "/dev/piper");
39       fhandler_base *fhw = fdtab.build_fhandler (fdw, FH_PIPEW, "/dev/pipew");
40
41       int binmode = mode & O_TEXT ? 0 : 1;
42       fhr->init (r, GENERIC_READ, binmode);
43       fhw->init (w, GENERIC_WRITE, binmode);
44       if (mode & O_NOINHERIT)
45        {
46          fhr->set_close_on_exec_flag (1);
47          fhw->set_close_on_exec_flag (1);
48        }
49
50       fildes[0] = fdr;
51       fildes[1] = fdw;
52
53       debug_printf ("0 = pipe (%p) (%d:%p, %d:%p)", fildes,
54                     fdr, fhr->get_handle (), fdw, fhw->get_handle ());
55
56       ReleaseResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK," make_pipe");
57       return 0;
58     }
59
60   syscall_printf ("-1 = pipe (%p)", fildes);
61   ReleaseResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK," make_pipe");
62   return -1;
63 }
64
65 extern "C" int
66 pipe (int filedes[2])
67 {
68   extern DWORD binmode;
69   return make_pipe (filedes, 16384, (!binmode || binmode == O_BINARY) ? O_BINARY : O_TEXT);
70 }
71
72 extern "C" int
73 _pipe (int filedes[2], unsigned int psize, int mode)
74 {
75   int res = make_pipe (filedes, psize, mode);
76   /* This type of pipe is not interruptible so set the appropriate flag. */
77   if (!res)
78     fdtab[filedes[0]]->set_r_no_interrupt (1);
79   return res;
80 }
81
82 int
83 dup (int fd)
84 {
85   int res;
86   SetResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK," dup");
87
88   res = dup2 (fd, fdtab.find_unused_handle ());
89
90   ReleaseResourceLock(LOCK_FD_LIST,WRITE_LOCK|READ_LOCK," dup");
91
92   return res;
93 }
94
95 int
96 dup2 (int oldfd, int newfd)
97 {
98   return fdtab.dup2 (oldfd, newfd);
99 }