summaryrefslogtreecommitdiff
path: root/sbin/fdisk/user.c
diff options
context:
space:
mode:
authorRay Lai <ray@cvs.openbsd.org>2006-07-27 04:06:14 +0000
committerRay Lai <ray@cvs.openbsd.org>2006-07-27 04:06:14 +0000
commit336c2dcb40bbb1fe30f92e79fbbd31123b292e55 (patch)
tree0fade1029a907c024795bfc826ef625619661fa5 /sbin/fdisk/user.c
parent3fcc141342739b3bffb3270bf5fe2e0088182201 (diff)
Add checks to each write(2) attempt and try to keep fdisk in a
consistent state upon write failure. OK krw@ and deraadt@.
Diffstat (limited to 'sbin/fdisk/user.c')
-rw-r--r--sbin/fdisk/user.c16
1 files changed, 13 insertions, 3 deletions
diff --git a/sbin/fdisk/user.c b/sbin/fdisk/user.c
index 3727ae4988d..8e952af5a5d 100644
--- a/sbin/fdisk/user.c
+++ b/sbin/fdisk/user.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: user.c,v 1.22 2005/01/19 15:48:20 deraadt Exp $ */
+/* $OpenBSD: user.c,v 1.23 2006/07/27 04:06:13 ray Exp $ */
/*
* Copyright (c) 1997 Tobias Weingartner
@@ -26,6 +26,7 @@
*/
#include <err.h>
+#include <errno.h>
#include <util.h>
#include <stdio.h>
#include <unistd.h>
@@ -89,7 +90,12 @@ USER_init(disk_t *disk, mbr_t *tt, int preserve)
if (yn) {
fd = DISK_open(disk->name, O_RDWR);
MBR_make(tt, mbr_buf);
- MBR_write(fd, (off_t)0, mbr_buf);
+ if (MBR_write(fd, 0, mbr_buf) == -1) {
+ int saved_errno = errno;
+ DISK_close(fd);
+ errno = saved_errno;
+ return (-1);
+ }
DISK_close(fd);
} else
printf("MBR is unchanged\n");
@@ -168,7 +174,11 @@ again:
printf("Writing current MBR to disk.\n");
fd = DISK_open(disk->name, O_RDWR);
MBR_make(&mbr, mbr_buf);
- MBR_write(fd, offset, mbr_buf);
+ if (MBR_write(fd, offset, mbr_buf) == -1) {
+ warn("error writing MBR");
+ close(fd);
+ goto again;
+ }
close(fd);
} else
printf("Aborting changes to current MBR.\n");