b0a0f1b17164d710da2411112021905f128b6f62
[m6w6/libmemcached] / src / bin / common / utilities.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 "utilities.h"
19
20 #include <cstdio>
21 #include <cassert>
22 #include <cstdlib>
23 #include <cstring>
24 #include <ctype.h>
25 #include <fcntl.h>
26 #include <sys/stat.h>
27 #include <sys/types.h>
28 #include <unistd.h>
29
30 long int timedif(struct timeval a, struct timeval b) {
31 long us, s;
32
33 us = (int) (a.tv_usec - b.tv_usec);
34 us /= 1000;
35 s = (int) (a.tv_sec - b.tv_sec);
36 s *= 1000;
37 return s + us;
38 }
39
40 void version_command(const char *command_name) {
41 printf("%s v%u.%u\n", command_name, 1U, 0U);
42 exit(EXIT_SUCCESS);
43 }
44
45 void close_stdio(void) {
46 int fd;
47 if ((fd = open("/dev/null", O_RDWR, 0)) < 0) {
48 return;
49 } else {
50 if (dup2(fd, STDIN_FILENO) < 0) {
51 return;
52 }
53
54 if (dup2(fd, STDOUT_FILENO) < 0) {
55 return;
56 }
57
58 if (dup2(fd, STDERR_FILENO) < 0) {
59 return;
60 }
61
62 if (fd > STDERR_FILENO) {
63 close(fd);
64 }
65 }
66 }
67
68 static const char *lookup_help(memcached_options option) {
69 switch (option) {
70 case OPT_SERVERS:
71 return ("List which servers you wish to connect to.");
72 case OPT_VERSION:
73 return ("Display the version of the application and then exit.");
74 case OPT_HELP:
75 return ("Display this message and then exit.");
76 case OPT_VERBOSE:
77 return ("Give more details on the progression of the application.");
78 case OPT_QUIET:
79 return ("stderr and stdin will be closed at application startup.");
80 case OPT_DEBUG:
81 return ("Provide output only useful for debugging.");
82 case OPT_FLAG:
83 return ("Provide flag information for storage operation.");
84 case OPT_EXPIRE:
85 return ("Set the expire option for the object.");
86 case OPT_SET:
87 return ("Use set command with memcached when storing.");
88 case OPT_REPLACE:
89 return ("Use replace command with memcached when storing.");
90 case OPT_ADD:
91 return ("Use add command with memcached when storing.");
92 case OPT_SLAP_EXECUTE_NUMBER:
93 return ("Number of times to execute the given test.");
94 case OPT_SLAP_INITIAL_LOAD:
95 return ("Number of key pairs to load before executing tests.");
96 case OPT_SLAP_TEST:
97 return ("Test to run (currently \"get\" or \"set\").");
98 case OPT_SLAP_CONCURRENCY:
99 return ("Number of users to simulate with load.");
100 case OPT_SLAP_NON_BLOCK:
101 return ("Set TCP up to use non-blocking IO.");
102 case OPT_SLAP_TCP_NODELAY:
103 return ("Set TCP socket up to use nodelay.");
104 case OPT_FLUSH:
105 return ("Flush servers before running tests.");
106 case OPT_HASH:
107 return ("Select hash type.");
108 case OPT_BINARY:
109 return ("Switch to binary protocol.");
110 case OPT_ANALYZE:
111 return ("Analyze the provided servers.");
112 case OPT_UDP:
113 return ("Use UDP protocol when communicating with server.");
114 case OPT_BUFFER:
115 return ("Enable request buffering.");
116 case OPT_USERNAME:
117 return "Username to use for SASL authentication";
118 case OPT_PASSWD:
119 return "Password to use for SASL authentication";
120 case OPT_FILE:
121 return "Path to file in which to save result";
122 case OPT_STAT_ARGS:
123 return "Argument for statistics";
124 case OPT_SERVER_VERSION:
125 return "Memcached daemon software version";
126 default:
127 break;
128 };
129
130 assert(0);
131 return "forgot to document this function :)";
132 }
133
134 void help_command(const char *command_name, const char *description,
135 const struct option *long_options, memcached_programs_help_st *options) {
136 unsigned int x;
137 (void) options;
138
139 printf("%s v%u.%u\n\n", command_name, 1U, 0U);
140 printf("\t%s\n\n", description);
141 printf("Current options. A '=' means the option takes a value.\n\n");
142
143 for (x = 0; long_options[x].name; x++) {
144 const char *help_message;
145
146 printf("\t --%s%c\n", long_options[x].name, long_options[x].has_arg ? '=' : ' ');
147 if ((help_message = lookup_help(memcached_options(long_options[x].val))))
148 printf("\t\t%s\n", help_message);
149 }
150
151 printf("\n");
152 exit(EXIT_SUCCESS);
153 }
154
155 void process_hash_option(memcached_st *memc, char *opt_hash) {
156 uint64_t set;
157 memcached_return_t rc;
158
159 if (opt_hash == NULL) {
160 return;
161 }
162
163 set = MEMCACHED_HASH_DEFAULT; /* Just here to solve warning */
164 if (!strcasecmp(opt_hash, "CRC")) {
165 set = MEMCACHED_HASH_CRC;
166 } else if (!strcasecmp(opt_hash, "FNV1_64")) {
167 set = MEMCACHED_HASH_FNV1_64;
168 } else if (!strcasecmp(opt_hash, "FNV1A_64")) {
169 set = MEMCACHED_HASH_FNV1A_64;
170 } else if (!strcasecmp(opt_hash, "FNV1_32")) {
171 set = MEMCACHED_HASH_FNV1_32;
172 } else if (!strcasecmp(opt_hash, "FNV1A_32")) {
173 set = MEMCACHED_HASH_FNV1A_32;
174 } else {
175 fprintf(stderr, "hash: type not recognized %s\n", opt_hash);
176 exit(EXIT_FAILURE);
177 }
178
179 rc = memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, set);
180 if (rc != MEMCACHED_SUCCESS) {
181 fprintf(stderr, "hash: memcache error %s\n", memcached_strerror(memc, rc));
182 exit(EXIT_FAILURE);
183 }
184 }
185
186 void initialize_sockets(void) {
187 /* Define the function for all platforms to avoid #ifdefs in each program */
188 #if defined(_WIN32)
189 WSADATA wsaData;
190 if (WSAStartup(MAKEWORD(2, 0), &wsaData)) {
191 fprintf(stderr, "Socket Initialization Error. Program aborted\n");
192 exit(EXIT_FAILURE);
193 }
194 #endif // #if defined(_WIN32)
195 }