bool Splicer::check () const { bool pass = true; commentator.start ("Checking integrity of blocks in splicer", __FUNCTION__); pass = check_blocks (_horiz_blocks, "horizontal") && pass; pass = check_blocks (_vert_blocks, "vertical") && pass; commentator.stop (MSG_STATUS (pass)); return pass; }
int reflect_main(int argc, char *argv[]) { uint32_t total = 0; printf("Starting reflector\n"); allocate_blocks(); while (true) { char buf[128]; ssize_t n = read(0, buf, sizeof(buf)); if (n < 0) { break; } if (n > 0) { write(1, buf, n); } total += n; if (total > 1024000) { check_blocks(); total = 0; } } return OK; }
int main(int argc, char **argv) { int *lower_blocks, *upper_blocks; int lower_num_blocks, upper_num_blocks; int rc = EXIT_SUCCESS; if (argc != 3) { fprintf(stderr, "Usage: %s lower-file upper-file\n", argv[0]); exit(EXIT_FAILURE); } lower_blocks = get_blocks(argv[1], &lower_num_blocks); if (!lower_blocks) exit(EXIT_FAILURE); upper_blocks = get_blocks(argv[2], &upper_num_blocks); if (!upper_blocks) { free(lower_blocks); exit(EXIT_FAILURE); } rc = check_blocks(lower_blocks, lower_num_blocks, upper_blocks, upper_num_blocks); free(upper_blocks); free(lower_blocks); exit(rc); }
static gboolean fs_callback (SeafFSManager *mgr, const char *obj_id, int type, void *user_data, gboolean *stop) { if (type == SEAF_METADATA_TYPE_FILE && check_blocks (mgr, obj_id) < 0) return FALSE; return TRUE; }
static gboolean fs_callback (SeafFSManager *mgr, const char *store_id, int version, const char *obj_id, int type, void *user_data, gboolean *stop) { VerifyData *data = user_data; if (type == SEAF_METADATA_TYPE_FILE && check_blocks (data, obj_id) < 0) return FALSE; return TRUE; }
int main(int argc, char **argv) { struct stat statbuf; struct swap_header_v1_2 *hdr; int c; unsigned long long maxpages; unsigned long long goodpages; unsigned long long sz; off_t offset; int force = 0; int version = 1; char *block_count = 0; char *opt_label = NULL; unsigned char *uuid = NULL; #ifdef HAVE_LIBUUID const char *opt_uuid = NULL; uuid_t uuid_dat; #endif static const struct option longopts[] = { { "check", no_argument, 0, 'c' }, { "force", no_argument, 0, 'f' }, { "pagesize", required_argument, 0, 'p' }, { "label", required_argument, 0, 'L' }, { "swapversion", required_argument, 0, 'v' }, { "uuid", required_argument, 0, 'U' }, { "version", no_argument, 0, 'V' }, { "help", no_argument, 0, 'h' }, { NULL, 0, 0, 0 } }; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); while((c = getopt_long(argc, argv, "cfp:L:v:U:Vh", longopts, NULL)) != -1) { switch (c) { case 'c': check=1; break; case 'f': force=1; break; case 'p': user_pagesize = strtou32_or_err(optarg, _("parsing page size failed")); break; case 'L': opt_label = optarg; break; case 'v': version = strtos32_or_err(optarg, _("parsing version number failed")); break; case 'U': #ifdef HAVE_LIBUUID opt_uuid = optarg; #else warnx(_("warning: ignoring -U (UUIDs are unsupported by %s)"), program_invocation_short_name); #endif break; case 'V': printf(UTIL_LINUX_VERSION); exit(EXIT_SUCCESS); case 'h': usage(stdout); default: usage(stderr); } } if (optind < argc) device_name = argv[optind++]; if (optind < argc) block_count = argv[optind++]; if (optind != argc) { warnx(_("only one device argument is currently supported")); usage(stderr); } if (version != 1) errx(EXIT_FAILURE, _("swapspace version %d is not supported"), version); #ifdef HAVE_LIBUUID if(opt_uuid) { if (uuid_parse(opt_uuid, uuid_dat) != 0) errx(EXIT_FAILURE, _("error: parsing UUID failed")); } else uuid_generate(uuid_dat); uuid = uuid_dat; #endif init_signature_page(); /* get pagesize */ if (!device_name) { warnx(_("error: Nowhere to set up swap on?")); usage(stderr); } if (block_count) { /* this silly user specified the number of blocks explicitly */ uint64_t blks = strtou64_or_err(block_count, _("invalid block count argument")); PAGES = blks / (pagesize / 1024); } sz = get_size(device_name); if (!PAGES) PAGES = sz; else if (PAGES > sz && !force) { errx(EXIT_FAILURE, _("error: " "size %llu KiB is larger than device size %llu KiB"), PAGES*(pagesize/1024), sz*(pagesize/1024)); } if (PAGES < MIN_GOODPAGES) { warnx(_("error: swap area needs to be at least %ld KiB"), (long)(MIN_GOODPAGES * pagesize/1024)); usage(stderr); } #ifdef __linux__ if (get_linux_version() >= KERNEL_VERSION(2,3,4)) maxpages = UINT_MAX + 1ULL; else if (get_linux_version() >= KERNEL_VERSION(2,2,1)) maxpages = V1_MAX_PAGES; else #endif maxpages = V1_OLD_MAX_PAGES; if (PAGES > maxpages) { PAGES = maxpages; warnx(_("warning: truncating swap area to %llu KiB"), PAGES * pagesize / 1024); } if (is_mounted(device_name)) errx(EXIT_FAILURE, _("error: " "%s is mounted; will not make swapspace"), device_name); if (stat(device_name, &statbuf) < 0) { perror(device_name); exit(EXIT_FAILURE); } if (S_ISBLK(statbuf.st_mode)) DEV = open(device_name, O_RDWR | O_EXCL); else DEV = open(device_name, O_RDWR); if (DEV < 0) { perror(device_name); exit(EXIT_FAILURE); } if (!S_ISBLK(statbuf.st_mode)) check=0; else if (blkdev_is_misaligned(DEV)) warnx(_("warning: %s is misaligned"), device_name); if (check) check_blocks(); wipe_device(DEV, device_name, force); hdr = (struct swap_header_v1_2 *) signature_page; hdr->version = 1; hdr->last_page = PAGES - 1; hdr->nr_badpages = badpages; if (badpages > PAGES - MIN_GOODPAGES) errx(EXIT_FAILURE, _("Unable to set up swap-space: unreadable")); goodpages = PAGES - badpages - 1; printf(_("Setting up swapspace version 1, size = %llu KiB\n"), goodpages * pagesize / 1024); write_signature("SWAPSPACE2"); write_uuid_and_label(uuid, opt_label); offset = 1024; if (lseek(DEV, offset, SEEK_SET) != offset) errx(EXIT_FAILURE, _("unable to rewind swap-device")); if (write_all(DEV, (char *) signature_page + offset, pagesize - offset) == -1) err(EXIT_FAILURE, _("%s: unable to write signature page"), device_name); #ifdef HAVE_LIBSELINUX if (S_ISREG(statbuf.st_mode) && is_selinux_enabled() > 0) { security_context_t context_string; security_context_t oldcontext; context_t newcontext; if (fgetfilecon(DEV, &oldcontext) < 0) { if (errno != ENODATA) err(EXIT_FAILURE, _("%s: unable to obtain selinux file label"), device_name); if (matchpathcon(device_name, statbuf.st_mode, &oldcontext)) errx(EXIT_FAILURE, _("unable to matchpathcon()")); } if (!(newcontext = context_new(oldcontext))) errx(EXIT_FAILURE, _("unable to create new selinux context")); if (context_type_set(newcontext, SELINUX_SWAPFILE_TYPE)) errx(EXIT_FAILURE, _("couldn't compute selinux context")); context_string = context_str(newcontext); if (strcmp(context_string, oldcontext)!=0) { if (fsetfilecon(DEV, context_string)) err(EXIT_FAILURE, _("unable to relabel %s to %s"), device_name, context_string); } context_free(newcontext); freecon(oldcontext); } #endif /* * A subsequent swapon() will fail if the signature * is not actually on disk. (This is a kernel bug.) * The fsync() in close_fd() will take care of writing. */ if (close_fd(DEV) != 0) err(EXIT_FAILURE, _("write failed")); return EXIT_SUCCESS; }
int mkswap_main(int argc, char **argv) { char *tmp; struct stat statbuf; int sz; int maxpages; int goodpages; #ifdef __sparc__ int force = 0; #endif init_signature_page(); /* get pagesize */ bb_opt_complementally = "?"; /* call bb_show_usage internally */ sz = bb_getopt_ulflags(argc, argv, "+cfv:", &tmp); if (sz & 1) check = 1; #ifdef __sparc__ if (sz & 2) force = 1; #endif #if ENABLE_FEATURE_MKSWAP_V0 if (sz & 4) { version = bb_xgetlarg(tmp, 10, 0, 1); } else { if (get_linux_version_code() < KERNEL_VERSION(2, 1, 117)) version = 0; else version = 1; } #endif argv += optind; argc -= optind; goodpages = pagesize / 1024; /* cache division */ while (argc--) { if (device_name) { PAGES = bb_xgetlarg(argv[0], 0, 10, sz * goodpages) / goodpages; argc = 0; /* ignore any surplus args.. */ } else { device_name = argv[0]; sz = get_size(device_name); argv++; } } if (!device_name) { bb_error_msg_and_die("error: Nowhere to set up swap on?"); } if (!PAGES) { PAGES = sz; } #if 0 maxpages = ((version == 0) ? V0_MAX_PAGES : V1_MAX_PAGES); #else if (!version) maxpages = V0_MAX_PAGES; else if (get_linux_version_code() >= KERNEL_VERSION(2,2,1)) maxpages = V1_MAX_PAGES; else { maxpages = V1_OLD_MAX_PAGES; if (maxpages > V1_MAX_PAGES) maxpages = V1_MAX_PAGES; } #endif if (PAGES > maxpages) { PAGES = maxpages; bb_error_msg("warning: truncating swap area to %ldkB", PAGES * goodpages); } DEV = bb_xopen3(device_name, O_RDWR, 0); if (fstat(DEV, &statbuf) < 0) bb_perror_msg_and_die("%s", device_name); if (!S_ISBLK(statbuf.st_mode)) check = 0; else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340) bb_error_msg_and_die("Will not try to make swapdevice on '%s'", device_name); #ifdef __sparc__ if (!force && version == 0) { /* Don't overwrite partition table unless forced */ unsigned char *buffer = (unsigned char *) signature_page; unsigned short *q, sum; if (read(DEV, buffer, 512) != 512) bb_error_msg_and_die("fatal: first page unreadable"); if (buffer[508] == 0xDA && buffer[509] == 0xBE) { q = (unsigned short *) (buffer + 510); for (sum = 0; q >= (unsigned short *) buffer;) sum ^= *q--; if (!sum) { bb_error_msg("Device '%s' contains a valid Sun disklabel.\n" "This probably means creating v0 swap would destroy your partition table\n" "No swap created. If you really want to create swap v0 on that device, use\n" "the -f option to force it.", device_name); return EXIT_FAILURE; } } } #endif if (version == 0 || check) check_blocks(); if (version == 0 && !bit_test_and_clear(signature_page, 0)) bb_error_msg_and_die("fatal: first page unreadable"); if (version == 1) { p->swap_version = version; p->last_page = PAGES - 1; p->nr_badpages = badpages; } goodpages = PAGES - badpages - 1; if (goodpages <= 0) bb_error_msg_and_die("Unable to set up swap-space: unreadable"); printf("Setting up swapspace version %d, size = %ld bytes\n", version, (long) (goodpages * pagesize)); write_signature((version == 0) ? "SWAP-SPACE" : "SWAPSPACE2"); sz = ((version == 0) ? 0 : 1024); /* offset */ if (lseek(DEV, sz, SEEK_SET) != sz) bb_error_msg_and_die("unable to rewind swap-device"); goodpages = pagesize - sz; /* cache substraction */ if (write(DEV, (char *) signature_page + sz, goodpages) != goodpages) bb_error_msg_and_die("unable to write signature page"); /* * A subsequent swapon() will fail if the signature * is not actually on disk. (This is a kernel bug.) */ if (fsync(DEV)) bb_error_msg_and_die("fsync failed"); if (ENABLE_FEATURE_CLEAN_UP) { close(DEV); free(signature_page); } return EXIT_SUCCESS; }
int main(int argc, char **argv) { struct mkswap_control ctl = { .fd = -1 }; int c; uint64_t sz; int version = SWAP_VERSION; char *block_count = NULL, *strsz = NULL; #ifdef HAVE_LIBUUID const char *opt_uuid = NULL; uuid_t uuid_dat; #endif static const struct option longopts[] = { { "check", no_argument, 0, 'c' }, { "force", no_argument, 0, 'f' }, { "pagesize", required_argument, 0, 'p' }, { "label", required_argument, 0, 'L' }, { "swapversion", required_argument, 0, 'v' }, { "uuid", required_argument, 0, 'U' }, { "version", no_argument, 0, 'V' }, { "help", no_argument, 0, 'h' }, { NULL, 0, 0, 0 } }; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); while((c = getopt_long(argc, argv, "cfp:L:v:U:Vh", longopts, NULL)) != -1) { switch (c) { case 'c': ctl.check = 1; break; case 'f': ctl.force = 1; break; case 'p': ctl.user_pagesize = strtou32_or_err(optarg, _("parsing page size failed")); break; case 'L': ctl.opt_label = optarg; break; case 'v': version = strtos32_or_err(optarg, _("parsing version number failed")); if (version != SWAP_VERSION) errx(EXIT_FAILURE, _("swapspace version %d is not supported"), version); break; case 'U': #ifdef HAVE_LIBUUID opt_uuid = optarg; #else warnx(_("warning: ignoring -U (UUIDs are unsupported by %s)"), program_invocation_short_name); #endif break; case 'V': printf(UTIL_LINUX_VERSION); exit(EXIT_SUCCESS); case 'h': usage(stdout); default: usage(stderr); } } if (optind < argc) ctl.devname = argv[optind++]; if (optind < argc) block_count = argv[optind++]; if (optind != argc) { warnx(_("only one device argument is currently supported")); usage(stderr); } #ifdef HAVE_LIBUUID if(opt_uuid) { if (uuid_parse(opt_uuid, uuid_dat) != 0) errx(EXIT_FAILURE, _("error: parsing UUID failed")); } else uuid_generate(uuid_dat); ctl.uuid = uuid_dat; #endif init_signature_page(&ctl); /* get pagesize and allocate signature page */ if (!ctl.devname) { warnx(_("error: Nowhere to set up swap on?")); usage(stderr); } if (block_count) { /* this silly user specified the number of blocks explicitly */ uint64_t blks = strtou64_or_err(block_count, _("invalid block count argument")); ctl.npages = blks / (ctl.pagesize / 1024); } sz = get_size(&ctl); if (!ctl.npages) ctl.npages = sz; else if (ctl.npages > sz && !ctl.force) errx(EXIT_FAILURE, _("error: " "size %llu KiB is larger than device size %ju KiB"), ctl.npages * (ctl.pagesize / 1024), sz * (ctl.pagesize / 1024)); if (ctl.npages < MIN_GOODPAGES) errx(EXIT_FAILURE, _("error: swap area needs to be at least %ld KiB"), (long)(MIN_GOODPAGES * ctl.pagesize / 1024)); if (ctl.npages > UINT32_MAX) { /* true when swap is bigger than 17.59 terabytes */ ctl.npages = UINT32_MAX; warnx(_("warning: truncating swap area to %llu KiB"), ctl.npages * ctl.pagesize / 1024); } if (is_mounted(ctl.devname)) errx(EXIT_FAILURE, _("error: " "%s is mounted; will not make swapspace"), ctl.devname); open_device(&ctl); if (ctl.check) check_blocks(&ctl); wipe_device(&ctl); assert(ctl.hdr); ctl.hdr->version = version; ctl.hdr->last_page = ctl.npages - 1; ctl.hdr->nr_badpages = ctl.nbadpages; if ((ctl.npages - MIN_GOODPAGES) < ctl.nbadpages) errx(EXIT_FAILURE, _("Unable to set up swap-space: unreadable")); sz = (ctl.npages - ctl.nbadpages - 1) * ctl.pagesize; strsz = size_to_human_string(SIZE_SUFFIX_SPACE | SIZE_SUFFIX_3LETTER, sz); printf(_("Setting up swapspace version %d, size = %s (%ju bytes)\n"), version, strsz, sz); free(strsz); set_signature(&ctl); set_uuid_and_label(&ctl); write_header_to_device(&ctl); deinit_signature_page(&ctl); #ifdef HAVE_LIBSELINUX if (S_ISREG(ctl.devstat.st_mode) && is_selinux_enabled() > 0) { security_context_t context_string; security_context_t oldcontext; context_t newcontext; if (fgetfilecon(ctl.fd, &oldcontext) < 0) { if (errno != ENODATA) err(EXIT_FAILURE, _("%s: unable to obtain selinux file label"), ctl.devname); if (matchpathcon(ctl.devname, ctl.devstat.st_mode, &oldcontext)) errx(EXIT_FAILURE, _("unable to matchpathcon()")); } if (!(newcontext = context_new(oldcontext))) errx(EXIT_FAILURE, _("unable to create new selinux context")); if (context_type_set(newcontext, SELINUX_SWAPFILE_TYPE)) errx(EXIT_FAILURE, _("couldn't compute selinux context")); context_string = context_str(newcontext); if (strcmp(context_string, oldcontext)!=0) { if (fsetfilecon(ctl.fd, context_string)) err(EXIT_FAILURE, _("unable to relabel %s to %s"), ctl.devname, context_string); } context_free(newcontext); freecon(oldcontext); } #endif /* * A subsequent swapon() will fail if the signature * is not actually on disk. (This is a kernel bug.) * The fsync() in close_fd() will take care of writing. */ if (close_fd(ctl.fd) != 0) err(EXIT_FAILURE, _("write failed")); return EXIT_SUCCESS; }
static char* fsck_check_dir_recursive (const char *id, const char *parent_dir, FsckData *fsck_data) { SeafDir *dir; SeafDir *new_dir; GList *p; SeafDirent *seaf_dent; char *dir_id = NULL; char *path = NULL; gboolean io_error = FALSE; SeafFSManager *mgr = seaf->fs_mgr; char *store_id = fsck_data->repo->store_id; int version = fsck_data->repo->version; gboolean is_corrupted = FALSE; dir = seaf_fs_manager_get_seafdir (mgr, store_id, version, id); for (p = dir->entries; p; p = p->next) { seaf_dent = p->data; io_error = FALSE; if (S_ISREG(seaf_dent->mode)) { path = g_strdup_printf ("%s%s", parent_dir, seaf_dent->name); if (!path) { seaf_warning ("Out of memory, stop to run fsck for repo %.8s.\n", fsck_data->repo->id); goto out; } if (!fsck_verify_seafobj (store_id, version, seaf_dent->id, &io_error, VERIFY_FILE, fsck_data->repair)) { if (io_error) { g_free (path); goto out; } is_corrupted = TRUE; if (fsck_data->repair) { seaf_message ("File %s(%.8s) is corrupted, recreate an empty file.\n", path, seaf_dent->id); } else { seaf_message ("File %s(%.8s) is corrupted.\n", path, seaf_dent->id); } // file corrupted, set it empty memcpy (seaf_dent->id, EMPTY_SHA1, 40); seaf_dent->size = 0; } else { if (check_blocks (seaf_dent->id, fsck_data, &io_error) < 0) { if (io_error) { g_free (path); goto out; } is_corrupted = TRUE; if (fsck_data->repair) { seaf_message ("File %s(%.8s) is corrupted, recreate an empty file.\n", path, seaf_dent->id); } else { seaf_message ("File %s(%.8s) is corrupted.\n", path, seaf_dent->id); } // file corrupted, set it empty memcpy (seaf_dent->id, EMPTY_SHA1, 40); seaf_dent->size = 0; } } g_free (path); } else if (S_ISDIR(seaf_dent->mode)) { path = g_strdup_printf ("%s%s/", parent_dir, seaf_dent->name); if (!path) { seaf_warning ("Out of memory, stop to run fsck for repo %.8s.\n", fsck_data->repo->id); goto out; } if (!fsck_verify_seafobj (store_id, version, seaf_dent->id, &io_error, VERIFY_DIR, fsck_data->repair)) { if (io_error) { g_free (path); goto out; } if (fsck_data->repair) { seaf_message ("Dir %s(%.8s) is corrupted, recreate an empty dir.\n", path, seaf_dent->id); } else { seaf_message ("Dir %s(%.8s) is corrupted.\n", path, seaf_dent->id); } is_corrupted = TRUE; // dir corrupted, set it empty memcpy (seaf_dent->id, EMPTY_SHA1, 40); } else { dir_id = fsck_check_dir_recursive (seaf_dent->id, path, fsck_data); if (dir_id == NULL) { // IO error g_free (path); goto out; } if (strcmp (dir_id, seaf_dent->id) != 0) { is_corrupted = TRUE; // dir corrupted, set it to new dir_id memcpy (seaf_dent->id, dir_id, 41); } g_free (dir_id); } g_free (path); } } if (is_corrupted) { new_dir = seaf_dir_new (NULL, dir->entries, version); if (fsck_data->repair) { if (seaf_dir_save (mgr, store_id, version, new_dir) < 0) { seaf_warning ("Failed to save dir\n"); seaf_dir_free (new_dir); goto out; } } dir_id = g_strdup (new_dir->dir_id); seaf_dir_free (new_dir); dir->entries = NULL; } else { dir_id = g_strdup (dir->dir_id); } out: seaf_dir_free (dir); return dir_id; }
int mkswap_main(int argc, char **argv) { char *tmp; struct stat statbuf; int sz; int maxpages; int goodpages; int offset; int force = 0; init_signature_page(); /* get pagesize */ while (argc-- > 1) { argv++; if (argv[0][0] != '-') { if (device_name) { int blocks_per_page = pagesize / 1024; PAGES = strtol(argv[0], &tmp, 0) / blocks_per_page; if (*tmp) bb_show_usage(); } else device_name = argv[0]; } else { switch (argv[0][1]) { case 'c': check = 1; break; case 'f': force = 1; break; case 'v': version = atoi(argv[0] + 2); break; default: bb_show_usage(); } } } if (!device_name) { bb_error_msg("error: Nowhere to set up swap on?"); bb_show_usage(); } sz = get_size(device_name); if (!PAGES) { PAGES = sz; } else if (PAGES > sz && !force) { bb_error_msg("error: size %ld is larger than device size %d", PAGES * (pagesize / 1024), sz * (pagesize / 1024)); return EXIT_FAILURE; } if (version == -1) { if (PAGES <= V0_MAX_PAGES) version = 0; else if (get_kernel_revision() < MAKE_VERSION(2, 1, 117)) version = 0; else if (pagesize < 2048) version = 0; else version = 1; } if (version != 0 && version != 1) { bb_error_msg("error: unknown version %d", version); bb_show_usage(); } if (PAGES < 10) { bb_error_msg("error: swap area needs to be at least %ldkB", (long) (10 * pagesize / 1024)); bb_show_usage(); } #if 0 maxpages = ((version == 0) ? V0_MAX_PAGES : V1_MAX_PAGES); #else if (!version) maxpages = V0_MAX_PAGES; else if (get_kernel_revision() >= MAKE_VERSION(2, 2, 1)) maxpages = V1_MAX_PAGES; else { maxpages = V1_OLD_MAX_PAGES; if (maxpages > V1_MAX_PAGES) maxpages = V1_MAX_PAGES; } #endif if (PAGES > maxpages) { PAGES = maxpages; bb_error_msg("warning: truncating swap area to %ldkB", PAGES * pagesize / 1024); } DEV = open(device_name, O_RDWR); if (DEV < 0 || fstat(DEV, &statbuf) < 0) bb_perror_msg_and_die("%s", device_name); if (!S_ISBLK(statbuf.st_mode)) check = 0; else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340) bb_error_msg_and_die("Will not try to make swapdevice on '%s'", device_name); #ifdef __sparc__ if (!force && version == 0) { /* Don't overwrite partition table unless forced */ unsigned char *buffer = (unsigned char *) signature_page; unsigned short *q, sum; if (read(DEV, buffer, 512) != 512) bb_error_msg_and_die("fatal: first page unreadable"); if (buffer[508] == 0xDA && buffer[509] == 0xBE) { q = (unsigned short *) (buffer + 510); for (sum = 0; q >= (unsigned short *) buffer;) sum ^= *q--; if (!sum) { bb_error_msg("Device '%s' contains a valid Sun disklabel.\n" "This probably means creating v0 swap would destroy your partition table\n" "No swap created. If you really want to create swap v0 on that device, use\n" "the -f option to force it.", device_name); return EXIT_FAILURE; } } } #endif if (version == 0 || check) check_blocks(); if (version == 0 && !bit_test_and_clear(signature_page, 0)) bb_error_msg_and_die("fatal: first page unreadable"); if (version == 1) { p->version = version; p->last_page = PAGES - 1; p->nr_badpages = badpages; } goodpages = PAGES - badpages - 1; if (goodpages <= 0) bb_error_msg_and_die("Unable to set up swap-space: unreadable"); printf("Setting up swapspace version %d, size = %ld bytes\n", version, (long) (goodpages * pagesize)); write_signature((version == 0) ? "SWAP-SPACE" : "SWAPSPACE2"); offset = ((version == 0) ? 0 : 1024); if (lseek(DEV, offset, SEEK_SET) != offset) bb_error_msg_and_die("unable to rewind swap-device"); if (write(DEV, (char *) signature_page + offset, pagesize - offset) != pagesize - offset) bb_error_msg_and_die("unable to write signature page"); /* * A subsequent swapon() will fail if the signature * is not actually on disk. (This is a kernel bug.) */ if (fsync(DEV)) bb_error_msg_and_die("fsync failed"); return EXIT_SUCCESS; }
int main(int argc, char ** argv) { struct stat statbuf; struct swap_header_v1_2 *hdr; int i; unsigned long long maxpages; unsigned long long goodpages; unsigned long long sz; off_t offset; int force = 0; int version = 1; char *block_count = 0; char *pp; char *opt_label = NULL; unsigned char *uuid = NULL; #ifdef HAVE_LIBUUID const char *opt_uuid = NULL; uuid_t uuid_dat; #endif program_name = (argc && *argv) ? argv[0] : "mkswap"; if ((pp = strrchr(program_name, '/')) != NULL) program_name = pp+1; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); if (argc == 2 && (!strcmp(argv[1], "-V") || !strcmp(argv[1], "--version"))) { printf(_("%s (%s)\n"), program_name, PACKAGE_STRING); exit(0); } for (i=1; i<argc; i++) { if (argv[i][0] == '-') { switch (argv[i][1]) { case 'c': check=1; break; case 'f': force=1; break; case 'p': pp = argv[i]+2; if (!*pp && i+1 < argc) pp = argv[++i]; if (isnzdigit(*pp)) user_pagesize = atoi(pp); else usage(); break; case 'L': pp = argv[i]+2; if (!*pp && i+1 < argc) pp = argv[++i]; opt_label = pp; break; case 'v': version = atoi(argv[i]+2); break; case 'U': #ifdef HAVE_LIBUUID opt_uuid = argv[i]+2; if (!*opt_uuid && i+1 < argc) opt_uuid = argv[++i]; #else fprintf(stderr, _("%1$s: warning: ignore -U (UUIDs are unsupported by %1$s)\n"), program_name); #endif break; default: usage(); } } else if (!device_name) { device_name = argv[i]; } else if (!block_count) { block_count = argv[i]; } else usage(); } if (version != 1) { fprintf(stderr, _("%s: does not support swapspace version %d.\n"), program_name, version); exit(EXIT_FAILURE); } #ifdef HAVE_LIBUUID if(opt_uuid) { if (uuid_parse(opt_uuid, uuid_dat) != 0) die(_("error: UUID parsing failed")); } else uuid_generate(uuid_dat); uuid = uuid_dat; #endif init_signature_page(); /* get pagesize */ atexit(deinit_signature_page); if (!device_name) { fprintf(stderr, _("%s: error: Nowhere to set up swap on?\n"), program_name); usage(); } if (block_count) { /* this silly user specified the number of blocks explicitly */ char *tmp = NULL; long long blks; errno = 0; blks = strtoll(block_count, &tmp, 0); if ((tmp == block_count) || (tmp && *tmp) || (errno != 0 && (blks == LLONG_MAX || blks == LLONG_MIN)) || blks < 0) usage(); PAGES = blks / (pagesize / 1024); } sz = get_size(device_name); if (!PAGES) { PAGES = sz; } else if (PAGES > sz && !force) { fprintf(stderr, _("%s: error: " "size %llu KiB is larger than device size %llu KiB\n"), program_name, PAGES*(pagesize/1024), sz*(pagesize/1024)); exit(1); } if (PAGES < MIN_GOODPAGES) { fprintf(stderr, _("%s: error: swap area needs to be at least %ld KiB\n"), program_name, (long)(MIN_GOODPAGES * pagesize/1024)); usage(); } #ifdef __linux__ if (get_linux_version() >= KERNEL_VERSION(2,3,4)) maxpages = UINT_MAX + 1ULL; else if (get_linux_version() >= KERNEL_VERSION(2,2,1)) maxpages = V1_MAX_PAGES; else #endif maxpages = V1_OLD_MAX_PAGES; if (PAGES > maxpages) { PAGES = maxpages; fprintf(stderr, _("%s: warning: truncating swap area to %llu KiB\n"), program_name, PAGES * pagesize / 1024); } if (stat(device_name, &statbuf) < 0) { perror(device_name); exit(EXIT_FAILURE); } if (S_ISBLK(statbuf.st_mode)) DEV = open(device_name, O_RDWR | O_EXCL); else DEV = open(device_name, O_RDWR); if (DEV < 0) { perror(device_name); exit(1); } /* Want a block device. Probably not /dev/hda or /dev/hdb. */ if (!S_ISBLK(statbuf.st_mode)) check=0; else if (statbuf.st_rdev == 0x0300 || statbuf.st_rdev == 0x0340) { fprintf(stderr, _("%s: error: " "will not try to make swapdevice on '%s'\n"), program_name, device_name); exit(1); } else if (check_mount()) { fprintf(stderr, _("%s: error: " "%s is mounted; will not make swapspace.\n"), program_name, device_name); exit(1); } if (check) check_blocks(); zap_bootbits(DEV, device_name, force, S_ISBLK(statbuf.st_mode)); hdr = (struct swap_header_v1_2 *) signature_page; hdr->version = 1; hdr->last_page = PAGES - 1; hdr->nr_badpages = badpages; if (badpages > PAGES - MIN_GOODPAGES) die(_("Unable to set up swap-space: unreadable")); goodpages = PAGES - badpages - 1; printf(_("Setting up swapspace version 1, size = %llu KiB\n"), goodpages * pagesize / 1024); write_signature("SWAPSPACE2"); write_uuid_and_label(uuid, opt_label); offset = 1024; if (lseek(DEV, offset, SEEK_SET) != offset) die(_("unable to rewind swap-device")); if (write_all(DEV, (char *) signature_page + offset, pagesize - offset) == -1) { fprintf(stderr, _("%s: %s: unable to write signature page: %s"), program_name, device_name, strerror(errno)); exit(1); } /* * A subsequent swapon() will fail if the signature * is not actually on disk. (This is a kernel bug.) */ #ifdef HAVE_FSYNC if (fsync(DEV)) die(_("fsync failed")); #endif #ifdef HAVE_LIBSELINUX if (S_ISREG(statbuf.st_mode) && is_selinux_enabled() > 0) { security_context_t context_string; security_context_t oldcontext; context_t newcontext; if (fgetfilecon(DEV, &oldcontext) < 0) { if (errno != ENODATA) { fprintf(stderr, _("%s: %s: unable to obtain selinux file label: %s\n"), program_name, device_name, strerror(errno)); exit(1); } if (matchpathcon(device_name, statbuf.st_mode, &oldcontext)) die(_("unable to matchpathcon()")); } if (!(newcontext = context_new(oldcontext))) die(_("unable to create new selinux context")); if (context_type_set(newcontext, SELINUX_SWAPFILE_TYPE)) die(_("couldn't compute selinux context")); context_string = context_str(newcontext); if (strcmp(context_string, oldcontext)!=0) { if (fsetfilecon(DEV, context_string)) { fprintf(stderr, _("%s: unable to relabel %s to %s: %s\n"), program_name, device_name, context_string, strerror(errno)); exit(1); } } context_free(newcontext); freecon(oldcontext); } #endif return 0; }
/************************************************************************* * MAIN ENTRY POINT FOR THE FILE-SYSTEM CHECKER * * - The different passes of the checking process ... * * Parameter : full = TRUE : Full check incl. directory tree scan * FALSE: only check_unique() then full check if * any errors occurred * Return : FALSE, if a fatal error-condition occurred * *************************************************************************/ word fscheck ( word full ) { struct buf *bpar, *bp; /* used for check_inodes() */ struct dir_elem root_de; /* For fatal error handling. This longjmp - */ /* location is used, if bread() to fixed block- */ /* numbers (like 1,2,3) fails or memory alloca- */ /* operations do not succeed. */ if ( setjmp ( term_jmp ) != 0 ) { IOdebug ("%sCHECKER INTERRUPTED DUE TO A FATAL ERROR CONDITION!", S_FATAL); IOdebug ("%s(Especially block-read and memory allocation operations)", S_FATAL); finish_checker (); return FALSE; } if ( ! init_checker () ) /* Basic initialization */ return FALSE; /*--------------- The four steps of the checking process ---------------*/ IOdebug ( "%sSTEP 1 : CHECKING BASIC DATA STRUCTURES", S_INFO ); unique_err = 0; if ( ! check_unique () ) /* Basic tests on superblock */ { /* and other unique structures */ IOdebug ("%sDURING BASIC DATA STRUCTURE CHECK", S_FATAL); finish_checker (); return FALSE; } if ( !full && !unique_err ) { finish_checker (); /* Finish checking, tidy up and */ /* unlock the server's ports */ return TRUE; /* Terminate checker process */ } IOdebug ( "%sSTEP 2 : TRAVERSING THE DIRECTORY TREE (INODE-CHECK)", S_INFO); bpar = bread ( 0, 1, 1, SAVEA ); /* Get sum-block with root-dir */ /* if read fails jmp to term_jmp */ /* Start from the file-server's */ /* root position */ memcpy ( &root_de, &bpar->b_un.b_sum->root_dir, sizeof (struct dir_elem) ); brelse ( bpar->b_tbp, TAIL ); /* Release summary block */ /* Up from this point, we get a */ /* Work on the directory tree */ if ( ! check_inodes ( &root_de , 1, 0 ) ) { IOdebug ("%sDURING SIMPLE OBJECT (INODE) CHECK", S_FATAL); finish_checker (); return FALSE; } /* Work on the bit-maps */ IOdebug ( "%sSTEP 3 : MAKE BLOCK-BASED CONNECTIVITY CHECKS", S_INFO); if ( ! check_blocks () ) { IOdebug ("%sDURING BIT-MAP AND CONNECTIVITY CHECK", S_FATAL); finish_checker (); return FALSE; } actual_path[0] = '\0'; /* Perform some final tidy-up */ /* operations */ IOdebug ("%sSTEP 4 : TIDYUP EVERYTHING AND CORRECT SUMMARY INFORMATIONS", S_INFO); if ( ! tidy_update () ) { IOdebug ("%sDURING TIDY UP OPERATION", S_FATAL); finish_checker (); return FALSE; } finish_checker (); /* Finish checking, tidy up and */ /* unlock the server's ports */ return TRUE; /* Terminate checker process */ }