-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
selftests/user_events: Fix failures when user_events is not installed
When user_events is not installed the self tests currently fail. Now that these self tests run by default we need to ensure they don't fail when user_events was not enabled for the kernel being tested. Add common methods to detect if tracefs and user_events is enabled. If either is not enabled skip the test. If tracefs is enabled, but is not mounted, mount tracefs and fail if there were any errors. Fail if not run as root. Fixes: 68b4d2d ("selftests/user_events: Reenable build") Reported-by: Naresh Kamboju <naresh.kamboju@linaro.org> Link: https://lore.kernel.org/all/CA+G9fYuugZ0OMeS6HvpSS4nuf_A3s455ecipGBvER0LJHojKZg@mail.gmail.com/ Signed-off-by: Beau Belgrave <beaub@linux.microsoft.com> Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
- Loading branch information
Beau Belgrave
authored and
Shuah Khan
committed
Sep 11, 2023
1 parent
0bb80ec
commit a06023a
Showing
5 changed files
with
111 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
tools/testing/selftests/user_events/user_events_selftests.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* SPDX-License-Identifier: GPL-2.0 */ | ||
|
||
#ifndef _USER_EVENTS_SELFTESTS_H | ||
#define _USER_EVENTS_SELFTESTS_H | ||
|
||
#include <sys/stat.h> | ||
#include <sys/types.h> | ||
#include <sys/mount.h> | ||
#include <unistd.h> | ||
#include <errno.h> | ||
|
||
#include "../kselftest.h" | ||
|
||
static inline bool tracefs_enabled(char **message, bool *fail) | ||
{ | ||
struct stat buf; | ||
int ret; | ||
|
||
*message = ""; | ||
*fail = false; | ||
|
||
/* Ensure tracefs is installed */ | ||
ret = stat("/sys/kernel/tracing", &buf); | ||
|
||
if (ret == -1) { | ||
*message = "Tracefs is not installed"; | ||
return false; | ||
} | ||
|
||
/* Ensure mounted tracefs */ | ||
ret = stat("/sys/kernel/tracing/README", &buf); | ||
|
||
if (ret == -1 && errno == ENOENT) { | ||
if (mount(NULL, "/sys/kernel/tracing", "tracefs", 0, NULL) != 0) { | ||
*message = "Cannot mount tracefs"; | ||
*fail = true; | ||
return false; | ||
} | ||
|
||
ret = stat("/sys/kernel/tracing/README", &buf); | ||
} | ||
|
||
if (ret == -1) { | ||
*message = "Cannot access tracefs"; | ||
*fail = true; | ||
return false; | ||
} | ||
|
||
return true; | ||
} | ||
|
||
static inline bool user_events_enabled(char **message, bool *fail) | ||
{ | ||
struct stat buf; | ||
int ret; | ||
|
||
*message = ""; | ||
*fail = false; | ||
|
||
if (getuid() != 0) { | ||
*message = "Must be run as root"; | ||
*fail = true; | ||
return false; | ||
} | ||
|
||
if (!tracefs_enabled(message, fail)) | ||
return false; | ||
|
||
/* Ensure user_events is installed */ | ||
ret = stat("/sys/kernel/tracing/user_events_data", &buf); | ||
|
||
if (ret == -1) { | ||
switch (errno) { | ||
case ENOENT: | ||
*message = "user_events is not installed"; | ||
return false; | ||
|
||
default: | ||
*message = "Cannot access user_events_data"; | ||
*fail = true; | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
#define USER_EVENT_FIXTURE_SETUP(statement) do { \ | ||
char *message; \ | ||
bool fail; \ | ||
if (!user_events_enabled(&message, &fail)) { \ | ||
if (fail) { \ | ||
TH_LOG("Setup failed due to: %s", message); \ | ||
ASSERT_FALSE(fail); \ | ||
} \ | ||
SKIP(statement, "Skipping due to: %s", message); \ | ||
} \ | ||
} while (0) | ||
|
||
#endif /* _USER_EVENTS_SELFTESTS_H */ |