From 5edad1549c82ef1f634b9d92052b4a3138f39469 Mon Sep 17 00:00:00 2001 From: Michal Sojka Date: Wed, 5 Feb 2014 18:23:23 +0100 Subject: [PATCH] sterm: Detect stale lock files --- utils/sterm.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/utils/sterm.c b/utils/sterm.c index a3e9786..e708a68 100644 --- a/utils/sterm.c +++ b/utils/sterm.c @@ -174,9 +174,21 @@ int main(int argc, char *argv[]) CHECK(write(tmp, pid, strlen(pid))); close(tmp); snprintf(lockfile, sizeof(lockfile), "/var/lock/LCK..%s", dev + 5); + retry: if (link(template, lockfile) == -1) { - perror(lockfile); - exit(1); + tmp = CHECK(open(lockfile, O_RDONLY)); + CHECK(read(tmp, pid, sizeof(pid))); + close(tmp); + int p = atoi(pid); + char proc[50]; + snprintf(proc, sizeof(proc), "/proc/%d", p); + if (access(proc, F_OK) == 0) { + fprintf(stderr, "%s is used by PID %d\n", dev, p); + exit(1); + } + fprintf(stderr, "Stale lock file %s (PID %d) - removing it!\n", lockfile, p); + CHECK(unlink(lockfile)); + goto retry; } rm_file(0, template); on_exit(rm_file, lockfile); -- 2.39.2