bin/memflush: fix exit code on connection failure
[m6w6/libmemcached] / src / bin / 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 == NULL)
43 {
44 char *temp;
45
46 if ((temp= getenv("MEMCACHED_SERVERS")))
47 {
48 opt_servers= strdup(temp);
49 }
50
51 if (opt_servers == NULL)
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 free(opt_servers);
63 return EXIT_FAILURE;
64 }
65
66 free(opt_servers);
67
68 memcached_st *memc= memcached_create(NULL);
69 memcached_server_push(memc, servers);
70 memcached_server_list_free(servers);
71 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL,
72 (uint64_t) opt_binary);
73
74 if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0)
75 {
76 memcached_free(memc);
77 std::cerr << "--username was supplied, but binary was not built with SASL support." << std::endl;
78 return EXIT_FAILURE;
79 }
80
81 if (opt_username)
82 {
83 memcached_return_t ret;
84 if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd)))
85 {
86 std::cerr << memcached_last_error_message(memc) << std::endl;
87 memcached_free(memc);
88 return EXIT_FAILURE;
89 }
90 }
91
92 memcached_return_t rc = memcached_flush(memc, opt_expire);
93 if (rc != MEMCACHED_SUCCESS)
94 {
95 std::cerr << memcached_last_error_message(memc) << std::endl;
96 memcached_free(memc);
97 return EXIT_FAILURE;
98 }
99
100 memcached_free(memc);
101 return EXIT_SUCCESS;
102 }
103
104
105 void options_parse(int argc, char *argv[])
106 {
107 static struct option long_options[]=
108 {
109 {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
110 {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
111 {(OPTIONSTRING)"quiet", no_argument, NULL, OPT_QUIET},
112 {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
113 {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG},
114 {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS},
115 {(OPTIONSTRING)"expire", required_argument, NULL, OPT_EXPIRE},
116 {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY},
117 {(OPTIONSTRING)"username", required_argument, NULL, OPT_USERNAME},
118 {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD},
119 {0, 0, 0, 0},
120 };
121
122 bool opt_version= false;
123 bool opt_help= false;
124 int option_index= 0;
125 while (1)
126 {
127 int option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
128 if (option_rv == -1) break;
129 switch (option_rv)
130 {
131 case 0:
132 break;
133
134 case OPT_BINARY:
135 opt_binary= true;
136 break;
137
138 case OPT_VERBOSE: /* --verbose or -v */
139 opt_verbose= OPT_VERBOSE;
140 break;
141
142 case OPT_DEBUG: /* --debug or -d */
143 opt_verbose= OPT_DEBUG;
144 break;
145
146 case OPT_VERSION: /* --version or -V */
147 opt_version= true;
148 break;
149
150 case OPT_HELP: /* --help or -h */
151 opt_help= true;
152 break;
153
154 case OPT_SERVERS: /* --servers or -s */
155 opt_servers= strdup(optarg);
156 break;
157
158 case OPT_EXPIRE: /* --expire */
159 errno= 0;
160 opt_expire= (time_t)strtoll(optarg, (char **)NULL, 10);
161 if (errno != 0)
162 {
163 std::cerr << "Incorrect value passed to --expire: `" << optarg << "`" << std::endl;
164 exit(EXIT_FAILURE);
165 }
166 break;
167
168 case OPT_USERNAME:
169 opt_username= optarg;
170 break;
171
172 case OPT_PASSWD:
173 opt_passwd= optarg;
174 break;
175
176 case OPT_QUIET:
177 close_stdio();
178 break;
179
180 case '?':
181 /* getopt_long already printed an error message. */
182 exit(EXIT_FAILURE);
183
184 default:
185 abort();
186 }
187 }
188
189 if (opt_version)
190 {
191 version_command(PROGRAM_NAME);
192 exit(EXIT_SUCCESS);
193 }
194
195 if (opt_help)
196 {
197 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, NULL);
198 exit(EXIT_SUCCESS);
199 }
200 }