From cc1cc0b2ce4825057f6a8bb0c6b1b82e17be313e Mon Sep 17 00:00:00 2001 From: david gauchard Date: Fri, 3 Jul 2020 19:10:08 +0200 Subject: [PATCH] emulation on host: option for FS persistence location (#7424) * fix warnings * emulation on host: option -P to change FS persistence location * exit on SIGTERM too, with SIGINT --- tests/host/Makefile | 1 + tests/host/common/ArduinoMain.cpp | 60 +++++++++++++++++++------- tests/host/common/UdpContextSocket.cpp | 6 +-- 3 files changed, 49 insertions(+), 18 deletions(-) diff --git a/tests/host/Makefile b/tests/host/Makefile index f6361f36a..f4513eb22 100644 --- a/tests/host/Makefile +++ b/tests/host/Makefile @@ -153,6 +153,7 @@ DEBUG += -DDEBUG_ESP_SSL -DDEBUG_ESP_TLS_MEM -DDEBUG_ESP_HTTP_CLIENT -DDEBUG_ESP endif FLAGS += $(DEBUG) -Wall $(OPTZ) -fno-common -g $(M32) +FLAGS += -fstack-check -fstack-protector-all FLAGS += -DHTTPCLIENT_1_1_COMPATIBLE=0 FLAGS += -DLWIP_IPV6=0 FLAGS += -DHOST_MOCK=1 diff --git a/tests/host/common/ArduinoMain.cpp b/tests/host/common/ArduinoMain.cpp index 7bfcd9703..fd7bd0a04 100644 --- a/tests/host/common/ArduinoMain.cpp +++ b/tests/host/common/ArduinoMain.cpp @@ -49,6 +49,7 @@ bool ignore_sigint = false; bool restore_tty = false; bool mockdebug = false; int mock_port_shifter = MOCK_PORT_SHIFTER; +const char* fspath = nullptr; #define STDIN STDIN_FILENO @@ -120,19 +121,24 @@ void help (const char* argv0, int exitcode) printf( "%s - compiled with esp8266/arduino emulator\n" "options:\n" - " -h\n" - " -i - use this interface for IP address\n" - " -l - bind tcp/udp servers to interface only (not 0.0.0.0)\n" - " -s - port shifter (default: %d, when root: 0)\n" - " -c - ignore CTRL-C (send it via Serial)\n" - " -f - no throttle (possibly 100%%CPU)\n" - " -b - blocking tty/mocked-uart (default: not blocking tty)\n" - " -S - spiffs size in KBytes (default: %zd)\n" - " -L - littlefs size in KBytes (default: %zd)\n" + "\t-h\n" + "\tnetwork:\n" + "\t-i - use this interface for IP address\n" + "\t-l - bind tcp/udp servers to interface only (not 0.0.0.0)\n" + "\t-s - port shifter (default: %d, when root: 0)\n" + "\tterminal:\n" + "\t-b - blocking tty/mocked-uart (default: not blocking tty)\n" + "\t-T - show timestamp on output\n" + "\tFS:\n" + "\t-P - path for fs-persistent files (default: %s-)\n" + "\t-S - spiffs size in KBytes (default: %zd)\n" + "\t-L - littlefs size in KBytes (default: %zd)\n" "\t (spiffs, littlefs: negative value will force mismatched size)\n" - " -T - show timestamp on output\n" - " -v - verbose\n" - , argv0, MOCK_PORT_SHIFTER, spiffs_kb, littlefs_kb); + "\tgeneral:\n" + "\t-c - ignore CTRL-C (send it via Serial)\n" + "\t-f - no throttle (possibly 100%%CPU)\n" + "\t-v - verbose\n" + , argv0, MOCK_PORT_SHIFTER, argv0, spiffs_kb, littlefs_kb); exit(exitcode); } @@ -146,6 +152,7 @@ static struct option options[] = { "verbose", no_argument, NULL, 'v' }, { "timestamp", no_argument, NULL, 'T' }, { "interface", required_argument, NULL, 'i' }, + { "fspath", required_argument, NULL, 'P' }, { "spiffskb", required_argument, NULL, 'S' }, { "littlefskb", required_argument, NULL, 'L' }, { "portshifter", required_argument, NULL, 's' }, @@ -158,6 +165,23 @@ void cleanup () mock_stop_uart(); } +void make_fs_filename (String& name, const char* fspath, const char* argv0) +{ + name.clear(); + if (fspath) + { + int lastSlash = -1; + for (int i = 0; argv0[i]; i++) + if (argv0[i] == '/') + lastSlash = i; + name = fspath; + name += '/'; + name += &argv0[lastSlash + 1]; + } + else + name = argv0; +} + void control_c (int sig) { (void)sig; @@ -177,6 +201,7 @@ int main (int argc, char* const argv []) blocking_uart = false; // global signal(SIGINT, control_c); + signal(SIGTERM, control_c); if (geteuid() == 0) mock_port_shifter = 0; else @@ -184,7 +209,7 @@ int main (int argc, char* const argv []) for (;;) { - int n = getopt_long(argc, argv, "hlcfbvTi:S:s:L:", options, NULL); + int n = getopt_long(argc, argv, "hlcfbvTi:S:s:L:P:", options, NULL); if (n < 0) break; switch (n) @@ -213,6 +238,9 @@ int main (int argc, char* const argv []) case 'L': littlefs_kb = atoi(optarg); break; + case 'P': + fspath = optarg; + break; case 'b': blocking_uart = true; break; @@ -231,7 +259,8 @@ int main (int argc, char* const argv []) if (spiffs_kb) { - String name = argv[0]; + String name; + make_fs_filename(name, fspath, argv[0]); name += "-spiffs"; name += String(spiffs_kb > 0? spiffs_kb: -spiffs_kb, DEC); name += "KB"; @@ -240,7 +269,8 @@ int main (int argc, char* const argv []) if (littlefs_kb) { - String name = argv[0]; + String name; + make_fs_filename(name, fspath, argv[0]); name += "-littlefs"; name += String(littlefs_kb > 0? littlefs_kb: -littlefs_kb, DEC); name += "KB"; diff --git a/tests/host/common/UdpContextSocket.cpp b/tests/host/common/UdpContextSocket.cpp index f9cedbb7b..4212bb0ee 100644 --- a/tests/host/common/UdpContextSocket.cpp +++ b/tests/host/common/UdpContextSocket.cpp @@ -90,8 +90,8 @@ bool mockUDPListen (int sock, uint32_t dstaddr, uint16_t port, uint32_t mcast) else mockverbose("UDP server on port %d (sock=%d)\n", mockport, sock); - if (!mcast) - mcast = inet_addr("224.0.0.1"); // all hosts group + if (!mcast) + mcast = inet_addr("224.0.0.1"); // all hosts group if (mcast) { // https://web.cs.wpi.edu/~claypool/courses/4514-B99/samples/multicast.c @@ -121,7 +121,7 @@ bool mockUDPListen (int sock, uint32_t dstaddr, uint16_t port, uint32_t mcast) return false; } else - mockverbose("joined multicast group addr %08lx\n", ntohl(mcast)); + mockverbose("joined multicast group addr %08lx\n", (long)ntohl(mcast)); } return true;