e2781b26f2161a5aab402dc6631bc41e18de164c
[awesomized/libmemcached] / tests / 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
30 #include <libmemcached/memcached.h>
31 #include <libmemcached/util.h>
32
33 #include "server.h"
34
35 static void kill_file(const char *file_buffer)
36 {
37 FILE *fp= fopen(file_buffer, "r");
38
39 if (fp != NULL)
40 {
41 char pid_buffer[1024];
42
43 if (fgets(pid_buffer, sizeof(pid_buffer), fp) != NULL)
44 {
45 pid_t pid= (pid_t)atoi(pid_buffer);
46 if (pid != 0)
47 {
48 if (kill(pid, SIGTERM) == -1)
49 {
50 perror(file_buffer);
51 }
52 }
53 }
54
55 fclose(fp);
56 }
57 }
58
59 void server_startup(server_startup_st *construct)
60 {
61 if ((construct->server_list= getenv("MEMCACHED_SERVERS")))
62 {
63 printf("servers %s\n", construct->server_list);
64 construct->servers= memcached_servers_parse(construct->server_list);
65 construct->server_list= NULL;
66 construct->count= 0;
67 }
68 else
69 {
70 {
71 char server_string_buffer[8096];
72 char *end_ptr;
73 end_ptr= server_string_buffer;
74
75 for (uint32_t x= 0; x < construct->count; x++)
76 {
77 int count;
78 int status;
79 in_port_t port;
80
81 {
82 char *var;
83 char variable_buffer[1024];
84
85 snprintf(variable_buffer, sizeof(variable_buffer), "LIBMEMCACHED_PORT_%u", x);
86
87 if ((var= getenv(variable_buffer)))
88 {
89 port= (in_port_t)atoi(var);
90 }
91 else
92 {
93 port= (in_port_t)(x + TEST_PORT_BASE);
94 }
95 }
96
97 char buffer[PATH_MAX];
98
99 snprintf(buffer, sizeof(buffer), "/tmp/%umemc.pid", x);
100
101 uint32_t counter= 3;
102 while (--counter)
103 {
104 if (access(buffer, F_OK) == 0)
105 {
106 kill_file(buffer);
107 #ifndef WIN32
108 struct timespec req= { .tv_sec= 0, .tv_nsec= 5000 };
109 nanosleep(&req, NULL);
110 #endif
111 }
112 else
113 {
114 break;
115 }
116 }
117
118 if (x == 0)
119 {
120 snprintf(buffer, sizeof(buffer), "%s -d -u root -P /tmp/%umemc.pid -t 1 -p %u -U %u -m 128",
121 MEMCACHED_BINARY, x, port, port);
122 }
123 else
124 {
125 snprintf(buffer, sizeof(buffer), "%s -d -u root -P /tmp/%umemc.pid -t 1 -p %u -U %u",
126 MEMCACHED_BINARY, x, port, port);
127 }
128 if (libmemcached_util_ping("localhost", port, NULL))
129 {
130 fprintf(stderr, "Server on port %u already exists\n", port);
131 }
132 else
133 {
134 status= system(buffer);
135 fprintf(stderr, "STARTING SERVER: %s status:%d\n", buffer, status);
136 }
137 count= sprintf(end_ptr, "localhost:%u,", port);
138 end_ptr+= count;
139 }
140 *end_ptr= 0;
141
142 for (uint32_t x= 0; x < construct->count; x++)
143 {
144 uint32_t counter= 3;
145 char buffer[PATH_MAX]; /* Nothing special for number */
146
147 snprintf(buffer, sizeof(buffer), "/tmp/%umemc.pid", x);
148
149 bool was_started= false;
150 while (--counter)
151 {
152 int memcached_pid;
153
154 FILE *file;
155 file= fopen(buffer, "r");
156 if (file == NULL)
157 {
158 #ifndef WIN32
159 struct timespec req= { .tv_sec= 0, .tv_nsec= 5000 };
160 nanosleep(&req, NULL);
161 #endif
162 continue;
163 }
164 char *found= fgets(buffer, sizeof(buffer), file);
165 if (!found)
166 continue;
167
168 // Yes, we currently pitch this and don't make use of it.
169 memcached_pid= atoi(buffer);
170 fclose(file);
171 was_started= true;
172 break;
173 }
174
175 if (was_started == false)
176 {
177 fprintf(stderr, "Failed to open buffer %s\n", buffer);
178 abort();
179 }
180 }
181
182 construct->server_list= strdup(server_string_buffer);
183 }
184 printf("servers %s\n", construct->server_list);
185 construct->servers= memcached_servers_parse(construct->server_list);
186 }
187
188 assert(construct->servers);
189
190 srandom((unsigned int)time(NULL));
191
192 for (uint32_t x= 0; x < memcached_server_list_count(construct->servers); x++)
193 {
194 printf("\t%s : %d\n", memcached_server_name(&construct->servers[x]), memcached_server_port(&construct->servers[x]));
195 assert(construct->servers[x].fd == -1);
196 assert(construct->servers[x].cursor_active == 0);
197 }
198
199 printf("\n");
200 }
201
202 void server_shutdown(server_startup_st *construct)
203 {
204 if (construct->server_list)
205 {
206 for (uint32_t x= 0; x < construct->count; x++)
207 {
208 char file_buffer[PATH_MAX]; /* Nothing special for number */
209 snprintf(file_buffer, sizeof(file_buffer), "/tmp/%umemc.pid", x);
210 kill_file(file_buffer);
211 }
212
213 free(construct->server_list);
214 }
215 }