blob: afa686852bb909af20012f7af4fe5833959b6eef [file] [log] [blame]
Rom Lemarchand367297c2013-06-05 13:25:12 -07001#include <stdio.h>
2#include <stdlib.h>
3#include <unistd.h>
4#include <getopt.h>
5#include <asm/page.h>
6#include <sys/swap.h>
7
8/* XXX These need to be obtained from kernel headers. See b/9336527 */
9#define SWAP_FLAG_PREFER 0x8000
10#define SWAP_FLAG_PRIO_MASK 0x7fff
11#define SWAP_FLAG_PRIO_SHIFT 0
12#define SWAP_FLAG_DISCARD 0x10000
13
14void usage(char *name)
15{
16 fprintf(stderr, "Usage: %s [-p prio] <filename>\n"
17 " prio must be between 0 and %d\n", name, SWAP_FLAG_PRIO_MASK);
18}
19
20int parse_prio(char *prio_str)
21{
22 unsigned long p = strtoul(prio_str, NULL, 10);
23
24 return (p > SWAP_FLAG_PRIO_MASK)? -1 : (int)p;
25}
26
27int swapon_main(int argc, char **argv)
28{
29 int err = 0;
30 int flags = 0;
31 int prio;
32
33 opterr = 0;
34 do {
35 int c = getopt(argc, argv, "hp:");
36 if (c == -1)
37 break;
38
39 switch (c) {
40 case 'p':
41 if (optarg != NULL)
42 prio = parse_prio(optarg);
43 else
44 prio = -1;
45
46 if (prio < 0) {
47 usage(argv[0]);
48 return -EINVAL;
49 }
50 flags |= SWAP_FLAG_PREFER;
51 flags |= (prio << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK;
52 break;
53 case 'h':
54 usage(argv[0]);
55 return 0;
56 case '?':
57 fprintf(stderr, "unknown option: %c\n", optopt);
58 return -EINVAL;
59 }
60 } while (1);
61
62 if (optind != argc - 1) {
63 usage(argv[0]);
64 return -EINVAL;
65 }
66
67 err = swapon(argv[argc - 1], flags);
68 if (err) {
69 fprintf(stderr, "swapon failed for %s\n", argv[argc - 1]);
70 }
71
72 return err;
73}