]> granicus.if.org Git - strace/blob - tests/swap.c
Fix decoding of swapon flags
[strace] / tests / swap.c
1 #include "tests.h"
2 #include <sys/syscall.h>
3
4 #if defined __NR_swapon && defined __NR_swapoff
5
6 # include <errno.h>
7 # include <stdio.h>
8 # include <sys/swap.h>
9 # include <unistd.h>
10
11 static const char *
12 error_msg(int error_num)
13 {
14         switch (error_num) {
15                 case ENOSYS: return "ENOSYS";
16                 case EPERM: return "EPERM";
17                 case EINVAL: return "EINVAL";
18                 default: return "ENOENT";
19         }
20 }
21
22 int
23 main(void)
24 {
25         static const char sample[] = "swap.sample";
26
27         int rc = syscall(__NR_swapon, sample, 0);
28         printf("swapon(\"%s\", 0) = %d %s (%m)\n",
29                sample, rc, error_msg(errno));
30
31         rc = syscall(__NR_swapon, sample, 42);
32         printf("swapon(\"%s\", %s) = %d %s (%m)\n",
33                sample, "42", rc, error_msg(errno));
34
35         rc = syscall(__NR_swapon, sample, SWAP_FLAG_PREFER);
36         printf("swapon(\"%s\", %s) = %d %s (%m)\n",
37                sample, "SWAP_FLAG_PREFER", rc, error_msg(errno));
38
39         rc = syscall(__NR_swapon, sample, SWAP_FLAG_PREFER | 42);
40         printf("swapon(\"%s\", %s) = %d %s (%m)\n",
41                sample, "SWAP_FLAG_PREFER|42", rc, error_msg(errno));
42
43         rc = syscall(__NR_swapon, sample, -1L);
44         printf("swapon(\"%s\", %s) = %d %s (%m)\n",
45                sample,
46                "SWAP_FLAG_PREFER|SWAP_FLAG_DISCARD|SWAP_FLAG_DISCARD_ONCE"
47                "|SWAP_FLAG_DISCARD_PAGES|0xfff80000|32767",
48                rc, error_msg(errno));
49
50         rc = syscall(__NR_swapoff, sample);
51         printf("swapoff(\"%s\") = %d %s (%m)\n",
52                sample, rc, error_msg(errno));
53
54         puts("+++ exited with 0 +++");
55         return 0;
56 }
57
58 #else
59
60 SKIP_MAIN_UNDEFINED("__NR_swapon && __NR_swapoff")
61
62 #endif