Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ LDLIBS = -L/usr/local/lib -lm -lgmp -lsqlite3 -lz $(COVFLAGS)

default: all-programs all-test-programs

config.vars ccan/config.h: configure
config.vars ccan/config.h: configure ccan/tools/configurator/configurator.c
@if [ ! -f config.vars ]; then echo 'The 1990s are calling: use ./configure!' >&2; exit 1; fi
./configure --reconfigure

Expand Down
2 changes: 1 addition & 1 deletion ccan/README
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
CCAN imported from http://ccodearchive.net.

CCAN version: init-2454-gc656dceb
CCAN version: init-2455-gd3d2242b
58 changes: 58 additions & 0 deletions ccan/ccan/compiler/compiler.h
Original file line number Diff line number Diff line change
Expand Up @@ -228,4 +228,62 @@
#define WARN_UNUSED_RESULT
#endif
#endif


#if HAVE_ATTRIBUTE_DEPRECATED
/**
* WARN_DEPRECATED - warn that a function/type/variable is deprecated when used.
*
* Used to mark a function, type or variable should not be used.
*
* Example:
* WARN_DEPRECATED char *oldfunc(char *buf);
*/
#define WARN_DEPRECATED __attribute__((__deprecated__))
#else
#define WARN_DEPRECATED
#endif


#if HAVE_ATTRIBUTE_NONNULL
/**
* NO_NULL_ARGS - specify that no arguments to this function can be NULL.
*
* The compiler will warn if any pointer args are NULL.
*
* Example:
* NO_NULL_ARGS char *my_copy(char *buf);
*/
#define NO_NULL_ARGS __attribute__((__nonnull__))

/**
* NON_NULL_ARGS - specify that some arguments to this function can't be NULL.
* @...: 1-based argument numbers for which args can't be NULL.
*
* The compiler will warn if any of the specified pointer args are NULL.
*
* Example:
* char *my_copy2(char *buf, char *maybenull) NON_NULL_ARGS(1, 2);
*/
#define NON_NULL_ARGS(index, ...) __attribute__((__nonnull__(index, __VA_ARGS__)))
#else
#define NO_NULL_ARGS
#define NON_NULL_ARGS(index, ...)
#endif


#if HAVE_ATTRIBUTE_SENTINEL
/**
* LAST_ARG_NULL - specify the last argument of a variadic function must be NULL.
*
* The compiler will warn if the last argument isn't NULL.
*
* Example:
* char *join_string(char *buf, ...) LAST_ARG_NULL;
*/
#define LAST_ARG_NULL __attribute__((__sentinel__))
#else
#define LAST_ARG_NULL
#endif

#endif /* CCAN_COMPILER_H */
2 changes: 1 addition & 1 deletion ccan/ccan/pipecmd/_info
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
* exit(1);
* exit(0);
* }
* child = pipecmd(&outputfd, NULL, NULL, argv[0], "ignoredarg", NULL);
* child = pipecmd(NULL, &outputfd, NULL, argv[0], "ignoredarg", NULL);
* if (child < 0)
* err(1, "Creating child");
* if (read(outputfd, input, sizeof(input)) != sizeof(input))
Expand Down
29 changes: 20 additions & 9 deletions ccan/ccan/pipecmd/pipecmd.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include <unistd.h>
#include <fcntl.h>

int pipecmd_preserve;

static char **gather_args(const char *arg0, va_list ap)
{
size_t n = 1;
Expand All @@ -26,7 +28,7 @@ static char **gather_args(const char *arg0, va_list ap)
return arr;
}

