Fixed files to work with vpath builds for distcheck.
[awesomized/libmemcached] / tests / server.c
1 /*
2 Startup, and shutdown the memcached servers.
3 */
4
5 #define TEST_PORT_BASE MEMCACHED_DEFAULT_PORT+10
6
7 #include "libmemcached/libmemcached_config.h"
8
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <time.h>
13 #include <assert.h>
14 #include <signal.h>
15 #include <libmemcached/memcached.h>
16 #include <unistd.h>
17 #include "server.h"
18
19 void server_startup(server_startup_st *construct)
20 {
21 unsigned int x;
22
23 if ((construct->server_list= getenv("MEMCACHED_SERVERS")))
24 {
25 printf("servers %s\n", construct->server_list);
26 construct->servers= memcached_servers_parse(construct->server_list);
27 construct->server_list= NULL;
28 construct->count= 0;
29 }
30 else
31 {
32 {
33 char server_string_buffer[8096];
34 char *end_ptr;
35 end_ptr= server_string_buffer;
36
37 for (x= 0; x < construct->count; x++)
38 {
39 char buffer[1024]; /* Nothing special for number */
40 int count;
41 int status;
42
43 sprintf(buffer, "/tmp/%umemc.pid", x);
44 if (access(buffer, F_OK) == 0)
45 {
46 FILE *fp= fopen(buffer, "r");
47 remove(buffer);
48
49 if (fp != NULL)
50 {
51 if (fgets(buffer, sizeof(buffer), fp) != NULL)
52 {
53 pid_t pid = atol(buffer);
54 if (pid != 0)
55 kill(pid, SIGTERM);
56 }
57
58 fclose(fp);
59 }
60 }
61
62 if (x == 0)
63 {
64 sprintf(buffer, "%s -d -P /tmp/%umemc.pid -t 1 -p %u -U %u -m 128",
65 MEMCACHED_BINARY, x, x + TEST_PORT_BASE, x + TEST_PORT_BASE);
66 }
67 else
68 {
69 sprintf(buffer, "%s -d -P /tmp/%umemc.pid -t 1 -p %u -U %u",
70 MEMCACHED_BINARY, x, x + TEST_PORT_BASE, x + TEST_PORT_BASE);
71 }
72 fprintf(stderr, "STARTING SERVER: %s\n", buffer);
73 status= system(buffer);
74 count= sprintf(end_ptr, "localhost:%u,", x + TEST_PORT_BASE);
75 end_ptr+= count;
76 }
77 *end_ptr= 0;
78
79 construct->server_list= strdup(server_string_buffer);
80 }
81 printf("servers %s\n", construct->server_list);
82 construct->servers= memcached_servers_parse(construct->server_list);
83 }
84
85 assert(construct->servers);
86
87 srandom(time(NULL));
88
89 for (x= 0; x < memcached_server_list_count(construct->servers); x++)
90 {
91 printf("\t%s : %u\n", construct->servers[x].hostname, construct->servers[x].port);
92 assert(construct->servers[x].fd == -1);
93 assert(construct->servers[x].cursor_active == 0);
94 }
95
96 printf("\n");
97 }
98
99 void server_shutdown(server_startup_st *construct)
100 {
101 unsigned int x;
102
103 if (construct->server_list)
104 {
105 for (x= 0; x < construct->count; x++)
106 {
107 char buffer[1024]; /* Nothing special for number */
108 sprintf(buffer, "cat /tmp/%umemc.pid | xargs kill", x);
109 /* We have to check the return value of this or the compiler will yell */
110 int sys_ret= system(buffer);
111 assert(sys_ret != -1);
112 sprintf(buffer, "/tmp/%umemc.pid", x);
113 unlink(buffer);
114 }
115
116 free(construct->server_list);
117 }
118 }