~dave-terei/libmemcached/sasl-fixes

631 by Brian Aker
Updated copyright headers.
1
/* LibMemcached
2
 * Copyright (C) 2006-2009 Brian Aker
3
 * All rights reserved.
4
 *
5
 * Use and distribution licensed under the BSD license.  See
6
 * the COPYING file in the parent directory for full text.
7
 *
8
 * Summary:
9
 *
10
 */
862 by Brian Aker
Merge in changes for ICC fix.
11
#include "config.h"
631 by Brian Aker
Updated copyright headers.
12
929.1.228 by Brian Aker
Merge in fixes for SASL.
13
#include <cstdio>
14
#include <cstring>
15
#include <getopt.h>
16
#include <iostream>
317.1.7 by brian@gir-2.local
Huge refactoring of directory structure.
17
#include <unistd.h>
929.1.228 by Brian Aker
Merge in fixes for SASL.
18
317.1.7 by brian@gir-2.local
Huge refactoring of directory structure.
19
#include <libmemcached/memcached.h>
20
#include "client_options.h"
21
#include "utilities.h"
22
353.1.1 by trond@localhost
Initial support for the binary protocol
23
static int opt_binary= 0;
317.1.7 by brian@gir-2.local
Huge refactoring of directory structure.
24
static int opt_verbose= 0;
25
static time_t opt_expire= 0;
26
static char *opt_servers= NULL;
800.1.2 by Trond Norbye
Add support for SASL
27
static char *opt_username;
28
static char *opt_passwd;
317.1.7 by brian@gir-2.local
Huge refactoring of directory structure.
29
30
#define PROGRAM_NAME "memflush"
31
#define PROGRAM_DESCRIPTION "Erase all data in a server of memcached servers."
32
33
/* Prototypes */
34
void options_parse(int argc, char *argv[]);
35
36
int main(int argc, char *argv[])
37
{
38
  options_parse(argc, argv);
39
929.1.259 by Brian Aker
Improve tesing of command line apps
40
  if (opt_servers == false)
317.1.7 by brian@gir-2.local
Huge refactoring of directory structure.
41
  {
42
    char *temp;
43
44
    if ((temp= getenv("MEMCACHED_SERVERS")))
929.1.259 by Brian Aker
Improve tesing of command line apps
45
    {
317.1.7 by brian@gir-2.local
Huge refactoring of directory structure.
46
      opt_servers= strdup(temp);
929.1.259 by Brian Aker
Improve tesing of command line apps
47
    }
317.1.7 by brian@gir-2.local
Huge refactoring of directory structure.
48
    else
49
    {
929.1.259 by Brian Aker
Improve tesing of command line apps
50
      std::cerr << "No Servers provided" << std::endl;
51
      exit(EXIT_FAILURE);
317.1.7 by brian@gir-2.local
Huge refactoring of directory structure.
52
    }
53
  }
54
929.1.259 by Brian Aker
Improve tesing of command line apps
55
  memcached_st *memc= memcached_create(NULL);
317.1.7 by brian@gir-2.local
Huge refactoring of directory structure.
56
929.1.259 by Brian Aker
Improve tesing of command line apps
57
  memcached_server_st *servers= memcached_servers_parse(opt_servers);
317.1.7 by brian@gir-2.local
Huge refactoring of directory structure.
58
  memcached_server_push(memc, servers);
59
  memcached_server_list_free(servers);
537.3.1 by Monty Taylor
Fixed -Wconversion warnings.
60
  memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_BINARY_PROTOCOL,
61
                         (uint64_t) opt_binary);
800.1.2 by Trond Norbye
Add support for SASL
62
929.1.228 by Brian Aker
Merge in fixes for SASL.
63
  if (opt_username and LIBMEMCACHED_WITH_SASL_SUPPORT == 0)
64
  {
65
    memcached_free(memc);
66
    std::cerr << "--username was supplied, but binary was not built with SASL support." << std::endl;
67
    return EXIT_FAILURE;
68
  }
