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