0e21d70937453278a6aaf2c29332b10ea9a0452f
[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: return ("List which servers you wish to connect to.");
71 case OPT_VERSION: return ("Display the version of the application and then exit.");
72 case OPT_HELP: return ("Display this message and then exit.");
73 case OPT_VERBOSE: return ("Give more details on the progression of the application.");
74 case OPT_QUIET: return ("stderr and stdin will be closed at application startup.");
75 case OPT_DEBUG: return ("Provide output only useful for debugging.");
76 case OPT_FLAG: return ("Provide flag information for storage operation.");
77 case OPT_EXPIRE: return ("Set the expire option for the object.");
78 case OPT_SET: return ("Use set command with memcached when storing.");
79 case OPT_REPLACE: return ("Use replace command with memcached when storing.");
80 case OPT_ADD: return ("Use add command with memcached when storing.");
81 case OPT_SLAP_EXECUTE_NUMBER: return ("Number of times to execute the given test.");
82 case OPT_SLAP_INITIAL_LOAD: return ("Number of key pairs to load before executing tests.");
83 case OPT_SLAP_TEST: return ("Test to run (currently \"get\" or \"set\").");
84 case OPT_SLAP_CONCURRENCY: return ("Number of users to simulate with load.");
85 case OPT_SLAP_NON_BLOCK: return ("Set TCP up to use non-blocking IO.");
86 case OPT_SLAP_TCP_NODELAY: return ("Set TCP socket up to use nodelay.");
87 case OPT_FLUSH: return ("Flush servers before running tests.");
88 case OPT_HASH: return ("Select hash type.");
89 case OPT_BINARY: return ("Switch to binary protocol.");
90 case OPT_ANALYZE: return ("Analyze the provided servers.");
91 case OPT_UDP: return ("Use UDP protocol when communicating with server.");
92 case OPT_BUFFER: return ("Enable request buffering.");
93 case OPT_USERNAME: return "Username to use for SASL authentication";
94 case OPT_PASSWD: return "Password to use for SASL authentication";
95 case OPT_FILE: return "Path to file in which to save result";
96 case OPT_STAT_ARGS: return "Argument for statistics";
97 case OPT_SERVER_VERSION: return "Memcached daemon software version";
98 default: break;
99 };
100
101 assert(0);
102 return "forgot to document this function :)";
103 }
104
105 void help_command(const char *command_name, const char *description,
106 const struct option *long_options, memcached_programs_help_st *options) {
107 unsigned int x;
108 (void) options;
109
110 printf("%s v%u.%u\n\n", command_name, 1U, 0U);
111 printf("\t%s\n\n", description);
112 printf("Current options. A '=' means the option takes a value.\n\n");
113
114 for (x = 0; long_options[x].name; x++) {
115 const char *help_message;
116
117 printf("\t --%s%c\n", long_options[x].name, long_options[x].has_arg ? '=' : ' ');
118 if ((help_message = lookup_help(memcached_options(long_options[x].val))))
119 printf("\t\t%s\n", help_message);
120 }
121
122 printf("\n");
123 exit(EXIT_SUCCESS);
124 }
125
126 void process_hash_option(memcached_st *memc, char *opt_hash) {
127 uint64_t set;
128 memcached_return_t rc;
129
130 if (opt_hash == NULL) {
131 return;
132 }
133
134 set = MEMCACHED_HASH_DEFAULT; /* Just here to solve warning */
135 if (!strcasecmp(opt_hash, "CRC")) {
136 set = MEMCACHED_HASH_CRC;
137 } else if (!strcasecmp(opt_hash, "FNV1_64")) {
138 set = MEMCACHED_HASH_FNV1_64;
139 } else if (!strcasecmp(opt_hash, "FNV1A_64")) {
140 set = MEMCACHED_HASH_FNV1A_64;
141 } else if (!strcasecmp(opt_hash, "FNV1_32")) {
142 set = MEMCACHED_HASH_FNV1_32;
143 } else if (!strcasecmp(opt_hash, "FNV1A_32")) {
144 set = MEMCACHED_HASH_FNV1A_32;
145 } else {
146 fprintf(stderr, "hash: type not recognized %s\n", opt_hash);
147 exit(EXIT_FAILURE);
148 }
149
150 rc = memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_HASH, set);
151 if (rc != MEMCACHED_SUCCESS) {
152 fprintf(stderr, "hash: memcache error %s\n", memcached_strerror(memc, rc));
153 exit(EXIT_FAILURE);
154 }
155 }
156
157 void initialize_sockets(void) {
158 /* Define the function for all platforms to avoid #ifdefs in each program */
159 #if defined(_WIN32)
160 WSADATA wsaData;
161 if (WSAStartup(MAKEWORD(2, 0), &wsaData) != 0) {
162 fprintf(stderr, "Socket Initialization Error. Program aborted\n");
163 exit(EXIT_FAILURE);
164 }
165 #endif // #if defined(_WIN32)
166 }