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