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