Skip to content

Commit c0fc8cc

Browse files
committed
libplugin: insist on always having a non-NULL command context.
And remove command_done() which was used when there was no cmd. Signed-off-by: Rusty Russell <[email protected]>
1 parent d727804 commit c0fc8cc

File tree

5 files changed

+76
-57
lines changed

5 files changed

+76
-57
lines changed

plugins/autoclean.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -509,7 +509,7 @@ static struct command_result *list_done(struct command *cmd,
509509
}
510510

511511
subsystem->cinfo->cleanup_reqs_remaining++;
512-
req = jsonrpc_request_start(plugin, NULL, ops->del_command,
512+
req = jsonrpc_request_start(plugin, cmd, ops->del_command,
513513
del_done, del_failed, variant);
514514
ops->add_del_fields(req, buf, t);
515515
send_outreq(plugin, req);
@@ -572,7 +572,7 @@ static struct command_result *do_clean(struct clean_info *cinfo)
572572
}
573573

574574
if (cinfo->cleanup_reqs_remaining)
575-
return command_still_pending(NULL);
575+
return command_still_pending(cinfo->cmd);
576576
return clean_finished(cinfo);
577577
}
578578

@@ -628,7 +628,7 @@ static struct command_result *start_clean(struct clean_info *cinfo)
628628
}
629629
ps->offset = 0;
630630

631-
req = jsonrpc_request_start(plugin, NULL,
631+
req = jsonrpc_request_start(plugin, cinfo->cmd,
632632
"wait",
633633
wait_done, wait_failed, ps);
634634
json_add_string(req->js, "subsystem", ops->system_name);

plugins/libplugin.c

Lines changed: 7 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,6 @@ struct command_result *command_param_failed(void)
164164
return &complete;
165165
}
166166

