1 /* Copyright (c) 2011, 2018, Oracle and/or its affiliates. All rights reserved.
2 
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License, version 2.0,
5    as published by the Free Software Foundation.
6 
7    This program is also distributed with certain software (including
8    but not limited to OpenSSL) that is licensed under separate terms,
9    as designated in a particular file or component or in included license
10    documentation.  The authors of MySQL hereby grant you an additional
11    permission to link the program and your derivative works with the
12    separately licensed software that they have included with MySQL.
13 
14    Without limiting anything contained in the foregoing, this file,
15    which is part of C Driver for MySQL (Connector/C), is also subject to the
16    Universal FOSS Exception, version 1.0, a copy of which can be found at
17    http://oss.oracle.com/licenses/universal-foss-exception.
18 
19    This program is distributed in the hope that it will be useful,
20    but WITHOUT ANY WARRANTY; without even the implied warranty of
21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22    GNU General Public License, version 2.0, for more details.
23 
24    You should have received a copy of the GNU General Public License
25    along with this program; if not, write to the Free Software
26    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA */
27 
28 #include "vio_priv.h"
29 
30 #ifdef _WIN32
31 
wait_overlapped_result(Vio * vio,int timeout)32 static size_t wait_overlapped_result(Vio *vio, int timeout)
33 {
34   size_t ret= (size_t) -1;
35   DWORD transferred, wait_status, timeout_ms;
36 
37   timeout_ms= timeout >= 0 ? timeout : INFINITE;
38 
39   /* Wait for the overlapped operation to be completed. */
40   wait_status= WaitForSingleObject(vio->overlapped.hEvent, timeout_ms);
41 
42   /* The operation might have completed, attempt to retrieve the result. */
43   if (wait_status == WAIT_OBJECT_0)
44   {
45     /* If retrieval fails, a error code will have been set. */
46     if (GetOverlappedResult(vio->hPipe, &vio->overlapped, &transferred, FALSE))
47       ret= transferred;
48   }
49   else
50   {
51     /* Error or timeout, cancel the pending I/O operation. */
52     CancelIo(vio->hPipe);
53 
54     /*
55       If the wait timed out, set error code to indicate a
56       timeout error. Otherwise, wait_status is WAIT_FAILED
57       and extended error information was already set.
58     */
59     if (wait_status == WAIT_TIMEOUT)
60       SetLastError(SOCKET_ETIMEDOUT);
61   }
62 
63   return ret;
64 }
65 
66 
vio_read_pipe(Vio * vio,uchar * buf,size_t count)67 size_t vio_read_pipe(Vio *vio, uchar *buf, size_t count)
68 {
69   DWORD transferred;
70   size_t ret= (size_t) -1;
71   DBUG_ENTER("vio_read_pipe");
72 
73   /* Attempt to read from the pipe (overlapped I/O). */
74   if (ReadFile(vio->hPipe, buf, count, &transferred, &vio->overlapped))
75   {
76     /* The operation completed immediately. */
77     ret= transferred;
78   }
79   /* Read operation is pending completion asynchronously? */
80   else if (GetLastError() == ERROR_IO_PENDING)
81     ret= wait_overlapped_result(vio, vio->read_timeout);
82 
83   DBUG_RETURN(ret);
84 }
85 
86 
vio_write_pipe(Vio * vio,const uchar * buf,size_t count)87 size_t vio_write_pipe(Vio *vio, const uchar *buf, size_t count)
88 {
89   DWORD transferred;
90   size_t ret= (size_t) -1;
91   DBUG_ENTER("vio_write_pipe");
92 
93   /* Attempt to write to the pipe (overlapped I/O). */
94   if (WriteFile(vio->hPipe, buf, count, &transferred, &vio->overlapped))
95   {
96     /* The operation completed immediately. */
97     ret= transferred;
98   }
99   /* Write operation is pending completion asynchronously? */
100   else if (GetLastError() == ERROR_IO_PENDING)
101     ret= wait_overlapped_result(vio, vio->write_timeout);
102 
103   DBUG_RETURN(ret);
104 }
105 
106 
vio_is_connected_pipe(Vio * vio)107 my_bool vio_is_connected_pipe(Vio *vio)
108 {
109   if (PeekNamedPipe(vio->hPipe, NULL, 0, NULL, NULL, NULL))
110     return TRUE;
111   else
112     return (GetLastError() != ERROR_BROKEN_PIPE);
113 }
114 
115 
vio_shutdown_pipe(Vio * vio)116 int vio_shutdown_pipe(Vio *vio)
117 {
118   BOOL ret= FALSE;
119   DBUG_ENTER("vio_shutdown_pipe");
120 
121   if (vio->inactive == FALSE)
122   {
123     CancelIo(vio->hPipe);
124     CloseHandle(vio->overlapped.hEvent);
125     DisconnectNamedPipe(vio->hPipe);
126     ret= CloseHandle(vio->hPipe);
127   }
128   vio->inactive= TRUE;
129   vio->hPipe= NULL;
130   vio->mysql_socket= MYSQL_INVALID_SOCKET;
131 
132   DBUG_RETURN(ret);
133 }
134 
135 #endif
136 
137