Merge in next patch for --socket
[awesomized/libmemcached] / libtest / memcached.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached library
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
22 * written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 */
37
38
39 /*
40 Startup, and shutdown the memcached servers.
41 */
42
43 #define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT+10
44
45 #include <config.h>
46
47 #include <iso646.h>
48
49 #include <assert.h>
50 #include <limits.h>
51 #include <signal.h>
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <sys/time.h>
56 #include <time.h>
57 #include <unistd.h>
58 #include <errno.h>
59
60 #include <libmemcached/memcached.h>
61 #include <libmemcached/util.h>
62
63 #include <libtest/server.h>
64
65 static void global_sleep(void)
66 {
67 static struct timespec global_sleep_value= { 0, 50000 };
68
69 #ifdef WIN32
70 sleep(1);
71 #else
72 nanosleep(&global_sleep_value, NULL);
73 #endif
74 }
75
76 static bool wait_for_file(const char *filename)
77 {
78 uint32_t timeout= 6;
79 uint32_t waited;
80 uint32_t this_wait;
81 uint32_t retry;
82
83 for (waited= 0, retry= 1; ; retry++, waited+= this_wait)
84 {
85 if ((! access(filename, R_OK)) || (waited >= timeout))
86 {
87 return true;
88 }
89
90 this_wait= retry * retry / 3 + 1;
91 sleep(this_wait);
92 }
93
94 return false;
95 }
96
97 static void kill_file(const char *file_buffer)
98 {
99 FILE *fp;
100
101 while ((fp= fopen(file_buffer, "r")))
102 {
103 char pid_buffer[1024];
104
105 if (fgets(pid_buffer, sizeof(pid_buffer), fp) != NULL)
106 {
107 pid_t pid= (pid_t)atoi(pid_buffer);
108 if (pid != 0)
109 {
110 if (kill(pid, SIGTERM) == -1)
111 {
112 remove(file_buffer); // If this happens we may be dealing with a dead server that left its pid file.
113 }
114 else
115 {
116 uint32_t counter= 3;
117 while ((kill(pid, 0) == 0) && --counter)
118 {
119 global_sleep();
120 }
121 }
122 }
123 }
124
125 global_sleep();
126
127 fclose(fp);
128 }
129 }
130
131 void server_startup(server_startup_st *construct)
132 {
133 if ((construct->server_list= getenv("MEMCACHED_SERVERS")))
134 {
135 printf("servers %s\n", construct->server_list);
136 construct->count= 0;
137 }
138 else
139 {
140 {
141 char server_string_buffer[8096];
142 char *end_ptr;
143 end_ptr= server_string_buffer;
144
145 uint32_t port_base= 0;
146 for (uint32_t x= 0; x < construct->count; x++)
147 {
148 int status;
149
150 snprintf(construct->pid_file[x], FILENAME_MAX, "/tmp/memcached.pidXXXXXX");
151 int fd;
152 if ((fd= mkstemp(construct->pid_file[x])) == -1)
153 {
154 perror("mkstemp");
155 return;
156 }
157 close(fd);
158
159 {
160 char *var;
161 char variable_buffer[1024];
162
163 snprintf(variable_buffer, sizeof(variable_buffer), "LIBMEMCACHED_PORT_%u", x);
164
165 if ((var= getenv(variable_buffer)))
166 {
167 construct->port[x]= (in_port_t)atoi(var);
168 }
169 else
170 {
171 do {
172 construct->port[x]= (in_port_t)(x + TEST_PORT_BASE + port_base);
173
174 if (libmemcached_util_ping("localhost", construct->port[x], NULL))
175 {
176 if (libmemcached_util_flush("localhost", construct->port[x], NULL))
177 {
178 fprintf(stderr, "Found server on port %d, flushed it!\n", (int)construct->port[x]);
179 construct->is_used[x]= true;
180 } // If we can flush it, we will just use it
181 else
182 {
183 fprintf(stderr, "Found server on port %d, could not flush it, so trying next port.\n", (int)construct->port[x]);
184 port_base++;
185 construct->port[x]= 0;
186 }
187 }
188 } while (construct->port[x] == 0);
189 }
190 }
191
192 char buffer[FILENAME_MAX];
193 if (x == 0)
194 {
195 snprintf(buffer, sizeof(buffer), "%s -d -P %s -t 1 -p %u -U %u -m 128",
196 MEMCACHED_BINARY, construct->pid_file[x], construct->port[x], construct->port[x]);
197 }
198 else
199 {
200 snprintf(buffer, sizeof(buffer), "%s -d -P %s -t 1 -p %u -U %u",
201 MEMCACHED_BINARY, construct->pid_file[x], construct->port[x], construct->port[x]);
202 }
203
204 if (construct->is_used[x])
205 {
206 fprintf(stderr, "USING SERVER: %s\n", buffer);
207 }
208 else
209 {
210 if (libmemcached_util_ping("localhost", construct->port[x], NULL))
211 {
212 fprintf(stderr, "Server on port %u already exists\n", construct->port[x]);
213 }
214 else
215 {
216 status= system(buffer);
217 fprintf(stderr, "STARTING SERVER: %s status:%d\n", buffer, status);
218 }
219 }
220
221 size_t remaining_length= sizeof(server_string_buffer) - (size_t)(end_ptr -server_string_buffer);
222 int count= snprintf(end_ptr, remaining_length, "--server=localhost:%u ", construct->port[x]);
223
224 if ((size_t)count >= remaining_length or count < 0)
225 {
226 fprintf(stderr, "server names grew to be larger then buffer allowed\n");
227 abort();
228 }
229 end_ptr+= count;
230 }
231 *end_ptr= 0;
232
233
234 for (uint32_t x= 0; x < construct->count; x++)
235 {
236 if (! wait_for_file(construct->pid_file[x]))
237 {
238 abort();
239 }
240 }
241
242 for (uint32_t x= 0; x < construct->count; x++)
243 {
244 uint32_t counter= 3000; // Absurd, just to catch run away process
245
246 if (construct->is_used[x])
247 continue;
248
249 while (construct->pids[x] <= 0 && --counter)
250 {
251 FILE *file= fopen(construct->pid_file[x], "r");
252 if (file)
253 {
254 char pid_buffer[1024];
255 char *found= fgets(pid_buffer, sizeof(pid_buffer), file);
256
257 if (found)
258 {
259 construct->pids[x]= atoi(pid_buffer);
260 fclose(file);
261
262 if (construct->pids[x] > 0)
263 break;
264 }
265 fclose(file);
266 }
267
268 switch (errno)
269 {
270 default:
271 fprintf(stderr, "Could not open pid file %s -> fopen(%s) -> %s:%d\n", construct->pid_file[x], strerror(errno), __FILE__, __LINE__);
272 abort();
273
274 case ENOENT:
275 case EINTR:
276 case EACCES:
277 case EINPROGRESS:
278 break;
279
280 case ENOTCONN:
281 continue;
282 }
283
284 // Safety 3rd, check to see if the file has gone away
285 if (! wait_for_file(construct->pid_file[x]))
286 {
287 abort();
288 }
289 }
290
291 bool was_started= false;
292 if (construct->pids[x] > 0)
293 {
294 counter= 30;
295 while (--counter)
296 {
297 if (kill(construct->pids[x], 0) == 0)
298 {
299 was_started= true;
300 break;
301 }
302 global_sleep();
303 }
304 }
305
306 if (was_started == false)
307 {
308 fprintf(stderr, "Failed to open buffer %s(%d)\n", construct->pid_file[x], construct->pids[x]);
309 for (uint32_t y= 0; y < construct->count; y++)
310 {
311 if (construct->pids[y] > 0)
312 kill(construct->pids[y], SIGTERM);
313 }
314 abort();
315 }
316 }
317
318 construct->server_list= strndup(server_string_buffer, strlen(server_string_buffer) -1);
319 }
320 }
321
322 srandom((unsigned int)time(NULL));
323
324 printf("\n");
325 }
326
327 void server_shutdown(server_startup_st *construct)
328 {
329 if (construct->server_list)
330 {
331 for (uint32_t x= 0; x < construct->count; x++)
332 {
333 if (construct->is_used[x])
334 continue;
335
336 kill_file(construct->pid_file[x]);
337 }
338
339 free(construct->server_list);
340 }
341 }