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"
32 #include <libmemcached/memcached.h>
33 #include <libmemcached/util.h>
37 static struct timespec global_sleep_value
= { .tv_sec
= 0, .tv_nsec
= 50000 };
39 static void global_sleep(void)
44 nanosleep(&global_sleep_value
, NULL
);
48 static void kill_file(const char *file_buffer
)
50 FILE *fp
= fopen(file_buffer
, "r");
52 while ((fp
= fopen(file_buffer
, "r")))
54 char pid_buffer
[1024];
56 if (fgets(pid_buffer
, sizeof(pid_buffer
), fp
) != NULL
)
58 pid_t pid
= (pid_t
)atoi(pid_buffer
);
61 if (kill(pid
, SIGTERM
) == -1)
63 remove(file_buffer
); // If this happens we may be dealing with a dead server that left its pid file.
68 while ((kill(pid
, 0) == 0) && --counter
)
82 void server_startup(server_startup_st
*construct
)
84 if ((construct
->server_list
= getenv("MEMCACHED_SERVERS")))
86 printf("servers %s\n", construct
->server_list
);
87 construct
->servers
= memcached_servers_parse(construct
->server_list
);
88 construct
->server_list
= NULL
;
94 char server_string_buffer
[8096];
96 end_ptr
= server_string_buffer
;
98 for (uint32_t x
= 0; x
< construct
->count
; x
++)
106 char variable_buffer
[1024];
108 snprintf(variable_buffer
, sizeof(variable_buffer
), "LIBMEMCACHED_PORT_%u", x
);
110 if ((var
= getenv(variable_buffer
)))
112 port
= (in_port_t
)atoi(var
);
116 port
= (in_port_t
)(x
+ TEST_PORT_BASE
);
120 char buffer
[PATH_MAX
];
121 snprintf(buffer
, sizeof(buffer
), PID_FILE_BASE
, x
);
126 snprintf(buffer
, sizeof(buffer
), "%s -d -u root -P "PID_FILE_BASE
" -t 1 -p %u -U %u -m 128",
127 MEMCACHED_BINARY
, x
, port
, port
);
131 snprintf(buffer
, sizeof(buffer
), "%s -d -u root -P "PID_FILE_BASE
" -t 1 -p %u -U %u",
132 MEMCACHED_BINARY
, x
, port
, port
);
134 if (libmemcached_util_ping("localhost", port
, NULL
))
136 fprintf(stderr
, "Server on port %u already exists\n", port
);
140 status
= system(buffer
);
141 fprintf(stderr
, "STARTING SERVER: %s status:%d\n", buffer
, status
);
143 count
= sprintf(end_ptr
, "localhost:%u,", port
);
149 int *pids
= calloc(construct
->count
, sizeof(int));
150 for (uint32_t x
= 0; x
< construct
->count
; x
++)
152 char buffer
[PATH_MAX
]; /* Nothing special for number */
154 snprintf(buffer
, sizeof(buffer
), PID_FILE_BASE
, x
);
156 uint32_t counter
= 3000; // Absurd, just to catch run away process
157 while (pids
[x
] <= 0 && --counter
)
159 FILE *file
= fopen(buffer
, "r");
162 char pid_buffer
[1024];
163 char *found
= fgets(pid_buffer
, sizeof(pid_buffer
), file
);
167 pids
[x
]= atoi(pid_buffer
);
178 bool was_started
= false;
184 if (kill(pids
[x
], 0) == 0)
193 if (was_started
== false)
195 fprintf(stderr
, "Failed to open buffer %s(%d)\n", buffer
, pids
[x
]);
196 for (uint32_t y
= 0; y
< construct
->count
; y
++)
199 kill(pids
[y
], SIGTERM
);
206 construct
->server_list
= strdup(server_string_buffer
);
208 printf("servers %s\n", construct
->server_list
);
209 construct
->servers
= memcached_servers_parse(construct
->server_list
);
212 assert(construct
->servers
);
214 srandom((unsigned int)time(NULL
));
216 for (uint32_t x
= 0; x
< memcached_server_list_count(construct
->servers
); x
++)
218 printf("\t%s : %d\n", memcached_server_name(&construct
->servers
[x
]), memcached_server_port(&construct
->servers
[x
]));
219 assert(construct
->servers
[x
].fd
== -1);
220 assert(construct
->servers
[x
].cursor_active
== 0);
226 void server_shutdown(server_startup_st
*construct
)
228 if (construct
->server_list
)
230 for (uint32_t x
= 0; x
< construct
->count
; x
++)
232 char file_buffer
[PATH_MAX
]; /* Nothing special for number */
233 snprintf(file_buffer
, sizeof(file_buffer
), PID_FILE_BASE
, x
);
234 kill_file(file_buffer
);
237 free(construct
->server_list
);