69
929.1.233 by Brian Aker
Merge in updates for sasl.
70
  if (opt_username)
800.1.2 by Trond Norbye
Add support for SASL
71
  {
929.1.233 by Brian Aker
Merge in updates for sasl.
72
    memcached_return_t ret;
73
    if (memcached_failed(ret= memcached_set_sasl_auth_data(memc, opt_username, opt_passwd)))
74
    {
75
      std::cerr << memcached_last_error_message(memc) << std::endl;
76
      memcached_free(memc);
77
      return EXIT_FAILURE;
78
    }
800.1.2 by Trond Norbye
Add support for SASL
79
  }
80
929.1.259 by Brian Aker
Improve tesing of command line apps
81
  memcached_return_t rc = memcached_flush(memc, opt_expire);
800.1.2 by Trond Norbye
Add support for SASL
82
  if (rc != MEMCACHED_SUCCESS)
317.1.7 by brian@gir-2.local
Huge refactoring of directory structure.
83
  {
929.1.259 by Brian Aker
Improve tesing of command line apps
84
    std::cerr << memcached_last_error_message(memc) << std::endl;
317.1.7 by brian@gir-2.local
Huge refactoring of directory structure.
85
  }
86
87
  memcached_free(memc);
88
89
  free(opt_servers);
90
928 by Brian Aker
Merge in updates to make sure exit/return is done properly.
91
  return EXIT_SUCCESS;
