Motr  M0
ucookie.c
Go to the documentation of this file.
1 /* -*- C -*- */
2 /*
3  * Copyright (c) 2012-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 #include <setjmp.h> /* setjmp() and longjmp() */
24 
25 #include "lib/thread.h"
26 #include "lib/misc.h" /* M0_SET0 */
27 #include "lib/errno.h" /* errno */
28 #include "lib/assert.h" /* m0_panic */
29 
35 static const struct m0_panic_ctx signal_panic = {
36  .pc_expr = "fatal signal delivered",
37  .pc_func = "unknown",
38  .pc_file = "unknown",
39  .pc_lineno = 0,
40  .pc_fmt = "signo: %i"
41 };
42 
46 static void sigsegv(int sig)
47 {
48  struct m0_thread_tls *tls = m0_thread_tls();
49  jmp_buf *buf = tls == NULL ? NULL : tls->tls_arch.tat_jmp;
50 
51  if (buf == NULL)
52  m0_panic(&signal_panic, sig);
53  else
54  longjmp(*buf, 1);
55 }
56 
62 M0_INTERNAL bool m0_arch_addr_is_sane(const void *addr)
63 {
64  jmp_buf buf;
65  jmp_buf **tls = &m0_thread_tls()->tls_arch.tat_jmp;
66  int ret;
67  volatile uint64_t dummy M0_UNUSED;
68  volatile bool result = false;
69 
70  *tls = &buf;
71  ret = setjmp(buf);
72  if (ret == 0) {
73  dummy = *(uint64_t *)addr;
74  result = true;
75  }
76  *tls = NULL;
77 
78  return result;
79 }
80 
82 M0_INTERNAL int m0_arch_cookie_global_init(void)
83 {
84  int ret;
85  struct sigaction sa_sigsegv = {
86  .sa_handler = sigsegv,
87  .sa_flags = SA_NODEFER
88  };
89 
90  ret = sigemptyset(&sa_sigsegv.sa_mask) ?:
91  sigaction(SIGSEGV, &sa_sigsegv, NULL);
92  if (ret != 0) {
93  M0_ASSERT(ret == -1);
94  return -errno;
95  }
96  return 0;
97 }
98 
100 M0_INTERNAL void m0_arch_cookie_global_fini(void)
101 {
102  int ret;
103  struct sigaction sa_sigsegv = { .sa_handler = SIG_DFL };
104 
105  ret = sigemptyset(&sa_sigsegv.sa_mask) ?:
106  sigaction(SIGSEGV, &sa_sigsegv, NULL);
107  M0_ASSERT(ret == 0);
108 }
109 
112 /*
113  * Local variables:
114  * c-indentation-style: "K&R"
115  * c-basic-offset: 8
116  * tab-width: 8
117  * fill-column: 80
118  * scroll-step: 1
119  * End:
120  */
static struct m0_be_active_record_domain dummy
Definition: active_record.c:35
#define NULL
Definition: misc.h:38
void m0_panic(const struct m0_panic_ctx *ctx,...)
Definition: assert.c:40
static int void * buf
Definition: dir.c:1019
jmp_buf * tat_jmp
Definition: thread.h:57
Definition: sock.c:887
M0_INTERNAL struct m0_thread_tls * m0_thread_tls(void)
Definition: kthread.c:67
struct m0_thread_arch_tls tls_arch
Definition: thread.h:67
#define M0_ASSERT(cond)
Definition: xcode.h:73
const char * pc_expr
Definition: assert.h:85
static __thread struct m0_thread_tls * tls
Definition: uthread.c:66
#define M0_UNUSED
Definition: misc.h:380