Igt-dev Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Mohammed Thasleem <mohammed.thasleem@intel.com>
To: igt-dev@lists.freedesktop.org
Cc: Mohammed Thasleem <mohammed.thasleem@intel.com>
Subject: [PATCH i-g-t] lib/igt_kms: Function for executing cmd and capturing message from dmesg
Date: Mon, 24 Mar 2025 00:55:55 +0530	[thread overview]
Message-ID: <20250323192555.147825-1-mohammed.thasleem@intel.com> (raw)

The function is designed to execute a command and capture its output
into a buffer from dmesg, while providing robust error handling.
It validates input parameters, executes the command, reads the output,
and manages resource cleanup. The purpose is to facilitate capturing and
processing command results in applications, ensuring errors are
logged and handled appropriately.

Signed-off-by: Mohammed Thasleem <mohammed.thasleem@intel.com>
---
 lib/igt_kms.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/igt_kms.h |  1 +
 2 files changed, 54 insertions(+)

diff --git a/lib/igt_kms.c b/lib/igt_kms.c
index cc3bb3ae7..6a6269964 100644
--- a/lib/igt_kms.c
+++ b/lib/igt_kms.c
@@ -7439,3 +7439,56 @@ int igt_backlight_write(int value, const char *fname, igt_backlight_context_t *c
 
 	return 0;
 }
+
+/**
+ * igt_check_dmesg:
+ * @command:		The command to execute.
+ * @output_buffer:	Buffer to store the command output.
+ * @buffer_size:	Size of the output buffer.
+ *
+ * Executes a system command related to igt testing and captures its output.
+ *
+ * Returns: 0 on success, negative error code on failure.
+ */
+int igt_check_dmesg(const char *command, char *output_buffer, size_t buffer_size)
+{
+	int result;
+	size_t output_len;
+	FILE *process_stream;
+	size_t total_read = 0;
+
+	if (!command || !output_buffer || buffer_size <= 0) {
+		igt_warn("Invalid parameters in %s!\n", __func__);
+		return -EINVAL;
+	}
+
+	process_stream = popen(command, "r");
+	if (!process_stream) {
+		igt_warn("Failed to open process stream in %s: %s\n", __func__, strerror(errno));
+		return -EIO;
+	}
+
+	memset(output_buffer, 0, buffer_size);
+	while (fgets(output_buffer + total_read, buffer_size - total_read, process_stream)) {
+		size_t len = strlen(output_buffer + total_read);
+
+		total_read += len;
+		if (total_read >= buffer_size - 1) {
+			igt_warn("Output truncated in %s due to buffer size limit.\n", __func__);
+			break;
+		}
+	}
+
+	result = pclose(process_stream);
+	if (result == -1) {
+		igt_warn("Failed to close process stream in %s!\n", __func__);
+		return -EIO;
+	}
+
+	/* Remove trailing newline if present */
+	output_len = strlen(output_buffer);
+	if (output_len > 0 && output_buffer[output_len - 1] == '\n')
+		output_buffer[output_len - 1] = '\0';
+
+	return 0;
+}
diff --git a/lib/igt_kms.h b/lib/igt_kms.h
index 27b545f52..3bdb508eb 100644
--- a/lib/igt_kms.h
+++ b/lib/igt_kms.h
@@ -1279,5 +1279,6 @@ void igt_set_link_params(int drm_fd, igt_output_t *output,
 			   char *link_rate, char *lane_count);
 int igt_backlight_read(int *result, const char *fname, igt_backlight_context_t *context);
 int igt_backlight_write(int value, const char *fname, igt_backlight_context_t *context);
+int igt_check_dmesg(const char *command, char *output_buffer, size_t buffer_size);
 
 #endif /* __IGT_KMS_H__ */
-- 
2.43.0


             reply	other threads:[~2025-03-23 19:26 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-03-23 19:25 Mohammed Thasleem [this message]
2025-03-23 20:15 ` ✓ Xe.CI.BAT: success for lib/igt_kms: Function for executing cmd and capturing message from dmesg Patchwork
2025-03-23 20:32 ` ✓ i915.CI.BAT: " Patchwork
2025-03-23 21:12 ` ✓ Xe.CI.Full: " Patchwork
2025-03-23 22:02 ` ✗ i915.CI.Full: failure " 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=20250323192555.147825-1-mohammed.thasleem@intel.com \
    --to=mohammed.thasleem@intel.com \
    --cc=igt-dev@lists.freedesktop.org \
    /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