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/message_loop/message_loop_proxy.h" | |
| 9 #include "base/message_loop/message_pump_mac.h" | |
| 10 #include "base/posix/eintr_wrapper.h" | |
| 11 #include "chrome/common/extensions/image_writer_util_mac.h" | |
| 12 #include "chrome/utility/image_writer/disk_unmounter_mac.h" | |
| 13 #include "chrome/utility/image_writer/error_messages.h" | |
| 14 #include "chrome/utility/image_writer/image_writer.h" | |
| 15 | |
| 16 namespace image_writer { | |
| 17 | |
| 18 bool ImageWriter::IsValidDevice() { | |
| 19 base::ScopedCFTypeRef<DASessionRef> session(DASessionCreate(NULL)); | |
| 20 DADiskRef disk = DADiskCreateFromBSDName( | |
| 21 kCFAllocatorDefault, session, device_path_.value().c_str()); | |
| 22 | |
| 23 if (!disk) | |
| 24 return false; | |
| 25 | |
| 26 base::ScopedCFTypeRef<CFDictionaryRef> dict(DADiskCopyDescription(disk)); | |
| 27 | |
| 28 return extensions::image_writer::IsRemovableDevice(dict.get()); | |
| 29 } | |
| 30 | |
| 31 void ImageWriter::UnmountVolumes(const base::Closure& continuation) { | |
| 32 if (unmounter_ == NULL) { | |
| 33 unmounter_.reset(new DiskUnmounterMac()); | |
| 34 } | |
| 35 | |
| 36 unmounter_->Unmount( | |
| 37 device_path_.value(), | |
| 38 continuation, | |
| 39 base::Bind( | |
| 40 &ImageWriter::Error, base::Unretained(this), error::kUnmountVolumes)); | |
| 41 } | |
| 42 | |
| 43 bool ImageWriter::OpenDevice() { | |
| 44 int sockets[2]; // [parent's end, child's end] | |
| 45 int result = socketpair(AF_UNIX, SOCK_STREAM, 0, sockets); | |
| 46 if (result == -1) { | |
| 47 LOG(ERROR) << "Unable to allocate socket pair."; | |
| 48 return false; | |
| 49 } | |
| 50 | |
| 51 char rdwr[10]; | |
| 52 snprintf(rdwr, sizeof(rdwr), "%d", O_RDWR); | |
| 53 | |
| 54 pid_t childPid = fork(); | |
|
Robert Sesek
2014/06/10 19:47:46
This should probably use base/process/launch.h.
Drew Haven
2014/06/12 02:24:26
Done.
| |
| 55 if (childPid == -1) { | |
| 56 LOG(ERROR) << "Fork failed."; | |
| 57 return false; | |
| 58 } | |
| 59 | |
| 60 if (childPid == 0) { // child | |
| 61 HANDLE_EINTR(dup2(sockets[1], STDOUT_FILENO)); | |
| 62 close(sockets[0]); | |
| 63 close(sockets[1]); | |
| 64 | |
| 65 base::FilePath real_device_path; | |
| 66 if (device_path_.IsAbsolute()) { | |
| 67 real_device_path = device_path_; | |
| 68 } else { | |
| 69 real_device_path = base::FilePath("/dev").Append(device_path_); | |
|
Robert Sesek
2014/06/10 19:47:46
Yikes, this is going to do allocation between fork
Drew Haven
2014/06/12 02:24:26
With the switch to LaunchProcess I think we should
| |
| 70 } | |
| 71 | |
| 72 const char authopenPath[] = "/usr/libexec/authopen"; | |
| 73 execl(authopenPath, | |
| 74 authopenPath, | |
| 75 "-stdoutpipe", | |
| 76 "-o", | |
| 77 rdwr, | |
| 78 real_device_path.value().c_str(), | |
| 79 NULL); | |
| 80 _exit(errno); | |
| 81 } else { // parent | |
| 82 close(sockets[1]); | |
| 83 int fd = -1; | |
| 84 | |
| 85 msghdr message = {0}; | |
|
Robert Sesek
2014/06/10 19:47:47
What is this code supposed to do? Commentary defin
Drew Haven
2014/06/12 02:24:26
I commented it up a bit. I couldn't figure out a
| |
| 86 const size_t kDataBufferSize = 1024; | |
| 87 char dataBuffer[kDataBufferSize]; | |
| 88 iovec ioVec[1]; | |
| 89 ioVec[0].iov_base = dataBuffer; | |
| 90 ioVec[0].iov_len = kDataBufferSize; | |
| 91 message.msg_iov = ioVec; | |
| 92 message.msg_iovlen = 1; | |
| 93 const socklen_t kCmsgSocketSize = (socklen_t)CMSG_SPACE(sizeof(int)); | |
| 94 char cmsgSocket[kCmsgSocketSize]; | |
| 95 message.msg_control = cmsgSocket; | |
| 96 message.msg_controllen = kCmsgSocketSize; | |
| 97 ssize_t size = HANDLE_EINTR(recvmsg(sockets[0], &message, 0)); | |
| 98 if (size > 0) { | |
| 99 cmsghdr* cmsgSocketHeader = CMSG_FIRSTHDR(&message); | |
| 100 // Paranoia. | |
| 101 if (cmsgSocketHeader && cmsgSocketHeader->cmsg_level == SOL_SOCKET && | |
| 102 cmsgSocketHeader->cmsg_type == SCM_RIGHTS) | |
| 103 fd = *((int*)CMSG_DATA(cmsgSocketHeader)); | |
| 104 } | |
| 105 | |
| 106 int childStat; | |
| 107 result = HANDLE_EINTR(waitpid(childPid, &childStat, 0)); | |
| 108 close(sockets[0]); | |
| 109 | |
| 110 if (result != -1 && WIFEXITED(childStat)) { | |
| 111 int exitStatus = WEXITSTATUS(childStat); | |
| 112 if (exitStatus) { | |
| 113 LOG(ERROR) << "Child process returned failure."; | |
| 114 return false; | |
| 115 } | |
| 116 } | |
| 117 | |
| 118 device_file_ = base::File(fd); | |
| 119 | |
| 120 return device_file_.IsValid(); | |
| 121 } | |
| 122 } | |
| 123 | |
| 124 } // namespace image_writer | |
| OLD | NEW |