Motr  M0
m0d.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2013-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 #define M0_TRACE_SUBSYSTEM M0_TRACE_SUBSYS_M0D
24 #include "lib/trace.h"
25 
26 #include <stdio.h> /* printf */
27 #include <unistd.h> /* pause */
28 #include <stdlib.h> /* atoi */
29 #include <err.h> /* warnx */
30 #include <signal.h> /* sigaction */
31 #include <sys/time.h>
32 #include <sys/resource.h>
33 
34 #ifdef HAVE_SYSTEMD
35 #include <systemd/sd-daemon.h>
36 #endif
37 
38 #include "lib/errno.h"
39 #include "lib/memory.h"
40 #include "lib/misc.h" /* M0_SET0 */
41 #include "lib/user_space/trace.h" /* m0_trace_set_buffer_size */
42 
43 #include "motr/setup.h"
44 #include "motr/init.h"
45 #include "motr/version.h"
46 #include "module/instance.h" /* m0 */
47 #include "reqh/reqh_service.h"
48 #include "motr/process_attr.h"
49 #include "ha/note.h" /* M0_NC_ONLINE */
50 
57 /* Signal handler result */
59 {
60  /* Default value */
62  /* Stop work Motr instance */
64  /* Restart Motr instance */
66 };
67 
68 extern volatile sig_atomic_t gotsignal;
69 static bool regsignal = false;
70 
75 static void cs_term_sig_handler(int signum)
76 {
77  gotsignal = signum == SIGUSR1 ? M0_RESULT_STATUS_RESTART :
79 }
80 
87 static int cs_register_signal(void)
88 {
89  struct sigaction term_act;
90  int rc;
91 
92  regsignal = false;
94  term_act.sa_handler = cs_term_sig_handler;
95  sigemptyset(&term_act.sa_mask);
96  term_act.sa_flags = 0;
97 
98  rc = sigaction(SIGTERM, &term_act, NULL) ?:
99  sigaction(SIGINT, &term_act, NULL) ?:
100  sigaction(SIGQUIT, &term_act, NULL) ?:
101  sigaction(SIGUSR1, &term_act, NULL);
102  if (rc == 0)
103  regsignal = true;
104  return rc;
105 }
106 
107 static int cs_wait_signal(void)
108 {
109  M0_PRE(regsignal);
110  m0_console_printf("Press CTRL+C to quit.\n");
112  do {
113  pause();
114  } while (!gotsignal);
115 
116  return gotsignal;
117 }
118 
119 M0_INTERNAL int main(int argc, char **argv)
120 {
121  static struct m0 instance;
122  int trace_buf_size;
123  int result;
124  int rc;
125  struct m0_motr motr_ctx;
126  struct rlimit rlim = {10240, 10240};
127 
128  if (argc > 1 &&
129  (strcmp(argv[1], "-v") == 0 || strcmp(argv[1], "--version") == 0)) {
131  exit(EXIT_SUCCESS);
132  }
133 
134  if (argc > 2 && strcmp(argv[1], "--trace-buf-size-mb") == 0) {
135  trace_buf_size = atoi(argv[2]); /* in MiB */
136  if (trace_buf_size > 0 &&
138  1024 * 1024) == 0)
139  {
140  argv[2] = argv[0];
141  argv += 2;
142  argc -= 2;
143  } else {
144  if (trace_buf_size <= 0)
145  m0_error_printf("motr: trace buffer size should"
146  " be greater than zero "
147  "(was %i)\n", trace_buf_size);
148  exit(EXIT_FAILURE);
149  }
150  }
151 
152  rc = setrlimit(RLIMIT_NOFILE, &rlim);
153  if (rc != 0) {
154  warnx("\n Failed to setrlimit\n");
155  goto out;
156  }
157 
159  if (rc != 0) {
160  warnx("\n Failed to register signals\n");
161  goto out;
162  }
163 
164 init_m0d:
166 
167  errno = 0;
168  M0_SET0(&instance);
169  rc = m0_init(&instance);
170  if (rc != 0) {
171  warnx("\n Failed to initialise Motr \n");
172  goto out;
173  }
174 
175 start_m0d:
176  M0_SET0(&motr_ctx);
178  stderr, false);
179  if (rc != 0) {
180  warnx("\n Failed to initialise Motr \n");
181  goto cleanup2;
182  }
183 
184  /*
185  * Prevent from automatic process fid generation by setting the context
186  * up with a dummy fid of non-process type.
187  */
188  motr_ctx.cc_reqh_ctx.rc_fid = M0_FID_INIT(0, 1);
189  /*
190  * Process FID specification is mandatory for m0d. Motr instance setup
191  * is going to stumble upon fid type precondition in m0_reqh_init()
192  * unless real process fid is present in argv.
193  */
194  rc = m0_cs_setup_env(&motr_ctx, argc, argv);
195  if (rc != 0)
196  goto cleanup1;
197 
198  rc = m0_cs_start(&motr_ctx);
199  if (rc == 0) {
200  /* For st/m0d-signal-test.sh */
201  m0_console_printf("Started\n");
203 
204 #ifdef HAVE_SYSTEMD
205  /*
206  * From the systemd's point of view, service can be considered as
207  * started when it can handle incoming connections, which is true
208  * after m0_cs_start() is called.
209  */
210  rc = sd_notify(0, "READY=1");
211  if (rc < 0)
212  warnx("systemd READY notification failed, rc=%d\n", rc);
213  else if (rc == 0)
214  warnx("systemd notifications not allowed\n");
215  else
216  warnx("systemd READY notification successful\n");
217  rc = 0;
218 #endif
219  result = cs_wait_signal();
220  if (gotsignal)
221  warnx("got signal %d", gotsignal);
222  }
223 
224  if (rc == 0 && result == M0_RESULT_STATUS_RESTART) {
225  /*
226  * Note! A very common cause of failure restart is
227  * non-finalize (non-clean) any subsystem
228  */
229  m0_cs_fini(&motr_ctx);
230 restart_signal:
231  m0_quiesce();
232 
234 
236  if (rc != 0) {
237  warnx("\n Failed to set process memory limits Motr \n");
238  goto out;
239  }
240 
241  rc = m0_resume(&instance);
242  if (rc != 0) {
243  warnx("\n Failed to reconfigure Motr \n");
244  goto out;
245  }
246 
247  /* Print to m0.log for ./sss/st system test*/
248  m0_console_printf("Restarting\n");
250 
251  goto start_m0d;
252  }
253 
254  if (rc == 0) {
255  /* Ignore cleanup labels signal handling for normal start. */
257  }
258 
259 cleanup1:
260  m0_cs_fini(&motr_ctx);
261  if (gotsignal) {
263  goto restart_signal;
265  }
266 cleanup2:
267  m0_fini();
269  goto init_m0d;
270 out:
271  errno = rc < 0 ? -rc : rc;
272  return errno;
273 }
274 
277 /*
278  * Local variables:
279  * c-indentation-style: "K&R"
280  * c-basic-offset: 8
281  * tab-width: 8
282  * fill-column: 80
283  * scroll-step: 1
284  * End:
285  */
#define M0_PRE(cond)
#define NULL
Definition: misc.h:38
void m0_fini(void)
Definition: init.c:318
void m0_error_printf(const char *fmt,...)
Definition: ktrace.c:169
void m0_console_printf(const char *fmt,...)
Definition: trace.c:801
#define M0_FID_INIT(container, key)
Definition: fid.h:84
int m0_cs_setup_env(struct m0_motr *cctx, int argc, char **argv)
Definition: setup.c:2972
int m0_resume(struct m0 *instance)
Definition: init.c:323
void m0_cs_fini(struct m0_motr *cctx)
Definition: setup.c:3029
volatile sig_atomic_t gotsignal
Definition: setup.c:2415
int m0_init(struct m0 *instance)
Definition: init.c:310
#define M0_SET0(obj)
Definition: misc.h:64
void m0_build_info_print(void)
Definition: version.c:66
static int cs_register_signal(void)
Definition: m0d.c:87
int m0_cs_memory_limits_setup(struct m0 *instance)
Definition: process_attr.c:54
int m0_net_xprt_nr(void)
Definition: net.c:168
Definition: instance.h:80
struct m0_fid rc_fid
Definition: setup.h:338
int m0_cs_init(struct m0_motr *cctx, struct m0_net_xprt **xprts, size_t xprts_nr, FILE *out, bool mkfs)
Definition: setup.c:2999
Definition: setup.h:354
void m0_console_flush(void)
Definition: ktrace.c:165
struct m0_reqh_context cc_reqh_ctx
Definition: setup.h:361
M0_INTERNAL int main(int argc, char **argv)
Definition: m0d.c:119
struct m0_net_xprt ** m0_net_all_xprt_get(void)
Definition: net.c:161
static unsigned long trace_buf_size
Definition: ktrace.c:65
int m0_trace_set_buffer_size(size_t size)
Definition: utrace.c:166
static struct m0 instance
Definition: main.c:78
int m0_cs_start(struct m0_motr *cctx)
Definition: setup.c:2987
static bool regsignal
Definition: m0d.c:69
#define out(...)
Definition: gen.c:41
static int cs_wait_signal(void)
Definition: m0d.c:107
int32_t rc
Definition: trigger_fop.h:47
void m0_quiesce(void)
Definition: init.c:328
result_status
Definition: m0d.c:58
static void cs_term_sig_handler(int signum)
Definition: m0d.c:75