~linuxjedi/drizzle/trunk-bug-667053

« back to all changes in this revision

Viewing changes to vio/vio.c

  • Committer: brian
  • Date: 2008-06-25 05:29:13 UTC
  • Revision ID: brian@localhost.localdomain-20080625052913-6upwo0jsrl4lnapl
clean slate

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2000 MySQL AB
 
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 as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
15
 
 
16
/*
 
17
  Note that we can't have assertion on file descriptors;  The reason for
 
18
  this is that during mysql shutdown, another thread can close a file
 
19
  we are working on.  In this case we should just return read errors from
 
20
  the file descriptior.
 
21
*/
 
22
 
 
23
#include "vio_priv.h"
 
24
 
 
25
/*
 
26
 * Helper to fill most of the Vio* with defaults.
 
27
 */
 
28
 
 
29
static void vio_init(Vio* vio, enum enum_vio_type type,
 
30
                     my_socket sd, HANDLE hPipe, uint flags)
 
31
{
 
32
  DBUG_ENTER("vio_init");
 
33
  DBUG_PRINT("enter", ("type: %d  sd: %d  flags: %d", type, sd, flags));
 
34
 
 
35
#ifndef HAVE_VIO_READ_BUFF
 
36
  flags&= ~VIO_BUFFERED_READ;
 
37
#endif
 
38
  bzero((char*) vio, sizeof(*vio));
 
39
  vio->type     = type;
 
40
  vio->sd       = sd;
 
41
  vio->hPipe    = hPipe;
 
42
  vio->localhost= flags & VIO_LOCALHOST;
 
43
  if ((flags & VIO_BUFFERED_READ) &&
 
44
      !(vio->read_buffer= (char*)my_malloc(VIO_READ_BUFFER_SIZE, MYF(MY_WME))))
 
45
    flags&= ~VIO_BUFFERED_READ;
 
46
#ifdef __WIN__ 
 
47
  if (type == VIO_TYPE_NAMEDPIPE)
 
48
  {
 
49
    vio->viodelete      =vio_delete;
 
50
    vio->vioerrno       =vio_errno;
 
51
    vio->read           =vio_read_pipe;
 
52
    vio->write          =vio_write_pipe;
 
53
    vio->fastsend       =vio_fastsend;
 
54
    vio->viokeepalive   =vio_keepalive;
 
55
    vio->should_retry   =vio_should_retry;
 
56
    vio->was_interrupted=vio_was_interrupted;
 
57
    vio->vioclose       =vio_close_pipe;
 
58
    vio->peer_addr      =vio_peer_addr;
 
59
    vio->vioblocking    =vio_blocking;
 
60
    vio->is_blocking    =vio_is_blocking;
 
61
    vio->timeout        =vio_ignore_timeout;
 
62
  }
 
63
  else                                  /* default is VIO_TYPE_TCPIP */
 
64
#endif
 
65
#ifdef HAVE_SMEM 
 
66
  if (type == VIO_TYPE_SHARED_MEMORY)
 
67
  {
 
68
    vio->viodelete      =vio_delete;
 
69
    vio->vioerrno       =vio_errno;
 
70
    vio->read           =vio_read_shared_memory;
 
71
    vio->write          =vio_write_shared_memory;
 
72
    vio->fastsend       =vio_fastsend;
 
73
    vio->viokeepalive   =vio_keepalive;
 
74
    vio->should_retry   =vio_should_retry;
 
75
    vio->was_interrupted=vio_was_interrupted;
 
76
    vio->vioclose       =vio_close_shared_memory;
 
77
    vio->peer_addr      =vio_peer_addr;
 
78
    vio->vioblocking    =vio_blocking;
 
79
    vio->is_blocking    =vio_is_blocking;
 
80
    vio->timeout        =vio_ignore_timeout;
 
81
  }
 
82
  else
 
83
#endif   
 
84
#ifdef HAVE_OPENSSL 
 
85
  if (type == VIO_TYPE_SSL)
 
86
  {
 
87
    vio->viodelete      =vio_ssl_delete;
 
88
    vio->vioerrno       =vio_errno;
 
89
    vio->read           =vio_ssl_read;
 
90
    vio->write          =vio_ssl_write;
 
91
    vio->fastsend       =vio_fastsend;
 
92
    vio->viokeepalive   =vio_keepalive;
 
93
    vio->should_retry   =vio_should_retry;
 
94
    vio->was_interrupted=vio_was_interrupted;
 
95
    vio->vioclose       =vio_ssl_close;
 
96
    vio->peer_addr      =vio_peer_addr;
 
97
    vio->vioblocking    =vio_ssl_blocking;
 
98
    vio->is_blocking    =vio_is_blocking;
 
99
    vio->timeout        =vio_timeout;
 
100
  }
 
101
  else                                  /* default is VIO_TYPE_TCPIP */
 
102
#endif /* HAVE_OPENSSL */
 
103
  {
 
104
    vio->viodelete      =vio_delete;
 
105
    vio->vioerrno       =vio_errno;
 
106
    vio->read= (flags & VIO_BUFFERED_READ) ? vio_read_buff : vio_read;
 
107
    vio->write          =vio_write;
 
108
    vio->fastsend       =vio_fastsend;
 
109
    vio->viokeepalive   =vio_keepalive;
 
110
    vio->should_retry   =vio_should_retry;
 
111
    vio->was_interrupted=vio_was_interrupted;
 
112
    vio->vioclose       =vio_close;
 
113
    vio->peer_addr      =vio_peer_addr;
 
114
    vio->vioblocking    =vio_blocking;
 
115
    vio->is_blocking    =vio_is_blocking;
 
116
    vio->timeout        =vio_timeout;
 
117
  }
 
118
  DBUG_VOID_RETURN;
 
119
}
 
