static int do_swapoff(const char *orig_special, int quiet) { const char *special; if (verbose) printf(_("%s on %s\n"), progname, orig_special); special = mount_get_devname(orig_special); if (!special) return cannot_find(orig_special); if (swapoff(special) == 0) return 0; /* success */ if (errno == EPERM) { fprintf(stderr, _("Not superuser.\n")); exit(1); /* any further swapoffs will also fail */ } if (!quiet || errno == ENOMEM) { fprintf(stderr, "%s: %s: %s\n", progname, orig_special, strerror(errno)); } return -1; }
static int do_swapoff(const char *orig_special, int quiet, int canonic) { const char *special = orig_special; if (verbose) printf(_("swapoff %s\n"), orig_special); if (!canonic) { char *n, *v; special = mnt_resolve_spec(orig_special, mntcache); if (!special && blkid_parse_tag_string(orig_special, &n, &v) == 0) special = swapoff_resolve_tag(n, v, mntcache); if (!special) return cannot_find(orig_special); } if (swapoff(special) == 0) return 0; /* success */ if (errno == EPERM) errx(EXIT_FAILURE, _("Not superuser.")); if (!quiet || errno == ENOMEM) warn(_("%s: swapoff failed"), orig_special); return -1; }
static int do_swapon(const char *orig_special, int prio, int canonic) { int status; const char *special = orig_special; int flags = 0; if (verbose) printf(_("%s on %s\n"), progname, orig_special); if (!canonic) { special = fsprobe_get_devname_by_spec(orig_special); if (!special) return cannot_find(orig_special); } if (swapon_checks(special)) return -1; #ifdef SWAP_FLAG_PREFER if (prio >= 0) { if (prio > SWAP_FLAG_PRIO_MASK) prio = SWAP_FLAG_PRIO_MASK; flags = SWAP_FLAG_PREFER | ((prio & SWAP_FLAG_PRIO_MASK) << SWAP_FLAG_PRIO_SHIFT); } #endif status = swapon(special, flags); if (status < 0) warn(_("%s: swapon failed"), orig_special); return status; }
static int swapon_all(void) { FILE *fp; struct mntent *fstab; int status = 0; read_proc_swaps(); fp = setmntent(_PATH_MNTTAB, "r"); if (fp == NULL) err(2, _("%s: open failed"), _PATH_MNTTAB); while ((fstab = getmntent(fp)) != NULL) { const char *special; int skip = 0, nofail = ifexists; int pri = priority, dsc = discard; char *opt, *opts; if (!streq(fstab->mnt_type, MNTTYPE_SWAP)) continue; opts = strdup(fstab->mnt_opts); for (opt = strtok(opts, ","); opt != NULL; opt = strtok(NULL, ",")) { if (strncmp(opt, "pri=", 4) == 0) pri = atoi(opt+4); if (strcmp(opt, "discard") == 0) dsc = 1; if (strcmp(opt, "noauto") == 0) skip = 1; if (strcmp(opt, "nofail") == 0) nofail = 1; } free(opts); if (skip) continue; special = fsprobe_get_devname_by_spec(fstab->mnt_fsname); if (!special) { if (!nofail) status |= cannot_find(fstab->mnt_fsname); continue; } if (!is_in_proc_swaps(special) && (!nofail || !access(special, R_OK))) status |= do_swapon(special, pri, dsc, CANONIC); free((void *) special); } fclose(fp); return status; }
static int do_swapoff(const char *orig_special, int quiet, int canonic) { const char *special = orig_special; if (verbose) printf(_("%s on %s\n"), progname, orig_special); if (!canonic) { special = fsprobe_get_devname_by_spec(orig_special); if (!special) return cannot_find(orig_special); } if (swapoff(special) == 0) return 0; /* success */ if (errno == EPERM) errx(EXIT_FAILURE, _("Not superuser.")); if (!quiet || errno == ENOMEM) warn(_("%s: swapoff failed"), orig_special); return -1; }
static int swapoff_by(const char *name, const char *value, int quiet) { const char *special = swapoff_resolve_tag(name, value, mntcache); return special ? do_swapoff(special, quiet, CANONIC) : cannot_find(value); }
static int swapoff_by_uuid(const char *uuid, int quiet) { const char *special = fsprobe_get_devname_by_uuid(uuid); return special ? do_swapoff(special, quiet, CANONIC) : cannot_find(uuid); }
static int swapoff_by_label(const char *label, int quiet) { const char *special = fsprobe_get_devname_by_label(label); return special ? do_swapoff(special, quiet, CANONIC) : cannot_find(label); }
static int swapon_by_uuid(const char *uuid, int prio, int dsc) { const char *special = fsprobe_get_devname_by_uuid(uuid); return special ? do_swapon(special, prio, dsc, CANONIC) : cannot_find(uuid); }
static int swapon_by_label(const char *label, int prio, int dsc) { const char *special = fsprobe_get_devname_by_label(label); return special ? do_swapon(special, prio, dsc, CANONIC) : cannot_find(label); }
static int swapoff_by_uuid(const char *uuid, int quiet) { const char *special = mount_get_devname_by_uuid(uuid); return special ? do_swapoff(special, quiet) : cannot_find(uuid); }
static int swapoff_by_label(const char *label, int quiet) { const char *special = mount_get_devname_by_label(label); return special ? do_swapoff(special, quiet) : cannot_find(label); }
static int swapon_by_uuid(const char *uuid, int prio) { const char *special = mount_get_devname_by_uuid(uuid); return special ? do_swapon(special, prio) : cannot_find(uuid); }
static int swapon_by_label(const char *label, int prio) { const char *special = mount_get_devname_by_label(label); return special ? do_swapon(special, prio) : cannot_find(label); }
static int do_swapon(const char *orig_special, int prio, int canonic) { int status; struct stat st; const char *special = orig_special; if (verbose) printf(_("%s on %s\n"), progname, orig_special); if (!canonic) { special = fsprobe_get_devname(orig_special); if (!special) return cannot_find(orig_special); } if (stat(special, &st) < 0) { int errsv = errno; fprintf(stderr, _("%s: cannot stat %s: %s\n"), progname, special, strerror(errsv)); return -1; } /* We have to reinitialize swap with old (=useless) software suspend * data. The problem is that if we don't do it, then we get data * corruption the next time an attempt at unsuspending is made. */ if (swap_is_suspend(special)) { fprintf(stdout, _("%s: %s: software suspend data detected. " "Reinitializing the swap.\n"), progname, special); if (swap_reinitialize(special) < 0) return -1; } /* people generally dislike this warning - now it is printed only when `verbose' is set */ if (verbose) { int permMask = (S_ISBLK(st.st_mode) ? 07007 : 07077); if ((st.st_mode & permMask) != 0) { fprintf(stderr, _("%s: warning: %s has " "insecure permissions %04o, " "%04o suggested\n"), progname, special, st.st_mode & 07777, ~permMask & 0666); } } /* test for holes by LBT */ if (S_ISREG(st.st_mode)) { if (st.st_blocks * 512 < st.st_size) { fprintf(stderr, _("%s: Skipping file %s - it appears " "to have holes.\n"), progname, special); return -1; } } { int flags = 0; #ifdef SWAP_FLAG_PREFER if (prio >= 0) { if (prio > SWAP_FLAG_PRIO_MASK) prio = SWAP_FLAG_PRIO_MASK; flags = SWAP_FLAG_PREFER | ((prio & SWAP_FLAG_PRIO_MASK) << SWAP_FLAG_PRIO_SHIFT); } #endif status = swapon(special, flags); } if (status < 0) { int errsv = errno; fprintf(stderr, "%s: %s: %s\n", progname, orig_special, strerror(errsv)); } return status; }