d577a166c8e0a4d4a2b7a37dbec54c741dbd398c
[m6w6/libmemcached] / src / bin / memflush.cc
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
14 */
15
16 #include "mem_config.h"
17
18 #include <cerrno>
19 #include <cstdio>
20 #include <cstring>
21 #include <getopt.h>
22 #include <iostream>
23 #include <unistd.h>
24
25 #include "libmemcached-1.0/memcached.h"
26 #include "client_options.h"
27 #include "utilities.h"
28
29 static int opt_binary = 0;
30 static int opt_verbose = 0;
31 static time_t opt_expire = 0;
32 static char *opt_servers = NULL;
33 static char *opt_username;
34 static char *opt_passwd;
35
36 #define PROGRAM_NAME "memflush"
37 #define PROGRAM_DESCRIPTION "Erase all data in a server of memcached servers."
38
39 /* Prototypes */
40 void options_parse(int argc, char *argv[]);
41
42 int main(int argc, char *argv[]) {
43 options_parse(argc, argv);
44
45 if (opt_servers == NULL) {
46 char *temp;
47
48 if ((temp = getenv("MEMCACHED_SERVERS"))) {
49 opt_servers = strdup(temp);
50 }
51
52 if (opt_servers == NULL) {
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 std::cerr << "Invalid server list provided:" << opt_servers << std::endl;
61 free(opt_servers);
62 return EXIT_FAILURE;
63 }
64
65 free(opt_servers);
66
67 memcached_st *memc = memcached_create(NULL);
68 memcached_server_push(memc, servers);
69 memcached_server_list_free(servers);
70 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, (uint64_t) opt_binary);
71
72 if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0) {
73 memcached_free(memc);
74 std::cerr << "--username was supplied, but binary was not built with SASL support."
75 << std::endl;
76 return EXIT_FAILURE;
77 }
78
79 if (opt_username) {
80 memcached_return_t ret;
81 if (memcached_failed(ret = memcached_set_sasl_auth_data(memc, opt_username, opt_passwd))) {
82 std::cerr << memcached_last_error_message(memc) << std::endl;
83 memcached_free(memc);
84 return EXIT_FAILURE;
85 }
86 }
87
88 memcached_return_t rc = memcached_flush(memc, opt_expire);
89 if (rc != MEMCACHED_SUCCESS) {
90 std::cerr << memcached_last_error_message(memc) << std::endl;
91 memcached_free(memc);
92 return EXIT_FAILURE;
93 }
94
95 memcached_free(memc);
96 return EXIT_SUCCESS;
97 }
98
99 void options_parse(int argc, char *argv[]) {
100 static struct option long_options[] = {
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 int option_rv = getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
119 if (option_rv == -1)
120 break;
121 switch (option_rv) {
122 case 0:
123 break;
124
125 case OPT_BINARY:
126 opt_binary = true;
127 break;
128
129 case OPT_VERBOSE: /* --verbose or -v */
130 opt_verbose = OPT_VERBOSE;
131 break;
132
133 case OPT_DEBUG: /* --debug or -d */
134 opt_verbose = OPT_DEBUG;
135 break;
136
137 case OPT_VERSION: /* --version or -V */
138 opt_version = true;
139 break;
140
141 case OPT_HELP: /* --help or -h */
142 opt_help = true;
143 break;
144
145 case OPT_SERVERS: /* --servers or -s */
146 opt_servers = strdup(optarg);
147 break;
148
149 case OPT_EXPIRE: /* --expire */
150 errno = 0;
151 opt_expire = (time_t) strtoll(optarg, (char **) NULL, 10);
152 if (errno) {
153 std::cerr << "Incorrect value passed to --expire: `" << optarg << "`" << std::endl;
154 exit(EXIT_FAILURE);
155 }
156 break;
157
158 case OPT_USERNAME:
159 opt_username = optarg;
160 break;
161
162 case OPT_PASSWD:
163 opt_passwd = optarg;
164 break;
165
166 case OPT_QUIET:
167 close_stdio();
168 break;
169
170 case '?':
171 /* getopt_long already printed an error message. */
172 exit(EXIT_FAILURE);
173
174 default:
175 abort();
176 }
177 }
178
179 if (opt_version) {
180 version_command(PROGRAM_NAME);
181 exit(EXIT_SUCCESS);
182 }
183
184 if (opt_help) {
185 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, NULL);
186 exit(EXIT_SUCCESS);
187 }
188 }