Re: [PATCH] selftests/user_events: Fix failures when user_events is not installed

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Thu,  7 Sep 2023 22:35:36 +0000
Beau Belgrave <beaub@xxxxxxxxxxxxxxxxxxx> wrote:

> diff --git a/tools/testing/selftests/user_events/user_events_selftests.h b/tools/testing/selftests/user_events/user_events_selftests.h
> new file mode 100644
> index 000000000000..72692e62c709
> --- /dev/null
> +++ b/tools/testing/selftests/user_events/user_events_selftests.h
> @@ -0,0 +1,103 @@
> +/* SPDX-License-Identifier: GPL-2.0 */
> +
> +#ifndef _USER_EVENTS_SELFTESTS_H
> +#define _USER_EVENTS_SELFTESTS_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)
> +{
> +	int fd;
> +
> +	*message = "";
> +	*fail = false;
> +
> +	/* Ensure tracefs is installed */
> +	fd = open("/sys/kernel/tracing", O_RDONLY);

Instead of opening the directory, I believe that "stat()" is the preferred
method of testing existence.

> +
> +	if (fd == -1) {
> +		*message = "Tracefs is not installed";
> +		return false;
> +	}
> +
> +	close(fd);
> +
> +	/* Ensure mounted tracefs */
> +	fd = open("/sys/kernel/tracing/README", O_RDONLY);
> +
> +	if (fd == -1 && errno == ENOENT) {
> +		if (mount(NULL, "/sys/kernel/tracing", "tracefs", 0, NULL) != 0) {
> +			*message = "Cannot mount tracefs";
> +			*fail = true;
> +			return false;
> +		}
> +
> +		fd = open("/sys/kernel/tracing/README", O_RDONLY);

Same here, unless you want to make sure you can also read it.

> +	}
> +
> +	if (fd == -1) {
> +		*message = "Cannot access tracefs";
> +		*fail = true;
> +		return false;
> +	}
> +
> +	close(fd);
> +
> +	return true;
> +}
> +
> +static inline bool user_events_enabled(char **message, bool *fail)
> +{
> +	int fd;
> +
> +	*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 */
> +	fd = open("/sys/kernel/tracing/user_events_data", O_RDONLY);

ditto.

-- Steve

> +
> +	if (fd == -1) {
> +		switch (errno) {
> +		case ENOENT:
> +			*message = "user_events is not installed";
> +			return false;
> +
> +		default:
> +			*message = "Cannot access user_events_data";
> +			*fail = true;
> +			return false;
> +		}
> +	}
> +
> +	close(fd);
> +
> +	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 */
> 
> base-commit: 9b1db732866bee060b9bca9493e5ebf5e8874c48




[Index of Archives]     [Linux Wireless]     [Linux Kernel]     [ATH6KL]     [Linux Bluetooth]     [Linux Netdev]     [Kernel Newbies]     [Share Photos]     [IDE]     [Security]     [Git]     [Netfilter]     [Bugtraq]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux ATA RAID]     [Samba]     [Device Mapper]

  Powered by Linux