Igt-dev Archive on lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH i-g-t] lib/igt_kms: Function for executing cmd and capturing message from dmesg
@ 2025-03-23 19:25 Mohammed Thasleem
  2025-03-23 20:15 ` ✓ Xe.CI.BAT: success for " Patchwork
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Mohammed Thasleem @ 2025-03-23 19:25 UTC (permalink / raw)
  To: igt-dev; +Cc: Mohammed Thasleem

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


^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2025-03-23 22:02 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-03-23 19:25 [PATCH i-g-t] lib/igt_kms: Function for executing cmd and capturing message from dmesg Mohammed Thasleem
2025-03-23 20:15 ` ✓ Xe.CI.BAT: success for " 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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox