e2781b26f2161a5aab402dc6631bc41e18de164c
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
30 #include <libmemcached/memcached.h>
31 #include <libmemcached/util.h>
35 static void kill_file(const char *file_buffer
)
37 FILE *fp
= fopen(file_buffer
, "r");
41 char pid_buffer
[1024];
43 if (fgets(pid_buffer
, sizeof(pid_buffer
), fp
) != NULL
)
45 pid_t pid
= (pid_t
)atoi(pid_buffer
);
48 if (kill(pid
, SIGTERM
) == -1)
59 void server_startup(server_startup_st
*construct
)
61 if ((construct
->server_list
= getenv("MEMCACHED_SERVERS")))
63 printf("servers %s\n", construct
->server_list
);
64 construct
->servers
= memcached_servers_parse(construct
->server_list
);
65 construct
->server_list
= NULL
;
71 char server_string_buffer
[8096];
73 end_ptr
= server_string_buffer
;
75 for (uint32_t x
= 0; x
< construct
->count
; x
++)
83 char variable_buffer
[1024];
85 snprintf(variable_buffer
, sizeof(variable_buffer
), "LIBMEMCACHED_PORT_%u", x
);
87 if ((var
= getenv(variable_buffer
)))
89 port
= (in_port_t
)atoi(var
);
93 port
= (in_port_t
)(x
+ TEST_PORT_BASE
);
97 char buffer
[PATH_MAX
];
99 snprintf(buffer
, sizeof(buffer
), "/tmp/%umemc.pid", x
);
104 if (access(buffer
, F_OK
) == 0)
108 struct timespec req
= { .tv_sec
= 0, .tv_nsec
= 5000 };
109 nanosleep(&req
, NULL
);
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
);
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
);
128 if (libmemcached_util_ping("localhost", port
, NULL
))
130 fprintf(stderr
, "Server on port %u already exists\n", port
);
134 status
= system(buffer
);
135 fprintf(stderr
, "STARTING SERVER: %s status:%d\n", buffer
, status
);
137 count
= sprintf(end_ptr
, "localhost:%u,", port
);
142 for (uint32_t x
= 0; x
< construct
->count
; x
++)
145 char buffer
[PATH_MAX
]; /* Nothing special for number */
147 snprintf(buffer
, sizeof(buffer
), "/tmp/%umemc.pid", x
);
149 bool was_started
= false;
155 file
= fopen(buffer
, "r");
159 struct timespec req
= { .tv_sec
= 0, .tv_nsec
= 5000 };
160 nanosleep(&req
, NULL
);
164 char *found
= fgets(buffer
, sizeof(buffer
), file
);
168 // Yes, we currently pitch this and don't make use of it.
169 memcached_pid
= atoi(buffer
);
175 if (was_started
== false)
177 fprintf(stderr
, "Failed to open buffer %s\n", buffer
);
182 construct
->server_list
= strdup(server_string_buffer
);
184 printf("servers %s\n", construct
->server_list
);
185 construct
->servers
= memcached_servers_parse(construct
->server_list
);
188 assert(construct
->servers
);
190 srandom((unsigned int)time(NULL
));
192 for (uint32_t x
= 0; x
< memcached_server_list_count(construct
->servers
); x
++)
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);
202 void server_shutdown(server_startup_st
*construct
)
204 if (construct
->server_list
)
206 for (uint32_t x
= 0; x
< construct
->count
; x
++)
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
);
213 free(construct
->server_list
);