a12df583e7f9db599ac403a2f6b8b006806e5ee0
[m6w6/libmemcached] / libtest / blobslap_worker.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/blobslap_worker.h>
26 #include <libtest/killpid.h>
27
28 using namespace libtest;
29
30 #include <cassert>
31 #include <cerrno>
32 #include <cstdio>
33 #include <cstdlib>
34 #include <cstring>
35 #include <iostream>
36 #include <signal.h>
37 #include <sys/types.h>
38 #include <sys/wait.h>
39 #include <unistd.h>
40
41 #include <libgearman/gearman.h>
42
43 #ifndef __INTEL_COMPILER
44 #pragma GCC diagnostic ignored "-Wold-style-cast"
45 #endif
46
47 using namespace libtest;
48
49 class BlobslapWorker : public Server
50 {
51 private:
52 public:
53 BlobslapWorker(in_port_t port_arg) :
54 Server("localhost", port_arg)
55 { }
56
57 pid_t get_pid(bool error_is_ok)
58 {
59 if (not pid_file().empty())
60 {
61 Wait wait(pid_file(), 0);
62
63 if (error_is_ok and not wait.successful())
64 {
65 Error << "Pidfile was not found:" << pid_file();
66 return -1;
67
68 return get_pid_from_file(pid_file());
69 }
70 }
71
72 return -1;
73 }
74
75 bool ping()
76 {
77 if (pid_file().empty())
78 {
79 Error << "No pid file available";
80 return false;
81 }
82
83 Wait wait(pid_file(), 0);
84 if (not wait.successful())
85 {
86 Error << "Pidfile was not found:" << pid_file();
87 return false;
88 }
89
90 pid_t local_pid= get_pid_from_file(pid_file());
91 if (local_pid <= 0)
92 {
93 return false;
94 }
95
96 if (::kill(local_pid, 0) == 0)
97 {
98 return true;
99 }
100
101 return false;
102 }
103
104 const char *name()
105 {
106 return "blobslap_worker";
107 };
108
109 const char *executable()
110 {
111 return GEARMAND_BLOBSLAP_WORKER;
112 }
113
114 const char *pid_file_option()
115 {
116 return "--pid-file=";
117 }
118
119 const char *daemon_file_option()
120 {
121 return "--daemon";
122 }
123
124 const char *log_file_option()
125 {
126 return NULL;
127 }
128
129 const char *port_option()
130 {
131 return "--port=";
132 }
133
134 bool is_libtool()
135 {
136 return true;
137 }
138
139 bool build(int argc, const char *argv[]);
140 };
141
142
143 #include <sstream>
144
145 bool BlobslapWorker::build(int argc, const char *argv[])
146 {
147 std::stringstream arg_buffer;
148
149 for (int x= 1 ; x < argc ; x++)
150 {
151 arg_buffer << " " << argv[x] << " ";
152 }
153
154 set_extra_args(arg_buffer.str());
155
156 return true;
157 }
158
159 namespace libtest {
160
161 Server *build_blobslap_worker(in_port_t try_port)
162 {
163 return new BlobslapWorker(try_port);
164 }
165
166 }