Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(451)

Side by Side Diff: dbus/message.cc

Issue 9700072: dbus: add support for passing file descriptors (Closed) Base URL: http://git.chromium.org/git/chromium/src@master
Patch Set: fix unit test to not run where fd-passing support is missing Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/platform_file.h"
12 #include "base/stringprintf.h" 13 #include "base/stringprintf.h"
13 #include "dbus/object_path.h" 14 #include "dbus/object_path.h"
14 #include "third_party/protobuf/src/google/protobuf/message_lite.h" 15 #include "third_party/protobuf/src/google/protobuf/message_lite.h"
15 16
16 namespace { 17 namespace {
17 18
18 // 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
19 // not empty. 20 // not empty.
20 static void AppendStringHeader(const std::string& header_name, 21 static void AppendStringHeader(const std::string& header_name,
21 const std::string& header_value, 22 const std::string& header_value,
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
192 break; 193 break;
193 } 194 }
194 case VARIANT: { 195 case VARIANT: {
195 MessageReader sub_reader(this); 196 MessageReader sub_reader(this);
196 if (!reader->PopVariant(&sub_reader)) 197 if (!reader->PopVariant(&sub_reader))
197 return kBrokenMessage; 198 return kBrokenMessage;
198 output += indent + "variant "; 199 output += indent + "variant ";
199 output += ToStringInternal(indent + " ", &sub_reader); 200 output += ToStringInternal(indent + " ", &sub_reader);
200 break; 201 break;
201 } 202 }
203 case UNIX_FD: {
204 CHECK(kDBusTypeUnixFdIsSupported);
205
206 FileDescriptor value;
satorux1 2012/03/28 20:49:37 nit: value -> file_descriptor?
Sam Leffler 2012/03/28 22:06:33 Done.
207 if (!reader->PopFileDescriptor(&value))
208 return kBrokenMessage;
209 output += indent + "fd#" +
210 base::StringPrintf("%u", value.value()) + "\n";
satorux1 2012/03/28 20:49:37 nit: %d? fd is an int.
Sam Leffler 2012/03/28 22:06:33 Done.
211 break;
212 }
202 default: 213 default:
203 LOG(FATAL) << "Unknown type: " << type; 214 LOG(FATAL) << "Unknown type: " << type;
204 } 215 }
205 } 216 }
206 return output; 217 return output;
207 } 218 }
208 219
209 // The returned string consists of message headers such as 220 // The returned string consists of message headers such as
210 // destination if any, followed by a blank line, and the message 221 // destination if any, followed by a blank line, and the message
211 // payload. For example, a MethodCall's ToString() will look like: 222 // payload. For example, a MethodCall's ToString() will look like:
(...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after
670 } 681 }
671 682
672 void MessageWriter::AppendVariantOfBasic(int dbus_type, const void* value) { 683 void MessageWriter::AppendVariantOfBasic(int dbus_type, const void* value) {
673 const std::string signature = base::StringPrintf("%c", dbus_type); 684 const std::string signature = base::StringPrintf("%c", dbus_type);
674 MessageWriter variant_writer(message_); 685 MessageWriter variant_writer(message_);
675 OpenVariant(signature, &variant_writer); 686 OpenVariant(signature, &variant_writer);
676 variant_writer.AppendBasic(dbus_type, value); 687 variant_writer.AppendBasic(dbus_type, value);
677 CloseContainer(&variant_writer); 688 CloseContainer(&variant_writer);
678 } 689 }
679 690
691 void MessageWriter::AppendFileDescriptor(const FileDescriptor& value) {
692 CHECK(kDBusTypeUnixFdIsSupported);
693
694 base::PlatformFileInfo info;
695 int fd = value.value();
696 bool ok = base::GetPlatformFileInfo(fd, &info);
697 if (!ok || info.is_directory)
satorux1 2012/03/28 20:49:37 These are different errors. !ok means it just fail
Jorge Lucangeli Obes 2012/03/28 21:33:37 And in this case, we can display an error message
Sam Leffler 2012/03/28 22:06:33 Yes they are different errors; the message is inte
698 LOG(FATAL) << "Attempt to pass invalid file descriptor";
satorux1 2012/03/28 20:49:37 Please add some comment why we are disallowing the
Sam Leffler 2012/03/28 22:06:33 Done.
699 AppendBasic(DBUS_TYPE_UNIX_FD, &fd);
700 }
701
680 // 702 //
681 // MessageReader implementation. 703 // MessageReader implementation.
682 // 704 //
683 705
684 MessageReader::MessageReader(Message* message) 706 MessageReader::MessageReader(Message* message)
685 : message_(message) { 707 : message_(message) {
686 memset(&raw_message_iter_, 0, sizeof(raw_message_iter_)); 708 memset(&raw_message_iter_, 0, sizeof(raw_message_iter_));
687 if (message) 709 if (message)
688 dbus_message_iter_init(message_->raw_message(), &raw_message_iter_); 710 dbus_message_iter_init(message_->raw_message(), &raw_message_iter_);
689 } 711 }
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after
929 return true; 951 return true;
930 } 952 }
931 953
932 bool MessageReader::PopVariantOfBasic(int dbus_type, void* value) { 954 bool MessageReader::PopVariantOfBasic(int dbus_type, void* value) {
933 dbus::MessageReader variant_reader(message_); 955 dbus::MessageReader variant_reader(message_);
934 if (!PopVariant(&variant_reader)) 956 if (!PopVariant(&variant_reader))
935 return false; 957 return false;
936 return variant_reader.PopBasic(dbus_type, value); 958 return variant_reader.PopBasic(dbus_type, value);
937 } 959 }
938 960
961 bool MessageReader::PopFileDescriptor(FileDescriptor* value) {
962 CHECK(kDBusTypeUnixFdIsSupported);
963
964 int fd;
satorux1 2012/03/28 20:49:37 nit: initialize it to -1 to be extra defensive.
Sam Leffler 2012/03/28 22:06:33 Done.
965 const bool success = PopBasic(DBUS_TYPE_UNIX_FD, &fd);
966 if (!success)
967 return false;
968
969 base::PlatformFileInfo info;
970 bool ok = base::GetPlatformFileInfo(fd, &info);
971 if (!ok || info.is_directory) {
satorux1 2012/03/28 20:49:37 ditto. these are different errors.
Sam Leffler 2012/03/28 22:06:33 see above
972 base::ClosePlatformFile(fd);
973 LOG(FATAL) << "Attempt to receive invalid file descriptor";
974 return false; // NB: not reached
975 }
976 value->PutValue(fd);
977 return true;
978 }
979
939 } // namespace dbus 980 } // namespace dbus
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698