Merge in test.hpp
[m6w6/libmemcached] / libtest / server.c
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 struct timespec global_sleep_value= { .tv_sec= 0, .tv_nsec= 50000 };
66
67 static void global_sleep(void)
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->servers= memcached_servers_parse(construct->server_list);
137 construct->server_list= NULL;
138 construct->count= 0;
139 }
140 else
141 {
142 {
143 char server_string_buffer[8096];
144 char *end_ptr;
145 end_ptr= server_string_buffer;
146
147 uint32_t port_base= 0;
148 for (uint32_t x= 0; x < construct->count; x++)
149 {
150 int status;
151
152 snprintf(construct->pid_file[x], FILENAME_MAX, "/tmp/memcached.pidXXXXXX");
153 int fd;
154 if ((fd= mkstemp(construct->pid_file[x])) == -1)
155 {
156 perror("mkstemp");
157 return;
158 }
159 close(fd);
160
161 {
162 char *var;
163 char variable_buffer[1024];
164
165 snprintf(variable_buffer, sizeof(variable_buffer), "LIBMEMCACHED_PORT_%u", x);
166
167 if ((var= getenv(variable_buffer)))
168 {
169 construct->port[x]= (in_port_t)atoi(var);
170 }
171 else
172 {
173 do {
174 construct->port[x]= (in_port_t)(x + TEST_PORT_BASE + port_base);
175
176 if (libmemcached_util_ping("localhost", construct->port[x], NULL))
177 {
178 if (libmemcached_util_flush("localhost", construct->port[x], NULL))
179 {
180 fprintf(stderr, "Found server on port %d, flushed it!\n", (int)construct->port[x]);
181 construct->is_used[x]= true;
182 } // If we can flush it, we will just use it
183 else
184 {
185 fprintf(stderr, "Found server on port %d, could not flush it, so trying next port.\n", (int)construct->port[x]);
186 port_base++;
187 construct->port[x]= 0;
188 }
189 }
190 } while (construct->port[x] == 0);
191 }
192 }
193
194 char buffer[FILENAME_MAX];
195 if (x == 0)
196 {
197 snprintf(buffer, sizeof(buffer), "%s -d -P %s -t 1 -p %u -U %u -m 128",
198 MEMCACHED_BINARY, construct->pid_file[x], construct->port[x], construct->port[x]);
199 }
200 else
201 {
202 snprintf(buffer, sizeof(buffer), "%s -d -P %s -t 1 -p %u -U %u",
203 MEMCACHED_BINARY, construct->pid_file[x], construct->port[x], construct->port[x]);
204 }
205
206 if (construct->is_used[x])
207 {
208 fprintf(stderr, "USING SERVER: %s\n", buffer);
209 }
210 else
211 {
212 if (libmemcached_util_ping("localhost", construct->port[x], NULL))
213 {
214 fprintf(stderr, "Server on port %u already exists\n", construct->port[x]);
215 }
216 else
217 {
218 status= system(buffer);
219 fprintf(stderr, "STARTING SERVER: %s status:%d\n", buffer, status);
220 }
221 }
222
223 int count;
224 size_t remaining_length= sizeof(server_string_buffer) - (size_t)(end_ptr -server_string_buffer);
225 count= snprintf(end_ptr, remaining_length, "localhost:%u,", construct->port[x]);
226
227 if ((size_t)count >= remaining_length || count < 0)
228 {
229 fprintf(stderr, "server names grew to be larger then buffer allowed\n");
230 abort();
231 }
232 end_ptr+= count;
233 }
234 *end_ptr= 0;
235
236
237 for (uint32_t x= 0; x < construct->count; x++)
238 {
239 if (! wait_for_file(construct->pid_file[x]))
240 {
241 abort();
242 }
243 }
244
245 for (uint32_t x= 0; x < construct->count; x++)
246 {
247 uint32_t counter= 3000; // Absurd, just to catch run away process
248
249 if (construct->is_used[x])
250 continue;
251
252 while (construct->pids[x] <= 0 && --counter)
253 {
254 FILE *file= fopen(construct->pid_file[x], "r");
255 if (file)
256 {
257 char pid_buffer[1024];
258 char *found= fgets(pid_buffer, sizeof(pid_buffer), file);
259
260 if (found)
261 {
262 construct->pids[x]= atoi(pid_buffer);
263 fclose(file);
264
265 if (construct->pids[x] > 0)
266 break;
267 }
268 fclose(file);
269 }
270
271 switch (errno)
272 {
273 default:
274 fprintf(stderr, "Could not open pid file %s -> fopen(%s) -> %s:%d\n", construct->pid_file[x], strerror(errno), __FILE__, __LINE__);
275 abort();
276
277 case ENOENT:
278 case EINTR:
279 case EACCES:
280 case EINPROGRESS:
281 break;
282
283 case ENOTCONN:
284 continue;
285 }
286
287 // Safety 3rd, check to see if the file has gone away
288 if (! wait_for_file(construct->pid_file[x]))
289 {
290 abort();
291 }
292 }
293
294 bool was_started= false;
295 if (construct->pids[x] > 0)
296 {
297 counter= 30;
298 while (--counter)
299 {
300 if (kill(construct->pids[x], 0) == 0)
301 {
302 was_started= true;
303 break;
304 }
305 global_sleep();
306 }
307 }
308
309 if (was_started == false)
310 {
311 fprintf(stderr, "Failed to open buffer %s(%d)\n", construct->pid_file[x], construct->pids[x]);
312 for (uint32_t y= 0; y < construct->count; y++)
313 {
314 if (construct->pids[y] > 0)
315 kill(construct->pids[y], SIGTERM);
316 }
317 abort();
318 }
319 }
320
321 construct->server_list= strdup(server_string_buffer);
322 }
323 printf("servers %s\n", construct->server_list);
324 construct->servers= memcached_servers_parse(construct->server_list);
325 }
326
327 assert(construct->servers);
328
329 srandom((unsigned int)time(NULL));
330
331 for (uint32_t x= 0; x < memcached_server_list_count(construct->servers); x++)
332 {
333 printf("\t%s : %d\n", memcached_server_name(&construct->servers[x]), memcached_server_port(&construct->servers[x]));
334 assert(construct->servers[x].fd == -1);
335 assert(construct->servers[x].cursor_active == 0);
336 }
337
338 printf("\n");
339 }
340
341 void server_shutdown(server_startup_st *construct)
342 {
343 if (construct->server_list)
344 {
345 for (uint32_t x= 0; x < construct->count; x++)
346 {
347 if (construct->is_used[x])
348 continue;
349
350 kill_file(construct->pid_file[x]);
351 }
352
353 free(construct->server_list);
354 }
355 }