1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
|
/*
Copyright (c) 2010 Gluster, Inc. <http://www.gluster.com>
This file is part of GlusterFS.
GlusterFS is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
by the Free Software Foundation; either version 3 of the License,
or (at your option) any later version.
GlusterFS is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see
<http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/resource.h>
#include <sys/file.h>
#include <netdb.h>
#include <signal.h>
#include <libgen.h>
#include <sys/utsname.h>
#include <stdint.h>
#include <pthread.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
#include <semaphore.h>
#include <errno.h>
#ifndef _CONFIG_H
#define _CONFIG_H
#include "config.h"
#endif
#ifdef HAVE_MALLOC_H
#include <malloc.h>
#endif
#ifdef HAVE_MALLOC_STATS
#ifdef DEBUG
#include <mcheck.h>
#endif
#endif
#include "cli.h"
#include "cli-cmd.h"
#include "cli-mem-types.h"
#include "xlator.h"
#include "glusterfs.h"
#include "compat.h"
#include "logging.h"
#include "dict.h"
#include "list.h"
#include "timer.h"
#include "stack.h"
#include "revision.h"
#include "common-utils.h"
#include "event.h"
#include "globals.h"
#include "syscall.h"
#include <fnmatch.h>
/* using argp for command line parsing */
static char gf_doc[] = "";
static char argp_doc[] = "COMMAND [PARAM ...]";
const char *argp_program_version = "" \
PACKAGE_NAME" "PACKAGE_VERSION" built on "__DATE__" "__TIME__ \
"\nRepository revision: " GLUSTERFS_REPOSITORY_REVISION "\n" \
"Copyright (c) 2006-2010 Gluster Inc. " \
"<http://www.gluster.com>\n" \
"GlusterFS comes with ABSOLUTELY NO WARRANTY.\n" \
"You may redistribute copies of GlusterFS under the terms of "\
"the GNU General Public License.";
const char *argp_program_bug_address = "<" PACKAGE_BUGREPORT ">";
static struct argp_option gf_options[] = {
{0, 0, 0, 0, "Basic options:"},
{"debug", ARGP_DEBUG_KEY, 0, 0,
"Process runs in foreground and logs to console"},
{"remote-port", ARGP_PORT_KEY, "PORT", 0,
"glusterd port to connect with"},
{0, }
};
struct rpc_clnt *global_rpc;
rpc_clnt_prog_t *cli_rpc_prog;
extern struct rpc_clnt_program cli3_1_prog;
static error_t
parse_opts (int key, char *arg, struct argp_state *argp_state)
{
struct cli_state *state = NULL;
char **argv = NULL;
state = argp_state->input;
switch (key) {
case ARGP_DEBUG_KEY:
break;
case ARGP_PORT_KEY:
state->remote_port = strtol (arg, NULL, 0);
break;
case ARGP_KEY_ARG:
if (!state->argc) {
argv = calloc (state->argc + 2,
sizeof (*state->argv));
} else {
argv = realloc (state->argv, (state->argc + 2) *
sizeof (*state->argv));
}
if (!argv)
return -1;
state->argv = argv;
argv[state->argc] = strdup (arg);
if (!argv[state->argc])
return -1;
state->argc++;
argv[state->argc] = NULL;
break;
}
return 0;
}
static char *
generate_uuid ()
{
char tmp_str[1024] = {0,};
char hostname[256] = {0,};
struct timeval tv = {0,};
struct tm now = {0, };
char now_str[32];
if (gettimeofday (&tv, NULL) == -1) {
gf_log ("glusterfsd", GF_LOG_ERROR,
"gettimeofday: failed %s",
strerror (errno));
}
if (gethostname (hostname, 256) == -1) {
gf_log ("glusterfsd", GF_LOG_ERROR,
"gethostname: failed %s",
strerror (errno));
}
localtime_r (&tv.tv_sec, &now);
strftime (now_str, 32, "%Y/%m/%d-%H:%M:%S", &now);
snprintf (tmp_str, 1024, "%s-%d-%s:%"
#ifdef GF_DARWIN_HOST_OS
PRId32,
#else
"ld",
#endif
hostname, getpid(), now_str, tv.tv_usec);
return gf_strdup (tmp_str);
}
static int
glusterfs_ctx_defaults_init (glusterfs_ctx_t *ctx)
{
cmd_args_t *cmd_args = NULL;
struct rlimit lim = {0, };
call_pool_t *pool = NULL;
xlator_mem_acct_init (THIS, cli_mt_end);
ctx->process_uuid = generate_uuid ();
if (!ctx->process_uuid)
return -1;
ctx->page_size = 128 * GF_UNIT_KB;
ctx->iobuf_pool = iobuf_pool_new (8 * GF_UNIT_MB, ctx->page_size);
if (!ctx->iobuf_pool)
return -1;
ctx->event_pool = event_pool_new (DEFAULT_EVENT_POOL_SIZE);
if (!ctx->event_pool)
return -1;
pool = GF_CALLOC (1, sizeof (call_pool_t),
cli_mt_call_pool_t);
if (!pool)
return -1;
INIT_LIST_HEAD (&pool->all_frames);
LOCK_INIT (&pool->lock);
ctx->pool = pool;
pthread_mutex_init (&(ctx->lock), NULL);
cmd_args = &ctx->cmd_args;
/* parsing command line arguments */
cmd_args->log_file = "/dev/stderr";
cmd_args->log_level = GF_LOG_NORMAL;
INIT_LIST_HEAD (&cmd_args->xlator_options);
lim.rlim_cur = RLIM_INFINITY;
lim.rlim_max = RLIM_INFINITY;
setrlimit (RLIMIT_CORE, &lim);
return 0;
}
static int
logging_init (glusterfs_ctx_t *ctx)
{
cmd_args_t *cmd_args = NULL;
cmd_args = &ctx->cmd_args;
if (gf_log_init (cmd_args->log_file) == -1) {
fprintf (stderr,
"failed to open logfile %s. exiting\n",
cmd_args->log_file);
return -1;
}
gf_log_set_loglevel (cmd_args->log_level);
return 0;
}
int
cli_submit_request (void *req, call_frame_t *frame,
rpc_clnt_prog_t *prog,
int procnum, struct iobref *iobref,
cli_serialize_t sfunc, xlator_t *this,
fop_cbk_fn_t cbkfn)
{
int ret = -1;
int count = 0;
char start_ping = 0;
struct iovec iov = {0, };
struct iobuf *iobuf = NULL;
char new_iobref = 0;
GF_ASSERT (this);
iobuf = iobuf_get (this->ctx->iobuf_pool);
if (!iobuf) {
goto out;
};
if (!iobref) {
iobref = iobref_new ();
if (!iobref) {
goto out;
}
new_iobref = 1;
}
iobref_add (iobref, iobuf);
iov.iov_base = iobuf->ptr;
iov.iov_len = 128 * GF_UNIT_KB;
/* Create the xdr payload */
if (req && sfunc) {
ret = sfunc (iov, req);
if (ret == -1) {
goto out;
}
iov.iov_len = ret;
count = 1;
}
/* Send the msg */
ret = rpc_clnt_submit (global_rpc, prog, procnum, cbkfn,
&iov, count,
NULL, 0, iobref, frame);
if (ret == 0) {
pthread_mutex_lock (&global_rpc->conn.lock);
{
if (!global_rpc->conn.ping_started) {
start_ping = 1;
}
}
pthread_mutex_unlock (&global_rpc->conn.lock);
}
if (start_ping)
//client_start_ping ((void *) this);
ret = 0;
out:
return ret;
}
int
parse_cmdline (int argc, char *argv[], struct cli_state *state)
{
int ret = 0;
struct argp argp = { 0,};
argp.options = gf_options;
argp.parser = parse_opts;
argp.args_doc = argp_doc;
argp.doc = gf_doc;
ret = argp_parse (&argp, argc, argv, ARGP_IN_ORDER, NULL, state);
return ret;
}
int
cli_cmd_tree_init (struct cli_cmd_tree *tree)
{
struct cli_cmd_word *root = NULL;
int ret = 0;
root = &tree->root;
root->tree = tree;
return ret;
}
int
cli_state_init (struct cli_state *state)
{
struct cli_cmd_tree *tree = NULL;
int ret = 0;
tree = &state->tree;
tree->state = state;
ret = cli_cmd_tree_init (tree);
return ret;
}
int
cli_out (const char *fmt, ...)
{
struct cli_state *state = NULL;
va_list ap;
state = global_state;
va_start (ap, fmt);
#ifdef HAVE_READLINE
if (state->rl_enabled && !state->rl_processing)
return cli_rl_out(state, fmt, ap);
#endif
return vprintf (fmt, ap);
}
struct rpc_clnt *
cli_rpc_init (struct cli_state *state)
{
struct rpc_clnt *rpc = NULL;
struct rpc_clnt_config rpc_cfg = {0,};
dict_t *options = NULL;
int ret = -1;
int port = CLI_GLUSTERD_PORT;
cli_rpc_prog = &cli3_1_prog;
options = dict_new ();
if (!options)
goto out;
ret = dict_set_str (options, "remote-host", "localhost");
if (ret)
goto out;
if (state->remote_port)
port = state->remote_port;
rpc_cfg.remote_host = "localhost";
rpc_cfg.remote_port = port;
ret = dict_set_int32 (options, "remote-port", port);
if (ret)
goto out;
ret = dict_set_str (options, "transport.address-family", "inet");
if (ret)
goto out;
rpc = rpc_clnt_init (&rpc_cfg, options, THIS->ctx, THIS->name);
out:
return rpc;
}
struct cli_state *global_state;
int
main (int argc, char *argv[])
{
struct cli_state state = {0, };
int ret = -1;
glusterfs_ctx_t *ctx = NULL;
ret = glusterfs_globals_init ();
if (ret)
return ret;
ctx = glusterfs_ctx_get ();
if (!ctx)
return ENOMEM;
ret = glusterfs_ctx_defaults_init (ctx);
if (ret)
goto out;
ret = cli_state_init (&state);
if (ret)
goto out;
state.ctx = ctx;
global_state = &state;
ret = parse_cmdline (argc, argv, &state);
if (ret)
goto out;
ret = logging_init (ctx);
if (ret)
goto out;
ret = cli_cmds_register (&state);
if (ret)
goto out;
ret = cli_input_init (&state);
if (ret)
goto out;
global_rpc = cli_rpc_init (&state);
if (!global_rpc)
goto out;
ret = event_dispatch (ctx->event_pool);
out:
// glusterfs_ctx_destroy (ctx);
return ret;
}
|