Igt-dev Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Riana Tauro <riana.tauro@intel.com>
To: igt-dev@lists.freedesktop.org
Cc: riana.tauro@intel.com, anshuman.gupta@intel.com,
	lucas.demarchi@intel.com, rodrigo.vivi@intel.com,
	kamil.konieczny@linux.intel.com, katarzyna.piecielska@intel.com
Subject: [PATCH i-g-t 1/2] lib/igt_configfs: Add library for configfs
Date: Thu, 10 Apr 2025 11:37:25 +0530	[thread overview]
Message-ID: <20250410060727.1857299-2-riana.tauro@intel.com> (raw)
In-Reply-To: <20250410060727.1857299-1-riana.tauro@intel.com>

Add library functions to open configfs and create
and remove configfs directories.

Signed-off-by: Riana Tauro <riana.tauro@intel.com>
---
 lib/igt_configfs.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++
 lib/igt_configfs.h | 13 +++++++
 lib/meson.build    |  1 +
 3 files changed, 102 insertions(+)
 create mode 100644 lib/igt_configfs.c
 create mode 100644 lib/igt_configfs.h

diff --git a/lib/igt_configfs.c b/lib/igt_configfs.c
new file mode 100644
index 000000000..57d59f1c4
--- /dev/null
+++ b/lib/igt_configfs.c
@@ -0,0 +1,88 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2025 Intel Corporation
+ */
+#include <sys/mount.h>
+#include <sys/stat.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+
+#include "igt_configfs.h"
+#include "igt_core.h"
+
+static const char *igt_configfs_mount(void)
+{
+	struct stat st;
+
+	if (stat("/sys/kernel/debug", &st) == 0)
+		return "/sys/kernel/config";
+
+	if (mount("none", "/sys/kernel/config", "configfs", 0, 0))
+		return NULL;
+
+	return "/sys/kernel/config";
+}
+
+/**
+ * igt_configfs_open: open configfs path
+ * @name: name of the configfs directory
+ *
+ * Opens the configfs directory corresponding to the name
+ *
+ * Returns:
+ * The directory fd, or -1 on failure.
+ */
+int igt_configfs_open(const char *name)
+{
+	char path[PATH_MAX];
+	const char *configfs_path;
+
+	configfs_path = igt_configfs_mount();
+	igt_assert(configfs_path);
+
+	snprintf(path, sizeof(path), "%s/%s", configfs_path, name);
+
+	return open(path, O_RDONLY);
+}
+
+/**
+ * igt_configfs_create_directory: creates configfs group
+ * @fd: fd of configfs parent directory
+ * @name: name of the directory to create
+ *
+ * creates a directory under configfs parent directory
+ *
+ * Returns: 0 on success, -errno otherwise
+ */
+int igt_configfs_create_directory(int fd, const char *name)
+{
+	int ret;
+	int dirfd;
+
+	ret = mkdirat(fd, name, 0755);
+	if (ret)
+		return -errno;
+
+	dirfd = openat(fd, name, O_DIRECTORY);
+	if (dirfd < 0)
+		return -errno;
+
+	return dirfd;
+}
+
+/**
+ * igt_configfs_remove_directory: removes configfs group
+ * @fd: fd of configfs parent directory
+ * @name: name of directory to create
+ *
+ * removes directory under configfs parent directory
+ */
+void igt_configfs_remove_directory(int fd, const char *name)
+{
+	int ret = unlinkat(fd, name, AT_REMOVEDIR);
+
+	if (ret)
+		igt_warn("Unable to remove %s directory: %s\n", name, strerror(errno));
+}
diff --git a/lib/igt_configfs.h b/lib/igt_configfs.h
new file mode 100644
index 000000000..d839db49a
--- /dev/null
+++ b/lib/igt_configfs.h
@@ -0,0 +1,13 @@
+/* SPDX-License-Identifier: MIT */
+/*
+ * Copyright © 2025 Intel Corporation
+ */
+
+#ifndef IGT_CONFIGFS_H
+#define IGT_CONFIGFS_H
+
+int igt_configfs_open(const char *name);
+int igt_configfs_create_directory(int fd, const char *name);
+void igt_configfs_remove_directory(int fd, const char *name);
+
+#endif /* IGT_CONFIGFS_H */
diff --git a/lib/meson.build b/lib/meson.build
index d7bb72c57..f087947e7 100644
--- a/lib/meson.build
+++ b/lib/meson.build
@@ -19,6 +19,7 @@ lib_sources = [
 	'igt_collection.c',
 	'igt_color_encoding.c',
 	'igt_facts.c',
+	'igt_configfs.c',
 	'igt_crc.c',
 	'igt_debugfs.c',
 	'igt_device.c',
-- 
2.47.1


  reply	other threads:[~2025-04-10  5:45 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-04-10  6:07 [PATCH i-g-t 0/2] Add test to validate survivability mode Riana Tauro
2025-04-10  6:07 ` Riana Tauro [this message]
2025-04-11  7:53   ` [PATCH i-g-t 1/2] lib/igt_configfs: Add library for configfs Louis Chauvet
2025-04-11 10:58     ` Riana Tauro
2025-04-13 17:56       ` José Expósito
2025-04-10  6:07 ` [PATCH i-g-t 2/2] tests/intel/xe_configfs: Add test to validate survivability mode Riana Tauro
2025-04-11  7:53   ` Louis Chauvet
2025-04-15  5:44     ` Riana Tauro
2025-04-10  6:55 ` ✓ Xe.CI.BAT: success for " Patchwork
2025-04-10  7:53 ` ✗ Xe.CI.Full: failure " Patchwork
2025-04-10  8:55 ` ✗ i915.CI.BAT: " Patchwork

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250410060727.1857299-2-riana.tauro@intel.com \
    --to=riana.tauro@intel.com \
    --cc=anshuman.gupta@intel.com \
    --cc=igt-dev@lists.freedesktop.org \
    --cc=kamil.konieczny@linux.intel.com \
    --cc=katarzyna.piecielska@intel.com \
    --cc=lucas.demarchi@intel.com \
    --cc=rodrigo.vivi@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox