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.
5 #include <IOKit/storage/IOStorageProtocolCharacteristics.h>
6 #include <sys/socket.h>
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"
18 namespace image_writer {
20 static const char kAuthOpenPath[] = "/usr/libexec/authopen";
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()));
30 base::ScopedCFTypeRef<CFDictionaryRef> disk_description(
31 DADiskCopyDescription(disk));
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);
44 // A drive is valid if it is
49 // - it is of type IOMedia (external DVD drives and the like are IOCDMedia or
51 return CFBooleanGetValue(ejectable) && CFBooleanGetValue(removable) &&
52 CFBooleanGetValue(writable) && CFBooleanGetValue(whole) &&
53 CFStringCompare(kind, CFSTR("IOMedia"), 0) == kCFCompareEqualTo;
56 void ImageWriter::UnmountVolumes(const base::Closure& continuation) {
57 if (unmounter_ == NULL) {
58 unmounter_.reset(new DiskUnmounterMac());
65 &ImageWriter::Error, base::Unretained(this), error::kUnmountVolumes));
68 bool ImageWriter::OpenDevice() {
69 base::LaunchOptions options = base::LaunchOptions();
72 // Create a socket pair for communication.
74 int result = socketpair(AF_UNIX, SOCK_STREAM, 0, sockets);
76 PLOG(ERROR) << "Unable to allocate socket pair.";
79 base::ScopedFD parent_socket(sockets[0]);
80 base::ScopedFD child_socket(sockets[1]);
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;
87 // Find the file path to open.
88 base::FilePath real_device_path;
89 if (device_path_.IsAbsolute()) {
90 real_device_path = device_path_;
92 real_device_path = base::FilePath("/dev").Append(device_path_);
95 // Build the command line.
96 std::string rdwr = base::StringPrintf("%d", O_RDWR);
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);
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.";
112 // Receive a file descriptor from authopen which sends a single FD via
113 // sendmsg and the SCM_RIGHTS extension.
115 const size_t kDataBufferSize = sizeof(struct cmsghdr) + sizeof(int);
116 char data_buffer[kDataBufferSize];
118 struct iovec io_vec[1];
119 io_vec[0].iov_base = data_buffer;
120 io_vec[0].iov_len = kDataBufferSize;
122 const socklen_t kCmsgSocketSize =
123 static_cast<socklen_t>(CMSG_SPACE(sizeof(int)));
124 char cmsg_socket[kCmsgSocketSize];
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;
132 ssize_t size = HANDLE_EINTR(recvmsg(parent_socket.get(), &message, 0));
134 struct cmsghdr* cmsg_socket_header = CMSG_FIRSTHDR(&message);
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));
142 device_file_ = base::File(fd);
144 // Wait for the child.
145 int child_exit_status;
146 if (!base::WaitForExitCode(process_handle, &child_exit_status)) {
147 LOG(ERROR) << "Unable to wait for child.";
151 if (child_exit_status) {
152 LOG(ERROR) << "Child process returned failure.";
156 return device_file_.IsValid();
159 } // namespace image_writer