Tiny style fix
[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 "server.h"
30
31 void server_startup(server_startup_st *construct)
32 {
33 if ((construct->server_list= getenv("MEMCACHED_SERVERS")))
34 {
35 printf("servers %s\n", construct->server_list);
36 construct->servers= memcached_servers_parse(construct->server_list);
37 construct->server_list= NULL;
38 construct->count= 0;
39 }
40 else
41 {
42 {
43 char server_string_buffer[8096];
44 char *end_ptr;
45 end_ptr= server_string_buffer;
46
47 for (uint32_t x= 0; x < construct->count; x++)
48 {
49 char buffer[1024]; /* Nothing special for number */
50 int count;
51 int status;
52
53 sprintf(buffer, "/tmp/%umemc.pid", x);
54 if (access(buffer, F_OK) == 0)
55 {
56 FILE *fp= fopen(buffer, "r");
57 remove(buffer);
58
59 if (fp != NULL)
60 {
61 if (fgets(buffer, sizeof(buffer), fp) != NULL)
62 {
63 pid_t pid= (pid_t)atoi(buffer);
64 if (pid != 0)
65 kill(pid, SIGTERM);
66 }
67
68 fclose(fp);
69 }
70 }
71
72 if (x == 0)
73 {
74 sprintf(buffer, "%s -d -P /tmp/%umemc.pid -t 1 -p %u -U %u -m 128",
75 MEMCACHED_BINARY, x, x + TEST_PORT_BASE, x + TEST_PORT_BASE);
76 }
77 else
78 {
79 sprintf(buffer, "%s -d -P /tmp/%umemc.pid -t 1 -p %u -U %u",
80 MEMCACHED_BINARY, x, x + TEST_PORT_BASE, x + TEST_PORT_BASE);
81 }
82 if (libmemcached_util_ping("localhost", (in_port_t)(x + TEST_PORT_BASE), NULL))
83 {
84 fprintf(stderr, "Server on port %u already exists\n", x + TEST_PORT_BASE);
85 }
86 else
87 {
88 status= system(buffer);
89 fprintf(stderr, "STARTING SERVER: %s status:%d\n", buffer, status);
90 }
91 count= sprintf(end_ptr, "localhost:%u,", x + TEST_PORT_BASE);
92 end_ptr+= count;
93 }
94 *end_ptr= 0;
95
96 construct->server_list= strdup(server_string_buffer);
97 }
98 printf("servers %s\n", construct->server_list);
99 construct->servers= memcached_servers_parse(construct->server_list);
100 }
101
102 assert(construct->servers);
103
104 srandom((unsigned int)time(NULL));
105
106 for (uint32_t x= 0; x < memcached_server_list_count(construct->servers); x++)
107 {
108 printf("\t%s : %d\n", memcached_server_name(&construct->servers[x]), memcached_server_port(&construct->servers[x]));
109 assert(construct->servers[x].fd == -1);
110 assert(construct->servers[x].cursor_active == 0);
111 }
112
113 printf("\n");
114 }
115
116 void server_shutdown(server_startup_st *construct)
117 {
118 if (construct->server_list)
119 {
120 for (uint32_t x= 0; x < construct->count; x++)
121 {
122 char buffer[1024]; /* Nothing special for number */
123 sprintf(buffer, "cat /tmp/%umemc.pid | xargs kill", x);
124 /* We have to check the return value of this or the compiler will yell */
125 int sys_ret= system(buffer);
126 assert(sys_ret != -1);
127 sprintf(buffer, "/tmp/%umemc.pid", x);
128 unlink(buffer);
129 }
130
131 free(construct->server_list);
132 }
133 }