We prepare a canned JSON response, for top level to write as reply * immediately before we exit. */
| 167 | /* We prepare a canned JSON response, for top level to write as reply |
| 168 | * immediately before we exit. */ |
| 169 | static struct command_result *prepare_stop_conn(struct command *cmd, |
| 170 | const char *why) |
| 171 | { |
| 172 | struct json_out *jout; |
| 173 | const char *p; |
| 174 | size_t len; |
| 175 | |
| 176 | /* With rpc_command_hook, jcon might have closed in the meantime! */ |
| 177 | if (!cmd->jcon) { |
| 178 | /* Return us to toplevel lightningd.c */ |
| 179 | log_debug(cmd->ld->log, "io_break: %s", __func__); |
| 180 | io_break(cmd->ld); |
| 181 | return command_still_pending(cmd); |
| 182 | } |
| 183 | |
| 184 | cmd->ld->stop_conn = cmd->jcon->conn; |
| 185 | |
| 186 | jout = json_out_new(tmpctx); |
| 187 | json_out_start(jout, NULL, '{'); |
| 188 | json_out_addstr(jout, "jsonrpc", "2.0"); |
| 189 | /* Copy input id token exactly */ |
| 190 | memcpy(json_out_member_direct(jout, "id", strlen(cmd->id)), |
| 191 | cmd->id, strlen(cmd->id)); |
| 192 | json_out_start(jout, "result", '{'); |
| 193 | json_out_addstr(jout, "result", why); |
| 194 | json_out_end(jout, '}'); |
| 195 | json_out_end(jout, '}'); |
| 196 | json_out_finished(jout); |
| 197 | |
| 198 | /* Add two \n */ |
| 199 | memcpy(json_out_direct(jout, 2), "\n\n", strlen("\n\n")); |
| 200 | p = json_out_contents(jout, &len); |
| 201 | cmd->ld->stop_response = tal_strndup(cmd->ld, p, len); |
| 202 | |
| 203 | /* Wake write loop in case it's not already. */ |
| 204 | io_wake(cmd->jcon); |
| 205 | |
| 206 | return command_still_pending(cmd); |
| 207 | } |
| 208 | |
| 209 | static struct command_result *json_stop(struct command *cmd, |
| 210 | const char *buffer, |
no test coverage detected