Merge lp:~brianaker/libmemcached/exception-cleanup/ Build: jenkins-Libmemcached-272
[awesomized/libmemcached] / libtest / has.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Data Differential YATL (i.e. libtest) library
4 *
5 * Copyright (C) 2012-2013 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/yatlcon.h"
38 #include <libtest/common.h>
39
40 #include <cstdio>
41 #include <cstdlib>
42 #include <unistd.h>
43
44 namespace libtest {
45
46 bool has_libmemcached_sasl(void)
47 {
48 #if defined(LIBMEMCACHED_WITH_SASL_SUPPORT) && LIBMEMCACHED_WITH_SASL_SUPPORT
49 if (LIBMEMCACHED_WITH_SASL_SUPPORT)
50 {
51 return true;
52 }
53 #endif
54
55 return false;
56 }
57
58 bool has_libmemcached(void)
59 {
60 #if defined(HAVE_LIBMEMCACHED) && HAVE_LIBMEMCACHED
61 if (HAVE_LIBMEMCACHED)
62 {
63 return true;
64 }
65 #endif
66
67 return false;
68 }
69
70 bool has_libdrizzle(void)
71 {
72 #if defined(HAVE_LIBDRIZZLE) && HAVE_LIBDRIZZLE
73 if (HAVE_LIBDRIZZLE)
74 {
75 return true;
76 }
77 #endif
78
79 return false;
80 }
81
82 bool has_postgres_support(void)
83 {
84 char *getenv_ptr;
85 if (bool((getenv_ptr= getenv("POSTGES_IS_RUNNING_AND_SETUP"))))
86 {
87 (void)(getenv_ptr);
88 if (HAVE_LIBPQ)
89 {
90 return true;
91 }
92 }
93
94 return false;
95 }
96
97
98 bool has_gearmand()
99 {
100 #if defined(GEARMAND_BINARY) && defined(HAVE_GEARMAND_BINARY) && HAVE_GEARMAND_BINARY
101 if (HAVE_GEARMAND_BINARY)
102 {
103 std::stringstream arg_buffer;
104
105 char *getenv_ptr;
106 if (bool((getenv_ptr= getenv("PWD"))) and
107 ((strcmp(GEARMAND_BINARY, "./gearmand/gearmand") == 0) or (strcmp(GEARMAND_BINARY, "gearmand/gearmand") == 0)))
108 {
109 arg_buffer << getenv_ptr;
110 arg_buffer << "/";
111 }
112 arg_buffer << GEARMAND_BINARY;
113
114 if (access(arg_buffer.str().c_str(), X_OK) == 0)
115 {
116 return true;
117 }
118 }
119 #endif
120
121 return false;
122 }
123
124 bool has_drizzled()
125 {
126 #if defined(DRIZZLED_BINARY) && defined(HAVE_DRIZZLED_BINARY) && HAVE_DRIZZLED_BINARY
127 if (HAVE_DRIZZLED_BINARY)
128 {
129 if (access(DRIZZLED_BINARY, X_OK) == 0)
130 {
131 return true;
132 }
133 }
134 #endif
135
136 return false;
137 }
138
139 bool has_mysqld()
140 {
141 #if defined(MYSQLD_BINARY) && defined(HAVE_MYSQLD_BUILD) && HAVE_MYSQLD_BUILD
142 if (HAVE_MYSQLD_BUILD)
143 {
144 if (access(MYSQLD_BINARY, X_OK) == 0)
145 {
146 return true;
147 }
148 }
149 #endif
150
151 return false;
152 }
153
154 static char memcached_binary_path[FILENAME_MAX];
155
156 static void initialize_memcached_binary_path()
157 {
158 memcached_binary_path[0]= 0;
159
160 #if defined(MEMCACHED_BINARY) && defined(HAVE_MEMCACHED_BINARY) && HAVE_MEMCACHED_BINARY
161 if (HAVE_MEMCACHED_BINARY)
162 {
163 std::stringstream arg_buffer;
164
165 char *getenv_ptr;
166 if (bool((getenv_ptr= getenv("PWD"))) and strcmp(MEMCACHED_BINARY, "memcached/memcached") == 0)
167 {
168 arg_buffer << getenv_ptr;
169 arg_buffer << "/";
170 }
171 arg_buffer << MEMCACHED_BINARY;
172
173 if (access(arg_buffer.str().c_str(), X_OK) == 0)
174 {
175 strncpy(memcached_binary_path, arg_buffer.str().c_str(), FILENAME_MAX);
176 }
177 }
178 #endif
179 }
180
181 static pthread_once_t memcached_binary_once= PTHREAD_ONCE_INIT;
182 static void initialize_memcached_binary(void)
183 {
184 int ret;
185 if ((ret= pthread_once(&memcached_binary_once, initialize_memcached_binary_path)) != 0)
186 {
187 FATAL(strerror(ret));
188 }
189 }
190
191 bool has_memcached()
192 {
193 initialize_memcached_binary();
194
195 if (memcached_binary_path[0])
196 if (memcached_binary_path[0] and (strlen(memcached_binary_path) > 0))
197 {
198 return true;
199 }
200
201 return false;
202 }
203
204 const char* memcached_binary()
205 {
206 initialize_memcached_binary();
207
208 if (memcached_binary_path[0])
209 {
210 return memcached_binary_path;
211 }
212
213 return NULL;
214 }
215
216 static char memcached_sasl_binary_path[FILENAME_MAX];
217
218 static void initialize_has_memcached_sasl()
219 {
220 memcached_sasl_binary_path[0]= 0;
221
222 #if defined(MEMCACHED_BINARY) && defined(HAVE_MEMCACHED_BINARY) && HAVE_MEMCACHED_BINARY
223 if (HAVE_MEMCACHED_BINARY)
224 {
225 std::stringstream arg_buffer;
226
227 char *getenv_ptr;
228 if (bool((getenv_ptr= getenv("PWD"))) and strcmp(MEMCACHED_BINARY, "memcached/memcached") == 0)
229 {
230 arg_buffer << getenv_ptr;
231 arg_buffer << "/";
232 }
233 arg_buffer << MEMCACHED_BINARY;
234
235 if (access(arg_buffer.str().c_str(), X_OK) == 0)
236 {
237 strncpy(memcached_sasl_binary_path, arg_buffer.str().c_str(), FILENAME_MAX);
238 }
239 }
240 #endif
241 }
242
243 bool has_memcached_sasl()
244 {
245 initialize_has_memcached_sasl();
246
247 if (memcached_sasl_binary_path[0] and (strlen(memcached_sasl_binary_path) > 0))
248 {
249 return true;
250 }
251
252 return false;
253 }
254
255 const char *gearmand_binary()
256 {
257 #if defined(GEARMAND_BINARY)
258 return GEARMAND_BINARY;
259 #else
260 return NULL;
261 #endif
262 }
263
264 const char *drizzled_binary()
265 {
266 #if defined(DRIZZLED_BINARY)
267 return DRIZZLED_BINARY;
268 #else
269 return NULL;
270 #endif
271 }
272
273 } // namespace libtest