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 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 main_swapon(int argc, char *argv[]) { int status = 0; int c, i; while ((c = getopt_long(argc, argv, "ahdefp:svVL:U:", longswaponopts, NULL)) != -1) { switch (c) { case 'a': /* all */ ++all; break; case 'h': /* help */ swapon_usage(stdout, 0); break; case 'p': /* priority */ priority = atoi(optarg); break; case 'L': addl(optarg); break; case 'U': addu(optarg); break; case 'd': discard = 1; break; case 'e': /* ifexists */ ifexists = 1; break; case 'f': fixpgsz = 1; break; case 's': /* status report */ status = display_summary(); exit(status); case 'v': /* be chatty */ ++verbose; break; case 'V': /* version */ printf(_("%s (%s)\n"), progname, PACKAGE_STRING); exit(EXIT_SUCCESS); case 0: break; case '?': default: swapon_usage(stderr, 1); } } argv += optind; if (!all && !llct && !ulct && *argv == NULL) swapon_usage(stderr, 2); if (ifexists && (!all || strcmp(progname, "swapon"))) swapon_usage(stderr, 1); if (all) status |= swapon_all(); for (i = 0; i < llct; i++) status |= swapon_by_label(llist[i], priority, discard); for (i = 0; i < ulct; i++) status |= swapon_by_uuid(ulist[i], priority, discard); while (*argv != NULL) status |= do_swapon(*argv++, priority, discard, !CANONIC); return status; }
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 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); }