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