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 +--------------------------------------------------------------------+
16 #include "mem_config.h"
25 #include "libmemcached-1.0/memcached.h"
26 #include "client_options.h"
27 #include "utilities.h"
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
;
36 #define PROGRAM_NAME "memflush"
37 #define PROGRAM_DESCRIPTION "Erase all data in a server of memcached servers."
40 void options_parse(int argc
, char *argv
[]);
42 int main(int argc
, char *argv
[]) {
43 options_parse(argc
, argv
);
45 if (opt_servers
== NULL
) {
48 if ((temp
= getenv("MEMCACHED_SERVERS"))) {
49 opt_servers
= strdup(temp
);
52 if (opt_servers
== NULL
) {
53 std::cerr
<< "No Servers provided" << std::endl
;
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
;
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
);
72 if (opt_username
and LIBMEMCACHED_WITH_SASL_SUPPORT
== 0) {
74 std::cerr
<< "--username was supplied, but binary was not built with SASL support."
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
;
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
;
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
},
114 bool opt_version
= false;
115 bool opt_help
= false;
116 int option_index
= 0;
118 int option_rv
= getopt_long(argc
, argv
, "Vhvds:", long_options
, &option_index
);
129 case OPT_VERBOSE
: /* --verbose or -v */
130 opt_verbose
= OPT_VERBOSE
;
133 case OPT_DEBUG
: /* --debug or -d */
134 opt_verbose
= OPT_DEBUG
;
137 case OPT_VERSION
: /* --version or -V */
141 case OPT_HELP
: /* --help or -h */
145 case OPT_SERVERS
: /* --servers or -s */
146 opt_servers
= strdup(optarg
);
149 case OPT_EXPIRE
: /* --expire */
151 opt_expire
= (time_t) strtoll(optarg
, (char **) NULL
, 10);
153 std::cerr
<< "Incorrect value passed to --expire: `" << optarg
<< "`" << std::endl
;
159 opt_username
= optarg
;
171 /* getopt_long already printed an error message. */
180 version_command(PROGRAM_NAME
);
185 help_command(PROGRAM_NAME
, PROGRAM_DESCRIPTION
, long_options
, NULL
);