Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/x86/syscall: Simplify message reporting in syscall_numbering

Reduce some boiler plate in printing and indenting messages.
This makes it easier to produce clean status output.

Signed-off-by: H. Peter Anvin (Intel) <hpa@zytor.com>
Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
Link: https://lore.kernel.org/r/20210518191303.4135296-3-hpa@zytor.com

authored by

H. Peter Anvin (Intel) and committed by
Thomas Gleixner
c5c39488 15c82d98

+72 -31
+72 -31
tools/testing/selftests/x86/syscall_numbering.c
··· 16 16 #include <string.h> 17 17 #include <fcntl.h> 18 18 #include <limits.h> 19 + #include <sysexits.h> 19 20 20 21 /* Common system call numbers */ 21 22 #define SYS_READ 0 ··· 35 34 36 35 static unsigned int nerr = 0; /* Cumulative error count */ 37 36 static int nullfd = -1; /* File descriptor for /dev/null */ 37 + static int indent = 0; 38 + 39 + static inline unsigned int offset(void) 40 + { 41 + return 8 + indent * 4; 42 + } 43 + 44 + #define msg(lvl, fmt, ...) printf("%-*s" fmt, offset(), "[" #lvl "]", \ 45 + ## __VA_ARGS__) 46 + 47 + #define run(fmt, ...) msg(RUN, fmt, ## __VA_ARGS__) 48 + #define info(fmt, ...) msg(INFO, fmt, ## __VA_ARGS__) 49 + #define ok(fmt, ...) msg(OK, fmt, ## __VA_ARGS__) 50 + 51 + #define fail(fmt, ...) \ 52 + do { \ 53 + msg(FAIL, fmt, ## __VA_ARGS__); \ 54 + nerr++; \ 55 + } while (0) 56 + 57 + #define crit(fmt, ...) \ 58 + do { \ 59 + indent = 0; \ 60 + msg(FAIL, fmt, ## __VA_ARGS__); \ 61 + msg(SKIP, "Unable to run test\n"); \ 62 + exit(EX_OSERR); 63 + } while (0) 38 64 39 65 /* 40 66 * Directly invokes the given syscall with nullfd as the first argument ··· 119 91 { 120 92 unsigned int err = 0; 121 93 94 + indent++; 95 + if (start != end) 96 + indent++; 97 + 122 98 for (int nr = start; nr <= end; nr++) { 123 99 long long ret = probe_syscall(msb, nr); 124 100 125 101 if (ret != expect) { 126 - printf("[FAIL]\t %s returned %lld, but it should have returned %s\n", 102 + fail("%s returned %lld, but it should have returned %s\n", 127 103 syscall_str(msb, nr, nr), 128 104 ret, expect_str); 129 105 err++; 130 106 } 131 107 } 132 108 109 + if (start != end) 110 + indent--; 111 + 133 112 if (err) { 134 113 nerr += err; 135 114 if (start != end) 136 - printf("[FAIL]\t %s had %u failure%s\n", 115 + fail("%s had %u failure%s\n", 137 116 syscall_str(msb, start, end), 138 - err, (err == 1) ? "s" : ""); 117 + err, err == 1 ? "s" : ""); 139 118 } else { 140 - printf("[OK]\t %s returned %s as expected\n", 141 - syscall_str(msb, start, end), expect_str); 119 + ok("%s returned %s as expected\n", 120 + syscall_str(msb, start, end), expect_str); 142 121 } 122 + 123 + indent--; 143 124 144 125 return err; 145 126 } ··· 174 137 static bool test_x32(void) 175 138 { 176 139 long long ret; 177 - long long mypid = getpid(); 140 + pid_t mypid = getpid(); 141 + bool with_x32; 178 142 179 - printf("[RUN]\tChecking for x32 by calling x32 getpid()\n"); 143 + run("Checking for x32 by calling x32 getpid()\n"); 180 144 ret = probe_syscall(0, SYS_GETPID | X32_BIT); 181 145 146 + indent++; 182 147 if (ret == mypid) { 183 - printf("[INFO]\t x32 is supported\n"); 184 - return true; 148 + info("x32 is supported\n"); 149 + with_x32 = true; 185 150 } else if (ret == -ENOSYS) { 186 - printf("[INFO]\t x32 is not supported\n"); 187 - return false; 151 + info("x32 is not supported\n"); 152 + with_x32 = false; 188 153 } else { 189 - printf("[FAIL]\t x32 getpid() returned %lld, but it should have returned either %lld or -ENOSYS\n", ret, mypid); 190 - nerr++; 191 - return true; /* Proceed as if... */ 154 + fail("x32 getpid() returned %lld, but it should have returned either %lld or -ENOSYS\n", ret, mypid); 155 + with_x32 = false; 192 156 } 157 + indent--; 158 + return with_x32; 193 159 } 194 160 195 161 static void test_syscalls_common(int msb) 196 162 { 197 - printf("[RUN]\t Checking some common syscalls as 64 bit\n"); 163 + run("Checking some common syscalls as 64 bit\n"); 198 164 check_zero(msb, SYS_READ); 199 165 check_zero(msb, SYS_WRITE); 200 166 201 - printf("[RUN]\t Checking some 64-bit only syscalls as 64 bit\n"); 167 + run("Checking some 64-bit only syscalls as 64 bit\n"); 202 168 check_zero(msb, X64_READV); 203 169 check_zero(msb, X64_WRITEV); 204 170 205 - printf("[RUN]\t Checking out of range system calls\n"); 171 + run("Checking out of range system calls\n"); 206 172 check_for(msb, -64, -1, -ENOSYS); 207 173 check_for(msb, X32_BIT-64, X32_BIT-1, -ENOSYS); 208 174 check_for(msb, -64-X32_BIT, -1-X32_BIT, -ENOSYS); ··· 220 180 * set. Calling them without the x32 bit set is 221 181 * nonsense and should not work. 222 182 */ 223 - printf("[RUN]\t Checking x32 syscalls as 64 bit\n"); 183 + run("Checking x32 syscalls as 64 bit\n"); 224 184 check_for(msb, 512, 547, -ENOSYS); 225 185 226 - printf("[RUN]\t Checking some common syscalls as x32\n"); 186 + run("Checking some common syscalls as x32\n"); 227 187 check_zero(msb, SYS_READ | X32_BIT); 228 188 check_zero(msb, SYS_WRITE | X32_BIT); 229 189 230 - printf("[RUN]\t Checking some x32 syscalls as x32\n"); 190 + run("Checking some x32 syscalls as x32\n"); 231 191 check_zero(msb, X32_READV | X32_BIT); 232 192 check_zero(msb, X32_WRITEV | X32_BIT); 233 193 234 - printf("[RUN]\t Checking some 64-bit syscalls as x32\n"); 194 + run("Checking some 64-bit syscalls as x32\n"); 235 195 check_enosys(msb, X64_IOCTL | X32_BIT); 236 196 check_enosys(msb, X64_READV | X32_BIT); 237 197 check_enosys(msb, X64_WRITEV | X32_BIT); ··· 239 199 240 200 static void test_syscalls_without_x32(int msb) 241 201 { 242 - printf("[RUN]\t Checking for absence of x32 system calls\n"); 202 + run("Checking for absence of x32 system calls\n"); 243 203 check_for(msb, 0 | X32_BIT, 999 | X32_BIT, -ENOSYS); 244 204 } 245 205 ··· 257 217 */ 258 218 for (size_t i = 0; i < sizeof(msbs)/sizeof(msbs[0]); i++) { 259 219 int msb = msbs[i]; 260 - printf("[RUN]\tChecking system calls with msb = %d (0x%x)\n", 261 - msb, msb); 220 + run("Checking system calls with msb = %d (0x%x)\n", 221 + msb, msb); 222 + 223 + indent++; 262 224 263 225 test_syscalls_common(msb); 264 226 if (with_x32) 265 227 test_syscalls_with_x32(msb); 266 228 else 267 229 test_syscalls_without_x32(msb); 230 + 231 + indent--; 268 232 } 269 233 } 270 234 ··· 285 241 */ 286 242 nullfd = open("/dev/null", O_RDWR); 287 243 if (nullfd < 0) { 288 - printf("[FAIL]\tUnable to open /dev/null: %s\n", 289 - strerror(errno)); 290 - printf("[SKIP]\tCannot execute test\n"); 291 - return 71; /* EX_OSERR */ 244 + crit("Unable to open /dev/null: %s\n", strerror(errno)); 292 245 } 293 246 294 247 test_syscall_numbering(); 295 248 if (!nerr) { 296 - printf("[OK]\tAll system calls succeeded or failed as expected\n"); 249 + ok("All system calls succeeded or failed as expected\n"); 297 250 return 0; 298 251 } else { 299 - printf("[FAIL]\tA total of %u system call%s had incorrect behavior\n", 300 - nerr, nerr != 1 ? "s" : ""); 252 + fail("A total of %u system call%s had incorrect behavior\n", 253 + nerr, nerr != 1 ? "s" : ""); 301 254 return 1; 302 255 } 303 256 }