pid_t pipecmdv(int *fd_fromchild, int *fd_tochild, int *fd_errfromchild,
pid_t pipecmdv(int *fd_tochild, int *fd_fromchild, int *fd_errfromchild,
const char *cmd, va_list ap)
{
char **arr = gather_args(cmd, ap);
Expand All @@ -36,36 +38,45 @@ pid_t pipecmdv(int *fd_fromchild, int *fd_tochild, int *fd_errfromchild,
errno = ENOMEM;
return -1;
}
ret = pipecmdarr(fd_fromchild, fd_tochild, fd_errfromchild, arr);
ret = pipecmdarr(fd_tochild, fd_fromchild, fd_errfromchild, arr);
free_noerr(arr);
return ret;
}

pid_t pipecmdarr(int *fd_fromchild, int *fd_tochild, int *fd_errfromchild,
pid_t pipecmdarr(int *fd_tochild, int *fd_fromchild, int *fd_errfromchild,
char *const *arr)
{
int tochild[2], fromchild[2], errfromchild[2], execfail[2];
pid_t childpid;
int err;

if (fd_tochild) {
if (pipe(tochild) != 0)
if (fd_tochild == &pipecmd_preserve) {
tochild[0] = STDIN_FILENO;
fd_tochild = NULL;
} else if (pipe(tochild) != 0)
goto fail;
} else {
tochild[0] = open("/dev/null", O_RDONLY);
if (tochild[0] < 0)
goto fail;
}
if (fd_fromchild) {
if (pipe(fromchild) != 0)
if (fd_fromchild == &pipecmd_preserve) {
fromchild[1] = STDOUT_FILENO;
fd_fromchild = NULL;
} else if (pipe(fromchild) != 0)
goto close_tochild_fail;
} else {
fromchild[1] = open("/dev/null", O_WRONLY);
if (fromchild[1] < 0)
goto close_tochild_fail;
}
if (fd_errfromchild) {
if (fd_errfromchild == fd_fromchild) {
if (fd_errfromchild == &pipecmd_preserve) {
errfromchild[1] = STDERR_FILENO;
fd_errfromchild = NULL;
} else if (fd_errfromchild == fd_fromchild) {
errfromchild[0] = fromchild[0];
errfromchild[1] = fromchild[1];
} else {
Expand All @@ -77,7 +88,7 @@ pid_t pipecmdarr(int *fd_fromchild, int *fd_tochild, int *fd_errfromchild,
if (errfromchild[1] < 0)
goto close_fromchild_fail;
}

if (pipe(execfail) != 0)
goto close_errfromchild_fail;

Expand Down Expand Up @@ -168,14 +179,14 @@ pid_t pipecmdarr(int *fd_fromchild, int *fd_tochild, int *fd_errfromchild,
return -1;
}

pid_t pipecmd(int *fd_fromchild, int *fd_tochild, int *fd_errfromchild,
pid_t pipecmd(int *fd_tochild, int *fd_fromchild, int *fd_errfromchild,
const char *cmd, ...)
{
pid_t childpid;

va_list ap;
va_start(ap, cmd);
childpid = pipecmdv(fd_fromchild, fd_tochild, fd_errfromchild, cmd, ap);
childpid = pipecmdv(fd_tochild, fd_fromchild, fd_errfromchild, cmd, ap);
va_end(ap);

return childpid;
Expand Down
7 changes: 7 additions & 0 deletions ccan/ccan/pipecmd/pipecmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* If @errfd is NULL, the child's stderr is (write-only) /dev/null.
*
* If @errfd == @outfd (and non-NULL) they will be shared.
* If @infd, @outfd or @errfd is &pipecmd_preserve, it is unchanged.
*
* The return value is the pid of the child, or -1.
*/
Expand All @@ -41,4 +42,10 @@ pid_t pipecmdv(int *infd, int *outfd, int *errfd, const char *cmd, va_list ap);
* @arr: NULL-terminated array for arguments (first is program to run).
*/
pid_t pipecmdarr(int *infd, int *outfd, int *errfd, char *const *arr);

/**
* pipecmd_preserve - special value for fds to indicate it is unchanged
*/
extern int pipecmd_preserve;

#endif /* CCAN_PIPECMD_H */
2 changes: 1 addition & 1 deletion ccan/ccan/pipecmd/test/run-fdleak.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ int main(int argc, char *argv[])

/* This is how many tests you plan to run */
plan_tests(13);
child = pipecmd(&outfd, NULL, NULL, argv[0], "out", NULL);
child = pipecmd(NULL, &outfd, NULL, argv[0], "out", NULL);
if (!ok1(child > 0))
exit(1);
ok1(read(outfd, buf, sizeof(buf)) == sizeof(buf));
Expand Down
10 changes: 5 additions & 5 deletions ccan/ccan/pipecmd/test/run.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ int main(int argc, char *argv[])

/* This is how many tests you plan to run */
plan_tests(67);
child = pipecmd(&outfd, &infd, &errfd, argv[0], "inout", NULL);
child = pipecmd(&infd, &outfd, &errfd, argv[0], "inout", NULL);
if (!ok1(child > 0))
exit(1);
ok1(write(infd, buf, sizeof(buf)) == sizeof(buf));
Expand All @@ -63,7 +63,7 @@ int main(int argc, char *argv[])
ok1(WIFEXITED(status));
ok1(WEXITSTATUS(status) == 0);

child = pipecmd(NULL, &infd, NULL, argv[0], "in", NULL);
child = pipecmd(&infd, NULL, NULL, argv[0], "in", NULL);
if (!ok1(child > 0))
exit(1);
ok1(write(infd, buf, sizeof(buf)) == sizeof(buf));
Expand All @@ -72,7 +72,7 @@ int main(int argc, char *argv[])
ok1(WIFEXITED(status));
ok1(WEXITSTATUS(status) == 0);

child = pipecmd(&outfd, NULL, NULL, argv[0], "out", NULL);
child = pipecmd(NULL, &outfd, NULL, argv[0], "out", NULL);
if (!ok1(child > 0))
exit(1);
ok1(read(outfd, buf, sizeof(buf)) == sizeof(buf));
Expand All @@ -94,7 +94,7 @@ int main(int argc, char *argv[])
ok1(WEXITSTATUS(status) == 0);

/* errfd == outfd should work with both. */
child = pipecmd(&errfd, NULL, &errfd, argv[0], "err", NULL);
child = pipecmd(NULL, &errfd, &errfd, argv[0], "err", NULL);
if (!ok1(child > 0))
exit(1);
ok1(read(errfd, buf, sizeof(buf)) == sizeof(buf));
Expand All @@ -104,7 +104,7 @@ int main(int argc, char *argv[])
ok1(WIFEXITED(status));
ok1(WEXITSTATUS(status) == 0);

child = pipecmd(&outfd, NULL, &outfd, argv[0], "out", NULL);
child = pipecmd(NULL, &outfd, &outfd, argv[0], "out", NULL);
if (!ok1(child > 0))
exit(1);
ok1(read(outfd, buf, sizeof(buf)) == sizeof(buf));
Expand Down
17 changes: 14 additions & 3 deletions ccan/ccan/take/take.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,20 @@ void *take_(const void *p, const char *label)
}
takenarr = new;
/* Once labelarr is set, we maintain it. */
if (labelarr)
labelarr = realloc(labelarr,
sizeof(*labelarr) * (max_taken+1));
if (labelarr) {
const char **labelarr_new;
labelarr_new = realloc(labelarr,
sizeof(*labelarr) * (max_taken+1));
if (labelarr_new) {
labelarr = labelarr_new;
} else {
/* num_taken will be out of sync with the size of
* labelarr after realloc failure.
* Just pretend that we never had labelarr allocated. */
free(labelarr);
labelarr = NULL;
}
}
max_taken++;
}
if (unlikely(labelarr))
Expand Down
9 changes: 9 additions & 0 deletions ccan/tools/configurator/configurator.c
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,15 @@ static const struct test base_tests[] = {
{ "HAVE_ATTRIBUTE_CONST", "__attribute__((const)) support",
"DEFINES_FUNC", NULL, NULL,
"static int __attribute__((const)) func(int x) { return x; }" },
{ "HAVE_ATTRIBUTE_DEPRECATED", "__attribute__((deprecated)) support",
"DEFINES_FUNC", NULL, NULL,
"static int __attribute__((deprecated)) func(int x) { return x; }" },
{ "HAVE_ATTRIBUTE_NONNULL", "__attribute__((nonnull)) support",
"DEFINES_FUNC", NULL, NULL,
"static char *__attribute__((nonnull)) func(char *p) { return p; }" },
{ "HAVE_ATTRIBUTE_SENTINEL", "__attribute__((sentinel)) support",
"DEFINES_FUNC", NULL, NULL,
"static int __attribute__((sentinel)) func(int i, ...) { return i; }" },
{ "HAVE_ATTRIBUTE_PURE", "__attribute__((pure)) support",
"DEFINES_FUNC", NULL, NULL,
"static int __attribute__((pure)) func(int x) { return x; }" },
Expand Down
5 changes: 4 additions & 1 deletion common/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,13 @@ COMMON_SRC_NOGEN := \
common/initial_commit_tx.c \
common/io_lock.c \
common/json.c \
common/json_escaped.c \
common/json_tok.c \
common/key_derive.c \
common/keyset.c \
common/memleak.c \
common/msg_queue.c \
common/param.c \
common/peer_billboard.c \
common/peer_failed.c \
common/permute_tx.c \
Expand All @@ -52,7 +55,7 @@ COMMON_SRC_NOGEN := \

COMMON_SRC_GEN := common/gen_status_wire.c common/gen_peer_status_wire.c

COMMON_HEADERS_NOGEN := $(COMMON_SRC_NOGEN:.c=.h) common/overflows.h common/htlc.h common/status_levels.h
COMMON_HEADERS_NOGEN := $(COMMON_SRC_NOGEN:.c=.h) common/overflows.h common/htlc.h common/status_levels.h common/json_command.h common/jsonrpc_errors.h
COMMON_HEADERS_GEN := common/gen_htlc_state_names.h common/gen_status_wire.h common/gen_peer_status_wire.h

COMMON_HEADERS := $(COMMON_HEADERS_GEN) $(COMMON_HEADERS_NOGEN)
Expand Down
21 changes: 21 additions & 0 deletions common/daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <ccan/err/err.h>
#include <ccan/io/io.h>
#include <ccan/str/str.h>
#include <ccan/tal/str/str.h>
#include <common/daemon.h>
#include <common/memleak.h>
#include <common/status.h>
Expand Down Expand Up @@ -150,3 +151,23 @@ void daemon_shutdown(void)
tal_free(tmpctx);
wally_cleanup(0);
}

void daemon_maybe_debug(int argc, char *argv[])
{
#if DEVELOPER
for (int i = 1; i < argc; i++) {
if (!streq(argv[i], "--debugger"))
continue;

/* Don't let this mess up stdout, so redir to /dev/null */
char *cmd = tal_fmt(NULL, "${DEBUG_TERM:-gnome-terminal --} gdb -ex 'attach %u' %s >/dev/null &", getpid(), argv[0]);
fprintf(stderr, "Running %s\n", cmd);
/* warn_unused_result is fascist bullshit.
* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66425 */
if (system(cmd))
;
/* Continue in the debugger. */
kill(getpid(), SIGSTOP);
}
#endif /* DEVELOPER */
}
3 changes: 3 additions & 0 deletions common/daemon.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ int daemon_poll(struct pollfd *fds, nfds_t nfds, int timeout);
/* Shutdown for a valgrind-clean exit (frees everything) */
void daemon_shutdown(void);

/* Kick in a debugger if they set --debugger */
void daemon_maybe_debug(int argc, char *argv[]);

struct backtrace_state *backtrace_state;

#endif /* LIGHTNING_COMMON_DAEMON_H */
9 changes: 5 additions & 4 deletions common/json.c
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ bool json_to_number(const char *buffer, const jsmntok_t *tok,
return true;
}

bool json_tok_bitcoin_amount(const char *buffer, const jsmntok_t *tok,
uint64_t *satoshi)
bool json_to_bitcoin_amount(const char *buffer, const jsmntok_t *tok,
uint64_t *satoshi)
{
char *end;
unsigned long btc, sat;
Expand Down Expand Up @@ -172,13 +172,14 @@ const jsmntok_t *json_get_arr(const jsmntok_t tok[], size_t index)
return NULL;
}

jsmntok_t *json_parse_input(const char *input, int len, bool *valid)
jsmntok_t *json_parse_input(const tal_t *ctx,
const char *input, int len, bool *valid)
{
jsmn_parser parser;
jsmntok_t *toks;
int ret;

toks = tal_arr(input, jsmntok_t, 10);
toks = tal_arr(ctx, jsmntok_t, 10);
toks[0].type = JSMN_UNDEFINED;

jsmn_init(&parser);
Expand Down
Loading