static int resolve_mount_spec(char **fsname) { char *tmp = NULL; if (!strncmp(*fsname, "UUID=", 5)) tmp = mount_get_devname(*fsname + 5, MOUNT_SPEC_UUID); else if (!strncmp(*fsname, "LABEL=", 6)) tmp = mount_get_devname(*fsname + 6, MOUNT_SPEC_LABEL); if (tmp) { *fsname = tmp; return 1; } return 0; }
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 swapon_all(void) { FILE *fp; struct mntent *fstab; int status = 0; read_proc_swaps(); fp = setmntent(_PATH_FSTAB, "r"); if (fp == NULL) { int errsv = errno; fprintf(stderr, _("%s: cannot open %s: %s\n"), progname, _PATH_FSTAB, strerror(errsv)); exit(2); } while ((fstab = getmntent(fp)) != NULL) { const char *orig_special = fstab->mnt_fsname; const char *special; int skip = 0; int pri = priority; if (!streq(fstab->mnt_type, MNTTYPE_SWAP)) continue; special = mount_get_devname(orig_special); if (!special) continue; if (!is_in_proc_swaps(special) && (!ifexists || !access(special, R_OK))) { /* parse mount options; */ char *opt, *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, "noauto") == 0) skip = 1; } if (!skip) status |= do_swapon(special, pri); } } fclose(fp); return status; }
static int do_swapon(const char *orig_special, int prio) { int status; struct stat st; const char *special; if (verbose) printf(_("%s on %s\n"), progname, orig_special); special = mount_get_devname(orig_special); if (!special) { fprintf(stderr, _("%s: cannot find the device for %s\n"), progname, orig_special); return -1; } if (stat(special, &st) < 0) { int errsv = errno; fprintf(stderr, _("%s: cannot stat %s: %s\n"), progname, special, strerror(errsv)); 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; } } #ifdef SWAPON_NEEDS_TWO_ARGS { 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); } #else status = swapon(special); #endif if (status < 0) { int errsv = errno; fprintf(stderr, "%s: %s: %s\n", progname, orig_special, strerror(errsv)); } return status; }