chiark / gitweb /
prefork: split off lock_path etc.
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 9 Jul 2022 13:59:43 +0000 (14:59 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 21 Aug 2022 20:21:10 +0000 (21:21 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
cprogs/prefork.c
cprogs/prefork.h

index 004b5d346ffebad2f310f233181e90ff46a97609..5b6c75ba7434ad71ae74a750bf2b43b03aff00af 100644 (file)
@@ -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<ident> 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<ident> 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) {
index 4cde7770ba077348e22ddb5cfbd0fb0b8e11c04f..ef78418c18862aa5c686b5e96cefc195310829b3 100644 (file)
@@ -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);