]> git.ipfire.org Git - thirdparty/util-linux.git/blob - sys-utils/swapoff.c
Merge branch 'usage-part2' of https://github.com/rudimeier/util-linux
[thirdparty/util-linux.git] / sys-utils / swapoff.c
1 #include <stdio.h>
2 #include <errno.h>
3 #include <getopt.h>
4
5 #ifdef HAVE_SYS_SWAP_H
6 # include <sys/swap.h>
7 #endif
8
9 #include "nls.h"
10 #include "c.h"
11 #include "xalloc.h"
12 #include "closestream.h"
13
14 #include "swapprober.h"
15 #include "swapon-common.h"
16
17 #ifndef SWAPON_HAS_TWO_ARGS
18 /* libc is insane, let's call the kernel */
19 # include <sys/syscall.h>
20 # define swapoff(path) syscall(SYS_swapoff, path)
21 #endif
22
23 static int verbose;
24 static int all;
25
26 #define QUIET 1
27 #define CANONIC 1
28
29 /*
30 * This function works like mnt_resolve_tag(), but it's able to read UUID/LABEL
31 * from regular swap files too (according to entries in /proc/swaps). Note that
32 * mnt_resolve_tag() and mnt_resolve_spec() works with system visible block
33 * devices only.
34 */
35 static char *swapoff_resolve_tag(const char *name, const char *value,
36 struct libmnt_cache *cache)
37 {
38 char *path;
39 struct libmnt_table *tb;
40 struct libmnt_iter *itr;
41 struct libmnt_fs *fs;
42
43 /* this is usual case for block devices (and it's really fast as it uses
44 * udev /dev/disk/by-* symlinks by default */
45 path = mnt_resolve_tag(name, value, cache);
46 if (path)
47 return path;
48
49 /* try regular files from /proc/swaps */
50 tb = get_swaps();
51 if (!tb)
52 return NULL;
53
54 itr = mnt_new_iter(MNT_ITER_BACKWARD);
55 if (!itr)
56 err(EXIT_FAILURE, _("failed to initialize libmount iterator"));
57
58 while (tb && mnt_table_next_fs(tb, itr, &fs) == 0) {
59 blkid_probe pr = NULL;
60 const char *src = mnt_fs_get_source(fs);
61 const char *type = mnt_fs_get_swaptype(fs);
62 const char *data = NULL;
63
64 if (!src || !type || strcmp(type, "file") != 0)
65 continue;
66 pr = get_swap_prober(src);
67 if (!pr)
68 continue;
69 blkid_probe_lookup_value(pr, name, &data, NULL);
70 if (data && strcmp(data, value) == 0)
71 path = xstrdup(src);
72 blkid_free_probe(pr);
73 if (path)
74 break;
75 }
76
77 mnt_free_iter(itr);
78 return path;
79 }
80
81 static int do_swapoff(const char *orig_special, int quiet, int canonic)
82 {
83 const char *special = orig_special;
84
85 if (verbose)
86 printf(_("swapoff %s\n"), orig_special);
87
88 if (!canonic) {
89 char *n, *v;
90
91 special = mnt_resolve_spec(orig_special, mntcache);
92 if (!special && blkid_parse_tag_string(orig_special, &n, &v) == 0) {
93 special = swapoff_resolve_tag(n, v, mntcache);
94 free(n);
95 free(v);
96 }
97 if (!special)
98 return cannot_find(orig_special);
99 }
100
101 if (swapoff(special) == 0)
102 return 0; /* success */
103
104 if (errno == EPERM)
105 errx(EXIT_FAILURE, _("Not superuser."));
106
107 if (!quiet || errno == ENOMEM)
108 warn(_("%s: swapoff failed"), orig_special);
109
110 return -1;
111 }
112
113 static int swapoff_by(const char *name, const char *value, int quiet)
114 {
115 const char *special = swapoff_resolve_tag(name, value, mntcache);
116 return special ? do_swapoff(special, quiet, CANONIC) : cannot_find(value);
117 }
118
119 static void __attribute__((__noreturn__)) usage(void)
120 {
121 FILE *out = stdout;
122 fputs(USAGE_HEADER, out);
123 fprintf(out, _(" %s [options] [<spec>]\n"), program_invocation_short_name);
124
125 fputs(USAGE_SEPARATOR, out);
126 fputs(_("Disable devices and files for paging and swapping.\n"), out);
127
128 fputs(USAGE_OPTIONS, out);
129 fputs(_(" -a, --all disable all swaps from /proc/swaps\n"
130 " -v, --verbose verbose mode\n"), out);
131
132 fputs(USAGE_SEPARATOR, out);
133 fputs(USAGE_HELP, out);
134 fputs(USAGE_VERSION, out);
135
136 fputs(_("\nThe <spec> parameter:\n" \
137 " -L <label> LABEL of device to be used\n" \
138 " -U <uuid> UUID of device to be used\n" \
139 " LABEL=<label> LABEL of device to be used\n" \
140 " UUID=<uuid> UUID of device to be used\n" \
141 " <device> name of device to be used\n" \
142 " <file> name of file to be used\n"), out);
143
144 fprintf(out, USAGE_MAN_TAIL("swapoff(8)"));
145 exit(EXIT_SUCCESS);
146 }
147
148 static int swapoff_all(void)
149 {
150 int status = 0;
151 struct libmnt_table *tb;
152 struct libmnt_fs *fs;
153 struct libmnt_iter *itr = mnt_new_iter(MNT_ITER_BACKWARD);
154
155 if (!itr)
156 err(EXIT_FAILURE, _("failed to initialize libmount iterator"));
157
158 /*
159 * In case /proc/swaps exists, unswap stuff listed there. We are quiet
160 * but report errors in status. Errors might mean that /proc/swaps
161 * exists as ordinary file, not in procfs. do_swapoff() exits
162 * immediately on EPERM.
163 */
164 tb = get_swaps();
165
166 while (tb && mnt_table_find_next_fs(tb, itr, match_swap, NULL, &fs) == 0)
167 status |= do_swapoff(mnt_fs_get_source(fs), QUIET, CANONIC);
168
169 /*
170 * Unswap stuff mentioned in /etc/fstab. Probably it was unmounted
171 * already, so errors are not bad. Doing swapoff -a twice should not
172 * give error messages.
173 */
174 tb = get_fstab();
175 mnt_reset_iter(itr, MNT_ITER_FORWARD);
176
177 while (tb && mnt_table_find_next_fs(tb, itr, match_swap, NULL, &fs) == 0) {
178 if (!is_active_swap(mnt_fs_get_source(fs)))
179 do_swapoff(mnt_fs_get_source(fs), QUIET, !CANONIC);
180 }
181
182 mnt_free_iter(itr);
183 return status;
184 }
185
186 int main(int argc, char *argv[])
187 {
188 int status = 0, c;
189 size_t i;
190
191 static const struct option long_opts[] = {
192 { "all", no_argument, NULL, 'a' },
193 { "help", no_argument, NULL, 'h' },
194 { "verbose", no_argument, NULL, 'v' },
195 { "version", no_argument, NULL, 'V' },
196 { NULL, 0, NULL, 0 }
197 };
198
199 setlocale(LC_ALL, "");
200 bindtextdomain(PACKAGE, LOCALEDIR);
201 textdomain(PACKAGE);
202 atexit(close_stdout);
203
204 while ((c = getopt_long(argc, argv, "ahvVL:U:",
205 long_opts, NULL)) != -1) {
206 switch (c) {
207 case 'a': /* all */
208 ++all;
209 break;
210 case 'h': /* help */
211 usage();
212 break;
213 case 'v': /* be chatty */
214 ++verbose;
215 break;
216 case 'V': /* version */
217 printf(UTIL_LINUX_VERSION);
218 return EXIT_SUCCESS;
219 case 'L':
220 add_label(optarg);
221 break;
222 case 'U':
223 add_uuid(optarg);
224 break;
225 default:
226 errtryhelp(EXIT_FAILURE);
227 }
228 }
229 argv += optind;
230
231 if (!all && !numof_labels() && !numof_uuids() && *argv == NULL) {
232 warnx(_("bad usage"));
233 errtryhelp(EXIT_FAILURE);
234 }
235
236 mnt_init_debug(0);
237 mntcache = mnt_new_cache();
238
239 for (i = 0; i < numof_labels(); i++)
240 status |= swapoff_by("LABEL", get_label(i), !QUIET);
241
242 for (i = 0; i < numof_uuids(); i++)
243 status |= swapoff_by("UUID", get_uuid(i), !QUIET);
244
245 while (*argv != NULL)
246 status |= do_swapoff(*argv++, !QUIET, !CANONIC);
247
248 if (all)
249 status |= swapoff_all();
250
251 free_tables();
252 mnt_unref_cache(mntcache);
253
254 return status;
255 }