2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
13 Startup, and shutdown the memcached servers.
16 #define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT+10
18 #define PID_FILE_BASE "/tmp/%ulibmemcached_memc.pid"
33 #include <libmemcached/memcached.h>
34 #include <libmemcached/util.h>
36 #include <libtest/server.h>
38 static struct timespec global_sleep_value
= { .tv_sec
= 0, .tv_nsec
= 50000 };
40 static void global_sleep(void)
45 nanosleep(&global_sleep_value
, NULL
);
49 static bool wait_for_file(const char *filename
)
56 for (waited
= 0, retry
= 1; ; retry
++, waited
+= this_wait
)
58 if ((! access(filename
, R_OK
)) || (waited
>= timeout
))
63 this_wait
= retry
* retry
/ 3 + 1;
70 static void kill_file(const char *file_buffer
)
74 while ((fp
= fopen(file_buffer
, "r")))
76 char pid_buffer
[1024];
78 if (fgets(pid_buffer
, sizeof(pid_buffer
), fp
) != NULL
)
80 pid_t pid
= (pid_t
)atoi(pid_buffer
);
83 if (kill(pid
, SIGTERM
) == -1)
85 remove(file_buffer
); // If this happens we may be dealing with a dead server that left its pid file.
90 while ((kill(pid
, 0) == 0) && --counter
)
104 void server_startup(server_startup_st
*construct
)
106 if ((construct
->server_list
= getenv("MEMCACHED_SERVERS")))
108 printf("servers %s\n", construct
->server_list
);
109 construct
->servers
= memcached_servers_parse(construct
->server_list
);
110 construct
->server_list
= NULL
;
116 char server_string_buffer
[8096];
118 end_ptr
= server_string_buffer
;
120 for (uint32_t x
= 0; x
< construct
->count
; x
++)
127 char variable_buffer
[1024];
129 snprintf(variable_buffer
, sizeof(variable_buffer
), "LIBMEMCACHED_PORT_%u", x
);
131 if ((var
= getenv(variable_buffer
)))
133 port
= (in_port_t
)atoi(var
);
137 port
= (in_port_t
)(x
+ TEST_PORT_BASE
);
141 char buffer
[PATH_MAX
];
142 snprintf(buffer
, sizeof(buffer
), PID_FILE_BASE
, x
);
147 snprintf(buffer
, sizeof(buffer
), "%s -d -u root -P "PID_FILE_BASE
" -t 1 -p %u -U %u -m 128",
148 MEMCACHED_BINARY
, x
, port
, port
);
152 snprintf(buffer
, sizeof(buffer
), "%s -d -u root -P "PID_FILE_BASE
" -t 1 -p %u -U %u",
153 MEMCACHED_BINARY
, x
, port
, port
);
155 if (libmemcached_util_ping("localhost", port
, NULL
))
157 fprintf(stderr
, "Server on port %u already exists\n", port
);
161 status
= system(buffer
);
162 fprintf(stderr
, "STARTING SERVER: %s status:%d\n", buffer
, status
);
165 size_t remaining_length
= sizeof(server_string_buffer
) - (size_t)(end_ptr
-server_string_buffer
);
166 count
= snprintf(end_ptr
, remaining_length
, "localhost:%u,", port
);
168 if ((size_t)count
>= remaining_length
|| count
< 0)
170 fprintf(stderr
, "server names grew to be larger then buffer allowed\n");
178 int *pids
= calloc(construct
->count
, sizeof(int));
179 for (uint32_t x
= 0; x
< construct
->count
; x
++)
181 char buffer
[PATH_MAX
]; /* Nothing special for number */
183 snprintf(buffer
, sizeof(buffer
), PID_FILE_BASE
, x
);
185 uint32_t counter
= 3000; // Absurd, just to catch run away process
186 while (pids
[x
] <= 0 && --counter
)
188 FILE *file
= fopen(buffer
, "r");
191 char pid_buffer
[1024];
192 char *found
= fgets(pid_buffer
, sizeof(pid_buffer
), file
);
196 pids
[x
]= atoi(pid_buffer
);
207 fprintf(stderr
, "%s\n", strerror(errno
));
215 if (! wait_for_file(buffer
))
221 bool was_started
= false;
227 if (kill(pids
[x
], 0) == 0)
236 if (was_started
== false)
238 fprintf(stderr
, "Failed to open buffer %s(%d)\n", buffer
, pids
[x
]);
239 for (uint32_t y
= 0; y
< construct
->count
; y
++)
242 kill(pids
[y
], SIGTERM
);
249 construct
->server_list
= strdup(server_string_buffer
);
251 printf("servers %s\n", construct
->server_list
);
252 construct
->servers
= memcached_servers_parse(construct
->server_list
);
255 assert(construct
->servers
);
257 srandom((unsigned int)time(NULL
));
259 for (uint32_t x
= 0; x
< memcached_server_list_count(construct
->servers
); x
++)
261 printf("\t%s : %d\n", memcached_server_name(&construct
->servers
[x
]), memcached_server_port(&construct
->servers
[x
]));
262 assert(construct
->servers
[x
].fd
== -1);
263 assert(construct
->servers
[x
].cursor_active
== 0);
269 void server_shutdown(server_startup_st
*construct
)
271 if (construct
->server_list
)
273 for (uint32_t x
= 0; x
< construct
->count
; x
++)
275 char file_buffer
[PATH_MAX
]; /* Nothing special for number */
276 snprintf(file_buffer
, sizeof(file_buffer
), PID_FILE_BASE
, x
);
277 kill_file(file_buffer
);
280 free(construct
->server_list
);