1 /*
2 * Copyright (c) 2015 Dmitry V. Levin <ldv@altlinux.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <stdio.h>
29 #include <sys/personality.h>
30
main(void)31 int main(void)
32 {
33 const unsigned int good_type = PER_BSD;
34 const char *good_type_str = "PER_BSD";
35
36 const unsigned int bad_type = 0x1f;
37 const char *bad_type_str = "0x1f /\\* PER_\\?\\?\\? \\*/";
38
39 const unsigned int good_flags =
40 SHORT_INODE | WHOLE_SECONDS | STICKY_TIMEOUTS;
41 const char *good_flags_str =
42 "SHORT_INODE\\|WHOLE_SECONDS\\|STICKY_TIMEOUTS";
43
44 const unsigned int bad_flags = 0x10000;
45 const char *bad_flags_str = "0x10000";
46
47 const unsigned int saved_pers = personality(0xffffffff);
48 printf("personality\\(0xffffffff\\) = %#x \\([^)]*\\)\n", saved_pers);
49
50 /* PER_LINUX */
51 personality(PER_LINUX);
52 printf("personality\\(PER_LINUX\\) = %#x \\([^)]*\\)\n", saved_pers);
53
54 personality(0xffffffff);
55 puts("personality\\(0xffffffff\\) = 0 \\(PER_LINUX\\)");
56
57 personality(good_flags);
58 printf("personality\\(PER_LINUX\\|%s\\) = 0 \\(PER_LINUX\\)\n",
59 good_flags_str);
60
61 personality(bad_flags);
62 printf("personality\\(PER_LINUX\\|%s\\)"
63 " = %#x \\(PER_LINUX\\|%s\\)\n",
64 bad_flags_str, good_flags, good_flags_str);
65
66 personality(good_flags | bad_flags);
67 printf("personality\\(PER_LINUX\\|%s\\|%s\\)"
68 " = %#x \\(PER_LINUX\\|%s\\)\n",
69 good_flags_str, bad_flags_str, bad_flags, bad_flags_str);
70
71 /* another valid type */
72 personality(good_type);
73 printf("personality\\(%s\\) = %#x \\(PER_LINUX\\|%s\\|%s\\)\n",
74 good_type_str, good_flags | bad_flags,
75 good_flags_str, bad_flags_str);
76
77 personality(good_type | good_flags);
78 printf("personality\\(%s\\|%s\\) = %#x \\(%s\\)\n",
79 good_type_str, good_flags_str, good_type, good_type_str);
80
81 personality(good_type | bad_flags);
82 printf("personality\\(%s\\|%s\\) = %#x \\(%s\\|%s\\)\n",
83 good_type_str, bad_flags_str, good_type | good_flags,
84 good_type_str, good_flags_str);
85
86 personality(good_type | good_flags | bad_flags);
87 printf("personality\\(%s\\|%s\\|%s\\) = %#x \\(%s\\|%s\\)\n",
88 good_type_str, good_flags_str, bad_flags_str,
89 good_type | bad_flags,
90 good_type_str, bad_flags_str);
91
92 /* invalid type */
93 personality(bad_type);
94 printf("personality\\(%s\\) = %#x \\(%s\\|%s\\|%s\\)\n",
95 bad_type_str, good_type | good_flags | bad_flags,
96 good_type_str, good_flags_str, bad_flags_str);
97
98 personality(bad_type | good_flags);
99 printf("personality\\(%s\\|%s\\) = %#x \\(%s\\)\n",
100 bad_type_str, good_flags_str, bad_type, bad_type_str);
101
102 personality(bad_type | bad_flags);
103 printf("personality\\(%s\\|%s\\) = %#x \\(%s\\|%s\\)\n",
104 bad_type_str, bad_flags_str, bad_type | good_flags,
105 bad_type_str, good_flags_str);
106
107 personality(bad_type | good_flags | bad_flags);
108 printf("personality\\(%s\\|%s\\|%s\\) = %#x \\(%s\\|%s\\)\n",
109 bad_type_str, good_flags_str, bad_flags_str,
110 bad_type | bad_flags, bad_type_str, bad_flags_str);
111
112 personality(saved_pers);
113 printf("personality\\([^)]*\\) = %#x \\(%s\\|%s\\|%s\\)\n",
114 bad_type | good_flags | bad_flags,
115 bad_type_str, good_flags_str, bad_flags_str);
116
117 return 0;
118 }
119