342157d5b4a080b842326e036d47157d6055e1d4
[sfrench/samba-autobuild/.git] / source4 / lib / messaging / messaging_handlers.c
1 /*
2    Unix SMB/CIFS implementation.
3
4    Handers for non core Samba internal messages
5
6    Handlers for messages that are only included in developer and self test
7    builds.
8
9    Copyright (C) Andrew Bartlett <abartlet@samba.org> 2018
10
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 3 of the License, or
14    (at your option) any later version.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 */
24
25 #include "includes.h"
26 #include "lib/util/server_id.h"
27 #include "messaging/messaging.h"
28 #include "messaging/messaging_internal.h"
29
30 #if defined(DEVELOPER) || defined(ENABLE_SELFTEST)
31
32 /*
33  * Inject a fault into the currently running process
34  */
35 static void do_inject_fault(struct imessaging_context *msg,
36                             void *private_data,
37                             uint32_t msg_type,
38                             struct server_id src,
39                             DATA_BLOB *data)
40 {
41         int sig;
42         struct server_id_buf tmp;
43
44         if (data->length != sizeof(sig)) {
45                 DBG_ERR("Process %s sent bogus signal injection request\n",
46                         server_id_str_buf(src, &tmp));
47                 return;
48         }
49
50         sig = *(int *)data->data;
51         if (sig == -1) {
52                 DBG_ERR("Process %s requested an iternal failure, "
53                         "calling exit(1)\n",
54                         server_id_str_buf(src, &tmp));
55                 exit(1);
56         }
57
58 #if HAVE_STRSIGNAL
59         DBG_ERR("Process %s requested injection of signal %d (%s)\n",
60                 server_id_str_buf(src, &tmp),
61                 sig,
62                 strsignal(sig));
63 #else
64         DBG_ERR("Process %s requested injection of signal %d\n",
65                 server_id_str_buf(src, &tmp),
66                 sig);
67 #endif
68
69         kill(getpid(), sig);
70 }
71
72 /*
73  * Cause the current process to sleep for a specified number of seconds
74  */
75 static void do_sleep(struct imessaging_context *msg,
76                      void *private_data,
77                      uint32_t msg_type,
78                      struct server_id src,
79                      DATA_BLOB *data)
80 {
81         unsigned int seconds;
82         struct server_id_buf tmp;
83
84         if (data->length != sizeof(seconds)) {
85                 DBG_ERR("Process %s sent bogus sleep request\n",
86                         server_id_str_buf(src, &tmp));
87                 return;
88         }
89
90         seconds = *(unsigned int *)data->data;
91         DBG_ERR("Process %s requested a sleep of %u seconds\n",
92                 server_id_str_buf(src, &tmp),
93                 seconds);
94         sleep(seconds);
95         DBG_ERR("Restarting after %u second sleep requested by process %s\n",
96                 seconds,
97                 server_id_str_buf(src, &tmp));
98 }
99
100 /*
101  * Register the extra messaging handlers
102  */
103 NTSTATUS imessaging_register_extra_handlers(struct imessaging_context *msg)
104 {
105         NTSTATUS status;
106
107         status = imessaging_register(
108             msg, NULL, MSG_SMB_INJECT_FAULT, do_inject_fault);
109         if (!NT_STATUS_IS_OK(status)) {
110                 return status;
111         }
112
113         status = imessaging_register(msg, NULL, MSG_SMB_SLEEP, do_sleep);
114         if (!NT_STATUS_IS_OK(status)) {
115                 return status;
116         }
117
118         return NT_STATUS_OK;
119 }
120
121 #endif /* defined(DEVELOPER) || defined(ENABLE_SELFTEST) */