[PATCH i-g-t] lib/igt_kms: Function for executing cmd and capturing message from dmesg

Mohammed Thasleem mohammed.thasleem at intel.com
Sun Mar 23 19:25:55 UTC 2025


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 at 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



More information about the igt-dev mailing list