Fix valgrind issues.
[m6w6/libmemcached] / libtest / gearmand.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * libtest
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22
23 #include <libtest/common.h>
24
25 #include <libtest/gearmand.h>
26
27 #include "util/instance.hpp"
28 #include "util/operation.hpp"
29
30 using namespace datadifferential;
31 using namespace libtest;
32
33 #include <cassert>
34 #include <cerrno>
35 #include <cstdio>
36 #include <cstdlib>
37 #include <cstring>
38 #include <iostream>
39 #include <signal.h>
40 #include <sstream>
41 #include <sys/types.h>
42 #include <sys/wait.h>
43 #include <unistd.h>
44
45 #include <libgearman/gearman.h>
46
47 #ifndef __INTEL_COMPILER
48 #pragma GCC diagnostic ignored "-Wold-style-cast"
49 #endif
50
51 class GetPid : public util::Instance::Finish
52 {
53 private:
54 pid_t _pid;
55
56 public:
57 GetPid() :
58 _pid(-1)
59 { }
60
61 pid_t pid()
62 {
63 return _pid;
64 }
65
66
67 bool call(const bool success, const std::string &response)
68 {
69 _pid= -1;
70 if (success and response.size())
71 {
72 _pid= atoi(response.c_str());
73 }
74
75 if (is_pid_valid(_pid) == false)
76 {
77 _pid= -1;
78 return false;
79 }
80
81 return true;
82 }
83 };
84
85 using namespace libtest;
86
87 class Gearmand : public libtest::Server
88 {
89 private:
90 public:
91 Gearmand(const std::string& host_arg, in_port_t port_arg) :
92 libtest::Server(host_arg, port_arg)
93 {
94 set_pid_file();
95 }
96
97 pid_t get_pid(bool error_is_ok)
98 {
99 if (pid_file().empty() == false)
100 {
101 Wait wait(pid_file(), 0);
102
103 if (error_is_ok and not wait.successful())
104 {
105 Error << "Pidfile was not found:" << pid_file();
106 return -1;
107 }
108 }
109
110 GetPid *get_instance_pid;
111 util::Instance instance(hostname(), port());
112 instance.set_finish(get_instance_pid= new GetPid);
113
114 instance.push(new util::Operation(test_literal_param("getpid\r\n"), true));
115
116 if (error_is_ok and instance.run() == false)
117 {
118 Error << "Failed to obtain pid of server";
119 }
120
121 return get_instance_pid->pid();
122 }
123
124 bool ping()
125 {
126 gearman_client_st *client= gearman_client_create(NULL);
127 if (client == NULL)
128 {
129 Error << "Could not allocate memory for gearman_client_create()";
130 return false;
131 }
132 gearman_client_set_timeout(client, 3000);
133
134 if (gearman_success(gearman_client_add_server(client, hostname().c_str(), port())))
135 {
136 gearman_return_t rc= gearman_client_echo(client, test_literal_param("This is my echo test"));
137
138 if (gearman_success(rc))
139 {
140 gearman_client_free(client);
141 return true;
142 }
143 #if 0
144 Error << hostname().c_str() << ":" << port() << " was " << gearman_strerror(rc) << " extended: " << gearman_client_error(client);
145 #endif
146 }
147 else
148 {
149 Error << "gearman_client_add_server() " << gearman_client_error(client);
150 }
151
152 gearman_client_free(client);
153
154 return false;;
155 }
156
157 const char *name()
158 {
159 return "gearmand";
160 };
161
162 const char *executable()
163 {
164 return GEARMAND_BINARY;
165 }
166
167 const char *daemon_file_option()
168 {
169 return "--daemon";
170 }
171
172 void log_file_option(Application& app, const std::string& arg)
173 {
174 if (arg.empty() == false)
175 {
176 std::string buffer("--log-file=");
177 buffer+= arg;
178 app.add_option("--verbose=DEBUG");
179 app.add_option(buffer);
180 }
181 }
182
183 bool has_log_file_option() const
184 {
185 return true;
186 }
187
188 bool is_libtool()
189 {
190 return true;
191 }
192
193 bool has_syslog() const
194 {
195 return true;
196 }
197
198 bool has_port_option() const
199 {
200 return true;
201 }
202
203 bool build(int argc, const char *argv[]);
204 };
205
206 bool Gearmand::build(int argc, const char *argv[])
207 {
208 std::stringstream arg_buffer;
209
210 if (getuid() == 0 or geteuid() == 0)
211 {
212 add_option("-u", "root");
213 }
214
215 add_option("--listen=localhost");
216
217 for (int x= 1 ; x < argc ; x++)
218 {
219 add_option(argv[x]);
220 }
221
222 return true;
223 }
224
225 namespace libtest {
226
227 libtest::Server *build_gearmand(const char *hostname, in_port_t try_port)
228 {
229 return new Gearmand(hostname, try_port);
230 }
231
232 }