Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / test / TestSignalHandlers.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4  * Ceph - scalable distributed file system
5  *
6  * Copyright (C) 2010 Dreamhost
7  *
8  * This is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License version 2.1, as published by the Free Software
11  * Foundation.  See file COPYING.
12  *
13  */
14
15 /*
16  * TestSignalHandlers
17  *
18  * Test the Ceph signal handlers
19  */
20 #include "common/ceph_argparse.h"
21 #include "global/global_init.h"
22 #include "common/errno.h"
23 #include "common/debug.h"
24 #include "common/config.h"
25
26 #include <errno.h>
27 #include <iostream>
28 #include <sstream>
29 #include <string>
30
31 #define dout_context g_ceph_context
32 using std::string;
33
34 // avoid compiler warning about dereferencing NULL pointer
35 static int* get_null()
36 {
37   return 0;
38 }
39
40 static void simple_segv_test()
41 {
42   generic_dout(-1) << "triggering SIGSEGV..." << dendl;
43   // cppcheck-suppress nullPointer
44   int i = *get_null();
45   std::cout << "i = " << i << std::endl;
46 }
47
48 // Given the name of the function, we can be pretty sure this is intentional.
49
50 #pragma clang diagnostic push
51
52 #pragma clang diagnostic ignored "-Winfinite-recursion"
53
54 static void infinite_recursion_test_impl()
55 {
56   infinite_recursion_test_impl();
57 }
58
59 #pragma clang diagnostic pop
60
61 static void infinite_recursion_test()
62 {
63   generic_dout(0) << "triggering SIGSEGV with infinite recursion..." << dendl;
64   infinite_recursion_test_impl();
65 }
66
67 static void usage()
68 {
69   cerr << "usage: TestSignalHandlers [test]" << std::endl;
70   cerr << "--simple_segv: run simple_segv test" << std::endl;
71   cerr << "--infinite_recursion: run infinite_recursion test" << std::endl;
72   generic_client_usage(); // Will exit()
73 }
74
75 typedef void (*test_fn_t)(void);
76
77 int main(int argc, const char **argv)
78 {
79   vector<const char*> args;
80   argv_to_vec(argc, argv, args);
81   env_to_vec(args);
82
83   auto cct = global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT,
84                          CODE_ENVIRONMENT_UTILITY, 0);
85   common_init_finish(g_ceph_context);
86
87   test_fn_t fn = NULL;
88   for (std::vector<const char*>::iterator i = args.begin(); i != args.end(); ) {
89     if (ceph_argparse_double_dash(args, i)) {
90       break;
91     } else if (ceph_argparse_flag(args, i, "-h", "--help", (char*)NULL)) {
92       usage();
93     } else if (ceph_argparse_flag(args, i, "--infinite_recursion", (char*)NULL)) {
94       fn = infinite_recursion_test;
95     } else if (ceph_argparse_flag(args, i, "-s", "--simple_segv", (char*)NULL)) {
96       fn = simple_segv_test;
97     } else {
98       cerr << "Garbage at end of command line." << std::endl;
99       usage();
100     }
101   }
102   if (!fn) {
103     std::cerr << "Please select a test to run. Type -h for help." << std::endl;
104     usage();
105   }
106   fn();
107   return 0;
108 }