1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
2 /* dbus-message.c DBusMessage object
4 * Copyright (C) 2002, 2003, 2004, 2005 Red Hat Inc.
5 * Copyright (C) 2002, 2003 CodeFactory AB
6 * Copyright (C) 2015 Samsung Electronics
8 * Licensed under the Academic Free License version 2.1
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include "dbus-internals.h"
28 #include "dbus-marshal-recursive.h"
29 #include "dbus-marshal-validate.h"
30 #include "dbus-marshal-byteswap.h"
31 #include "dbus-marshal-header.h"
32 #include "dbus-signature.h"
33 #include "dbus-message-private.h"
34 #include "dbus-object-tree.h"
35 #include "dbus-memory.h"
36 #include "dbus-list.h"
37 #include "dbus-threads-internal.h"
38 #ifdef HAVE_UNIX_FD_PASSING
39 #include "dbus-sysdeps.h"
40 #include "dbus-sysdeps-unix.h"
42 #include "dbus-marshal-gvariant.h"
43 #include "dbus-protocol-gvariant.h"
47 #define _DBUS_TYPE_IS_STRINGLIKE(type) \
48 (type == DBUS_TYPE_STRING || type == DBUS_TYPE_SIGNATURE || \
49 type == DBUS_TYPE_OBJECT_PATH)
51 static unsigned char _dbus_default_protocol_version = DBUS_MAJOR_PROTOCOL_VERSION; /* DBUS_PROTOCOL_VERSION_GVARIANT; */
52 static dbus_bool_t _dbus_first_bus_open = FALSE;
54 static void protocol_strategy_last_type (int type);
55 static void protocol_strategy_static (int type);
57 typedef void (*DBusProtocolStrategyFunction)(int type);
58 static DBusProtocolStrategyFunction _dbus_protocol_strategy_bus_function = protocol_strategy_last_type;
59 static DBusProtocolStrategyFunction _dbus_protocol_strategy_message_function = protocol_strategy_static;
61 static void dbus_message_finalize (DBusMessage *message);
64 * @defgroup DBusMessageInternals DBusMessage implementation details
65 * @ingroup DBusInternals
66 * @brief DBusMessage private implementation details.
68 * The guts of DBusMessage and its methods.
73 #ifdef DBUS_ENABLE_EMBEDDED_TESTS
75 _dbus_enable_message_cache (void)
77 static int enabled = -1;
81 const char *s = _dbus_getenv ("DBUS_MESSAGE_CACHE");
92 _dbus_warn ("DBUS_MESSAGE_CACHE should be 0 or 1 if set, not '%s'",
100 /* constant expression, should be optimized away */
101 # define _dbus_enable_message_cache() (TRUE)
104 #ifndef _dbus_message_trace_ref
106 _dbus_message_trace_ref (DBusMessage *message,
111 static int enabled = -1;
113 _dbus_trace_ref ("DBusMessage", message, old_refcount, new_refcount, why,
114 "DBUS_MESSAGE_TRACE", &enabled);
118 /* Not thread locked, but strictly const/read-only so should be OK
120 /** An static string representing an empty signature */
121 _DBUS_STRING_DEFINE_STATIC(_dbus_empty_signature_str, "");
123 /* these have wacky values to help trap uninitialized iterators;
124 * but has to fit in 3 bits
127 DBUS_MESSAGE_ITER_TYPE_READER = 3,
128 DBUS_MESSAGE_ITER_TYPE_WRITER = 7
131 /** typedef for internals of message iterator */
132 typedef struct DBusMessageRealIter DBusMessageRealIter;
134 #define BUILD_BUG_ON(condition) ((void)sizeof(char[1 - 2*!!(condition)]))
137 * @brief Internals of DBusMessageIter
139 * Object representing a position in a message. All fields are internal.
141 struct DBusMessageRealIter
143 DBusMessage *message; /**< Message used */
144 dbus_uint32_t changed_stamp : CHANGED_STAMP_BITS; /**< stamp to detect invalid iters */
145 dbus_uint32_t iter_type : 3; /**< whether this is a reader or writer iter */
146 dbus_uint32_t sig_refcount : 8; /**< depth of open_signature() */
149 DBusTypeWriter writer; /**< writer */
150 DBusTypeReader reader; /**< reader */
151 } u; /**< the type writer or reader that does all the work */
155 _dbus_header_is_gvariant (const DBusHeader *header)
157 return (header->protocol_version == DBUS_PROTOCOL_VERSION_GVARIANT);
161 _dbus_message_is_gvariant (const DBusMessage *message)
163 return _dbus_header_is_gvariant (&message->header);
167 _dbus_message_toggle_gvariant (DBusMessage *message, dbus_bool_t gvariant)
169 message->header.protocol_version = gvariant ? DBUS_PROTOCOL_VERSION_GVARIANT : DBUS_MAJOR_PROTOCOL_VERSION;
173 * Layout of a DBusMessageIter on the stack in dbus 1.10.0. This is no
174 * longer used, but for ABI compatibility we need to assert that the
175 * new layout is the same size.
180 dbus_uint32_t dummy3a : 21;
181 dbus_uint32_t dummy3b : 3;
182 dbus_uint32_t dummy3c : 8;
187 dbus_uint32_t dummy1a : 8;
188 dbus_uint32_t dummy1b : 1;
189 dbus_uint32_t dummy1c : 3;
190 dbus_uint32_t dummy1d : 1;
191 dbus_uint32_t dummy1e : 1;
192 dbus_uint32_t dummy1f : 1;
213 dbus_uint32_t dummy1a : 8;
214 dbus_uint32_t dummy1b : 8;
215 dbus_uint32_t dummy1c : 1;
216 dbus_uint32_t dummy1d : 1;
217 dbus_uint32_t dummy1e : 1;
218 dbus_uint32_t dummy1f : 1;
219 dbus_uint32_t dummy1g : 1;
247 } DBusMessageIter_1_10_0;
250 get_const_signature (DBusMessage *message,
251 const DBusString **type_str_p,
254 dbus_bool_t got_signature = FALSE;
255 if (_dbus_message_is_gvariant (message) && message->locked)
257 /* only locked GVariant messages have signatures in the body */
259 * in case of received GVariant message, there may be no signature field in a header,
260 * but in the body. However, it is not nul-terminated.
261 * So, we need to allocate space and put it into message.
262 * It could also happen before, so check message->signature for already existing.
263 * FIXME: That may kinda break oom-safety.
264 * For now - if oom, then return empty signature.
266 if (message->signature == NULL)
269 got_signature = _dbus_message_gvariant_get_signature (message,
273 if (got_signature && type_str_len > 1)
275 message->signature = dbus_new (DBusString, 1);
276 got_signature = got_signature &&
277 _dbus_string_init_preallocated (message->signature, type_str_len - 1);
279 /* we need to copy the signature, but to ensure backward compatibility
280 * it must be stripped off outer parentheses - they are always there */
281 got_signature = got_signature &&
282 _dbus_string_copy_len (*type_str_p, *type_pos_p + 1, type_str_len - 2,
283 message->signature, 0);
284 got_signature = got_signature &&
285 _dbus_string_append_byte (message->signature, 0);
289 got_signature = TRUE;
293 *type_str_p = message->signature;
297 else if (_dbus_header_get_field_raw (&message->header,
298 DBUS_HEADER_FIELD_SIGNATURE,
302 if (!_dbus_message_is_gvariant (message))
303 *type_pos_p += 1; /* skip the signature length which is 1 byte */
304 got_signature = TRUE;
308 *type_str_p = &_dbus_empty_signature_str;
314 * Swaps the message to compiler byte order if required
316 * @param message the message
319 _dbus_message_byteswap (DBusMessage *message)
321 const DBusString *type_str;
325 byte_order = _dbus_header_get_byte_order (&message->header);
327 if (byte_order == DBUS_COMPILER_BYTE_ORDER)
330 _dbus_verbose ("Swapping message into compiler byte order\n");
332 get_const_signature (message, &type_str, &type_pos);
334 _dbus_marshal_byteswap (type_str, type_pos,
336 DBUS_COMPILER_BYTE_ORDER,
339 _dbus_header_byteswap (&message->header, DBUS_COMPILER_BYTE_ORDER);
340 _dbus_assert (_dbus_header_get_byte_order (&message->header) ==
341 DBUS_COMPILER_BYTE_ORDER);
344 /** byte-swap the message if it doesn't match our byte order.
345 * Called only when we need the message in our own byte order,
346 * normally when reading arrays of integers or doubles.
347 * Otherwise should not be called since it would do needless
350 #define ensure_byte_order(message) _dbus_message_byteswap (message)
353 * Gets the data to be sent over the network for this message.
354 * The header and then the body should be written out.
355 * This function is guaranteed to always return the same
356 * data once a message is locked (with dbus_message_lock()).
358 * @param message the message.
359 * @param header return location for message header data.
360 * @param body return location for message body data.
363 _dbus_message_get_network_data (DBusMessage *message,
364 const DBusString **header,
365 const DBusString **body)
367 _dbus_assert (message->locked);
369 *header = &message->header.data;
370 *body = &message->body;
373 void _dbus_message_set_timeout_ms(DBusMessage *message, int timeout_ms) {
374 message->timeout_ms = timeout_ms;
376 int _dbus_message_get_timeout_ms(DBusMessage const *message) {
377 return message->timeout_ms;
381 * Gets the unix fds to be sent over the network for this message.
382 * This function is guaranteed to always return the same data once a
383 * message is locked (with dbus_message_lock()).
385 * @param message the message.
386 * @param fds return location of unix fd array
387 * @param n_fds return number of entries in array
389 void _dbus_message_get_unix_fds(DBusMessage *message,
393 _dbus_assert (message->locked);
395 #ifdef HAVE_UNIX_FD_PASSING
396 *fds = message->unix_fds;
397 *n_fds = message->n_unix_fds;
405 * Sets the serial number of a message.
406 * This can only be done once on a message.
408 * DBusConnection will automatically set the serial to an appropriate value
409 * when the message is sent; this function is only needed when encapsulating
410 * messages in another protocol, or otherwise bypassing DBusConnection.
412 * @param message the message
413 * @param serial the serial
416 dbus_message_set_serial (DBusMessage *message,
417 dbus_uint32_t serial)
419 _dbus_return_if_fail (message != NULL);
420 _dbus_return_if_fail (!message->locked);
422 _dbus_header_set_serial (&message->header, serial);
426 * Adds a counter to be incremented immediately with the size/unix fds
427 * of this message, and decremented by the size/unix fds of this
428 * message when this message if finalized. The link contains a
429 * counter with its refcount already incremented, but the counter
430 * itself not incremented. Ownership of link and counter refcount is
431 * passed to the message.
433 * This function may be called with locks held. As a result, the counter's
434 * notify function is not called; the caller is expected to either call
435 * _dbus_counter_notify() on the counter when they are no longer holding
436 * locks, or take the same action that would be taken by the notify function.
438 * @param message the message
439 * @param link link with counter as data
442 _dbus_message_add_counter_link (DBusMessage *message,
445 /* right now we don't recompute the delta when message
446 * size changes, and that's OK for current purposes
447 * I think, but could be important to change later.
448 * Do recompute it whenever there are no outstanding counters,
449 * since it's basically free.
451 if (message->counters == NULL)
453 message->size_counter_delta =
454 _dbus_string_get_length (&message->header.data) +
455 _dbus_string_get_length (&message->body);
457 #ifdef HAVE_UNIX_FD_PASSING
458 message->unix_fd_counter_delta = message->n_unix_fds;
462 _dbus_verbose ("message has size %ld\n",
463 message->size_counter_delta);
467 _dbus_list_append_link (&message->counters, link);
469 _dbus_counter_adjust_size (link->data, message->size_counter_delta);
471 #ifdef HAVE_UNIX_FD_PASSING
472 _dbus_counter_adjust_unix_fd (link->data, message->unix_fd_counter_delta);
477 * Adds a counter to be incremented immediately with the size/unix fds
478 * of this message, and decremented by the size/unix fds of this
479 * message when this message if finalized.
481 * This function may be called with locks held. As a result, the counter's
482 * notify function is not called; the caller is expected to either call
483 * _dbus_counter_notify() on the counter when they are no longer holding
484 * locks, or take the same action that would be taken by the notify function.
486 * @param message the message
487 * @param counter the counter
488 * @returns #FALSE if no memory
491 _dbus_message_add_counter (DBusMessage *message,
492 DBusCounter *counter)
496 link = _dbus_list_alloc_link (counter);
500 _dbus_counter_ref (counter);
501 _dbus_message_add_counter_link (message, link);
507 * Removes a counter tracking the size/unix fds of this message, and
508 * decrements the counter by the size/unix fds of this message.
510 * @param message the message
511 * @param counter the counter
514 _dbus_message_remove_counter (DBusMessage *message,
515 DBusCounter *counter)
519 link = _dbus_list_find_last (&message->counters,
521 _dbus_assert (link != NULL);
523 _dbus_list_remove_link (&message->counters, link);
525 _dbus_counter_adjust_size (counter, - message->size_counter_delta);
527 #ifdef HAVE_UNIX_FD_PASSING
528 _dbus_counter_adjust_unix_fd (counter, - message->unix_fd_counter_delta);
531 _dbus_counter_notify (counter);
532 _dbus_counter_unref (counter);
536 * Locks a message. Allows checking that applications don't keep a
537 * reference to a message in the outgoing queue and change it
538 * underneath us. Messages are locked when they enter the outgoing
539 * queue (dbus_connection_send_message()), and the library complains
540 * if the message is modified while locked. This function may also
541 * called externally, for applications wrapping D-Bus in another protocol.
543 * @param message the message to lock.
546 dbus_message_lock (DBusMessage *message)
548 if (!message->locked)
550 if (!_dbus_message_is_gvariant (message))
551 _dbus_header_update_lengths (&message->header,
552 _dbus_string_get_length (&message->body));
554 _dbus_message_finalize_gvariant (message, TRUE);
556 /* must have a signature if you have a body */
557 _dbus_assert (_dbus_string_get_length (&message->body) == 0 ||
558 dbus_message_get_signature (message) != NULL);
560 message->locked = TRUE;
565 set_or_delete_string_field (DBusMessage *message,
571 return _dbus_header_delete_field (&message->header, field);
573 return _dbus_header_set_field_basic (&message->header,
581 * We cache some DBusMessage to reduce the overhead of allocating
582 * them. In my profiling this consistently made about an 8%
583 * difference. It avoids the malloc for the message, the malloc for
584 * the slot list, the malloc for the header string and body string,
585 * and the associated free() calls. It does introduce another global
586 * lock which could be a performance issue in certain cases.
588 * For the echo client/server the round trip time goes from around
589 * .000077 to .000069 with the message cache on my laptop. The sysprof
590 * change is as follows (numbers are cumulative percentage):
592 * with message cache implemented as array as it is now (0.000069 per):
593 * new_empty_header 1.46
594 * mutex_lock 0.56 # i.e. _DBUS_LOCK(message_cache)
600 * mutex_lock 0.33 # i.e. _DBUS_LOCK(message_cache)
603 * with message cache implemented as list (0.000070 per roundtrip):
604 * new_empty_header 2.72
605 * list_pop_first 1.88
609 * without cache (0.000077 per roundtrip):
610 * new_empty_header 6.7
611 * string_init_preallocated 3.43
620 * If you implement the message_cache with a list, the primary reason
621 * it's slower is that you add another thread lock (on the DBusList
625 /** Avoid caching huge messages */
626 #define MAX_MESSAGE_SIZE_TO_CACHE 10 * _DBUS_ONE_KILOBYTE
628 /** Avoid caching too many messages */
629 #define MAX_MESSAGE_CACHE_SIZE 5
631 /* Protected by _DBUS_LOCK (message_cache) */
632 static DBusMessage *message_cache[MAX_MESSAGE_CACHE_SIZE];
633 static int message_cache_count = 0;
634 static dbus_bool_t message_cache_shutdown_registered = FALSE;
637 dbus_message_cache_shutdown (void *data)
641 if (!_DBUS_LOCK (message_cache))
642 _dbus_assert_not_reached ("we would have initialized global locks "
643 "before registering a shutdown function");
646 while (i < MAX_MESSAGE_CACHE_SIZE)
648 if (message_cache[i])
649 dbus_message_finalize (message_cache[i]);
654 message_cache_count = 0;
655 message_cache_shutdown_registered = FALSE;
657 _DBUS_UNLOCK (message_cache);
661 * Tries to get a message from the message cache. The retrieved
662 * message will have junk in it, so it still needs to be cleared out
663 * in dbus_message_new_empty_header()
665 * @returns the message, or #NULL if none cached
668 dbus_message_get_cached (void)
670 DBusMessage *message;
675 if (!_DBUS_LOCK (message_cache))
677 /* we'd have initialized global locks before caching anything,
678 * so there can't be anything in the cache */
682 _dbus_assert (message_cache_count >= 0);
684 if (message_cache_count == 0)
686 _DBUS_UNLOCK (message_cache);
690 /* This is not necessarily true unless count > 0, and
691 * message_cache is uninitialized until the shutdown is
694 _dbus_assert (message_cache_shutdown_registered);
697 while (i < MAX_MESSAGE_CACHE_SIZE)
699 if (message_cache[i])
701 message = message_cache[i];
702 message_cache[i] = NULL;
703 message_cache_count -= 1;
708 _dbus_assert (message_cache_count >= 0);
709 _dbus_assert (i < MAX_MESSAGE_CACHE_SIZE);
710 _dbus_assert (message != NULL);
712 _dbus_assert (_dbus_atomic_get (&message->refcount) == 0);
714 _dbus_assert (message->counters == NULL);
716 _DBUS_UNLOCK (message_cache);
721 #ifdef HAVE_UNIX_FD_PASSING
723 close_unix_fds(int *fds, unsigned *n_fds)
733 for (i = 0; i < *n_fds; i++)
735 if (!_dbus_close(fds[i], &e))
737 _dbus_warn("Failed to close file descriptor: %s", e.message);
744 /* We don't free the array here, in case we can recycle it later */
749 free_counter (void *element,
752 DBusCounter *counter = element;
753 DBusMessage *message = data;
755 _dbus_counter_adjust_size (counter, - message->size_counter_delta);
756 #ifdef HAVE_UNIX_FD_PASSING
757 _dbus_counter_adjust_unix_fd (counter, - message->unix_fd_counter_delta);
760 _dbus_counter_notify (counter);
761 _dbus_counter_unref (counter);
765 * Tries to cache a message, otherwise finalize it.
767 * @param message the message
770 dbus_message_cache_or_finalize (DBusMessage *message)
772 dbus_bool_t was_cached;
775 _dbus_assert (_dbus_atomic_get (&message->refcount) == 0);
777 /* This calls application code and has to be done first thing
778 * without holding the lock
780 _dbus_data_slot_list_clear (&message->slot_list);
782 _dbus_list_foreach (&message->counters,
783 free_counter, message);
784 _dbus_list_clear (&message->counters);
786 #ifdef HAVE_UNIX_FD_PASSING
787 close_unix_fds(message->unix_fds, &message->n_unix_fds);
790 if (NULL != message->signature)
792 _dbus_string_free (message->signature);
793 dbus_free (message->signature);
794 message->signature = NULL;
797 if (NULL != message->unique_sender)
799 _dbus_string_free (message->unique_sender);
800 dbus_free (message->unique_sender);
801 message->unique_sender = NULL;
806 if (!_DBUS_LOCK (message_cache))
808 /* The only way to get a non-null message goes through
809 * dbus_message_get_cached() which takes the lock. */
810 _dbus_assert_not_reached ("we would have initialized global locks "
811 "the first time we constructed a message");
814 if (!message_cache_shutdown_registered)
816 _dbus_assert (message_cache_count == 0);
818 if (!_dbus_register_shutdown_func (dbus_message_cache_shutdown, NULL))
822 while (i < MAX_MESSAGE_CACHE_SIZE)
824 message_cache[i] = NULL;
828 message_cache_shutdown_registered = TRUE;
831 _dbus_assert (message_cache_count >= 0);
833 if (!_dbus_enable_message_cache ())
836 if ((_dbus_string_get_length (&message->header.data) +
837 _dbus_string_get_length (&message->body)) >
838 MAX_MESSAGE_SIZE_TO_CACHE)
841 if (message_cache_count >= MAX_MESSAGE_CACHE_SIZE)
844 /* Find empty slot */
846 while (message_cache[i] != NULL)
849 _dbus_assert (i < MAX_MESSAGE_CACHE_SIZE);
851 _dbus_assert (message_cache[i] == NULL);
852 message_cache[i] = message;
853 message_cache_count += 1;
855 #ifndef DBUS_DISABLE_CHECKS
856 message->in_cache = TRUE;
860 _dbus_assert (_dbus_atomic_get (&message->refcount) == 0);
862 _DBUS_UNLOCK (message_cache);
865 dbus_message_finalize (message);
869 * Arrange for iter to be something that _dbus_message_iter_check() would
870 * reject as not a valid iterator.
873 _dbus_message_real_iter_zero (DBusMessageRealIter *iter)
875 _dbus_assert (iter != NULL);
877 /* NULL is not, strictly speaking, guaranteed to be all-bits-zero */
878 iter->message = NULL;
882 * Initialize iter as if with #DBUS_MESSAGE_ITER_INIT_CLOSED. The only valid
883 * operation for such an iterator is
884 * dbus_message_iter_abandon_container_if_open(), which does nothing.
887 dbus_message_iter_init_closed (DBusMessageIter *iter)
889 _dbus_return_if_fail (iter != NULL);
890 _dbus_message_real_iter_zero ((DBusMessageRealIter *) iter);
894 _dbus_message_real_iter_is_zeroed (DBusMessageRealIter *iter)
896 return (iter != NULL && iter->message == NULL && iter->changed_stamp == 0 &&
897 iter->iter_type == 0 && iter->sig_refcount == 0);
900 #if defined(DBUS_ENABLE_CHECKS) || defined(DBUS_ENABLE_ASSERT)
902 _dbus_message_iter_check (DBusMessageRealIter *iter)
908 _dbus_warn_check_failed ("dbus message iterator is NULL");
912 if (iter->message == NULL || iter->iter_type == 0)
914 _dbus_warn_check_failed ("dbus message iterator has already been "
915 "closed, or is uninitialized or corrupt");
919 byte_order = _dbus_header_get_byte_order (&iter->message->header);
921 if (iter->iter_type == DBUS_MESSAGE_ITER_TYPE_READER)
923 if (iter->u.reader.byte_order != byte_order)
925 _dbus_warn_check_failed ("dbus message changed byte order since iterator was created");
928 /* because we swap the message into compiler order when you init an iter */
929 _dbus_assert (iter->u.reader.byte_order == DBUS_COMPILER_BYTE_ORDER);
931 else if (iter->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER)
933 if (iter->u.writer.byte_order != byte_order)
935 _dbus_warn_check_failed ("dbus message changed byte order since append iterator was created");
938 /* because we swap the message into compiler order when you init an iter */
939 _dbus_assert (iter->u.writer.byte_order == DBUS_COMPILER_BYTE_ORDER);
943 _dbus_warn_check_failed ("dbus message iterator looks uninitialized or corrupted");
947 if (iter->changed_stamp != iter->message->changed_stamp)
949 _dbus_warn_check_failed ("dbus message iterator invalid because the message has been modified (or perhaps the iterator is just uninitialized)");
955 #endif /* DBUS_ENABLE_CHECKS || DBUS_ENABLE_ASSERT */
958 * Implementation of the varargs arg-getting functions.
959 * dbus_message_get_args() is the place to go for complete
962 * @see dbus_message_get_args
963 * @param iter the message iter
964 * @param error error to be filled in
965 * @param first_arg_type type of the first argument
966 * @param var_args return location for first argument, followed by list of type/location pairs
967 * @returns #FALSE if error was set
970 _dbus_message_iter_get_args_valist (DBusMessageIter *iter,
975 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
976 int spec_type, msg_type, i, j;
980 _dbus_assert (_dbus_message_iter_check (real));
984 spec_type = first_arg_type;
987 /* copy var_args first, then we can do another iteration over it to
988 * free memory and close unix fds if parse failed at some point.
990 DBUS_VA_COPY (copy_args, var_args);
992 while (spec_type != DBUS_TYPE_INVALID)
994 msg_type = dbus_message_iter_get_arg_type (iter);
996 if (msg_type != spec_type)
998 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
999 "Argument %d is specified to be of type \"%s\", but "
1000 "is actually of type \"%s\"\n", i,
1001 _dbus_type_to_string (spec_type),
1002 _dbus_type_to_string (msg_type));
1007 if (spec_type == DBUS_TYPE_UNIX_FD)
1009 #ifdef HAVE_UNIX_FD_PASSING
1013 pfd = va_arg (var_args, int*);
1016 _dbus_type_reader_read_basic(&real->u.reader, &idx);
1018 if (idx.u32 >= real->message->n_unix_fds)
1020 dbus_set_error (error, DBUS_ERROR_INCONSISTENT_MESSAGE,
1021 "Message refers to file descriptor at index %i,"
1022 "but has only %i descriptors attached.\n",
1024 real->message->n_unix_fds);
1028 if ((nfd = _dbus_dup(real->message->unix_fds[idx.u32], error)) < 0)
1033 dbus_set_error (error, DBUS_ERROR_NOT_SUPPORTED,
1034 "Platform does not support file desciptor passing.\n");
1038 else if (dbus_type_is_basic (spec_type))
1040 DBusBasicValue *ptr;
1042 ptr = va_arg (var_args, DBusBasicValue*);
1044 _dbus_assert (ptr != NULL);
1046 _dbus_type_reader_read_basic (&real->u.reader,
1049 else if (spec_type == DBUS_TYPE_ARRAY)
1052 int spec_element_type;
1053 const DBusBasicValue **ptr;
1055 DBusTypeReader array;
1057 spec_element_type = va_arg (var_args, int);
1058 element_type = _dbus_type_reader_get_element_type (&real->u.reader);
1060 if (spec_element_type != element_type)
1062 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1063 "Argument %d is specified to be an array of \"%s\", but "
1064 "is actually an array of \"%s\"\n",
1066 _dbus_type_to_string (spec_element_type),
1067 _dbus_type_to_string (element_type));
1072 if (dbus_type_is_fixed (spec_element_type) &&
1073 element_type != DBUS_TYPE_UNIX_FD)
1075 ptr = va_arg (var_args, const DBusBasicValue**);
1076 n_elements_p = va_arg (var_args, int*);
1078 _dbus_assert (ptr != NULL);
1079 _dbus_assert (n_elements_p != NULL);
1081 _dbus_type_reader_recurse (&real->u.reader, &array);
1083 _dbus_type_reader_read_fixed_multi (&array,
1084 (void *) ptr, n_elements_p);
1086 else if (_DBUS_TYPE_IS_STRINGLIKE (spec_element_type))
1088 char ***str_array_p;
1092 str_array_p = va_arg (var_args, char***);
1093 n_elements_p = va_arg (var_args, int*);
1095 _dbus_assert (str_array_p != NULL);
1096 _dbus_assert (n_elements_p != NULL);
1098 /* Count elements in the array */
1099 _dbus_type_reader_recurse (&real->u.reader, &array);
1102 while (_dbus_type_reader_get_current_type (&array) != DBUS_TYPE_INVALID)
1105 _dbus_type_reader_next (&array);
1108 str_array = dbus_new0 (char*, n_elements + 1);
1109 if (str_array == NULL)
1111 _DBUS_SET_OOM (error);
1115 /* Now go through and dup each string */
1116 _dbus_type_reader_recurse (&real->u.reader, &array);
1119 while (j < n_elements)
1122 _dbus_type_reader_read_basic (&array,
1125 str_array[j] = _dbus_strdup (s);
1126 if (str_array[j] == NULL)
1128 dbus_free_string_array (str_array);
1129 _DBUS_SET_OOM (error);
1135 if (!_dbus_type_reader_next (&array))
1136 _dbus_assert (j == n_elements);
1139 _dbus_assert (_dbus_type_reader_get_current_type (&array) == DBUS_TYPE_INVALID);
1140 _dbus_assert (j == n_elements);
1141 _dbus_assert (str_array[j] == NULL);
1143 *str_array_p = str_array;
1144 *n_elements_p = n_elements;
1146 #ifndef DBUS_DISABLE_CHECKS
1149 _dbus_warn ("you can't read arrays of container types (struct, variant, array) with %s for now",
1150 _DBUS_FUNCTION_NAME);
1155 #ifndef DBUS_DISABLE_CHECKS
1158 _dbus_warn ("you can only read arrays and basic types with %s for now",
1159 _DBUS_FUNCTION_NAME);
1164 /* how many arguments already handled */
1167 spec_type = va_arg (var_args, int);
1168 if (!_dbus_type_reader_next (&real->u.reader) && spec_type != DBUS_TYPE_INVALID)
1170 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS,
1171 "Message has only %d arguments, but more were expected", i);
1179 /* there may memory or unix fd leak in the above iteration if parse failed.
1180 * so we have another iteration over copy_args to free memory and close
1185 spec_type = first_arg_type;
1190 if (spec_type == DBUS_TYPE_UNIX_FD)
1192 #ifdef HAVE_UNIX_FD_PASSING
1195 pfd = va_arg (copy_args, int *);
1199 _dbus_close (*pfd, NULL);
1204 else if (dbus_type_is_basic (spec_type))
1206 /* move the index forward */
1207 va_arg (copy_args, DBusBasicValue *);
1209 else if (spec_type == DBUS_TYPE_ARRAY)
1211 int spec_element_type;
1213 spec_element_type = va_arg (copy_args, int);
1214 if (dbus_type_is_fixed (spec_element_type))
1216 /* move the index forward */
1217 va_arg (copy_args, const DBusBasicValue **);
1218 va_arg (copy_args, int *);
1220 else if (_DBUS_TYPE_IS_STRINGLIKE (spec_element_type))
1222 char ***str_array_p;
1224 str_array_p = va_arg (copy_args, char ***);
1225 /* move the index forward */
1226 va_arg (copy_args, int *);
1227 _dbus_assert (str_array_p != NULL);
1228 dbus_free_string_array (*str_array_p);
1229 *str_array_p = NULL;
1233 spec_type = va_arg (copy_args, int);
1245 * @defgroup DBusMessage DBusMessage
1247 * @brief Message to be sent or received over a #DBusConnection.
1249 * A DBusMessage is the most basic unit of communication over a
1250 * DBusConnection. A DBusConnection represents a stream of messages
1251 * received from a remote application, and a stream of messages
1252 * sent to a remote application.
1254 * A message has a message type, returned from
1255 * dbus_message_get_type(). This indicates whether the message is a
1256 * method call, a reply to a method call, a signal, or an error reply.
1258 * A message has header fields such as the sender, destination, method
1259 * or signal name, and so forth. DBusMessage has accessor functions for
1260 * these, such as dbus_message_get_member().
1262 * Convenience functions dbus_message_is_method_call(), dbus_message_is_signal(),
1263 * and dbus_message_is_error() check several header fields at once and are
1264 * slightly more efficient than checking the header fields with individual
1265 * accessor functions.
1267 * Finally, a message has arguments. The number and types of arguments
1268 * are in the message's signature header field (accessed with
1269 * dbus_message_get_signature()). Simple argument values are usually
1270 * retrieved with dbus_message_get_args() but more complex values such
1271 * as structs may require the use of #DBusMessageIter.
1273 * The D-Bus specification goes into some more detail about header fields and
1280 * @typedef DBusMessage
1282 * Opaque data type representing a message received from or to be
1283 * sent to another application.
1287 * Returns the serial of a message or 0 if none has been specified.
1288 * The message's serial number is provided by the application sending
1289 * the message and is used to identify replies to this message.
1291 * All messages received on a connection will have a serial provided
1292 * by the remote application.
1294 * For messages you're sending, dbus_connection_send() will assign a
1295 * serial and return it to you.
1297 * @param message the message
1298 * @returns the serial
1301 dbus_message_get_serial (DBusMessage *message)
1303 _dbus_return_val_if_fail (message != NULL, 0);
1305 return _dbus_header_get_serial (&message->header);
1309 * Sets the reply serial of a message (the serial of the message this
1312 * @param message the message
1313 * @param reply_serial the serial we're replying to
1314 * @returns #FALSE if not enough memory
1317 dbus_message_set_reply_serial (DBusMessage *message,
1318 dbus_uint32_t reply_serial)
1320 DBusBasicValue value;
1323 _dbus_return_val_if_fail (message != NULL, FALSE);
1324 _dbus_return_val_if_fail (!message->locked, FALSE);
1325 _dbus_return_val_if_fail (reply_serial != 0, FALSE); /* 0 is invalid */
1327 if (_dbus_message_is_gvariant (message))
1329 type = DBUS_TYPE_UINT64;
1330 value.u64 = reply_serial;
1334 type = DBUS_TYPE_UINT32;
1335 value.u32 = reply_serial;
1338 return _dbus_header_set_field_basic (&message->header,
1339 DBUS_HEADER_FIELD_REPLY_SERIAL,
1345 * Returns the serial that the message is a reply to or 0 if none.
1347 * @param message the message
1348 * @returns the reply serial
1351 dbus_message_get_reply_serial (DBusMessage *message)
1353 dbus_uint32_t v_UINT32;
1354 dbus_uint64_t v_UINT64;
1355 int type = DBUS_TYPE_UINT32;
1356 void *value = &v_UINT32;
1358 _dbus_return_val_if_fail (message != NULL, 0);
1360 if (_dbus_message_is_gvariant (message))
1362 type = DBUS_TYPE_UINT64;
1366 if (_dbus_header_get_field_basic (&message->header,
1367 DBUS_HEADER_FIELD_REPLY_SERIAL,
1371 if (_dbus_message_is_gvariant (message))
1381 dbus_message_finalize (DBusMessage *message)
1383 _dbus_assert (_dbus_atomic_get (&message->refcount) == 0);
1385 /* This calls application callbacks! */
1386 _dbus_data_slot_list_free (&message->slot_list);
1388 _dbus_list_foreach (&message->counters,
1389 free_counter, message);
1390 _dbus_list_clear (&message->counters);
1392 _dbus_header_free (&message->header);
1393 _dbus_string_free (&message->body);
1395 #ifdef HAVE_UNIX_FD_PASSING
1396 close_unix_fds(message->unix_fds, &message->n_unix_fds);
1397 dbus_free(message->unix_fds);
1400 _dbus_assert (_dbus_atomic_get (&message->refcount) == 0);
1402 dbus_free (message);
1406 dbus_message_new_empty_header (dbus_bool_t gvariant)
1408 DBusMessage *message;
1409 dbus_bool_t from_cache;
1411 message = dbus_message_get_cached ();
1413 if (message != NULL)
1420 message = dbus_new0 (DBusMessage, 1);
1421 if (message == NULL)
1423 #ifndef DBUS_DISABLE_CHECKS
1424 message->generation = _dbus_current_generation;
1427 #ifdef HAVE_UNIX_FD_PASSING
1428 message->unix_fds = NULL;
1429 message->n_unix_fds_allocated = 0;
1433 _dbus_atomic_inc (&message->refcount);
1435 _dbus_message_trace_ref (message, 0, 1, "new_empty_header");
1437 message->locked = FALSE;
1438 #ifndef DBUS_DISABLE_CHECKS
1439 message->in_cache = FALSE;
1441 message->counters = NULL;
1442 message->size_counter_delta = 0;
1443 message->timeout_ms = -1;
1444 message->changed_stamp = 0;
1446 #ifdef HAVE_UNIX_FD_PASSING
1447 message->n_unix_fds = 0;
1448 message->n_unix_fds_allocated = 0;
1449 message->unix_fd_counter_delta = 0;
1452 _dbus_message_toggle_gvariant (message, gvariant); /* this works only if kdbus is enabled */
1455 _dbus_data_slot_list_init (&message->slot_list);
1459 _dbus_header_reinit (&message->header);
1460 _dbus_string_set_length (&message->body, 0);
1464 if (!_dbus_header_init (&message->header))
1466 dbus_free (message);
1470 if (!_dbus_string_init_preallocated (&message->body, 32))
1472 _dbus_header_free (&message->header);
1473 dbus_free (message);
1478 message->signature = NULL;
1479 message->unique_sender = NULL;
1480 message->gvariant_body_last_offset = GVARIANT_LAST_OFFSET_NOT_SET;
1481 message->gvariant_body_last_pos = 0;
1487 _dbus_message_create_protocol_version (int message_type,
1488 const char *destination,
1490 const char *interface,
1492 const char *error_name,
1493 dbus_bool_t gvariant)
1495 DBusMessage *message;
1497 _dbus_assert (message_type != DBUS_MESSAGE_TYPE_INVALID);
1499 message = dbus_message_new_empty_header (gvariant);
1500 if (message == NULL)
1503 if (!(_dbus_message_is_gvariant(message) ? _dbus_header_gvariant_create : _dbus_header_create) (&message->header,
1504 DBUS_COMPILER_BYTE_ORDER,
1506 destination, path, interface, member, error_name))
1508 dbus_message_unref (message);
1516 _dbus_message_create (int message_type,
1517 const char *destination,
1519 const char *interface,
1521 const char *error_name)
1523 return _dbus_message_create_protocol_version(message_type,
1529 _dbus_default_protocol_version == DBUS_PROTOCOL_VERSION_GVARIANT);
1533 * Constructs a new message of the given message type.
1534 * Types include #DBUS_MESSAGE_TYPE_METHOD_CALL,
1535 * #DBUS_MESSAGE_TYPE_SIGNAL, and so forth.
1537 * Usually you want to use dbus_message_new_method_call(),
1538 * dbus_message_new_method_return(), dbus_message_new_signal(),
1539 * or dbus_message_new_error() instead.
1541 * @param message_type type of message
1542 * @returns new message or #NULL if no memory
1545 dbus_message_new (int message_type)
1547 return _dbus_message_create(message_type,
1548 NULL, NULL, NULL, NULL, NULL);
1552 * Constructs a new message to invoke a method on a remote
1553 * object. Returns #NULL if memory can't be allocated for the
1554 * message. The destination may be #NULL in which case no destination
1555 * is set; this is appropriate when using D-Bus in a peer-to-peer
1556 * context (no message bus). The interface may be #NULL, which means
1557 * that if multiple methods with the given name exist it is undefined
1558 * which one will be invoked.
1560 * The path and method names may not be #NULL.
1562 * Destination, path, interface, and method name can't contain
1563 * any invalid characters (see the D-Bus specification).
1565 * @param destination name that the message should be sent to or #NULL
1566 * @param path object path the message should be sent to
1567 * @param iface interface to invoke method on, or #NULL
1568 * @param method method to invoke
1570 * @returns a new DBusMessage, free with dbus_message_unref()
1573 dbus_message_new_method_call (const char *destination,
1578 _dbus_return_val_if_fail (path != NULL, NULL);
1579 _dbus_return_val_if_fail (method != NULL, NULL);
1580 _dbus_return_val_if_fail (destination == NULL ||
1581 _dbus_check_is_valid_bus_name (destination), NULL);
1582 _dbus_return_val_if_fail (_dbus_check_is_valid_path (path), NULL);
1583 _dbus_return_val_if_fail (iface == NULL ||
1584 _dbus_check_is_valid_interface (iface), NULL);
1585 _dbus_return_val_if_fail (_dbus_check_is_valid_member (method), NULL);
1587 return _dbus_message_create(DBUS_MESSAGE_TYPE_METHOD_CALL,
1588 destination, path, iface, method, NULL);
1592 * Constructs a message that is a reply to a method call. Returns
1593 * #NULL if memory can't be allocated for the message.
1595 * @param method_call the message being replied to
1596 * @returns a new DBusMessage, free with dbus_message_unref()
1599 dbus_message_new_method_return (DBusMessage *method_call)
1601 DBusMessage *message;
1604 _dbus_return_val_if_fail (method_call != NULL, NULL);
1606 sender = dbus_message_get_sender (method_call);
1608 /* sender is allowed to be null here in peer-to-peer case */
1610 message = _dbus_message_create (DBUS_MESSAGE_TYPE_METHOD_RETURN,
1611 sender, NULL, NULL, NULL, NULL);
1612 if (message == NULL)
1615 dbus_message_set_no_reply (message, TRUE);
1617 if (!dbus_message_set_reply_serial (message,
1618 dbus_message_get_serial (method_call)))
1620 dbus_message_unref (message);
1628 * Constructs a new message representing a signal emission. Returns
1629 * #NULL if memory can't be allocated for the message. A signal is
1630 * identified by its originating object path, interface, and the name
1633 * Path, interface, and signal name must all be valid (the D-Bus
1634 * specification defines the syntax of these fields).
1636 * @param path the path to the object emitting the signal
1637 * @param iface the interface the signal is emitted from
1638 * @param name name of the signal
1639 * @returns a new DBusMessage, free with dbus_message_unref()
1642 dbus_message_new_signal (const char *path,
1646 DBusMessage *message;
1648 _dbus_return_val_if_fail (path != NULL, NULL);
1649 _dbus_return_val_if_fail (iface != NULL, NULL);
1650 _dbus_return_val_if_fail (name != NULL, NULL);
1651 _dbus_return_val_if_fail (_dbus_check_is_valid_path (path), NULL);
1652 _dbus_return_val_if_fail (_dbus_check_is_valid_interface (iface), NULL);
1653 _dbus_return_val_if_fail (_dbus_check_is_valid_member (name), NULL);
1655 message = _dbus_message_create (DBUS_MESSAGE_TYPE_SIGNAL,
1656 NULL, path, iface, name, NULL);
1657 if (message == NULL)
1660 dbus_message_set_no_reply (message, TRUE);
1666 * Creates a new message that is an error reply to another message.
1667 * Error replies are most common in response to method calls, but
1668 * can be returned in reply to any message.
1670 * The error name must be a valid error name according to the syntax
1671 * given in the D-Bus specification. If you don't want to make
1672 * up an error name just use #DBUS_ERROR_FAILED.
1674 * @param reply_to the message we're replying to
1675 * @param error_name the error name
1676 * @param error_message the error message string (or #NULL for none, but please give a message)
1677 * @returns a new error message object, free with dbus_message_unref()
1680 dbus_message_new_error (DBusMessage *reply_to,
1681 const char *error_name,
1682 const char *error_message)
1684 DBusMessage *message;
1686 DBusMessageIter iter;
1688 _dbus_return_val_if_fail (reply_to != NULL, NULL);
1689 _dbus_return_val_if_fail (error_name != NULL, NULL);
1690 _dbus_return_val_if_fail (_dbus_check_is_valid_error_name (error_name), NULL);
1692 sender = dbus_message_get_sender (reply_to);
1694 /* sender may be NULL for non-message-bus case or
1695 * when the message bus is dealing with an unregistered
1698 message = _dbus_message_create (DBUS_MESSAGE_TYPE_ERROR,
1699 sender, NULL, NULL, NULL, error_name);
1700 if (message == NULL)
1703 dbus_message_set_no_reply (message, TRUE);
1705 if (!dbus_message_set_reply_serial (message,
1706 dbus_message_get_serial (reply_to)))
1708 dbus_message_unref (message);
1712 if (error_message != NULL)
1714 dbus_message_iter_init_append (message, &iter);
1715 if (!dbus_message_iter_append_basic (&iter,
1719 dbus_message_unref (message);
1728 * Creates a new message that is an error reply to another message, allowing
1729 * you to use printf formatting.
1731 * See dbus_message_new_error() for details - this function is the same
1732 * aside from the printf formatting.
1734 * @todo add _DBUS_GNUC_PRINTF to this (requires moving _DBUS_GNUC_PRINTF to
1735 * public header, see DBUS_DEPRECATED for an example)
1737 * @param reply_to the original message
1738 * @param error_name the error name
1739 * @param error_format the error message format as with printf
1740 * @param ... format string arguments
1741 * @returns a new error message
1744 dbus_message_new_error_printf (DBusMessage *reply_to,
1745 const char *error_name,
1746 const char *error_format,
1751 DBusMessage *message;
1753 _dbus_return_val_if_fail (reply_to != NULL, NULL);
1754 _dbus_return_val_if_fail (error_name != NULL, NULL);
1755 _dbus_return_val_if_fail (_dbus_check_is_valid_error_name (error_name), NULL);
1757 if (!_dbus_string_init (&str))
1760 va_start (args, error_format);
1762 if (_dbus_string_append_printf_valist (&str, error_format, args))
1763 message = dbus_message_new_error (reply_to, error_name,
1764 _dbus_string_get_const_data (&str));
1768 _dbus_string_free (&str);
1777 * Creates a new message that is an exact replica of the message
1778 * specified, except that its refcount is set to 1, its message serial
1779 * is reset to 0, and if the original message was "locked" (in the
1780 * outgoing message queue and thus not modifiable) the new message
1781 * will not be locked.
1783 * @todo This function can't be used in programs that try to recover from OOM errors.
1785 * @param message the message
1786 * @returns the new message.or #NULL if not enough memory or Unix file descriptors (in case the message to copy includes Unix file descriptors) can be allocated.
1789 dbus_message_copy (const DBusMessage *message)
1791 DBusMessage *retval;
1793 _dbus_return_val_if_fail (message != NULL, NULL);
1795 retval = dbus_new0 (DBusMessage, 1);
1799 _dbus_atomic_inc (&retval->refcount);
1801 retval->locked = FALSE;
1802 #ifndef DBUS_DISABLE_CHECKS
1803 retval->generation = message->generation;
1805 _dbus_message_toggle_gvariant (retval, _dbus_message_is_gvariant (message));
1807 if (!_dbus_header_copy (&message->header, &retval->header))
1813 if (!_dbus_string_init_preallocated (&retval->body,
1814 _dbus_string_get_length (&message->body)))
1816 _dbus_header_free (&retval->header);
1821 if (!_dbus_string_copy (&message->body, 0,
1825 #ifdef HAVE_UNIX_FD_PASSING
1826 retval->unix_fds = dbus_new(int, message->n_unix_fds);
1827 if (retval->unix_fds == NULL && message->n_unix_fds > 0)
1830 retval->n_unix_fds_allocated = message->n_unix_fds;
1832 for (retval->n_unix_fds = 0;
1833 retval->n_unix_fds < message->n_unix_fds;
1834 retval->n_unix_fds++)
1836 retval->unix_fds[retval->n_unix_fds] = _dbus_dup(message->unix_fds[retval->n_unix_fds], NULL);
1838 if (retval->unix_fds[retval->n_unix_fds] < 0)
1844 _dbus_message_trace_ref (retval, 0, 1, "copy");
1848 _dbus_header_free (&retval->header);
1849 _dbus_string_free (&retval->body);
1851 #ifdef HAVE_UNIX_FD_PASSING
1852 close_unix_fds(retval->unix_fds, &retval->n_unix_fds);
1853 dbus_free(retval->unix_fds);
1863 * Increments the reference count of a DBusMessage.
1865 * @param message the message
1866 * @returns the message
1867 * @see dbus_message_unref
1870 dbus_message_ref (DBusMessage *message)
1872 dbus_int32_t old_refcount;
1874 _dbus_return_val_if_fail (message != NULL, NULL);
1875 _dbus_return_val_if_fail (message->generation == _dbus_current_generation, NULL);
1876 _dbus_return_val_if_fail (!message->in_cache, NULL);
1878 old_refcount = _dbus_atomic_inc (&message->refcount);
1879 _dbus_assert (old_refcount >= 1);
1880 _dbus_message_trace_ref (message, old_refcount, old_refcount + 1, "ref");
1886 * Decrements the reference count of a DBusMessage, freeing the
1887 * message if the count reaches 0.
1889 * @param message the message
1890 * @see dbus_message_ref
1893 dbus_message_unref (DBusMessage *message)
1895 dbus_int32_t old_refcount;
1897 _dbus_return_if_fail (message != NULL);
1898 _dbus_return_if_fail (message->generation == _dbus_current_generation);
1899 _dbus_return_if_fail (!message->in_cache);
1901 old_refcount = _dbus_atomic_dec (&message->refcount);
1903 _dbus_assert (old_refcount >= 1);
1905 _dbus_message_trace_ref (message, old_refcount, old_refcount - 1, "unref");
1907 if (old_refcount == 1)
1909 /* Calls application callbacks! */
1910 dbus_message_cache_or_finalize (message);
1915 * Gets the type of a message. Types include
1916 * #DBUS_MESSAGE_TYPE_METHOD_CALL, #DBUS_MESSAGE_TYPE_METHOD_RETURN,
1917 * #DBUS_MESSAGE_TYPE_ERROR, #DBUS_MESSAGE_TYPE_SIGNAL, but other
1918 * types are allowed and all code must silently ignore messages of
1919 * unknown type. #DBUS_MESSAGE_TYPE_INVALID will never be returned.
1921 * @param message the message
1922 * @returns the type of the message
1925 dbus_message_get_type (DBusMessage *message)
1927 _dbus_return_val_if_fail (message != NULL, DBUS_MESSAGE_TYPE_INVALID);
1929 return _dbus_header_get_message_type (&message->header);
1933 * Appends fields to a message given a variable argument list. The
1934 * variable argument list should contain the type of each argument
1935 * followed by the value to append. Appendable types are basic types,
1936 * and arrays of fixed-length basic types (except arrays of Unix file
1937 * descriptors). To append variable-length basic types, or any more
1938 * complex value, you have to use an iterator rather than this
1941 * To append a basic type, specify its type code followed by the
1942 * address of the value. For example:
1946 * dbus_int32_t v_INT32 = 42;
1947 * const char *v_STRING = "Hello World";
1948 * dbus_message_append_args (message,
1949 * DBUS_TYPE_INT32, &v_INT32,
1950 * DBUS_TYPE_STRING, &v_STRING,
1951 * DBUS_TYPE_INVALID);
1954 * To append an array of fixed-length basic types (except Unix file
1955 * descriptors), pass in the DBUS_TYPE_ARRAY typecode, the element
1956 * typecode, the address of the array pointer, and a 32-bit integer
1957 * giving the number of elements in the array. So for example:
1961 * const dbus_int32_t array[] = { 1, 2, 3 };
1962 * const dbus_int32_t *v_ARRAY = array;
1963 * dbus_message_append_args (message,
1964 * DBUS_TYPE_ARRAY, DBUS_TYPE_INT32, &v_ARRAY, 3,
1965 * DBUS_TYPE_INVALID);
1969 * This function does not support arrays of Unix file descriptors. If
1970 * you need those you need to manually recurse into the array.
1972 * For Unix file descriptors this function will internally duplicate
1973 * the descriptor you passed in. Hence you may close the descriptor
1974 * immediately after this call.
1976 * @warning in C, given "int array[]", "&array == array" (the
1977 * comp.lang.c FAQ says otherwise, but gcc and the FAQ don't agree).
1978 * So if you're using an array instead of a pointer you have to create
1979 * a pointer variable, assign the array to it, then take the address
1980 * of the pointer variable. For strings it works to write
1981 * const char *array = "Hello" and then use &array though.
1983 * The last argument to this function must be #DBUS_TYPE_INVALID,
1984 * marking the end of the argument list. If you don't do this
1985 * then libdbus won't know to stop and will read invalid memory.
1987 * String/signature/path arrays should be passed in as "const char***
1988 * address_of_array" and "int n_elements"
1990 * @todo support DBUS_TYPE_STRUCT and DBUS_TYPE_VARIANT and complex arrays
1992 * @todo If this fails due to lack of memory, the message is hosed and
1993 * you have to start over building the whole message.
1995 * @param message the message
1996 * @param first_arg_type type of the first argument
1997 * @param ... value of first argument, list of additional type-value pairs
1998 * @returns #TRUE on success
2001 dbus_message_append_args (DBusMessage *message,
2008 _dbus_return_val_if_fail (message != NULL, FALSE);
2010 va_start (var_args, first_arg_type);
2011 retval = dbus_message_append_args_valist (message,
2020 * Like dbus_message_append_args() but takes a va_list for use by language bindings.
2022 * @todo for now, if this function fails due to OOM it will leave
2023 * the message half-written and you have to discard the message
2026 * @see dbus_message_append_args.
2027 * @param message the message
2028 * @param first_arg_type type of first argument
2029 * @param var_args value of first argument, then list of type/value pairs
2030 * @returns #TRUE on success
2033 dbus_message_append_args_valist (DBusMessage *message,
2038 DBusMessageIter iter;
2040 _dbus_return_val_if_fail (message != NULL, FALSE);
2042 type = first_arg_type;
2044 dbus_message_iter_init_append (message, &iter);
2046 while (type != DBUS_TYPE_INVALID)
2048 if (dbus_type_is_basic (type))
2050 const DBusBasicValue *value;
2051 value = va_arg (var_args, const DBusBasicValue*);
2053 if (!dbus_message_iter_append_basic (&iter,
2058 else if (type == DBUS_TYPE_ARRAY)
2061 DBusMessageIter array;
2064 element_type = va_arg (var_args, int);
2066 buf[0] = element_type;
2068 if (!dbus_message_iter_open_container (&iter,
2074 if (dbus_type_is_fixed (element_type) &&
2075 element_type != DBUS_TYPE_UNIX_FD)
2077 const DBusBasicValue **value;
2080 value = va_arg (var_args, const DBusBasicValue**);
2081 n_elements = va_arg (var_args, int);
2083 if (!dbus_message_iter_append_fixed_array (&array,
2087 dbus_message_iter_abandon_container (&iter, &array);
2091 else if (_DBUS_TYPE_IS_STRINGLIKE (element_type))
2093 const char ***value_p;
2098 value_p = va_arg (var_args, const char***);
2099 n_elements = va_arg (var_args, int);
2104 while (i < n_elements)
2106 if (!dbus_message_iter_append_basic (&array,
2109 dbus_message_iter_abandon_container (&iter, &array);
2117 _dbus_warn ("arrays of %s can't be appended with %s for now",
2118 _dbus_type_to_string (element_type),
2119 _DBUS_FUNCTION_NAME);
2120 dbus_message_iter_abandon_container (&iter, &array);
2124 if (!dbus_message_iter_close_container (&iter, &array))
2127 #ifndef DBUS_DISABLE_CHECKS
2130 _dbus_warn ("type %s isn't supported yet in %s",
2131 _dbus_type_to_string (type), _DBUS_FUNCTION_NAME);
2136 type = va_arg (var_args, int);
2146 * Gets arguments from a message given a variable argument list. The
2147 * supported types include those supported by
2148 * dbus_message_append_args(); that is, basic types and arrays of
2149 * fixed-length basic types. The arguments are the same as they would
2150 * be for dbus_message_iter_get_basic() or
2151 * dbus_message_iter_get_fixed_array().
2153 * In addition to those types, arrays of string, object path, and
2154 * signature are supported; but these are returned as allocated memory
2155 * and must be freed with dbus_free_string_array(), while the other
2156 * types are returned as const references. To get a string array
2157 * pass in "char ***array_location" and "int *n_elements".
2159 * Similar to dbus_message_get_fixed_array() this function does not
2160 * support arrays of type DBUS_TYPE_UNIX_FD. If you need to parse
2161 * messages with arrays of Unix file descriptors you need to recurse
2162 * into the array manually.
2164 * Unix file descriptors that are read with this function will have
2165 * the FD_CLOEXEC flag set. If you need them without this flag set,
2166 * make sure to unset it with fcntl().
2168 * The variable argument list should contain the type of the argument
2169 * followed by a pointer to where the value should be stored. The list
2170 * is terminated with #DBUS_TYPE_INVALID.
2172 * Except for string arrays, the returned values are constant; do not
2173 * free them. They point into the #DBusMessage.
2175 * If the requested arguments are not present, or do not have the
2176 * requested types, then an error will be set.
2178 * If more arguments than requested are present, the requested
2179 * arguments are returned and the extra arguments are ignored.
2181 * @todo support DBUS_TYPE_STRUCT and DBUS_TYPE_VARIANT and complex arrays
2183 * @param message the message
2184 * @param error error to be filled in on failure
2185 * @param first_arg_type the first argument type
2186 * @param ... location for first argument value, then list of type-location pairs
2187 * @returns #FALSE if the error was set
2190 dbus_message_get_args (DBusMessage *message,
2198 _dbus_return_val_if_fail (message != NULL, FALSE);
2199 _dbus_return_val_if_error_is_set (error, FALSE);
2201 va_start (var_args, first_arg_type);
2202 retval = dbus_message_get_args_valist (message, error, first_arg_type, var_args);
2209 * Like dbus_message_get_args but takes a va_list for use by language bindings.
2211 * @see dbus_message_get_args
2212 * @param message the message
2213 * @param error error to be filled in
2214 * @param first_arg_type type of the first argument
2215 * @param var_args return location for first argument, followed by list of type/location pairs
2216 * @returns #FALSE if error was set
2219 dbus_message_get_args_valist (DBusMessage *message,
2224 DBusMessageIter iter;
2226 _dbus_return_val_if_fail (message != NULL, FALSE);
2227 _dbus_return_val_if_error_is_set (error, FALSE);
2229 dbus_message_iter_init (message, &iter);
2230 return _dbus_message_iter_get_args_valist (&iter, error, first_arg_type, var_args);
2234 _dbus_message_iter_init_common (DBusMessage *message,
2235 DBusMessageRealIter *real,
2238 /* If these static assertions fail on your platform, report it as a bug. */
2239 _DBUS_STATIC_ASSERT (sizeof (DBusMessageRealIter) <= sizeof (DBusMessageIter));
2240 _DBUS_STATIC_ASSERT (_DBUS_ALIGNOF (DBusMessageRealIter) <=
2241 _DBUS_ALIGNOF (DBusMessageIter));
2242 /* A failure of these two assertions would indicate that we've broken
2243 * ABI on this platform since 1.10.0. */
2244 _DBUS_STATIC_ASSERT (sizeof (DBusMessageIter_1_10_0) ==
2245 sizeof (DBusMessageIter));
2246 _DBUS_STATIC_ASSERT (_DBUS_ALIGNOF (DBusMessageIter_1_10_0) ==
2247 _DBUS_ALIGNOF (DBusMessageIter));
2248 /* If this static assertion fails, it means the DBusMessageIter struct
2249 * is not "packed", which might result in "iter = other_iter" not copying
2251 _DBUS_STATIC_ASSERT (sizeof (DBusMessageIter) ==
2252 10 * sizeof (void *) + sizeof (dbus_uint32_t) + 3 * sizeof (int));
2254 /* Since the iterator will read or write who-knows-what from the
2255 * message, we need to get in the right byte order
2257 ensure_byte_order (message);
2259 real->message = message;
2260 real->changed_stamp = message->changed_stamp;
2261 real->iter_type = iter_type;
2262 real->sig_refcount = 0;
2266 * Initializes a #DBusMessageIter for reading the arguments of the
2267 * message passed in.
2269 * When possible, dbus_message_get_args() is much more convenient.
2270 * Some types of argument can only be read with #DBusMessageIter
2273 * The easiest way to iterate is like this:
2275 * dbus_message_iter_init (message, &iter);
2276 * while ((current_type = dbus_message_iter_get_arg_type (&iter)) != DBUS_TYPE_INVALID)
2277 * dbus_message_iter_next (&iter);
2280 * #DBusMessageIter contains no allocated memory; it need not be
2281 * freed, and can be copied by assignment or memcpy().
2283 * @param message the message
2284 * @param iter pointer to an iterator to initialize
2285 * @returns #FALSE if the message has no arguments
2288 dbus_message_iter_init (DBusMessage *message,
2289 DBusMessageIter *iter)
2291 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2292 const DBusString *type_str;
2295 BUILD_BUG_ON (sizeof(DBusMessageIter) != sizeof(DBusMessageRealIter));
2297 _dbus_return_val_if_fail (message != NULL, FALSE);
2298 _dbus_return_val_if_fail (iter != NULL, FALSE);
2300 get_const_signature (message, &type_str, &type_pos);
2302 _dbus_message_iter_init_common (message, real,
2303 DBUS_MESSAGE_ITER_TYPE_READER);
2305 _dbus_type_reader_init (&real->u.reader,
2306 _dbus_header_get_byte_order (&message->header),
2311 if (_dbus_message_is_gvariant (message))
2312 _dbus_type_reader_gvariant_init (&real->u.reader, message);
2314 return _dbus_type_reader_get_current_type (&real->u.reader) != DBUS_TYPE_INVALID;
2318 * Checks if an iterator has any more fields.
2320 * @param iter the message iter
2321 * @returns #TRUE if there are more fields following
2324 dbus_message_iter_has_next (DBusMessageIter *iter)
2326 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2328 _dbus_return_val_if_fail (_dbus_message_iter_check (real), FALSE);
2329 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, FALSE);
2331 return _dbus_type_reader_has_next (&real->u.reader);
2335 * Moves the iterator to the next field, if any. If there's no next
2336 * field, returns #FALSE. If the iterator moves forward, returns
2339 * @param iter the message iter
2340 * @returns #TRUE if the iterator was moved to the next field
2343 dbus_message_iter_next (DBusMessageIter *iter)
2345 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2347 _dbus_return_val_if_fail (_dbus_message_iter_check (real), FALSE);
2348 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, FALSE);
2350 return _dbus_type_reader_next (&real->u.reader);
2354 * Returns the argument type of the argument that the message iterator
2355 * points to. If the iterator is at the end of the message, returns
2356 * #DBUS_TYPE_INVALID. You can thus write a loop as follows:
2359 * dbus_message_iter_init (message, &iter);
2360 * while ((current_type = dbus_message_iter_get_arg_type (&iter)) != DBUS_TYPE_INVALID)
2361 * dbus_message_iter_next (&iter);
2364 * @param iter the message iter
2365 * @returns the argument type
2368 dbus_message_iter_get_arg_type (DBusMessageIter *iter)
2370 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2372 _dbus_return_val_if_fail (_dbus_message_iter_check (real), DBUS_TYPE_INVALID);
2373 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, FALSE);
2375 return _dbus_type_reader_get_current_type (&real->u.reader);
2379 * Returns the element type of the array that the message iterator
2380 * points to. Note that you need to check that the iterator points to
2381 * an array prior to using this function.
2383 * @param iter the message iter
2384 * @returns the array element type
2387 dbus_message_iter_get_element_type (DBusMessageIter *iter)
2389 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2391 _dbus_return_val_if_fail (_dbus_message_iter_check (real), DBUS_TYPE_INVALID);
2392 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_READER, DBUS_TYPE_INVALID);
2393 _dbus_return_val_if_fail (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_ARRAY, DBUS_TYPE_INVALID);
2395 return _dbus_type_reader_get_element_type (&real->u.reader);
2399 * Recurses into a container value when reading values from a message,
2400 * initializing a sub-iterator to use for traversing the child values
2403 * Note that this recurses into a value, not a type, so you can only
2404 * recurse if the value exists. The main implication of this is that
2405 * if you have for example an empty array of array of int32, you can
2406 * recurse into the outermost array, but it will have no values, so
2407 * you won't be able to recurse further. There's no array of int32 to
2410 * If a container is an array of fixed-length types (except Unix file
2411 * descriptors), it is much more efficient to use
2412 * dbus_message_iter_get_fixed_array() to get the whole array in one
2413 * shot, rather than individually walking over the array elements.
2415 * Be sure you have somehow checked that
2416 * dbus_message_iter_get_arg_type() matches the type you are expecting
2417 * to recurse into. Results of this function are undefined if there is
2418 * no container to recurse into at the current iterator position.
2420 * @param iter the message iterator
2421 * @param sub the sub-iterator to initialize
2424 dbus_message_iter_recurse (DBusMessageIter *iter,
2425 DBusMessageIter *sub)
2427 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2428 DBusMessageRealIter *real_sub = (DBusMessageRealIter *)sub;
2430 _dbus_return_if_fail (_dbus_message_iter_check (real));
2431 _dbus_return_if_fail (sub != NULL);
2434 _dbus_type_reader_recurse (&real->u.reader, &real_sub->u.reader);
2438 * Returns the current signature of a message iterator. This
2439 * is useful primarily for dealing with variants; one can
2440 * recurse into a variant and determine the signature of
2441 * the variant's value.
2443 * The returned string must be freed with dbus_free().
2445 * @param iter the message iterator
2446 * @returns the contained signature, or NULL if out of memory
2449 dbus_message_iter_get_signature (DBusMessageIter *iter)
2451 const DBusString *sig;
2455 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2457 _dbus_return_val_if_fail (_dbus_message_iter_check (real), NULL);
2459 if (!_dbus_string_init (&retstr))
2462 _dbus_type_reader_get_signature (&real->u.reader, &sig,
2464 if (!_dbus_string_append_len (&retstr,
2465 _dbus_string_get_const_data (sig) + start,
2468 if (!_dbus_string_steal_data (&retstr, &ret))
2470 _dbus_string_free (&retstr);
2475 * Reads a basic-typed value from the message iterator.
2476 * Basic types are the non-containers such as integer and string.
2478 * The value argument should be the address of a location to store
2479 * the returned value. So for int32 it should be a "dbus_int32_t*"
2480 * and for string a "const char**". The returned value is
2481 * by reference and should not be freed.
2483 * This call duplicates Unix file descriptors when reading them. It is
2484 * your job to close them when you don't need them anymore.
2486 * Unix file descriptors that are read with this function will have
2487 * the FD_CLOEXEC flag set. If you need them without this flag set,
2488 * make sure to unset it with fcntl().
2490 * Be sure you have somehow checked that
2491 * dbus_message_iter_get_arg_type() matches the type you are
2492 * expecting, or you'll crash when you try to use an integer as a
2493 * string or something.
2495 * To read any container type (array, struct, dict) you will need to
2496 * recurse into the container with dbus_message_iter_recurse(). If
2497 * the container is an array of fixed-length values (except Unix file
2498 * descriptors), you can get all the array elements at once with
2499 * dbus_message_iter_get_fixed_array(). Otherwise, you have to iterate
2500 * over the container's contents one value at a time.
2502 * All basic-typed values are guaranteed to fit in a #DBusBasicValue,
2503 * so in versions of libdbus that have that type, you can write code like this:
2506 * DBusBasicValue value;
2508 * dbus_message_iter_get_basic (&read_iter, &value);
2509 * type = dbus_message_iter_get_arg_type (&read_iter);
2510 * dbus_message_iter_append_basic (&write_iter, type, &value);
2513 * (All D-Bus basic types are either numeric and 8 bytes or smaller, or
2514 * behave like a string; so in older versions of libdbus, DBusBasicValue
2515 * can be replaced with union { char *string; unsigned char bytes[8]; },
2518 * @param iter the iterator
2519 * @param value location to store the value
2522 dbus_message_iter_get_basic (DBusMessageIter *iter,
2525 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2527 _dbus_return_if_fail (_dbus_message_iter_check (real));
2528 _dbus_return_if_fail (value != NULL);
2530 if (dbus_message_iter_get_arg_type (iter) == DBUS_TYPE_UNIX_FD)
2532 #ifdef HAVE_UNIX_FD_PASSING
2535 _dbus_type_reader_read_basic(&real->u.reader, &idx);
2537 if (idx.u32 >= real->message->n_unix_fds) {
2538 /* Hmm, we cannot really signal an error here, so let's make
2539 sure to return an invalid fd. */
2540 *((int*) value) = -1;
2544 *((int*) value) = _dbus_dup(real->message->unix_fds[idx.u32], NULL);
2546 *((int*) value) = -1;
2551 _dbus_type_reader_read_basic (&real->u.reader,
2557 * Returns the number of elements in the array-typed value pointed
2558 * to by the iterator.
2559 * Note that this function is O(1) for arrays of fixed-size types
2560 * but O(n) for arrays of variable-length types such as strings,
2561 * so it may be a bad idea to use it.
2563 * @param iter the iterator
2564 * @returns the number of elements in the array
2567 dbus_message_iter_get_element_count (DBusMessageIter *iter)
2569 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2570 DBusTypeReader array;
2574 _dbus_return_val_if_fail (_dbus_message_iter_check (real), 0);
2575 _dbus_return_val_if_fail (_dbus_type_reader_get_current_type (&real->u.reader)
2576 == DBUS_TYPE_ARRAY, 0);
2578 element_type = _dbus_type_reader_get_element_type (&real->u.reader);
2579 _dbus_type_reader_recurse (&real->u.reader, &array);
2580 if (dbus_type_is_fixed (element_type))
2582 int alignment = _dbus_type_get_alignment (element_type);
2583 int total_len = _dbus_type_reader_get_array_length (&array);
2584 n_elements = total_len / alignment;
2588 while (_dbus_type_reader_get_current_type (&array) != DBUS_TYPE_INVALID)
2591 _dbus_type_reader_next (&array);
2599 * Returns the number of bytes in the array as marshaled in the wire
2600 * protocol. The iterator must currently be inside an array-typed
2603 * This function is deprecated on the grounds that it is stupid. Why
2604 * would you want to know how many bytes are in the array as marshaled
2605 * in the wire protocol? Use dbus_message_iter_get_element_count() instead.
2607 * @param iter the iterator
2608 * @returns the number of bytes in the array
2611 dbus_message_iter_get_array_len (DBusMessageIter *iter)
2613 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2615 _dbus_return_val_if_fail (_dbus_message_iter_check (real), 0);
2617 return _dbus_type_reader_get_array_length (&real->u.reader);
2621 * Reads a block of fixed-length values from the message iterator.
2622 * Fixed-length values are those basic types that are not string-like,
2623 * such as integers, bool, double. The returned block will be from the
2624 * current position in the array until the end of the array.
2626 * There is one exception here: although DBUS_TYPE_UNIX_FD is
2627 * considered a 'fixed' type arrays of this type may not be read with
2630 * The message iter should be "in" the array (that is, you recurse into the
2631 * array, and then you call dbus_message_iter_get_fixed_array() on the
2632 * "sub-iterator" created by dbus_message_iter_recurse()).
2634 * The value argument should be the address of a location to store the
2635 * returned array. So for int32 it should be a "const dbus_int32_t**"
2636 * The returned value is by reference and should not be freed.
2638 * This function should only be used if dbus_type_is_fixed() returns
2639 * #TRUE for the element type.
2641 * If an array's elements are not fixed in size, you have to recurse
2642 * into the array with dbus_message_iter_recurse() and read the
2643 * elements one by one.
2645 * Because the array is not copied, this function runs in constant
2646 * time and is fast; it's much preferred over walking the entire array
2647 * with an iterator. (However, you can always use
2648 * dbus_message_iter_recurse(), even for fixed-length types;
2649 * dbus_message_iter_get_fixed_array() is just an optimization.)
2651 * @param iter the iterator
2652 * @param value location to store the block
2653 * @param n_elements number of elements in the block
2656 dbus_message_iter_get_fixed_array (DBusMessageIter *iter,
2660 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2661 #ifndef DBUS_DISABLE_CHECKS
2662 int subtype = _dbus_type_reader_get_current_type(&real->u.reader);
2664 _dbus_return_if_fail (_dbus_message_iter_check (real));
2665 _dbus_return_if_fail (value != NULL);
2666 _dbus_return_if_fail ((subtype == DBUS_TYPE_INVALID) ||
2667 (dbus_type_is_fixed (subtype) && subtype != DBUS_TYPE_UNIX_FD));
2670 _dbus_type_reader_read_fixed_multi (&real->u.reader,
2675 * Initializes a #DBusMessageIter for appending arguments to the end
2678 * @todo If appending any of the arguments fails due to lack of
2679 * memory, the message is hosed and you have to start over building
2680 * the whole message.
2682 * @param message the message
2683 * @param iter pointer to an iterator to initialize
2686 dbus_message_iter_init_append (DBusMessage *message,
2687 DBusMessageIter *iter)
2689 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2691 _dbus_return_if_fail (message != NULL);
2692 _dbus_return_if_fail (iter != NULL);
2694 _dbus_message_iter_init_common (message, real,
2695 DBUS_MESSAGE_ITER_TYPE_WRITER);
2697 /* We create the signature string and point iterators at it "on demand"
2698 * when a value is actually appended. That means that init() never fails
2701 _dbus_type_writer_gvariant_init_types_delayed (
2703 _dbus_header_get_byte_order (&message->header),
2705 _dbus_string_get_length (&message->body),
2706 _dbus_message_is_gvariant (message),
2707 &message->gvariant_body_last_offset,
2708 &message->gvariant_body_last_pos);
2712 * Creates a temporary signature string containing the current
2713 * signature, stores it in the iterator, and points the iterator to
2714 * the end of it. Used any time we write to the message.
2716 * @param real an iterator without a type_str
2717 * @returns #FALSE if no memory
2720 _dbus_message_iter_open_signature (DBusMessageRealIter *real)
2723 const DBusString *current_sig;
2724 int current_sig_pos;
2726 _dbus_assert (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER);
2728 if (real->u.writer.type_str != NULL)
2730 _dbus_assert (real->sig_refcount > 0);
2731 real->sig_refcount += 1;
2735 str = dbus_new (DBusString, 1);
2739 if (!_dbus_header_get_field_raw (&real->message->header,
2740 DBUS_HEADER_FIELD_SIGNATURE,
2741 ¤t_sig, ¤t_sig_pos))
2747 int additional_size_for_len = 0;
2749 if (!real->u.writer.gvariant)
2751 current_len = _dbus_string_get_byte (current_sig, current_sig_pos);
2752 current_sig_pos += 1; /* move on to sig data */
2753 additional_size_for_len = 4;
2757 /* GVariant has no length field, simply string */
2758 current_len = strlen (_dbus_string_get_const_data (current_sig) + current_sig_pos);
2761 if (!_dbus_string_init_preallocated (str, current_len + additional_size_for_len))
2767 if (!_dbus_string_copy_len (current_sig, current_sig_pos, current_len,
2770 _dbus_string_free (str);
2777 if (!_dbus_string_init_preallocated (str, 4))
2784 real->sig_refcount = 1;
2786 /* If this assertion failed, then str would be neither stored in u.writer
2787 * nor freed by this function, resulting in a memory leak. */
2788 _dbus_assert (real->u.writer.type_str == NULL);
2789 _dbus_type_writer_add_types (&real->u.writer,
2790 str, _dbus_string_get_length (str));
2795 * Sets the new signature as the message signature, frees the
2796 * signature string, and marks the iterator as not having a type_str
2797 * anymore. Frees the signature even if it fails, so you can't
2798 * really recover from failure. Kinda busted.
2800 * @param real an iterator without a type_str
2801 * @returns #FALSE if no memory
2804 _dbus_message_iter_close_signature (DBusMessageRealIter *real)
2807 const char *v_STRING;
2810 _dbus_assert (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER);
2811 _dbus_assert (real->u.writer.type_str != NULL);
2812 _dbus_assert (real->sig_refcount > 0);
2814 real->sig_refcount -= 1;
2816 if (real->sig_refcount > 0)
2818 _dbus_assert (real->sig_refcount == 0);
2822 str = real->u.writer.type_str;
2824 v_STRING = _dbus_string_get_const_data (str);
2825 if (!_dbus_header_set_field_basic (&real->message->header,
2826 DBUS_HEADER_FIELD_SIGNATURE,
2827 DBUS_TYPE_SIGNATURE,
2831 _dbus_type_writer_remove_types (&real->u.writer);
2832 _dbus_string_free (str);
2839 * Frees the signature string and marks the iterator as not having a
2840 * type_str anymore. Since the new signature is not set, the message
2841 * will generally be hosed after this is called.
2843 * @param real an iterator without a type_str
2846 _dbus_message_iter_abandon_signature (DBusMessageRealIter *real)
2850 _dbus_assert (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER);
2851 _dbus_assert (real->u.writer.type_str != NULL);
2852 _dbus_assert (real->sig_refcount > 0);
2854 real->sig_refcount -= 1;
2856 if (real->sig_refcount > 0)
2858 _dbus_assert (real->sig_refcount == 0);
2860 str = real->u.writer.type_str;
2862 _dbus_type_writer_remove_types (&real->u.writer);
2863 _dbus_string_free (str);
2867 #ifndef DBUS_DISABLE_CHECKS
2869 _dbus_message_iter_append_check (DBusMessageRealIter *iter)
2871 if (!_dbus_message_iter_check (iter))
2874 if (iter->message->locked)
2876 _dbus_warn_check_failed ("dbus append iterator can't be used: message is locked (has already been sent)");
2882 #endif /* DBUS_DISABLE_CHECKS */
2884 #ifdef HAVE_UNIX_FD_PASSING
2886 expand_fd_array(DBusMessage *m,
2891 /* This makes space for adding n new fds to the array and returns a
2892 pointer to the place were the first fd should be put. */
2894 if (m->n_unix_fds + n > m->n_unix_fds_allocated)
2899 /* Make twice as much space as necessary */
2900 k = (m->n_unix_fds + n) * 2;
2902 /* Allocate at least four */
2906 p = dbus_realloc(m->unix_fds, k * sizeof(int));
2911 m->n_unix_fds_allocated = k;
2914 return m->unix_fds + m->n_unix_fds;
2919 * Appends a basic-typed value to the message. The basic types are the
2920 * non-container types such as integer and string.
2922 * The "value" argument should be the address of a basic-typed value.
2923 * So for string, const char**. For integer, dbus_int32_t*.
2925 * For Unix file descriptors this function will internally duplicate
2926 * the descriptor you passed in. Hence you may close the descriptor
2927 * immediately after this call.
2929 * @todo If this fails due to lack of memory, the message is hosed and
2930 * you have to start over building the whole message.
2932 * @param iter the append iterator
2933 * @param type the type of the value
2934 * @param value the address of the value
2935 * @returns #FALSE if not enough memory
2938 dbus_message_iter_append_basic (DBusMessageIter *iter,
2942 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
2945 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
2946 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
2947 _dbus_return_val_if_fail (dbus_type_is_basic (type), FALSE);
2948 _dbus_return_val_if_fail (value != NULL, FALSE);
2950 #ifndef DBUS_DISABLE_CHECKS
2954 DBusValidity signature_validity;
2955 const char * const *string_p;
2956 const dbus_bool_t *bool_p;
2958 case DBUS_TYPE_STRING:
2960 _dbus_return_val_if_fail (_dbus_check_is_valid_utf8 (*string_p), FALSE);
2963 case DBUS_TYPE_OBJECT_PATH:
2965 _dbus_return_val_if_fail (_dbus_check_is_valid_path (*string_p), FALSE);
2968 case DBUS_TYPE_SIGNATURE:
2970 _dbus_string_init_const (&str, *string_p);
2971 signature_validity = _dbus_validate_signature_with_reason (&str,
2973 _dbus_string_get_length (&str));
2975 if (signature_validity == DBUS_VALIDITY_UNKNOWN_OOM_ERROR)
2978 _dbus_return_val_if_fail (signature_validity == DBUS_VALID, FALSE);
2981 case DBUS_TYPE_BOOLEAN:
2983 _dbus_return_val_if_fail (*bool_p == 0 || *bool_p == 1, FALSE);
2988 /* nothing to check, all possible values are allowed */
2993 if (!_dbus_message_iter_open_signature (real))
2996 if (type == DBUS_TYPE_UNIX_FD)
2998 #ifdef HAVE_UNIX_FD_PASSING
3004 /* First step, include the fd in the fd list of this message */
3005 if (!(fds = expand_fd_array(real->message, 1)))
3008 *fds = _dbus_dup(*(int*) value, NULL);
3012 u = real->message->n_unix_fds;
3014 /* Second step, write the index to the fd */
3015 if (!(ret = _dbus_type_writer_write_basic (&real->u.writer, DBUS_TYPE_UNIX_FD, &u))) {
3016 _dbus_close(*fds, NULL);
3020 real->message->n_unix_fds += 1;
3023 /* Final step, update the header accordingly */
3024 ret = _dbus_header_set_field_basic (&real->message->header,
3025 DBUS_HEADER_FIELD_UNIX_FDS,
3029 /* If any of these operations fail the message is
3030 hosed. However, no memory or fds should be leaked since what
3031 has been added to message has been added to the message, and
3032 can hence be accounted for when the message is being
3036 /* This is redundant (we could just fall through), but it avoids
3037 * -Wunused-label in builds that don't HAVE_UNIX_FD_PASSING */
3043 ret = _dbus_type_writer_write_basic (&real->u.writer, type, value);
3047 if (!_dbus_message_iter_close_signature (real))
3054 * Appends a block of fixed-length values to an array. The
3055 * fixed-length types are all basic types that are not string-like. So
3056 * int32, double, bool, etc. (Unix file descriptors however are not
3057 * supported.) You must call dbus_message_iter_open_container() to
3058 * open an array of values before calling this function. You may call
3059 * this function multiple times (and intermixed with calls to
3060 * dbus_message_iter_append_basic()) for the same array.
3062 * The "value" argument should be the address of the array. So for
3063 * integer, "dbus_int32_t**" is expected for example.
3065 * @warning in C, given "int array[]", "&array == array" (the
3066 * comp.lang.c FAQ says otherwise, but gcc and the FAQ don't agree).
3067 * So if you're using an array instead of a pointer you have to create
3068 * a pointer variable, assign the array to it, then take the address
3069 * of the pointer variable.
3071 * const dbus_int32_t array[] = { 1, 2, 3 };
3072 * const dbus_int32_t *v_ARRAY = array;
3073 * if (!dbus_message_iter_append_fixed_array (&iter, DBUS_TYPE_INT32, &v_ARRAY, 3))
3074 * fprintf (stderr, "No memory!\n");
3076 * For strings it works to write const char *array = "Hello" and then
3077 * use &array though.
3079 * @todo If this fails due to lack of memory, the message is hosed and
3080 * you have to start over building the whole message.
3082 * @param iter the append iterator
3083 * @param element_type the type of the array elements
3084 * @param value the address of the array
3085 * @param n_elements the number of elements to append
3086 * @returns #FALSE if not enough memory
3089 dbus_message_iter_append_fixed_array (DBusMessageIter *iter,
3094 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3097 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
3098 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
3099 _dbus_return_val_if_fail (dbus_type_is_fixed (element_type) && element_type != DBUS_TYPE_UNIX_FD, FALSE);
3100 _dbus_return_val_if_fail (real->u.writer.container_type == DBUS_TYPE_ARRAY, FALSE);
3101 _dbus_return_val_if_fail (value != NULL, FALSE);
3102 _dbus_return_val_if_fail (n_elements >= 0, FALSE);
3103 _dbus_return_val_if_fail (n_elements <=
3104 DBUS_MAXIMUM_ARRAY_LENGTH / _dbus_type_get_alignment (element_type),
3107 #ifndef DBUS_DISABLE_CHECKS
3108 if (element_type == DBUS_TYPE_BOOLEAN)
3110 const dbus_bool_t * const *bools = value;
3113 for (i = 0; i < n_elements; i++)
3115 _dbus_return_val_if_fail ((*bools)[i] == 0 || (*bools)[i] == 1, FALSE);
3120 ret = _dbus_type_writer_write_fixed_multi (&real->u.writer, element_type, value, n_elements);
3126 * Appends a container-typed value to the message. On success, you are
3127 * required to append the contents of the container using the returned
3128 * sub-iterator, and then call
3129 * dbus_message_iter_close_container(). Container types are for
3130 * example struct, variant, and array. For variants, the
3131 * contained_signature should be the type of the single value inside
3132 * the variant. For structs and dict entries, contained_signature
3133 * should be #NULL; it will be set to whatever types you write into
3134 * the struct. For arrays, contained_signature should be the type of
3135 * the array elements.
3137 * @todo If this fails due to lack of memory, the message is hosed and
3138 * you have to start over building the whole message.
3140 * If this function fails, the sub-iterator remains invalid, and must
3141 * not be closed with dbus_message_iter_close_container() or abandoned
3142 * with dbus_message_iter_abandon_container(). However, after this
3143 * function has either succeeded or failed, it is valid to call
3144 * dbus_message_iter_abandon_container_if_open().
3146 * @param iter the append iterator
3147 * @param type the type of the value
3148 * @param contained_signature the type of container contents
3149 * @param sub sub-iterator to initialize
3150 * @returns #FALSE if not enough memory
3153 dbus_message_iter_open_container (DBusMessageIter *iter,
3155 const char *contained_signature,
3156 DBusMessageIter *sub)
3158 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3159 DBusMessageRealIter *real_sub = (DBusMessageRealIter *)sub;
3160 DBusString contained_str;
3161 DBusValidity contained_signature_validity;
3164 _dbus_return_val_if_fail (sub != NULL, FALSE);
3165 /* Do our best to make sure the sub-iterator doesn't contain something
3166 * valid-looking on failure */
3167 _dbus_message_real_iter_zero (real_sub);
3169 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
3170 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
3171 _dbus_return_val_if_fail (dbus_type_is_container (type), FALSE);
3172 _dbus_return_val_if_fail ((type == DBUS_TYPE_STRUCT &&
3173 contained_signature == NULL) ||
3174 (type == DBUS_TYPE_DICT_ENTRY &&
3175 contained_signature == NULL) ||
3176 (type == DBUS_TYPE_VARIANT &&
3177 contained_signature != NULL) ||
3178 (type == DBUS_TYPE_ARRAY &&
3179 contained_signature != NULL), FALSE);
3181 /* this would fail if the contained_signature is a dict entry, since
3182 * dict entries are invalid signatures standalone (they must be in
3185 if (contained_signature != NULL)
3187 _dbus_string_init_const (&contained_str, contained_signature);
3188 contained_signature_validity = _dbus_validate_signature_with_reason (&contained_str,
3190 _dbus_string_get_length (&contained_str));
3192 if (contained_signature_validity == DBUS_VALIDITY_UNKNOWN_OOM_ERROR)
3197 /* just some placeholder value */
3198 contained_signature_validity = DBUS_VALID_BUT_INCOMPLETE;
3201 _dbus_return_val_if_fail ((type == DBUS_TYPE_ARRAY && contained_signature && *contained_signature == DBUS_DICT_ENTRY_BEGIN_CHAR) ||
3202 contained_signature == NULL ||
3203 contained_signature_validity == DBUS_VALID,
3206 if (!_dbus_message_iter_open_signature (real))
3212 if (contained_signature != NULL)
3214 _dbus_string_init_const (&contained_str, contained_signature);
3216 ret = _dbus_type_writer_recurse (&real->u.writer,
3219 &real_sub->u.writer);
3223 ret = _dbus_type_writer_recurse (&real->u.writer,
3226 &real_sub->u.writer);
3230 _dbus_message_iter_abandon_signature (real);
3237 * Closes a container-typed value appended to the message; may write
3238 * out more information to the message known only after the entire
3239 * container is written, and may free resources created by
3240 * dbus_message_iter_open_container().
3242 * Even if this function fails due to lack of memory, the sub-iterator sub
3243 * has been closed and invalidated. It must not be closed again with this
3244 * function, or abandoned with dbus_message_iter_abandon_container().
3245 * However, it remains valid to call
3246 * dbus_message_iter_abandon_container_if_open().
3248 * @todo If this fails due to lack of memory, the message is hosed and
3249 * you have to start over building the whole message.
3251 * @param iter the append iterator
3252 * @param sub sub-iterator to close
3253 * @returns #FALSE if not enough memory
3256 dbus_message_iter_close_container (DBusMessageIter *iter,
3257 DBusMessageIter *sub)
3259 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3260 DBusMessageRealIter *real_sub = (DBusMessageRealIter *)sub;
3263 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real), FALSE);
3264 _dbus_return_val_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
3265 _dbus_return_val_if_fail (_dbus_message_iter_append_check (real_sub), FALSE);
3266 _dbus_return_val_if_fail (real_sub->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER, FALSE);
3268 ret = _dbus_type_writer_unrecurse (&real->u.writer,
3269 &real_sub->u.writer);
3270 _dbus_message_real_iter_zero (real_sub);
3272 if (!_dbus_message_iter_close_signature (real))
3279 * Abandons creation of a contained-typed value and frees resources created
3280 * by dbus_message_iter_open_container(). Once this returns, the message
3281 * is hosed and you have to start over building the whole message.
3283 * This should only be used to abandon creation of a message when you have
3286 * @param iter the append iterator
3287 * @param sub sub-iterator to close
3290 dbus_message_iter_abandon_container (DBusMessageIter *iter,
3291 DBusMessageIter *sub)
3293 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3294 DBusMessageRealIter *real_sub = (DBusMessageRealIter *)sub;
3296 #ifndef DBUS_DISABLE_CHECKS
3297 _dbus_return_if_fail (_dbus_message_iter_append_check (real));
3298 _dbus_return_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER);
3299 _dbus_return_if_fail (_dbus_message_iter_append_check (real_sub));
3300 _dbus_return_if_fail (real_sub->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER);
3303 _dbus_message_iter_abandon_signature (real);
3304 _dbus_message_real_iter_zero (real_sub);
3308 * Abandons creation of a contained-typed value and frees resources created
3309 * by dbus_message_iter_open_container(). Once this returns, the message
3310 * is hosed and you have to start over building the whole message.
3312 * Unlike dbus_message_iter_abandon_container(), it is valid to call this
3313 * function on an iterator that was initialized with
3314 * #DBUS_MESSAGE_ITER_INIT_CLOSED, or an iterator that was already closed
3315 * or abandoned. However, it is not valid to call this function on
3316 * uninitialized memory. This is intended to be used in error cleanup
3317 * code paths, similar to this pattern:
3319 * DBusMessageIter outer = DBUS_MESSAGE_ITER_INIT_CLOSED;
3320 * DBusMessageIter inner = DBUS_MESSAGE_ITER_INIT_CLOSED;
3321 * dbus_bool_t result = FALSE;
3323 * if (!dbus_message_iter_open_container (iter, ..., &outer))
3326 * if (!dbus_message_iter_open_container (&outer, ..., &inner))
3329 * if (!dbus_message_iter_append_basic (&inner, ...))
3332 * if (!dbus_message_iter_close_container (&outer, ..., &inner))
3335 * if (!dbus_message_iter_close_container (iter, ..., &outer))
3341 * dbus_message_iter_abandon_container_if_open (&outer, &inner);
3342 * dbus_message_iter_abandon_container_if_open (iter, &outer);
3345 * @param iter the append iterator
3346 * @param sub sub-iterator to close
3349 dbus_message_iter_abandon_container_if_open (DBusMessageIter *iter,
3350 DBusMessageIter *sub)
3352 DBusMessageRealIter *real = (DBusMessageRealIter *)iter;
3353 DBusMessageRealIter *real_sub = (DBusMessageRealIter *)sub;
3355 /* If both the parent and the child are zeroed out, then either we didn't
3356 * even get as far as successfully recursing into the parent, or we already
3357 * closed both the child and the parent. For example, in the code sample
3358 * in the doc-comment above, this happens for
3359 * abandon_container_if_open (&outer, &inner) if the first open_container
3360 * call failed, or if we reached result = TRUE and fell through. */
3361 if (_dbus_message_real_iter_is_zeroed (real) &&
3362 _dbus_message_real_iter_is_zeroed (real_sub))
3365 #ifndef DBUS_DISABLE_CHECKS
3366 /* If the child is not zeroed out, but the parent is, then something has
3367 * gone horribly wrong (in practice that would probably mean both are
3368 * uninitialized or corrupt, and the parent happens to have ended up
3369 * all-bytes-zero). */
3370 _dbus_return_if_fail (_dbus_message_iter_append_check (real));
3371 _dbus_return_if_fail (real->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER);
3374 /* If the parent is not zeroed out, but the child is, then either we did
3375 * not successfully open the child, or we already closed the child. This
3376 * means we do not own a reference to the parent's signature, so it would
3377 * be wrong to release it; so we must not call abandon_signature() here.
3378 * In the code sample in the doc-comment above, this happens for
3379 * abandon_container_if_open (&outer, &inner) if the second open_container
3380 * call failed, or if the second close_container call failed. */
3381 if (_dbus_message_real_iter_is_zeroed (real_sub))
3384 #ifndef DBUS_DISABLE_CHECKS
3385 _dbus_return_if_fail (_dbus_message_iter_append_check (real_sub));
3386 _dbus_return_if_fail (real_sub->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER);
3389 /* If neither the parent nor the child is zeroed out, then we genuinely
3390 * have an open container; close it. In the code sample in the doc-comment,
3391 * this happens for abandon_container_if_open (&outer, &inner) if the
3392 * append_basic call failed. */
3393 _dbus_message_iter_abandon_signature (real);
3394 _dbus_message_real_iter_zero (real_sub);
3398 * Sets a flag indicating that the message does not want a reply; if
3399 * this flag is set, the other end of the connection may (but is not
3400 * required to) optimize by not sending method return or error
3401 * replies. If this flag is set, there is no way to know whether the
3402 * message successfully arrived at the remote end. Normally you know a
3403 * message was received when you receive the reply to it.
3405 * The flag is #FALSE by default, that is by default the other end is
3406 * required to reply.
3408 * On the protocol level this toggles #DBUS_HEADER_FLAG_NO_REPLY_EXPECTED
3410 * @param message the message
3411 * @param no_reply #TRUE if no reply is desired
3414 dbus_message_set_no_reply (DBusMessage *message,
3415 dbus_bool_t no_reply)
3417 _dbus_return_if_fail (message != NULL);
3418 _dbus_return_if_fail (!message->locked);
3420 _dbus_header_toggle_flag (&message->header,
3421 DBUS_HEADER_FLAG_NO_REPLY_EXPECTED,
3426 * Returns #TRUE if the message does not expect
3429 * @param message the message
3430 * @returns #TRUE if the message sender isn't waiting for a reply
3433 dbus_message_get_no_reply (DBusMessage *message)
3435 _dbus_return_val_if_fail (message != NULL, FALSE);
3437 return _dbus_header_get_flag (&message->header,
3438 DBUS_HEADER_FLAG_NO_REPLY_EXPECTED);
3442 * Sets a flag indicating that an owner for the destination name will
3443 * be automatically started before the message is delivered. When this
3444 * flag is set, the message is held until a name owner finishes
3445 * starting up, or fails to start up. In case of failure, the reply
3448 * The flag is set to #TRUE by default, i.e. auto starting is the default.
3450 * On the protocol level this toggles #DBUS_HEADER_FLAG_NO_AUTO_START
3452 * @param message the message
3453 * @param auto_start #TRUE if auto-starting is desired
3456 dbus_message_set_auto_start (DBusMessage *message,
3457 dbus_bool_t auto_start)
3459 _dbus_return_if_fail (message != NULL);
3460 _dbus_return_if_fail (!message->locked);
3462 _dbus_header_toggle_flag (&message->header,
3463 DBUS_HEADER_FLAG_NO_AUTO_START,
3468 * Returns #TRUE if the message will cause an owner for
3469 * destination name to be auto-started.
3471 * @param message the message
3472 * @returns #TRUE if the message will use auto-start
3475 dbus_message_get_auto_start (DBusMessage *message)
3477 _dbus_return_val_if_fail (message != NULL, FALSE);
3479 return !_dbus_header_get_flag (&message->header,
3480 DBUS_HEADER_FLAG_NO_AUTO_START);
3485 * Sets the object path this message is being sent to (for
3486 * DBUS_MESSAGE_TYPE_METHOD_CALL) or the one a signal is being
3487 * emitted from (for DBUS_MESSAGE_TYPE_SIGNAL).
3489 * The path must contain only valid characters as defined
3490 * in the D-Bus specification.
3492 * @param message the message
3493 * @param object_path the path or #NULL to unset
3494 * @returns #FALSE if not enough memory
3497 dbus_message_set_path (DBusMessage *message,
3498 const char *object_path)
3500 _dbus_return_val_if_fail (message != NULL, FALSE);
3501 _dbus_return_val_if_fail (!message->locked, FALSE);
3502 _dbus_return_val_if_fail (object_path == NULL ||
3503 _dbus_check_is_valid_path (object_path),
3506 return set_or_delete_string_field (message,
3507 DBUS_HEADER_FIELD_PATH,
3508 DBUS_TYPE_OBJECT_PATH,
3513 * Gets the object path this message is being sent to (for
3514 * DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted from (for
3515 * DBUS_MESSAGE_TYPE_SIGNAL). Returns #NULL if none.
3517 * See also dbus_message_get_path_decomposed().
3519 * The returned string becomes invalid if the message is
3520 * modified, since it points into the wire-marshaled message data.
3522 * @param message the message
3523 * @returns the path (should not be freed) or #NULL
3526 dbus_message_get_path (DBusMessage *message)
3530 _dbus_return_val_if_fail (message != NULL, NULL);
3532 v = NULL; /* in case field doesn't exist */
3533 _dbus_header_get_field_basic (&message->header,
3534 DBUS_HEADER_FIELD_PATH,
3535 DBUS_TYPE_OBJECT_PATH,
3541 * Checks if the message has a particular object path. The object
3542 * path is the destination object for a method call or the emitting
3543 * object for a signal.
3545 * @param message the message
3546 * @param path the path name
3547 * @returns #TRUE if there is a path field in the header
3550 dbus_message_has_path (DBusMessage *message,
3553 const char *msg_path;
3554 msg_path = dbus_message_get_path (message);
3556 if (msg_path == NULL)
3567 if (strcmp (msg_path, path) == 0)
3574 * Gets the object path this message is being sent to
3575 * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted
3576 * from (for DBUS_MESSAGE_TYPE_SIGNAL) in a decomposed
3577 * format (one array element per path component).
3578 * Free the returned array with dbus_free_string_array().
3580 * An empty but non-NULL path array means the path "/".
3581 * So the path "/foo/bar" becomes { "foo", "bar", NULL }
3582 * and the path "/" becomes { NULL }.
3584 * See also dbus_message_get_path().
3586 * @todo this could be optimized by using the len from the message
3587 * instead of calling strlen() again
3589 * @param message the message
3590 * @param path place to store allocated array of path components; #NULL set here if no path field exists
3591 * @returns #FALSE if no memory to allocate the array
3594 dbus_message_get_path_decomposed (DBusMessage *message,
3599 _dbus_return_val_if_fail (message != NULL, FALSE);
3600 _dbus_return_val_if_fail (path != NULL, FALSE);
3604 v = dbus_message_get_path (message);
3607 if (!_dbus_decompose_path (v, strlen (v),
3615 * Sets the interface this message is being sent to
3616 * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or
3617 * the interface a signal is being emitted from
3618 * (for DBUS_MESSAGE_TYPE_SIGNAL).
3620 * The interface name must contain only valid characters as defined
3621 * in the D-Bus specification.
3623 * @param message the message
3624 * @param iface the interface or #NULL to unset
3625 * @returns #FALSE if not enough memory
3628 dbus_message_set_interface (DBusMessage *message,
3631 _dbus_return_val_if_fail (message != NULL, FALSE);
3632 _dbus_return_val_if_fail (!message->locked, FALSE);
3633 _dbus_return_val_if_fail (iface == NULL ||
3634 _dbus_check_is_valid_interface (iface),
3637 return set_or_delete_string_field (message,
3638 DBUS_HEADER_FIELD_INTERFACE,
3644 * Gets the interface this message is being sent to
3645 * (for DBUS_MESSAGE_TYPE_METHOD_CALL) or being emitted
3646 * from (for DBUS_MESSAGE_TYPE_SIGNAL).
3647 * The interface name is fully-qualified (namespaced).
3648 * Returns #NULL if none.
3650 * The returned string becomes invalid if the message is
3651 * modified, since it points into the wire-marshaled message data.
3653 * @param message the message
3654 * @returns the message interface (should not be freed) or #NULL
3657 dbus_message_get_interface (DBusMessage *message)
3661 _dbus_return_val_if_fail (message != NULL, NULL);
3663 v = NULL; /* in case field doesn't exist */
3664 _dbus_header_get_field_basic (&message->header,
3665 DBUS_HEADER_FIELD_INTERFACE,
3672 * Checks if the message has an interface
3674 * @param message the message
3675 * @param iface the interface name
3676 * @returns #TRUE if the interface field in the header matches
3679 dbus_message_has_interface (DBusMessage *message,
3682 const char *msg_interface;
3683 msg_interface = dbus_message_get_interface (message);
3685 if (msg_interface == NULL)
3696 if (strcmp (msg_interface, iface) == 0)
3704 * Sets the interface member being invoked
3705 * (DBUS_MESSAGE_TYPE_METHOD_CALL) or emitted
3706 * (DBUS_MESSAGE_TYPE_SIGNAL).
3708 * The member name must contain only valid characters as defined
3709 * in the D-Bus specification.
3711 * @param message the message
3712 * @param member the member or #NULL to unset
3713 * @returns #FALSE if not enough memory
3716 dbus_message_set_member (DBusMessage *message,
3719 _dbus_return_val_if_fail (message != NULL, FALSE);
3720 _dbus_return_val_if_fail (!message->locked, FALSE);
3721 _dbus_return_val_if_fail (member == NULL ||
3722 _dbus_check_is_valid_member (member),
3725 return set_or_delete_string_field (message,
3726 DBUS_HEADER_FIELD_MEMBER,
3732 * Gets the interface member being invoked
3733 * (DBUS_MESSAGE_TYPE_METHOD_CALL) or emitted
3734 * (DBUS_MESSAGE_TYPE_SIGNAL). Returns #NULL if none.
3736 * The returned string becomes invalid if the message is
3737 * modified, since it points into the wire-marshaled message data.
3739 * @param message the message
3740 * @returns the member name (should not be freed) or #NULL
3743 dbus_message_get_member (DBusMessage *message)
3747 _dbus_return_val_if_fail (message != NULL, NULL);
3749 v = NULL; /* in case field doesn't exist */
3750 _dbus_header_get_field_basic (&message->header,
3751 DBUS_HEADER_FIELD_MEMBER,
3758 * Checks if the message has an interface member
3760 * @param message the message
3761 * @param member the member name
3762 * @returns #TRUE if there is a member field in the header
3765 dbus_message_has_member (DBusMessage *message,
3768 const char *msg_member;
3769 msg_member = dbus_message_get_member (message);
3771 if (msg_member == NULL)
3782 if (strcmp (msg_member, member) == 0)
3790 * Sets the name of the error (DBUS_MESSAGE_TYPE_ERROR).
3791 * The name is fully-qualified (namespaced).
3793 * The error name must contain only valid characters as defined
3794 * in the D-Bus specification.
3796 * @param message the message
3797 * @param error_name the name or #NULL to unset
3798 * @returns #FALSE if not enough memory
3801 dbus_message_set_error_name (DBusMessage *message,
3802 const char *error_name)
3804 _dbus_return_val_if_fail (message != NULL, FALSE);
3805 _dbus_return_val_if_fail (!message->locked, FALSE);
3806 _dbus_return_val_if_fail (error_name == NULL ||
3807 _dbus_check_is_valid_error_name (error_name),
3810 return set_or_delete_string_field (message,
3811 DBUS_HEADER_FIELD_ERROR_NAME,
3817 * Gets the error name (DBUS_MESSAGE_TYPE_ERROR only)
3820 * The returned string becomes invalid if the message is
3821 * modified, since it points into the wire-marshaled message data.
3823 * @param message the message
3824 * @returns the error name (should not be freed) or #NULL
3827 dbus_message_get_error_name (DBusMessage *message)
3831 _dbus_return_val_if_fail (message != NULL, NULL);
3833 v = NULL; /* in case field doesn't exist */
3834 _dbus_header_get_field_basic (&message->header,
3835 DBUS_HEADER_FIELD_ERROR_NAME,
3842 * Sets the message's destination. The destination is the name of
3843 * another connection on the bus and may be either the unique name
3844 * assigned by the bus to each connection, or a well-known name
3845 * specified in advance.
3847 * The destination name must contain only valid characters as defined
3848 * in the D-Bus specification.
3850 * @param message the message
3851 * @param destination the destination name or #NULL to unset
3852 * @returns #FALSE if not enough memory
3855 dbus_message_set_destination (DBusMessage *message,
3856 const char *destination)
3858 _dbus_return_val_if_fail (message != NULL, FALSE);
3859 _dbus_return_val_if_fail (!message->locked, FALSE);
3860 _dbus_return_val_if_fail (destination == NULL ||
3861 _dbus_check_is_valid_bus_name (destination),
3864 return set_or_delete_string_field (message,
3865 DBUS_HEADER_FIELD_DESTINATION,
3871 * Gets the destination of a message or #NULL if there is none set.
3873 * The returned string becomes invalid if the message is
3874 * modified, since it points into the wire-marshaled message data.
3876 * @param message the message
3877 * @returns the message destination (should not be freed) or #NULL
3880 dbus_message_get_destination (DBusMessage *message)
3884 _dbus_return_val_if_fail (message != NULL, NULL);
3886 v = NULL; /* in case field doesn't exist */
3887 _dbus_header_get_field_basic (&message->header,
3888 DBUS_HEADER_FIELD_DESTINATION,
3895 * Sets the message sender.
3897 * The sender must be a valid bus name as defined in the D-Bus
3900 * Usually you don't want to call this. The message bus daemon will
3901 * call it to set the origin of each message. If you aren't implementing
3902 * a message bus daemon you shouldn't need to set the sender.
3904 * @param message the message
3905 * @param sender the sender or #NULL to unset
3906 * @returns #FALSE if not enough memory
3909 dbus_message_set_sender (DBusMessage *message,
3912 _dbus_return_val_if_fail (message != NULL, FALSE);
3913 _dbus_return_val_if_fail (!message->locked, FALSE);
3914 _dbus_return_val_if_fail (sender == NULL ||
3915 _dbus_check_is_valid_bus_name (sender),
3918 return set_or_delete_string_field (message,
3919 DBUS_HEADER_FIELD_SENDER,
3925 * Gets the unique name of the connection which originated this
3926 * message, or #NULL if unknown or inapplicable. The sender is filled
3927 * in by the message bus.
3929 * Note, the returned sender is always the unique bus name.
3930 * Connections may own multiple other bus names, but those
3931 * are not found in the sender field.
3933 * The returned string becomes invalid if the message is
3934 * modified, since it points into the wire-marshaled message data.
3936 * @param message the message
3937 * @returns the unique name of the sender or #NULL
3940 dbus_message_get_sender (DBusMessage *message)
3944 _dbus_return_val_if_fail (message != NULL, NULL);
3946 if (NULL != message->unique_sender)
3947 return _dbus_string_get_const_data (message->unique_sender);
3949 v = NULL; /* in case field doesn't exist */
3950 _dbus_header_get_field_basic (&message->header,
3951 DBUS_HEADER_FIELD_SENDER,
3958 * Gets the type signature of the message, i.e. the arguments in the
3959 * message payload. The signature includes only "in" arguments for
3960 * #DBUS_MESSAGE_TYPE_METHOD_CALL and only "out" arguments for
3961 * #DBUS_MESSAGE_TYPE_METHOD_RETURN, so is slightly different from
3962 * what you might expect (that is, it does not include the signature of the
3963 * entire C++-style method).
3965 * The signature is a string made up of type codes such as
3966 * #DBUS_TYPE_INT32. The string is terminated with nul (nul is also
3967 * the value of #DBUS_TYPE_INVALID).
3969 * The returned string becomes invalid if the message is
3970 * modified, since it points into the wire-marshaled message data.
3972 * @param message the message
3973 * @returns the type signature
3976 dbus_message_get_signature (DBusMessage *message)
3978 const DBusString *type_str;
3981 _dbus_return_val_if_fail (message != NULL, NULL);
3983 get_const_signature (message, &type_str, &type_pos);
3985 return _dbus_string_get_const_data_len (type_str, type_pos, 0);
3989 _dbus_message_has_type_interface_member (DBusMessage *message,
3996 _dbus_assert (message != NULL);
3997 _dbus_assert (iface != NULL);
3998 _dbus_assert (member != NULL);
4000 if (dbus_message_get_type (message) != type)
4003 /* Optimize by checking the short member name first
4004 * instead of the longer interface name
4007 n = dbus_message_get_member (message);
4009 if (n && strcmp (n, member) == 0)
4011 n = dbus_message_get_interface (message);
4013 if (n == NULL || strcmp (n, iface) == 0)
4021 * Checks whether the message is a method call with the given
4022 * interface and member fields. If the message is not
4023 * #DBUS_MESSAGE_TYPE_METHOD_CALL, or has a different interface or
4024 * member field, returns #FALSE. If the interface field is missing,
4025 * then it will be assumed equal to the provided interface. The D-Bus
4026 * protocol allows method callers to leave out the interface name.
4028 * @param message the message
4029 * @param iface the name to check (must not be #NULL)
4030 * @param method the name to check (must not be #NULL)
4032 * @returns #TRUE if the message is the specified method call
4035 dbus_message_is_method_call (DBusMessage *message,
4039 _dbus_return_val_if_fail (message != NULL, FALSE);
4040 _dbus_return_val_if_fail (iface != NULL, FALSE);
4041 _dbus_return_val_if_fail (method != NULL, FALSE);
4042 /* don't check that interface/method are valid since it would be
4043 * expensive, and not catch many common errors
4046 return _dbus_message_has_type_interface_member (message,
4047 DBUS_MESSAGE_TYPE_METHOD_CALL,
4052 * Checks whether the message is a signal with the given interface and
4053 * member fields. If the message is not #DBUS_MESSAGE_TYPE_SIGNAL, or
4054 * has a different interface or member field, returns #FALSE.
4056 * @param message the message
4057 * @param iface the name to check (must not be #NULL)
4058 * @param signal_name the name to check (must not be #NULL)
4060 * @returns #TRUE if the message is the specified signal
4063 dbus_message_is_signal (DBusMessage *message,
4065 const char *signal_name)
4067 _dbus_return_val_if_fail (message != NULL, FALSE);
4068 _dbus_return_val_if_fail (iface != NULL, FALSE);
4069 _dbus_return_val_if_fail (signal_name != NULL, FALSE);
4070 /* don't check that interface/name are valid since it would be
4071 * expensive, and not catch many common errors
4074 return _dbus_message_has_type_interface_member (message,
4075 DBUS_MESSAGE_TYPE_SIGNAL,
4076 iface, signal_name);
4080 * Checks whether the message is an error reply with the given error
4081 * name. If the message is not #DBUS_MESSAGE_TYPE_ERROR, or has a
4082 * different name, returns #FALSE.
4084 * @param message the message
4085 * @param error_name the name to check (must not be #NULL)
4087 * @returns #TRUE if the message is the specified error
4090 dbus_message_is_error (DBusMessage *message,
4091 const char *error_name)
4095 _dbus_return_val_if_fail (message != NULL, FALSE);
4096 _dbus_return_val_if_fail (error_name != NULL, FALSE);
4097 /* don't check that error_name is valid since it would be expensive,
4098 * and not catch many common errors
4101 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
4104 n = dbus_message_get_error_name (message);
4106 if (n && strcmp (n, error_name) == 0)
4113 * Checks whether the message was sent to the given name. If the
4114 * message has no destination specified or has a different
4115 * destination, returns #FALSE.
4117 * @param message the message
4118 * @param name the name to check (must not be #NULL)
4120 * @returns #TRUE if the message has the given destination name
4123 dbus_message_has_destination (DBusMessage *message,
4128 _dbus_return_val_if_fail (message != NULL, FALSE);
4129 _dbus_return_val_if_fail (name != NULL, FALSE);
4130 /* don't check that name is valid since it would be expensive, and
4131 * not catch many common errors
4134 s = dbus_message_get_destination (message);
4136 if (s && strcmp (s, name) == 0)
4143 * Checks whether the message has the given unique name as its sender.
4144 * If the message has no sender specified or has a different sender,
4145 * returns #FALSE. Note that a peer application will always have the
4146 * unique name of the connection as the sender. So you can't use this
4147 * function to see whether a sender owned a well-known name.
4149 * Messages from the bus itself will have #DBUS_SERVICE_DBUS
4152 * @param message the message
4153 * @param name the name to check (must not be #NULL)
4155 * @returns #TRUE if the message has the given sender
4158 dbus_message_has_sender (DBusMessage *message,
4163 _dbus_return_val_if_fail (message != NULL, FALSE);
4164 _dbus_return_val_if_fail (name != NULL, FALSE);
4165 /* don't check that name is valid since it would be expensive, and
4166 * not catch many common errors
4169 s = dbus_message_get_sender (message);
4171 if (s && strcmp (s, name) == 0)
4178 * Checks whether the message has the given signature; see
4179 * dbus_message_get_signature() for more details on what the signature
4182 * @param message the message
4183 * @param signature typecode array
4184 * @returns #TRUE if message has the given signature
4187 dbus_message_has_signature (DBusMessage *message,
4188 const char *signature)
4192 _dbus_return_val_if_fail (message != NULL, FALSE);
4193 _dbus_return_val_if_fail (signature != NULL, FALSE);
4194 /* don't check that signature is valid since it would be expensive,
4195 * and not catch many common errors
4198 s = dbus_message_get_signature (message);
4200 if (s && strcmp (s, signature) == 0)
4207 * Sets a #DBusError based on the contents of the given
4208 * message. The error is only set if the message
4209 * is an error message, as in #DBUS_MESSAGE_TYPE_ERROR.
4210 * The name of the error is set to the name of the message,
4211 * and the error message is set to the first argument
4212 * if the argument exists and is a string.
4214 * The return value indicates whether the error was set (the error is
4215 * set if and only if the message is an error message). So you can
4216 * check for an error reply and convert it to DBusError in one go:
4218 * if (dbus_set_error_from_message (error, reply))
4224 * @param error the error to set
4225 * @param message the message to set it from
4226 * @returns #TRUE if the message had type #DBUS_MESSAGE_TYPE_ERROR
4229 dbus_set_error_from_message (DBusError *error,
4230 DBusMessage *message)
4234 _dbus_return_val_if_fail (message != NULL, FALSE);
4235 _dbus_return_val_if_error_is_set (error, FALSE);
4237 if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_ERROR)
4241 dbus_message_get_args (message, NULL,
4242 DBUS_TYPE_STRING, &str,
4245 dbus_set_error (error, dbus_message_get_error_name (message),
4246 str ? "%s" : NULL, str);
4252 * Checks whether a message contains unix fds
4254 * @param message the message
4255 * @returns #TRUE if the message contains unix fds
4258 dbus_message_contains_unix_fds(DBusMessage *message)
4260 #ifdef HAVE_UNIX_FD_PASSING
4261 _dbus_assert(message);
4263 return message->n_unix_fds > 0;
4272 * @addtogroup DBusMessageInternals
4278 * The initial buffer size of the message loader.
4280 * @todo this should be based on min header size plus some average
4281 * body size, or something. Or rather, the min header size only, if we
4282 * want to try to read only the header, store that in a DBusMessage,
4283 * then read only the body and store that, etc., depends on
4284 * how we optimize _dbus_message_loader_get_buffer() and what
4285 * the exact message format is.
4287 #define INITIAL_LOADER_DATA_LEN 32
4290 * Creates a new message loader. Returns #NULL if memory can't
4293 * @returns new loader, or #NULL.
4296 _dbus_message_loader_new (void)
4298 DBusMessageLoader *loader;
4300 loader = dbus_new0 (DBusMessageLoader, 1);
4304 loader->refcount = 1;
4306 loader->corrupted = FALSE;
4307 loader->corruption_reason = DBUS_VALID;
4309 /* this can be configured by the app, but defaults to the protocol max */
4310 loader->max_message_size = DBUS_MAXIMUM_MESSAGE_LENGTH;
4312 /* We set a very relatively conservative default here since due to how
4313 SCM_RIGHTS works we need to preallocate an fd array of the maximum
4314 number of unix fds we want to receive in advance. A
4315 try-and-reallocate loop is not possible. */
4316 loader->max_message_unix_fds = DBUS_DEFAULT_MESSAGE_UNIX_FDS;
4318 if (!_dbus_string_init (&loader->data))
4324 /* preallocate the buffer for speed, ignore failure */
4325 _dbus_string_set_length (&loader->data, INITIAL_LOADER_DATA_LEN);
4326 _dbus_string_set_length (&loader->data, 0);
4328 #ifdef HAVE_UNIX_FD_PASSING
4329 loader->unix_fds = NULL;
4330 loader->n_unix_fds = loader->n_unix_fds_allocated = 0;
4331 loader->unix_fds_outstanding = FALSE;
4338 * Increments the reference count of the loader.
4340 * @param loader the loader.
4341 * @returns the loader
4344 _dbus_message_loader_ref (DBusMessageLoader *loader)
4346 loader->refcount += 1;
4352 * Decrements the reference count of the loader and finalizes the
4353 * loader when the count reaches zero.
4355 * @param loader the loader.
4358 _dbus_message_loader_unref (DBusMessageLoader *loader)
4360 loader->refcount -= 1;
4361 if (loader->refcount == 0)
4363 #ifdef HAVE_UNIX_FD_PASSING
4364 close_unix_fds(loader->unix_fds, &loader->n_unix_fds);
4365 dbus_free(loader->unix_fds);
4367 _dbus_list_foreach (&loader->messages,
4368 (DBusForeachFunction) dbus_message_unref,
4370 _dbus_list_clear (&loader->messages);
4371 _dbus_string_free (&loader->data);
4377 * Gets the buffer to use for reading data from the network. Network
4378 * data is read directly into an allocated buffer, which is then used
4379 * in the DBusMessage, to avoid as many extra memcpy's as possible.
4380 * The buffer must always be returned immediately using
4381 * _dbus_message_loader_return_buffer(), even if no bytes are
4382 * successfully read.
4384 * @todo this function can be a lot more clever. For example
4385 * it can probably always return a buffer size to read exactly
4386 * the body of the next message, thus avoiding any memory wastage
4389 * @todo we need to enforce a max length on strings in header fields.
4391 * @param loader the message loader.
4392 * @param buffer the buffer
4395 _dbus_message_loader_get_buffer (DBusMessageLoader *loader,
4396 DBusString **buffer,
4398 dbus_bool_t *may_read_fds)
4400 _dbus_assert (!loader->buffer_outstanding);
4402 *buffer = &loader->data;
4404 loader->buffer_outstanding = TRUE;
4406 if (max_to_read != NULL)
4408 #ifdef HAVE_UNIX_FD_PASSING
4412 int fields_array_len;
4417 *max_to_read = DBUS_MAXIMUM_MESSAGE_LENGTH;
4418 *may_read_fds = TRUE;
4420 #ifdef HAVE_UNIX_FD_PASSING
4421 /* If we aren't holding onto any fds, we can read as much as we want
4423 if (loader->n_unix_fds == 0)
4426 /* Slow path: we have a message with some fds in it. We don't want
4427 * to start on the next message until this one is out of the way;
4428 * otherwise a legitimate sender can keep us processing messages
4429 * containing fds, until we disconnect it for having had fds pending
4430 * for too long, a limit that is in place to stop malicious senders
4431 * from setting up recursive fd-passing that takes up our quota and
4432 * will never go away. */
4434 remain = _dbus_string_get_length (&loader->data);
4438 DBusValidity validity = DBUS_VALIDITY_UNKNOWN;
4440 dbus_bool_t is_gvariant;
4442 /* If 0 < remain < DBUS_MINIMUM_HEADER_SIZE, then we've had at
4443 * least the first byte of a message, but we don't know how
4444 * much more to read. Only read the rest of the
4445 * DBUS_MINIMUM_HEADER_SIZE for now; then we'll know. */
4446 if (remain < DBUS_MINIMUM_HEADER_SIZE)
4448 *max_to_read = DBUS_MINIMUM_HEADER_SIZE - remain;
4449 *may_read_fds = FALSE;
4453 if (!_dbus_header_have_message_untrusted (loader->max_message_size,
4464 /* If a message in the buffer is invalid, we're going to
4465 * disconnect the sender anyway, so reading an arbitrary amount
4467 if (validity != DBUS_VALID)
4470 /* We have a partial message, with the
4471 * DBUS_MINIMUM_HEADER_SIZE-byte fixed part of the header (which
4472 * lets us work out how much more we need), but no more. Read
4473 * the rest of the message. */
4474 needed = header_len + body_len;
4475 _dbus_assert (needed > remain);
4476 *max_to_read = needed - remain;
4477 *may_read_fds = FALSE;
4481 /* Skip over entire messages until we have less than a message
4483 needed = header_len + body_len;
4484 _dbus_assert (needed > DBUS_MINIMUM_HEADER_SIZE);
4485 _dbus_assert (remain >= needed);
4494 * Returns a buffer obtained from _dbus_message_loader_get_buffer(),
4495 * indicating to the loader how many bytes of the buffer were filled
4496 * in. This function must always be called, even if no bytes were
4497 * successfully read.
4499 * @param loader the loader.
4500 * @param buffer the buffer.
4503 _dbus_message_loader_return_buffer (DBusMessageLoader *loader,
4506 _dbus_assert (loader->buffer_outstanding);
4507 _dbus_assert (buffer == &loader->data);
4509 loader->buffer_outstanding = FALSE;
4512 #ifdef HAVE_UNIX_FD_PASSING
4514 * Gets the buffer to use for reading unix fds from the network.
4516 * This works similar to _dbus_message_loader_get_buffer()
4518 * @param loader the message loader.
4519 * @param fds the array to read fds into
4520 * @param max_n_fds how many fds to read at most
4521 * @return TRUE on success, FALSE on OOM
4524 _dbus_message_loader_get_unix_fds(DBusMessageLoader *loader,
4526 unsigned *max_n_fds)
4528 _dbus_assert (!loader->unix_fds_outstanding);
4530 /* Allocate space where we can put the fds we read. We allocate
4531 space for max_message_unix_fds since this is an
4532 upper limit how many fds can be received within a single
4533 message. Since SCM_RIGHTS doesn't allow a reallocate+retry logic
4534 we are allocating the maximum possible array size right from the
4535 beginning. This sucks a bit, however unless SCM_RIGHTS is fixed
4536 there is no better way. */
4538 if (loader->n_unix_fds_allocated < loader->max_message_unix_fds)
4540 int *a = dbus_realloc(loader->unix_fds,
4541 loader->max_message_unix_fds * sizeof(loader->unix_fds[0]));
4546 loader->unix_fds = a;
4547 loader->n_unix_fds_allocated = loader->max_message_unix_fds;
4550 *fds = loader->unix_fds + loader->n_unix_fds;
4551 *max_n_fds = loader->n_unix_fds_allocated - loader->n_unix_fds;
4553 loader->unix_fds_outstanding = TRUE;
4558 * Returns a buffer obtained from _dbus_message_loader_get_unix_fds().
4560 * This works similar to _dbus_message_loader_return_buffer()
4562 * @param loader the message loader.
4563 * @param fds the array fds were read into
4564 * @param n_fds how many fds were read
4568 _dbus_message_loader_return_unix_fds(DBusMessageLoader *loader,
4572 _dbus_assert(loader->unix_fds_outstanding);
4573 _dbus_assert(loader->unix_fds + loader->n_unix_fds == fds);
4574 _dbus_assert(loader->n_unix_fds + n_fds <= loader->n_unix_fds_allocated);
4576 loader->n_unix_fds += n_fds;
4577 loader->unix_fds_outstanding = FALSE;
4579 if (n_fds && loader->unix_fds_change)
4580 loader->unix_fds_change (loader->unix_fds_change_data);
4585 * FIXME when we move the header out of the buffer, that memmoves all
4586 * buffered messages. Kind of crappy.
4588 * Also we copy the header and body, which is kind of crappy. To
4589 * avoid this, we have to allow header and body to be in a single
4590 * memory block, which is good for messages we read and bad for
4591 * messages we are creating. But we could move_len() the buffer into
4592 * this single memory block, and move_len() will just swap the buffers
4593 * if you're moving the entire buffer replacing the dest string.
4595 * We could also have the message loader tell the transport how many
4596 * bytes to read; so it would first ask for some arbitrary number like
4597 * 256, then if the message was incomplete it would use the
4598 * header/body len to ask for exactly the size of the message (or
4599 * blocks the size of a typical kernel buffer for the socket). That
4600 * way we don't get trailing bytes in the buffer that have to be
4601 * memmoved. Though I suppose we also don't have a chance of reading a
4602 * bunch of small messages at once, so the optimization may be stupid.
4604 * Another approach would be to keep a "start" index into
4605 * loader->data and only delete it occasionally, instead of after
4606 * each message is loaded.
4608 * load_message() returns FALSE if not enough memory OR the loader was corrupted
4611 load_message (DBusMessageLoader *loader,
4612 DBusMessage *message,
4614 int fields_array_len,
4619 DBusValidity validity;
4620 const DBusString *type_str;
4622 DBusValidationMode mode;
4623 dbus_uint32_t n_unix_fds = 0;
4625 mode = DBUS_VALIDATION_MODE_DATA_IS_UNTRUSTED;
4630 _dbus_verbose_bytes_of_string (&loader->data, 0, header_len /* + body_len */);
4633 /* 1. VALIDATE AND COPY OVER HEADER */
4634 _dbus_assert (_dbus_string_get_length (&message->header.data) == 0);
4635 _dbus_assert ((header_len + body_len) <= _dbus_string_get_length (&loader->data));
4637 if (!_dbus_header_load (&message->header,
4645 _dbus_string_get_length (&loader->data)))
4647 _dbus_verbose ("Failed to load header for new message code %d\n", validity);
4649 /* assert here so we can catch any code that still uses DBUS_VALID to indicate
4650 oom errors. They should use DBUS_VALIDITY_UNKNOWN_OOM_ERROR instead */
4651 _dbus_assert (validity != DBUS_VALID);
4653 if (validity == DBUS_VALIDITY_UNKNOWN_OOM_ERROR)
4657 loader->corrupted = TRUE;
4658 loader->corruption_reason = validity;
4663 _dbus_assert (validity == DBUS_VALID);
4665 /* 2. VALIDATE BODY */
4666 if (mode != DBUS_VALIDATION_MODE_WE_TRUST_THIS_DATA_ABSOLUTELY)
4668 if (_dbus_message_is_gvariant (message))
4672 validity = _dbus_validate_gvariant_body_with_reason (type_str,
4683 get_const_signature (message, &type_str, &type_pos);
4685 /* Because the bytes_remaining arg is NULL, this validates that the
4686 * body is the right length
4689 validity = _dbus_validate_body_with_reason (type_str,
4697 if (validity != DBUS_VALID)
4699 _dbus_verbose ("Failed to validate message body code %d\n", validity);
4701 loader->corrupted = TRUE;
4702 loader->corruption_reason = validity;
4708 /* 3. COPY OVER UNIX FDS */
4709 _dbus_header_get_field_basic(&message->header,
4710 DBUS_HEADER_FIELD_UNIX_FDS,
4714 #ifdef HAVE_UNIX_FD_PASSING
4716 if (n_unix_fds > loader->n_unix_fds)
4718 _dbus_verbose("Message contains references to more unix fds than were sent %u != %u\n",
4719 n_unix_fds, loader->n_unix_fds);
4721 loader->corrupted = TRUE;
4722 loader->corruption_reason = DBUS_INVALID_MISSING_UNIX_FDS;
4726 /* If this was a recycled message there might still be
4727 some memory allocated for the fds */
4728 dbus_free(message->unix_fds);
4732 message->unix_fds = _dbus_memdup(loader->unix_fds, n_unix_fds * sizeof(message->unix_fds[0]));
4733 if (message->unix_fds == NULL)
4735 _dbus_verbose ("Failed to allocate file descriptor array\n");
4740 message->n_unix_fds_allocated = message->n_unix_fds = n_unix_fds;
4741 loader->n_unix_fds -= n_unix_fds;
4742 memmove (loader->unix_fds, loader->unix_fds + n_unix_fds, loader->n_unix_fds * sizeof (loader->unix_fds[0]));
4744 if (loader->unix_fds_change)
4745 loader->unix_fds_change (loader->unix_fds_change_data);
4748 message->unix_fds = NULL;
4754 _dbus_verbose ("Hmm, message claims to come with file descriptors "
4755 "but that's not supported on our platform, disconnecting.\n");
4757 loader->corrupted = TRUE;
4758 loader->corruption_reason = DBUS_INVALID_MISSING_UNIX_FDS;
4764 /* 3. COPY OVER BODY AND QUEUE MESSAGE */
4766 if (!_dbus_list_append (&loader->messages, message))
4768 _dbus_verbose ("Failed to append new message to loader queue\n");
4773 _dbus_assert (_dbus_string_get_length (&message->body) == 0);
4774 _dbus_assert (_dbus_string_get_length (&loader->data) >=
4775 (header_len + body_len));
4777 if (!_dbus_string_copy_len (&loader->data, header_len, body_len, &message->body, 0))
4779 _dbus_verbose ("Failed to move body into new message\n");
4784 _dbus_string_delete (&loader->data, 0, header_len + body_len);
4786 /* don't waste more than 2k of memory */
4787 _dbus_string_compact (&loader->data, 2048);
4789 _dbus_assert (_dbus_string_get_length (&message->header.data) == header_len);
4790 _dbus_assert (_dbus_string_get_length (&message->body) == body_len);
4792 _dbus_verbose ("Loaded message %p\n", message);
4794 _dbus_assert (!oom);
4795 _dbus_assert (!loader->corrupted);
4796 _dbus_assert (loader->messages != NULL);
4797 _dbus_assert (_dbus_list_find_last (&loader->messages, message) != NULL);
4805 /* does nothing if the message isn't in the list */
4806 _dbus_list_remove_last (&loader->messages, message);
4809 _dbus_assert (!loader->corrupted);
4811 _dbus_assert (loader->corrupted);
4813 _dbus_verbose_bytes_of_string (&loader->data, 0, _dbus_string_get_length (&loader->data));
4819 set_unique_sender (DBusMessage *message, uint64_t unique_sender_id)
4821 if (NULL == message->unique_sender)
4823 message->unique_sender = dbus_new (DBusString, 1);
4824 if (NULL == message->unique_sender)
4827 if (!_dbus_string_init (message->unique_sender))
4831 _dbus_string_set_length (message->unique_sender, 0);
4833 if (!_dbus_string_append_printf (message->unique_sender, ":1.%llu", (unsigned long long)unique_sender_id))
4835 _dbus_string_free (message->unique_sender);
4836 dbus_free (message->unique_sender);
4837 message->unique_sender = NULL;
4845 * Converts buffered data into messages, if we have enough data. If
4846 * we don't have enough data, does nothing.
4848 * @todo we need to check that the proper named header fields exist
4849 * for each message type.
4851 * @todo If a message has unknown type, we should probably eat it
4852 * right here rather than passing it out to applications. However
4853 * it's not an error to see messages of unknown type.
4855 * @param loader the loader.
4856 * @returns #TRUE if we had enough memory to finish.
4859 _dbus_message_loader_queue_messages (DBusMessageLoader *loader)
4861 while (!loader->corrupted &&
4862 _dbus_string_get_length (&loader->data) >= DBUS_MINIMUM_HEADER_SIZE)
4864 DBusValidity validity;
4865 int byte_order, fields_array_len, header_len, body_len;
4866 dbus_bool_t is_gvariant;
4868 if (_dbus_header_have_message_untrusted (loader->max_message_size,
4875 _dbus_string_get_length (&loader->data),
4878 DBusMessage *message;
4880 _dbus_assert (validity == DBUS_VALID);
4882 message = dbus_message_new_empty_header (is_gvariant);
4883 if (message == NULL)
4886 if (!load_message (loader, message,
4887 byte_order, fields_array_len,
4888 header_len, body_len))
4890 dbus_message_unref (message);
4891 /* load_message() returns false if corrupted or OOM; if
4892 * corrupted then return TRUE for not OOM
4894 return loader->corrupted;
4897 if (_dbus_message_is_gvariant (message))
4899 set_unique_sender (message, _dbus_message_loader_get_unique_sender_id (loader));
4900 message->locked = TRUE;
4903 _dbus_assert (loader->messages != NULL);
4904 _dbus_assert (_dbus_list_find_last (&loader->messages, message) != NULL);
4908 _dbus_verbose ("Initial peek at header says we don't have a whole message yet, or data broken with invalid code %d\n",
4910 if (validity != DBUS_VALID)
4912 loader->corrupted = TRUE;
4913 loader->corruption_reason = validity;
4923 _dbus_decode_kmsg (DBusString *data,
4928 if (_dbus_string_get_length (data) >= DBUS_MINIMUM_HEADER_SIZE)
4931 DBusValidity validity;
4932 int byte_order, fields_array_len, header_len, body_len;
4933 dbus_bool_t is_gvariant;
4935 if (_dbus_header_have_message_untrusted (DBUS_MAXIMUM_MESSAGE_LENGTH,
4942 _dbus_string_get_length (data),
4945 DBusMessage *message;
4946 dbus_uint32_t n_unix_fds = 0;
4947 const DBusString *type_str = NULL;
4950 _dbus_assert (validity == DBUS_VALID);
4952 message = dbus_message_new_empty_header (is_gvariant);
4953 if (message == NULL)
4957 * Validate and copy over header
4959 _dbus_assert (_dbus_string_get_length (&message->header.data) == 0);
4960 _dbus_assert ((header_len + body_len) <= _dbus_string_get_length (data));
4962 if (!_dbus_header_load (&message->header,
4963 DBUS_VALIDATION_MODE_DATA_IS_UNTRUSTED,
4970 _dbus_string_get_length (data)))
4972 _dbus_verbose ("Failed to load header for new message code %d\n", validity);
4973 dbus_message_unref (message);
4977 _dbus_assert (validity == DBUS_VALID);
4982 if (_dbus_message_is_gvariant (message))
4984 validity = _dbus_validate_gvariant_body_with_reason (type_str,
4994 _dbus_verbose ("Not valid GVariant dbus message\n");
4995 dbus_message_unref (message);
4999 if (validity != DBUS_VALID)
5001 _dbus_verbose ("Failed to validate message body code %d\n", validity);
5002 dbus_message_unref (message);
5007 * Copy over Unix FDS
5009 _dbus_header_get_field_basic(&message->header,
5010 DBUS_HEADER_FIELD_UNIX_FDS,
5014 #ifdef HAVE_UNIX_FD_PASSING
5016 if (n_unix_fds > n_fds)
5018 _dbus_verbose("Message contains references to more unix fds than were sent %u != %u\n",
5019 n_unix_fds, message->n_unix_fds);
5020 dbus_message_unref (message);
5024 /* If this was a recycled message there might still be
5025 some memory allocated for the fds */
5026 dbus_free(message->unix_fds);
5030 message->unix_fds = _dbus_memdup(fds, n_unix_fds * sizeof(message->unix_fds[0]));
5031 if (message->unix_fds == NULL)
5033 _dbus_verbose ("Failed to allocate file descriptor array\n");
5034 dbus_message_unref (message);
5038 message->n_unix_fds_allocated = message->n_unix_fds = n_unix_fds;
5041 message->unix_fds = NULL;
5045 _dbus_verbose ("Hmm, message claims to come with file descriptors "
5046 "but that's not supported on our platform, disconnecting.\n");
5047 dbus_message_unref (message);
5053 * Copy over message body
5055 _dbus_assert (_dbus_string_get_length (&message->body) == 0);
5056 _dbus_assert (_dbus_string_get_length (data) >= (header_len + body_len));
5058 if (!_dbus_string_copy_len (data, header_len, body_len, &message->body, 0))
5060 _dbus_verbose ("Failed to move body into new message\n");
5061 dbus_message_unref (message);
5065 _dbus_assert (_dbus_string_get_length (&message->header.data) == header_len);
5066 _dbus_assert (_dbus_string_get_length (&message->body) == body_len);
5068 set_unique_sender (message, sender_id);
5069 message->locked = TRUE;
5071 /* Yupi, we have DBusMessage* */
5076 _dbus_verbose ("Data broken with invalid code %d\n", validity);
5078 } /* if _dbus_header_have_message_untrusted() */
5083 _dbus_verbose ("message size < DBUS_MINIMUM_HEADER_SIZE\n");
5085 } /* if DBUS_MINIMUM_HEADER_SIZE */
5089 * Peeks at first loaded message, returns #NULL if no messages have
5092 * @param loader the loader.
5093 * @returns the next message, or #NULL if none.
5096 _dbus_message_loader_peek_message (DBusMessageLoader *loader)
5098 if (loader->messages)
5099 return loader->messages->data;
5105 * Pops a loaded message (passing ownership of the message
5106 * to the caller). Returns #NULL if no messages have been
5109 * @param loader the loader.
5110 * @returns the next message, or #NULL if none.
5113 _dbus_message_loader_pop_message (DBusMessageLoader *loader)
5115 return _dbus_list_pop_first (&loader->messages);
5119 * Pops a loaded message inside a list link (passing ownership of the
5120 * message and link to the caller). Returns #NULL if no messages have
5123 * @param loader the loader.
5124 * @returns the next message link, or #NULL if none.
5127 _dbus_message_loader_pop_message_link (DBusMessageLoader *loader)
5129 return _dbus_list_pop_first_link (&loader->messages);
5133 * Returns a popped message link, used to undo a pop.
5135 * @param loader the loader
5136 * @param link the link with a message in it
5139 _dbus_message_loader_putback_message_link (DBusMessageLoader *loader,
5142 _dbus_list_prepend_link (&loader->messages, link);
5146 * Checks whether the loader is confused due to bad data.
5147 * If messages are received that are invalid, the
5148 * loader gets confused and gives up permanently.
5149 * This state is called "corrupted."
5151 * @param loader the loader
5152 * @returns #TRUE if the loader is hosed.
5155 _dbus_message_loader_get_is_corrupted (DBusMessageLoader *loader)
5157 _dbus_assert ((loader->corrupted && loader->corruption_reason != DBUS_VALID) ||
5158 (!loader->corrupted && loader->corruption_reason == DBUS_VALID));
5159 return loader->corrupted;
5163 * Checks what kind of bad data confused the loader.
5165 * @param loader the loader
5166 * @returns why the loader is hosed, or DBUS_VALID if it isn't.
5169 _dbus_message_loader_get_corruption_reason (DBusMessageLoader *loader)
5171 _dbus_assert ((loader->corrupted && loader->corruption_reason != DBUS_VALID) ||
5172 (!loader->corrupted && loader->corruption_reason == DBUS_VALID));
5174 return loader->corruption_reason;
5178 * Sets the maximum size message we allow.
5180 * @param loader the loader
5181 * @param size the max message size in bytes
5184 _dbus_message_loader_set_max_message_size (DBusMessageLoader *loader,
5187 if (size > DBUS_MAXIMUM_MESSAGE_LENGTH)
5189 _dbus_verbose ("clamping requested max message size %ld to %d\n",
5190 size, DBUS_MAXIMUM_MESSAGE_LENGTH);
5191 size = DBUS_MAXIMUM_MESSAGE_LENGTH;
5193 loader->max_message_size = size;
5197 * Gets the maximum allowed message size in bytes.
5199 * @param loader the loader
5200 * @returns max size in bytes
5203 _dbus_message_loader_get_max_message_size (DBusMessageLoader *loader)
5205 return loader->max_message_size;
5209 * Sets the maximum unix fds per message we allow.
5211 * @param loader the loader
5212 * @param n the max number of unix fds in a message
5215 _dbus_message_loader_set_max_message_unix_fds (DBusMessageLoader *loader,
5218 if (n > DBUS_MAXIMUM_MESSAGE_UNIX_FDS)
5220 _dbus_verbose ("clamping requested max message unix_fds %ld to %d\n",
5221 n, DBUS_MAXIMUM_MESSAGE_UNIX_FDS);
5222 n = DBUS_MAXIMUM_MESSAGE_UNIX_FDS;
5224 loader->max_message_unix_fds = n;
5228 * Gets the maximum allowed number of unix fds per message
5230 * @param loader the loader
5231 * @returns max unix fds
5234 _dbus_message_loader_get_max_message_unix_fds (DBusMessageLoader *loader)
5236 return loader->max_message_unix_fds;
5240 * Return how many file descriptors are pending in the loader
5242 * @param loader the loader
5245 _dbus_message_loader_get_pending_fds_count (DBusMessageLoader *loader)
5247 #ifdef HAVE_UNIX_FD_PASSING
5248 return loader->n_unix_fds;
5255 * Register a function to be called whenever the number of pending file
5256 * descriptors in the loader change.
5258 * @param loader the loader
5259 * @param callback the callback
5260 * @param data the data for the callback
5263 _dbus_message_loader_set_pending_fds_function (DBusMessageLoader *loader,
5264 void (* callback) (void *),
5267 #ifdef HAVE_UNIX_FD_PASSING
5268 loader->unix_fds_change = callback;
5269 loader->unix_fds_change_data = data;
5274 _dbus_message_loader_set_unique_sender_id (DBusMessageLoader *loader,
5277 loader->unique_sender_id = id;
5281 _dbus_message_loader_get_unique_sender_id (DBusMessageLoader *loader)
5283 return loader->unique_sender_id;
5286 static DBusDataSlotAllocator slot_allocator =
5287 _DBUS_DATA_SLOT_ALLOCATOR_INIT (_DBUS_LOCK_NAME (message_slots));
5290 * Allocates an integer ID to be used for storing application-specific
5291 * data on any DBusMessage. The allocated ID may then be used
5292 * with dbus_message_set_data() and dbus_message_get_data().
5293 * The passed-in slot must be initialized to -1, and is filled in
5294 * with the slot ID. If the passed-in slot is not -1, it's assumed
5295 * to be already allocated, and its refcount is incremented.
5297 * The allocated slot is global, i.e. all DBusMessage objects will
5298 * have a slot with the given integer ID reserved.
5300 * @param slot_p address of a global variable storing the slot
5301 * @returns #FALSE on failure (no memory)
5304 dbus_message_allocate_data_slot (dbus_int32_t *slot_p)
5306 return _dbus_data_slot_allocator_alloc (&slot_allocator,
5311 * Deallocates a global ID for message data slots.
5312 * dbus_message_get_data() and dbus_message_set_data() may no
5313 * longer be used with this slot. Existing data stored on existing
5314 * DBusMessage objects will be freed when the message is
5315 * finalized, but may not be retrieved (and may only be replaced if
5316 * someone else reallocates the slot). When the refcount on the
5317 * passed-in slot reaches 0, it is set to -1.
5319 * @param slot_p address storing the slot to deallocate
5322 dbus_message_free_data_slot (dbus_int32_t *slot_p)
5324 _dbus_return_if_fail (*slot_p >= 0);
5326 _dbus_data_slot_allocator_free (&slot_allocator, slot_p);
5330 * Stores a pointer on a DBusMessage, along
5331 * with an optional function to be used for freeing
5332 * the data when the data is set again, or when
5333 * the message is finalized. The slot number
5334 * must have been allocated with dbus_message_allocate_data_slot().
5336 * @param message the message
5337 * @param slot the slot number
5338 * @param data the data to store
5339 * @param free_data_func finalizer function for the data
5340 * @returns #TRUE if there was enough memory to store the data
5343 dbus_message_set_data (DBusMessage *message,
5346 DBusFreeFunction free_data_func)
5348 DBusFreeFunction old_free_func;
5352 _dbus_return_val_if_fail (message != NULL, FALSE);
5353 _dbus_return_val_if_fail (slot >= 0, FALSE);
5355 retval = _dbus_data_slot_list_set (&slot_allocator,
5356 &message->slot_list,
5357 slot, data, free_data_func,
5358 &old_free_func, &old_data);
5362 /* Do the actual free outside the message lock */
5364 (* old_free_func) (old_data);
5371 * Retrieves data previously set with dbus_message_set_data().
5372 * The slot must still be allocated (must not have been freed).
5374 * @param message the message
5375 * @param slot the slot to get data from
5376 * @returns the data, or #NULL if not found
5379 dbus_message_get_data (DBusMessage *message,
5384 _dbus_return_val_if_fail (message != NULL, NULL);
5386 res = _dbus_data_slot_list_get (&slot_allocator,
5387 &message->slot_list,
5394 * Utility function to convert a machine-readable (not translated)
5395 * string into a D-Bus message type.
5398 * "method_call" -> DBUS_MESSAGE_TYPE_METHOD_CALL
5399 * "method_return" -> DBUS_MESSAGE_TYPE_METHOD_RETURN
5400 * "signal" -> DBUS_MESSAGE_TYPE_SIGNAL
5401 * "error" -> DBUS_MESSAGE_TYPE_ERROR
5402 * anything else -> DBUS_MESSAGE_TYPE_INVALID
5407 dbus_message_type_from_string (const char *type_str)
5409 if (strcmp (type_str, "method_call") == 0)
5410 return DBUS_MESSAGE_TYPE_METHOD_CALL;
5411 if (strcmp (type_str, "method_return") == 0)
5412 return DBUS_MESSAGE_TYPE_METHOD_RETURN;
5413 else if (strcmp (type_str, "signal") == 0)
5414 return DBUS_MESSAGE_TYPE_SIGNAL;
5415 else if (strcmp (type_str, "error") == 0)
5416 return DBUS_MESSAGE_TYPE_ERROR;
5418 return DBUS_MESSAGE_TYPE_INVALID;
5422 * Utility function to convert a D-Bus message type into a
5423 * machine-readable string (not translated).
5426 * DBUS_MESSAGE_TYPE_METHOD_CALL -> "method_call"
5427 * DBUS_MESSAGE_TYPE_METHOD_RETURN -> "method_return"
5428 * DBUS_MESSAGE_TYPE_SIGNAL -> "signal"
5429 * DBUS_MESSAGE_TYPE_ERROR -> "error"
5430 * DBUS_MESSAGE_TYPE_INVALID -> "invalid"
5435 dbus_message_type_to_string (int type)
5439 case DBUS_MESSAGE_TYPE_METHOD_CALL:
5440 return "method_call";
5441 case DBUS_MESSAGE_TYPE_METHOD_RETURN:
5442 return "method_return";
5443 case DBUS_MESSAGE_TYPE_SIGNAL:
5445 case DBUS_MESSAGE_TYPE_ERROR:
5453 * Turn a DBusMessage into the marshalled form as described in the D-Bus
5456 * Generally, this function is only useful for encapsulating D-Bus messages in
5457 * a different protocol.
5459 * @param msg the DBusMessage
5460 * @param marshalled_data_p the location to save the marshalled form to
5461 * @param len_p the location to save the length of the marshalled form to
5462 * @returns #FALSE if there was not enough memory
5465 dbus_message_marshal (DBusMessage *msg,
5466 char **marshalled_data_p,
5470 dbus_bool_t was_locked;
5472 _dbus_return_val_if_fail (msg != NULL, FALSE);
5473 _dbus_return_val_if_fail (marshalled_data_p != NULL, FALSE);
5474 _dbus_return_val_if_fail (len_p != NULL, FALSE);
5476 if (!_dbus_string_init (&tmp))
5479 /* Ensure the message is locked, to ensure the length header is filled in. */
5480 was_locked = msg->locked;
5483 dbus_message_lock (msg);
5485 if (!_dbus_string_copy (&(msg->header.data), 0, &tmp, 0))
5488 *len_p = _dbus_string_get_length (&tmp);
5490 if (!_dbus_string_copy (&(msg->body), 0, &tmp, *len_p))
5493 *len_p = _dbus_string_get_length (&tmp);
5495 if (!_dbus_string_steal_data (&tmp, marshalled_data_p))
5498 _dbus_string_free (&tmp);
5501 msg->locked = FALSE;
5506 _dbus_string_free (&tmp);
5509 msg->locked = FALSE;
5515 * Demarshal a D-Bus message from the format described in the D-Bus
5518 * Generally, this function is only useful for encapsulating D-Bus messages in
5519 * a different protocol.
5521 * @param str the marshalled DBusMessage
5522 * @param len the length of str
5523 * @param error the location to save errors to
5524 * @returns #NULL if there was an error
5527 dbus_message_demarshal (const char *str,
5531 DBusMessageLoader *loader;
5535 _dbus_return_val_if_fail (str != NULL, NULL);
5537 loader = _dbus_message_loader_new ();
5542 _dbus_message_loader_get_buffer (loader, &buffer, NULL, NULL);
5544 if (!_dbus_string_append_len (buffer, str, len))
5547 _dbus_message_loader_return_buffer (loader, buffer);
5549 if (!_dbus_message_loader_queue_messages (loader))
5552 if (_dbus_message_loader_get_is_corrupted (loader))
5555 msg = _dbus_message_loader_pop_message (loader);
5560 _dbus_message_loader_unref (loader);
5564 dbus_set_error (error, DBUS_ERROR_INVALID_ARGS, "Message is corrupted (%s)",
5565 _dbus_validity_to_error_message (loader->corruption_reason));
5566 _dbus_message_loader_unref (loader);
5570 _DBUS_SET_OOM (error);
5571 _dbus_message_loader_unref (loader);
5576 * Returns the number of bytes required to be in the buffer to demarshal a
5579 * Generally, this function is only useful for encapsulating D-Bus messages in
5580 * a different protocol.
5582 * @param buf data to be marshalled
5583 * @param len the length of @p buf
5584 * @returns -1 if there was no valid data to be demarshalled, 0 if there wasn't enough data to determine how much should be demarshalled. Otherwise returns the number of bytes to be demarshalled
5588 dbus_message_demarshal_bytes_needed(const char *buf,
5592 int byte_order, fields_array_len, header_len, body_len;
5593 DBusValidity validity = DBUS_VALID;
5595 dbus_bool_t is_gvariant;
5597 if (!buf || len < DBUS_MINIMUM_HEADER_SIZE)
5600 if (len > DBUS_MAXIMUM_MESSAGE_LENGTH)
5601 len = DBUS_MAXIMUM_MESSAGE_LENGTH;
5602 _dbus_string_init_const_len (&str, buf, len);
5604 validity = DBUS_VALID;
5606 = _dbus_header_have_message_untrusted(DBUS_MAXIMUM_MESSAGE_LENGTH,
5607 &validity, &byte_order,
5614 _dbus_string_free (&str);
5616 if (validity == DBUS_VALID)
5618 _dbus_assert (have_message || (header_len + body_len) > len);
5619 (void) have_message; /* unused unless asserting */
5620 return header_len + body_len;
5624 return -1; /* broken! */
5629 _dbus_message_copy_recursive(DBusMessageIter *iter, DBusMessageIter *dest)
5631 dbus_bool_t res = TRUE;
5634 while ((current_type = dbus_message_iter_get_arg_type (iter)) != DBUS_TYPE_INVALID) {
5635 if (dbus_type_is_basic(current_type)) {
5636 DBusBasicValue value;
5637 dbus_message_iter_get_basic (iter, &value);
5638 dbus_message_iter_append_basic (dest, current_type, &value);
5641 DBusMessageIter sub;
5642 DBusMessageIter dest_sub;
5645 dbus_message_iter_recurse (iter, &sub);
5646 if (DBUS_TYPE_VARIANT == current_type)
5647 sig = dbus_message_iter_get_signature (&sub);
5648 else if (DBUS_TYPE_ARRAY == current_type)
5649 sig = dbus_message_iter_get_signature (&sub);
5651 res = res && dbus_message_iter_open_container (dest, current_type, sig, &dest_sub);
5653 res = res && _dbus_message_copy_recursive (&sub, &dest_sub);
5654 res = res && dbus_message_iter_close_container (dest, &dest_sub);
5661 dbus_message_iter_next (iter);
5668 _dbus_on_new_bus (int type)
5670 _dbus_assert (type == DBUS_MAJOR_PROTOCOL_VERSION || type == DBUS_PROTOCOL_VERSION_GVARIANT);
5671 _dbus_protocol_strategy_bus_function (type);
5675 _dbus_on_send_message (int type)
5677 _dbus_assert (type == DBUS_MAJOR_PROTOCOL_VERSION || type == DBUS_PROTOCOL_VERSION_GVARIANT);
5678 _dbus_protocol_strategy_message_function (type);
5682 _dbus_message_remarshal (DBusMessage *message, dbus_bool_t gvariant)
5685 DBusMessageIter iter, ret_iter;
5687 dbus_uint32_t serial;
5690 _dbus_assert (message->locked);
5692 _dbus_on_send_message (gvariant ? DBUS_PROTOCOL_VERSION_GVARIANT : DBUS_MAJOR_PROTOCOL_VERSION);
5694 ret = _dbus_message_create_protocol_version (dbus_message_get_type(message),
5695 dbus_message_get_destination(message),
5696 dbus_message_get_path(message),
5697 dbus_message_get_interface(message),
5698 dbus_message_get_member(message),
5699 dbus_message_get_error_name(message),
5702 dbus_message_iter_init (message, &iter);
5703 dbus_message_iter_init_append (ret, &ret_iter);
5704 if (!_dbus_message_copy_recursive(&iter, &ret_iter))
5707 #ifdef HAVE_UNIX_FD_PASSING
5708 ret->unix_fds = dbus_new(int, message->n_unix_fds);
5709 if (ret->unix_fds == NULL && message->n_unix_fds > 0)
5712 ret->n_unix_fds_allocated = message->n_unix_fds;
5714 for (i = 0; i < message->n_unix_fds; ++i) {
5715 ret->unix_fds[i] = _dbus_dup(message->unix_fds[i], NULL);
5717 if (ret->unix_fds[i] < 0) {
5718 ret->n_unix_fds = i;
5723 ret->n_unix_fds = message->n_unix_fds;
5726 /* Remarshal data in header:
5727 byte order (already set)
5729 flags - only those we understand
5730 version (already set)
5733 fields array (length)
5736 interface (already set)
5737 member (already set)
5738 error name (already set)
5740 destination (already set)
5742 signature (set during copy, but an action needed for conversion to GVariant)
5747 _dbus_header_toggle_flag (&ret->header, DBUS_HEADER_FLAG_NO_REPLY_EXPECTED,
5748 _dbus_header_get_flag (&message->header, DBUS_HEADER_FLAG_NO_REPLY_EXPECTED));
5750 _dbus_header_toggle_flag (&ret->header, DBUS_HEADER_FLAG_NO_AUTO_START,
5751 _dbus_header_get_flag (&message->header, DBUS_HEADER_FLAG_NO_AUTO_START));
5753 /* SERIAL / COOKIE */
5754 serial = dbus_message_get_serial (message);
5757 dbus_message_set_serial (ret, serial);
5759 /* Field: REPLY_SERIAL */
5760 serial = dbus_message_get_reply_serial (message);
5762 if (0 != serial && !dbus_message_set_reply_serial (ret, serial))
5766 sender = dbus_message_get_sender (message);
5768 if (NULL != sender && !dbus_message_set_sender (ret, sender))
5773 _dbus_header_update_lengths (&ret->header,
5774 _dbus_string_get_length (&ret->body));
5776 /* Field: SIGNATURE to body; add body offset - this is done with dbus_message_lock() */
5778 dbus_message_lock (ret);
5783 _dbus_header_free (&ret->header);
5784 _dbus_string_free (&ret->body);
5786 #ifdef HAVE_UNIX_FD_PASSING
5789 close_unix_fds(ret->unix_fds, &ret->n_unix_fds);
5790 dbus_free(ret->unix_fds);
5798 dbus_set_protocol_version (unsigned char version)
5800 _dbus_default_protocol_version = version;
5804 protocol_strategy_first_type (int type)
5806 /* change protocol once */
5807 if (!_dbus_first_bus_open)
5809 _dbus_first_bus_open = TRUE;
5810 _dbus_default_protocol_version = type;
5815 protocol_strategy_last_type (int type)
5817 /* change protocol every time it is needed */
5818 if (_dbus_default_protocol_version != type)
5819 _dbus_default_protocol_version = type;
5823 protocol_strategy_static (int type)
5829 dbus_set_default_protocol_strategy (const char *strategy_name)
5831 if (strcmp (strategy_name, "first-bus") == 0)
5833 _dbus_protocol_strategy_bus_function = protocol_strategy_first_type;
5834 _dbus_protocol_strategy_message_function = protocol_strategy_static;
5836 else if (strcmp (strategy_name, "dbus1") == 0)
5838 _dbus_default_protocol_version = DBUS_MAJOR_PROTOCOL_VERSION;
5839 _dbus_protocol_strategy_bus_function = protocol_strategy_static;
5840 _dbus_protocol_strategy_message_function = protocol_strategy_static;
5842 else if (strcmp (strategy_name, "gvariant") == 0)
5844 _dbus_default_protocol_version = DBUS_PROTOCOL_VERSION_GVARIANT;
5845 _dbus_protocol_strategy_bus_function = protocol_strategy_static;
5846 _dbus_protocol_strategy_message_function = protocol_strategy_static;
5848 else if (strcmp (strategy_name, "last-message") == 0)
5850 _dbus_protocol_strategy_bus_function = protocol_strategy_static;
5851 _dbus_protocol_strategy_message_function = protocol_strategy_last_type;
5853 else /* "last-bus" is default strategy */
5855 _dbus_protocol_strategy_bus_function = protocol_strategy_last_type;
5856 _dbus_protocol_strategy_message_function = protocol_strategy_static;
5861 _dbus_generate_local_error_message (dbus_uint32_t serial,
5862 const char *error_name,
5863 const char *error_msg)
5865 DBusMessage *message;
5866 message = dbus_message_new (DBUS_MESSAGE_TYPE_ERROR);
5870 if (!dbus_message_set_error_name (message, error_name))
5872 dbus_message_unref (message);
5877 dbus_message_set_no_reply (message, TRUE);
5879 if (!dbus_message_set_reply_serial (message,
5882 dbus_message_unref (message);
5887 if (error_msg != NULL)
5889 DBusMessageIter iter;
5891 dbus_message_iter_init_append (message, &iter);
5892 if (!dbus_message_iter_append_basic (&iter,
5896 dbus_message_unref (message);
5907 _dbus_message_assure_dbus1 (DBusMessage **message)
5909 if ((*message)->header.protocol_version != DBUS_MAJOR_PROTOCOL_VERSION)
5911 *message = _dbus_message_remarshal (*message, FALSE);
5918 _dbus_message_assure_gvariant (DBusMessage **message)
5920 if ((*message)->header.protocol_version != DBUS_PROTOCOL_VERSION_GVARIANT)
5922 *message = _dbus_message_remarshal (*message, TRUE);
5929 * An opaque data structure containing the serialized form of any single
5930 * D-Bus message item, whose signature is a single complete type.
5932 * (Implementation detail: It's serialized as a single variant.)
5940 * Copy a single D-Bus message item from reader into a
5941 * newly-allocated #DBusVariant.
5943 * For example, if a message contains three string arguments, and reader points
5944 * to the second string, the resulting DBusVariant will have signature
5945 * #DBUS_TYPE_STRING_AS_STRING and contain only that second string.
5947 * @param reader An iterator over message items, pointing to one item to copy
5948 * @returns The variant, or #NULL if out of memory
5951 _dbus_variant_read (DBusMessageIter *reader)
5953 DBusVariant *self = NULL;
5954 /* Points to the single item we will read from the reader */
5955 DBusMessageRealIter *real_reader = (DBusMessageRealIter *) reader;
5956 /* The position in self at which we will write a single variant
5957 * (it is position 0) */
5958 DBusTypeWriter items_writer;
5959 /* The position in self at which we will write a copy of reader
5960 * (it is inside the variant) */
5961 DBusTypeWriter variant_writer;
5963 DBusString variant_signature;
5964 /* Whatever is the signature of the item we will copy from the reader */
5965 DBusString contained_signature;
5966 /* TRUE if self->data needs to be freed */
5967 dbus_bool_t data_inited = FALSE;
5968 /* The type of the item we will read from the reader */
5970 /* The string, start position within that string, and length of the signature
5971 * of the single complete type of the item reader points to */
5972 const DBusString *sig;
5975 _dbus_assert (_dbus_message_iter_check (real_reader));
5976 _dbus_assert (real_reader->iter_type == DBUS_MESSAGE_ITER_TYPE_READER);
5977 _dbus_string_init_const (&variant_signature, DBUS_TYPE_VARIANT_AS_STRING);
5978 type = dbus_message_iter_get_arg_type (reader);
5979 _dbus_type_reader_get_signature (&real_reader->u.reader, &sig, &start, &len);
5981 if (!_dbus_string_init (&contained_signature))
5984 if (!_dbus_string_copy_len (sig, start, len, &contained_signature, 0))
5987 self = dbus_new0 (DBusVariant, 1);
5992 if (!_dbus_string_init (&self->data))
5997 _dbus_type_writer_init_values_only (&items_writer, DBUS_COMPILER_BYTE_ORDER,
5998 &variant_signature, 0, &self->data, 0);
6000 if (!_dbus_type_writer_recurse (&items_writer, DBUS_TYPE_VARIANT,
6001 &contained_signature, 0, &variant_writer))
6004 if (type == DBUS_TYPE_ARRAY)
6006 /* Points to each item in turn inside the array we are copying */
6007 DBusMessageIter array_reader;
6008 /* Same as array_reader */
6009 DBusMessageRealIter *real_array_reader = (DBusMessageRealIter *) &array_reader;
6010 /* The position inside the copied array at which we will write
6011 * the copy of array_reader */
6012 DBusTypeWriter array_writer;
6014 dbus_message_iter_recurse (reader, &array_reader);
6016 if (!_dbus_type_writer_recurse (&variant_writer, type,
6017 &contained_signature, 1, &array_writer))
6020 if (!_dbus_type_writer_write_reader (&array_writer,
6021 &real_array_reader->u.reader))
6024 if (!_dbus_type_writer_unrecurse (&variant_writer, &array_writer))
6027 else if (type == DBUS_TYPE_DICT_ENTRY || type == DBUS_TYPE_VARIANT ||
6028 type == DBUS_TYPE_STRUCT)
6030 /* Points to each item in turn inside the container we are copying */
6031 DBusMessageIter inner_reader;
6032 /* Same as inner_reader */
6033 DBusMessageRealIter *real_inner_reader = (DBusMessageRealIter *) &inner_reader;
6034 /* The position inside the copied container at which we will write the
6035 * copy of inner_reader */
6036 DBusTypeWriter inner_writer;
6038 dbus_message_iter_recurse (reader, &inner_reader);
6040 if (!_dbus_type_writer_recurse (&variant_writer, type, NULL, 0,
6044 if (!_dbus_type_writer_write_reader (&inner_writer,
6045 &real_inner_reader->u.reader))
6048 if (!_dbus_type_writer_unrecurse (&variant_writer, &inner_writer))
6053 DBusBasicValue value;
6055 /* We eliminated all the container types above */
6056 _dbus_assert (dbus_type_is_basic (type));
6058 dbus_message_iter_get_basic (reader, &value);
6060 if (!_dbus_type_writer_write_basic (&variant_writer, type, &value))
6064 _dbus_string_free (&contained_signature);
6071 _dbus_string_free (&self->data);
6076 _dbus_string_free (&contained_signature);
6081 * Return the signature of the item stored in self. It is a single complete
6084 * @param self the variant
6087 _dbus_variant_get_signature (DBusVariant *self)
6092 _dbus_assert (self != NULL);
6094 /* Here we make use of the fact that the serialization of a variant starts
6095 * with the 1-byte length, then that many bytes of signature, then \0. */
6096 len = _dbus_string_get_byte (&self->data, 0);
6097 ret = _dbus_string_get_const_data_len (&self->data, 1, len);
6098 _dbus_assert (strlen (ret) == len);
6103 * Copy the single D-Bus message item from self into writer.
6105 * For example, if writer points into the body of an empty message and self has
6106 * signature #DBUS_TYPE_STRING_AS_STRING, then the message will
6107 * have signature #DBUS_TYPE_STRING_AS_STRING after this function returns
6109 * @param self the variant
6110 * @param writer the place to write the contents of the variant
6111 * @returns #TRUE on success, #FALSE if out of memory
6114 _dbus_variant_write (DBusVariant *self,
6115 DBusMessageIter *writer)
6118 DBusString variant_signature;
6119 /* Points to the single item in self */
6120 DBusTypeReader variant_reader;
6121 /* Points to the single item (of whatever type) inside the variant */
6122 DBusTypeReader reader;
6123 /* The position at which we will copy reader */
6124 DBusMessageRealIter *real_writer = (DBusMessageRealIter *) writer;
6127 _dbus_assert (self != NULL);
6128 _dbus_assert (_dbus_message_iter_append_check (real_writer));
6129 _dbus_assert (real_writer->iter_type == DBUS_MESSAGE_ITER_TYPE_WRITER);
6131 _dbus_string_init_const (&variant_signature, DBUS_TYPE_VARIANT_AS_STRING);
6132 _dbus_type_reader_init (&reader, DBUS_COMPILER_BYTE_ORDER,
6133 &variant_signature, 0, &self->data, 0);
6134 _dbus_type_reader_recurse (&reader, &variant_reader);
6136 if (!_dbus_message_iter_open_signature (real_writer))
6139 ret = _dbus_type_writer_write_reader (&real_writer->u.writer,
6142 if (!_dbus_message_iter_close_signature (real_writer))
6149 _dbus_variant_get_length (DBusVariant *self)
6151 _dbus_assert (self != NULL);
6152 return _dbus_string_get_length (&self->data);
6156 _dbus_variant_peek (DBusVariant *self)
6158 _dbus_assert (self != NULL);
6163 _dbus_variant_free (DBusVariant *self)
6165 _dbus_assert (self != NULL);
6166 _dbus_string_free (&self->data);
6172 /* tests in dbus-message-util.c */