From: Michal Sojka Date: Wed, 5 Feb 2014 17:23:23 +0000 (+0100) Subject: sterm: Detect stale lock files X-Git-Tag: fix-allnoconfig~36 X-Git-Url: http://rtime.felk.cvut.cz/gitweb/can-benchmark.git/commitdiff_plain/5edad1549c82ef1f634b9d92052b4a3138f39469 sterm: Detect stale lock files --- 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);