Motr  M0
cmd_main.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2016-2020 Seagate Technology LLC and/or its Affiliates
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * For any questions about this software or licensing,
18  * please email opensource@seagate.com or cortx-questions@seagate.com.
19  *
20  */
21 
22 
23 
30 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_CLIENT
31 #include "lib/trace.h"
32 
33 #include <stdio.h> /* vprintf */
34 #include <stdlib.h> /* exit */
35 #include <stdarg.h> /* va_list */
36 #include "lib/errno.h"
37 #include "index.h"
38 #include "common.h"
39 #include "lib/getopts.h" /* M0_GETOPTS */
40 #include "lib/thread.h" /* LAMBDA */
41 #include "module/instance.h" /* m0 */
42 
43 struct c_subsystem {
44  const char *s_name;
45  int (*s_init)(struct params *par);
46  void (*s_fini)(void);
47  void (*s_usage)(void);
48  int (*s_execute)(int argc, char** argv);
49 };
50 
51 static struct c_subsystem subsystems[] = {
53 };
54 
55 enum {
57  HA,
61 };
62 
63 static struct m0 instance;
64 bool is_str;
67 
68 static int subsystem_id(char *name)
69 {
70  int i;
71 
72  for (i = 0; i < ARRAY_SIZE(subsystems); ++i) {
73  if (!strcmp(name, subsystems[i].s_name))
74  return i;
75  }
76  return M0_ERR(-EPROTONOSUPPORT);
77 }
78 
79 static void usage(void)
80 {
81  int i;
82 
84  "Client Command Line tool: m0kv\n"
85  "Usage: ./m0kv "
86  "-l local_addr -h ha_addr -p profile -f proc_fid "
87  "[-s] [subsystem] [subsystem commands]\n"
88  "\n"
89  "Use -? for more verbose help on common arguments.\n"
90  "Usage example for common arguments: \n"
91  "./m0kv -l 10.0.2.15@tcp:12345:33:100 "
92  "-h 10.0.2.15@tcp:12345:34:1 "
93  "-p '<0x7000000000000001:0>' -f '<0x7200000000000000:0>'"
94  " [subsystem] [subsystem commands]\n"
95  "\n"
96  "-s Enable string format and it is optional.\n"
97  "-e Enable M0_ENF_META flag and it is optional.\n"
98  "-v 7600000000000001:30 it is mandatory with -e for "
99  "PUT, GET, DEL and NEXT operations.\n"
100  "Available subsystems and subsystem-specific commands are "
101  "listed below.\n");
102  for (i = 0; i < ARRAY_SIZE(subsystems); i++)
103  subsystems[i].s_usage();
104 }
105 
106 static int opts_get(struct params *par, int *argc, char ***argv)
107 {
108  int rc = 0;
109  char **arg = *(argv);
110  int common_args = 9;
111  char *pv = NULL;
112 
113  par->cp_local_addr = NULL;
114  par->cp_ha_addr = NULL;
115  par->cp_prof = NULL;
116  par->cp_proc_fid = NULL;
117 
118  rc = M0_GETOPTS("m0kv", *argc, *argv,
119  M0_HELPARG('?'),
120  M0_VOIDARG('i', "more verbose help",
121  LAMBDA(void, (void) {
122  usage();
123  exit(0);
124  })),
125  M0_STRINGARG('l', "Local endpoint address",
126  LAMBDA(void, (const char *string) {
127  par->cp_local_addr = (char*)string;
128  })),
129  M0_STRINGARG('h', "HA address",
130  LAMBDA(void, (const char *str) {
131  par->cp_ha_addr = (char*)str;
132  })),
133  M0_STRINGARG('f', "Process FID",
134  LAMBDA(void, (const char *str) {
135  par->cp_proc_fid = (char*)str;
136  })),
137  M0_STRINGARG('p', "Profile options for Client",
138  LAMBDA(void, (const char *str) {
139  par->cp_prof = (char*)str;
140  })),
141  M0_FLAGARG('s', "Enable string format",
142  &is_str),
143  M0_FLAGARG('e', "Enable M0_ENF_META flag",
144  &is_enf_meta),
145  M0_STRINGARG('v', "DIX pool version information",
146  LAMBDA(void, (const char *str) {
147  pv = (char *)str;
148  })));
149  if (rc != 0)
150  return M0_ERR(rc);
151  /* All mandatory params must be defined. */
152  if (rc == 0 &&
153  (par->cp_local_addr == NULL || par->cp_ha_addr == NULL ||
154  par->cp_prof == NULL || par->cp_proc_fid == NULL)) {
155  usage();
156  rc = M0_ERR(-EINVAL);
157  }
158 
159  if (is_str)
160  common_args++;
161 
162  if (is_enf_meta)
163  common_args++;
164 
165  if (pv != NULL) {
166  common_args += 2;
168  }
169 
170  *argc -= common_args;
171  *(argv) = arg + common_args;
172  return rc;
173 }
174 
175 int main(int argc, char **argv)
176 {
177  int rc;
178  int subid;
179  struct params params;
180 
183  if (rc != 0) {
184  m0_console_printf("Cannot init module %i\n", rc);
185  return M0_ERR(rc);
186  }
187  rc = opts_get(&params, &argc, &argv);
188 
189  if (rc == 0)
190  rc = subsystem_id(argv[0]);
191  if (rc == 0) {
192  subid = rc;
193  rc = subsystems[subid].s_init(&params);
194  if (rc == 0) {
195  rc = subsystems[subid].s_execute(argc - 1, argv + 1);
196  if (rc != 0)
197  m0_console_printf("Execution result %i\n", rc);
198  else
199  rc = 0;
200  subsystems[subid].s_fini();
201  }
202  else
203  m0_console_printf("Initialization error %i\n", rc);
204  }
205  if (rc < 0) {
206  m0_console_printf("Got error %i\n", rc);
207  /* main() should return positive values. */
208  rc = -rc;
209  }
210  m0_console_printf("Done, rc: %i\n", rc);
211  return rc;
212 }
213 
214 #undef M0_TRACE_SUBSYSTEM
215 
218 /*
219  * Local variables:
220  * c-indentation-style: "K&R"
221  * c-basic-offset: 8
222  * tab-width: 8
223  * fill-column: 80
224  * scroll-step: 1
225  * End:
226  */
227 /*
228  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
229  */
void index_usage(void)
Definition: index.c:336
#define M0_GETOPTS(progname, argc, argv,...)
Definition: getopts.h:169
void(* s_fini)(void)
Definition: cmd_main.c:46
#define M0_FLAGARG(ch, desc, ptr)
Definition: getopts.h:232
Definition: cmd_main.c:59
#define NULL
Definition: misc.h:38
char * cp_local_addr
Definition: common.h:35
struct m0_pool_version * pv
Definition: dir.c:629
void m0_console_printf(const char *fmt,...)
Definition: trace.c:801
const char * s_name
Definition: cmd_main.c:44
M0_INTERNAL void m0_instance_setup(struct m0 *instance)
Definition: instance.c:110
static int opts_get(struct params *par, int *argc, char ***argv)
Definition: cmd_main.c:106
#define M0_VOIDARG(ch, desc, func)
Definition: getopts.h:177
static struct c_subsystem subsystems[]
Definition: cmd_main.c:51
char * cp_ha_addr
Definition: common.h:36
#define M0_STRINGARG(ch, desc, func)
Definition: getopts.h:207
int i
Definition: dir.c:1033
return M0_ERR(-EOPNOTSUPP)
#define LAMBDA(T,...)
Definition: thread.h:153
const char * name
Definition: trace.c:110
int main(int argc, char **argv)
Definition: cmd_main.c:175
bool is_enf_meta
Definition: cmd_main.c:65
bool is_str
Definition: cmd_main.c:64
Definition: cmd_main.c:60
Definition: cmd_main.c:57
Definition: instance.h:80
void index_fini(void)
Definition: index.c:331
void(* s_usage)(void)
Definition: cmd_main.c:47
M0_INTERNAL int m0_fid_sscanf(const char *s, struct m0_fid *fid)
Definition: fid.c:227
static int subsystem_id(char *name)
Definition: cmd_main.c:68
int(* s_execute)(int argc, char **argv)
Definition: cmd_main.c:48
Definition: fid.h:38
static void usage(void)
Definition: cmd_main.c:79
#define M0_HELPARG(ch)
Definition: getopts.h:242
Definition: common.h:34
char * cp_proc_fid
Definition: common.h:39
static struct m0 instance
Definition: cmd_main.c:63
int index_init(struct params *params)
Definition: index.c:316
Definition: cmd_main.c:58
struct m0_module i_self
Definition: instance.h:88
int32_t rc
Definition: trigger_fop.h:47
#define ARRAY_SIZE(a)
Definition: misc.h:45
struct m0_fid dix_pool_ver
Definition: cmd_main.c:66
char * cp_prof
Definition: common.h:38
int(* s_init)(struct params *par)
Definition: cmd_main.c:45
Definition: cmd_main.c:56
M0_INTERNAL int m0_module_init(struct m0_module *module, int level)
Definition: module.c:131
int index_execute(int argc, char **argv)
Definition: index.c:300