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