summaryrefslogtreecommitdiffstats
path: root/dbus/dbus-message-util.c
diff options
context:
space:
mode:
authorLennart Poettering <lennart@poettering.net>2009-04-22 03:41:05 +0200
committerLennart Poettering <lennart@poettering.net>2009-05-20 02:09:03 +0200
commita0cc21f8bb6752ffe0ee5f4f5b575dc50d6d46ae (patch)
tree950cbd47691d09ecef51dab996e93163d938a2f1 /dbus/dbus-message-util.c
parentba7daa606cf20ff3b5e992907f380a425feaef01 (diff)
unix-fd: add message encoding/decoding for unix fds
When appending unix fds to the message a new entry in the fd array will be allocated and the index to it will be written to the message payload. When parsing unix fds from the message the index will be read from the payload and then looked up in the fd array. When we read fds we put them in a queue first. Since each message knows how many fds are attached to it we will then pop enough fds from this queue each time we decode a message from the stream. This should make sending and receiving more portable since we don't make any strong requirements on the exact semantics of the SCM_RIGHTS implementation: as long as fds are recieved in order, none or lost and the arrive at the same time as at least one byte from the actual message dat we should be able to handle them correctly.
Diffstat (limited to 'dbus/dbus-message-util.c')
-rw-r--r--dbus/dbus-message-util.c108
1 files changed, 101 insertions, 7 deletions
diff --git a/dbus/dbus-message-util.c b/dbus/dbus-message-util.c
index 46cbe4e3..1b139436 100644
--- a/dbus/dbus-message-util.c
+++ b/dbus/dbus-message-util.c
@@ -27,6 +27,17 @@
#include "dbus-message-private.h"
#include "dbus-marshal-recursive.h"
#include "dbus-string.h"
+#ifdef HAVE_UNIX_FD_PASSING
+#include "dbus-sysdeps-unix.h"
+#endif
+
+#ifdef __linux__
+/* Necessary for the Linux-specific fd leak checking code only */
+#include <sys/types.h>
+#include <dirent.h>
+#include <stdlib.h>
+#include <errno.h>
+#endif
/**
* @addtogroup DBusMessage
@@ -126,6 +137,50 @@ check_memleaks (void)
}
}
+void
+_dbus_check_fdleaks(void)
+{
+
+#ifdef __linux__
+
+ DIR *d;
+
+ /* This works on Linux only */
+
+ if ((d = opendir("/proc/self/fd")))
+ {
+ struct dirent *de;
+
+ while ((de = readdir(d)))
+ {
+ long l;
+ char *e = NULL;
+ int fd;
+
+ if (de->d_name[0] == '.')
+ continue;
+
+ errno = 0;
+ l = strtol(de->d_name, &e, 10);
+ _dbus_assert(errno == 0 && e && !*e);
+
+ fd = (int) l;
+
+ if (fd < 3)
+ continue;
+
+ if (fd == dirfd(d))
+ continue;
+
+ _dbus_warn("file descriptor %i leaked in %s.\n", fd, __FILE__);
+ _dbus_assert_not_reached("fdleaks");
+ }
+
+ closedir(d);
+ }
+#endif
+}
+
static dbus_bool_t
check_have_valid_message (DBusMessageLoader *loader)
{
@@ -895,7 +950,7 @@ verify_test_message (DBusMessage *message)
dbus_bool_t
_dbus_message_test (const char *test_data_dir)
{
- DBusMessage *message;
+ DBusMessage *message, *message_without_unix_fds;
DBusMessageLoader *loader;
int i;
const char *data;
@@ -939,6 +994,9 @@ _dbus_message_test (const char *test_data_dir)
unsigned char v_BYTE;
unsigned char v2_BYTE;
dbus_bool_t v_BOOLEAN;
+#ifdef HAVE_UNIX_FD_PASSING
+ int v_UNIX_FD;
+#endif
message = dbus_message_new_method_call ("org.freedesktop.DBus.TestService",
"/org/freedesktop/TestPath",
@@ -1057,6 +1115,9 @@ _dbus_message_test (const char *test_data_dir)
v_BOOLEAN = TRUE;
v_BYTE = 42;
v2_BYTE = 24;
+#ifdef HAVE_UNIX_FD_PASSING
+ v_UNIX_FD = 1;
+#endif
dbus_message_append_args (message,
DBUS_TYPE_INT16, &v_INT16,
@@ -1090,6 +1151,7 @@ _dbus_message_test (const char *test_data_dir)
_DBUS_N_ELEMENTS (our_boolean_array),
DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &v_ARRAY_STRING,
_DBUS_N_ELEMENTS (our_string_array),
+
DBUS_TYPE_INVALID);
i = 0;
@@ -1124,7 +1186,16 @@ _dbus_message_test (const char *test_data_dir)
sig[i++] = DBUS_TYPE_BOOLEAN;
sig[i++] = DBUS_TYPE_ARRAY;
sig[i++] = DBUS_TYPE_STRING;
- sig[i++] = DBUS_TYPE_INVALID;
+
+ message_without_unix_fds = dbus_message_copy(message);
+ _dbus_assert(message_without_unix_fds);
+#ifdef HAVE_UNIX_FD_PASSING
+ dbus_message_append_args (message,
+ DBUS_TYPE_UNIX_FD, &v_UNIX_FD,
+ DBUS_TYPE_INVALID);
+ sig[i++] = DBUS_TYPE_UNIX_FD;
+#endif
+ sig[i++] = DBUS_TYPE_INVALID;
_dbus_assert (i < (int) _DBUS_N_ELEMENTS (sig));
@@ -1201,6 +1272,20 @@ _dbus_message_test (const char *test_data_dir)
_dbus_message_loader_return_buffer (loader, buffer, 1);
}
+#ifdef HAVE_UNIX_FD_PASSING
+ {
+ int *unix_fds;
+ unsigned n_unix_fds;
+ /* Write unix fd */
+ _dbus_message_loader_get_unix_fds(loader, &unix_fds, &n_unix_fds);
+ _dbus_assert(n_unix_fds > 0);
+ _dbus_assert(message->n_unix_fds == 1);
+ unix_fds[0] = _dbus_dup(message->unix_fds[0], NULL);
+ _dbus_assert(unix_fds[0] >= 0);
+ _dbus_message_loader_return_unix_fds(loader, unix_fds, 1);
+ }
+#endif
+
dbus_message_unref (message);
/* Now pop back the message */
@@ -1217,7 +1302,14 @@ _dbus_message_test (const char *test_data_dir)
if (dbus_message_get_reply_serial (message) != 5678)
_dbus_assert_not_reached ("reply serial fields differ");
- verify_test_message (message);
+ dbus_message_unref (message);
+
+ /* ovveride the serial, since it was reset by dbus_message_copy() */
+ dbus_message_set_serial(message_without_unix_fds, 8901);
+
+ dbus_message_lock (message_without_unix_fds);
+
+ verify_test_message (message_without_unix_fds);
{
/* Marshal and demarshal the message. */
@@ -1228,7 +1320,7 @@ _dbus_message_test (const char *test_data_dir)
int len = 0;
char garbage_header[DBUS_MINIMUM_HEADER_SIZE] = "xxx";
- if (!dbus_message_marshal (message, &marshalled, &len))
+ if (!dbus_message_marshal (message_without_unix_fds, &marshalled, &len))
_dbus_assert_not_reached ("failed to marshal message");
_dbus_assert (len != 0);
@@ -1267,10 +1359,11 @@ _dbus_message_test (const char *test_data_dir)
_dbus_assert (dbus_message_demarshal_bytes_needed (garbage_header, DBUS_MINIMUM_HEADER_SIZE) == -1);
}
- dbus_message_unref (message);
+ dbus_message_unref (message_without_unix_fds);
_dbus_message_loader_unref (loader);
check_memleaks ();
+ _dbus_check_fdleaks();
/* Load all the sample messages from the message factory */
{
@@ -1304,9 +1397,10 @@ _dbus_message_test (const char *test_data_dir)
print_validities_seen (FALSE);
print_validities_seen (TRUE);
}
-
+
check_memleaks ();
-
+ _dbus_check_fdleaks();
+
/* Now load every message in test_data_dir if we have one */
if (test_data_dir == NULL)
return TRUE;