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/format_macros.h" | 9 #include "base/format_macros.h" |
10 #include "base/logging.h" | 10 #include "base/logging.h" |
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
215 MessageReader sub_reader(this); | 215 MessageReader sub_reader(this); |
216 if (!reader->PopVariant(&sub_reader)) | 216 if (!reader->PopVariant(&sub_reader)) |
217 return kBrokenMessage; | 217 return kBrokenMessage; |
218 output += indent + "variant "; | 218 output += indent + "variant "; |
219 output += ToStringInternal(indent + " ", &sub_reader); | 219 output += ToStringInternal(indent + " ", &sub_reader); |
220 break; | 220 break; |
221 } | 221 } |
222 case UNIX_FD: { | 222 case UNIX_FD: { |
223 CHECK(IsDBusTypeUnixFdSupported()); | 223 CHECK(IsDBusTypeUnixFdSupported()); |
224 | 224 |
225 FileDescriptor file_descriptor; | 225 base::ScopedFD file_descriptor; |
226 if (!reader->PopFileDescriptor(&file_descriptor)) | 226 if (!reader->PopFileDescriptor(&file_descriptor)) |
227 return kBrokenMessage; | 227 return kBrokenMessage; |
228 output += indent + "fd#" + | 228 output += indent + "fd#" + |
229 base::IntToString(file_descriptor.value()) + "\n"; | 229 base::IntToString(file_descriptor.get()) + "\n"; |
230 break; | 230 break; |
231 } | 231 } |
232 default: | 232 default: |
233 LOG(FATAL) << "Unknown type: " << type; | 233 LOG(FATAL) << "Unknown type: " << type; |
234 } | 234 } |
235 } | 235 } |
236 return output; | 236 return output; |
237 } | 237 } |
238 | 238 |
239 // The returned string consists of message headers such as | 239 // The returned string consists of message headers such as |
(...skipping 472 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
712 OpenVariant(signature, &variant_writer); | 712 OpenVariant(signature, &variant_writer); |
713 variant_writer.AppendBasic(dbus_type, value); | 713 variant_writer.AppendBasic(dbus_type, value); |
714 CloseContainer(&variant_writer); | 714 CloseContainer(&variant_writer); |
715 } | 715 } |
716 | 716 |
717 void MessageWriter::AppendFileDescriptor(int value) { | 717 void MessageWriter::AppendFileDescriptor(int value) { |
718 CHECK(IsDBusTypeUnixFdSupported()); | 718 CHECK(IsDBusTypeUnixFdSupported()); |
719 AppendBasic(DBUS_TYPE_UNIX_FD, &value); // This duplicates the FD. | 719 AppendBasic(DBUS_TYPE_UNIX_FD, &value); // This duplicates the FD. |
720 } | 720 } |
721 | 721 |
722 void MessageWriter::AppendFileDescriptor(const FileDescriptor& value) { | |
723 CHECK(IsDBusTypeUnixFdSupported()); | |
724 | |
725 if (!value.is_valid()) { | |
726 // NB: sending a directory potentially enables sandbox escape | |
727 LOG(FATAL) << "Attempt to pass invalid file descriptor"; | |
728 } | |
729 int fd = value.value(); | |
730 AppendBasic(DBUS_TYPE_UNIX_FD, &fd); | |
731 } | |
732 | |
733 // | 722 // |
734 // MessageReader implementation. | 723 // MessageReader implementation. |
735 // | 724 // |
736 | 725 |
737 MessageReader::MessageReader(Message* message) | 726 MessageReader::MessageReader(Message* message) |
738 : message_(message) { | 727 : message_(message) { |
739 memset(&raw_message_iter_, 0, sizeof(raw_message_iter_)); | 728 memset(&raw_message_iter_, 0, sizeof(raw_message_iter_)); |
740 if (message) | 729 if (message) |
741 dbus_message_iter_init(message_->raw_message(), &raw_message_iter_); | 730 dbus_message_iter_init(message_->raw_message(), &raw_message_iter_); |
742 } | 731 } |
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1026 | 1015 |
1027 int fd = -1; | 1016 int fd = -1; |
1028 const bool success = PopBasic(DBUS_TYPE_UNIX_FD, &fd); | 1017 const bool success = PopBasic(DBUS_TYPE_UNIX_FD, &fd); |
1029 if (!success) | 1018 if (!success) |
1030 return false; | 1019 return false; |
1031 | 1020 |
1032 *value = base::ScopedFD(fd); | 1021 *value = base::ScopedFD(fd); |
1033 return true; | 1022 return true; |
1034 } | 1023 } |
1035 | 1024 |
1036 bool MessageReader::PopFileDescriptor(FileDescriptor* value) { | |
1037 CHECK(IsDBusTypeUnixFdSupported()); | |
1038 | |
1039 int fd = -1; | |
1040 const bool success = PopBasic(DBUS_TYPE_UNIX_FD, &fd); | |
1041 if (!success) | |
1042 return false; | |
1043 | |
1044 value->PutValue(fd); | |
1045 // NB: the caller must check validity before using the value | |
1046 return true; | |
1047 } | |
1048 | |
1049 } // namespace dbus | 1025 } // namespace dbus |
OLD | NEW |