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