util: Detect more I/O errors

Many of GRUB's utilities don't check anywhere near all the possible
write errors.  For example, if grub-install runs out of space when
copying a file, it won't notice.  There were missing checks for the
return values of write, fflush, fsync, and close (or the equivalents on
other OSes), all of which must be checked.

I tried to be consistent with the existing logging practices of the
various hostdisk implementations, but they weren't entirely consistent
to start with so I used my judgement.  The result at least looks
reasonable on GNU/Linux when I provoke a write error:

  Installing for x86_64-efi platform.
  grub-install: error: cannot copy `/usr/lib/grub/x86_64-efi-signed/grubx64.efi.signed' to `/boot/efi/EFI/debian/grubx64.efi': No space left on device.

There are more missing checks in other utilities, but this should fix
the most critical ones.

Fixes Debian bug #922741.

Signed-off-by: Colin Watson <cjwatson@ubuntu.com>
Reviewed-by: Steve McIntyre <93sam@debian.org>
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
This commit is contained in:
Colin Watson 2019-02-27 09:10:08 +00:00 committed by Daniel Kiper
parent 222cb8f6df
commit 62daa27056
10 changed files with 90 additions and 45 deletions

View file

@ -439,36 +439,44 @@ grub_util_get_fd_size (grub_util_fd_t fd,
return -1;
}
void
int
grub_util_fd_close (grub_util_fd_t fd)
{
switch (fd->type)
{
case GRUB_UTIL_FD_FILE:
close (fd->fd);
return;
return close (fd->fd);
case GRUB_UTIL_FD_DISK:
CloseDevice ((struct IORequest *) fd->ioreq);
DeleteIORequest((struct IORequest *) fd->ioreq);
DeleteMsgPort (fd->mp);
return;
return 0;
}
return 0;
}
static int allow_fd_syncs = 1;
static void
static int
grub_util_fd_sync_volume (grub_util_fd_t fd)
{
LONG err;
fd->ioreq->iotd_Req.io_Command = CMD_UPDATE;
fd->ioreq->iotd_Req.io_Length = 0;
fd->ioreq->iotd_Req.io_Data = 0;
fd->ioreq->iotd_Req.io_Offset = 0;
fd->ioreq->iotd_Req.io_Actual = 0;
DoIO ((struct IORequest *) fd->ioreq);
err = DoIO ((struct IORequest *) fd->ioreq);
if (err)
{
grub_util_info ("I/O failed with error %d, IoErr=%d", (int)err, (int) IoErr ());
return -1;
}
return 0;
}
void
int
grub_util_fd_sync (grub_util_fd_t fd)
{
if (allow_fd_syncs)
@ -476,22 +484,22 @@ grub_util_fd_sync (grub_util_fd_t fd)
switch (fd->type)
{
case GRUB_UTIL_FD_FILE:
fsync (fd->fd);
return;
return fsync (fd->fd);
case GRUB_UTIL_FD_DISK:
grub_util_fd_sync_volume (fd);
return;
return grub_util_fd_sync_volume (fd);
}
}
return 0;
}
void
int
grub_util_file_sync (FILE *f)
{
fflush (f);
if (fflush (f) != 0)
return -1;
if (!allow_fd_syncs)
return;
fsync (fileno (f));
return 0;
return fsync (fileno (f));
}
void