OLD | NEW |
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 "chromeos/process_proxy/process_output_watcher.h" | 5 #include "chromeos/process_proxy/process_output_watcher.h" |
6 | 6 |
7 #include <sys/ioctl.h> | 7 #include <sys/ioctl.h> |
8 #include <sys/select.h> | 8 #include <sys/select.h> |
9 #include <unistd.h> | 9 #include <unistd.h> |
10 | 10 |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 mask >>= 1; | 48 mask >>= 1; |
49 ++byte_count; | 49 ++byte_count; |
50 } | 50 } |
51 return byte_count ? byte_count : 1; | 51 return byte_count ? byte_count : 1; |
52 } | 52 } |
53 | 53 |
54 } // namespace | 54 } // namespace |
55 | 55 |
56 namespace chromeos { | 56 namespace chromeos { |
57 | 57 |
| 58 // static |
| 59 bool ProcessOutputWatcher::VerifyFileDescriptor(int fd) { |
| 60 return (fd >= 0) && (fd < FD_SETSIZE); |
| 61 } |
| 62 |
58 ProcessOutputWatcher::ProcessOutputWatcher( | 63 ProcessOutputWatcher::ProcessOutputWatcher( |
59 int out_fd, | 64 int out_fd, |
60 int stop_fd, | 65 int stop_fd, |
61 const ProcessOutputCallback& callback) | 66 const ProcessOutputCallback& callback) |
62 : read_buffer_size_(0), | 67 : read_buffer_size_(0), |
63 out_fd_(out_fd), | 68 out_fd_(out_fd), |
64 stop_fd_(stop_fd), | 69 stop_fd_(stop_fd), |
65 on_read_callback_(callback) { | 70 on_read_callback_(callback) { |
66 VerifyFileDescriptor(out_fd_); | 71 CHECK(VerifyFileDescriptor(out_fd_)); |
67 VerifyFileDescriptor(stop_fd_); | 72 CHECK(VerifyFileDescriptor(stop_fd_)); |
68 max_fd_ = std::max(out_fd_, stop_fd_); | 73 max_fd_ = std::max(out_fd_, stop_fd_); |
69 // We want to be sure we will be able to add 0 at the end of the input, so -1. | 74 // We want to be sure we will be able to add 0 at the end of the input, so -1. |
70 read_buffer_capacity_ = arraysize(read_buffer_) - 1; | 75 read_buffer_capacity_ = arraysize(read_buffer_) - 1; |
71 } | 76 } |
72 | 77 |
73 void ProcessOutputWatcher::Start() { | 78 void ProcessOutputWatcher::Start() { |
74 WatchProcessOutput(); | 79 WatchProcessOutput(); |
75 OnStop(); | 80 OnStop(); |
76 } | 81 } |
77 | 82 |
(...skipping 21 matching lines...) Expand all Loading... |
99 if (FD_ISSET(stop_fd_, &rfds)) { | 104 if (FD_ISSET(stop_fd_, &rfds)) { |
100 return; | 105 return; |
101 } | 106 } |
102 | 107 |
103 if (out_fd_ != -1 && FD_ISSET(out_fd_, &rfds)) { | 108 if (out_fd_ != -1 && FD_ISSET(out_fd_, &rfds)) { |
104 ReadFromFd(PROCESS_OUTPUT_TYPE_OUT, &out_fd_); | 109 ReadFromFd(PROCESS_OUTPUT_TYPE_OUT, &out_fd_); |
105 } | 110 } |
106 } | 111 } |
107 } | 112 } |
108 | 113 |
109 void ProcessOutputWatcher::VerifyFileDescriptor(int fd) { | |
110 CHECK_LE(0, fd); | |
111 CHECK_GT(FD_SETSIZE, fd); | |
112 } | |
113 | |
114 void ProcessOutputWatcher::ReadFromFd(ProcessOutputType type, int* fd) { | 114 void ProcessOutputWatcher::ReadFromFd(ProcessOutputType type, int* fd) { |
115 // We don't want to necessary read pipe until it is empty so we don't starve | 115 // We don't want to necessary read pipe until it is empty so we don't starve |
116 // other streams in case data is written faster than we read it. If there is | 116 // other streams in case data is written faster than we read it. If there is |
117 // more than read_buffer_size_ bytes in pipe, it will be read in the next | 117 // more than read_buffer_size_ bytes in pipe, it will be read in the next |
118 // iteration. | 118 // iteration. |
119 DCHECK_GT(read_buffer_capacity_, read_buffer_size_); | 119 DCHECK_GT(read_buffer_capacity_, read_buffer_size_); |
120 ssize_t bytes_read = | 120 ssize_t bytes_read = |
121 HANDLE_EINTR(read(*fd, | 121 HANDLE_EINTR(read(*fd, |
122 &read_buffer_[read_buffer_size_], | 122 &read_buffer_[read_buffer_size_], |
123 read_buffer_capacity_ - read_buffer_size_)); | 123 read_buffer_capacity_ - read_buffer_size_)); |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 } | 189 } |
190 } | 190 } |
191 read_buffer_size_ -= output_to_report; | 191 read_buffer_size_ -= output_to_report; |
192 } | 192 } |
193 | 193 |
194 void ProcessOutputWatcher::OnStop() { | 194 void ProcessOutputWatcher::OnStop() { |
195 delete this; | 195 delete this; |
196 } | 196 } |
197 | 197 |
198 } // namespace chromeos | 198 } // namespace chromeos |
OLD | NEW |