120
 
 
121
 
 
122
/* Reset initialized VIO to use with another transport type */
 
123
 
 
124
void vio_reset(Vio* vio, enum enum_vio_type type,
 
125
               my_socket sd, HANDLE hPipe, uint flags)
 
126
{
 
127
  my_free(vio->read_buffer, MYF(MY_ALLOW_ZERO_PTR));
 
128
  vio_init(vio, type, sd, hPipe, flags);
 
129
}
 
130
 
 
131
 
 
132
/* Open the socket or TCP/IP connection and read the fnctl() status */
 
133
 
 
134
Vio *vio_new(my_socket sd, enum enum_vio_type type, uint flags)
 
135
{
 
136
  Vio *vio;
 
137
  DBUG_ENTER("vio_new");
 
138
  DBUG_PRINT("enter", ("sd: %d", sd));
 
139
  if ((vio = (Vio*) my_malloc(sizeof(*vio),MYF(MY_WME))))
 
140
  {
 
141
    vio_init(vio, type, sd, 0, flags);
 
142
    sprintf(vio->desc,
 
143
            (vio->type == VIO_TYPE_SOCKET ? "socket (%d)" : "TCP/IP (%d)"),
 
144
            vio->sd);
 
145
#if !defined(__WIN__)
 
146
#if !defined(NO_FCNTL_NONBLOCK)
 
147
    /*
 
148
      We call fcntl() to set the flags and then immediately read them back
 
149
      to make sure that we and the system are in agreement on the state of
 
150
      things.
 
151
 
 
152
      An example of why we need to do this is FreeBSD (and apparently some
 
153
      other BSD-derived systems, like Mac OS X), where the system sometimes
 
154
      reports that the socket is set for non-blocking when it really will
 
155
      block.
 
156
    */
 
157
    fcntl(sd, F_SETFL, 0);
 
158
    vio->fcntl_mode= fcntl(sd, F_GETFL);
 
159
#elif defined(HAVE_SYS_IOCTL_H)                 /* hpux */
 
160
    /* Non blocking sockets doesn't work good on HPUX 11.0 */
 
161
    (void) ioctl(sd,FIOSNBIO,0);
 
162
    vio->fcntl_mode &= ~O_NONBLOCK;
 
163
#endif
 
164
#else /* !defined(__WIN__) */
 
165
    {
 
166
      /* set to blocking mode by default */
 
167
      ulong arg=0, r;
 
168
      r = ioctlsocket(sd,FIONBIO,(void*) &arg);
 
169
      vio->fcntl_mode &= ~O_NONBLOCK;
 
170
    }
 
171
#endif
 
172
  }
 
173
  DBUG_RETURN(vio);
 
174
}
 
175
 
 
176
 
 
177
#ifdef __WIN__
 
178
 
 
179
Vio *vio_new_win32pipe(HANDLE hPipe)
 
180
{
 
181
  Vio *vio;
 
182
  DBUG_ENTER("vio_new_handle");
 
183
  if ((vio = (Vio*) my_malloc(sizeof(Vio),MYF(MY_WME))))
 
184
  {
 
185
    vio_init(vio, VIO_TYPE_NAMEDPIPE, 0, hPipe, VIO_LOCALHOST);
 
186
    strmov(vio->desc, "named pipe");
 
187
  }
 
188
  DBUG_RETURN(vio);
 
189
}
 
190
 
 
191
#ifdef HAVE_SMEM
 
192
Vio *vio_new_win32shared_memory(NET *net,HANDLE handle_file_map, HANDLE handle_map,
 
193
                                HANDLE event_server_wrote, HANDLE event_server_read,
 
194
                                HANDLE event_client_wrote, HANDLE event_client_read,
 
195
                                HANDLE event_conn_closed)
 
196
{
 
197
  Vio *vio;
 
198
  DBUG_ENTER("vio_new_win32shared_memory");
 
199
  if ((vio = (Vio*) my_malloc(sizeof(Vio),MYF(MY_WME))))
 
200
  {
 
201
    vio_init(vio, VIO_TYPE_SHARED_MEMORY, 0, 0, VIO_LOCALHOST);
 
202
    vio->handle_file_map= handle_file_map;
 
203
    vio->handle_map= handle_map;
 
204
    vio->event_server_wrote= event_server_wrote;
 
205
    vio->event_server_read= event_server_read;
 
206
    vio->event_client_wrote= event_client_wrote;
 
207
    vio->event_client_read= event_client_read;
 
208
    vio->event_conn_closed= event_conn_closed;
 
209
    vio->shared_memory_remain= 0;
 
210
    vio->shared_memory_pos= handle_map;
 
211
    vio->net= net;
 
212
    strmov(vio->desc, "shared memory");
 
213
  }
 
214
  DBUG_RETURN(vio);
 
215
}
 
216
#endif
 
217
#endif
 
218
 
 
219
 
 
220
void vio_delete(Vio* vio)
 
221
{
 
222
  if (!vio)
 
223
    return; /* It must be safe to delete null pointers. */
 
224
 
 
225
  if (vio->type != VIO_CLOSED)
 
226
    vio->vioclose(vio);
 
227
  my_free((uchar*) vio->read_buffer, MYF(MY_ALLOW_ZERO_PTR));
 
228
  my_free((uchar*) vio,MYF(0));
 
229
}
 
230
 
 
231
 
 
232
/*
 
233
  Cleanup memory allocated by vio or the
 
234
  components below it when application finish
 
235
 
 
236
*/
 
237
void vio_end(void)
 
238
{
 
239
#ifdef HAVE_YASSL
 
240
  yaSSL_CleanUp();
 
241
#endif
 
242
}