]>
Commit | Line | Data |
---|---|---|
e076b552 | 1 | #include <assert.h> |
fd6b7a7f KZ |
2 | #include <stdlib.h> |
3 | #include <stdio.h> | |
4 | #include <getopt.h> | |
5 | #include <string.h> | |
fd6b7a7f | 6 | #include <errno.h> |
5c36a0eb | 7 | #include <sys/stat.h> |
ffa63a3d | 8 | #include <unistd.h> |
6274b987 KC |
9 | #include <sys/types.h> |
10 | #include <sys/wait.h> | |
11 | #include <fcntl.h> | |
3399a218 | 12 | #include <stdint.h> |
57a60bf0 | 13 | #include <ctype.h> |
81772d3c | 14 | |
248a7f70 | 15 | #include <libsmartcols.h> |
b5b2c388 | 16 | |
d335c9bd KZ |
17 | #include "c.h" |
18 | #include "nls.h" | |
3399a218 MK |
19 | #include "bitops.h" |
20 | #include "blkdev.h" | |
4e270e3f | 21 | #include "pathnames.h" |
bfbb12d2 | 22 | #include "xalloc.h" |
18b3e549 | 23 | #include "strutils.h" |
e07fdac4 | 24 | #include "optutils.h" |
efb8854f | 25 | #include "closestream.h" |
167eaed7 | 26 | |
d335c9bd | 27 | #include "swapheader.h" |
18b3e549 | 28 | #include "swapprober.h" |
0b0c231f KZ |
29 | #include "swapon-common.h" |
30 | ||
ffa63a3d MF |
31 | #ifdef HAVE_SYS_SWAP_H |
32 | # include <sys/swap.h> | |
33 | #endif | |
34 | ||
c2301380 | 35 | #ifndef SWAP_FLAG_DISCARD |
d6387c98 | 36 | # define SWAP_FLAG_DISCARD 0x10000 /* enable discard for swap */ |
c2301380 HD |
37 | #endif |
38 | ||
d6387c98 RA |
39 | #ifndef SWAP_FLAG_DISCARD_ONCE |
40 | # define SWAP_FLAG_DISCARD_ONCE 0x20000 /* discard swap area at swapon-time */ | |
41 | #endif | |
42 | ||
43 | #ifndef SWAP_FLAG_DISCARD_PAGES | |
44 | # define SWAP_FLAG_DISCARD_PAGES 0x40000 /* discard page-clusters after use */ | |
45 | #endif | |
46 | ||
47 | #define SWAP_FLAGS_DISCARD_VALID (SWAP_FLAG_DISCARD | SWAP_FLAG_DISCARD_ONCE | \ | |
48 | SWAP_FLAG_DISCARD_PAGES) | |
49 | ||
9514a612 KZ |
50 | #ifndef SWAP_FLAG_PREFER |
51 | # define SWAP_FLAG_PREFER 0x8000 /* set if swap priority specified */ | |
52 | #endif | |
53 | ||
54 | #ifndef SWAP_FLAG_PRIO_MASK | |
55 | # define SWAP_FLAG_PRIO_MASK 0x7fff | |
56 | #endif | |
57 | ||
58 | #ifndef SWAP_FLAG_PRIO_SHIFT | |
59 | # define SWAP_FLAG_PRIO_SHIFT 0 | |
60 | #endif | |
61 | ||
ffa63a3d MF |
62 | #ifndef SWAPON_HAS_TWO_ARGS |
63 | /* libc is insane, let's call the kernel */ | |
64 | # include <sys/syscall.h> | |
65 | # define swapon(path, flags) syscall(SYS_swapon, path, flags) | |
ffa63a3d MF |
66 | #endif |
67 | ||
3399a218 MK |
68 | #define MAX_PAGESIZE (64 * 1024) |
69 | ||
e8be80dd KZ |
70 | enum { |
71 | SIG_SWAPSPACE = 1, | |
72 | SIG_SWSUSPEND | |
73 | }; | |
74 | ||
e076b552 SK |
75 | /* column names */ |
76 | struct colinfo { | |
77 | const char *name; /* header */ | |
78 | double whint; /* width hint (N < 1 is in percent of termwidth) */ | |
248a7f70 | 79 | int flags; /* SCOLS_FL_* */ |
e076b552 SK |
80 | const char *help; |
81 | }; | |
248a7f70 | 82 | |
5db57cfc SK |
83 | enum { |
84 | COL_PATH, | |
85 | COL_TYPE, | |
86 | COL_SIZE, | |
87 | COL_USED, | |
88 | COL_PRIO, | |
89 | COL_UUID, | |
90 | COL_LABEL | |
91 | }; | |
2ba641e5 | 92 | static struct colinfo infos[] = { |
e076b552 | 93 | [COL_PATH] = { "NAME", 0.20, 0, N_("device file or partition path") }, |
248a7f70 OO |
94 | [COL_TYPE] = { "TYPE", 0.20, SCOLS_FL_TRUNC, N_("type of the device")}, |
95 | [COL_SIZE] = { "SIZE", 0.20, SCOLS_FL_RIGHT, N_("size of the swap area")}, | |
96 | [COL_USED] = { "USED", 0.20, SCOLS_FL_RIGHT, N_("bytes in use")}, | |
97 | [COL_PRIO] = { "PRIO", 0.20, SCOLS_FL_RIGHT, N_("swap priority")}, | |
5db57cfc SK |
98 | [COL_UUID] = { "UUID", 0.20, 0, N_("swap uuid")}, |
99 | [COL_LABEL] = { "LABEL", 0.20, 0, N_("swap label")}, | |
e076b552 | 100 | }; |
a560a348 | 101 | |
6c44094a KZ |
102 | |
103 | /* swap area properties */ | |
104 | struct swap_prop { | |
105 | int discard; /* discard policy */ | |
106 | int priority; /* non-prioritized swap by default */ | |
107 | int no_fail; /* skip device if not exist */ | |
108 | }; | |
109 | ||
110 | /* device description */ | |
111 | struct swap_device { | |
112 | const char *path; /* device or file to be turned on */ | |
9eeba70e SK |
113 | const char *label; /* swap label */ |
114 | const char *uuid; /* unique identifier */ | |
6c44094a KZ |
115 | unsigned int pagesize; |
116 | }; | |
117 | ||
118 | /* control struct */ | |
119 | struct swapon_ctl { | |
9eeba70e | 120 | int columns[ARRAY_SIZE(infos) * 2]; /* --show columns */ |
6c44094a KZ |
121 | int ncolumns; /* number of columns */ |
122 | ||
123 | struct swap_prop props; /* global settings for all devices */ | |
124 | ||
9eeba70e SK |
125 | unsigned int |
126 | all:1, /* turn on all swap devices */ | |
127 | bytes:1, /* display --show in bytes */ | |
128 | fix_page_size:1, /* reinitialize page size */ | |
9eeba70e SK |
129 | no_heading:1, /* toggle --show headers */ |
130 | raw:1, /* toggle --show alignment */ | |
131 | show:1, /* display --show information */ | |
132 | verbose:1; /* be chatty */ | |
133 | }; | |
e076b552 SK |
134 | |
135 | static int column_name_to_id(const char *name, size_t namesz) | |
136 | { | |
137 | size_t i; | |
138 | ||
139 | assert(name); | |
140 | ||
a560a348 | 141 | for (i = 0; i < ARRAY_SIZE(infos); i++) { |
e076b552 SK |
142 | const char *cn = infos[i].name; |
143 | ||
144 | if (!strncasecmp(name, cn, namesz) && !*(cn + namesz)) | |
145 | return i; | |
146 | } | |
147 | warnx(_("unknown column: %s"), name); | |
148 | return -1; | |
149 | } | |
150 | ||
9eeba70e | 151 | static inline int get_column_id(const struct swapon_ctl *ctl, int num) |
e076b552 | 152 | { |
9eeba70e SK |
153 | assert(num < ctl->ncolumns); |
154 | assert(ctl->columns[num] < (int) ARRAY_SIZE(infos)); | |
e076b552 | 155 | |
9eeba70e | 156 | return ctl->columns[num]; |
e076b552 SK |
157 | } |
158 | ||
9eeba70e | 159 | static inline struct colinfo *get_column_info(const struct swapon_ctl *ctl, unsigned num) |
e076b552 | 160 | { |
9eeba70e | 161 | return &infos[get_column_id(ctl, num)]; |
e076b552 SK |
162 | } |
163 | ||
9eeba70e | 164 | static void add_scols_line(const struct swapon_ctl *ctl, struct libscols_table *table, struct libmnt_fs *fs) |
e076b552 SK |
165 | { |
166 | int i; | |
248a7f70 | 167 | struct libscols_line *line; |
5db57cfc SK |
168 | blkid_probe pr = NULL; |
169 | const char *data; | |
e076b552 | 170 | |
248a7f70 | 171 | assert(table); |
e076b552 SK |
172 | assert(fs); |
173 | ||
248a7f70 | 174 | line = scols_table_new_line(table, NULL); |
e5166348 | 175 | if (!line) |
780ce22c KZ |
176 | err(EXIT_FAILURE, _("failed to allocate output line")); |
177 | ||
5db57cfc SK |
178 | data = mnt_fs_get_source(fs); |
179 | if (access(data, R_OK) == 0) | |
180 | pr = get_swap_prober(data); | |
9eeba70e | 181 | for (i = 0; i < ctl->ncolumns; i++) { |
e076b552 | 182 | char *str = NULL; |
7ae8b469 | 183 | off_t size; |
e076b552 | 184 | |
9eeba70e | 185 | switch (get_column_id(ctl, i)) { |
e076b552 | 186 | case COL_PATH: |
a0e35e30 | 187 | xasprintf(&str, "%s", mnt_fs_get_source(fs)); |
e076b552 SK |
188 | break; |
189 | case COL_TYPE: | |
a0e35e30 | 190 | xasprintf(&str, "%s", mnt_fs_get_swaptype(fs)); |
e076b552 SK |
191 | break; |
192 | case COL_SIZE: | |
7ae8b469 SK |
193 | size = mnt_fs_get_size(fs); |
194 | size *= 1024; /* convert to bytes */ | |
9eeba70e | 195 | if (ctl->bytes) |
a0e35e30 | 196 | xasprintf(&str, "%jd", size); |
7ae8b469 SK |
197 | else |
198 | str = size_to_human_string(SIZE_SUFFIX_1LETTER, size); | |
e076b552 SK |
199 | break; |
200 | case COL_USED: | |
7ae8b469 SK |
201 | size = mnt_fs_get_usedsize(fs); |
202 | size *= 1024; /* convert to bytes */ | |
9eeba70e | 203 | if (ctl->bytes) |
a0e35e30 | 204 | xasprintf(&str, "%jd", size); |
7ae8b469 SK |
205 | else |
206 | str = size_to_human_string(SIZE_SUFFIX_1LETTER, size); | |
e076b552 SK |
207 | break; |
208 | case COL_PRIO: | |
a0e35e30 | 209 | xasprintf(&str, "%d", mnt_fs_get_priority(fs)); |
e076b552 | 210 | break; |
5db57cfc SK |
211 | case COL_UUID: |
212 | if (pr && !blkid_probe_lookup_value(pr, "UUID", &data, NULL)) | |
213 | xasprintf(&str, "%s", data); | |
5db57cfc SK |
214 | break; |
215 | case COL_LABEL: | |
216 | if (pr && !blkid_probe_lookup_value(pr, "LABEL", &data, NULL)) | |
217 | xasprintf(&str, "%s", data); | |
5db57cfc | 218 | break; |
e076b552 SK |
219 | default: |
220 | break; | |
221 | } | |
222 | ||
780ce22c KZ |
223 | if (str && scols_line_refer_data(line, i, str)) |
224 | err(EXIT_FAILURE, _("failed to add output data")); | |
e076b552 | 225 | } |
5db57cfc SK |
226 | if (pr) |
227 | blkid_free_probe(pr); | |
e076b552 SK |
228 | return; |
229 | } | |
230 | ||
d335c9bd | 231 | static int display_summary(void) |
fd6b7a7f | 232 | { |
5072b90a | 233 | struct libmnt_table *st = get_swaps(); |
64b58881 KZ |
234 | struct libmnt_iter *itr; |
235 | struct libmnt_fs *fs; | |
fd6b7a7f | 236 | |
64b58881 | 237 | if (!st) |
57a60bf0 | 238 | return -1; |
e8f26419 | 239 | |
a2f17bb2 KZ |
240 | if (mnt_table_is_empty(st)) |
241 | return 0; | |
242 | ||
64b58881 KZ |
243 | itr = mnt_new_iter(MNT_ITER_FORWARD); |
244 | if (!itr) | |
245 | err(EXIT_FAILURE, _("failed to initialize libmount iterator")); | |
57a60bf0 | 246 | |
a2f17bb2 | 247 | printf(_("%s\t\t\t\tType\t\tSize\tUsed\tPriority\n"), _("Filename")); |
64b58881 KZ |
248 | |
249 | while (mnt_table_next_fs(st, itr, &fs) == 0) { | |
74712441 | 250 | printf("%-39s\t%-8s\t%jd\t%jd\t%d\n", |
64b58881 KZ |
251 | mnt_fs_get_source(fs), |
252 | mnt_fs_get_swaptype(fs), | |
253 | mnt_fs_get_size(fs), | |
254 | mnt_fs_get_usedsize(fs), | |
255 | mnt_fs_get_priority(fs)); | |
57a60bf0 | 256 | } |
fd6b7a7f | 257 | |
64b58881 KZ |
258 | mnt_free_iter(itr); |
259 | return 0; | |
fd6b7a7f KZ |
260 | } |
261 | ||
9eeba70e | 262 | static int show_table(struct swapon_ctl *ctl) |
e076b552 SK |
263 | { |
264 | struct libmnt_table *st = get_swaps(); | |
880f09c8 | 265 | struct libmnt_iter *itr = NULL; |
e076b552 | 266 | struct libmnt_fs *fs; |
e5166348 | 267 | int i; |
248a7f70 | 268 | struct libscols_table *table = NULL; |
e076b552 SK |
269 | |
270 | if (!st) | |
271 | return -1; | |
272 | ||
273 | itr = mnt_new_iter(MNT_ITER_FORWARD); | |
274 | if (!itr) | |
275 | err(EXIT_FAILURE, _("failed to initialize libmount iterator")); | |
276 | ||
710ed55d KZ |
277 | scols_init_debug(0); |
278 | ||
0925a9dd | 279 | table = scols_new_table(); |
e5166348 | 280 | if (!table) |
780ce22c | 281 | err(EXIT_FAILURE, _("failed to allocate output table")); |
e5166348 | 282 | |
9eeba70e SK |
283 | scols_table_enable_raw(table, ctl->raw); |
284 | scols_table_enable_noheadings(table, ctl->no_heading); | |
e076b552 | 285 | |
9eeba70e SK |
286 | for (i = 0; i < ctl->ncolumns; i++) { |
287 | struct colinfo *col = get_column_info(ctl, i); | |
e076b552 | 288 | |
e5166348 | 289 | if (!scols_table_new_column(table, col->name, col->whint, col->flags)) |
780ce22c | 290 | err(EXIT_FAILURE, _("failed to allocate output column")); |
e076b552 SK |
291 | } |
292 | ||
293 | while (mnt_table_next_fs(st, itr, &fs) == 0) | |
9eeba70e | 294 | add_scols_line(ctl, table, fs); |
e076b552 | 295 | |
248a7f70 | 296 | scols_print_table(table); |
248a7f70 | 297 | scols_unref_table(table); |
e5166348 KZ |
298 | mnt_free_iter(itr); |
299 | return 0; | |
e076b552 SK |
300 | } |
301 | ||
6274b987 | 302 | /* calls mkswap */ |
6c44094a | 303 | static int swap_reinitialize(struct swap_device *dev) |
b5b2c388 | 304 | { |
6274b987 KC |
305 | pid_t pid; |
306 | int status, ret; | |
2dd3ba2c | 307 | char const *cmd[7]; |
6274b987 KC |
308 | int idx=0; |
309 | ||
6c44094a KZ |
310 | assert(dev); |
311 | assert(dev->path); | |
312 | ||
313 | warnx(_("%s: reinitializing the swap."), dev->path); | |
0d761944 | 314 | |
a125123f | 315 | switch ((pid=fork())) { |
6274b987 | 316 | case -1: /* fork error */ |
d51d05d3 | 317 | warn(_("fork failed")); |
6274b987 KC |
318 | return -1; |
319 | ||
320 | case 0: /* child */ | |
eac478dc KZ |
321 | if (geteuid() != getuid()) { |
322 | /* in case someone uses swapon as setuid binary */ | |
323 | if (setgid(getgid()) < 0) | |
324 | exit(EXIT_FAILURE); | |
325 | if (setuid(getuid()) < 0) | |
326 | exit(EXIT_FAILURE); | |
327 | } | |
328 | ||
f1bc5996 | 329 | cmd[idx++] = "mkswap"; |
6c44094a | 330 | if (dev->label) { |
6274b987 | 331 | cmd[idx++] = "-L"; |
6c44094a | 332 | cmd[idx++] = dev->label; |
6274b987 | 333 | } |
6c44094a | 334 | if (dev->uuid) { |
6274b987 | 335 | cmd[idx++] = "-U"; |
6c44094a | 336 | cmd[idx++] = dev->uuid; |
6274b987 | 337 | } |
6c44094a | 338 | cmd[idx++] = dev->path; |
6274b987 | 339 | cmd[idx++] = NULL; |
2dd3ba2c | 340 | execvp(cmd[0], (char * const *) cmd); |
07ff972e | 341 | err(EXIT_FAILURE, _("failed to execute %s"), cmd[0]); |
6274b987 KC |
342 | |
343 | default: /* parent */ | |
344 | do { | |
048811c9 KZ |
345 | ret = waitpid(pid, &status, 0); |
346 | } while (ret == -1 && errno == EINTR); | |
347 | ||
348 | if (ret < 0) { | |
349 | warn(_("waitpid failed")); | |
350 | return -1; | |
351 | } | |
6274b987 KC |
352 | |
353 | /* mkswap returns: 0=suss, 1=error */ | |
354 | if (WIFEXITED(status) && WEXITSTATUS(status)==0) | |
355 | return 0; /* ok */ | |
f8ff4ebb | 356 | break; |
6274b987 KC |
357 | } |
358 | return -1; /* error */ | |
359 | } | |
360 | ||
6c44094a KZ |
361 | /* Replaces unwanted SWSUSPEND signature with swap signature */ |
362 | static int swap_rewrite_signature(const struct swap_device *dev) | |
617d8a3b KZ |
363 | { |
364 | int fd, rc = -1; | |
365 | ||
6c44094a KZ |
366 | assert(dev); |
367 | assert(dev->path); | |
368 | assert(dev->pagesize); | |
369 | ||
370 | fd = open(dev->path, O_WRONLY); | |
617d8a3b | 371 | if (fd == -1) { |
6c44094a | 372 | warn(_("cannot open %s"), dev->path); |
617d8a3b KZ |
373 | return -1; |
374 | } | |
375 | ||
6c44094a KZ |
376 | if (lseek(fd, dev->pagesize - SWAP_SIGNATURE_SZ, SEEK_SET) < 0) { |
377 | warn(_("%s: lseek failed"), dev->path); | |
617d8a3b | 378 | goto err; |
5dd53f43 | 379 | } |
617d8a3b KZ |
380 | |
381 | if (write(fd, (void *) SWAP_SIGNATURE, | |
5dd53f43 | 382 | SWAP_SIGNATURE_SZ) != SWAP_SIGNATURE_SZ) { |
6c44094a | 383 | warn(_("%s: write signature failed"), dev->path); |
617d8a3b | 384 | goto err; |
5dd53f43 | 385 | } |
617d8a3b KZ |
386 | |
387 | rc = 0; | |
388 | err: | |
14699715 | 389 | if (close_fd(fd) != 0) { |
6c44094a | 390 | warn(_("write failed: %s"), dev->path); |
14699715 SK |
391 | rc = -1; |
392 | } | |
617d8a3b KZ |
393 | return rc; |
394 | } | |
395 | ||
d335c9bd | 396 | static int swap_detect_signature(const char *buf, int *sig) |
3399a218 | 397 | { |
6c44094a KZ |
398 | assert(buf); |
399 | assert(sig); | |
400 | ||
eb397d19 | 401 | if (memcmp(buf, SWAP_SIGNATURE, SWAP_SIGNATURE_SZ) == 0) |
e8be80dd KZ |
402 | *sig = SIG_SWAPSPACE; |
403 | ||
404 | else if (memcmp(buf, "S1SUSPEND", 9) == 0 || | |
405 | memcmp(buf, "S2SUSPEND", 9) == 0 || | |
406 | memcmp(buf, "ULSUSPEND", 9) == 0 || | |
5a16af58 KZ |
407 | memcmp(buf, "\xed\xc3\x02\xe9\x98\x56\xe5\x0c", 8) == 0 || |
408 | memcmp(buf, "LINHIB0001", 10) == 0) | |
e8be80dd KZ |
409 | *sig = SIG_SWSUSPEND; |
410 | else | |
411 | return 0; | |
3399a218 | 412 | |
e8be80dd | 413 | return 1; |
3399a218 MK |
414 | } |
415 | ||
6c44094a | 416 | static char *swap_get_header(int fd, int *sig, unsigned int *pagesize) |
3399a218 | 417 | { |
3399a218 | 418 | char *buf; |
e8be80dd KZ |
419 | ssize_t datasz; |
420 | unsigned int page; | |
3399a218 | 421 | |
6c44094a KZ |
422 | assert(sig); |
423 | assert(pagesize); | |
424 | ||
425 | *pagesize = 0; | |
e8be80dd | 426 | *sig = 0; |
3399a218 | 427 | |
bfbb12d2 | 428 | buf = xmalloc(MAX_PAGESIZE); |
3399a218 MK |
429 | |
430 | datasz = read(fd, buf, MAX_PAGESIZE); | |
e8be80dd KZ |
431 | if (datasz == (ssize_t) -1) |
432 | goto err; | |
3399a218 MK |
433 | |
434 | for (page = 0x1000; page <= MAX_PAGESIZE; page <<= 1) { | |
435 | /* skip 32k pagesize since this does not seem to | |
436 | * be supported */ | |
437 | if (page == 0x8000) | |
438 | continue; | |
439 | /* the smallest swap area is PAGE_SIZE*10, it means | |
440 | * 40k, that's less than MAX_PAGESIZE */ | |
24f010e8 | 441 | if (datasz < 0 || (size_t) datasz < (page - SWAP_SIGNATURE_SZ)) |
3399a218 | 442 | break; |
617d8a3b | 443 | if (swap_detect_signature(buf + page - SWAP_SIGNATURE_SZ, sig)) { |
6c44094a | 444 | *pagesize = page; |
3399a218 MK |
445 | break; |
446 | } | |
447 | } | |
448 | ||
6c44094a | 449 | if (*pagesize) |
e8be80dd | 450 | return buf; |
3399a218 | 451 | err: |
e8be80dd KZ |
452 | free(buf); |
453 | return NULL; | |
454 | } | |
455 | ||
456 | /* returns real size of swap space */ | |
6c44094a KZ |
457 | static unsigned long long swap_get_size(const struct swap_device *dev, |
458 | const char *hdr) | |
e8be80dd KZ |
459 | { |
460 | unsigned int last_page = 0; | |
8a101b14 | 461 | const unsigned int swap_version = SWAP_VERSION; |
e8be80dd KZ |
462 | struct swap_header_v1_2 *s; |
463 | ||
6c44094a KZ |
464 | assert(dev); |
465 | assert(dev->pagesize > 0); | |
466 | ||
e8be80dd | 467 | s = (struct swap_header_v1_2 *) hdr; |
6c44094a KZ |
468 | |
469 | if (s->version == swap_version) | |
e8be80dd | 470 | last_page = s->last_page; |
6c44094a | 471 | else if (swab32(s->version) == swap_version) |
e8be80dd | 472 | last_page = swab32(s->last_page); |
6c44094a KZ |
473 | |
474 | return ((unsigned long long) last_page + 1) * dev->pagesize; | |
3399a218 MK |
475 | } |
476 | ||
6c44094a | 477 | static void swap_get_info(struct swap_device *dev, const char *hdr) |
b5b2c388 KZ |
478 | { |
479 | struct swap_header_v1_2 *s = (struct swap_header_v1_2 *) hdr; | |
480 | ||
6c44094a KZ |
481 | assert(dev); |
482 | ||
a125123f | 483 | if (s && *s->volume_name) |
6c44094a | 484 | dev->label = xstrdup(s->volume_name); |
b5b2c388 | 485 | |
a125123f | 486 | if (s && *s->uuid) { |
b5b2c388 KZ |
487 | const unsigned char *u = s->uuid; |
488 | char str[37]; | |
489 | ||
490 | snprintf(str, sizeof(str), | |
491 | "%02x%02x%02x%02x-" | |
492 | "%02x%02x-%02x%02x-" | |
493 | "%02x%02x-%02x%02x%02x%02x%02x%02x", | |
494 | u[0], u[1], u[2], u[3], | |
495 | u[4], u[5], u[6], u[7], | |
496 | u[8], u[9], u[10], u[11], u[12], u[13], u[14], u[15]); | |
6c44094a | 497 | dev->uuid = xstrdup(str); |
b5b2c388 KZ |
498 | } |
499 | } | |
500 | ||
6c44094a | 501 | static int swapon_checks(const struct swapon_ctl *ctl, struct swap_device *dev) |
05c79b78 | 502 | { |
63cccae4 | 503 | struct stat st; |
1b504263 | 504 | int fd, sig; |
05c79b78 | 505 | char *hdr = NULL; |
e8be80dd | 506 | unsigned long long devsize = 0; |
cd04b26b | 507 | int permMask; |
63cccae4 | 508 | |
6c44094a KZ |
509 | assert(ctl); |
510 | assert(dev); | |
511 | assert(dev->path); | |
512 | ||
513 | fd = open(dev->path, O_RDONLY); | |
8df370d3 | 514 | if (fd == -1) { |
6c44094a | 515 | warn(_("cannot open %s"), dev->path); |
8df370d3 SK |
516 | goto err; |
517 | } | |
518 | ||
519 | if (fstat(fd, &st) < 0) { | |
6c44094a | 520 | warn(_("stat of %s failed"), dev->path); |
05c79b78 | 521 | goto err; |
63cccae4 KZ |
522 | } |
523 | ||
cd04b26b KZ |
524 | permMask = S_ISBLK(st.st_mode) ? 07007 : 07077; |
525 | if ((st.st_mode & permMask) != 0) | |
526 | warnx(_("%s: insecure permissions %04o, %04o suggested."), | |
6c44094a | 527 | dev->path, st.st_mode & 07777, |
88530f9f | 528 | ~permMask & 0666); |
306c1df2 | 529 | |
cd04b26b KZ |
530 | if (S_ISREG(st.st_mode) && st.st_uid != 0) |
531 | warnx(_("%s: insecure file owner %d, 0 (root) suggested."), | |
6c44094a | 532 | dev->path, st.st_uid); |
88530f9f KZ |
533 | |
534 | /* test for holes by LBT */ | |
535 | if (S_ISREG(st.st_mode)) { | |
536 | if (st.st_blocks * 512 < st.st_size) { | |
d51d05d3 | 537 | warnx(_("%s: skipping - it appears to have holes."), |
6c44094a | 538 | dev->path); |
05c79b78 | 539 | goto err; |
88530f9f | 540 | } |
e8be80dd KZ |
541 | devsize = st.st_size; |
542 | } | |
543 | ||
05c79b78 | 544 | if (S_ISBLK(st.st_mode) && blkdev_get_size(fd, &devsize)) { |
6c44094a | 545 | warnx(_("%s: get size failed"), dev->path); |
05c79b78 | 546 | goto err; |
e8be80dd KZ |
547 | } |
548 | ||
6c44094a | 549 | hdr = swap_get_header(fd, &sig, &dev->pagesize); |
e8be80dd | 550 | if (!hdr) { |
6c44094a | 551 | warnx(_("%s: read swap header failed"), dev->path); |
05c79b78 | 552 | goto err; |
88530f9f KZ |
553 | } |
554 | ||
6c44094a KZ |
555 | if (ctl->verbose) |
556 | warnx(_("%s: found signature [pagesize=%d, signature=%s]"), | |
557 | dev->path, | |
558 | dev->pagesize, | |
559 | sig == SIG_SWAPSPACE ? "swap" : | |
560 | sig == SIG_SWSUSPEND ? "suspend" : "unknown"); | |
561 | ||
562 | if (sig == SIG_SWAPSPACE && dev->pagesize) { | |
563 | unsigned long long swapsize = swap_get_size(dev, hdr); | |
24f010e8 KZ |
564 | int syspg = getpagesize(); |
565 | ||
9eeba70e | 566 | if (ctl->verbose) |
e6b0611b | 567 | warnx(_("%s: pagesize=%d, swapsize=%llu, devsize=%llu"), |
6c44094a | 568 | dev->path, dev->pagesize, swapsize, devsize); |
e8be80dd KZ |
569 | |
570 | if (swapsize > devsize) { | |
9eeba70e | 571 | if (ctl->verbose) |
d51d05d3 KZ |
572 | warnx(_("%s: last_page 0x%08llx is larger" |
573 | " than actual size of swapspace"), | |
6c44094a KZ |
574 | dev->path, swapsize); |
575 | ||
576 | } else if (syspg < 0 || (unsigned int) syspg != dev->pagesize) { | |
9eeba70e | 577 | if (ctl->fix_page_size) { |
b5b2c388 KZ |
578 | int rc; |
579 | ||
6c44094a | 580 | swap_get_info(dev, hdr); |
b5b2c388 | 581 | |
93bfcc94 | 582 | warnx(_("%s: swap format pagesize does not match."), |
6c44094a KZ |
583 | dev->path); |
584 | rc = swap_reinitialize(dev); | |
b5b2c388 | 585 | if (rc < 0) |
0d761944 KZ |
586 | goto err; |
587 | } else | |
93bfcc94 KZ |
588 | warnx(_("%s: swap format pagesize does not match. " |
589 | "(Use --fixpgsz to reinitialize it.)"), | |
6c44094a | 590 | dev->path); |
e8be80dd KZ |
591 | } |
592 | } else if (sig == SIG_SWSUSPEND) { | |
593 | /* We have to reinitialize swap with old (=useless) software suspend | |
594 | * data. The problem is that if we don't do it, then we get data | |
595 | * corruption the next time an attempt at unsuspending is made. | |
596 | */ | |
d51d05d3 | 597 | warnx(_("%s: software suspend data detected. " |
617d8a3b | 598 | "Rewriting the swap signature."), |
6c44094a KZ |
599 | dev->path); |
600 | if (swap_rewrite_signature(dev) < 0) | |
05c79b78 | 601 | goto err; |
6274b987 KC |
602 | } |
603 | ||
05c79b78 KZ |
604 | free(hdr); |
605 | close(fd); | |
606 | return 0; | |
05c79b78 KZ |
607 | err: |
608 | if (fd != -1) | |
609 | close(fd); | |
610 | free(hdr); | |
611 | return -1; | |
612 | } | |
63cccae4 | 613 | |
6c44094a KZ |
614 | static int do_swapon(const struct swapon_ctl *ctl, |
615 | const struct swap_prop *prop, | |
616 | const char *spec, | |
617 | int canonic) | |
d335c9bd | 618 | { |
6c44094a | 619 | struct swap_device dev = { .path = NULL }; |
05c79b78 | 620 | int status; |
05c79b78 | 621 | int flags = 0; |
6c44094a | 622 | int priority; |
63cccae4 | 623 | |
6c44094a KZ |
624 | assert(ctl); |
625 | assert(prop); | |
05c79b78 | 626 | |
a125123f | 627 | if (!canonic) { |
6c44094a KZ |
628 | dev.path = mnt_resolve_spec(spec, mntcache); |
629 | if (!dev.path) | |
a125123f | 630 | return cannot_find(spec); |
2dd3ba2c | 631 | } else |
6c44094a | 632 | dev.path = spec; |
ffa63a3d | 633 | |
6c44094a KZ |
634 | priority = prop->priority; |
635 | ||
636 | if (swapon_checks(ctl, &dev)) | |
05c79b78 KZ |
637 | return -1; |
638 | ||
639 | #ifdef SWAP_FLAG_PREFER | |
6c44094a KZ |
640 | if (priority >= 0) { |
641 | if (priority > SWAP_FLAG_PRIO_MASK) | |
642 | priority = SWAP_FLAG_PRIO_MASK; | |
643 | ||
05c79b78 | 644 | flags = SWAP_FLAG_PREFER |
6c44094a | 645 | | ((priority & SWAP_FLAG_PRIO_MASK) |
05c79b78 KZ |
646 | << SWAP_FLAG_PRIO_SHIFT); |
647 | } | |
648 | #endif | |
d6387c98 RA |
649 | /* |
650 | * Validate the discard flags passed and set them | |
651 | * accordingly before calling sys_swapon. | |
652 | */ | |
6c44094a | 653 | if (prop->discard && !(prop->discard & ~SWAP_FLAGS_DISCARD_VALID)) { |
d6387c98 RA |
654 | /* |
655 | * If we get here with both discard policy flags set, | |
656 | * we just need to tell the kernel to enable discards | |
657 | * and it will do correctly, just as we expect. | |
658 | */ | |
6c44094a KZ |
659 | if ((prop->discard & SWAP_FLAG_DISCARD_ONCE) && |
660 | (prop->discard & SWAP_FLAG_DISCARD_PAGES)) | |
d6387c98 RA |
661 | flags |= SWAP_FLAG_DISCARD; |
662 | else | |
6c44094a | 663 | flags |= prop->discard; |
d6387c98 | 664 | } |
c2301380 | 665 | |
6c44094a KZ |
666 | if (ctl->verbose) |
667 | printf(_("swapon %s\n"), dev.path); | |
668 | ||
669 | status = swapon(dev.path, flags); | |
d51d05d3 | 670 | if (status < 0) |
6c44094a | 671 | warn(_("%s: swapon failed"), dev.path); |
63cccae4 KZ |
672 | |
673 | return status; | |
674 | } | |
675 | ||
a125123f | 676 | static int swapon_by_label(struct swapon_ctl *ctl, const char *label) |
d335c9bd | 677 | { |
a125123f | 678 | char *device = mnt_resolve_tag("LABEL", label, mntcache); |
6c44094a | 679 | return device ? do_swapon(ctl, &ctl->props, device, TRUE) : cannot_find(label); |
756bfd01 KZ |
680 | } |
681 | ||
a125123f | 682 | static int swapon_by_uuid(struct swapon_ctl *ctl, const char *uuid) |
d335c9bd | 683 | { |
a125123f | 684 | char *device = mnt_resolve_tag("UUID", uuid, mntcache); |
6c44094a | 685 | return device ? do_swapon(ctl, &ctl->props, device, TRUE) : cannot_find(uuid); |
756bfd01 KZ |
686 | } |
687 | ||
e07fdac4 | 688 | /* -o <options> or fstab */ |
6c44094a | 689 | static int parse_options(struct swap_prop *props, const char *options) |
e07fdac4 KZ |
690 | { |
691 | char *arg = NULL; | |
07332bfa | 692 | size_t argsz = 0; |
e07fdac4 | 693 | |
6c44094a KZ |
694 | assert(props); |
695 | assert(options); | |
e07fdac4 | 696 | |
87918040 | 697 | if (mnt_optstr_get_option(options, "nofail", NULL, NULL) == 0) |
6c44094a | 698 | props->no_fail = 1; |
e07fdac4 | 699 | |
07332bfa | 700 | if (mnt_optstr_get_option(options, "discard", &arg, &argsz) == 0) { |
6c44094a | 701 | props->discard |= SWAP_FLAG_DISCARD; |
e07fdac4 KZ |
702 | |
703 | if (arg) { | |
704 | /* only single-time discards are wanted */ | |
07332bfa | 705 | if (strncmp(arg, "once", argsz) == 0) |
6c44094a | 706 | props->discard |= SWAP_FLAG_DISCARD_ONCE; |
e07fdac4 KZ |
707 | |
708 | /* do discard for every released swap page */ | |
07332bfa | 709 | if (strncmp(arg, "pages", argsz) == 0) |
6c44094a KZ |
710 | props->discard |= SWAP_FLAG_DISCARD_PAGES; |
711 | } | |
e07fdac4 KZ |
712 | } |
713 | ||
714 | arg = NULL; | |
6c44094a KZ |
715 | if (mnt_optstr_get_option(options, "pri", &arg, NULL) == 0 && arg) |
716 | props->priority = atoi(arg); | |
e07fdac4 KZ |
717 | |
718 | return 0; | |
719 | } | |
720 | ||
721 | ||
9eeba70e | 722 | static int swapon_all(struct swapon_ctl *ctl) |
d335c9bd | 723 | { |
5072b90a KZ |
724 | struct libmnt_table *tb = get_fstab(); |
725 | struct libmnt_iter *itr; | |
726 | struct libmnt_fs *fs; | |
63cccae4 | 727 | int status = 0; |
63cccae4 | 728 | |
5072b90a | 729 | if (!tb) |
d335c9bd | 730 | err(EXIT_FAILURE, _("failed to parse %s"), mnt_get_fstab_path()); |
756bfd01 | 731 | |
5072b90a KZ |
732 | itr = mnt_new_iter(MNT_ITER_FORWARD); |
733 | if (!itr) | |
734 | err(EXIT_FAILURE, _("failed to initialize libmount iterator")); | |
756bfd01 | 735 | |
5072b90a KZ |
736 | while (mnt_table_find_next_fs(tb, itr, match_swap, NULL, &fs) == 0) { |
737 | /* defaults */ | |
2dd3ba2c | 738 | const char *opts; |
a125123f | 739 | const char *device; |
6c44094a | 740 | struct swap_prop prop; /* per device setting */ |
1b414d89 | 741 | |
6c44094a KZ |
742 | if (mnt_fs_get_option(fs, "noauto", NULL, NULL) == 0) { |
743 | if (ctl->verbose) | |
744 | warnx(_("%s: noauto option -- ignored"), mnt_fs_get_source(fs)); | |
1b414d89 | 745 | continue; |
6c44094a KZ |
746 | } |
747 | ||
748 | /* default setting */ | |
749 | prop = ctl->props; | |
e07fdac4 | 750 | |
6c44094a | 751 | /* overwrite default by setting from fstab */ |
e07fdac4 KZ |
752 | opts = mnt_fs_get_options(fs); |
753 | if (opts) | |
6c44094a | 754 | parse_options(&prop, opts); |
5072b90a | 755 | |
6c44094a | 756 | /* convert LABEL=, UUID= etc. from fstab to device name */ |
a125123f KZ |
757 | device = mnt_resolve_spec(mnt_fs_get_source(fs), mntcache); |
758 | if (!device) { | |
6c44094a | 759 | if (!prop.no_fail) |
5072b90a | 760 | status |= cannot_find(mnt_fs_get_source(fs)); |
756bfd01 | 761 | continue; |
1b414d89 | 762 | } |
756bfd01 | 763 | |
6c44094a KZ |
764 | if (is_active_swap(device)) { |
765 | if (ctl->verbose) | |
766 | warnx(_("%s: already active -- ignored"), device); | |
767 | continue; | |
768 | } | |
769 | ||
770 | if (prop.no_fail && access(device, R_OK) != 0) { | |
771 | if (ctl->verbose) | |
9e930041 | 772 | warnx(_("%s: inaccessible -- ignored"), device); |
6c44094a KZ |
773 | continue; |
774 | } | |
775 | ||
776 | /* swapon */ | |
777 | status |= do_swapon(ctl, &prop, device, TRUE); | |
756bfd01 | 778 | } |
756bfd01 | 779 | |
5072b90a | 780 | mnt_free_iter(itr); |
756bfd01 KZ |
781 | return status; |
782 | } | |
783 | ||
e07fdac4 | 784 | |
6e1eda6f | 785 | static void __attribute__((__noreturn__)) usage(void) |
8b0d5341 | 786 | { |
6e1eda6f | 787 | FILE *out = stdout; |
e09bd8d0 | 788 | size_t i; |
6e1eda6f | 789 | |
8b0d5341 | 790 | fputs(USAGE_HEADER, out); |
8b0d5341 KZ |
791 | fprintf(out, _(" %s [options] [<spec>]\n"), program_invocation_short_name); |
792 | ||
451dbcfa BS |
793 | fputs(USAGE_SEPARATOR, out); |
794 | fputs(_("Enable devices and files for paging and swapping.\n"), out); | |
795 | ||
8b0d5341 | 796 | fputs(USAGE_OPTIONS, out); |
6ee8e194 BS |
797 | fputs(_(" -a, --all enable all swaps from /etc/fstab\n"), out); |
798 | fputs(_(" -d, --discard[=<policy>] enable swap discards, if supported by device\n"), out); | |
799 | fputs(_(" -e, --ifexists silently skip devices that do not exist\n"), out); | |
800 | fputs(_(" -f, --fixpgsz reinitialize the swap space if necessary\n"), out); | |
801 | fputs(_(" -o, --options <list> comma-separated list of swap options\n"), out); | |
802 | fputs(_(" -p, --priority <prio> specify the priority of the swap device\n"), out); | |
803 | fputs(_(" -s, --summary display summary about used swap devices (DEPRECATED)\n"), out); | |
804 | fputs(_(" --show[=<columns>] display summary in definable table\n"), out); | |
805 | fputs(_(" --noheadings don't print table heading (with --show)\n"), out); | |
806 | fputs(_(" --raw use the raw output format (with --show)\n"), out); | |
807 | fputs(_(" --bytes display swap size in bytes in --show output\n"), out); | |
808 | fputs(_(" -v, --verbose verbose mode\n"), out); | |
8b0d5341 KZ |
809 | |
810 | fputs(USAGE_SEPARATOR, out); | |
811 | fputs(USAGE_HELP, out); | |
812 | fputs(USAGE_VERSION, out); | |
813 | ||
814 | fputs(_("\nThe <spec> parameter:\n" \ | |
9f9d0f21 KZ |
815 | " -L <label> synonym for LABEL=<label>\n" |
816 | " -U <uuid> synonym for UUID=<uuid>\n" | |
817 | " LABEL=<label> specifies device by swap area label\n" | |
818 | " UUID=<uuid> specifies device by swap area UUID\n" | |
819 | " PARTLABEL=<label> specifies device by partition label\n" | |
820 | " PARTUUID=<uuid> specifies device by partition UUID\n" | |
821 | " <device> name of device to be used\n" | |
8b0d5341 KZ |
822 | " <file> name of file to be used\n"), out); |
823 | ||
d6387c98 | 824 | fputs(_("\nAvailable discard policy types (for --discard):\n" |
54fefa07 BS |
825 | " once : only single-time area discards are issued\n" |
826 | " pages : freed pages are discarded before they are reused\n" | |
827 | "If no policy is selected, both discard types are enabled (default).\n"), out); | |
d6387c98 | 828 | |
c3a4cfc5 | 829 | fputs(USAGE_COLUMNS, out); |
a560a348 | 830 | for (i = 0; i < ARRAY_SIZE(infos); i++) |
a12124ce | 831 | fprintf(out, " %-5s %s\n", infos[i].name, _(infos[i].help)); |
e076b552 | 832 | |
8b0d5341 | 833 | fprintf(out, USAGE_MAN_TAIL("swapon(8)")); |
6e1eda6f | 834 | exit(EXIT_SUCCESS); |
8b0d5341 KZ |
835 | } |
836 | ||
d9f07b10 KZ |
837 | int main(int argc, char *argv[]) |
838 | { | |
8b0d5341 | 839 | int status = 0, c; |
0b0c231f | 840 | size_t i; |
6c44094a | 841 | char *options = NULL; |
756bfd01 | 842 | |
e076b552 | 843 | enum { |
e07fdac4 | 844 | BYTES_OPTION = CHAR_MAX + 1, |
7ae8b469 | 845 | NOHEADINGS_OPTION, |
e07fdac4 KZ |
846 | RAW_OPTION, |
847 | SHOW_OPTION | |
e076b552 SK |
848 | }; |
849 | ||
8b0d5341 | 850 | static const struct option long_opts[] = { |
87918040 SK |
851 | { "priority", required_argument, NULL, 'p' }, |
852 | { "discard", optional_argument, NULL, 'd' }, | |
853 | { "ifexists", no_argument, NULL, 'e' }, | |
854 | { "options", optional_argument, NULL, 'o' }, | |
855 | { "summary", no_argument, NULL, 's' }, | |
856 | { "fixpgsz", no_argument, NULL, 'f' }, | |
857 | { "all", no_argument, NULL, 'a' }, | |
858 | { "help", no_argument, NULL, 'h' }, | |
859 | { "verbose", no_argument, NULL, 'v' }, | |
860 | { "version", no_argument, NULL, 'V' }, | |
861 | { "show", optional_argument, NULL, SHOW_OPTION }, | |
862 | { "noheadings", no_argument, NULL, NOHEADINGS_OPTION }, | |
863 | { "raw", no_argument, NULL, RAW_OPTION }, | |
864 | { "bytes", no_argument, NULL, BYTES_OPTION }, | |
865 | { NULL, 0, NULL, 0 } | |
8b0d5341 KZ |
866 | }; |
867 | ||
a7349ee3 | 868 | static const ul_excl_t excl[] = { /* rows and cols in ASCII order */ |
e07fdac4 KZ |
869 | { 'a','o','s', SHOW_OPTION }, |
870 | { 'a','o', BYTES_OPTION }, | |
871 | { 'a','o', NOHEADINGS_OPTION }, | |
872 | { 'a','o', RAW_OPTION }, | |
873 | { 0 } | |
874 | }; | |
875 | int excl_st[ARRAY_SIZE(excl)] = UL_EXCL_STATUS_INIT; | |
876 | ||
6c44094a | 877 | struct swapon_ctl ctl; |
9eeba70e | 878 | |
d9f07b10 KZ |
879 | setlocale(LC_ALL, ""); |
880 | bindtextdomain(PACKAGE, LOCALEDIR); | |
881 | textdomain(PACKAGE); | |
882 | atexit(close_stdout); | |
883 | ||
6c44094a KZ |
884 | memset(&ctl, 0, sizeof(struct swapon_ctl)); |
885 | ctl.props.priority = -1; | |
886 | ||
d9f07b10 KZ |
887 | mnt_init_debug(0); |
888 | mntcache = mnt_new_cache(); | |
889 | ||
e07fdac4 | 890 | while ((c = getopt_long(argc, argv, "ahd::efo:p:svVL:U:", |
8b0d5341 | 891 | long_opts, NULL)) != -1) { |
e07fdac4 KZ |
892 | |
893 | err_exclusive_options(c, long_opts, excl, excl_st); | |
894 | ||
63cccae4 KZ |
895 | switch (c) { |
896 | case 'a': /* all */ | |
9eeba70e | 897 | ctl.all = 1; |
63cccae4 KZ |
898 | break; |
899 | case 'h': /* help */ | |
6e1eda6f | 900 | usage(); |
63cccae4 | 901 | break; |
e07fdac4 | 902 | case 'o': |
6c44094a | 903 | options = optarg; |
e07fdac4 | 904 | break; |
63cccae4 | 905 | case 'p': /* priority */ |
6c44094a | 906 | ctl.props.priority = strtos16_or_err(optarg, |
9db6a749 | 907 | _("failed to parse priority")); |
63cccae4 | 908 | break; |
756bfd01 | 909 | case 'L': |
0b0c231f | 910 | add_label(optarg); |
756bfd01 KZ |
911 | break; |
912 | case 'U': | |
0b0c231f | 913 | add_uuid(optarg); |
756bfd01 | 914 | break; |
c2301380 | 915 | case 'd': |
6c44094a | 916 | ctl.props.discard |= SWAP_FLAG_DISCARD; |
d6387c98 RA |
917 | if (optarg) { |
918 | if (*optarg == '=') | |
919 | optarg++; | |
920 | ||
921 | if (strcmp(optarg, "once") == 0) | |
6c44094a | 922 | ctl.props.discard |= SWAP_FLAG_DISCARD_ONCE; |
d6387c98 | 923 | else if (strcmp(optarg, "pages") == 0) |
6c44094a | 924 | ctl.props.discard |= SWAP_FLAG_DISCARD_PAGES; |
d6387c98 RA |
925 | else |
926 | errx(EXIT_FAILURE, _("unsupported discard policy: %s"), optarg); | |
927 | } | |
c2301380 | 928 | break; |
95f1bdee | 929 | case 'e': /* ifexists */ |
6c44094a | 930 | ctl.props.no_fail = 1; |
95f1bdee | 931 | break; |
0d761944 | 932 | case 'f': |
9eeba70e | 933 | ctl.fix_page_size = 1; |
0d761944 | 934 | break; |
63cccae4 KZ |
935 | case 's': /* status report */ |
936 | status = display_summary(); | |
8b0d5341 | 937 | return status; |
63cccae4 | 938 | case 'v': /* be chatty */ |
9eeba70e | 939 | ctl.verbose = 1; |
63cccae4 | 940 | break; |
e076b552 SK |
941 | case SHOW_OPTION: |
942 | if (optarg) { | |
9eeba70e SK |
943 | ctl.ncolumns = string_to_idarray(optarg, |
944 | ctl.columns, | |
945 | ARRAY_SIZE(ctl.columns), | |
e076b552 | 946 | column_name_to_id); |
9eeba70e | 947 | if (ctl.ncolumns < 0) |
e076b552 SK |
948 | return EXIT_FAILURE; |
949 | } | |
9eeba70e | 950 | ctl.show = 1; |
e076b552 SK |
951 | break; |
952 | case NOHEADINGS_OPTION: | |
9eeba70e | 953 | ctl.no_heading = 1; |
e076b552 SK |
954 | break; |
955 | case RAW_OPTION: | |
9eeba70e | 956 | ctl.raw = 1; |
e076b552 | 957 | break; |
7ae8b469 | 958 | case BYTES_OPTION: |
9eeba70e | 959 | ctl.bytes = 1; |
7ae8b469 | 960 | break; |
63cccae4 | 961 | case 'V': /* version */ |
d9f07b10 KZ |
962 | printf(UTIL_LINUX_VERSION); |
963 | return EXIT_SUCCESS; | |
63cccae4 KZ |
964 | case 0: |
965 | break; | |
63cccae4 | 966 | default: |
677ec86c | 967 | errtryhelp(EXIT_FAILURE); |
63cccae4 KZ |
968 | } |
969 | } | |
970 | argv += optind; | |
971 | ||
9eeba70e SK |
972 | if (ctl.show || (!ctl.all && !numof_labels() && !numof_uuids() && *argv == NULL)) { |
973 | if (!ctl.ncolumns) { | |
e076b552 | 974 | /* default columns */ |
9eeba70e SK |
975 | ctl.columns[ctl.ncolumns++] = COL_PATH; |
976 | ctl.columns[ctl.ncolumns++] = COL_TYPE; | |
977 | ctl.columns[ctl.ncolumns++] = COL_SIZE; | |
978 | ctl.columns[ctl.ncolumns++] = COL_USED; | |
979 | ctl.columns[ctl.ncolumns++] = COL_PRIO; | |
e076b552 | 980 | } |
9eeba70e | 981 | status = show_table(&ctl); |
e076b552 SK |
982 | return status; |
983 | } | |
984 | ||
6e1eda6f RM |
985 | if (ctl.props.no_fail && !ctl.all) { |
986 | warnx(_("bad usage")); | |
987 | errtryhelp(EXIT_FAILURE); | |
988 | } | |
95f1bdee | 989 | |
9eeba70e SK |
990 | if (ctl.all) |
991 | status |= swapon_all(&ctl); | |
63cccae4 | 992 | |
6c44094a KZ |
993 | if (options) |
994 | parse_options(&ctl.props, options); | |
e07fdac4 | 995 | |
a125123f KZ |
996 | for (i = 0; i < numof_labels(); i++) |
997 | status |= swapon_by_label(&ctl, get_label(i)); | |
63cccae4 | 998 | |
a125123f KZ |
999 | for (i = 0; i < numof_uuids(); i++) |
1000 | status |= swapon_by_uuid(&ctl, get_uuid(i)); | |
1001 | ||
1002 | while (*argv != NULL) | |
6c44094a | 1003 | status |= do_swapon(&ctl, &ctl.props, *argv++, FALSE); |
63cccae4 | 1004 | |
0b0c231f | 1005 | free_tables(); |
6195f9e6 | 1006 | mnt_unref_cache(mntcache); |
d9f07b10 | 1007 | |
64b58881 | 1008 | return status; |
6dbe3af9 | 1009 | } |