reorganize directories
[m6w6/libmemcached] / src / 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 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
49 return LIBMEMCACHED_WITH_SASL_SUPPORT;
50 #else
51 return false;
52 #endif
53 }
54
55 bool has_libmemcached(void)
56 {
57 #if defined(HAVE_LIBMEMCACHED) && HAVE_LIBMEMCACHED
58 if (HAVE_LIBMEMCACHED)
59 {
60 return true;
61 }
62 #endif
63
64 return false;
65 }
66
67 bool has_libdrizzle(void)
68 {
69 #if defined(HAVE_LIBDRIZZLE) && HAVE_LIBDRIZZLE
70 if (HAVE_LIBDRIZZLE)
71 {
72 return true;
73 }
74 #endif
75
76 return false;
77 }
78
79 bool has_postgres_support(void)
80 {
81 char *getenv_ptr;
82 if (bool((getenv_ptr= getenv("POSTGRES_IS_RUNNING_AND_SETUP"))))
83 {
84 (void)(getenv_ptr);
85 #if defined(HAVE_LIBPQ) && HAVE_LIBPQ
86 if (HAVE_LIBPQ)
87 {
88 return true;
89 }
90 #endif
91 }
92
93 return false;
94 }
95
96
97 bool has_gearmand()
98 {
99 #if defined(GEARMAND_BINARY) && defined(HAVE_GEARMAND_BINARY) && HAVE_GEARMAND_BINARY
100 if (HAVE_GEARMAND_BINARY)
101 {
102 std::stringstream arg_buffer;
103
104 char *getenv_ptr;
105 if (bool((getenv_ptr= getenv("PWD"))) and
106 ((strcmp(GEARMAND_BINARY, "./gearmand/gearmand") == 0) or (strcmp(GEARMAND_BINARY, "gearmand/gearmand") == 0)))
107 {
108 arg_buffer << getenv_ptr;
109 arg_buffer << "/";
110 }
111 arg_buffer << GEARMAND_BINARY;
112
113 if (access(arg_buffer.str().c_str(), X_OK) == 0)
114 {
115 return true;
116 }
117 }
118 #endif
119
120 return false;
121 }
122
123 bool has_drizzled()
124 {
125 #if defined(DRIZZLED_BINARY) && defined(HAVE_DRIZZLED_BINARY) && HAVE_DRIZZLED_BINARY
126 if (HAVE_DRIZZLED_BINARY)
127 {
128 if (access(DRIZZLED_BINARY, X_OK) == 0)
129 {
130 return true;
131 }
132 }
133 #endif
134
135 return false;
136 }
137
138 bool has_mysqld()
139 {
140 #if defined(MYSQLD_BINARY) && defined(HAVE_MYSQLD_BUILD) && HAVE_MYSQLD_BUILD
141 if (HAVE_MYSQLD_BUILD)
142 {
143 if (access(MYSQLD_BINARY, X_OK) == 0)
144 {
145 return true;
146 }
147 }
148 #endif
149
150 return false;
151 }
152
153 static char memcached_binary_path[FILENAME_MAX];
154
155 static void initialize_memcached_binary_path()
156 {
157 memcached_binary_path[0]= 0;
158
159 #if defined(MEMCACHED_BINARY) && defined(HAVE_MEMCACHED_BINARY) && HAVE_MEMCACHED_BINARY
160 if (HAVE_MEMCACHED_BINARY)
161 {
162 std::stringstream arg_buffer;
163
164 char *getenv_ptr;
165 if (bool((getenv_ptr= getenv("PWD"))) and strcmp(MEMCACHED_BINARY, "memcached/memcached") == 0)
166 {
167 arg_buffer << getenv_ptr;
168 arg_buffer << "/";
169 }
170 arg_buffer << MEMCACHED_BINARY;
171
172 if (access(arg_buffer.str().c_str(), X_OK) == 0)
173 {
174 strncpy(memcached_binary_path, arg_buffer.str().c_str(), FILENAME_MAX-1);
175 }
176 }
177 #endif
178 }
179
180 static pthread_once_t memcached_binary_once= PTHREAD_ONCE_INIT;
181 static void initialize_memcached_binary(void)
182 {
183 int ret;
184 if ((ret= pthread_once(&memcached_binary_once, initialize_memcached_binary_path)) != 0)
185 {
186 FATAL(strerror(ret));
187 }
188 }
189
190 bool has_memcached()
191 {
192 initialize_memcached_binary();
193
194 if (memcached_binary_path[0] and (strlen(memcached_binary_path) > 0))
195 {
196 return true;
197 }
198
199 return false;
200 }
201
202 const char* memcached_binary()
203 {
204 initialize_memcached_binary();
205
206 if (memcached_binary_path[0])
207 {
208 return memcached_binary_path;
209 }
210
211 return NULL;
212 }
213
214 const char *gearmand_binary()
215 {
216 #if defined(GEARMAND_BINARY)
217 return GEARMAND_BINARY;
218 #else
219 return NULL;
220 #endif
221 }
222
223 const char *drizzled_binary()
224 {
225 #if defined(DRIZZLED_BINARY)
226 return DRIZZLED_BINARY;
227 #else
228 return NULL;
229 #endif
230 }
231
232 } // namespace libtest