Skip to content

Commit cd4c31c

Browse files
committed
Delete lock file if contained pid is stale
Fixes #2. Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
1 parent 7431b86 commit cd4c31c

File tree

1 file changed

+13
-8
lines changed

1 file changed

+13
-8
lines changed

serial.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
#include <limits.h>
2424
#include <sys/ioctl.h>
25+
#include <signal.h>
26+
#include <errno.h>
2527
#include <arpa/telnet.h>
2628

2729
#include "microcom.h"
@@ -178,15 +180,18 @@ struct ios_ops * serial_init(char *device)
178180
}
179181

180182
fd = open(lockfile, O_RDONLY);
181-
if (fd >= 0 && !opt_force) {
183+
if (fd >= 0) {
184+
read(fd, &pid, sizeof(long));
182185
close(fd);
183-
main_usage(3, "lockfile for port exists", device);
184-
}
185-
186-
if (fd >= 0 && opt_force) {
187-
close(fd);
188-
printf("lockfile for port exists, ignoring\n");
189-
serial_unlock();
186+
if (kill(pid, 0) < 0 && errno == ESRCH) {
187+
printf("lockfile contains stale pid, ignoring\n");
188+
serial_unlock();
189+
} else if (opt_force) {
190+
printf("lockfile for port exists, ignoring\n");
191+
serial_unlock();
192+
} else {
193+
main_usage(3, "lockfile for port exists", device);
194+
}
190195
}
191196

192197
fd = open(lockfile, O_RDWR | O_CREAT, 0444);

0 commit comments

Comments
 (0)