From 35eaaf8e63449e737c8a340070bfbcd57bd12540 Mon Sep 17 00:00:00 2001 From: Vincenzo Palazzo Date: Wed, 12 Jan 2022 21:36:56 +0100 Subject: [PATCH] cli: change the redefine logic to read from the stdin Procedure redefinition is a very cool feature but when other libraries redefine the same function can be very tricky to avoid compilation error. This PR proposed a change of logic and use a customizzation function definition to read from the stdin, so we can avoid future error at compile time. However, we could be check also the os or compiler that cause the error and redefine the missing function. Changelog-None: Fixed compilation error due redefinition procedure. Signed-off-by: Vincenzo Palazzo --- cli/config_cli.h | 15 +++++++++++++++ cli/lightning-cli.c | 8 ++++---- cli/test/config_test.h | 8 ++++++++ cli/test/run-human-mode.c | 4 ++-- cli/test/run-large-input.c | 3 ++- cli/test/run-remove-hint.c | 3 ++- 6 files changed, 33 insertions(+), 8 deletions(-) create mode 100644 cli/config_cli.h create mode 100644 cli/test/config_test.h diff --git a/cli/config_cli.h b/cli/config_cli.h new file mode 100644 index 000000000..6faeb3606 --- /dev/null +++ b/cli/config_cli.h @@ -0,0 +1,15 @@ +#ifndef LIGHTNING_CLI_CONFIG_CLI_H +#define LIGHTNING_CLI_CONFIG_CLI_H + +#include "config.h" +#include + +#ifndef CLN_TEST +/* Redefinition procedure is a very cool feature, but + if we try to redefine a procedure that is already + redefined somewhere (like read in alpine) we can have + tricky compilation error */ +#define cli_read read +#endif + +#endif /* LIGHTNING_CLI_CONFIG_CLI_H */ diff --git a/cli/lightning-cli.c b/cli/lightning-cli.c index 743d8b8f0..06e47f2b8 100644 --- a/cli/lightning-cli.c +++ b/cli/lightning-cli.c @@ -2,6 +2,7 @@ * Helper to submit via JSON-RPC and get back response. */ #include "config.h" +#include "config_cli.h" #include #include #include @@ -15,7 +16,6 @@ #include #include #include -#include #include #include #include @@ -386,7 +386,7 @@ static size_t read_nofail(int fd, void *buf, size_t len) ssize_t i; assert(len > 0); - i = read(fd, buf, len); + i = cli_read(fd, buf, len); if (i == 0) errx(ERROR_TALKING_TO_LIGHTNINGD, "reading response: socket closed"); @@ -572,7 +572,7 @@ static void enable_notifications(int fd) memset(rbuf, 0, sizeof(rbuf)); while (!strends(rbuf, "\n\n")) { size_t len = strlen(rbuf); - if (read(fd, rbuf + len, sizeof(rbuf) - len) < 0) + if (cli_read(fd, rbuf + len, sizeof(rbuf) - len) < 0) err(ERROR_TALKING_TO_LIGHTNINGD, "Reading enable response"); } @@ -756,7 +756,7 @@ int main(int argc, char *argv[]) while (parserr <= 0) { /* Read more if parser says, or we have 0 tokens. */ if (parserr == 0 || parserr == JSMN_ERROR_PART) { - ssize_t i = read(fd, resp + off, tal_bytelen(resp) - 1 - off); + ssize_t i = cli_read(fd, resp + off, tal_bytelen(resp) - 1 - off); if (i == 0) errx(ERROR_TALKING_TO_LIGHTNINGD, "reading response: socket closed"); diff --git a/cli/test/config_test.h b/cli/test/config_test.h new file mode 100644 index 000000000..288751118 --- /dev/null +++ b/cli/test/config_test.h @@ -0,0 +1,8 @@ +#ifndef LIGHTNING_CLI_TEST_CONFIG_TEST_H +#define LIGHTNING_CLI_TEST_CONFIG_TEST_H + +#include "config.h" + +#define CLN_TEST 1 + +#endif /* LIGHTNING_CLI_TEST_CONFIG_TEST_H */ diff --git a/cli/test/run-human-mode.c b/cli/test/run-human-mode.c index bf7c4a25f..1548aa404 100644 --- a/cli/test/run-human-mode.c +++ b/cli/test/run-human-mode.c @@ -1,4 +1,5 @@ #include "config.h" +#include "config_test.h" #include #include #include @@ -18,7 +19,7 @@ int test_printf(const char *format, ...); int test_chdir(const char *path); #define main test_main -#define read test_read +#define cli_read test_read #define socket test_socket #define connect test_connect #define getpid test_getpid @@ -174,4 +175,3 @@ int main(int argc UNUSED, char *argv[]) common_shutdown(); return 0; } - diff --git a/cli/test/run-large-input.c b/cli/test/run-large-input.c index 40c8d67f9..507b4e01d 100644 --- a/cli/test/run-large-input.c +++ b/cli/test/run-large-input.c @@ -1,4 +1,5 @@ #include "config.h" +#include "config_test.h" #include #include #include @@ -18,7 +19,7 @@ int test_printf(const char *format, ...); int test_chdir(const char *path); #define main test_main -#define read test_read +#define cli_read test_read #define socket test_socket #define connect test_connect #define getpid test_getpid diff --git a/cli/test/run-remove-hint.c b/cli/test/run-remove-hint.c index 18e21e490..795488483 100644 --- a/cli/test/run-remove-hint.c +++ b/cli/test/run-remove-hint.c @@ -1,4 +1,5 @@ #include "config.h" +#include "config_test.h" #include #include #include @@ -20,7 +21,7 @@ int test_fputc(int c, FILE *stream); int test_chdir(const char *path); #define main test_main -#define read test_read +#define cli_read test_read #define socket test_socket #define connect test_connect #define getpid test_getpid