Skip to content

Commit 76f116d

Browse files
committed
lightningd: minor cleanups
Code changes: 1. Expose daemon_poll() so lightningd can call it directly, which avoids us having store a global and document it. 2. Remove the (undocumented, unused, forgotten) --rpc-file="" option to disable JSON RPC. 3. Move the ickiness of finding the executable path into subd.c, so it doesn't distract from lightningd.c overview. Signed-off-by: Rusty Russell <[email protected]>
1 parent 0d46a3d commit 76f116d

File tree

7 files changed

+109
-87
lines changed

7 files changed

+109
-87
lines changed

common/daemon.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include <common/status.h>
88
#include <common/utils.h>
99
#include <common/version.h>
10-
#include <poll.h>
1110
#include <signal.h>
1211
#include <stdio.h>
1312
#include <stdlib.h>
@@ -66,7 +65,7 @@ static void crashlog_activate(void)
6665
}
6766
#endif
6867

69-
static int daemon_poll(struct pollfd *fds, nfds_t nfds, int timeout)
68+
int daemon_poll(struct pollfd *fds, nfds_t nfds, int timeout)
7069
{
7170
const char *t;
7271

common/daemon.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
#ifndef LIGHTNING_COMMON_DAEMON_H
22
#define LIGHTNING_COMMON_DAEMON_H
33
#include "config.h"
4+
#include <poll.h>
45

56
/* Common setup for all daemons */
67
void daemon_setup(const char *argv0,
78
void (*backtrace_print)(const char *fmt, ...),
89
void (*backtrace_exit)(void));
910

11+
/* Exposed for lightningd's use. */
12+
int daemon_poll(struct pollfd *fds, nfds_t nfds, int timeout);
13+
1014
/* Shutdown for a valgrind-clean exit (frees everything) */
1115
void daemon_shutdown(void);
1216

lightningd/jsonrpc.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -600,9 +600,6 @@ void setup_jsonrpc(struct lightningd *ld, const char *rpc_filename)
600600
struct sockaddr_un addr;
601601
int fd, old_umask;
602602

603-
if (streq(rpc_filename, ""))
604-
return;
605-
606603
if (streq(rpc_filename, "/dev/tty")) {
607604
fd = open(rpc_filename, O_RDWR);
608605
if (fd == -1)

lightningd/lightningd.c

Lines changed: 21 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -232,62 +232,24 @@ static bool has_all_subdaemons(const char* daemon_dir)
232232
return !missing_daemon;
233233
}
234234

235-
/* This routine tries to determine what path the lightningd binary is in.
236-
* It's not actually that simple! */
237-
static const char *find_my_path(const tal_t *ctx, const char *argv0)
235+
/* Returns the directory this executable is running from */
236+
static const char *find_my_directory(const tal_t *ctx, const char *argv0)
238237
{
239-
char *me;
240-
241-
/* A command containing / is run relative to the current directory,
242-
* not searched through the path. The shell sets argv0 to the command
243-
* run, though something else could set it to a arbitrary value and
244-
* this logic would be wrong. */
245-
if (strchr(argv0, PATH_SEP)) {
246-
const char *path;
247-
/* Absolute paths are easy. */
248-
if (strstarts(argv0, PATH_SEP_STR))
249-
path = argv0;
250-
/* It contains a '/', it's relative to current dir. */
251-
else
252-
path = path_join(tmpctx, path_cwd(tmpctx), argv0);
253-
254-
me = path_canon(ctx, path);
255-
if (!me || access(me, X_OK) != 0)
256-
errx(1, "I cannot find myself at %s based on my name %s",
257-
path, argv0);
258-
} else {
259-
/* No /, search path */
260-
char **pathdirs;
261-
const char *pathenv = getenv("PATH");
262-
size_t i;
263-
264-
/* This replicates the standard shell path search algorithm */
265-
if (!pathenv)
266-
errx(1, "Cannot find myself: no $PATH set");
267-
268-
pathdirs = tal_strsplit(tmpctx, pathenv, ":", STR_NO_EMPTY);
269-
me = NULL;
270-
for (i = 0; pathdirs[i]; i++) {
271-
/* This returns NULL if it doesn't exist. */
272-
me = path_canon(ctx,
273-
path_join(tmpctx, pathdirs[i], argv0));
274-
if (me && access(me, X_OK) == 0)
275-
break;
276-
/* Nope, try again. */
277-
me = tal_free(me);
278-
}
279-
if (!me)
280-
errx(1, "Cannot find %s in $PATH", argv0);
281-
}
238+
/* find_my_abspath simply exits on failure, so never returns NULL. */
239+
const char *me = find_my_abspath(NULL, argv0);
282240

283241
/*~ The caller just wants the directory we're in.
284242
*
285-
* Note the magic "take()" macro here: it annotates a pointer as "to
243+
* Note the magic `take()` macro here: it annotates a pointer as "to
286244
* be taken", and the recipient is expected to take ownership of the
287-
* pointer.
245+
* pointer. This improves efficiency because the recipient might
246+
* choose to use or even keep it rather than make a copy (or it
247+
* might just free it).
288248
*
289-
* Many CCAN and our own routines support this, but if you hand a take()
290-
* to a non-take routine unfortunately you don't get a compile error.
249+
* Many CCAN and our own routines support this, but if you hand a
250+
* `take()` to a routine which *doesn't* expect it, unfortunately you
251+
* don't get a compile error (we have runtime detection for this
252+
* case, however).
291253
*/
292254
return path_dirname(ctx, take(me));
293255
}
@@ -310,7 +272,7 @@ static const char *find_my_pkglibexec_path(const tal_t *ctx,
310272
/* Determine the correct daemon dir. */
311273
static const char *find_daemon_dir(const tal_t *ctx, const char *argv0)
312274
{
313-
const char *my_path = find_my_path(ctx, argv0);
275+
const char *my_path = find_my_directory(ctx, argv0);
314276
/* If we're running in-tree, all the subdaemons are with lightningd. */
315277
if (has_all_subdaemons(my_path))
316278
return my_path;
@@ -470,19 +432,17 @@ static void pidfile_create(const struct lightningd *ld)
470432
/* Leave file open: we close it implicitly when we exit */
471433
}
472434

473-
/*~ Yuck, we need a global here.
474-
*
475-
* ccan/io allows overriding the poll() function for special effects: for
476-
* lightningd, we make sure we haven't left a db transaction open. All
477-
* daemons which use ccan/io add sanity checks in this loop, so we chain
478-
* that after our own override.
479-
*/
480-
static int (*io_poll_debug)(struct pollfd *, nfds_t, int);
435+
/*~ ccan/io allows overriding the poll() function that is the very core
436+
* of the event loop it runs for us. We override it so that we can do
437+
* extra sanity checks, and it's also a good point to free the tmpctx. */
481438
static int io_poll_lightningd(struct pollfd *fds, nfds_t nfds, int timeout)
482439
{
440+
/*~ In particular, we should *not* have left a database transaction
441+
* open! */
483442
db_assert_no_outstanding_statements();
484443

485-
return io_poll_debug(fds, nfds, timeout);
444+
/* The other checks and freeing tmpctx are common to all daemons. */
445+
return daemon_poll(fds, nfds, timeout);
486446
}
487447

488448
/*~ Ever had one of those functions which doesn't quite fit anywhere? Me too.
@@ -540,7 +500,7 @@ int main(int argc, char *argv[])
540500
ld->owned_txfilter = txfilter_new(ld);
541501

542502
/*~ This is the ccan/io central poll override from above. */
543-
io_poll_debug = io_poll_override(io_poll_lightningd);
503+
io_poll_override(io_poll_lightningd);
544504

545505
/*~ Set up HSM: it knows our node secret key, so tells us who we are. */
546506
hsm_init(ld);

lightningd/subd.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -822,3 +822,53 @@ bool dev_disconnect_permanent(struct lightningd *ld)
822822
return false;
823823
}
824824
#endif /* DEVELOPER */
825+
826+
/* Ugly helper to get full pathname of the current binary. */
827+
const char *find_my_abspath(const tal_t *ctx, const char *argv0)
828+
{
829+
char *me;
830+
831+
/* A command containing / is run relative to the current directory,
832+
* not searched through the path. The shell sets argv0 to the command
833+
* run, though something else could set it to a arbitrary value and
834+
* this logic would be wrong. */
835+
if (strchr(argv0, PATH_SEP)) {
836+
const char *path;
837+
/* Absolute paths are easy. */
838+
if (strstarts(argv0, PATH_SEP_STR))
839+
path = argv0;
840+
/* It contains a '/', it's relative to current dir. */
841+
else
842+
path = path_join(tmpctx, path_cwd(tmpctx), argv0);
843+
844+
me = path_canon(ctx, path);
845+
if (!me || access(me, X_OK) != 0)
846+
errx(1, "I cannot find myself at %s based on my name %s",
847+
path, argv0);
848+
} else {
849+
/* No /, search path */
850+
char **pathdirs;
851+
const char *pathenv = getenv("PATH");
852+
size_t i;
853+
854+
/* This replicates the standard shell path search algorithm */
855+
if (!pathenv)
856+
errx(1, "Cannot find myself: no $PATH set");
857+
858+
pathdirs = tal_strsplit(tmpctx, pathenv, ":", STR_NO_EMPTY);
859+
me = NULL;
860+
for (i = 0; pathdirs[i]; i++) {
861+
/* This returns NULL if it doesn't exist. */
862+
me = path_canon(ctx,
863+
path_join(tmpctx, pathdirs[i], argv0));
864+
if (me && access(me, X_OK) == 0)
865+
break;
866+
/* Nope, try again. */
867+
me = tal_free(me);
868+
}
869+
if (!me)
870+
errx(1, "Cannot find %s in $PATH", argv0);
871+
}
872+
873+
return me;
874+
}

lightningd/subd.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,9 @@ void subd_release_channel(struct subd *owner, void *channel);
202202
*/
203203
void subd_shutdown(struct subd *subd, unsigned int seconds);
204204

205+
/* Ugly helper to get full pathname of the current binary. */
206+
const char *find_my_abspath(const tal_t *ctx, const char *argv0);
207+
205208
#if DEVELOPER
206209
char *opt_subd_debug(const char *optarg, struct lightningd *ld);
207210
char *opt_subd_dev_disconnect(const char *optarg, struct lightningd *ld);

lightningd/test/run-find_my_path.c renamed to lightningd/test/run-find_my_abspath.c

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ int unused_main(int argc, char *argv[]);
33
#include "../../common/base32.c"
44
#include "../../common/wireaddr.c"
55
#include "../lightningd.c"
6+
#include "../subd.c"
67

78
/* AUTOGENERATED MOCKS START */
89
/* Generated stub for activate_peers */
@@ -21,6 +22,9 @@ void connectd_activate(struct lightningd *ld UNNEEDED)
2122
/* Generated stub for connectd_init */
2223
int connectd_init(struct lightningd *ld UNNEEDED)
2324
{ fprintf(stderr, "connectd_init called!\n"); abort(); }
25+
/* Generated stub for daemon_poll */
26+
int daemon_poll(struct pollfd *fds UNNEEDED, nfds_t nfds UNNEEDED, int timeout UNNEEDED)
27+
{ fprintf(stderr, "daemon_poll called!\n"); abort(); }
2428
/* Generated stub for daemon_setup */
2529
void daemon_setup(const char *argv0 UNNEEDED,
2630
void (*backtrace_print)(const char *fmt UNNEEDED, ...) UNNEEDED,
@@ -53,6 +57,18 @@ void fatal(const char *fmt UNNEEDED, ...)
5357
/* Generated stub for free_htlcs */
5458
void free_htlcs(struct lightningd *ld UNNEEDED, const struct channel *channel UNNEEDED)
5559
{ fprintf(stderr, "free_htlcs called!\n"); abort(); }
60+
/* Generated stub for fromwire_status_fail */
61+
bool fromwire_status_fail(const tal_t *ctx UNNEEDED, const void *p UNNEEDED, enum status_failreason *failreason UNNEEDED, wirestring **desc UNNEEDED)
62+
{ fprintf(stderr, "fromwire_status_fail called!\n"); abort(); }
63+
/* Generated stub for fromwire_status_peer_billboard */
64+
bool fromwire_status_peer_billboard(const tal_t *ctx UNNEEDED, const void *p UNNEEDED, bool *perm UNNEEDED, wirestring **happenings UNNEEDED)
65+
{ fprintf(stderr, "fromwire_status_peer_billboard called!\n"); abort(); }
66+
/* Generated stub for fromwire_status_peer_error */
67+
bool fromwire_status_peer_error(const tal_t *ctx UNNEEDED, const void *p UNNEEDED, struct channel_id *channel UNNEEDED, wirestring **desc UNNEEDED, struct crypto_state *crypto_state UNNEEDED, u8 **error_for_them UNNEEDED)
68+
{ fprintf(stderr, "fromwire_status_peer_error called!\n"); abort(); }
69+
/* Generated stub for get_log_book */
70+
struct log_book *get_log_book(const struct log *log UNNEEDED)
71+
{ fprintf(stderr, "get_log_book called!\n"); abort(); }
5672
/* Generated stub for gossip_init */
5773
void gossip_init(struct lightningd *ld UNNEEDED, int connectd_fd UNNEEDED)
5874
{ fprintf(stderr, "gossip_init called!\n"); abort(); }
@@ -84,6 +100,12 @@ void log_backtrace_exit(void)
84100
/* Generated stub for log_backtrace_print */
85101
void log_backtrace_print(const char *fmt UNNEEDED, ...)
86102
{ fprintf(stderr, "log_backtrace_print called!\n"); abort(); }
103+
/* Generated stub for log_prefix */
104+
const char *log_prefix(const struct log *log UNNEEDED)
105+
{ fprintf(stderr, "log_prefix called!\n"); abort(); }
106+
/* Generated stub for log_status_msg */
107+
bool log_status_msg(struct log *log UNNEEDED, const u8 *msg UNNEEDED)
108+
{ fprintf(stderr, "log_status_msg called!\n"); abort(); }
87109
/* Generated stub for new_log */
88110
struct log *new_log(const tal_t *ctx UNNEEDED, struct log_book *record UNNEEDED, const char *fmt UNNEEDED, ...)
89111
{ fprintf(stderr, "new_log called!\n"); abort(); }
@@ -110,9 +132,6 @@ void setup_jsonrpc(struct lightningd *ld UNNEEDED, const char *rpc_filename UNNE
110132
void setup_topology(struct chain_topology *topology UNNEEDED, struct timers *timers UNNEEDED,
111133
u32 min_blockheight UNNEEDED, u32 max_blockheight UNNEEDED)
112134
{ fprintf(stderr, "setup_topology called!\n"); abort(); }
113-
/* Generated stub for subd_shutdown */
114-
void subd_shutdown(struct subd *subd UNNEEDED, unsigned int seconds UNNEEDED)
115-
{ fprintf(stderr, "subd_shutdown called!\n"); abort(); }
116135
/* Generated stub for timer_expired */
117136
void timer_expired(tal_t *ctx UNNEEDED, struct timer *timer UNNEEDED)
118137
{ fprintf(stderr, "timer_expired called!\n"); abort(); }
@@ -144,16 +163,6 @@ struct wallet *wallet_new(struct lightningd *ld UNNEEDED,
144163
{ fprintf(stderr, "wallet_new called!\n"); abort(); }
145164
/* AUTOGENERATED MOCKS END */
146165

147-
/* We only need these in developer mode */
148-
#if DEVELOPER
149-
/* Generated stub for opt_subd_debug */
150-
char *opt_subd_debug(const char *optarg UNNEEDED, struct lightningd *ld UNNEEDED)
151-
{ fprintf(stderr, "opt_subd_debug called!\n"); abort(); }
152-
/* Generated stub for opt_subd_dev_disconnect */
153-
char *opt_subd_dev_disconnect(const char *optarg UNNEEDED, struct lightningd *ld UNNEEDED)
154-
{ fprintf(stderr, "opt_subd_dev_disconnect called!\n"); abort(); }
155-
#endif
156-
157166
struct log *crashlog;
158167

159168
#undef main
@@ -166,26 +175,26 @@ int main(int argc UNUSED, char *argv[] UNUSED)
166175
const char *answer;
167176

168177
setup_tmpctx();
169-
answer = path_canon(tmpctx, "lightningd/test");
178+
answer = path_canon(tmpctx, "lightningd/test/run-find_my_abspath");
170179

171180
/* Various different ways we could find ourselves. */
172181
argv0 = path_join(tmpctx,
173-
path_cwd(tmpctx), "lightningd/test/run-find_my_path");
182+
path_cwd(tmpctx), "lightningd/test/run-find_my_abspath");
174183
unsetenv("PATH");
175184

176185
/* Absolute path. */
177-
assert(streq(find_my_path(tmpctx, argv0), answer));
186+
assert(streq(find_my_abspath(tmpctx, argv0), answer));
178187

179188
/* Relative to cwd. */
180-
argv0 = "lightningd/test/run-find_my_path";
181-
assert(streq(find_my_path(tmpctx, argv0), answer));
189+
argv0 = "lightningd/test/run-find_my_abspath";
190+
assert(streq(find_my_abspath(tmpctx, argv0), answer));
182191

183192
/* Using $PATH */
184193
setenv("PATH", path_join(tmpctx,
185194
path_cwd(tmpctx), "lightningd/test"), 1);
186-
argv0 = "run-find_my_path";
195+
argv0 = "run-find_my_abspath";
187196

188-
assert(streq(find_my_path(tmpctx, argv0), answer));
197+
assert(streq(find_my_abspath(tmpctx, argv0), answer));
189198

190199
/* Even with dummy things in path. */
191200
char **pathelems = tal_arr(tmpctx, char *, 4);
@@ -195,7 +204,7 @@ int main(int argc UNUSED, char *argv[] UNUSED)
195204
pathelems[3] = NULL;
196205

197206
setenv("PATH", tal_strjoin(tmpctx, pathelems, ":", STR_NO_TRAIL), 1);
198-
assert(streq(find_my_path(tmpctx, argv0), answer));
207+
assert(streq(find_my_abspath(tmpctx, argv0), answer));
199208

200209
assert(!taken_any());
201210
take_cleanup();

0 commit comments

Comments
 (0)