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:
parent
0726ab2d54
commit
440ba4b011
10 changed files with 90 additions and 45 deletions
|
@ -439,36 +439,44 @@ grub_util_get_fd_size (grub_util_fd_t fd,
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
int
|
||||||
grub_util_fd_close (grub_util_fd_t fd)
|
grub_util_fd_close (grub_util_fd_t fd)
|
||||||
{
|
{
|
||||||
switch (fd->type)
|
switch (fd->type)
|
||||||
{
|
{
|
||||||
case GRUB_UTIL_FD_FILE:
|
case GRUB_UTIL_FD_FILE:
|
||||||
close (fd->fd);
|
return close (fd->fd);
|
||||||
return;
|
|
||||||
case GRUB_UTIL_FD_DISK:
|
case GRUB_UTIL_FD_DISK:
|
||||||
CloseDevice ((struct IORequest *) fd->ioreq);
|
CloseDevice ((struct IORequest *) fd->ioreq);
|
||||||
DeleteIORequest((struct IORequest *) fd->ioreq);
|
DeleteIORequest((struct IORequest *) fd->ioreq);
|
||||||
DeleteMsgPort (fd->mp);
|
DeleteMsgPort (fd->mp);
|
||||||
return;
|
return 0;
|
||||||
}
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int allow_fd_syncs = 1;
|
static int allow_fd_syncs = 1;
|
||||||
|
|
||||||
static void
|
static int
|
||||||
grub_util_fd_sync_volume (grub_util_fd_t fd)
|
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_Command = CMD_UPDATE;
|
||||||
fd->ioreq->iotd_Req.io_Length = 0;
|
fd->ioreq->iotd_Req.io_Length = 0;
|
||||||
fd->ioreq->iotd_Req.io_Data = 0;
|
fd->ioreq->iotd_Req.io_Data = 0;
|
||||||
fd->ioreq->iotd_Req.io_Offset = 0;
|
fd->ioreq->iotd_Req.io_Offset = 0;
|
||||||
fd->ioreq->iotd_Req.io_Actual = 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)
|
grub_util_fd_sync (grub_util_fd_t fd)
|
||||||
{
|
{
|
||||||
if (allow_fd_syncs)
|
if (allow_fd_syncs)
|
||||||
|
@ -476,22 +484,22 @@ grub_util_fd_sync (grub_util_fd_t fd)
|
||||||
switch (fd->type)
|
switch (fd->type)
|
||||||
{
|
{
|
||||||
case GRUB_UTIL_FD_FILE:
|
case GRUB_UTIL_FD_FILE:
|
||||||
fsync (fd->fd);
|
return fsync (fd->fd);
|
||||||
return;
|
|
||||||
case GRUB_UTIL_FD_DISK:
|
case GRUB_UTIL_FD_DISK:
|
||||||
grub_util_fd_sync_volume (fd);
|
return grub_util_fd_sync_volume (fd);
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
int
|
||||||
grub_util_file_sync (FILE *f)
|
grub_util_file_sync (FILE *f)
|
||||||
{
|
{
|
||||||
fflush (f);
|
if (fflush (f) != 0)
|
||||||
|
return -1;
|
||||||
if (!allow_fd_syncs)
|
if (!allow_fd_syncs)
|
||||||
return;
|
return 0;
|
||||||
fsync (fileno (f));
|
return fsync (fileno (f));
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
|
|
@ -177,20 +177,22 @@ grub_util_fd_strerror (void)
|
||||||
|
|
||||||
static int allow_fd_syncs = 1;
|
static int allow_fd_syncs = 1;
|
||||||
|
|
||||||
void
|
int
|
||||||
grub_util_fd_sync (grub_util_fd_t fd)
|
grub_util_fd_sync (grub_util_fd_t fd)
|
||||||
{
|
{
|
||||||
if (allow_fd_syncs)
|
if (allow_fd_syncs)
|
||||||
fsync (fd);
|
return fsync (fd);
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
int
|
||||||
grub_util_file_sync (FILE *f)
|
grub_util_file_sync (FILE *f)
|
||||||
{
|
{
|
||||||
fflush (f);
|
if (fflush (f) != 0)
|
||||||
|
return -1;
|
||||||
if (!allow_fd_syncs)
|
if (!allow_fd_syncs)
|
||||||
return;
|
return 0;
|
||||||
fsync (fileno (f));
|
return fsync (fileno (f));
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@ -199,10 +201,10 @@ grub_util_disable_fd_syncs (void)
|
||||||
allow_fd_syncs = 0;
|
allow_fd_syncs = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
int
|
||||||
grub_util_fd_close (grub_util_fd_t fd)
|
grub_util_fd_close (grub_util_fd_t fd)
|
||||||
{
|
{
|
||||||
close (fd);
|
return close (fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
char *
|
char *
|
||||||
|
|
|
@ -275,11 +275,18 @@ grub_util_fd_write (grub_util_fd_t fd, const char *buf, size_t len)
|
||||||
|
|
||||||
static int allow_fd_syncs = 1;
|
static int allow_fd_syncs = 1;
|
||||||
|
|
||||||
void
|
int
|
||||||
grub_util_fd_sync (grub_util_fd_t fd)
|
grub_util_fd_sync (grub_util_fd_t fd)
|
||||||
{
|
{
|
||||||
if (allow_fd_syncs)
|
if (allow_fd_syncs)
|
||||||
FlushFileBuffers (fd);
|
{
|
||||||
|
if (!FlushFileBuffers (fd))
|
||||||
|
{
|
||||||
|
grub_util_info ("flush err %x", (int) GetLastError ());
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
|
@ -288,10 +295,15 @@ grub_util_disable_fd_syncs (void)
|
||||||
allow_fd_syncs = 0;
|
allow_fd_syncs = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
int
|
||||||
grub_util_fd_close (grub_util_fd_t fd)
|
grub_util_fd_close (grub_util_fd_t fd)
|
||||||
{
|
{
|
||||||
CloseHandle (fd);
|
if (!CloseHandle (fd))
|
||||||
|
{
|
||||||
|
grub_util_info ("close err %x", (int) GetLastError ());
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
const char *
|
const char *
|
||||||
|
@ -620,16 +632,25 @@ grub_util_fopen (const char *path, const char *mode)
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
int
|
||||||
grub_util_file_sync (FILE *f)
|
grub_util_file_sync (FILE *f)
|
||||||
{
|
{
|
||||||
HANDLE hnd;
|
HANDLE hnd;
|
||||||
|
|
||||||
fflush (f);
|
if (fflush (f) != 0)
|
||||||
|
{
|
||||||
|
grub_util_info ("fflush err %x", (int) GetLastError ());
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
if (!allow_fd_syncs)
|
if (!allow_fd_syncs)
|
||||||
return;
|
return 0;
|
||||||
hnd = (HANDLE) _get_osfhandle (fileno (f));
|
hnd = (HANDLE) _get_osfhandle (fileno (f));
|
||||||
FlushFileBuffers (hnd);
|
if (!FlushFileBuffers (hnd))
|
||||||
|
{
|
||||||
|
grub_util_info ("flush err %x", (int) GetLastError ());
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
|
|
|
@ -47,11 +47,11 @@ grub_util_fd_t
|
||||||
EXPORT_FUNC(grub_util_fd_open) (const char *os_dev, int flags);
|
EXPORT_FUNC(grub_util_fd_open) (const char *os_dev, int flags);
|
||||||
const char *
|
const char *
|
||||||
EXPORT_FUNC(grub_util_fd_strerror) (void);
|
EXPORT_FUNC(grub_util_fd_strerror) (void);
|
||||||
void
|
int
|
||||||
grub_util_fd_sync (grub_util_fd_t fd);
|
grub_util_fd_sync (grub_util_fd_t fd);
|
||||||
void
|
void
|
||||||
grub_util_disable_fd_syncs (void);
|
grub_util_disable_fd_syncs (void);
|
||||||
void
|
int
|
||||||
EXPORT_FUNC(grub_util_fd_close) (grub_util_fd_t fd);
|
EXPORT_FUNC(grub_util_fd_close) (grub_util_fd_t fd);
|
||||||
|
|
||||||
grub_uint64_t
|
grub_uint64_t
|
||||||
|
|
|
@ -73,6 +73,6 @@ FILE *
|
||||||
grub_util_fopen (const char *path, const char *mode);
|
grub_util_fopen (const char *path, const char *mode);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void grub_util_file_sync (FILE *f);
|
int grub_util_file_sync (FILE *f);
|
||||||
|
|
||||||
#endif /* GRUB_EMU_MISC_H */
|
#endif /* GRUB_EMU_MISC_H */
|
||||||
|
|
|
@ -55,7 +55,8 @@ grub_util_create_envblk_file (const char *name)
|
||||||
strerror (errno));
|
strerror (errno));
|
||||||
|
|
||||||
|
|
||||||
grub_util_file_sync (fp);
|
if (grub_util_file_sync (fp) < 0)
|
||||||
|
grub_util_error (_("cannot sync `%s': %s"), namenew, strerror (errno));
|
||||||
free (buf);
|
free (buf);
|
||||||
fclose (fp);
|
fclose (fp);
|
||||||
|
|
||||||
|
|
|
@ -197,7 +197,8 @@ write_envblk (const char *name, grub_envblk_t envblk)
|
||||||
grub_util_error (_("cannot write to `%s': %s"), name,
|
grub_util_error (_("cannot write to `%s': %s"), name,
|
||||||
strerror (errno));
|
strerror (errno));
|
||||||
|
|
||||||
grub_util_file_sync (fp);
|
if (grub_util_file_sync (fp) < 0)
|
||||||
|
grub_util_error (_("cannot sync `%s': %s"), name, strerror (errno));
|
||||||
fclose (fp);
|
fclose (fp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -112,11 +112,16 @@ grub_install_copy_file (const char *src,
|
||||||
r = grub_util_fd_read (in, grub_install_copy_buffer, GRUB_INSTALL_COPY_BUFFER_SIZE);
|
r = grub_util_fd_read (in, grub_install_copy_buffer, GRUB_INSTALL_COPY_BUFFER_SIZE);
|
||||||
if (r <= 0)
|
if (r <= 0)
|
||||||
break;
|
break;
|
||||||
grub_util_fd_write (out, grub_install_copy_buffer, r);
|
r = grub_util_fd_write (out, grub_install_copy_buffer, r);
|
||||||
|
if (r <= 0)
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
grub_util_fd_sync (out);
|
if (grub_util_fd_sync (out) < 0)
|
||||||
grub_util_fd_close (in);
|
r = -1;
|
||||||
grub_util_fd_close (out);
|
if (grub_util_fd_close (in) < 0)
|
||||||
|
r = -1;
|
||||||
|
if (grub_util_fd_close (out) < 0)
|
||||||
|
r = -1;
|
||||||
|
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
grub_util_error (_("cannot copy `%s' to `%s': %s"),
|
grub_util_error (_("cannot copy `%s' to `%s': %s"),
|
||||||
|
@ -526,7 +531,8 @@ grub_install_make_image_wrap (const char *dir, const char *prefix,
|
||||||
grub_install_make_image_wrap_file (dir, prefix, fp, outname,
|
grub_install_make_image_wrap_file (dir, prefix, fp, outname,
|
||||||
memdisk_path, config_path,
|
memdisk_path, config_path,
|
||||||
mkimage_target, note);
|
mkimage_target, note);
|
||||||
grub_util_file_sync (fp);
|
if (grub_util_file_sync (fp) < 0)
|
||||||
|
grub_util_error (_("cannot sync `%s': %s"), outname, strerror (errno));
|
||||||
fclose (fp);
|
fclose (fp);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -311,8 +311,12 @@ main (int argc, char *argv[])
|
||||||
arguments.image_target, arguments.note,
|
arguments.image_target, arguments.note,
|
||||||
arguments.comp, arguments.dtb);
|
arguments.comp, arguments.dtb);
|
||||||
|
|
||||||
grub_util_file_sync (fp);
|
if (grub_util_file_sync (fp) < 0)
|
||||||
fclose (fp);
|
grub_util_error (_("cannot sync `%s': %s"), arguments.output ? : "stdout",
|
||||||
|
strerror (errno));
|
||||||
|
if (fclose (fp) == EOF)
|
||||||
|
grub_util_error (_("cannot close `%s': %s"), arguments.output ? : "stdout",
|
||||||
|
strerror (errno));
|
||||||
|
|
||||||
for (i = 0; i < arguments.nmodules; i++)
|
for (i = 0; i < arguments.nmodules; i++)
|
||||||
free (arguments.modules[i]);
|
free (arguments.modules[i]);
|
||||||
|
|
|
@ -728,8 +728,10 @@ unable_to_embed:
|
||||||
!= GRUB_DISK_SECTOR_SIZE * 2)
|
!= GRUB_DISK_SECTOR_SIZE * 2)
|
||||||
grub_util_error (_("cannot write to `%s': %s"),
|
grub_util_error (_("cannot write to `%s': %s"),
|
||||||
core_path, strerror (errno));
|
core_path, strerror (errno));
|
||||||
grub_util_fd_sync (fp);
|
if (grub_util_fd_sync (fp) < 0)
|
||||||
grub_util_fd_close (fp);
|
grub_util_error (_("cannot sync `%s': %s"), core_path, strerror (errno));
|
||||||
|
if (grub_util_fd_close (fp) < 0)
|
||||||
|
grub_util_error (_("cannot close `%s': %s"), core_path, strerror (errno));
|
||||||
grub_util_biosdisk_flush (root_dev->disk);
|
grub_util_biosdisk_flush (root_dev->disk);
|
||||||
|
|
||||||
grub_disk_cache_invalidate_all ();
|
grub_disk_cache_invalidate_all ();
|
||||||
|
|
Loading…
Reference in a new issue