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;
65 
66 static int subsystem_id(char *name)
67 {
68  int i;
69 
70  for (i = 0; i < ARRAY_SIZE(subsystems); ++i) {
71  if (!strcmp(name, subsystems[i].s_name))
72  return i;
73  }
74  return M0_ERR(-EPROTONOSUPPORT);
75 }
76 
77 static void usage(void)
78 {
79  int i;
80 
82  "Client Command Line tool: m0kv\n"
83  "Usage: ./m0kv "
84  "-l local_addr -h ha_addr -p profile -f proc_fid "
85  "[-s] [subsystem] [subsystem commands]\n"
86  "\n"
87  "Use -? for more verbose help on common arguments.\n"
88  "Usage example for common arguments: \n"
89  "./m0kv -l 10.0.2.15@tcp:12345:33:100 "
90  "-h 10.0.2.15@tcp:12345:34:1 "
91  "-p '<0x7000000000000001:0>' -f '<0x7200000000000000:0>'"
92  " [subsystem] [subsystem commands]\n"
93  "\n"
94  "-s Enable string format and it is optional.\n"
95  "Available subsystems and subsystem-specific commands are "
96  "listed below.\n");
97  for (i = 0; i < ARRAY_SIZE(subsystems); i++)
98  subsystems[i].s_usage();
99 }
100 
101 static int opts_get(struct params *par, int *argc, char ***argv)
102 {
103  int rc = 0;
104  char **arg = *(argv);
105  int common_args = 9;
106 
107  par->cp_local_addr = NULL;
108  par->cp_ha_addr = NULL;
109  par->cp_prof = NULL;
110  par->cp_proc_fid = NULL;
111 
112  rc = M0_GETOPTS("m0kv", *argc, *argv,
113  M0_HELPARG('?'),
114  M0_VOIDARG('i', "more verbose help",
115  LAMBDA(void, (void) {
116  usage();
117  exit(0);
118  })),
119  M0_STRINGARG('l', "Local endpoint address",
120  LAMBDA(void, (const char *string) {
121  par->cp_local_addr = (char*)string;
122  })),
123  M0_STRINGARG('h', "HA address",
124  LAMBDA(void, (const char *str) {
125  par->cp_ha_addr = (char*)str;
126  })),
127  M0_STRINGARG('f', "Process FID",
128  LAMBDA(void, (const char *str) {
129  par->cp_proc_fid = (char*)str;
130  })),
131  M0_STRINGARG('p', "Profile options for Client",
132  LAMBDA(void, (const char *str) {
133  par->cp_prof = (char*)str;
134  })),
135  M0_FLAGARG('s', "Enable string format",
136  &is_str));
137  if (rc != 0)
138  return M0_ERR(rc);
139  /* All mandatory params must be defined. */
140  if (rc == 0 &&
141  (par->cp_local_addr == NULL || par->cp_ha_addr == NULL ||
142  par->cp_prof == NULL || par->cp_proc_fid == NULL)) {
143  usage();
144  rc = M0_ERR(-EINVAL);
145  }
146 
147  if (is_str)
148  common_args++;
149 
150  *argc -= common_args;
151  *(argv) = arg + common_args;
152  return rc;
153 }
154 
155 int main(int argc, char **argv)
156 {
157  int rc;
158  int subid;
159  struct params params;
160 
163  if (rc != 0) {
164  m0_console_printf("Cannot init module %i\n", rc);
165  return M0_ERR(rc);
166  }
167  rc = opts_get(&params, &argc, &argv);
168 
169  if (rc == 0)
170  rc = subsystem_id(argv[0]);
171  if (rc == 0) {
172  subid = rc;
173  rc = subsystems[subid].s_init(&params);
174  if (rc == 0) {
175  rc = subsystems[subid].s_execute(argc - 1, argv + 1);
176  if (rc != 0)
177  m0_console_printf("Execution result %i\n", rc);
178  else
179  rc = 0;
180  subsystems[subid].s_fini();
181  }
182  else
183  m0_console_printf("Initialization error %i\n", rc);
184  }
185  if (rc < 0) {
186  m0_console_printf("Got error %i\n", rc);
187  /* main() should return positive values. */
188  rc = -rc;
189  }
190  m0_console_printf("Done, rc: %i\n", rc);
191  return rc;
192 }
193 
194 #undef M0_TRACE_SUBSYSTEM
195 
198 /*
199  * Local variables:
200  * c-indentation-style: "K&R"
201  * c-basic-offset: 8
202  * tab-width: 8
203  * fill-column: 80
204  * scroll-step: 1
205  * End:
206  */
207 /*
208  * vim: tabstop=8 shiftwidth=8 noexpandtab textwidth=80 nowrap
209  */
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
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:101
#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:155
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
static int subsystem_id(char *name)
Definition: cmd_main.c:66
int(* s_execute)(int argc, char **argv)
Definition: cmd_main.c:48
static void usage(void)
Definition: cmd_main.c:77
#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
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