~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to libdrizzle/libdrizzle_priv.c

  • Committer: Monty Taylor
  • Date: 2008-12-17 04:54:32 UTC
  • mto: (685.1.38 devel) (713.1.1 devel)
  • mto: This revision was merged to the branch mainline in revision 713.
  • Revision ID: monty@inaugust.com-20081217045432-dw4425razy7do46i
Fixed bool. No more sql_mode.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 
3
 *
 
4
 *  Copyright (C) 2008 Sun Microsystems, Inc.
 
5
 *
 
6
 *  This program is free software; you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU General Public License as published by
 
8
 *  the Free Software Foundation; version 2 of the License.
 
9
 *
 
10
 *  This program is distributed in the hope that it will be useful,
 
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
13
 *  GNU General Public License for more details.
 
14
 *
 
15
 *  You should have received a copy of the GNU General Public License
 
16
 *  along with this program; if not, write to the Free Software
 
17
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 
18
 */
 
19
 
 
20
/* Various helper functions not intended to be part of a public API */
 
21
 
 
22
#include <drizzled/global.h>
 
23
#include "libdrizzle_priv.h"
 
24
#include <poll.h>
 
25
#include <fcntl.h>
 
26
 
 
27
const char  *unknown_sqlstate= "HY000";
 
28
const char  *not_error_sqlstate= "00000";
 
29
const char  *cant_connect_sqlstate= "08001";
 
30
 
 
31
const char * sqlstate_get_unknown(void)
 
32
{
 
33
  return unknown_sqlstate;
 
34
}
 
35
 
 
36
const char * sqlstate_get_not_error(void)
 
37
{
 
38
  return not_error_sqlstate;
 
39
}
 
40
 
 
41
const char * sqlstate_get_cant_connect(void)
 
42
{
 
43
  return cant_connect_sqlstate;
 
44
}
 
45
 
 
46
/****************************************************************************
 
47
  A modified version of connect().  connect_with_timeout() allows you to specify
 
48
  a timeout value, in seconds, that we should wait until we
 
49
  derermine we can't connect to a particular host.  If timeout is 0,
 
50
  connect_with_timeout() will behave exactly like connect().
 
51
 
 
52
  Base version coded by Steve Bernacki, Jr. <steve@navinet.net>
 
53
*****************************************************************************/
 
54
 
 
55
int connect_with_timeout(int fd, const struct sockaddr *name, uint32_t namelen, int32_t timeout)
 
56
{
 
57
  int flags, res, s_err;
 
58
 
 
59
  /*
 
60
    If they passed us a timeout of zero, we should behave
 
61
    exactly like the normal connect() call does.
 
62
  */
 
63
 
 
64
  if (timeout == 0)
 
65
    return connect(fd, (struct sockaddr*) name, namelen);
 
66
 
 
67
  flags = fcntl(fd, F_GETFL, 0);    /* Set socket to not block */
 
68
#ifdef O_NONBLOCK
 
69
  fcntl(fd, F_SETFL, flags | O_NONBLOCK);  /* and save the flags..  */
 
70
#endif
 
71
 
 
72
  res= connect(fd, (struct sockaddr*) name, namelen);
 
73
  s_err= errno;      /* Save the error... */
 
74
  fcntl(fd, F_SETFL, flags);
 
75
  if ((res != 0) && (s_err != EINPROGRESS))
 
76
  {
 
77
    errno= s_err;      /* Restore it */
 
78
    return(-1);
 
79
  }
 
80
  if (res == 0)        /* Connected quickly! */
 
81
    return(0);
 
82
 
 
83
  return wait_for_data(fd, timeout);
 
84
}
 
85
 
 
86
/*
 
87
  Wait up to timeout seconds for a connection to be established.
 
88
 
 
89
  We prefer to do this with poll() as there is no limitations with this.
 
90
  If not, we will use select()
 
91
*/
 
92
 
 
93
int wait_for_data(int fd, int32_t timeout)
 
94
{
 
95
  struct pollfd ufds;
 
96
  int res;
 
97
 
 
98
  ufds.fd= fd;
 
99
  ufds.events= POLLIN | POLLPRI;
 
100
  if (!(res= poll(&ufds, 1, (int) timeout*1000)))
 
101
  {
 
102
    errno= EINTR;
 
103
    return -1;
 
104
  }
 
105
  if (res < 0 || !(ufds.revents & (POLLIN | POLLPRI)) || (ufds.revents & POLLHUP))
 
106
    return -1;
 
107
  return 0;
 
108
}