src/bin: apply clang-format
[awesomized/libmemcached] / src / bin / memping.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 <unistd.h>
23
24 #include "libmemcached-1.0/memcached.h"
25 #include "libmemcachedutil-1.0/util.h"
26 #include "client_options.h"
27 #include "utilities.h"
28
29 #include <iostream>
30
31 static bool opt_binary = false;
32 static int opt_verbose = 0;
33 static time_t opt_expire = 0;
34 static char *opt_servers = NULL;
35 static char *opt_username;
36 static char *opt_passwd;
37
38 #define PROGRAM_NAME "memping"
39 #define PROGRAM_DESCRIPTION "Ping a server to see if it is alive"
40
41 /* Prototypes */
42 void options_parse(int argc, char *argv[]);
43
44 int main(int argc, char *argv[]) {
45 options_parse(argc, argv);
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 int exit_code = EXIT_SUCCESS;
61 memcached_server_st *servers = memcached_servers_parse(opt_servers);
62 if (servers == NULL or memcached_server_list_count(servers) == 0) {
63 std::cerr << "Invalid server list provided:" << opt_servers << std::endl;
64 exit_code = EXIT_FAILURE;
65 } else {
66 for (uint32_t x = 0; x < memcached_server_list_count(servers); x++) {
67 memcached_return_t instance_rc;
68 const char *hostname = servers[x].hostname;
69 in_port_t port = servers[x].port;
70
71 if (opt_verbose) {
72 std::cout << "Trying to ping " << hostname << ":" << port << std::endl;
73 }
74
75 if (libmemcached_util_ping2(hostname, port, opt_username, opt_passwd, &instance_rc) == false)
76 {
77 std::cerr << "Failed to ping " << hostname << ":" << port << " "
78 << memcached_strerror(NULL, instance_rc) << std::endl;
79 exit_code = EXIT_FAILURE;
80 }
81 }
82 }
83 memcached_server_list_free(servers);
84
85 free(opt_servers);
86
87 return exit_code;
88 }
89
90 void options_parse(int argc, char *argv[]) {
91 memcached_programs_help_st help_options[] = {
92 {0},
93 };
94
95 static struct option long_options[] = {
96 {(OPTIONSTRING) "version", no_argument, NULL, OPT_VERSION},
97 {(OPTIONSTRING) "help", no_argument, NULL, OPT_HELP},
98 {(OPTIONSTRING) "quiet", no_argument, NULL, OPT_QUIET},
99 {(OPTIONSTRING) "verbose", no_argument, &opt_verbose, OPT_VERBOSE},
100 {(OPTIONSTRING) "debug", no_argument, &opt_verbose, OPT_DEBUG},
101 {(OPTIONSTRING) "servers", required_argument, NULL, OPT_SERVERS},
102 {(OPTIONSTRING) "expire", required_argument, NULL, OPT_EXPIRE},
103 {(OPTIONSTRING) "binary", no_argument, NULL, OPT_BINARY},
104 {(OPTIONSTRING) "username", required_argument, NULL, OPT_USERNAME},
105 {(OPTIONSTRING) "password", required_argument, NULL, OPT_PASSWD},
106 {0, 0, 0, 0},
107 };
108
109 bool opt_version = false;
110 bool opt_help = false;
111 int option_index = 0;
112 while (1) {
113 int option_rv = getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
114
115 if (option_rv == -1)
116 break;
117
118 switch (option_rv) {
119 case 0: break;
120
121 case OPT_BINARY: opt_binary = true; break;
122
123 case OPT_VERBOSE: /* --verbose or -v */ opt_verbose = OPT_VERBOSE; break;
124
125 case OPT_DEBUG: /* --debug or -d */ opt_verbose = OPT_DEBUG; break;
126
127 case OPT_VERSION: /* --version or -V */ version_command(PROGRAM_NAME); break;
128
129 case OPT_HELP: /* --help or -h */
130 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
131 break;
132
133 case OPT_SERVERS: /* --servers or -s */ opt_servers = strdup(optarg); break;
134
135 case OPT_EXPIRE: /* --expire */
136 errno = 0;
137 opt_expire = time_t(strtoll(optarg, (char **) NULL, 10));
138 if (errno != 0) {
139 std::cerr << "Incorrect value passed to --expire: `" << optarg << "`" << std::endl;
140 exit(EXIT_FAILURE);
141 }
142 break;
143
144 case OPT_USERNAME:
145 opt_username = optarg;
146 opt_binary = true;
147 break;
148
149 case OPT_PASSWD: opt_passwd = optarg; break;
150
151 case OPT_QUIET: close_stdio(); break;
152
153 case '?':
154 /* getopt_long already printed an error message. */
155 exit(1);
156 default: abort();
157 }
158 }
159
160 if (opt_version) {
161 version_command(PROGRAM_NAME);
162 exit(EXIT_SUCCESS);
163 }
164
165 if (opt_help) {
166 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
167 exit(EXIT_SUCCESS);
168 }
169 }