317.1.7 by brian@gir-2.local
Huge refactoring of directory structure.
92
}
93
94
95
void options_parse(int argc, char *argv[])
96
{
97
  static struct option long_options[]=
98
  {
569.1.1 by Trond Norbye
Fix problems reported by: -Wsign-conversion from gcc 4.4 on Solaris
99
    {(OPTIONSTRING)"version", no_argument, NULL, OPT_VERSION},
100
    {(OPTIONSTRING)"help", no_argument, NULL, OPT_HELP},
929.1.259 by Brian Aker
Improve tesing of command line apps
101
    {(OPTIONSTRING)"quiet", no_argument, NULL, OPT_QUIET},
569.1.1 by Trond Norbye
Fix problems reported by: -Wsign-conversion from gcc 4.4 on Solaris
102
    {(OPTIONSTRING)"verbose", no_argument, &opt_verbose, OPT_VERBOSE},
103
    {(OPTIONSTRING)"debug", no_argument, &opt_verbose, OPT_DEBUG},
104
    {(OPTIONSTRING)"servers", required_argument, NULL, OPT_SERVERS},
105
    {(OPTIONSTRING)"expire", required_argument, NULL, OPT_EXPIRE},
106
    {(OPTIONSTRING)"binary", no_argument, NULL, OPT_BINARY},
800.1.2 by Trond Norbye
Add support for SASL
107
    {(OPTIONSTRING)"username", required_argument, NULL, OPT_USERNAME},
108
    {(OPTIONSTRING)"password", required_argument, NULL, OPT_PASSWD},
317.1.7 by brian@gir-2.local
Huge refactoring of directory structure.
109
    {0, 0, 0, 0},
110
  };
929.1.259 by Brian Aker
Improve tesing of command line apps
111
112
  bool opt_version= false;
113
  bool opt_help= false;
317.1.7 by brian@gir-2.local
Huge refactoring of directory structure.
114
  int option_index= 0;
800.1.2 by Trond Norbye
Add support for SASL
115
  while (1)
317.1.7 by brian@gir-2.local
Huge refactoring of directory structure.
116
  {
929.1.259 by Brian Aker
Improve tesing of command line apps
117
    int option_rv= getopt_long(argc, argv, "Vhvds:", long_options, &option_index);
317.1.7 by brian@gir-2.local
Huge refactoring of directory structure.
118
    if (option_rv == -1) break;
119
    switch (option_rv)
120
    {
121
    case 0:
122
      break;
929.1.259 by Brian Aker
Improve tesing of command line apps
123
353.1.1 by trond@localhost
Initial support for the binary protocol
124
    case OPT_BINARY:
929.1.259 by Brian Aker
Improve tesing of command line apps
125
      opt_binary= true;
353.1.1 by trond@localhost
Initial support for the binary protocol
126
      break;
929.1.259 by Brian Aker
Improve tesing of command line apps
127
317.1.7 by brian@gir-2.local
Huge refactoring of directory structure.
128
    case OPT_VERBOSE: /* --verbose or -v */
929.1.259 by Brian Aker
Improve tesing of command line apps
129
      opt_verbose= OPT_VERBOSE;
317.1.7 by brian@gir-2.local
Huge refactoring of directory structure.
130
      break;
929.1.259 by Brian Aker
Improve tesing of command line apps
131
317.1.7 by brian@gir-2.local
Huge refactoring of directory structure.
132
    case OPT_DEBUG: /* --debug or -d */
929.1.259 by Brian Aker
Improve tesing of command line apps
133
      opt_verbose= OPT_DEBUG;
317.1.7 by brian@gir-2.local
Huge refactoring of directory structure.
134
      break;
929.1.259 by Brian Aker
Improve tesing of command line apps
135
317.1.7 by brian@gir-2.local
Huge refactoring of directory structure.
136
    case OPT_VERSION: /* --version or -V */
929.1.259 by Brian Aker
Improve tesing of command line apps
137
      opt_version= true;
317.1.7 by brian@gir-2.local
Huge refactoring of directory structure.
138
      break;
929.1.259 by Brian Aker
Improve tesing of command line apps
139
317.1.7 by brian@gir-2.local
Huge refactoring of directory structure.
140
    case OPT_HELP: /* --help or -h */
929.1.259 by Brian Aker
Improve tesing of command line apps
141
      opt_help= true;
317.1.7 by brian@gir-2.local
Huge refactoring of directory structure.
142
      break;
929.1.259 by Brian Aker
Improve tesing of command line apps
143
317.1.7 by brian@gir-2.local
Huge refactoring of directory structure.
144
    case OPT_SERVERS: /* --servers or -s */
145
      opt_servers= strdup(optarg);
146
      break;
929.1.259 by Brian Aker
Improve tesing of command line apps
147
317.1.7 by brian@gir-2.local
Huge refactoring of directory structure.
148
    case OPT_EXPIRE: /* --expire */
149
      opt_expire= (time_t)strtoll(optarg, (char **)NULL, 10);
150
      break;
929.1.259 by Brian Aker
Improve tesing of command line apps
151
800.1.2 by Trond Norbye
Add support for SASL
152
    case OPT_USERNAME:
153
      opt_username= optarg;
154
      break;
929.1.259 by Brian Aker
Improve tesing of command line apps
155
800.1.2 by Trond Norbye
Add support for SASL
156
    case OPT_PASSWD:
157
      opt_passwd= optarg;
158
      break;
929.1.259 by Brian Aker
Improve tesing of command line apps
159
160
    case OPT_QUIET:
161
      close_stdio();
162
      break;
163
317.1.7 by brian@gir-2.local
Huge refactoring of directory structure.
164
    case '?':
165
      /* getopt_long already printed an error message. */
929.1.259 by Brian Aker
Improve tesing of command line apps
166
      exit(EXIT_FAILURE);
167
317.1.7 by brian@gir-2.local
Huge refactoring of directory structure.
168
    default:
169
      abort();
170
    }
171
  }
929.1.259 by Brian Aker
Improve tesing of command line apps
172
173
  if (opt_version)
174
  {
175
    version_command(PROGRAM_NAME);
176
    exit(EXIT_SUCCESS);
177
  }
178
179
  if (opt_help)
180
  {
181
    help_command(PROGRAM_NAME, PROGRAM_DESCRIPTION, long_options, NULL);
182
    exit(EXIT_SUCCESS);
183
  }
317.1.7 by brian@gir-2.local
Huge refactoring of directory structure.
184
}