Rom Lemarchand | 367297c | 2013-06-05 13:25:12 -0700 | [diff] [blame] | 1 | #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 | |
Rom Lemarchand | 367297c | 2013-06-05 13:25:12 -0700 | [diff] [blame] | 8 | void usage(char *name) |
| 9 | { |
| 10 | fprintf(stderr, "Usage: %s [-p prio] <filename>\n" |
| 11 | " prio must be between 0 and %d\n", name, SWAP_FLAG_PRIO_MASK); |
| 12 | } |
| 13 | |
| 14 | int parse_prio(char *prio_str) |
| 15 | { |
| 16 | unsigned long p = strtoul(prio_str, NULL, 10); |
| 17 | |
| 18 | return (p > SWAP_FLAG_PRIO_MASK)? -1 : (int)p; |
| 19 | } |
| 20 | |
| 21 | int swapon_main(int argc, char **argv) |
| 22 | { |
| 23 | int err = 0; |
| 24 | int flags = 0; |
| 25 | int prio; |
| 26 | |
| 27 | opterr = 0; |
| 28 | do { |
| 29 | int c = getopt(argc, argv, "hp:"); |
| 30 | if (c == -1) |
| 31 | break; |
| 32 | |
| 33 | switch (c) { |
| 34 | case 'p': |
| 35 | if (optarg != NULL) |
| 36 | prio = parse_prio(optarg); |
| 37 | else |
| 38 | prio = -1; |
| 39 | |
| 40 | if (prio < 0) { |
| 41 | usage(argv[0]); |
| 42 | return -EINVAL; |
| 43 | } |
| 44 | flags |= SWAP_FLAG_PREFER; |
| 45 | flags |= (prio << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK; |
| 46 | break; |
| 47 | case 'h': |
| 48 | usage(argv[0]); |
| 49 | return 0; |
| 50 | case '?': |
| 51 | fprintf(stderr, "unknown option: %c\n", optopt); |
| 52 | return -EINVAL; |
| 53 | } |
| 54 | } while (1); |
| 55 | |
| 56 | if (optind != argc - 1) { |
| 57 | usage(argv[0]); |
| 58 | return -EINVAL; |
| 59 | } |
| 60 | |
| 61 | err = swapon(argv[argc - 1], flags); |
| 62 | if (err) { |
| 63 | fprintf(stderr, "swapon failed for %s\n", argv[argc - 1]); |
| 64 | } |
| 65 | |
| 66 | return err; |
| 67 | } |