1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems, Inc.
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.
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.
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
20
/* Various helper functions not intended to be part of a public API */
22
#include <drizzled/global.h>
23
#include "libdrizzle_priv.h"
27
const char *unknown_sqlstate= "HY000";
28
const char *not_error_sqlstate= "00000";
29
const char *cant_connect_sqlstate= "08001";
31
const char * sqlstate_get_unknown(void)
33
return unknown_sqlstate;
36
const char * sqlstate_get_not_error(void)
38
return not_error_sqlstate;
41
const char * sqlstate_get_cant_connect(void)
43
return cant_connect_sqlstate;
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().
52
Base version coded by Steve Bernacki, Jr. <steve@navinet.net>
53
*****************************************************************************/
55
int connect_with_timeout(int fd, const struct sockaddr *name, uint32_t namelen, int32_t timeout)
57
int flags, res, s_err;
60
If they passed us a timeout of zero, we should behave
61
exactly like the normal connect() call does.
65
return connect(fd, (struct sockaddr*) name, namelen);
67
flags = fcntl(fd, F_GETFL, 0); /* Set socket to not block */
69
fcntl(fd, F_SETFL, flags | O_NONBLOCK); /* and save the flags.. */
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))
77
errno= s_err; /* Restore it */
80
if (res == 0) /* Connected quickly! */
83
return wait_for_data(fd, timeout);
87
Wait up to timeout seconds for a connection to be established.
89
We prefer to do this with poll() as there is no limitations with this.
90
If not, we will use select()
93
int wait_for_data(int fd, int32_t timeout)
99
ufds.events= POLLIN | POLLPRI;
100
if (!(res= poll(&ufds, 1, (int) timeout*1000)))
105
if (res < 0 || !(ufds.revents & (POLLIN | POLLPRI)) || (ufds.revents & POLLHUP))