[PATCH 2/4] drm: Add dispatcher and driver identification for DRM

Patrik Jakobsson pjakobsson at suse.de
Tue Mar 12 07:49:17 UTC 2019


* Makefile.am: Add compilation of drm.c.
* drm.c: New file.
* ioctl.c (ioctl_decode): Dispatch drm ioctls when drm headers are found.

Signed-off-by: Patrik Jakobsson <pjakobsson at suse.de>
---
 Makefile.am |  1 +
 drm.c       | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 ioctl.c     |  8 ++++++
 3 files changed, 90 insertions(+)
 create mode 100644 drm.c

diff --git a/Makefile.am b/Makefile.am
index de994b1c..d7dd2e7b 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -101,6 +101,7 @@ strace_SOURCES =	\
 	dirent.c	\
 	dirent64.c	\
 	dm.c		\
+	drm.c		\
 	dyxlat.c	\
 	empty.h		\
 	epoll.c		\
diff --git a/drm.c b/drm.c
new file mode 100644
index 00000000..ea2006c5
--- /dev/null
+++ b/drm.c
@@ -0,0 +1,81 @@
+/*
+ * Copyright (c) 2019 Patrik Jakobsson <pjakobsson at suse.de>
+ *
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ */
+
+#include "defs.h"
+
+#if defined(HAVE_DRM_H) || defined(HAVE_DRM_DRM_H)
+
+#include <sys/param.h>
+
+#ifdef HAVE_DRM_H
+#include <drm.h>
+#else
+#include <drm/drm.h>
+#endif
+
+#define DRM_MAX_NAME_LEN 128
+
+#include MPERS_DEFS
+
+static inline int drm_is_priv(const unsigned int num)
+{
+	return (_IOC_NR(num) >= DRM_COMMAND_BASE &&
+		_IOC_NR(num) < DRM_COMMAND_END);
+}
+
+static char *drm_get_driver_name(struct tcb *tcp)
+{
+	char path[PATH_MAX];
+	char link[PATH_MAX];
+	int ret;
+
+	if (getfdpath(tcp, tcp->u_arg[0], path, PATH_MAX - 1) < 0)
+		return NULL;
+
+	if (snprintf(link, PATH_MAX, "/sys/class/drm/%s/device/driver",
+	    basename(path)) >= (signed int)sizeof(link))
+		return NULL;
+
+	ret = readlink(link, path, PATH_MAX - 1);
+	if (ret < 0)
+		return NULL;
+
+	path[ret] = '\0';
+	return strdup(basename(path));
+}
+
+static int drm_is_driver(struct tcb *tcp, const char *name)
+{
+	char *priv;
+
+	/*
+	 * If no private data is allocated we are detecting the driver name for
+	 * the first time and must resolve it.
+	 */
+	if (tcp->_priv_data == NULL) {
+		priv = drm_get_driver_name(tcp);
+
+		if (priv == NULL)
+			return 0;
+
+		set_tcb_priv_data(tcp, priv, free);
+	}
+
+	return strncmp(name, get_tcb_priv_data(tcp), DRM_MAX_NAME_LEN) == 0;
+}
+
+MPERS_PRINTER_DECL(int, drm_decode_number, struct tcb *tcp, unsigned int code)
+{
+	return 0;
+}
+
+MPERS_PRINTER_DECL(int, drm_ioctl, struct tcb *tcp, const unsigned int code,
+		   long arg)
+{
+	return 0;
+}
+
+#endif /* HAVE_DRM_H || HAVE_DRM_DRM_H */
diff --git a/ioctl.c b/ioctl.c
index d3205b22..be31b33d 100644
--- a/ioctl.c
+++ b/ioctl.c
@@ -197,6 +197,10 @@ ioctl_decode_command_number(struct tcb *tcp)
 				return 1;
 			}
 			return 0;
+#if defined(HAVE_DRM_H) || defined(HAVE_DRM_DRM_H)
+		case 'd':
+			return drm_decode_number(tcp, code);
+#endif
 		default:
 			return 0;
 	}
@@ -266,6 +270,10 @@ ioctl_decode(struct tcb *tcp)
 		return fs_x_ioctl(tcp, code, arg);
 	case 0x22:
 		return scsi_ioctl(tcp, code, arg);
+#if defined(HAVE_DRM_H) || defined(HAVE_DRM_DRM_H)
+	case 'd':
+		return drm_ioctl(tcp, code, arg);
+#endif
 	case 'L':
 		return loop_ioctl(tcp, code, arg);
 #ifdef HAVE_STRUCT_MTD_WRITE_REQ
-- 
2.21.0



More information about the Strace-devel mailing list