]> git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/lib/event/rpc/app_rpc.c
import 15.2.0 Octopus source
[ceph.git] / ceph / src / spdk / lib / event / rpc / app_rpc.c
1 /*-
2 * BSD LICENSE
3 *
4 * Copyright (c) Intel Corporation.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include "spdk/stdinc.h"
35
36 #include "spdk/event.h"
37 #include "spdk/rpc.h"
38 #include "spdk/string.h"
39 #include "spdk/util.h"
40
41 #include "spdk_internal/log.h"
42
43 struct rpc_kill_instance {
44 char *sig_name;
45 };
46
47 static void
48 free_rpc_kill_instance(struct rpc_kill_instance *req)
49 {
50 free(req->sig_name);
51 }
52
53 static const struct spdk_json_object_decoder rpc_kill_instance_decoders[] = {
54 {"sig_name", offsetof(struct rpc_kill_instance, sig_name), spdk_json_decode_string},
55 };
56
57 static void
58 spdk_rpc_kill_instance(struct spdk_jsonrpc_request *request,
59 const struct spdk_json_val *params)
60 {
61 static const struct {
62 const char *signal_string;
63 int32_t signal;
64 } signals[] = {
65 {"SIGINT", SIGINT},
66 {"SIGTERM", SIGTERM},
67 {"SIGQUIT", SIGQUIT},
68 {"SIGHUP", SIGHUP},
69 {"SIGKILL", SIGKILL},
70 };
71 size_t i, sig_count;
72 int signal;
73 struct rpc_kill_instance req = {};
74 struct spdk_json_write_ctx *w;
75
76 if (spdk_json_decode_object(params, rpc_kill_instance_decoders,
77 SPDK_COUNTOF(rpc_kill_instance_decoders),
78 &req)) {
79 SPDK_DEBUGLOG(SPDK_LOG_REACTOR, "spdk_json_decode_object failed\n");
80 goto invalid;
81 }
82
83 sig_count = SPDK_COUNTOF(signals);
84 signal = spdk_strtol(req.sig_name, 10);
85 for (i = 0 ; i < sig_count; i++) {
86 if (strcmp(req.sig_name, signals[i].signal_string) == 0 ||
87 signal == signals[i].signal) {
88 break;
89 }
90 }
91
92 if (i == sig_count) {
93 goto invalid;
94 }
95
96 SPDK_DEBUGLOG(SPDK_LOG_REACTOR, "sending signal %d\n", signals[i].signal);
97 free_rpc_kill_instance(&req);
98 kill(getpid(), signals[i].signal);
99
100 w = spdk_jsonrpc_begin_result(request);
101 if (w == NULL) {
102 return;
103 }
104 spdk_json_write_bool(w, true);
105 spdk_jsonrpc_end_result(request, w);
106 return;
107
108 invalid:
109 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
110 free_rpc_kill_instance(&req);
111 }
112 SPDK_RPC_REGISTER("kill_instance", spdk_rpc_kill_instance, SPDK_RPC_RUNTIME)
113
114
115 struct rpc_context_switch_monitor {
116 bool enabled;
117 };
118
119 static const struct spdk_json_object_decoder rpc_context_switch_monitor_decoders[] = {
120 {"enabled", offsetof(struct rpc_context_switch_monitor, enabled), spdk_json_decode_bool},
121 };
122
123 static void
124 spdk_rpc_context_switch_monitor(struct spdk_jsonrpc_request *request,
125 const struct spdk_json_val *params)
126 {
127 struct rpc_context_switch_monitor req = {};
128 struct spdk_json_write_ctx *w;
129
130 if (params != NULL) {
131 if (spdk_json_decode_object(params, rpc_context_switch_monitor_decoders,
132 SPDK_COUNTOF(rpc_context_switch_monitor_decoders),
133 &req)) {
134 SPDK_DEBUGLOG(SPDK_LOG_REACTOR, "spdk_json_decode_object failed\n");
135 spdk_jsonrpc_send_error_response(request, SPDK_JSONRPC_ERROR_INVALID_PARAMS, "Invalid parameters");
136 return;
137 }
138
139 spdk_reactor_enable_context_switch_monitor(req.enabled);
140 }
141
142 w = spdk_jsonrpc_begin_result(request);
143 if (w == NULL) {
144 return;
145 }
146
147 spdk_json_write_object_begin(w);
148
149 spdk_json_write_named_bool(w, "enabled", spdk_reactor_context_switch_monitor_enabled());
150
151 spdk_json_write_object_end(w);
152 spdk_jsonrpc_end_result(request, w);
153 }
154
155 SPDK_RPC_REGISTER("context_switch_monitor", spdk_rpc_context_switch_monitor, SPDK_RPC_RUNTIME)