~percona-dev/percona-server/5.1-kill_idle_transaction-innodb_fake_changes

« back to all changes in this revision

Viewing changes to HandlerSocket-Plugin-for-MySQL/libhsclient/socket.cpp

  • Committer: Aleksandr Kuzminsky
  • Date: 2010-11-24 08:58:24 UTC
  • mto: This revision was merged to the branch mainline in revision 155.
  • Revision ID: aleksandr.kuzminsky@percona.com-20101124085824-9742ytdt2x50djw3
Added HandlerSocket-Plugin-for-MySQL sources

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
// vim:sw=2:ai
 
3
 
 
4
/*
 
5
 * Copyright (C) 2010 DeNA Co.,Ltd.. All rights reserved.
 
6
 * See COPYRIGHT.txt for details.
 
7
 */
 
8
 
 
9
#include <stdexcept>
 
10
#include <string.h>
 
11
#include <errno.h>
 
12
#include <unistd.h>
 
13
#include <fcntl.h>
 
14
#include <signal.h>
 
15
#include <sys/un.h>
 
16
 
 
17
#include "socket.hpp"
 
18
#include "string_util.hpp"
 
19
#include "fatal.hpp"
 
20
 
 
21
namespace dena {
 
22
 
 
23
void
 
24
ignore_sigpipe()
 
25
{
 
26
  if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) {
 
27
    fatal_abort("SIGPIPE SIG_IGN");
 
28
  }
 
29
}
 
30
 
 
31
void
 
32
socket_args::set(const config& conf)
 
33
{
 
34
  timeout = conf.get_int("timeout", 600);
 
35
  listen_backlog = conf.get_int("listen_backlog", 256);
 
36
  std::string node = conf.get_str("host", "");
 
37
  std::string port = conf.get_str("port", "");
 
38
  if (!node.empty() || !port.empty()) {
 
39
    if (family == AF_UNIX || node == "/") {
 
40
      set_unix_domain(port.c_str());
 
41
    } else {
 
42
      const char *nd = node.empty() ? 0 : node.c_str();
 
43
      if (resolve(nd, port.c_str()) != 0) {
 
44
        fatal_exit("getaddrinfo failed: " + node + ":" + port);
 
45
      }
 
46
    }
 
47
  }
 
48
  sndbuf = conf.get_int("sndbuf", 0);
 
49
  rcvbuf = conf.get_int("rcvbuf", 0);
 
50
}
 
51
 
 
52
void
 
53
socket_args::set_unix_domain(const char *path)
 
54
{
 
55
  family = AF_UNIX; 
 
56
  addr = sockaddr_storage();
 
57
  addrlen = sizeof(sockaddr_un);
 
58
  sockaddr_un *const ap = reinterpret_cast<sockaddr_un *>(&addr);
 
59
  ap->sun_family = AF_UNIX;
 
60
  strncpy(ap->sun_path, path, sizeof(ap->sun_path) - 1);
 
61
}
 
62
 
 
63
int
 
64
socket_args::resolve(const char *node, const char *service)
 
65
{
 
66
  const int flags = (node == 0) ? AI_PASSIVE : 0;
 
67
  auto_addrinfo ai;
 
68
  addr = sockaddr_storage();
 
69
  addrlen = 0;
 
70
  const int r = ai.resolve(node, service, flags, family, socktype, protocol);
 
71
  if (r != 0) {
 
72
    return r;
 
73
  }
 
74
  memcpy(&addr, ai.get()->ai_addr, ai.get()->ai_addrlen);
 
75
  addrlen = ai.get()->ai_addrlen;
 
76
  return 0;
 
77
}
 
78
 
 
79
int
 
80
socket_set_options(auto_file& fd, const socket_args& args, std::string& err_r)
 
