Skip to content

Commit c5c3948

Browse files
H. Peter Anvin (Intel)KAGA-KOKO
authored andcommitted
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
1 parent 15c82d9 commit c5c3948

1 file changed

Lines changed: 72 additions & 31 deletions

File tree

tools/testing/selftests/x86/syscall_numbering.c

Lines changed: 72 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <string.h>
1717
#include <fcntl.h>
1818
#include <limits.h>
19+
#include <sysexits.h>
1920

2021
/* Common system call numbers */
2122
#define SYS_READ 0
@@ -34,6 +35,33 @@
3435

3536
static unsigned int nerr = 0; /* Cumulative error count */
3637
static int nullfd = -1; /* File descriptor for /dev/null */
38+
static int indent = 0;
39+
40+
static inline unsigned int offset(void)
41+
{
42+
return 8 + indent * 4;
43+
}
44+
45+
#define msg(lvl, fmt, ...) printf("%-*s" fmt, offset(), "[" #lvl "]", \
46+
## __VA_ARGS__)
47+
48+
#define run(fmt, ...) msg(RUN, fmt, ## __VA_ARGS__)
49+
#define info(fmt, ...) msg(INFO, fmt, ## __VA_ARGS__)
50+
#define ok(fmt, ...) msg(OK, fmt, ## __VA_ARGS__)
51+
52+
#define fail(fmt, ...) \
53+
do { \
54+
msg(FAIL, fmt, ## __VA_ARGS__); \
55+
nerr++; \
56+
} while (0)
57+
58+
#define crit(fmt, ...) \
59+
do { \
60+
indent = 0; \
61+
msg(FAIL, fmt, ## __VA_ARGS__); \
62+
msg(SKIP, "Unable to run test\n"); \
63+
exit(EX_OSERR);
64+
} while (0)
3765

3866
/*
3967
* Directly invokes the given syscall with nullfd as the first argument
@@ -91,28 +119,37 @@ static unsigned int _check_for(int msb, int start, int end, long long expect,
91119
{
92120
unsigned int err = 0;
93121

122+
indent++;
123+
if (start != end)
124+
indent++;
125+
94126
for (int nr = start; nr <= end; nr++) {
95127
long long ret = probe_syscall(msb, nr);
96128

97129
if (ret != expect) {
98-
printf("[FAIL]\t %s returned %lld, but it should have returned %s\n",
130+
fail("%s returned %lld, but it should have returned %s\n",
99131
syscall_str(msb, nr, nr),
100132
ret, expect_str);
101133
err++;
102134
}
103135
}
104136

137+
if (start != end)
138+
indent--;
139+
105140
if (err) {
106141
nerr += err;
107142
if (start != end)
108-
printf("[FAIL]\t %s had %u failure%s\n",
143+
fail("%s had %u failure%s\n",
109144
syscall_str(msb, start, end),
110-
err, (err == 1) ? "s" : "");
145+
err, err == 1 ? "s" : "");
111146
} else {
112-
printf("[OK]\t %s returned %s as expected\n",
113-
syscall_str(msb, start, end), expect_str);
147+
ok("%s returned %s as expected\n",
148+
syscall_str(msb, start, end), expect_str);
114149
}
115150

151+
indent--;
152+
116153
return err;
117154
}
118155

@@ -137,35 +174,38 @@ static bool check_enosys(int msb, int nr)
137174
static bool test_x32(void)
138175
{
139176
long long ret;
140-
long long mypid = getpid();
177+
pid_t mypid = getpid();
178+
bool with_x32;
141179

142-
printf("[RUN]\tChecking for x32 by calling x32 getpid()\n");
180+
run("Checking for x32 by calling x32 getpid()\n");
143181
ret = probe_syscall(0, SYS_GETPID | X32_BIT);
144182

183+
indent++;
145184
if (ret == mypid) {
146-
printf("[INFO]\t x32 is supported\n");
147-
return true;
185+
info("x32 is supported\n");
186+
with_x32 = true;
148187
} else if (ret == -ENOSYS) {
149-
printf("[INFO]\t x32 is not supported\n");
150-
return false;
188+
info("x32 is not supported\n");
189+
with_x32 = false;
151190
} else {
152-
printf("[FAIL]\t x32 getpid() returned %lld, but it should have returned either %lld or -ENOSYS\n", ret, mypid);
153-
nerr++;
154-
return true; /* Proceed as if... */
191+
fail("x32 getpid() returned %lld, but it should have returned either %lld or -ENOSYS\n", ret, mypid);
192+
with_x32 = false;
155193
}
194+
indent--;
195+
return with_x32;
156196
}
157197

