Chromium Code Reviews| 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 <sys/socket.h> | |
| 6 #include <IOKit/storage/IOStorageProtocolCharacteristics.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"; | |
|
Robert Sesek
2014/06/12 22:01:34
Use const char kAuthOpenPath[] = "..."; instead to
Drew Haven
2014/06/12 22:55:10
Done.
| |
| 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 LOG(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( | |
| 85 std::pair<int, int>(child_socket.get(), STDOUT_FILENO)); | |
| 86 options.fds_to_remap = &fd_map; | |
| 87 | |
| 88 // Find the file path to open. | |
| 89 base::FilePath real_device_path; | |
| 90 if (device_path_.IsAbsolute()) { | |
| 91 real_device_path = device_path_; | |
| 92 } else { | |
| 93 real_device_path = base::FilePath("/dev").Append(device_path_); | |
| 94 } | |
| 95 | |
| 96 // Build the command line. | |
| 97 std::string rdwr = base::StringPrintf("%d", O_RDWR); | |
| 98 | |
| 99 base::CommandLine cmd_line((base::FilePath(kAuthOpenPath))); | |
|
Robert Sesek
2014/06/12 22:01:34
I have two minds about this authopen stuff. On the
Robert Sesek
2014/06/12 22:01:35
nit: double parens around this
Drew Haven
2014/06/12 22:55:10
That was actually prompted by the compiler because
Drew Haven
2014/06/12 22:55:10
At this point I'd like to just go with this implem
Robert Sesek
2014/06/16 19:32:53
Yes, double parens look weird here. Use assignment
| |
| 100 cmd_line.AppendSwitch("-stdoutpipe"); | |
| 101 // Using AppendSwitchNative will use an equal-symbol which we don't want. | |
| 102 cmd_line.AppendArg("-o"); | |
| 103 cmd_line.AppendArg(rdwr); | |
| 104 cmd_line.AppendArgPath(real_device_path); | |
| 105 | |
| 106 // Launch the process. | |
| 107 base::ProcessHandle process_handle; | |
| 108 if (!base::LaunchProcess(cmd_line, options, &process_handle)) { | |
| 109 LOG(ERROR) << "Failed to launch authopen process."; | |
| 110 // TODO: close stuff. | |
|
Robert Sesek
2014/06/12 22:01:34
This seems like a rather important TODO.
Drew Haven
2014/06/12 22:55:10
ha, oops. I did it by making sure everything was
| |
| 111 return false; | |
| 112 } | |
| 113 | |
| 114 // Receive a file descriptor from authopen using sndmsg and the SCM_RIGHTS | |
| 115 // extension. |IPC::Channel| is built around |IPC::Listener|s, and it's | |
|
Robert Sesek
2014/06/12 22:01:35
Well, that and the man page for `authopen` explici
Drew Haven
2014/06/12 22:55:10
Done.
| |
| 116 // simpler to | |
| 117 // just process one message off the socket. | |
| 118 int fd = -1; | |
| 119 msghdr message = {0}; | |
| 120 const size_t kDataBufferSize = 1024; | |
| 121 char dataBuffer[kDataBufferSize]; | |
|
Robert Sesek
2014/06/12 22:01:35
Naming: use under_scores throughout this function.
Drew Haven
2014/06/12 22:55:10
Whoops, copy-paste.
| |
| 122 iovec ioVec[1]; | |
| 123 ioVec[0].iov_base = dataBuffer; | |
| 124 ioVec[0].iov_len = kDataBufferSize; | |
| 125 message.msg_iov = ioVec; | |
| 126 message.msg_iovlen = 1; | |
|
Robert Sesek
2014/06/12 22:01:35
sizeof(ioVec)
Drew Haven
2014/06/12 22:55:10
Done.
| |
| 127 const socklen_t kCmsgSocketSize = (socklen_t)CMSG_SPACE(sizeof(int)); | |
|
Robert Sesek
2014/06/12 22:01:34
C style casts are banned, and below.
Drew Haven
2014/06/12 22:55:10
Done.
| |
| 128 char cmsgSocket[kCmsgSocketSize]; | |
| 129 message.msg_control = cmsgSocket; | |
| 130 message.msg_controllen = kCmsgSocketSize; | |
| 131 ssize_t size = | |
| 132 HANDLE_EINTR(recvmsg(parent_socket.get(), &message, 0)); | |
| 133 if (size > 0) { | |
| 134 cmsghdr* cmsgSocketHeader = CMSG_FIRSTHDR(&message); | |
| 135 | |
| 136 if (cmsgSocketHeader && cmsgSocketHeader->cmsg_level == SOL_SOCKET && | |
| 137 cmsgSocketHeader->cmsg_type == SCM_RIGHTS) | |
| 138 fd = *((int*)CMSG_DATA(cmsgSocketHeader)); | |
| 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 |