From ad6722d7ab7ad11a9d6c0e4e53369cbcbc08ed36 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 9 Jul 2022 14:59:43 +0100 Subject: [PATCH] prefork: split off lock_path etc. Signed-off-by: Ian Jackson --- cprogs/prefork.c | 26 ++++++++++++++++++-------- cprogs/prefork.h | 3 ++- 2 files changed, 20 insertions(+), 9 deletions(-) diff --git a/cprogs/prefork.c b/cprogs/prefork.c index 004b5d3..5b6c75b 100644 --- a/cprogs/prefork.c +++ b/cprogs/prefork.c @@ -9,7 +9,7 @@ int check_interval=300; struct sha256_ctx identsc; uid_t us; -const char *run_base, *script, *socket_path; +const char *run_base, *script, *socket_path, *lock_path; const char *run_base_mkdir_p; bool logging; @@ -256,15 +256,12 @@ bool check_garbage(void) { return check_garbage_vs(&sock_stab); } -void tidy_garbage(void) { - /* We lock l and re-check. The effect of this is that each - * stale socket is removed only once. So unless multiple updates to - * the script happen rapidly, we can't be racing with the cgi-fcgi - * (which is recreating the socket */ - int lockfd = -1; +// Returns fd +int acquire_lock(void) { int r; + int lockfd = -1; - const char *lock_path = m_asprintf("%s/l%s",run_base,ident); + lock_path = m_asprintf("%s/l%s",run_base,ident); lockfd = open(lock_path, O_CREAT|O_RDWR, 0600); if (lockfd<0) diee("create lock (%s)", lock_path); @@ -272,6 +269,19 @@ void tidy_garbage(void) { r = flock(lockfd, LOCK_EX); if (r) diee("lock lock (%s)", lock_path); + return lockfd; +} + +void tidy_garbage(void) { + /* We lock l and re-check. The effect of this is that each + * stale socket is removed only once. So unless multiple updates to + * the script happen rapidly, we can't be racing with the cgi-fcgi + * (which is recreating the socket */ + int lockfd = -1; + int r; + + lockfd = acquire_lock(); + if (check_garbage()) { r = unlink(socket_path); if (r) { diff --git a/cprogs/prefork.h b/cprogs/prefork.h index 4cde777..ef78418 100644 --- a/cprogs/prefork.h +++ b/cprogs/prefork.h @@ -42,7 +42,7 @@ extern int check_interval; extern struct sha256_ctx identsc; extern uid_t us; -extern const char *run_base, *script, *socket_path; +extern const char *run_base, *script, *socket_path, *lock_path; extern const char *run_base_mkdir_p; extern bool logging; @@ -51,6 +51,7 @@ void find_socket_path(void); bool stab_isnewer(const struct stat *a, const struct stat *b); void stab_mtimenow(struct stat *out); +int acquire_lock(void); bool check_garbage_vs(const struct stat *started); bool check_garbage(void); void tidy_garbage(void); -- 2.30.2