Cleanup around errors on bad --servers for CLI.
[m6w6/libmemcached] / clients / memflush.cc
1 /* LibMemcached
2 * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/
3 * Copyright (C) 2006-2009 Brian Aker
4 * All rights reserved.
5 *
6 * Use and distribution licensed under the BSD license. See
7 * the COPYING file in the parent directory for full text.
8 *
9 * Summary:
10 *
11 */
12 #include "mem_config.h"
13
14 #include <cerrno>
15 #include <cstdio>
16 #include <cstring>
17 #include <getopt.h>
18 #include <iostream>
19 #include <unistd.h>
20
21 #include <libmemcached-1.0/memcached.h>
22 #include "client_options.h"
23 #include "utilities.h"
24
25 static int opt_binary= 0;
26 static int opt_verbose= 0;
27 static time_t opt_expire= 0;
28 static char *opt_servers= NULL;
29 static char *opt_username;
30 static char *opt_passwd;
31
32 #define PROGRAM_NAME "memflush"
33 #define PROGRAM_DESCRIPTION "Erase all data in a server of memcached servers."
34
35 /* Prototypes */
36 void options_parse(int argc, char *argv[]);
37
38 int main(int argc, char *argv[])
39 {
40 options_parse(argc, argv);
41
42 if (opt_servers == false)
43 {
44 char *temp;
45
46 if ((temp= getenv("MEMCACHED_SERVERS")))
47 {
48 opt_servers= strdup(temp);
49 }
50
51 if (opt_servers == false)
52 {
53 std::cerr << "No Servers provided" << std::endl;
54 exit(EXIT_FAILURE);
55 }
56 }
57
58 memcached_server_st* servers= memcached_servers_parse(opt_servers);
59 if (servers == NULL or memcached_server_list_count(servers) == 0)
60 {
61 std::cerr << "Invalid server list provided:" << opt_servers << std::endl;
62 return EXIT_FAILURE;
63 }
64
65 memcached_st *memc= memcached_create(NULL);
66 memcached_server_push(memc, servers);
67 memcached_server_list_free(servers);
68 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL,
69 (uint64_t) opt_binary);
70
71 if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0)
72 {
73 memcached_free(memc);
74 std::cerr << "--username was supplied, but binary was not built with SASL support." << std::endl;
75 return EXIT_FAILURE;
76 }
77
78 if (opt_username)
79 {
80 memcached_return_t ret;
81 if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd)))
82 {
83 std::cerr << memcached_last_error_message(memc) << std::endl;
84 memcached_free(memc);
85 return EXIT_FAILURE;
86 }
87 }
88
89 memcached_return_t rc = memcached_flush(memc, opt_expire);
90 if (rc != MEMCACHED_SUCCESS)
91 {
92 std::cerr << memcached_last_error_message(memc) << std::endl;
93 }
94
95 memcached_free(memc);
96
97 free(opt_servers);
98
99 return EXIT_SUCCESS;
100 }
101
102
103 void options_parse(int argc, char *argv[])
104 {
105 static struct option long_options[]=
106 {
107 {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
108 {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
109 {(OPTIONSTRING)"quiet", no_argument, NULL, OPT_QUIET},
110 {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
111 {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG},
112 {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS},
113 {(OPTIONSTRING)"expire", required_argument, NULL, OPT_EXPIRE},
114 {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY},
115 {(OPTIONSTRING)"username", required_argument, NULL, OPT_USERNAME},
116 {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD},
117 {0, 0, 0, 0},
118 };
119
120 bool opt_version= false;
121 bool opt_help= false;
122 int option_index= 0;
123 while (1)
124 {
125 int option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
126 if (option_rv == -1) break;
127 switch (option_rv)
128 {
129 case 0:
130 break;
131
132 case OPT_BINARY:
133 opt_binary= true;
134 break;
135
136 case OPT_VERBOSE: /* --verbose or -v */
137 opt_verbose= OPT_VERBOSE;
138 break;
139
140 case OPT_DEBUG: /* --debug or -d */
141 opt_verbose= OPT_DEBUG;
142 break;
143
144 case OPT_VERSION: /* --version or -V */
145 opt_version= true;
146 break;
147
148 case OPT_HELP: /* --help or -h */
149 opt_help= true;
150 break;
151
152 case OPT_SERVERS: /* --servers or -s */
153 opt_servers= strdup(optarg);
154 break;
155
156 case OPT_EXPIRE: /* --expire */
157 errno= 0;
158 opt_expire= (time_t)strtoll(optarg, (char **)NULL, 10);
159 if (errno != 0)
160 {
161 std::cerr << "Incorrect value passed to --expire: `" << optarg << "`" << std::cerr;
162 exit(EXIT_FAILURE);
163 }
164 break;
165
166 case OPT_USERNAME:
167 opt_username= optarg;
168 break;
169
170 case OPT_PASSWD:
171 opt_passwd= optarg;
172 break;
173
174 case OPT_QUIET:
175 close_stdio();
176 break;
177
178 case '?':
179 /* getopt_long already printed an error message. */
180 exit(EXIT_FAILURE);
181
182 default:
183 abort();
184 }
185 }
186
187 if (opt_version)
188 {
189 version_command(PROGRAM_NAME);
190 exit(EXIT_SUCCESS);
191 }
192
193 if (opt_help)
194 {
195 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, NULL);
196 exit(EXIT_SUCCESS);
197 }
198 }