OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "dbus/message.h" | 5 #include "dbus/message.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/format_macros.h" | 10 #include "base/format_macros.h" |
11 #include "base/logging.h" | 11 #include "base/logging.h" |
12 #include "base/string_util.h" | 12 #include "base/string_util.h" |
13 #include "base/stringprintf.h" | 13 #include "base/stringprintf.h" |
14 #include "dbus/object_path.h" | 14 #include "dbus/object_path.h" |
15 #include "third_party/protobuf/src/google/protobuf/message_lite.h" | 15 #include "third_party/protobuf/src/google/protobuf/message_lite.h" |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 // Appends the header name and the value to |output|, if the value is | 19 // Appends the header name and the value to |output|, if the value is |
20 // not empty. | 20 // not empty. |
21 static void AppendStringHeader(const std::string& header_name, | 21 void AppendStringHeader(const std::string& header_name, |
22 const std::string& header_value, | 22 const std::string& header_value, |
23 std::string* output) { | 23 std::string* output) { |
24 if (!header_value.empty()) { | 24 if (!header_value.empty()) { |
25 *output += header_name + ": " + header_value + "\n"; | 25 *output += header_name + ": " + header_value + "\n"; |
26 } | 26 } |
27 } | 27 } |
28 | 28 |
29 // Appends the header name and the value to |output|, if the value is | 29 // Appends the header name and the value to |output|, if the value is |
30 // nonzero. | 30 // nonzero. |
31 static void AppendUint32Header(const std::string& header_name, | 31 void AppendUint32Header(const std::string& header_name, |
32 uint32 header_value, | 32 uint32 header_value, |
33 std::string* output) { | 33 std::string* output) { |
34 if (header_value != 0) { | 34 if (header_value != 0) { |
35 *output += (header_name + ": " + base::StringPrintf("%u", header_value) + | 35 *output += (header_name + ": " + base::StringPrintf("%u", header_value) + |
36 "\n"); | 36 "\n"); |
37 } | 37 } |
38 } | 38 } |
39 | 39 |
| 40 // Returns true if Unix FD passing is supported in libdbus. |
| 41 // The check is done runtime rather than compile time as the libdbus |
| 42 // version used at runtime may be different from the one used at compile time. |
| 43 bool IsDBusTypeUnixFdSupported() { |
| 44 int major = 0, minor = 0, micro = 0; |
| 45 dbus_get_version(&major, &minor, µ); |
| 46 return major >= 1 && minor >= 4; |
| 47 } |
| 48 |
40 } // namespace | 49 } // namespace |
41 | 50 |
42 namespace dbus { | 51 namespace dbus { |
43 | 52 |
44 Message::Message() | 53 Message::Message() |
45 : raw_message_(NULL) { | 54 : raw_message_(NULL) { |
46 } | 55 } |
47 | 56 |
48 Message::~Message() { | 57 Message::~Message() { |
49 if (raw_message_) | 58 if (raw_message_) |
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 } | 213 } |
205 case VARIANT: { | 214 case VARIANT: { |
206 MessageReader sub_reader(this); | 215 MessageReader sub_reader(this); |
207 if (!reader->PopVariant(&sub_reader)) | 216 if (!reader->PopVariant(&sub_reader)) |
208 return kBrokenMessage; | 217 return kBrokenMessage; |
209 output += indent + "variant "; | 218 output += indent + "variant "; |
210 output += ToStringInternal(indent + " ", &sub_reader); | 219 output += ToStringInternal(indent + " ", &sub_reader); |
211 break; | 220 break; |
212 } | 221 } |
213 case UNIX_FD: { | 222 case UNIX_FD: { |
214 CHECK(kDBusTypeUnixFdIsSupported); | 223 CHECK(IsDBusTypeUnixFdSupported()); |
215 | 224 |
216 FileDescriptor file_descriptor; | 225 FileDescriptor file_descriptor; |
217 if (!reader->PopFileDescriptor(&file_descriptor)) | 226 if (!reader->PopFileDescriptor(&file_descriptor)) |
218 return kBrokenMessage; | 227 return kBrokenMessage; |
219 output += indent + "fd#" + | 228 output += indent + "fd#" + |
220 base::StringPrintf("%d", file_descriptor.value()) + "\n"; | 229 base::StringPrintf("%d", file_descriptor.value()) + "\n"; |
221 break; | 230 break; |
222 } | 231 } |
223 default: | 232 default: |
224 LOG(FATAL) << "Unknown type: " << type; | 233 LOG(FATAL) << "Unknown type: " << type; |
(...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
683 | 692 |
684 void MessageWriter::AppendVariantOfBasic(int dbus_type, const void* value) { | 693 void MessageWriter::AppendVariantOfBasic(int dbus_type, const void* value) { |
685 const std::string signature = base::StringPrintf("%c", dbus_type); | 694 const std::string signature = base::StringPrintf("%c", dbus_type); |
686 MessageWriter variant_writer(message_); | 695 MessageWriter variant_writer(message_); |
687 OpenVariant(signature, &variant_writer); | 696 OpenVariant(signature, &variant_writer); |
688 variant_writer.AppendBasic(dbus_type, value); | 697 variant_writer.AppendBasic(dbus_type, value); |
689 CloseContainer(&variant_writer); | 698 CloseContainer(&variant_writer); |
690 } | 699 } |
691 | 700 |
692 void MessageWriter::AppendFileDescriptor(const FileDescriptor& value) { | 701 void MessageWriter::AppendFileDescriptor(const FileDescriptor& value) { |
693 CHECK(kDBusTypeUnixFdIsSupported); | 702 CHECK(IsDBusTypeUnixFdSupported()); |
694 | 703 |
695 if (!value.is_valid()) { | 704 if (!value.is_valid()) { |
696 // NB: sending a directory potentially enables sandbox escape | 705 // NB: sending a directory potentially enables sandbox escape |
697 LOG(FATAL) << "Attempt to pass invalid file descriptor"; | 706 LOG(FATAL) << "Attempt to pass invalid file descriptor"; |
698 } | 707 } |
699 int fd = value.value(); | 708 int fd = value.value(); |
700 AppendBasic(DBUS_TYPE_UNIX_FD, &fd); | 709 AppendBasic(DBUS_TYPE_UNIX_FD, &fd); |
701 } | 710 } |
702 | 711 |
703 // | 712 // |
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
953 } | 962 } |
954 | 963 |
955 bool MessageReader::PopVariantOfBasic(int dbus_type, void* value) { | 964 bool MessageReader::PopVariantOfBasic(int dbus_type, void* value) { |
956 dbus::MessageReader variant_reader(message_); | 965 dbus::MessageReader variant_reader(message_); |
957 if (!PopVariant(&variant_reader)) | 966 if (!PopVariant(&variant_reader)) |
958 return false; | 967 return false; |
959 return variant_reader.PopBasic(dbus_type, value); | 968 return variant_reader.PopBasic(dbus_type, value); |
960 } | 969 } |
961 | 970 |
962 bool MessageReader::PopFileDescriptor(FileDescriptor* value) { | 971 bool MessageReader::PopFileDescriptor(FileDescriptor* value) { |
963 CHECK(kDBusTypeUnixFdIsSupported); | 972 CHECK(IsDBusTypeUnixFdSupported()); |
964 | 973 |
965 int fd = -1; | 974 int fd = -1; |
966 const bool success = PopBasic(DBUS_TYPE_UNIX_FD, &fd); | 975 const bool success = PopBasic(DBUS_TYPE_UNIX_FD, &fd); |
967 if (!success) | 976 if (!success) |
968 return false; | 977 return false; |
969 | 978 |
970 value->PutValue(fd); | 979 value->PutValue(fd); |
971 // NB: the caller must check validity before using the value | 980 // NB: the caller must check validity before using the value |
972 return true; | 981 return true; |
973 } | 982 } |
974 | 983 |
975 } // namespace dbus | 984 } // namespace dbus |
OLD | NEW |