Fix for lp:1123153 (poor use of strtol).
[awesomized/libmemcached] / clients / memping.cc
1 /* LibMemcached
2 * Copyright (C) 2011-2012 Data Differential, http://datadifferential.com/
3 * Copyright (C) 2006-2009 Brian Aker
4 * All rights reserved.
5 *
6 * Use and distribution licensed under the BSD license. See
7 * the COPYING file in the parent directory for full text.
8 *
9 * Summary:
10 *
11 */
12 #include "mem_config.h"
13
14 #include <cerrno>
15 #include <cstdio>
16 #include <cstring>
17 #include <getopt.h>
18 #include <unistd.h>
19
20 #include <libmemcached-1.0/memcached.h>
21 #include <libmemcachedutil-1.0/util.h>
22 #include "client_options.h"
23 #include "utilities.h"
24
25 #include <iostream>
26
27 static bool opt_binary= false;
28 static int opt_verbose= 0;
29 static time_t opt_expire= 0;
30 static char *opt_servers= NULL;
31 static char *opt_username;
32 static char *opt_passwd;
33
34 #define PROGRAM_NAME "memping"
35 #define PROGRAM_DESCRIPTION "Ping a server to see if it is alive"
36
37 /* Prototypes */
38 void options_parse(int argc, char *argv[]);
39
40 int main(int argc, char *argv[])
41 {
42 options_parse(argc, argv);
43
44 if (opt_servers == NULL)
45 {
46 char *temp;
47
48 if ((temp= getenv("MEMCACHED_SERVERS")))
49 {
50 opt_servers= strdup(temp);
51 }
52 else
53 {
54 std::cerr << "No Servers provided" << std::endl;
55 exit(EXIT_FAILURE);
56 }
57 }
58
59 int exit_code= EXIT_SUCCESS;
60 memcached_server_st *servers= memcached_servers_parse(opt_servers);
61 {
62 for (uint32_t x= 0; x < memcached_server_list_count(servers); x++)
63 {
64 memcached_return_t instance_rc;
65 const char *hostname= servers[x].hostname;
66 in_port_t port= servers[x].port;
67
68 if (libmemcached_util_ping2(hostname, port, opt_username, opt_passwd, &instance_rc) == false)
69 {
70 std::cerr << "Failed to ping " << hostname << ":" << port << " " << memcached_strerror(NULL, instance_rc) << std::endl;
71 exit_code= EXIT_FAILURE;
72 }
73 }
74 }
75 memcached_server_list_free(servers);
76
77 free(opt_servers);
78
79 return exit_code;
80 }
81
82
83 void options_parse(int argc, char *argv[])
84 {
85 memcached_programs_help_st help_options[]=
86 {
87 {0},
88 };
89
90 static struct option long_options[]=
91 {
92 {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
93 {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
94 {(OPTIONSTRING)"quiet", no_argument, NULL, OPT_QUIET},
95 {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
96 {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG},
97 {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS},
98 {(OPTIONSTRING)"expire", required_argument, NULL, OPT_EXPIRE},
99 {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY},
100 {(OPTIONSTRING)"username", required_argument, NULL, OPT_USERNAME},
101 {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD},
102 {0, 0, 0, 0},
103 };
104
105 bool opt_version= false;
106 bool opt_help= false;
107 int option_index= 0;
108 while (1)
109 {
110 int option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
111
112 if (option_rv == -1) break;
113
114 switch (option_rv)
115 {
116 case 0:
117 break;
118
119 case OPT_BINARY:
120 opt_binary= true;
121 break;
122
123 case OPT_VERBOSE: /* --verbose or -v */
124 opt_verbose = OPT_VERBOSE;
125 break;
126
127 case OPT_DEBUG: /* --debug or -d */
128 opt_verbose = OPT_DEBUG;
129 break;
130
131 case OPT_VERSION: /* --version or -V */
132 version_command(PROGRAM_NAME);
133 break;
134
135 case OPT_HELP: /* --help or -h */
136 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
137 break;
138
139 case OPT_SERVERS: /* --servers or -s */
140 opt_servers= strdup(optarg);
141 break;
142
143 case OPT_EXPIRE: /* --expire */
144 errno= 0;
145 opt_expire= time_t(strtoll(optarg, (char **)NULL, 10));
146 if (errno != 0)
147 {
148 std::cerr << "Incorrect value passed to --expire: `" << optarg << "`" << std::cerr;
149 exit(EXIT_FAILURE);
150 }
151 break;
152
153 case OPT_USERNAME:
154 opt_username= optarg;
155 opt_binary= true;
156 break;
157
158 case OPT_PASSWD:
159 opt_passwd= optarg;
160 break;
161
162 case OPT_QUIET:
163 close_stdio();
164 break;
165
166 case '?':
167 /* getopt_long already printed an error message. */
168 exit(1);
169 default:
170 abort();
171 }
172 }
173
174 if (opt_version)
175 {
176 version_command(PROGRAM_NAME);
177 exit(EXIT_SUCCESS);
178 }
179
180 if (opt_help)
181 {
182 help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, help_options);
183 exit(EXIT_SUCCESS);
184 }
185 }