~ubuntu-branches/ubuntu/lucid/mysql-dfsg-5.1/lucid-security

« back to all changes in this revision

Viewing changes to vio/viosocket.c

  • Committer: Package Import Robot
  • Author(s): Marc Deslauriers
  • Date: 2012-02-22 22:33:55 UTC
  • mto: (1.2.1) (37.1.1 lucid-security)
  • mto: This revision was merged to the branch mainline in revision 36.
  • Revision ID: package-import@ubuntu.com-20120222223355-ku1tb4r70osci6v2
Tags: upstream-5.1.61
ImportĀ upstreamĀ versionĀ 5.1.61

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* Copyright (C) 2000 MySQL AB
 
1
/*
 
2
   Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved.
2
3
 
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.
 
4
   This program is free software; you can redistribute it and/or
 
5
   modify it under the terms of the GNU General Public License
 
6
   as published by the Free Software Foundation; version 2 of
 
7
   the License.
6
8
 
7
9
   This program is distributed in the hope that it will be useful,
8
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
12
   GNU General Public License for more details.
11
13
 
12
14
   You should have received a copy of the GNU General Public License
13
15
   along with this program; if not, write to the Free Software
14
 
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
 
16
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
 
17
*/
15
18
 
16
19
/*
17
20
  Note that we can't have assertion on file descriptors;  The reason for
415
418
/*
416
419
  Finish pending IO on pipe. Honor wait timeout
417
420
*/
418
 
static int pipe_complete_io(Vio* vio, char* buf, size_t size, DWORD timeout_millis)
 
421
static size_t pipe_complete_io(Vio* vio, char* buf, size_t size, DWORD timeout_ms)
419
422
{
420
423
  DWORD length;
421
424
  DWORD ret;
422
425
 
423
426
  DBUG_ENTER("pipe_complete_io");
424
427
 
425
 
  ret= WaitForSingleObject(vio->pipe_overlapped.hEvent, timeout_millis);
 
428
  ret= WaitForSingleObject(vio->pipe_overlapped.hEvent, timeout_ms);
426
429
  /*
427
430
    WaitForSingleObjects will normally return WAIT_OBJECT_O (success, IO completed)
428
431
    or WAIT_TIMEOUT.
431
434
  {
432
435
    CancelIo(vio->hPipe);
433
436
    DBUG_PRINT("error",("WaitForSingleObject() returned  %d", ret));
434
 
    DBUG_RETURN(-1);
 
437
    DBUG_RETURN((size_t)-1);
435
438
  }
436
439
 
437
440
  if (!GetOverlappedResult(vio->hPipe,&(vio->pipe_overlapped),&length, FALSE))
438
441
  {
439
442
    DBUG_PRINT("error",("GetOverlappedResult() returned last error  %d", 
440
443
      GetLastError()));
441
 
    DBUG_RETURN(-1);
 
444
    DBUG_RETURN((size_t)-1);
442
445
  }
443
446
 
444
447
  DBUG_RETURN(length);
448
451
size_t vio_read_pipe(Vio * vio, uchar *buf, size_t size)
449
452
{
450
453
  DWORD bytes_read;
 
454
  size_t retval;
451
455
  DBUG_ENTER("vio_read_pipe");
452
456
  DBUG_PRINT("enter", ("sd: %d  buf: 0x%lx  size: %u", vio->sd, (long) buf,
453
457
                       (uint) size));
454
458
 
455
 
  if (!ReadFile(vio->hPipe, buf, (DWORD)size, &bytes_read,
 
459
  if (ReadFile(vio->hPipe, buf, (DWORD)size, &bytes_read,
456
460
      &(vio->pipe_overlapped)))
457
461
  {
 
462
    retval= bytes_read;
 
463
  }
 
464
  else
 
465
  {
458
466
    if (GetLastError() != ERROR_IO_PENDING)
459
467
    {
460
468
      DBUG_PRINT("error",("ReadFile() returned last error %d",
461
469
        GetLastError()));
462
470
      DBUG_RETURN((size_t)-1);
463
471
    }
464
 
    bytes_read= pipe_complete_io(vio, buf, size,vio->read_timeout_millis);
 
472
    retval= pipe_complete_io(vio, buf, size,vio->read_timeout_ms);
465
473
  }
466
474
 
467
 
  DBUG_PRINT("exit", ("%d", bytes_read));
468
 
  DBUG_RETURN(bytes_read);
 
475
  DBUG_PRINT("exit", ("%lld", (longlong)retval));
 
476
  DBUG_RETURN(retval);
469
477
}
470
478
 
471
479
 
472
480
size_t vio_write_pipe(Vio * vio, const uchar* buf, size_t size)
473
481
{
474
482
  DWORD bytes_written;
 
483
  size_t retval;
475
484
  DBUG_ENTER("vio_write_pipe");
476
485
  DBUG_PRINT("enter", ("sd: %d  buf: 0x%lx  size: %u", vio->sd, (long) buf,
477
486
                       (uint) size));
478
487
 
479
 
  if (!WriteFile(vio->hPipe, buf, (DWORD)size, &bytes_written,
 
488
  if (WriteFile(vio->hPipe, buf, (DWORD)size, &bytes_written, 
480
489
      &(vio->pipe_overlapped)))
481
490
  {
 
491
    retval= bytes_written;
 
492
  }
 
493
  else
 
494
  {
482
495
    if (GetLastError() != ERROR_IO_PENDING)
483
496
    {
484
497
      DBUG_PRINT("vio_error",("WriteFile() returned last error %d",
485
498
        GetLastError()));
486
499
      DBUG_RETURN((size_t)-1);
487
500
    }
488
 
    bytes_written = pipe_complete_io(vio, (char *)buf, size, 
489
 
        vio->write_timeout_millis);
 
501
    retval= pipe_complete_io(vio, (char *)buf, size, vio->write_timeout_ms);
490
502
  }
491
503
 
492
 
  DBUG_PRINT("exit", ("%d", bytes_written));
493
 
  DBUG_RETURN(bytes_written);
 
504
  DBUG_PRINT("exit", ("%lld", (longlong)retval));
 
505
  DBUG_RETURN(retval);
494
506
}
495
507
 
496
508
 
515
527
 
516
528
void vio_win32_timeout(Vio *vio, uint which , uint timeout_sec)
517
529
{
518
 
    DWORD timeout_millis;
 
530
    DWORD timeout_ms;
519
531
    /*
520
532
      Windows is measuring timeouts in milliseconds. Check for possible int 
521
533
      overflow.
522
534
    */
523
535
    if (timeout_sec > UINT_MAX/1000)
524
 
      timeout_millis= INFINITE;
 
536
      timeout_ms= INFINITE;
525
537
    else
526
 
      timeout_millis= timeout_sec * 1000;
 
538
      timeout_ms= timeout_sec * 1000;
527
539
 
528
540
    /* which == 1 means "write", which == 0 means "read".*/
529
541
    if(which)
530
 
      vio->write_timeout_millis= timeout_millis;
 
542
      vio->write_timeout_ms= timeout_ms;
531
543
    else
532
 
      vio->read_timeout_millis= timeout_millis;
 
544
      vio->read_timeout_ms= timeout_ms;
533
545
}
534
546
 
