static const char *run_base;
static const char *run_base_mkdir_p;
-static const char header_magic[4] = "PFI\n";
-
void common_diee(const char *m) { diee("%s", m); }
void common_die (const char *m) { die ("%s", m); }
}
}
+bool stabs_same_inode(struct stat *a, struct stat *b) {
+ return (a->st_dev == b->st_dev &&
+ a->st_ino == b->st_ino);
+}
+
bool find_run_base_var_run(void) {
struct stat stab;
char *try;
int acquire_lock(void) {
int r;
int lockfd = -1;
+ struct stat stab_fd;
+ struct stat stab_path;
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);
+ for (;;) {
+ if (lockfd >= 0) { close(lockfd); lockfd = -1; }
+
+ 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);
+ r = flock(lockfd, LOCK_EX);
+ if (r && errno == EINTR) continue;
+ if (r) diee("lock lock (%s)", lock_path);
+
+ r = fstat(lockfd, &stab_fd);
+ if (r) diee("fstat locked lock");
+
+ r = stat(lock_path, &stab_path);
+ if (!r) {
+ if (stabs_same_inode(&stab_path, &stab_fd)) break;
+ } else {
+ if (!(errno == ENOENT)) diee("re-stat locked lock (%s)", lock_path);
+ }
+ }
return lockfd;
}