Merge in code for C++ compiling of libmemcached.
[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 int fd;
125 if ((fd= mkstemp(construct->pid_file[x])) == -1)
126 {
127 perror("mkstemp");
128 return;
129 }
130 close(fd);
131
132 {
133 char *var;
134 char variable_buffer[1024];
135
136 snprintf(variable_buffer, sizeof(variable_buffer), "LIBMEMCACHED_PORT_%u", x);
137
138 if ((var= getenv(variable_buffer)))
139 {
140 construct->port[x]= (in_port_t)atoi(var);
141 }
142 else
143 {
144 do {
145 construct->port[x]= (in_port_t)(x + TEST_PORT_BASE + port_base);
146
147 if (libmemcached_util_ping("localhost", construct->port[x], NULL))
148 {
149 port_base++;
150 construct->port[x]= 0;
151 }
152 } while (construct->port[x] == 0);
153 }
154 }
155
156 char buffer[FILENAME_MAX];
157 if (x == 0)
158 {
159 snprintf(buffer, sizeof(buffer), "%s -d -P %s -t 1 -p %u -U %u -m 128",
160 MEMCACHED_BINARY, construct->pid_file[x], construct->port[x], construct->port[x]);
161 }
162 else
163 {
164 snprintf(buffer, sizeof(buffer), "%s -d -P %s -t 1 -p %u -U %u",
165 MEMCACHED_BINARY, construct->pid_file[x], construct->port[x], construct->port[x]);
166 }
167
168 if (libmemcached_util_ping("localhost", construct->port[x], NULL))
169 {
170 fprintf(stderr, "Server on port %u already exists\n", construct->port[x]);
171 }
172 else
173 {
174 status= system(buffer);
175 fprintf(stderr, "STARTING SERVER: %s status:%d\n", buffer, status);
176 }
177
178 int count;
179 size_t remaining_length= sizeof(server_string_buffer) - (size_t)(end_ptr -server_string_buffer);
180 count= snprintf(end_ptr, remaining_length, "localhost:%u,", construct->port[x]);
181
182 if ((size_t)count >= remaining_length || count < 0)
183 {
184 fprintf(stderr, "server names grew to be larger then buffer allowed\n");
185 abort();
186 }
187 end_ptr+= count;
188 }
189 *end_ptr= 0;
190
191
192 for (uint32_t x= 0; x < construct->count; x++)
193 {
194 if (! wait_for_file(construct->pid_file[x]))
195 {
196 abort();
197 }
198 }
199
200 for (uint32_t x= 0; x < construct->count; x++)
201 {
202 uint32_t counter= 3000; // Absurd, just to catch run away process
203 while (construct->pids[x] <= 0 && --counter)
204 {
205 FILE *file= fopen(construct->pid_file[x], "r");
206 if (file)
207 {
208 char pid_buffer[1024];
209 char *found= fgets(pid_buffer, sizeof(pid_buffer), file);
210
211 if (found)
212 {
213 construct->pids[x]= atoi(pid_buffer);
214 fclose(file);
215
216 if (construct->pids[x] > 0)
217 break;
218 }
219 fclose(file);
220 }
221 switch (errno)
222 {
223 default:
224 fprintf(stderr, "%s -> fopen(%s)\n", construct->pid_file[x], strerror(errno));
225 abort();
226 case ENOENT:
227 case EINTR:
228 case EACCES:
229 break;
230 case ENOTCONN:
231 continue;
232 }
233
234 // Safety 3rd, check to see if the file has gone away
235 if (! wait_for_file(construct->pid_file[x]))
236 {
237 abort();
238 }
239 }
240
241 bool was_started= false;
242 if (construct->pids[x] > 0)
243 {
244 counter= 30;
245 while (--counter)
246 {
247 if (kill(construct->pids[x], 0) == 0)
248 {
249 was_started= true;
250 break;
251 }
252 global_sleep();
253 }
254 }
255
256 if (was_started == false)
257 {
258 fprintf(stderr, "Failed to open buffer %s(%d)\n", construct->pid_file[x], construct->pids[x]);
259 for (uint32_t y= 0; y < construct->count; y++)
260 {
261 if (construct->pids[y] > 0)
262 kill(construct->pids[y], SIGTERM);
263 }
264 abort();
265 }
266 }
267
268 construct->server_list= strdup(server_string_buffer);
269 }
270 printf("servers %s\n", construct->server_list);
271 construct->servers= memcached_servers_parse(construct->server_list);
272 }
273
274 assert(construct->servers);
275
276 srandom((unsigned int)time(NULL));
277
278 for (uint32_t x= 0; x < memcached_server_list_count(construct->servers); x++)
279 {
280 printf("\t%s : %d\n", memcached_server_name(&construct->servers[x]), memcached_server_port(&construct->servers[x]));
281 assert(construct->servers[x].fd == -1);
282 assert(construct->servers[x].cursor_active == 0);
283 }
284
285 printf("\n");
286 }
287
288 void server_shutdown(server_startup_st *construct)
289 {
290 if (construct->server_list)
291 {
292 for (uint32_t x= 0; x < construct->count; x++)
293 {
294 kill_file(construct->pid_file[x]);
295 }
296
297 free(construct->server_list);
298 }
299 }