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