Merge in updates for sasl.
[m6w6/libmemcached] / clients / memflush.cc
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 #include "config.h"
12
13 #include <cstdio>
14 #include <cstring>
15 #include <getopt.h>
16 #include <iostream>
17 #include <unistd.h>
18
19 #include <libmemcached/memcached.h>
20 #include "client_options.h"
21 #include "utilities.h"
22
23 static int opt_binary= 0;
24 static int opt_verbose= 0;
25 static time_t opt_expire= 0;
26 static char *opt_servers= NULL;
27 static char *opt_username;
28 static char *opt_passwd;
29
30 #define PROGRAM_NAME "memflush"
31 #define PROGRAM_DESCRIPTION "Erase all data in a server of memcached servers."
32
33 /* Prototypes */
34 void options_parse(int argc, char *argv[]);
35
36 int main(int argc, char *argv[])
37 {
38 memcached_st *memc;
39 memcached_return_t rc;
40 memcached_server_st *servers;
41
42 options_parse(argc, argv);
43
44 if (!opt_servers)
45 {
46 char *temp;
47
48 if ((temp= getenv("MEMCACHED_SERVERS")))
49 opt_servers= strdup(temp);
50 else
51 {
52 fprintf(stderr, "No Servers provided\n");
53 exit(1);
54 }
55 }
56
57 memc= memcached_create(NULL);
58
59 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 rc = memcached_flush(memc, opt_expire);
84 if (rc != MEMCACHED_SUCCESS)
85 {
86 fprintf(stderr, "memflush: memcache error %s",
87 memcached_strerror(memc, rc));
88 if (memcached_last_error_errno(memc))
89 fprintf(stderr, " system error %s", strerror(memcached_last_error_errno(memc)));
90 fprintf(stderr, "\n");
91 }
92
93 memcached_free(memc);
94
95 free(opt_servers);
96
97 return EXIT_SUCCESS;
98 }
99
100
101 void options_parse(int argc, char *argv[])
102 {
103 memcached_programs_help_st help_options[]=
104 {
105 {0},
106 };
107
108 static struct option long_options[]=
109 {
110 {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
111 {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
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 int option_index= 0;
122 int option_rv;
123
124 while (1)
125 {
126 option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
127 if (option_rv == -1) break;
128 switch (option_rv)
129 {
130 case 0:
131 break;
132 case OPT_BINARY:
133 opt_binary = 1;
134 break;
135 case OPT_VERBOSE: /* --verbose or -v */
136 opt_verbose = OPT_VERBOSE;
137 break;
138 case OPT_DEBUG: /* --debug or -d */
139 opt_verbose = OPT_DEBUG;
140 break;
141 case OPT_VERSION: /* --version or -V */
142 version_command(PROGRAM_NAME);
143 break;
144 case OPT_HELP: /* --help or -h */
145 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
146 break;
147 case OPT_SERVERS: /* --servers or -s */
148 opt_servers= strdup(optarg);
149 break;
150 case OPT_EXPIRE: /* --expire */
151 opt_expire= (time_t)strtoll(optarg, (char **)NULL, 10);
152 break;
153 case OPT_USERNAME:
154 opt_username= optarg;
155 break;
156 case OPT_PASSWD:
157 opt_passwd= optarg;
158 break;
159 case '?':
160 /* getopt_long already printed an error message. */
161 exit(1);
162 default:
163 abort();
164 }
165 }
166 }