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;
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);
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) {
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;
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);