Change -F to not use opendisk().

Don't change default to the boot volume until after we've found we aren't
  doing -F.
For tool-fdisk enforce -F and use #defines to make code that can no
  longer possibly execute compile.
This commit is contained in:
dsl 2007-11-29 23:02:00 +00:00
parent 0b8a0d143c
commit 0d1f6529aa

View File

@ -1,4 +1,4 @@
/* $NetBSD: fdisk.c,v 1.108 2007/11/18 19:19:00 apb Exp $ */ /* $NetBSD: fdisk.c,v 1.109 2007/11/29 23:02:00 dsl Exp $ */
/* /*
* Mach Operating System * Mach Operating System
@ -39,24 +39,13 @@
#include <sys/cdefs.h> #include <sys/cdefs.h>
#ifndef lint #ifndef lint
__RCSID("$NetBSD: fdisk.c,v 1.108 2007/11/18 19:19:00 apb Exp $"); __RCSID("$NetBSD: fdisk.c,v 1.109 2007/11/29 23:02:00 dsl Exp $");
#endif /* not lint */ #endif /* not lint */
#define MBRPTYPENAMES #define MBRPTYPENAMES
#include <sys/types.h> #include <sys/types.h>
#include <sys/param.h> #include <sys/param.h>
#include <sys/stat.h> #include <sys/stat.h>
#if HAVE_NBTOOL_CONFIG_H
#include <nbinclude/sys/disklabel.h>
#include <nbinclude/sys/bootblock.h>
#else
#include <sys/disklabel.h>
#include <sys/bootblock.h>
#include <sys/ioctl.h>
#include <sys/sysctl.h>
#endif /* HAVE_NBTOOL_CONFIG_H */
#include <ctype.h> #include <ctype.h>
#include <err.h> #include <err.h>
#include <errno.h> #include <errno.h>
@ -70,11 +59,28 @@ __RCSID("$NetBSD: fdisk.c,v 1.108 2007/11/18 19:19:00 apb Exp $");
#include <unistd.h> #include <unistd.h>
#include <vis.h> #include <vis.h>
#if HAVE_NBTOOL_CONFIG_H #if !HAVE_NBTOOL_CONFIG_H
#include "../../include/disktab.h" #include <sys/disklabel.h>
#else #include <sys/bootblock.h>
#include <sys/ioctl.h>
#include <sys/sysctl.h>
#include <disktab.h> #include <disktab.h>
#include <util.h> #include <util.h>
#else
#include <nbinclude/sys/disklabel.h>
#include <nbinclude/sys/bootblock.h>
#include "../../include/disktab.h"
/* We enforce -F, so none of these possibly undefined items can be needed */
#define opendisk(path, fl, buf, buflen, cooked) (-1)
#ifndef DIOCGDEFLABEL
#define DIOCGDEFLABEL 0
#endif
#ifndef DIOCGDINFO
#define DIOCGDINFO 0
#endif
#ifndef DIOCWLABEL
#define DIOCWLABEL 0
#endif
#endif /* HAVE_NBTOOL_CONFIG_H */ #endif /* HAVE_NBTOOL_CONFIG_H */
#define DEFAULT_BOOTDIR "/usr/mdec" #define DEFAULT_BOOTDIR "/usr/mdec"
@ -189,7 +195,13 @@ int b_flag; /* Set cyl, heads, secs (as c/h/s) */
int B_flag; /* Edit/install bootselect code */ int B_flag; /* Edit/install bootselect code */
int E_flag; /* extended partition number */ int E_flag; /* extended partition number */
int b_cyl, b_head, b_sec; /* b_flag values. */ int b_cyl, b_head, b_sec; /* b_flag values. */
#if !HAVE_NBTOOL_CONFIG_H
int F_flag = 0; int F_flag = 0;
#else
/* Tool - force 'file' mode to avoid unsupported functions and ioctls */
int F_flag = 1;
#endif
struct mbr_sector bootcode[8192 / sizeof (struct mbr_sector)]; struct mbr_sector bootcode[8192 / sizeof (struct mbr_sector)];
int bootsize; /* actual size of bootcode */ int bootsize; /* actual size of bootcode */
@ -247,10 +259,10 @@ daddr_t get_default_boot(void);
void set_default_boot(daddr_t); void set_default_boot(daddr_t);
#endif #endif
#if !HAVE_NBTOOL_CONFIG_H
static void static void
initvar_disk(const char **diskp) initvar_disk(const char **diskp)
{ {
#if !HAVE_NBTOOL_CONFIG_H
int mib[2]; int mib[2];
size_t len; size_t len;
char *root_device; char *root_device;
@ -263,8 +275,8 @@ initvar_disk(const char **diskp)
return; return;
*diskp = root_device; *diskp = root_device;
}
#endif /* HAVE_NBTOOL_CONFIG_H */ #endif /* HAVE_NBTOOL_CONFIG_H */
}
int int
main(int argc, char *argv[]) main(int argc, char *argv[])
@ -281,10 +293,6 @@ main(int argc, char *argv[])
int csysid, cstart, csize; /* For the b_flag. */ int csysid, cstart, csize; /* For the b_flag. */
#if !HAVE_NBTOOL_CONFIG_H
initvar_disk(&disk);
#endif /* HAVE_NBTOOL_CONFIG_H */
a_flag = i_flag = u_flag = sh_flag = f_flag = s_flag = b_flag = 0; a_flag = i_flag = u_flag = sh_flag = f_flag = s_flag = b_flag = 0;
v_flag = 0; v_flag = 0;
E_flag = 0; E_flag = 0;
@ -393,13 +401,6 @@ main(int argc, char *argv[])
argc -= optind; argc -= optind;
argv += optind; argv += optind;
#if HAVE_NBTOOL_CONFIG_H
if (disk_file == NULL && argc > 0)
disk_file = argv[0];
else if (disk_file == NULL)
usage();
#endif /* HAVE_NBTOOL_CONFIG_H */
if (disk_type != NULL && getdiskbyname(disk_type) == NULL) if (disk_type != NULL && getdiskbyname(disk_type) == NULL)
errx(EXIT_FAILURE, "bad disktype"); errx(EXIT_FAILURE, "bad disktype");
@ -421,8 +422,15 @@ main(int argc, char *argv[])
usage(); usage();
} }
if (argc > 1)
usage();
if (argc > 0) if (argc > 0)
disk = argv[0]; disk = argv[0];
else if (!F_flag) {
/* Default to boot device */
initvar_disk(&disk);
}
if (open_disk(B_flag || a_flag || i_flag || u_flag) < 0) if (open_disk(B_flag || a_flag || i_flag || u_flag) < 0)
exit(1); exit(1);
@ -516,11 +524,7 @@ usage(void)
"[-s id/start/size[/bootmenu]]] \\\n" "[-s id/start/size[/bootmenu]]] \\\n"
"%*s[-t disktab] [-T disktype] \\\n" "%*s[-t disktab] [-T disktype] \\\n"
"%*s[-c bootcode] " "%*s[-c bootcode] "
#if !HAVE_NBTOOL_CONFIG_H
"[-r|-w file] [device]\n" "[-r|-w file] [device]\n"
#else
"[-r|-w] file\n"
#endif /* HAVE_NBTOOL_CONFIG_H */
"\t-a change active partition\n" "\t-a change active partition\n"
"\t-f force - not interactive\n" "\t-f force - not interactive\n"
"\t-i initialise MBR code\n" "\t-i initialise MBR code\n"
@ -529,7 +533,8 @@ usage(void)
"\t-v verbose output, -v -v more verbose still\n" "\t-v verbose output, -v -v more verbose still\n"
"\t-B update bootselect options\n" "\t-B update bootselect options\n"
"\t-F treat device as a regular file\n" "\t-F treat device as a regular file\n"
"\t-S output as shell defines\n", "\t-S output as shell defines\n"
"\t-r and -w access 'file' for non-destructive testing\n",
getprogname(), indent, "", indent, "", indent, ""); getprogname(), indent, "", indent, "", indent, "");
exit(1); exit(1);
} }
@ -2260,25 +2265,26 @@ int
open_disk(int update) open_disk(int update)
{ {
static char namebuf[MAXPATHLEN + 1]; static char namebuf[MAXPATHLEN + 1];
int flags = update && disk_file == NULL ? O_RDWR : O_RDONLY;
#if HAVE_NBTOOL_CONFIG_H if (!F_flag) {
strlcpy(namebuf, disk_file, sizeof(namebuf)); fd = opendisk(disk, flags, namebuf, sizeof(namebuf), 0);
if ((fd = open(disk_file, update ? O_RDWR : O_RDONLY, 0)) == -1) {
warn("%s", disk_file);
return -1;
}
#else
fd = opendisk(disk, update && disk_file == NULL ? O_RDWR : O_RDONLY,
namebuf, sizeof(namebuf), 0);
if (fd < 0) { if (fd < 0) {
if (errno == ENODEV) if (errno == ENODEV)
warnx("%s is not a character device", namebuf); warnx("%s is not a character device", namebuf);
else else
warn("%s", namebuf); warn("cannot opendisk %s", namebuf);
return (-1); return (-1);
} }
#endif /* HAVE_NBTOOL_CONFIG_H */
disk = namebuf; disk = namebuf;
} else {
fd = open(disk, flags, 0);
if (fd == -1) {
warn("cannot open %s", disk);
return -1;
}
}
if (get_params() == -1) { if (get_params() == -1) {
close(fd); close(fd);
fd = -1; fd = -1;
@ -2359,12 +2365,6 @@ get_params(void)
disklabel.d_ncylinders = dos_cylinders; disklabel.d_ncylinders = dos_cylinders;
disklabel.d_ntracks = dos_heads; disklabel.d_ntracks = dos_heads;
disklabel.d_nsectors = dos_sectors; disklabel.d_nsectors = dos_sectors;
#if HAVE_NBTOOL_CONFIG_H
} else {
warnx("no disklabel specified");
return -1;
}
#else
} else if (ioctl(fd, DIOCGDEFLABEL, &disklabel) == -1) { } else if (ioctl(fd, DIOCGDEFLABEL, &disklabel) == -1) {
warn("DIOCGDEFLABEL"); warn("DIOCGDEFLABEL");
if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) { if (ioctl(fd, DIOCGDINFO, &disklabel) == -1) {
@ -2372,7 +2372,7 @@ get_params(void)
return (-1); return (-1);
} }
} }
#endif /* HAVE_NBTOOL_CONFIG_H */
disksectors = disklabel.d_secperunit; disksectors = disklabel.d_secperunit;
cylinders = disklabel.d_ncylinders; cylinders = disklabel.d_ncylinders;
heads = disklabel.d_ntracks; heads = disklabel.d_ntracks;
@ -2518,10 +2518,8 @@ write_mbr(void)
* sector 0. (e.g. empty disk) * sector 0. (e.g. empty disk)
*/ */
flag = 1; flag = 1;
#if !HAVE_NBTOOL_CONFIG_H
if (wfd == fd && F_flag == 0 && ioctl(wfd, DIOCWLABEL, &flag) < 0) if (wfd == fd && F_flag == 0 && ioctl(wfd, DIOCWLABEL, &flag) < 0)
warn("DIOCWLABEL"); warn("DIOCWLABEL");
#endif /* HAVE_NBTOOL_CONFIG_H */
if (write_disk(0, &mboot) == -1) { if (write_disk(0, &mboot) == -1) {
warn("Can't write fdisk partition table"); warn("Can't write fdisk partition table");
goto protect_label; goto protect_label;
@ -2541,11 +2539,9 @@ write_mbr(void)
} }
rval = 0; rval = 0;
protect_label: protect_label:
#if !HAVE_NBTOOL_CONFIG_H
flag = 0; flag = 0;
if (wfd == fd && F_flag == 0 && ioctl(wfd, DIOCWLABEL, &flag) < 0) if (wfd == fd && F_flag == 0 && ioctl(wfd, DIOCWLABEL, &flag) < 0)
warn("DIOCWLABEL"); warn("DIOCWLABEL");
#endif /* HAVE_NBTOOL_CONFIG_H */
return rval; return rval;
} }