~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

Viewing changes to drizzled/plugin/listen_tcp.cc

  • Committer: Package Import Robot
  • Author(s): Clint Byrum
  • Date: 2012-06-19 10:46:49 UTC
  • mfrom: (1.1.6)
  • mto: This revision was merged to the branch mainline in revision 29.
  • Revision ID: package-import@ubuntu.com-20120619104649-e2l0ggd4oz3um0f4
Tags: upstream-7.1.36-stable
ImportĀ upstreamĀ versionĀ 7.1.36-stable

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
#include <netinet/tcp.h>
33
33
#include <cerrno>
34
34
 
35
 
#define MAX_ACCEPT_RETRY        10      // Test accept this many times
 
35
namespace drizzled {
36
36
 
37
 
namespace drizzled
38
 
{
39
37
extern back_log_constraints back_log;
40
38
extern uint32_t drizzled_bind_timeout;
41
39
 
42
 
 
43
40
int plugin::ListenTcp::acceptTcp(int fd)
44
41
{
45
 
  int new_fd;
46
 
  uint32_t retry;
47
 
 
48
 
  for (retry= 0; retry < MAX_ACCEPT_RETRY; retry++)
 
42
  for (int retry= 0; retry < 10; retry++)
49
43
  {
50
 
    new_fd= accept(fd, NULL, 0);
51
 
    if (new_fd != -1 || (errno != EINTR && errno != EAGAIN))
 
44
    int new_fd= accept(fd, NULL, 0);
 
45
    if (new_fd != -1)
 
46
      return new_fd;
 
47
    if (errno != EINTR && errno != EAGAIN)
52
48
      break;
53
49
  }
54
 
 
55
 
  if (new_fd == -1)
 
50
  if ((accept_error_count++ & 255) == 0)
56
51
  {
57
 
    if ((accept_error_count++ & 255) == 0)
58
 
    {
59
 
      sql_perror(_("accept() failed with errno %d"));
60
 
    }
61
 
 
62
 
    if (errno == ENFILE || errno == EMFILE)
63
 
      sleep(1);
64
 
 
65
 
    return -1;
 
52
    sql_perror(_("accept() failed with errno %d"));
66
53
  }
67
 
 
68
 
  return new_fd;
 
54
  if (errno == ENFILE || errno == EMFILE)
 
55
    sleep(1);
 
56
  return -1;
69
57
}
70
58
 
71
59
bool plugin::ListenTcp::getFileDescriptors(std::vector<int> &fds)
73
61
  int ret;
74
62
  char host_buf[NI_MAXHOST];
75
63
  char port_buf[NI_MAXSERV];
76
 
  struct addrinfo hints;
77
 
  struct addrinfo *ai;
78
 
  struct addrinfo *ai_list;
79
 
  int fd= -1;
80
 
  uint32_t waited;
 
64
  addrinfo hints;
 
65
  addrinfo *ai_list;
81
66
  uint32_t this_wait;
82
 
  uint32_t retry;
83
 
  struct linger ling= {0, 0};
84
67
  int flags= 1;
85
68
 
86
69
  memset(&hints, 0, sizeof(struct addrinfo));
96
79
    return true;
97
80
  }
98
81
 
99
 
  for (ai= ai_list; ai != NULL; ai= ai->ai_next)
 
82
  for (addrinfo* ai= ai_list; ai != NULL; ai= ai->ai_next)
100
83
  {
101
84
    ret= getnameinfo(ai->ai_addr, ai->ai_addrlen, host_buf, NI_MAXHOST,
102
85
                     port_buf, NI_MAXSERV, NI_NUMERICHOST | NI_NUMERICSERV);
106
89
      strcpy(port_buf, "-");
107
90
    }
108
91
 
109
 
    fd= socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
 
92
    int fd= socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
110
93
    if (fd == -1)
111
94
    {
112
95
      /*
149
132
      return true;
150
133
    }
151
134
 
 
135
    linger ling= {0, 0};
152
136
    ret= setsockopt(fd, SOL_SOCKET, SO_LINGER, &ling, sizeof(ling));
153
137
    if (ret != 0)
154
138
    {
171
155
      Retry at second: 1, 3, 7, 13, 22, 35, 52, 74, ...
172
156
      Limit the sequence by drizzled_bind_timeout.
173
157
    */
174
 
    for (waited= 0, retry= 1; ; retry++, waited+= this_wait)
 
158
    for (uint32_t waited= 0, retry= 1; ; retry++, waited+= this_wait)
175
159
    {
176
160
      if (((ret= ::bind(fd, ai->ai_addr, ai->ai_addrlen)) == 0) ||
177
161
          (errno != EADDRINUSE) || (waited >= drizzled_bind_timeout))
213
197
  return false;
214
198
}
215
199
 
216
 
const std::string plugin::ListenTcp::getHost(void) const
 
200
const std::string plugin::ListenTcp::getHost() const
217
201
{
218
202
  return "";
219
203
}