Merge in all current libmemcached work, plus restore older, working,
[m6w6/libmemcached] / libtest / server.c
1 /* LibMemcached
2 * Copyright (C) 2006-2009 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 *
8 * Summary:
9 *
10 */
11
12 /*
13 Startup, and shutdown the memcached servers.
14 */
15
16 #define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT+10
17
18 #include <config.h>
19
20 #include <assert.h>
21 #include <limits.h>
22 #include <signal.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/time.h>
27 #include <time.h>
28 #include <unistd.h>
29 #include <errno.h>
30
31 #include <libmemcached/memcached.h>
32 #include <libmemcached/util.h>
33
34 #include <libtest/server.h>
35
36 static struct timespec global_sleep_value= { .tv_sec= 0, .tv_nsec= 50000 };
37
38 static void global_sleep(void)
39 {
40 #ifdef WIN32
41 sleep(1);
42 #else
43 nanosleep(&global_sleep_value, NULL);
44 #endif
45 }
46
47 static bool wait_for_file(const char *filename)
48 {
49 uint32_t timeout= 6;
50 uint32_t waited;
51 uint32_t this_wait;
52 uint32_t retry;
53
54 for (waited= 0, retry= 1; ; retry++, waited+= this_wait)
55 {
56 if ((! access(filename, R_OK)) || (waited >= timeout))
57 {
58 return true;
59 }
60
61 this_wait= retry * retry / 3 + 1;
62 sleep(this_wait);
63 }
64
65 return false;
66 }
67
68 static void kill_file(const char *file_buffer)
69 {
70 FILE *fp;
71
72 while ((fp= fopen(file_buffer, "r")))
73 {
74 char pid_buffer[1024];
75
76 if (fgets(pid_buffer, sizeof(pid_buffer), fp) != NULL)
77 {
78 pid_t pid= (pid_t)atoi(pid_buffer);
79 if (pid != 0)
80 {
81 if (kill(pid, SIGTERM) == -1)
82 {
83 remove(file_buffer); // If this happens we may be dealing with a dead server that left its pid file.
84 }
85 else
86 {
87 uint32_t counter= 3;
88 while ((kill(pid, 0) == 0) && --counter)
89 {
90 global_sleep();
91 }
92 }
93 }
94 }
95
96 global_sleep();
97
98 fclose(fp);
99 }
100 }
101
102 void server_startup(server_startup_st *construct)
103 {
104 if ((construct->server_list= getenv("MEMCACHED_SERVERS")))
105 {
106 printf("servers %s\n", construct->server_list);
107 construct->servers= memcached_servers_parse(construct->server_list);
108 construct->server_list= NULL;
109 construct->count= 0;
110 }
111 else
112 {
113 {
114 char server_string_buffer[8096];
115 char *end_ptr;
116 end_ptr= server_string_buffer;
117
118 uint32_t port_base= 0;
119 for (uint32_t x= 0; x < construct->count; x++)
120 {
121 int status;
122
123 snprintf(construct->pid_file[x], FILENAME_MAX, "/tmp/memcached.pidXXXXXX");
124 if (mkstemp(construct->pid_file[x]) == -1)
125 {
126 perror("mkstemp");
127 return;
128 }
129
130 {
131 char *var;
132 char variable_buffer[1024];
133
134 snprintf(variable_buffer, sizeof(variable_buffer), "LIBMEMCACHED_PORT_%u", x);
135
136 if ((var= getenv(variable_buffer)))
137 {
138 construct->port[x]= (in_port_t)atoi(var);
139 }
140 else
141 {
142 do {
143 construct->port[x]= (in_port_t)(x + TEST_PORT_BASE + port_base);
144
145 if (libmemcached_util_ping("localhost", construct->port[x], NULL))
146 {
147 port_base++;
148 construct->port[x]= 0;
149 }
150 } while (construct->port[x] == 0);
151 }
152 }
153
154 char buffer[FILENAME_MAX];
155 if (x == 0)
156 {
157 snprintf(buffer, sizeof(buffer), "%s -d -P %s -t 1 -p %u -U %u -m 128",
158 MEMCACHED_BINARY, construct->pid_file[x], construct->port[x], construct->port[x]);
159 }
160 else
161 {
162 snprintf(buffer, sizeof(buffer), "%s -d -P %s -t 1 -p %u -U %u",
163 MEMCACHED_BINARY, construct->pid_file[x], construct->port[x], construct->port[x]);
164 }
165
166 if (libmemcached_util_ping("localhost", construct->port[x], NULL))
167 {
168 fprintf(stderr, "Server on port %u already exists\n", construct->port[x]);
169 }
170 else
171 {
172 status= system(buffer);
173 fprintf(stderr, "STARTING SERVER: %s status:%d\n", buffer, status);
174 }
175
176 int count;
177 size_t remaining_length= sizeof(server_string_buffer) - (size_t)(end_ptr -server_string_buffer);
178 count= snprintf(end_ptr, remaining_length, "localhost:%u,", construct->port[x]);
179
180 if ((size_t)count >= remaining_length || count < 0)
181 {
182 fprintf(stderr, "server names grew to be larger then buffer allowed\n");
183 abort();
184 }
185 end_ptr+= count;
186 }
187 *end_ptr= 0;
188
189
190 for (uint32_t x= 0; x < construct->count; x++)
191 {
192 if (! wait_for_file(construct->pid_file[x]))
193 {
194 abort();
195 }
196 }
197
198 for (uint32_t x= 0; x < construct->count; x++)
199 {
200 uint32_t counter= 3000; // Absurd, just to catch run away process
201 while (construct->pids[x] <= 0 && --counter)
202 {
203 FILE *file= fopen(construct->pid_file[x], "r");
204 if (file)
205 {
206 char pid_buffer[1024];
207 char *found= fgets(pid_buffer, sizeof(pid_buffer), file);
208
209 if (found)
210 {
211 construct->pids[x]= atoi(pid_buffer);
212 fclose(file);
213
214 if (construct->pids[x] > 0)
215 break;
216 }
217 fclose(file);
218 }
219 switch (errno)
220 {
221 default:
222 fprintf(stderr, "%s -> fopen(%s)\n", construct->pid_file[x], strerror(errno));
223 abort();
224 case ENOENT:
225 case EINTR:
226 case EACCES:
227 break;
228 case ENOTCONN:
229 continue;
230 }
231
232 // Safety 3rd, check to see if the file has gone away
233 if (! wait_for_file(construct->pid_file[x]))
234 {
235 abort();
236 }
237 }
238
239 bool was_started= false;
240 if (construct->pids[x] > 0)
241 {
242 counter= 30;
243 while (--counter)
244 {
245 if (kill(construct->pids[x], 0) == 0)
246 {
247 was_started= true;
248 break;
249 }
250 global_sleep();
251 }
252 }
253
254 if (was_started == false)
255 {
256 fprintf(stderr, "Failed to open buffer %s(%d)\n", construct->pid_file[x], construct->pids[x]);
257 for (uint32_t y= 0; y < construct->count; y++)
258 {
259 if (construct->pids[y] > 0)
260 kill(construct->pids[y], SIGTERM);
261 }
262 abort();
263 }
264 }
265
266 construct->server_list= strdup(server_string_buffer);
267 }
268 printf("servers %s\n", construct->server_list);
269 construct->servers= memcached_servers_parse(construct->server_list);
270 }
271
272 assert(construct->servers);
273
274 srandom((unsigned int)time(NULL));
275
276 for (uint32_t x= 0; x < memcached_server_list_count(construct->servers); x++)
277 {
278 printf("\t%s : %d\n", memcached_server_name(&construct->servers[x]), memcached_server_port(&construct->servers[x]));
279 assert(construct->servers[x].fd == -1);
280 assert(construct->servers[x].cursor_active == 0);
281 }
282
283 printf("\n");
284 }
285
286 void server_shutdown(server_startup_st *construct)
287 {
288 if (construct->server_list)
289 {
290 for (uint32_t x= 0; x < construct->count; x++)
291 {
292 kill_file(construct->pid_file[x]);
293 }
294
295 free(construct->server_list);
296 }
297 }