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

Side by Side Diff: dbus/message.cc

Issue 10815083: Make dbus file descriptor check dynamic (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: use && Created 8 years, 5 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 | Annotate | Revision Log
« no previous file with comments | « dbus/message.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/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 bool IsDBusTypeUnixFdSupported() {
satorux1 2012/07/24 21:49:08 It's not obvious why we should check this run time
41 int major, minor, micro;
satorux1 2012/07/24 21:49:08 nit: initialize them with 0 just in case.
42 dbus_get_version(&major, &minor, &micro);
43 return major >= 1 && minor >= 4;
44 }
45
40 } // namespace 46 } // namespace
41 47
42 namespace dbus { 48 namespace dbus {
43 49
44 Message::Message() 50 Message::Message()
45 : raw_message_(NULL) { 51 : raw_message_(NULL) {
46 } 52 }
47 53
48 Message::~Message() { 54 Message::~Message() {
49 if (raw_message_) 55 if (raw_message_)
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 } 210 }
205 case VARIANT: { 211 case VARIANT: {
206 MessageReader sub_reader(this); 212 MessageReader sub_reader(this);
207 if (!reader->PopVariant(&sub_reader)) 213 if (!reader->PopVariant(&sub_reader))
208 return kBrokenMessage; 214 return kBrokenMessage;
209 output += indent + "variant "; 215 output += indent + "variant ";
210 output += ToStringInternal(indent + " ", &sub_reader); 216 output += ToStringInternal(indent + " ", &sub_reader);
211 break; 217 break;
212 } 218 }
213 case UNIX_FD: { 219 case UNIX_FD: {
214 CHECK(kDBusTypeUnixFdIsSupported); 220 CHECK(IsDBusTypeUnixFdSupported());
215 221
216 FileDescriptor file_descriptor; 222 FileDescriptor file_descriptor;
217 if (!reader->PopFileDescriptor(&file_descriptor)) 223 if (!reader->PopFileDescriptor(&file_descriptor))
218 return kBrokenMessage; 224 return kBrokenMessage;
219 output += indent + "fd#" + 225 output += indent + "fd#" +
220 base::StringPrintf("%d", file_descriptor.value()) + "\n"; 226 base::StringPrintf("%d", file_descriptor.value()) + "\n";
221 break; 227 break;
222 } 228 }
223 default: 229 default:
224 LOG(FATAL) << "Unknown type: " << type; 230 LOG(FATAL) << "Unknown type: " << type;
(...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after
683 689
684 void MessageWriter::AppendVariantOfBasic(int dbus_type, const void* value) { 690 void MessageWriter::AppendVariantOfBasic(int dbus_type, const void* value) {
685 const std::string signature = base::StringPrintf("%c", dbus_type); 691 const std::string signature = base::StringPrintf("%c", dbus_type);
686 MessageWriter variant_writer(message_); 692 MessageWriter variant_writer(message_);
687 OpenVariant(signature, &variant_writer); 693 OpenVariant(signature, &variant_writer);
688 variant_writer.AppendBasic(dbus_type, value); 694 variant_writer.AppendBasic(dbus_type, value);
689 CloseContainer(&variant_writer); 695 CloseContainer(&variant_writer);
690 } 696 }
691 697
692 void MessageWriter::AppendFileDescriptor(const FileDescriptor& value) { 698 void MessageWriter::AppendFileDescriptor(const FileDescriptor& value) {
693 CHECK(kDBusTypeUnixFdIsSupported); 699 CHECK(IsDBusTypeUnixFdSupported());
694 700
695 if (!value.is_valid()) { 701 if (!value.is_valid()) {
696 // NB: sending a directory potentially enables sandbox escape 702 // NB: sending a directory potentially enables sandbox escape
697 LOG(FATAL) << "Attempt to pass invalid file descriptor"; 703 LOG(FATAL) << "Attempt to pass invalid file descriptor";
698 } 704 }
699 int fd = value.value(); 705 int fd = value.value();
700 AppendBasic(DBUS_TYPE_UNIX_FD, &fd); 706 AppendBasic(DBUS_TYPE_UNIX_FD, &fd);
701 } 707 }
702 708
703 // 709 //
(...skipping 249 matching lines...) Expand 10 before | Expand all | Expand 10 after
953 } 959 }
954 960
955 bool MessageReader::PopVariantOfBasic(int dbus_type, void* value) { 961 bool MessageReader::PopVariantOfBasic(int dbus_type, void* value) {
956 dbus::MessageReader variant_reader(message_); 962 dbus::MessageReader variant_reader(message_);
957 if (!PopVariant(&variant_reader)) 963 if (!PopVariant(&variant_reader))
958 return false; 964 return false;
959 return variant_reader.PopBasic(dbus_type, value); 965 return variant_reader.PopBasic(dbus_type, value);
960 } 966 }
961 967
962 bool MessageReader::PopFileDescriptor(FileDescriptor* value) { 968 bool MessageReader::PopFileDescriptor(FileDescriptor* value) {
963 CHECK(kDBusTypeUnixFdIsSupported); 969 CHECK(IsDBusTypeUnixFdSupported());
964 970
965 int fd = -1; 971 int fd = -1;
966 const bool success = PopBasic(DBUS_TYPE_UNIX_FD, &fd); 972 const bool success = PopBasic(DBUS_TYPE_UNIX_FD, &fd);
967 if (!success) 973 if (!success)
968 return false; 974 return false;
969 975
970 value->PutValue(fd); 976 value->PutValue(fd);
971 // NB: the caller must check validity before using the value 977 // NB: the caller must check validity before using the value
972 return true; 978 return true;
973 } 979 }
974 980
975 } // namespace dbus 981 } // namespace dbus
OLDNEW
« no previous file with comments | « dbus/message.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698