check for waitid and NOWAIT
[awesomized/libmemcached] / src / bin / memrm.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_hash = NULL;
34 static char *opt_username;
35 static char *opt_passwd;
36
37 #define PROGRAM_NAME "memrm"
38 #define PROGRAM_DESCRIPTION "Erase a key or set of keys from a memcached cluster."
39
40 /* Prototypes */
41 static void options_parse(int argc, char *argv[]);
42
43 int main(int argc, char *argv[]) {
44 options_parse(argc, argv);
45 initialize_sockets();
46
47 if (opt_servers == NULL) {
48 char *temp;
49
50 if ((temp = getenv("MEMCACHED_SERVERS"))) {
51 opt_servers = strdup(temp);
52 }
53
54 if (opt_servers == NULL) {
55 std::cerr << "No Servers provided" << std::endl;
56 exit(EXIT_FAILURE);
57 }
58 }
59
60 memcached_server_st *servers = memcached_servers_parse(opt_servers);
61 if (servers == NULL or memcached_server_list_count(servers) == 0) {
62 std::cerr << "Invalid server list provided:" << opt_servers << std::endl;
63 return EXIT_FAILURE;
64 }
65
66 memcached_st *memc = memcached_create(NULL);
67 process_hash_option(memc, opt_hash);
68
69 memcached_server_push(memc, servers);
70 memcached_server_list_free(servers);
71 memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL, (uint64_t) opt_binary);
72
73 if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0) {
74 memcached_free(memc);
75 std::cerr << "--username was supplied, but binary was not built with SASL support."
76 << std::endl;
77 return EXIT_FAILURE;
78 }
79
80 if (opt_username) {
81 memcached_return_t ret;
82 if (memcached_failed(ret = memcached_set_sasl_auth_data(memc, opt_username, opt_passwd))) {
83 std::cerr << memcached_last_error_message(memc) << std::endl;
84 memcached_free(memc);
85 return EXIT_FAILURE;
86 }
87 }
88
89 int return_code = EXIT_SUCCESS;
90
91 while (optind < argc) {
92 memcached_return_t rc = memcached_delete(memc, argv[optind], strlen(argv[optind]), opt_expire);
93
94 if (rc == MEMCACHED_NOTFOUND) {
95 if (opt_verbose) {
96 std::cerr << "Could not find key \"" << argv[optind] << "\"" << std::endl;
97 }
98 } else if (memcached_fatal(rc)) {
99 if (opt_verbose) {
100 std::cerr << "Failed to delete key \"" << argv[optind]
101 << "\" :" << memcached_last_error_message(memc) << std::endl;
102 }
103
104 return_code = EXIT_FAILURE;
105 } else // success
106 {
107 if (opt_verbose) {
108 std::cout << "Deleted key " << argv[optind];
109 if (opt_expire) {
110 std::cout << " expires: " << opt_expire << std::endl;
111 }
112 std::cout << std::endl;
113 }
114 }
115
116 optind++;
117 }
118
119 memcached_free(memc);
120
121 if (opt_servers) {
122 free(opt_servers);
123 }
124
125 if (opt_hash) {
126 free(opt_hash);
127 }
128
129 return return_code;
130 }
131
132 static void options_parse(int argc, char *argv[]) {
133 memcached_programs_help_st help_options[] = {
134 {0},
135 };
136
137 static struct option long_options[] = {
138 {(OPTIONSTRING) "version", no_argument, NULL, OPT_VERSION},
139 {(OPTIONSTRING) "help", no_argument, NULL, OPT_HELP},
140 {(OPTIONSTRING) "quiet", no_argument, NULL, OPT_QUIET},
141 {(OPTIONSTRING) "verbose", no_argument, &opt_verbose, OPT_VERBOSE},
142 {(OPTIONSTRING) "debug", no_argument, &opt_verbose, OPT_DEBUG},
143 {(OPTIONSTRING) "servers", required_argument, NULL, OPT_SERVERS},
144 {(OPTIONSTRING) "expire", required_argument, NULL, OPT_EXPIRE},
145 {(OPTIONSTRING) "hash", required_argument, NULL, OPT_HASH},
146 {(OPTIONSTRING) "binary", no_argument, NULL, OPT_BINARY},
147 {(OPTIONSTRING) "username", required_argument, NULL, OPT_USERNAME},
148 {(OPTIONSTRING) "password", required_argument, NULL, OPT_PASSWD},
149 {0, 0, 0, 0},
150 };
151
152 bool opt_version = false;
153 bool opt_help = false;
154 int option_index = 0;
155
156 while (1) {
157 int option_rv = getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
158 if (option_rv == -1) {
159 break;
160 }
161
162 switch (option_rv) {
163 case 0:
164 break;
165
166 case OPT_BINARY:
167 opt_binary = 1;
168 break;
169
170 case OPT_VERBOSE: /* --verbose or -v */
171 opt_verbose = OPT_VERBOSE;
172 break;
173
174 case OPT_DEBUG: /* --debug or -d */
175 opt_verbose = OPT_DEBUG;
176 break;
177
178 case OPT_VERSION: /* --version or -V */
179 opt_version = true;
180 break;
181
182 case OPT_HELP: /* --help or -h */
183 opt_help = true;
184 break;
185
186 case OPT_SERVERS: /* --servers or -s */
187 opt_servers = strdup(optarg);
188 break;
189
190 case OPT_EXPIRE: /* --expire */
191 errno = 0;
192 opt_expire = (time_t) strtoll(optarg, (char **) NULL, 10);
193 if (errno) {
194 std::cerr << "Incorrect value passed to --expire: `" << optarg << "`" << std::endl;
195 exit(EXIT_FAILURE);
196 }
197 break;
198
199 case OPT_HASH:
200 opt_hash = strdup(optarg);
201 break;
202
203 case OPT_USERNAME:
204 opt_username = optarg;
205 break;
206
207 case OPT_PASSWD:
208 opt_passwd = optarg;
209 break;
210
211 case OPT_QUIET:
212 close_stdio();
213 break;
214
215 case '?':
216 /* getopt_long already printed an error message. */
217 exit(EXIT_SUCCESS);
218
219 default:
220 abort();
221 }
222 }
223
224 if (opt_version) {
225 version_command(PROGRAM_NAME);
226 exit(EXIT_SUCCESS);
227 }
228
229 if (opt_help) {
230 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
231 exit(EXIT_SUCCESS);
232 }
233 }