Start memcached with "-u root", because it let us run tests via sudo or from the...
[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 <time.h>
30
31 #include "server.h"
32
33 void server_startup(server_startup_st *construct)
34 {
35 if ((construct->server_list= getenv("MEMCACHED_SERVERS")))
36 {
37 printf("servers %s\n", construct->server_list);
38 construct->servers= memcached_servers_parse(construct->server_list);
39 construct->server_list= NULL;
40 construct->count= 0;
41 }
42 else
43 {
44 {
45 char server_string_buffer[8096];
46 char *end_ptr;
47 end_ptr= server_string_buffer;
48
49 for (uint32_t x= 0; x < construct->count; x++)
50 {
51 char buffer[1024]; /* Nothing special for number */
52 int count;
53 int status;
54
55 sprintf(buffer, "/tmp/%umemc.pid", x);
56 if (access(buffer, F_OK) == 0)
57 {
58 FILE *fp= fopen(buffer, "r");
59 remove(buffer);
60
61 if (fp != NULL)
62 {
63 if (fgets(buffer, sizeof(buffer), fp) != NULL)
64 {
65 pid_t pid= (pid_t)atoi(buffer);
66 if (pid != 0)
67 kill(pid, SIGTERM);
68 }
69
70 fclose(fp);
71 }
72 }
73
74 if (x == 0)
75 {
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);
78 }
79 else
80 {
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);
83 }
84 if (libmemcached_util_ping("localhost", (in_port_t)(x + TEST_PORT_BASE), NULL))
85 {
86 fprintf(stderr, "Server on port %u already exists\n", x + TEST_PORT_BASE);
87 }
88 else
89 {
90 status= system(buffer);
91 fprintf(stderr, "STARTING SERVER: %s status:%d\n", buffer, status);
92 }
93 count= sprintf(end_ptr, "localhost:%u,", x + TEST_PORT_BASE);
94 end_ptr+= count;
95 }
96 *end_ptr= 0;
97
98 for (uint32_t x= 0; x < construct->count; x++)
99 {
100 uint32_t counter= 3;
101 char buffer[1024]; /* Nothing special for number */
102
103 snprintf(buffer, sizeof(buffer), "/tmp/%umemc.pid", x);
104
105 while (--counter)
106 {
107 int memcached_pid;
108
109 FILE *file;
110 file= fopen(buffer, "r");
111 if (file == NULL)
112 {
113 struct timespec req= { .tv_sec= 0, .tv_nsec= 5000 };
114 nanosleep(&req, NULL);
115
116 continue;
117 }
118 char *found= fgets(buffer, sizeof(buffer), file);
119 if (!found)
120 {
121 abort();
122 }
123 // Yes, we currently pitch this and don't make use of it.
124 memcached_pid= atoi(buffer);
125 fclose(file);
126 }
127
128
129 }
130
131 construct->server_list= strdup(server_string_buffer);
132 }
133 printf("servers %s\n", construct->server_list);
134 construct->servers= memcached_servers_parse(construct->server_list);
135 }
136
137 assert(construct->servers);
138
139 srandom((unsigned int)time(NULL));
140
141 for (uint32_t x= 0; x < memcached_server_list_count(construct->servers); x++)
142 {
143 printf("\t%s : %d\n", memcached_server_name(&construct->servers[x]), memcached_server_port(&construct->servers[x]));
144 assert(construct->servers[x].fd == -1);
145 assert(construct->servers[x].cursor_active == 0);
146 }
147
148 printf("\n");
149 }
150
151 void server_shutdown(server_startup_st *construct)
152 {
153 if (construct->server_list)
154 {
155 for (uint32_t x= 0; x < construct->count; x++)
156 {
157 char buffer[1024]; /* Nothing special for number */
158 sprintf(buffer, "cat /tmp/%umemc.pid | xargs kill", x);
159 /* We have to check the return value of this or the compiler will yell */
160 int sys_ret= system(buffer);
161 assert(sys_ret != -1);
162 sprintf(buffer, "/tmp/%umemc.pid", x);
163 unlink(buffer);
164 }
165
166 free(construct->server_list);
167 }
168 }