From b580fa9b0e6d580adb37017fe3ad67b9306dfa58 Mon Sep 17 00:00:00 2001 From: Alban Crequy Date: Fri, 27 Jun 2014 16:25:23 +0100 Subject: [PATCH 2/2] dbus-monitor: more details on file descriptors Print more details when receiving a file descriptor. Before: unix fd 5 After: unix fd 5 inode 1526474 type: socket address family: unix https://bugs.freedesktop.org/show_bug.cgi?id=80603 --- tools/dbus-print-message.c | 73 +++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 72 insertions(+), 1 deletion(-) diff --git a/tools/dbus-print-message.c b/tools/dbus-print-message.c index 320abd6..b8dc41d 100644 --- a/tools/dbus-print-message.c +++ b/tools/dbus-print-message.c @@ -23,6 +23,10 @@ #include #include "dbus-print-message.h" +#include +#include +#include +#include #include #include "config.h" @@ -143,6 +147,72 @@ print_ay (DBusMessageIter *iter, int depth) } static void +print_fd (int fd, int depth) +{ + int err; + struct stat statbuf = {0,}; + + /* What's the point of printing the fd number? It will be different in every + * process recipient of the D-Bus message... + */ + printf ("unix fd %d\n", fd); + if (fd == -1) + return; + + err = fstat (fd, &statbuf); + if (err == -1) + return; + + indent (depth+1); + printf ("inode %d\n", (int) statbuf.st_ino); + + indent (depth+1); + printf ("type: "); + if (S_ISREG(statbuf.st_mode)) + printf ("file\n"); + if (S_ISDIR(statbuf.st_mode)) + printf ("directory\n"); + if (S_ISCHR(statbuf.st_mode)) + printf ("char\n"); + if (S_ISBLK(statbuf.st_mode)) + printf ("block\n"); + if (S_ISFIFO(statbuf.st_mode)) + printf ("fifo\n"); + if (S_ISLNK(statbuf.st_mode)) + printf ("link\n"); + if (S_ISSOCK(statbuf.st_mode)) + { + struct sockaddr addr; + socklen_t addrlen = sizeof(addr); + printf ("socket\n"); + err = getsockname(fd, &addr, &addrlen); + if (err == 0 && addrlen >= sizeof(sa_family_t)) + { + indent (depth+1); + printf ("address family: "); + switch (addr.sa_family) + { + case AF_UNIX: + printf ("unix\n"); + break; + case AF_BLUETOOTH: + printf ("bluetooth\n"); + break; + case AF_INET: + printf ("inet\n"); + break; + case AF_INET6: + printf ("inet6\n"); + break; + default: + printf ("%d\n", addr.sa_family); + break; + } + } + } +} + +static void print_iter (DBusMessageIter *iter, dbus_bool_t literal, int depth) { do @@ -351,7 +421,8 @@ print_iter (DBusMessageIter *iter, dbus_bool_t literal, int depth) { int fd; dbus_message_iter_get_basic (iter, &fd); - printf ("unix fd %d\n", fd); + + print_fd (fd, depth+1); /* dbus_message_iter_get_basic() duplicated the fd, we need to * close it after use. The original fd will be closed when the -- 1.8.5.3