static int swapoff_all(void) { int status = 0; struct libmnt_table *tb; struct libmnt_fs *fs; struct libmnt_iter *itr = mnt_new_iter(MNT_ITER_BACKWARD); if (!itr) err(EXIT_FAILURE, _("failed to initialize libmount iterator")); /* * In case /proc/swaps exists, unswap stuff listed there. We are quiet * but report errors in status. Errors might mean that /proc/swaps * exists as ordinary file, not in procfs. do_swapoff() exits * immediately on EPERM. */ tb = get_swaps(); while (tb && mnt_table_find_next_fs(tb, itr, match_swap, NULL, &fs) == 0) status |= do_swapoff(mnt_fs_get_source(fs), QUIET, CANONIC); /* * Unswap stuff mentioned in /etc/fstab. Probably it was unmounted * already, so errors are not bad. Doing swapoff -a twice should not * give error messages. */ tb = get_fstab(); mnt_reset_iter(itr, MNT_ITER_FORWARD); while (tb && mnt_table_find_next_fs(tb, itr, match_swap, NULL, &fs) == 0) { if (!is_active_swap(mnt_fs_get_source(fs))) do_swapoff(mnt_fs_get_source(fs), QUIET, !CANONIC); } mnt_free_iter(itr); return status; }
int main(int argc, char *argv[]) { int status = 0, c; size_t i; static const struct option long_opts[] = { { "all", 0, 0, 'a' }, { "help", 0, 0, 'h' }, { "verbose", 0, 0, 'v' }, { "version", 0, 0, 'V' }, { NULL, 0, 0, 0 } }; setlocale(LC_ALL, ""); bindtextdomain(PACKAGE, LOCALEDIR); textdomain(PACKAGE); atexit(close_stdout); while ((c = getopt_long(argc, argv, "ahvVL:U:", long_opts, NULL)) != -1) { switch (c) { case 'a': /* all */ ++all; break; case 'h': /* help */ usage(stdout); break; case 'v': /* be chatty */ ++verbose; break; case 'V': /* version */ printf(UTIL_LINUX_VERSION); return EXIT_SUCCESS; case 'L': add_label(optarg); break; case 'U': add_uuid(optarg); break; case '?': default: usage(stderr); } } argv += optind; if (!all && !numof_labels() && !numof_uuids() && *argv == NULL) usage(stderr); mnt_init_debug(0); mntcache = mnt_new_cache(); for (i = 0; i < numof_labels(); i++) status |= swapoff_by("LABEL", get_label(i), !QUIET); for (i = 0; i < numof_uuids(); i++) status |= swapoff_by("UUID", get_uuid(i), !QUIET); while (*argv != NULL) status |= do_swapoff(*argv++, !QUIET, !CANONIC); if (all) status |= swapoff_all(); free_tables(); mnt_unref_cache(mntcache); return status; }
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 main_swapoff(int argc, char *argv[]) { FILE *fp; struct mntent *fstab; int status = 0; int c, i; while ((c = getopt_long(argc, argv, "ahvVL:U:", longswapoffopts, NULL)) != -1) { switch (c) { case 'a': /* all */ ++all; break; case 'h': /* help */ swapoff_usage(stdout, 0); break; case 'v': /* be chatty */ ++verbose; break; case 'V': /* version */ printf(_("%s (%s)\n"), progname, PACKAGE_STRING); exit(EXIT_SUCCESS); case 'L': addl(optarg); break; case 'U': addu(optarg); break; case 0: break; case '?': default: swapoff_usage(stderr, 1); } } argv += optind; if (!all && !llct && !ulct && *argv == NULL) swapoff_usage(stderr, 2); /* * swapoff any explicitly given arguments. * Complain in case the swapoff call fails. */ for (i = 0; i < llct; i++) status |= swapoff_by_label(llist[i], !QUIET); for (i = 0; i < ulct; i++) status |= swapoff_by_uuid(ulist[i], !QUIET); while (*argv != NULL) status |= do_swapoff(*argv++, !QUIET, !CANONIC); if (all) { /* * In case /proc/swaps exists, unswap stuff listed there. * We are quiet but report errors in status. * Errors might mean that /proc/swaps * exists as ordinary file, not in procfs. * do_swapoff() exits immediately on EPERM. */ read_proc_swaps(); for(i=0; i<numSwaps; i++) status |= do_swapoff(swapFiles[i], QUIET, CANONIC); /* * Unswap stuff mentioned in /etc/fstab. * Probably it was unmounted already, so errors are not bad. * Doing swapoff -a twice should not give error messages. */ fp = setmntent(_PATH_MNTTAB, "r"); if (fp == NULL) err(2, _("%s: open failed"), _PATH_MNTTAB); while ((fstab = getmntent(fp)) != NULL) { const char *special; if (!streq(fstab->mnt_type, MNTTYPE_SWAP)) continue; special = fsprobe_get_devname_by_spec(fstab->mnt_fsname); if (!special) continue; if (!is_in_proc_swaps(special)) do_swapoff(special, QUIET, CANONIC); } fclose(fp); } return status; }
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 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); }