158198
static void test_syscalls_common(int msb)
159199
{
160-
printf("[RUN]\t Checking some common syscalls as 64 bit\n");
200+
run("Checking some common syscalls as 64 bit\n");
161201
check_zero(msb, SYS_READ);
162202
check_zero(msb, SYS_WRITE);
163203

164-
printf("[RUN]\t Checking some 64-bit only syscalls as 64 bit\n");
204+
run("Checking some 64-bit only syscalls as 64 bit\n");
165205
check_zero(msb, X64_READV);
166206
check_zero(msb, X64_WRITEV);
167207

168-
printf("[RUN]\t Checking out of range system calls\n");
208+
run("Checking out of range system calls\n");
169209
check_for(msb, -64, -1, -ENOSYS);
170210
check_for(msb, X32_BIT-64, X32_BIT-1, -ENOSYS);
171211
check_for(msb, -64-X32_BIT, -1-X32_BIT, -ENOSYS);
@@ -180,26 +220,26 @@ static void test_syscalls_with_x32(int msb)
180220
* set. Calling them without the x32 bit set is
181221
* nonsense and should not work.
182222
*/
183-
printf("[RUN]\t Checking x32 syscalls as 64 bit\n");
223+
run("Checking x32 syscalls as 64 bit\n");
184224
check_for(msb, 512, 547, -ENOSYS);
185225

186-
printf("[RUN]\t Checking some common syscalls as x32\n");
226+
run("Checking some common syscalls as x32\n");
187227
check_zero(msb, SYS_READ | X32_BIT);
188228
check_zero(msb, SYS_WRITE | X32_BIT);
189229

190-
printf("[RUN]\t Checking some x32 syscalls as x32\n");
230+
run("Checking some x32 syscalls as x32\n");
191231
check_zero(msb, X32_READV | X32_BIT);
192232
check_zero(msb, X32_WRITEV | X32_BIT);
193233

194-
printf("[RUN]\t Checking some 64-bit syscalls as x32\n");
234+
run("Checking some 64-bit syscalls as x32\n");
195235
check_enosys(msb, X64_IOCTL | X32_BIT);
196236
check_enosys(msb, X64_READV | X32_BIT);
197237
check_enosys(msb, X64_WRITEV | X32_BIT);
198238
}
199239

200240
static void test_syscalls_without_x32(int msb)
201241
{
202-
printf("[RUN]\t Checking for absence of x32 system calls\n");
242+
run("Checking for absence of x32 system calls\n");
203243
check_for(msb, 0 | X32_BIT, 999 | X32_BIT, -ENOSYS);
204244
}
205245

@@ -217,14 +257,18 @@ static void test_syscall_numbering(void)
217257
*/
218258
for (size_t i = 0; i < sizeof(msbs)/sizeof(msbs[0]); i++) {
219259
int msb = msbs[i];
220-
printf("[RUN]\tChecking system calls with msb = %d (0x%x)\n",
221-
msb, msb);
260+
run("Checking system calls with msb = %d (0x%x)\n",
261+
msb, msb);
262+
263+
indent++;
222264

223265
test_syscalls_common(msb);
224266
if (with_x32)
225267
test_syscalls_with_x32(msb);
226268
else
227269
test_syscalls_without_x32(msb);
270+
271+
indent--;
228272
}
229273
}
230274

@@ -241,19 +285,16 @@ int main(void)
241285
*/
242286
nullfd = open("/dev/null", O_RDWR);
243287
if (nullfd < 0) {
244-
printf("[FAIL]\tUnable to open /dev/null: %s\n",
245-
strerror(errno));
246-
printf("[SKIP]\tCannot execute test\n");
247-
return 71; /* EX_OSERR */
288+
crit("Unable to open /dev/null: %s\n", strerror(errno));
248289
}
249290

250291
test_syscall_numbering();
251292
if (!nerr) {
252-
printf("[OK]\tAll system calls succeeded or failed as expected\n");
293+
ok("All system calls succeeded or failed as expected\n");
253294
return 0;
254295
} else {
255-
printf("[FAIL]\tA total of %u system call%s had incorrect behavior\n",
256-
nerr, nerr != 1 ? "s" : "");
296+
fail("A total of %u system call%s had incorrect behavior\n",
297+
nerr, nerr != 1 ? "s" : "");
257298
return 1;
258299
}
259300
}

0 commit comments

Comments
 (0)