535
547
 
539
551
{
540
552
  size_t length;
541
553
  size_t remain_local;
542
 
  char *current_postion;
 
554
  char *current_position;
543
555
  HANDLE events[2];
544
556
 
545
557
  DBUG_ENTER("vio_read_shared_memory");
547
559
                       size));
548
560
 
549
561
  remain_local = size;
550
 
  current_postion=buf;
 
562
  current_position=buf;
551
563
 
552
564
  events[0]= vio->event_server_wrote;
553
565
  events[1]= vio->event_conn_closed;
564
576
         WAIT_ABANDONED_0 and WAIT_TIMEOUT - fail.  We can't read anything
565
577
      */
566
578
      if (WaitForMultipleObjects(array_elements(events), events, FALSE,
567
 
                                 vio->read_timeout_millis) != WAIT_OBJECT_0)
 
579
                                 vio->read_timeout_ms) != WAIT_OBJECT_0)
568
580
      {
569
581
        DBUG_RETURN(-1);
570
582
      };
581
593
    if (length > remain_local)
582
594
       length = remain_local;
583
595
 
584
 
    memcpy(current_postion,vio->shared_memory_pos,length);
 
596
    memcpy(current_position,vio->shared_memory_pos,length);
585
597
 
586
598
    vio->shared_memory_remain-=length;
587
599
    vio->shared_memory_pos+=length;
588
 
    current_postion+=length;
 
600
    current_position+=length;
589
601
    remain_local-=length;
590
602
 
591
603
    if (!vio->shared_memory_remain)
605
617
{
606
618
  size_t length, remain, sz;
607
619
  HANDLE pos;
608
 
  const uchar *current_postion;
 
620
  const uchar *current_position;
609
621
  HANDLE events[2];
610
622
 
611
623
  DBUG_ENTER("vio_write_shared_memory");
613
625
                       size));
614
626
 
615
627
  remain = size;
616
 
  current_postion = buf;
 
628
  current_position = buf;
617
629
 
618
630
  events[0]= vio->event_server_read;
619
631
  events[1]= vio->event_conn_closed;
621
633
  while (remain != 0)
622
634
  {
623
635
    if (WaitForMultipleObjects(array_elements(events), events, FALSE,
624
 
                               vio->write_timeout_millis) != WAIT_OBJECT_0)
 
636
                               vio->write_timeout_ms) != WAIT_OBJECT_0)
625
637
    {
626
638
      DBUG_RETURN((size_t) -1);
627
639
    }
631
643
 
632
644
    int4store(vio->handle_map,sz);
633
645
    pos = vio->handle_map + 4;
634
 
    memcpy(pos,current_postion,sz);
 
646
    memcpy(pos,current_position,sz);
635
647
    remain-=sz;
636
 
    current_postion+=sz;
 
648
    current_position+=sz;
637
649
    if (!SetEvent(vio->event_client_wrote))
638
650
      DBUG_RETURN((size_t) -1);
639
651
  }