Merge "Lose mkswap, swapoff, and swapon to toybox."

This commit is contained in:
Elliott Hughes 2014-11-25 00:27:12 +00:00 committed by Gerrit Code Review
commit 077ff30954
4 changed files with 0 additions and 180 deletions

View file

@ -138,7 +138,6 @@ OUR_TOOLS := \
md5 \
mkdir \
mknod \
mkswap \
mount \
nandread \
netstat \
@ -159,8 +158,6 @@ OUR_TOOLS := \
smd \
start \
stop \
swapoff \
swapon \
top \
touch \
umount \

View file

@ -1,91 +0,0 @@
#include <fcntl.h>
#include <linux/fs.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/swap.h>
#include <sys/types.h>
/* This is not in a uapi header. */
struct linux_swap_header {
char bootbits[1024]; /* Space for disklabel etc. */
uint32_t version;
uint32_t last_page;
uint32_t nr_badpages;
unsigned char sws_uuid[16];
unsigned char sws_volume[16];
uint32_t padding[117];
uint32_t badpages[1];
};
#define MAGIC_SWAP_HEADER "SWAPSPACE2"
#define MAGIC_SWAP_HEADER_LEN 10
#define MIN_PAGES 10
int mkswap_main(int argc, char **argv)
{
if (argc != 2) {
fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
return EXIT_FAILURE;
}
int fd = open(argv[1], O_RDWR);
if (fd < 0) {
fprintf(stderr, "Cannot open %s: %s\n", argv[1], strerror(errno));
return EXIT_FAILURE;
}
/* Determine the length of the swap file */
off64_t swap_size;
struct stat sb;
if (fstat(fd, &sb)) {
fprintf(stderr, "Couldn't fstat file: %s\n", strerror(errno));
return EXIT_FAILURE;
}
if (S_ISBLK(sb.st_mode)) {
if (ioctl(fd, BLKGETSIZE64, &swap_size) < 0) {
fprintf(stderr, "Couldn't determine block device size: %s\n", strerror(errno));
return EXIT_FAILURE;
}
} else {
swap_size = sb.st_size;
}
int pagesize = getpagesize();
if (swap_size < MIN_PAGES * pagesize) {
fprintf(stderr, "Swap file needs to be at least %d KiB\n", (MIN_PAGES * pagesize) >> 10);
return EXIT_FAILURE;
}
struct linux_swap_header sw_hdr;
memset(&sw_hdr, 0, sizeof(sw_hdr));
sw_hdr.version = 1;
sw_hdr.last_page = (swap_size / pagesize) - 1;
ssize_t len = write(fd, &sw_hdr, sizeof(sw_hdr));
if (len != sizeof(sw_hdr)) {
fprintf(stderr, "Failed to write swap header into %s: %s\n", argv[1], strerror(errno));
return EXIT_FAILURE;
}
/* Write the magic header */
if (lseek(fd, pagesize - MAGIC_SWAP_HEADER_LEN, SEEK_SET) < 0) {
fprintf(stderr, "Failed to seek into %s: %s\n", argv[1], strerror(errno));
return EXIT_FAILURE;
}
len = write(fd, MAGIC_SWAP_HEADER, MAGIC_SWAP_HEADER_LEN);
if (len != MAGIC_SWAP_HEADER_LEN) {
fprintf(stderr, "Failed to write magic swap header into %s: %s\n", argv[1], strerror(errno));
return EXIT_FAILURE;
}
if (fsync(fd) < 0) {
fprintf(stderr, "Failed to sync %s: %s\n", argv[1], strerror(errno));
return EXIT_FAILURE;
}
close(fd);
return EXIT_SUCCESS;
}

View file

@ -1,20 +0,0 @@
#include <stdio.h>
#include <unistd.h>
#include <sys/swap.h>
int swapoff_main(int argc, char **argv)
{
int err = 0;
if (argc != 2) {
fprintf(stderr, "Usage: %s <filename>\n", argv[0]);
return -EINVAL;
}
err = swapoff(argv[1]);
if (err) {
fprintf(stderr, "swapoff failed for %s: %s\n", argv[1], strerror(errno));
}
return err;
}

View file

@ -1,66 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include <sys/swap.h>
static void usage(char *name)
{
fprintf(stderr, "Usage: %s [-p prio] <filename>\n"
" prio must be between 0 and %d\n", name, SWAP_FLAG_PRIO_MASK);
}
static int parse_prio(char *prio_str)
{
unsigned long p = strtoul(prio_str, NULL, 10);
return (p > SWAP_FLAG_PRIO_MASK)? -1 : (int)p;
}
int swapon_main(int argc, char **argv)
{
int err = 0;
int flags = 0;
int prio;
opterr = 0;
do {
int c = getopt(argc, argv, "hp:");
if (c == -1)
break;
switch (c) {
case 'p':
if (optarg != NULL)
prio = parse_prio(optarg);
else
prio = -1;
if (prio < 0) {
usage(argv[0]);
return -EINVAL;
}
flags |= SWAP_FLAG_PREFER;
flags |= (prio << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK;
break;
case 'h':
usage(argv[0]);
return 0;
case '?':
fprintf(stderr, "unknown option: %c\n", optopt);
return -EINVAL;
}
} while (1);
if (optind != argc - 1) {
usage(argv[0]);
return -EINVAL;
}
err = swapon(argv[argc - 1], flags);
if (err) {
fprintf(stderr, "swapon failed for %s: %s\n", argv[argc - 1], strerror(errno));
}
return err;
}