167-
struct command_result *command_done(void)
168-
{
169-
return &complete;
170-
}
171-
172167
struct json_filter **command_filter_ptr(struct command *cmd)
173168
{
174169
return &cmd->filter;
@@ -295,7 +290,7 @@ static struct command_result *ignore_cb(struct command *command,
295290
const jsmntok_t *result,
296291
void *arg)
297292
{
298-
return command_done();
293+
return &complete;
299294
}
300295

301296
static void disable_request_cb(struct command *cmd, struct out_req *out)
@@ -308,9 +303,6 @@ static void disable_request_cb(struct command *cmd, struct out_req *out)
308303

309304
const char *json_id_prefix(const tal_t *ctx, const struct command *cmd)
310305
{
311-
if (!cmd)
312-
return "";
313-
314306
/* Strip quotes! */
315307
if (strstarts(cmd->id, "\"")) {
316308
assert(strlen(cmd->id) >= 2);
@@ -354,6 +346,7 @@ jsonrpc_request_start_(struct plugin *plugin, struct command *cmd,
354346
{
355347
struct out_req *out;
356348

349+
assert(cmd);
357350
out = tal(cmd, struct out_req);
358351
out->id = append_json_id(out, plugin, method, id_prefix);
359352
out->cmd = cmd;
@@ -364,8 +357,7 @@ jsonrpc_request_start_(struct plugin *plugin, struct command *cmd,
364357
tal_add_destructor2(out, destroy_out_req, plugin);
365358

366359
/* If command goes away, don't call callbacks! */
367-
if (out->cmd)
368-
tal_add_destructor2(out->cmd, disable_request_cb, out);
360+
tal_add_destructor2(out->cmd, disable_request_cb, out);
369361

370362
out->js = new_json_stream(NULL, cmd, NULL);
371363
json_object_start(out->js, NULL);
@@ -481,8 +473,7 @@ struct command_result *command_finished(struct command *cmd,
481473
struct command_result *WARN_UNUSED_RESULT
482474
command_still_pending(struct command *cmd)
483475
{
484-
if (cmd)
485-
notleak_with_children(cmd);
476+
notleak_with_children(cmd);
486477
return &pending;
487478
}
488479

@@ -1022,8 +1013,7 @@ static void handle_rpc_reply(struct plugin *plugin, const jsmntok_t *toks)
10221013
}
10231014

10241015
/* Remove destructor if one existed */
1025-
if (out->cmd)
1026-
tal_del_destructor2(out->cmd, disable_request_cb, out);
1016+
tal_del_destructor2(out->cmd, disable_request_cb, out);
10271017

10281018
/* We want to free this if callback doesn't. */
10291019
tal_steal(tmpctx, out);
@@ -1129,7 +1119,7 @@ static struct command_result *batch_one_success(struct command *cmd,
11291119
{
11301120
/* If this frees stuff (e.g. fails), just return */
11311121
if (batch->cb && batch->cb(cmd, buf, result, batch->arg) == &complete)
1132-
return command_done();
1122+
return &complete;
11331123
return batch_one_complete(cmd, batch);
11341124
}
11351125

@@ -1140,7 +1130,7 @@ static struct command_result *batch_one_failed(struct command *cmd,
11401130
{
11411131
/* If this frees stuff (e.g. fails), just return */
11421132
if (batch->errcb && batch->errcb(cmd, buf, result, batch->arg) == &complete)
1143-
return command_done();
1133+
return &complete;
11441134
return batch_one_complete(cmd, batch);
11451135
}
11461136

plugins/libplugin.h

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ struct out_req *jsonrpc_request_start_(struct plugin *plugin,
123123
const char *buf,
124124
const jsmntok_t *result,
125125
void *arg),
126-
void *arg);
126+
void *arg)
127+
NON_NULL_ARGS(1, 2, 3, 4, 6);
127128

128129
/* This variant has callbacks received whole obj, not "result" or
129130
* "error" members. */
@@ -202,30 +203,36 @@ struct request_batch *request_batch_new_(const tal_t *ctx,
202203

203204
struct out_req *add_to_batch(struct command *cmd,
204205
struct request_batch *batch,
205-
const char *cmdname);
206+
const char *cmdname)
207+
NON_NULL_ARGS(1, 2, 3);
206208

207209
/* We want some commands to live after this command (possibly)
208210
* completes. This creates a new command with the same id but its own
209211
* lifetime: use aux_command_done() or tal_free() when you're done. */
210-
struct command *aux_command(const struct command *cmd);
212+
struct command *aux_command(const struct command *cmd)
213+
NON_NULL_ARGS(1);
211214

212215
/* Runs finalcb immediately if batch is empty. */
213216
struct command_result *batch_done(struct command *cmd,
214-
struct request_batch *batch);
217+
struct request_batch *batch)
218+
NON_NULL_ARGS(1, 2);
215219

216220
/* Helper to create a JSONRPC2 response stream with a "result" object. */
217-
struct json_stream *jsonrpc_stream_success(struct command *cmd);
221+
struct json_stream *jsonrpc_stream_success(struct command *cmd)
222+
NON_NULL_ARGS(1);
218223

219224
/* Helper to create a JSONRPC2 response stream with an "error" object. */
220225
struct json_stream *jsonrpc_stream_fail(struct command *cmd,
221226
int code,
222-
const char *err);
227+
const char *err)
228+
NON_NULL_ARGS(1, 3);
223229

224230
/* Helper to create a JSONRPC2 response stream with an "error" object,
225231
* to which will be added a "data" object. */
226232
struct json_stream *jsonrpc_stream_fail_data(struct command *cmd,
227233
int code,
228-
const char *err);
234+
const char *err)
235+
NON_NULL_ARGS(1, 3);
229236

230237
/* Helper to jsonrpc_request_start() and send_outreq() to update datastore.
231238
* NULL cb means ignore, NULL errcb means plugin_error.
@@ -244,7 +251,8 @@ struct command_result *jsonrpc_set_datastore_(struct plugin *plugin,
244251
const char *buf,
245252
const jsmntok_t *result,
246253
void *arg),
247-
void *arg);
254+
void *arg)
255+
NON_NULL_ARGS(1, 2, 3, 4, 6);
248256

249257
#define jsonrpc_set_datastore_string(plugin, cmd, path, str, mode, cb, errcb, arg) \
250258
jsonrpc_set_datastore_((plugin), (cmd), (path), (str), true, (mode), \
@@ -286,7 +294,8 @@ struct command_result *jsonrpc_get_datastore_(struct plugin *plugin,
286294
struct command_result *(*binary_cb)(struct command *command,
287295
const u8 *val,
288296
void *arg),
289-
void *arg);
297+
void *arg)
298+
NON_NULL_ARGS(1, 2, 3);
290299

291300
#define jsonrpc_get_datastore_string(plugin, cmd, path, cb, arg) \
292301
jsonrpc_get_datastore_((plugin), (cmd), (path), \
@@ -312,11 +321,13 @@ struct command_result *jsonrpc_get_datastore_(struct plugin *plugin,
312321
/* This command is finished, here's the response (the content of the
313322
* "result" or "error" field) */
314323
WARN_UNUSED_RESULT
315-
struct command_result *command_finished(struct command *cmd, struct json_stream *response);
324+
struct command_result *command_finished(struct command *cmd, struct json_stream *response)
325+
NON_NULL_ARGS(1, 2);
316326

317327
/* Helper for a command that'll be finished in a callback. */
318328
WARN_UNUSED_RESULT
319-
struct command_result *command_still_pending(struct command *cmd);
329+
struct command_result *command_still_pending(struct command *cmd)
330+
NON_NULL_ARGS(1);
320331

321332
/* Helper to create a zero or single-value JSON object; if @str is NULL,
322333
* object is empty. */
@@ -355,36 +366,43 @@ struct command_result *WARN_UNUSED_RESULT
355366
command_done_err(struct command *cmd,
356367
enum jsonrpc_errcode code,
357368
const char *errmsg,
358-
const struct json_out *data);
369+
const struct json_out *data)
370+
NON_NULL_ARGS(1, 3);
359371

360372
/* Send a raw error response. Useful for forwarding a previous
361373
* error after cleanup */
362374
struct command_result *command_err_raw(struct command *cmd,
363-
const char *json_str);
375+
const char *json_str)
376+
NON_NULL_ARGS(1, 2);
364377

365378
/* This command is finished, here's the result object; @cmd cannot be NULL. */
366379
struct command_result *WARN_UNUSED_RESULT
367-
command_success(struct command *cmd, const struct json_out *result);
380+
command_success(struct command *cmd, const struct json_out *result)
381+
NON_NULL_ARGS(1, 2);
368382

369383
/* End a hook normally (with "result": "continue") */
370384
struct command_result *WARN_UNUSED_RESULT
371-
command_hook_success(struct command *cmd);
385+
command_hook_success(struct command *cmd)
386+
NON_NULL_ARGS(1);
372387

373388
/* End a notification handler. */
374389
struct command_result *WARN_UNUSED_RESULT
375-
notification_handled(struct command *cmd);
390+
notification_handled(struct command *cmd)
391+
NON_NULL_ARGS(1);
376392

377393
/* End a command created with aux_command. */
378394
struct command_result *WARN_UNUSED_RESULT
379-
aux_command_done(struct command *cmd);
395+
aux_command_done(struct command *cmd)
396+
NON_NULL_ARGS(1);
380397

381398
/**
382399
* What's the deprecation_ok state for this cmd?
383400
* @cmd: the command.
384401
*
385402
* Either the default, or the explicit connection override.
386403
*/
387-
bool command_deprecated_ok_flag(const struct command *cmd);
404+
bool command_deprecated_ok_flag(const struct command *cmd)
405+
NON_NULL_ARGS(1);
388406

389407
/* Helper for notification handler that will be finished in a callback. */
390408
#define notification_handler_pending(cmd) command_still_pending(cmd)
@@ -422,22 +440,21 @@ struct command_result *send_outreq(struct plugin *plugin,
422440
struct command_result *forward_error(struct command *cmd,
423441
const char *buf,
424442
const jsmntok_t *error,
425-
void *arg);
443+
void *arg)
444+
NON_NULL_ARGS(1, 2, 3);
426445

427446
/* Callback to just forward result and close request; @cmd cannot be NULL */
428447
struct command_result *forward_result(struct command *cmd,
429448
const char *buf,
430449
const jsmntok_t *result,
431-
void *arg);
450+
void *arg)
451+
NON_NULL_ARGS(1, 2, 3);
432452

