OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include <IOKit/storage/IOStorageProtocolCharacteristics.h> | |
6 #include <sys/socket.h> | |
7 | |
8 #include "base/command_line.h" | |
9 #include "base/files/scoped_file.h" | |
10 #include "base/posix/eintr_wrapper.h" | |
11 #include "base/process/kill.h" | |
12 #include "base/process/launch.h" | |
13 #include "base/strings/stringprintf.h" | |
14 #include "chrome/utility/image_writer/disk_unmounter_mac.h" | |
15 #include "chrome/utility/image_writer/error_messages.h" | |
16 #include "chrome/utility/image_writer/image_writer.h" | |
17 | |
18 namespace image_writer { | |
19 | |
20 static const char kAuthOpenPath[] = "/usr/libexec/authopen"; | |
21 | |
22 bool ImageWriter::IsValidDevice() { | |
23 base::ScopedCFTypeRef<DASessionRef> session(DASessionCreate(NULL)); | |
24 base::ScopedCFTypeRef<DADiskRef> disk(DADiskCreateFromBSDName( | |
25 kCFAllocatorDefault, session, device_path_.value().c_str())); | |
26 | |
27 if (!disk) | |
28 return false; | |
29 | |
30 base::ScopedCFTypeRef<CFDictionaryRef> disk_description( | |
31 DADiskCopyDescription(disk)); | |
32 | |
33 CFBooleanRef ejectable = base::mac::GetValueFromDictionary<CFBooleanRef>( | |
34 disk_description, kDADiskDescriptionMediaEjectableKey); | |
35 CFBooleanRef removable = base::mac::GetValueFromDictionary<CFBooleanRef>( | |
36 disk_description, kDADiskDescriptionMediaRemovableKey); | |
37 CFBooleanRef writable = base::mac::GetValueFromDictionary<CFBooleanRef>( | |
38 disk_description, kDADiskDescriptionMediaWritableKey); | |
39 CFBooleanRef whole = base::mac::GetValueFromDictionary<CFBooleanRef>( | |
40 disk_description, kDADiskDescriptionMediaWholeKey); | |
41 CFStringRef kind = base::mac::GetValueFromDictionary<CFStringRef>( | |
42 disk_description, kDADiskDescriptionMediaKindKey); | |
43 | |
44 // A drive is valid if it is | |
45 // - ejectable | |
46 // - removable | |
47 // - writable | |
48 // - a whole drive | |
49 // - it is of type IOMedia (external DVD drives and the like are IOCDMedia or | |
50 // IODVDMedia) | |
51 return CFBooleanGetValue(ejectable) && CFBooleanGetValue(removable) && | |
52 CFBooleanGetValue(writable) && CFBooleanGetValue(whole) && | |
53 CFStringCompare(kind, CFSTR("IOMedia"), 0) == kCFCompareEqualTo; | |
54 } | |
55 | |
56 void ImageWriter::UnmountVolumes(const base::Closure& continuation) { | |
57 if (unmounter_ == NULL) { | |
58 unmounter_.reset(new DiskUnmounterMac()); | |
59 } | |
60 | |
61 unmounter_->Unmount( | |
62 device_path_.value(), | |
63 continuation, | |
64 base::Bind( | |
65 &ImageWriter::Error, base::Unretained(this), error::kUnmountVolumes)); | |
66 } | |
67 | |
68 bool ImageWriter::OpenDevice() { | |
69 base::LaunchOptions options = base::LaunchOptions(); | |
70 options.wait = false; | |
71 | |
72 // Create a socket pair for communication. | |
73 int sockets[2]; | |
74 int result = socketpair(AF_UNIX, SOCK_STREAM, 0, sockets); | |
75 if (result == -1) { | |
76 PLOG(ERROR) << "Unable to allocate socket pair."; | |
77 return false; | |
78 } | |
79 base::ScopedFD parent_socket(sockets[0]); | |
80 base::ScopedFD child_socket(sockets[1]); | |
81 | |
82 // Map the client socket to the client's STDOUT. | |
83 base::FileHandleMappingVector fd_map; | |
84 fd_map.push_back(std::pair<int, int>(child_socket.get(), STDOUT_FILENO)); | |
85 options.fds_to_remap = &fd_map; | |
86 | |
87 // Find the file path to open. | |
88 base::FilePath real_device_path; | |
89 if (device_path_.IsAbsolute()) { | |
90 real_device_path = device_path_; | |
91 } else { | |
92 real_device_path = base::FilePath("/dev").Append(device_path_); | |
93 } | |
94 | |
95 // Build the command line. | |
96 std::string rdwr = base::StringPrintf("%d", O_RDWR); | |
97 | |
98 base::CommandLine cmd_line = base::CommandLine(base::FilePath(kAuthOpenPath)); | |
99 cmd_line.AppendSwitch("-stdoutpipe"); | |
100 // Using AppendSwitchNative will use an equal-symbol which we don't want. | |
101 cmd_line.AppendArg("-o"); | |
102 cmd_line.AppendArg(rdwr); | |
103 cmd_line.AppendArgPath(real_device_path); | |
104 | |
105 // Launch the process. | |
106 base::ProcessHandle process_handle; | |
107 if (!base::LaunchProcess(cmd_line, options, &process_handle)) { | |
108 LOG(ERROR) << "Failed to launch authopen process."; | |
109 return false; | |
110 } | |
111 | |
112 // Receive a file descriptor from authopen which sends a single FD via | |
113 // sendmsg and the SCM_RIGHTS extension. | |
114 int fd = -1; | |
115 const size_t kDataBufferSize = sizeof(struct cmsghdr) + sizeof(int); | |
116 char data_buffer[kDataBufferSize]; | |
117 | |
118 struct iovec io_vec[1]; | |
119 io_vec[0].iov_base = data_buffer; | |
120 io_vec[0].iov_len = kDataBufferSize; | |
121 | |
122 const socklen_t kCmsgSocketSize = | |
123 static_cast<socklen_t>(CMSG_SPACE(sizeof(int))); | |
124 char cmsg_socket[kCmsgSocketSize]; | |
125 | |
126 struct msghdr message = {0}; | |
127 message.msg_iov = io_vec; | |
128 message.msg_iovlen = 1; | |
129 message.msg_control = cmsg_socket; | |
130 message.msg_controllen = kCmsgSocketSize; | |
131 | |
132 ssize_t size = HANDLE_EINTR(recvmsg(parent_socket.get(), &message, 0)); | |
133 if (size > 0) { | |
134 struct cmsghdr* cmsg_socket_header = CMSG_FIRSTHDR(&message); | |
135 | |
136 if (cmsg_socket_header && cmsg_socket_header->cmsg_level == SOL_SOCKET && | |
137 cmsg_socket_header->cmsg_type == SCM_RIGHTS) | |
138 fd = *reinterpret_cast<int*>(CMSG_DATA(cmsg_socket_header)); | |
Robert Sesek
2014/06/16 22:15:21
nit: braces needed around body since the condition
Drew Haven
2014/06/17 00:00:45
Done.
| |
139 } | |
140 | |
141 device_file_ = base::File(fd); | |
142 | |
143 // Wait for the child. | |
144 int child_exit_status; | |
145 if (!base::WaitForExitCode(process_handle, &child_exit_status)) { | |
146 LOG(ERROR) << "Unable to wait for child."; | |
147 return false; | |
148 } | |
149 | |
150 if (child_exit_status) { | |
151 LOG(ERROR) << "Child process returned failure."; | |
152 return false; | |
153 } | |
154 | |
155 return device_file_.IsValid(); | |
156 } | |
157 | |
158 } // namespace image_writer | |
OLD | NEW |