struct swap_header_v1_2 *hdr; /* swap header */
void *signature_page;/* buffer with swap header */
- char *device_name;
- int fd;
+ char *devname; /* device or file name */
+ struct stat devstat; /* stat() result */
+ int fd; /* swap file descriptor */
unsigned long long npages; /* number of pages */
unsigned long nbadpages; /* number of bad pages */
ctl->signature_page = NULL;
}
-static void
-write_signature(const struct mkswap_control *ctl)
+static void write_signature(const struct mkswap_control *ctl)
{
char *sp = (char *) ctl->signature_page;
+ assert(sp);
strncpy(sp + ctl->pagesize - SWAP_SIGNATURE_SZ, SWAP_SIGNATURE, SWAP_SIGNATURE_SZ);
}
-static void
-write_uuid_and_label(const struct mkswap_control *ctl)
+static void write_uuid_and_label(const struct mkswap_control *ctl)
{
assert(ctl);
assert(ctl->hdr);
}
/* return size in pages */
-static unsigned long long
-get_size(const struct mkswap_control *ctl)
+static unsigned long long get_size(const struct mkswap_control *ctl)
{
int fd;
unsigned long long size;
- fd = open(ctl->device_name, O_RDONLY);
+ fd = open(ctl->devname, O_RDONLY);
if (fd < 0)
- err(EXIT_FAILURE, _("cannot open %s"), ctl->device_name);
+ err(EXIT_FAILURE, _("cannot open %s"), ctl->devname);
if (blkdev_get_size(fd, &size) == 0)
size /= ctl->pagesize;
}
#ifdef HAVE_LIBBLKID
-static blkid_probe
-new_prober(const struct mkswap_control *ctl)
+static blkid_probe new_prober(const struct mkswap_control *ctl)
{
blkid_probe pr = blkid_new_probe();
if (!pr)
}
#endif
-static void
-wipe_device(struct mkswap_control *ctl)
+static void open_device(struct mkswap_control *ctl)
+{
+ assert(ctl);
+ assert(ctl->devname);
+
+ if (stat(ctl->devname, &ctl->devstat) < 0)
+ err(EXIT_FAILURE, _("stat failed %s"), ctl->devname);
+
+ if (S_ISBLK(ctl->devstat.st_mode))
+ ctl->fd = open(ctl->devname, O_RDWR | O_EXCL);
+ else {
+ if (ctl->check) {
+ ctl->check = 0;
+ warnx(_("warning: checking bad blocks from swap file is not supported: %s"),
+ ctl->devname);
+ }
+ ctl->fd = open(ctl->devname, O_RDWR);
+ }
+ if (ctl->fd < 0)
+ err(EXIT_FAILURE, _("cannot open %s"), ctl->devname);
+
+ if (S_ISBLK(ctl->devstat.st_mode))
+ if (blkdev_is_misaligned(ctl->fd))
+ warnx(_("warning: %s is misaligned"), ctl->devname);
+}
+
+static void wipe_device(struct mkswap_control *ctl)
{
char *type = NULL;
int zap = 1;
const char *data = NULL;
if (blkid_probe_lookup_value(pr, "TYPE", &data, NULL) == 0 && data)
- warnx(_("%s: warning: wiping old %s signature."), ctl->device_name, data);
+ warnx(_("%s: warning: wiping old %s signature."), ctl->devname, data);
blkid_do_wipe(pr, 0);
}
#endif
} else {
warnx(_("%s: warning: don't erase bootbits sectors"),
- ctl->device_name);
+ ctl->devname);
if (type)
fprintf(stderr, _(" (%s partition table detected). "), type);
else
int
main(int argc, char **argv) {
struct mkswap_control ctl = { .fd = -1 };
- struct stat statbuf;
int c;
unsigned long long goodpages;
unsigned long long sz;
}
}
if (optind < argc)
- ctl.device_name = argv[optind++];
+ ctl.devname = argv[optind++];
if (optind < argc)
block_count = argv[optind++];
if (optind != argc) {
init_signature_page(&ctl); /* get pagesize and allocate signature page */
- if (!ctl.device_name) {
+ if (!ctl.devname) {
warnx(_("error: Nowhere to set up swap on?"));
usage(stderr);
}
ctl.npages * ctl.pagesize / 1024);
}
- if (is_mounted(ctl.device_name))
+ if (is_mounted(ctl.devname))
errx(EXIT_FAILURE, _("error: "
"%s is mounted; will not make swapspace"),
- ctl.device_name);
+ ctl.devname);
+
+ open_device(&ctl);
- if (stat(ctl.device_name, &statbuf) < 0)
- err(EXIT_FAILURE, _("stat failed %s"), ctl.device_name);
- if (S_ISBLK(statbuf.st_mode))
- ctl.fd = open(ctl.device_name, O_RDWR | O_EXCL);
- else {
- if (ctl.check) {
- ctl.check = 0;
- warnx(_("warning: checking bad blocks from swap file is not supported: %s"),
- ctl.device_name);
- }
- ctl.fd = open(ctl.device_name, O_RDWR);
- }
- if (ctl.fd < 0)
- err(EXIT_FAILURE, _("cannot open %s"), ctl.device_name);
- if (S_ISBLK(statbuf.st_mode))
- if (blkdev_is_misaligned(ctl.fd))
- warnx(_("warning: %s is misaligned"), ctl.device_name);
if (ctl.check)
check_blocks(&ctl);
ctl.pagesize - offset) == -1)
err(EXIT_FAILURE,
_("%s: unable to write signature page"),
- ctl.device_name);
+ ctl.devname);
deinit_signature_page(&ctl);
#ifdef HAVE_LIBSELINUX
- if (S_ISREG(statbuf.st_mode) && is_selinux_enabled() > 0) {
+ if (S_ISREG(ctl.devstat.st_mode) && is_selinux_enabled() > 0) {
security_context_t context_string;
security_context_t oldcontext;
context_t newcontext;
if (errno != ENODATA)
err(EXIT_FAILURE,
_("%s: unable to obtain selinux file label"),
- ctl.device_name);
- if (matchpathcon(ctl.device_name, statbuf.st_mode, &oldcontext))
+ ctl.devname);
+ if (matchpathcon(ctl.devname, ctl.devstat.st_mode, &oldcontext))
errx(EXIT_FAILURE, _("unable to matchpathcon()"));
}
if (!(newcontext = context_new(oldcontext)))
if (strcmp(context_string, oldcontext)!=0) {
if (fsetfilecon(ctl.fd, context_string))
err(EXIT_FAILURE, _("unable to relabel %s to %s"),
- ctl.device_name, context_string);
+ ctl.devname, context_string);
}
context_free(newcontext);
freecon(oldcontext);