433453
/* Callback for timer where we expect a 'command_result'. All timers
434454
* must return this eventually, though they may do so via a convoluted
435455
* send_req() path. */
436-
struct command_result *timer_complete(struct command *cmd);
437-
438-
/* Signals that we've completed a command. Useful for when
439-
* there's no `cmd` present. Deprecated! */
440-
struct command_result *command_done(void);
456+
struct command_result *timer_complete(struct command *cmd)
457+
NON_NULL_ARGS(1);
441458

442459
/* Access timer infrastructure to add a global timer for the plugin.
443460
*

plugins/test/run-route-calc.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313

1414
/* AUTOGENERATED MOCKS START */
1515
/* Generated stub for aux_command */
16-
struct command *aux_command(const struct command *cmd UNNEEDED)
16+
struct command *aux_command(const struct command *cmd)
17+
1718
{ fprintf(stderr, "aux_command called!\n"); abort(); }
1819
/* Generated stub for blinded_onion_hops */
1920
u8 **blinded_onion_hops(const tal_t *ctx UNNEEDED,
@@ -23,10 +24,12 @@ u8 **blinded_onion_hops(const tal_t *ctx UNNEEDED,
2324
const struct blinded_path *path UNNEEDED)
2425
{ fprintf(stderr, "blinded_onion_hops called!\n"); abort(); }
2526
/* Generated stub for command_finished */
26-
struct command_result *command_finished(struct command *cmd UNNEEDED, struct json_stream *response UNNEEDED)
27+
struct command_result *command_finished(struct command *cmd UNNEEDED, struct json_stream *response)
28+
2729
{ fprintf(stderr, "command_finished called!\n"); abort(); }
2830
/* Generated stub for command_still_pending */
29-
struct command_result *command_still_pending(struct command *cmd UNNEEDED)
31+
struct command_result *command_still_pending(struct command *cmd)
32+
3033
{ fprintf(stderr, "command_still_pending called!\n"); abort(); }
3134
/* Generated stub for daemon_poll */
3235
int daemon_poll(struct pollfd *fds UNNEEDED, nfds_t nfds UNNEEDED, int timeout UNNEEDED)
@@ -259,15 +262,18 @@ struct out_req *jsonrpc_request_start_(struct plugin *plugin UNNEEDED,
259262
const char *buf UNNEEDED,
260263
const jsmntok_t *result UNNEEDED,
261264
void *arg) UNNEEDED,
262-
void *arg UNNEEDED)
265+
void *arg)
266+
263267
{ fprintf(stderr, "jsonrpc_request_start_ called!\n"); abort(); }
264268
/* Generated stub for jsonrpc_stream_fail */
265269
struct json_stream *jsonrpc_stream_fail(struct command *cmd UNNEEDED,
266270
int code UNNEEDED,
267-
const char *err UNNEEDED)
271+
const char *err)
272+
268273
{ fprintf(stderr, "jsonrpc_stream_fail called!\n"); abort(); }
269274
/* Generated stub for jsonrpc_stream_success */
270-
struct json_stream *jsonrpc_stream_success(struct command *cmd UNNEEDED)
275+
struct json_stream *jsonrpc_stream_success(struct command *cmd)
276+
271277
{ fprintf(stderr, "jsonrpc_stream_success called!\n"); abort(); }
272278
/* Generated stub for memleak_add_helper_ */
273279
void memleak_add_helper_(const tal_t *p UNNEEDED, void (*cb)(struct htable *memtable UNNEEDED,

plugins/test/run-route-overlong.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010

1111
/* AUTOGENERATED MOCKS START */
1212
/* Generated stub for aux_command */
13-
struct command *aux_command(const struct command *cmd UNNEEDED)
13+
struct command *aux_command(const struct command *cmd)
14+
1415
{ fprintf(stderr, "aux_command called!\n"); abort(); }
1516
/* Generated stub for blinded_onion_hops */
1617
u8 **blinded_onion_hops(const tal_t *ctx UNNEEDED,
@@ -20,10 +21,12 @@ u8 **blinded_onion_hops(const tal_t *ctx UNNEEDED,
2021
const struct blinded_path *path UNNEEDED)
2122
{ fprintf(stderr, "blinded_onion_hops called!\n"); abort(); }
2223
/* Generated stub for command_finished */
23-
struct command_result *command_finished(struct command *cmd UNNEEDED, struct json_stream *response UNNEEDED)
24+
struct command_result *command_finished(struct command *cmd UNNEEDED, struct json_stream *response)
25+
2426
{ fprintf(stderr, "command_finished called!\n"); abort(); }
2527
/* Generated stub for command_still_pending */
26-
struct command_result *command_still_pending(struct command *cmd UNNEEDED)
28+
struct command_result *command_still_pending(struct command *cmd)
29+
2730
{ fprintf(stderr, "command_still_pending called!\n"); abort(); }
2831
/* Generated stub for daemon_poll */
2932
int daemon_poll(struct pollfd *fds UNNEEDED, nfds_t nfds UNNEEDED, int timeout UNNEEDED)
@@ -256,15 +259,18 @@ struct out_req *jsonrpc_request_start_(struct plugin *plugin UNNEEDED,
256259
const char *buf UNNEEDED,
257260
const jsmntok_t *result UNNEEDED,
258261
void *arg) UNNEEDED,
259-
void *arg UNNEEDED)
262+
void *arg)
263+
260264
{ fprintf(stderr, "jsonrpc_request_start_ called!\n"); abort(); }
261265
/* Generated stub for jsonrpc_stream_fail */
262266
struct json_stream *jsonrpc_stream_fail(struct command *cmd UNNEEDED,
263267
int code UNNEEDED,
264-
const char *err UNNEEDED)
268+
const char *err)
269+
265270
{ fprintf(stderr, "jsonrpc_stream_fail called!\n"); abort(); }
266271
/* Generated stub for jsonrpc_stream_success */
267-
struct json_stream *jsonrpc_stream_success(struct command *cmd UNNEEDED)
272+
struct json_stream *jsonrpc_stream_success(struct command *cmd)
273+
268274
{ fprintf(stderr, "jsonrpc_stream_success called!\n"); abort(); }
269275
/* Generated stub for memleak_add_helper_ */
270276
void memleak_add_helper_(const tal_t *p UNNEEDED, void (*cb)(struct htable *memtable UNNEEDED,

0 commit comments

Comments
 (0)