-
Notifications
You must be signed in to change notification settings - Fork 23
Delete lock file if contained pid is stale #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,6 +22,7 @@ | |
|
|
||
| #include <limits.h> | ||
| #include <sys/ioctl.h> | ||
| #include <errno.h> | ||
| #include <arpa/telnet.h> | ||
|
|
||
| #include "microcom.h" | ||
|
|
@@ -177,29 +178,52 @@ struct ios_ops * serial_init(char *device) | |
| exit(1); | ||
| } | ||
|
|
||
| fd = open(lockfile, O_RDONLY); | ||
| if (fd >= 0 && !opt_force) { | ||
| close(fd); | ||
| main_usage(3, "lockfile for port exists", device); | ||
| } | ||
| relock: | ||
| fd = open(lockfile, O_RDWR | O_CREAT | O_EXCL, 0444); | ||
| if (fd < 0) { | ||
| if (errno == EEXIST) { | ||
| char pidbuf[12]; | ||
| ssize_t nbytes = 0; | ||
| if (opt_force) { | ||
| printf("lockfile for port exists, ignoring\n"); | ||
| serial_unlock(); | ||
| goto relock; | ||
| } | ||
|
|
||
| fd = open(lockfile, O_RDONLY); | ||
| if (fd < 0) | ||
| main_usage(3, "lockfile for port can't be opened", device); | ||
|
|
||
| do { | ||
| ret = read(fd, &pidbuf[nbytes], sizeof(pidbuf) - nbytes - 1); | ||
| nbytes += ret; | ||
| } while (ret > 0 && nbytes < sizeof (pidbuf) - 1); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it make sense here to do some additional checks on the format? E.g. check you read the file completely? If the lockfile happens to be created by an older microcom with pid 33333333 you check for pid 3333 instead. (I think, didn't test.) |
||
|
|
||
| if (ret >= 0) { | ||
| pidbuf[nbytes] = '\0'; | ||
| ret = sscanf(pidbuf, "%10ld\n", &pid); | ||
|
|
||
| if (ret == 1 && kill(pid, 0) < 0 && errno == ESRCH) { | ||
| printf("lockfile contains stale pid, ignoring\n"); | ||
| serial_unlock(); | ||
| goto relock; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same issue as with the first patch in this series (i.e. endless loop if serial_unlock fails to remove lockfile) |
||
| } | ||
| } | ||
|
|
||
| main_usage(3, "lockfile for port exists", device); | ||
| } | ||
|
|
||
| if (opt_force) { | ||
| printf("cannot create lockfile. ignoring\n"); | ||
| lockfile = NULL; | ||
| goto force; | ||
| } | ||
|
|
||
| if (fd >= 0 && opt_force) { | ||
| close(fd); | ||
| printf("lockfile for port exists, ignoring\n"); | ||
| serial_unlock(); | ||
| main_usage(3, "cannot create lockfile", device); | ||
| } | ||
|
|
||
| fd = open(lockfile, O_RDWR | O_CREAT, 0444); | ||
| if (fd < 0 && opt_force) { | ||
| printf("cannot create lockfile. ignoring\n"); | ||
| lockfile = NULL; | ||
| goto force; | ||
| } | ||
| if (fd < 0) | ||
| main_usage(3, "cannot create lockfile", device); | ||
| /* Kermit wants binary pid */ | ||
| pid = getpid(); | ||
| write(fd, &pid, sizeof(long)); | ||
| dprintf(fd, "%10ld\n", (long)pid); | ||
| close(fd); | ||
| force: | ||
| /* open the device */ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if
serial_unlock()fails to delete the lockfile, this is an endless loop.