81
{
 
82
  if (args.timeout != 0 && !args.nonblocking) {
 
83
    struct timeval tv = { };
 
84
    tv.tv_sec = args.timeout;
 
85
    tv.tv_usec = 0;
 
86
    if (setsockopt(fd.get(), SOL_SOCKET, SO_RCVTIMEO, &tv, sizeof(tv)) != 0) {
 
87
      return errno_string("setsockopt SO_RCVTIMEO", errno, err_r);
 
88
    }
 
89
    tv.tv_sec = args.timeout;
 
90
    tv.tv_usec = 0;
 
91
    if (setsockopt(fd.get(), SOL_SOCKET, SO_SNDTIMEO, &tv, sizeof(tv)) != 0) {
 
92
      return errno_string("setsockopt SO_RCVTIMEO", errno, err_r);
 
93
    }
 
94
  }
 
95
  if (args.nonblocking && fcntl(fd.get(), F_SETFL, O_NONBLOCK) != 0) {
 
96
    return errno_string("fcntl O_NONBLOCK", errno, err_r);
 
97
  }
 
98
  if (args.sndbuf != 0) {
 
99
    const int v = args.sndbuf;
 
100
    if (setsockopt(fd.get(), SOL_SOCKET, SO_SNDBUF, &v, sizeof(v)) != 0) {
 
101
      return errno_string("setsockopt SO_SNDBUF", errno, err_r);
 
102
    }
 
103
  }
 
104
  if (args.rcvbuf != 0) {
 
105
    const int v = args.rcvbuf;
 
106
    if (setsockopt(fd.get(), SOL_SOCKET, SO_RCVBUF, &v, sizeof(v)) != 0) {
 
107
      return errno_string("setsockopt SO_RCVBUF", errno, err_r);
 
108
    }
 
109
  }
 
110
  return 0;
 
111
}
 
112
 
 
113
int
 
114
socket_open(auto_file& fd, const socket_args& args, std::string& err_r)
 
115
{
 
116
  fd.reset(socket(args.family, args.socktype, args.protocol));
 
117
  if (fd.get() < 0) {
 
118
    return errno_string("socket", errno, err_r);
 
119
  }
 
120
  return socket_set_options(fd, args, err_r);
 
121
}
 
122
 
 
123
int
 
124
socket_connect(auto_file& fd, const socket_args& args, std::string& err_r)
 
125
{
 
126
  int r = 0;
 
127
  if ((r = socket_open(fd, args, err_r)) != 0) {
 
128
    return r;
 
129
  }
 
130
  if (connect(fd.get(), reinterpret_cast<const sockaddr *>(&args.addr),
 
131
    args.addrlen) != 0) {
 
132
    if (!args.nonblocking || errno != EINPROGRESS) {
 
133
      return errno_string("connect", errno, err_r);
 
134
    }
 
135
  }
 
136
  return 0;
 
137
}
 
138
 
 
139
int
 
140
socket_bind(auto_file& fd, const socket_args& args, std::string& err_r)
 
141
{
 
142
  fd.reset(socket(args.family, args.socktype, args.protocol));
 
143
  if (fd.get() < 0) {
 
144
    return errno_string("socket", errno, err_r);
 
145
  }
 
146
  if (args.reuseaddr) {
 
147
    if (args.family == AF_UNIX) {
 
148
      const sockaddr_un *const ap =
 
149
        reinterpret_cast<const sockaddr_un *>(&args.addr);
 
150
      if (unlink(ap->sun_path) != 0 && errno != ENOENT) {
 
151
        return errno_string("unlink uds", errno, err_r);
 
152
      }
 
153
    } else {
 
154
      int v = 1;
 
155
      if (setsockopt(fd.get(), SOL_SOCKET, SO_REUSEADDR, &v, sizeof(v)) != 0) {
 
156
        return errno_string("setsockopt SO_REUSEADDR", errno, err_r);
 
157
      }
 
158
    }
 
159
  }
 
160
  if (bind(fd.get(), reinterpret_cast<const sockaddr *>(&args.addr),
 
161
    args.addrlen) != 0) {
 
162
    return errno_string("bind", errno, err_r);
 
163
  }
 
164
  if (listen(fd.get(), args.listen_backlog) != 0) {
 
165
    return errno_string("listen", errno, err_r);
 
166
  }
 
167
  if (args.nonblocking && fcntl(fd.get(), F_SETFL, O_NONBLOCK) != 0) {
 
168
    return errno_string("fcntl O_NONBLOCK", errno, err_r);
 
169
  }
 
170
  return 0;
 
171
}
 
172
 
 
173
int
 
174
socket_accept(int listen_fd, auto_file& fd, const socket_args& args,
 
175
  sockaddr_storage& addr_r, socklen_t& addrlen_r, std::string& err_r)
 
176
{
 
177
  fd.reset(accept(listen_fd, reinterpret_cast<sockaddr *>(&addr_r),
 
178
    &addrlen_r));
 
179
  if (fd.get() < 0) {
 
180
    return errno_string("accept", errno, err_r);
 
181
  }
 
182
  return socket_set_options(fd, args, err_r);
 
183
}
 
184
 
 
185
};
 
186