From 25ddb8082369451024f471bc2d77fa7e7207cc47 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 14 Aug 2019 13:15:35 +0930 Subject: [PATCH] ccan: update to fix shutdown slowness after plugin load. Dynamic plugins were keeping fds open; they should not have these at all anyway, but worse, they interfere with operation because we don't notice they're closed. The symptom was that shutdown of the test_plugin_slowinit and test_plugin_command was 30 seconds (10 seconds grace to kill each daemon). Signed-off-by: Rusty Russell --- ccan/README | 2 +- ccan/ccan/htable/htable.h | 9 +++++++++ ccan/ccan/htable/htable_type.h | 7 +++++++ ccan/ccan/htable/test/run-type.c | 7 +++++-- ccan/ccan/htable/test/run.c | 9 ++++++--- ccan/ccan/pipecmd/pipecmd.c | 7 +++++++ ccan/ccan/pipecmd/pipecmd.h | 3 ++- 7 files changed, 37 insertions(+), 7 deletions(-) diff --git a/ccan/README b/ccan/README index 72b737b65..ae497dc61 100644 --- a/ccan/README +++ b/ccan/README @@ -1,3 +1,3 @@ CCAN imported from http://ccodearchive.net. -CCAN version: init-2484-ge16aa40b +CCAN version: init-2486-g46cfc3ad diff --git a/ccan/ccan/htable/htable.h b/ccan/ccan/htable/htable.h index 938b43afc..0ecae726c 100644 --- a/ccan/ccan/htable/htable.h +++ b/ccan/ccan/htable/htable.h @@ -75,6 +75,15 @@ bool htable_init_sized(struct htable *ht, size_t (*rehash)(const void *elem, void *priv), void *priv, size_t size); +/** + * htable_count - count number of entries in a hash table. + * @ht: the hash table + */ +static inline size_t htable_count(const struct htable *ht) +{ + return ht->elems; +} + /** * htable_clear - empty a hash table. * @ht: the hash table to clear diff --git a/ccan/ccan/htable/htable_type.h b/ccan/ccan/htable/htable_type.h index 140116769..9dad4b392 100644 --- a/ccan/ccan/htable/htable_type.h +++ b/ccan/ccan/htable/htable_type.h @@ -25,6 +25,9 @@ * void _clear(struct *); * bool _copy(struct *dst, const struct *src); * + * Count entries: + * size_t _count(const struct *ht); + * * Add function only fails if we run out of memory: * bool _add(struct *ht, const *e); * @@ -69,6 +72,10 @@ { \ return htable_init_sized(&ht->raw, name##_hash, NULL, s); \ } \ + static inline UNNEEDED size_t name##_count(const struct name *ht) \ + { \ + return htable_count(&ht->raw); \ + } \ static inline UNNEEDED void name##_clear(struct name *ht) \ { \ htable_clear(&ht->raw); \ diff --git a/ccan/ccan/htable/test/run-type.c b/ccan/ccan/htable/test/run-type.c index f097acb69..2fc38c3ac 100644 --- a/ccan/ccan/htable/test/run-type.c +++ b/ccan/ccan/htable/test/run-type.c @@ -65,7 +65,7 @@ static void find_vals(const struct htable_obj *ht, return; } } - pass("Found %u numbers in hash", i); + ok1(htable_obj_count(ht) == i); } static void del_vals(struct htable_obj *ht, @@ -116,12 +116,13 @@ int main(void) void *p; struct htable_obj_iter iter; - plan_tests(29); + plan_tests(32); for (i = 0; i < NUM_VALS; i++) val[i].key = i; dne = i; htable_obj_init(&ht); + ok1(htable_obj_count(&ht) == 0); ok1(ht_max(&ht.raw) == 0); ok1(ht.raw.bits == 0); @@ -205,6 +206,8 @@ int main(void) } htable_obj_clear(&ht); + ok1(htable_obj_count(&ht) == 0); htable_obj_clear(&ht2); + ok1(htable_obj_count(&ht2) == 0); return exit_status(); } diff --git a/ccan/ccan/htable/test/run.c b/ccan/ccan/htable/test/run.c index 85502c4ab..3608941d9 100644 --- a/ccan/ccan/htable/test/run.c +++ b/ccan/ccan/htable/test/run.c @@ -67,7 +67,7 @@ static void find_vals(struct htable *ht, return; } } - pass("Found %llu numbers in hash", (long long)i); + ok1(htable_count(ht) == i); } static void del_vals(struct htable *ht, @@ -105,12 +105,13 @@ int main(void) void *p; struct htable_iter iter; - plan_tests(36); + plan_tests(38); for (i = 0; i < NUM_VALS; i++) val[i] = i; dne = i; htable_init(&ht, hash, NULL); + ok1(htable_count(&ht) == 0); ok1(ht_max(&ht) == 0); ok1(ht.bits == 0); @@ -207,6 +208,8 @@ int main(void) ok1(htable_init_sized(&ht, hash, NULL, 1025)); ok1(ht_max(&ht) >= 1025); htable_clear(&ht); - + + ok1(htable_count(&ht) == 0); + return exit_status(); } diff --git a/ccan/ccan/pipecmd/pipecmd.c b/ccan/ccan/pipecmd/pipecmd.c index d45713b6f..afeaf5a0a 100644 --- a/ccan/ccan/pipecmd/pipecmd.c +++ b/ccan/ccan/pipecmd/pipecmd.c @@ -137,6 +137,13 @@ pid_t pipecmdarr(int *fd_tochild, int *fd_fromchild, int *fd_errfromchild, goto child_errno_fail; close(errfromchild[1]); } + + /* Make (fairly!) sure all other fds are closed. */ + int max = sysconf(_SC_OPEN_MAX); + for (int i = 3; i < max; i++) + if (i != execfail[1]) + close(i); + execvp(arr[0], arr); child_errno_fail: diff --git a/ccan/ccan/pipecmd/pipecmd.h b/ccan/ccan/pipecmd/pipecmd.h index 5bbaefc01..3c169ade3 100644 --- a/ccan/ccan/pipecmd/pipecmd.h +++ b/ccan/ccan/pipecmd/pipecmd.h @@ -20,7 +20,8 @@ * 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. + * The return value is the pid of the child, or -1. All other file-descriptors + * are closed in the child. */ pid_t pipecmd(int *infd, int *outfd, int *errfd, const char *cmd, ...);