Merge in libtest updates.
[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 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37 #include <libtest/common.h>
38 #include <libtest/blobslap_worker.h>
39 #include <libtest/killpid.h>
40
41 using namespace libtest;
42
43 #include <cassert>
44 #include <cerrno>
45 #include <cstdio>
46 #include <cstdlib>
47 #include <cstring>
48 #include <iostream>
49 #include <signal.h>
50 #include <sys/types.h>
51 #include <sys/wait.h>
52 #include <unistd.h>
53
54 #include <libgearman/gearman.h>
55
56 #ifndef __INTEL_COMPILER
57 #pragma GCC diagnostic ignored "-Wold-style-cast"
58 #endif
59
60 using namespace libtest;
61
62 class BlobslapWorker : public Server
63 {
64 private:
65 public:
66 BlobslapWorker(in_port_t port_arg) :
67 Server("localhost", port_arg)
68 { }
69
70 pid_t get_pid(bool error_is_ok)
71 {
72 if (not pid_file().empty())
73 {
74 Wait wait(pid_file(), 0);
75
76 if (error_is_ok and not wait.successful())
77 {
78 Error << "Pidfile was not found:" << pid_file();
79 return -1;
80
81 return get_pid_from_file(pid_file());
82 }
83 }
84
85 return -1;
86 }
87
88 bool ping()
89 {
90 if (pid_file().empty())
91 {
92 Error << "No pid file available";
93 return false;
94 }
95
96 Wait wait(pid_file(), 0);
97 if (not wait.successful())
98 {
99 Error << "Pidfile was not found:" << pid_file();
100 return false;
101 }
102
103 pid_t local_pid= get_pid_from_file(pid_file());
104 if (local_pid <= 0)
105 {
106 return false;
107 }
108
109 if (::kill(local_pid, 0) == 0)
110 {
111 return true;
112 }
113
114 return false;
115 }
116
117 const char *name()
118 {
119 return "blobslap_worker";
120 };
121
122 const char *executable()
123 {
124 return GEARMAND_BLOBSLAP_WORKER;
125 }
126
127 const char *pid_file_option()
128 {
129 return "--pid-file=";
130 }
131
132 const char *daemon_file_option()
133 {
134 return "--daemon";
135 }
136
137 const char *log_file_option()
138 {
139 return NULL;
140 }
141
142 const char *port_option()
143 {
144 return "--port=";
145 }
146
147 bool is_libtool()
148 {
149 return true;
150 }
151
152 bool build(int argc, const char *argv[]);
153 };
154
155
156 #include <sstream>
157
158 bool BlobslapWorker::build(int argc, const char *argv[])
159 {
160 std::stringstream arg_buffer;
161
162 for (int x= 1 ; x < argc ; x++)
163 {
164 arg_buffer << " " << argv[x] << " ";
165 }
166
167 set_extra_args(arg_buffer.str());
168
169 return true;
170 }
171
172 namespace libtest {
173
174 Server *build_blobslap_worker(in_port_t try_port)
175 {
176 return new BlobslapWorker(try_port);
177 }
178
179 }