Merge in fixes for SASL.
[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 #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 set_pid_file();
57 }
58
59 pid_t get_pid(bool error_is_ok)
60 {
61 if (pid_file().empty())
62 {
63 Error << "pid_file was empty";
64 return -1;
65 }
66
67 Wait wait(pid_file(), 0);
68
69 if (error_is_ok and not wait.successful())
70 {
71 Error << "Pidfile was not found:" << pid_file();
72 return -1;
73 }
74
75 std::stringstream error_message;
76 pid_t ret= get_pid_from_file(pid_file(), error_message);
77
78 if (error_is_ok and is_pid_valid(ret) == false)
79 {
80 Error << error_message.str();
81 }
82
83 return ret;
84 }
85
86 bool ping()
87 {
88 if (pid_file().empty())
89 {
90 Error << "No pid file available";
91 return false;
92 }
93
94 Wait wait(pid_file(), 0);
95 if (not wait.successful())
96 {
97 Error << "Pidfile was not found:" << pid_file();
98 return false;
99 }
100
101 std::stringstream error_message;
102 pid_t local_pid= get_pid_from_file(pid_file(), error_message);
103 if (is_pid_valid(local_pid) == false)
104 {
105 Error << error_message.str();
106 return false;
107 }
108
109 // Use kill to determine is the process exist
110 if (::kill(local_pid, 0) == 0)
111 {
112 return true;
113 }
114
115 return false;
116 }
117
118 const char *name()
119 {
120 return "blobslap_worker";
121 };
122
123 const char *executable()
124 {
125 return GEARMAND_BLOBSLAP_WORKER;
126 }
127
128 const char *pid_file_option()
129 {
130 return "--pid-file=";
131 }
132
133 const char *daemon_file_option()
134 {
135 return "--daemon";
136 }
137
138 const char *log_file_option()
139 {
140 return "--log-file=";
141 }
142
143 const char *port_option()
144 {
145 return "--port=";
146 }
147
148 bool is_libtool()
149 {
150 return true;
151 }
152
153 bool build(int argc, const char *argv[]);
154 };
155
156
157 #include <sstream>
158
159 bool BlobslapWorker::build(int argc, const char *argv[])
160 {
161 std::stringstream arg_buffer;
162
163 for (int x= 1 ; x < argc ; x++)
164 {
165 arg_buffer << " " << argv[x] << " ";
166 }
167
168 set_extra_args(arg_buffer.str());
169
170 return true;
171 }
172
173 namespace libtest {
174
175 Server *build_blobslap_worker(in_port_t try_port)
176 {
177 return new BlobslapWorker(try_port);
178 }
179
180 }