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