f8d06dbf8495e4d227b4dae54f57a308574227cf
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
26 #include <libmemcached/memcached.h>
27 #include <libmemcached/util.h>
33 void server_startup(server_startup_st
*construct
)
35 if ((construct
->server_list
= getenv("MEMCACHED_SERVERS")))
37 printf("servers %s\n", construct
->server_list
);
38 construct
->servers
= memcached_servers_parse(construct
->server_list
);
39 construct
->server_list
= NULL
;
45 char server_string_buffer
[8096];
47 end_ptr
= server_string_buffer
;
49 for (uint32_t x
= 0; x
< construct
->count
; x
++)
51 char buffer
[1024]; /* Nothing special for number */
55 sprintf(buffer
, "/tmp/%umemc.pid", x
);
56 if (access(buffer
, F_OK
) == 0)
58 FILE *fp
= fopen(buffer
, "r");
63 if (fgets(buffer
, sizeof(buffer
), fp
) != NULL
)
65 pid_t pid
= (pid_t
)atoi(buffer
);
76 sprintf(buffer
, "%s -d -u root -P /tmp/%umemc.pid -t 1 -p %u -U %u -m 128",
77 MEMCACHED_BINARY
, x
, x
+ TEST_PORT_BASE
, x
+ TEST_PORT_BASE
);
81 sprintf(buffer
, "%s -d -u root -P /tmp/%umemc.pid -t 1 -p %u -U %u",
82 MEMCACHED_BINARY
, x
, x
+ TEST_PORT_BASE
, x
+ TEST_PORT_BASE
);
84 if (libmemcached_util_ping("localhost", (in_port_t
)(x
+ TEST_PORT_BASE
), NULL
))
86 fprintf(stderr
, "Server on port %u already exists\n", x
+ TEST_PORT_BASE
);
90 status
= system(buffer
);
91 fprintf(stderr
, "STARTING SERVER: %s status:%d\n", buffer
, status
);
93 count
= sprintf(end_ptr
, "localhost:%u,", x
+ TEST_PORT_BASE
);
98 for (uint32_t x
= 0; x
< construct
->count
; x
++)
101 char buffer
[1024]; /* Nothing special for number */
103 snprintf(buffer
, sizeof(buffer
), "/tmp/%umemc.pid", x
);
110 file
= fopen(buffer
, "r");
114 struct timespec req
= { .tv_sec
= 0, .tv_nsec
= 5000 };
115 nanosleep(&req
, NULL
);
119 char *found
= fgets(buffer
, sizeof(buffer
), file
);
124 // Yes, we currently pitch this and don't make use of it.
125 memcached_pid
= atoi(buffer
);
132 construct
->server_list
= strdup(server_string_buffer
);
134 printf("servers %s\n", construct
->server_list
);
135 construct
->servers
= memcached_servers_parse(construct
->server_list
);
138 assert(construct
->servers
);
140 srandom((unsigned int)time(NULL
));
142 for (uint32_t x
= 0; x
< memcached_server_list_count(construct
->servers
); x
++)
144 printf("\t%s : %d\n", memcached_server_name(&construct
->servers
[x
]), memcached_server_port(&construct
->servers
[x
]));
145 assert(construct
->servers
[x
].fd
== -1);
146 assert(construct
->servers
[x
].cursor_active
== 0);
152 void server_shutdown(server_startup_st
*construct
)
154 if (construct
->server_list
)
156 for (uint32_t x
= 0; x
< construct
->count
; x
++)
158 char buffer
[1024]; /* Nothing special for number */
159 sprintf(buffer
, "cat /tmp/%umemc.pid | xargs kill", x
);
160 /* We have to check the return value of this or the compiler will yell */
161 int sys_ret
= system(buffer
);
162 assert(sys_ret
!= -1);
163 sprintf(buffer
, "/tmp/%umemc.pid", x
);
167 free(construct
->server_list
);