~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to libdrizzle/libdrizzle_priv.c

  • Committer: Brian Aker
  • Date: 2008-11-04 15:39:09 UTC
  • mfrom: (575.1.2 devel)
  • Revision ID: brian@tangent.org-20081104153909-c72hn65udxs1ccal
Merge of Monty's work

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 <config.h>
 
23
#include "libdrizzle_priv.h"
 
24
#include <poll.h>
 
25
#include <fcntl.h>
 
26
 
 
27
const char _dig_vec_upper[] =
 
28
  "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
 
29
const char _dig_vec_lower[] =
 
30
  "0123456789abcdefghijklmnopqrstuvwxyz";
 
31
 
 
32
const char  *unknown_sqlstate= "HY000";
 
33
const char  *not_error_sqlstate= "00000";
 
34
const char  *cant_connect_sqlstate= "08001";
 
35
 
 
36
const char * sqlstate_get_unknown(void)
 
37
{
 
38
  return unknown_sqlstate;
 
39
}
 
40
 
 
41
const char * sqlstate_get_not_error(void)
 
42
{
 
43
  return not_error_sqlstate;
 
44
}
 
45
 
 
46
const char * sqlstate_get_cant_connect(void)
 
47
{
 
48
  return cant_connect_sqlstate;
 
49
}
 
50
 
 
51
/****************************************************************************
 
52
  A modified version of connect().  connect_with_timeout() allows you to specify
 
53
  a timeout value, in seconds, that we should wait until we
 
54
  derermine we can't connect to a particular host.  If timeout is 0,
 
55
  connect_with_timeout() will behave exactly like connect().
 
56
 
 
57
  Base version coded by Steve Bernacki, Jr. <steve@navinet.net>
 
58
*****************************************************************************/
 
59
 
 
60
int connect_with_timeout(int fd, const struct sockaddr *name, uint32_t namelen, int32_t timeout)
 
61
{
 
62
  int flags, res, s_err;
 
63
 
 
64
  /*
 
65
    If they passed us a timeout of zero, we should behave
 
66
    exactly like the normal connect() call does.
 
67
  */
 
68
 
 
69
  if (timeout == 0)
 
70
    return connect(fd, (struct sockaddr*) name, namelen);
 
71
 
 
72
  flags = fcntl(fd, F_GETFL, 0);    /* Set socket to not block */
 
73
#ifdef O_NONBLOCK
 
74
  fcntl(fd, F_SETFL, flags | O_NONBLOCK);  /* and save the flags..  */
 
75
#endif
 
76
 
 
77
  res= connect(fd, (struct sockaddr*) name, namelen);
 
78
  s_err= errno;      /* Save the error... */
 
79
  fcntl(fd, F_SETFL, flags);
 
80
  if ((res != 0) && (s_err != EINPROGRESS))
 
81
  {
 
82
    errno= s_err;      /* Restore it */
 
83
    return(-1);
 
84
  }
 
85
  if (res == 0)        /* Connected quickly! */
 
86
    return(0);
 
87
 
 
88
  return wait_for_data(fd, timeout);
 
89
}
 
90
 
 
91
/*
 
92
  Wait up to timeout seconds for a connection to be established.
 
93
 
 
94
  We prefer to do this with poll() as there is no limitations with this.
 
95
  If not, we will use select()
 
96
*/
 
97
 
 
98
int wait_for_data(int fd, int32_t timeout)
 
99
{
 
100
  struct pollfd ufds;
 
101
  int res;
 
102
 
 
103
  ufds.fd= fd;
 
104
  ufds.events= POLLIN | POLLPRI;
 
105
  if (!(res= poll(&ufds, 1, (int) timeout*1000)))
 
106
  {
 
107
    errno= EINTR;
 
108
    return -1;
 
109
  }
 
110
  if (res < 0 || !(ufds.revents & (POLLIN | POLLPRI)) || (ufds.revents & POLLHUP))
 
111
    return -1;
 
112
  return 0;
 
113
}