tweaks: take just one shot at reading the lock file, and correct a type

Don't bother looping until the 1024 bytes are read.  Writing the lock
file takes just one shot too, and that is more important to get right.

Also, correct the type for the result of read(), so that -1 doesn't
get turned into a positive number, which would mean that any error
would get ignored.
master
Benno Schulenberg 2020-02-02 11:36:01 +01:00
parent 48df800db3
commit b6a1583e8e
1 changed files with 3 additions and 6 deletions

View File

@ -293,7 +293,7 @@ int do_lockfile(const char *filename, bool ask_the_user)
char *lockbuf, *question, *pidstring, *postedname, *promptstr; char *lockbuf, *question, *pidstring, *postedname, *promptstr;
static char lockprog[11], lockuser[17]; static char lockprog[11], lockuser[17];
int lockfd, lockpid, room, choice; int lockfd, lockpid, room, choice;
size_t readamt = 0, readtot = 0; ssize_t readamt;
if ((lockfd = open(lockfilename, O_RDONLY)) < 0) { if ((lockfd = open(lockfilename, O_RDONLY)) < 0) {
statusline(ALERT, _("Error opening lock file %s: %s"), statusline(ALERT, _("Error opening lock file %s: %s"),
@ -303,14 +303,11 @@ int do_lockfile(const char *filename, bool ask_the_user)
lockbuf = charalloc(LOCKSIZE); lockbuf = charalloc(LOCKSIZE);
do { readamt = read(lockfd, lockbuf, LOCKSIZE);
readamt = read(lockfd, &lockbuf[readtot], LOCKSIZE - readtot);
readtot += readamt;
} while (readamt > 0 && readtot < LOCKSIZE);
close(lockfd); close(lockfd);
if (readtot < LOCKSIZE || lockbuf[0] != 0x62 || lockbuf[1] != 0x30) { if (readamt < LOCKSIZE || lockbuf[0] != 0x62 || lockbuf[1] != 0x30) {
statusline(ALERT, _("Bad lock file is ignored: %s"), lockfilename); statusline(ALERT, _("Bad lock file is ignored: %s"), lockfilename);
free(lockbuf); free(lockbuf);
goto free_the_name; goto free_the_name;