Chromium Code Reviews| 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 "tools/android/forwarder2/socket.h" | 5 #include "tools/android/forwarder2/socket.h" |
| 6 | 6 |
| 7 #include <arpa/inet.h> | 7 #include <arpa/inet.h> |
| 8 #include <errno.h> | 8 #include <errno.h> |
| 9 #include <fcntl.h> | 9 #include <fcntl.h> |
| 10 #include <netdb.h> | 10 #include <netdb.h> |
| (...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 287 | 287 |
| 288 int Socket::GetPort() { | 288 int Socket::GetPort() { |
| 289 if (!FamilyIsTCP(family_)) { | 289 if (!FamilyIsTCP(family_)) { |
| 290 LOG(ERROR) << "Can't call GetPort() on an unix domain socket."; | 290 LOG(ERROR) << "Can't call GetPort() on an unix domain socket."; |
| 291 return 0; | 291 return 0; |
| 292 } | 292 } |
| 293 return port_; | 293 return port_; |
| 294 } | 294 } |
| 295 | 295 |
| 296 int Socket::ReadNumBytes(void* buffer, size_t num_bytes) { | 296 int Socket::ReadNumBytes(void* buffer, size_t num_bytes) { |
| 297 return ReadNumBytesWithTimeout(buffer, num_bytes, kNoTimeout); | |
| 298 } | |
| 299 | |
| 300 int Socket::ReadNumBytesWithTimeout(void* buffer, | |
| 301 size_t num_bytes, | |
| 302 int timeout_secs) { | |
| 297 size_t bytes_read = 0; | 303 size_t bytes_read = 0; |
| 298 int ret = 1; | 304 int ret = 1; |
| 299 while (bytes_read < num_bytes && ret > 0) { | 305 while (bytes_read < num_bytes && ret > 0) { |
| 300 ret = Read(static_cast<char*>(buffer) + bytes_read, num_bytes - bytes_read); | 306 ret = ReadWithTimeout(static_cast<char*>(buffer) + bytes_read, |
| 307 num_bytes - bytes_read, timeout_secs); | |
| 301 if (ret >= 0) | 308 if (ret >= 0) |
| 302 bytes_read += ret; | 309 bytes_read += ret; |
| 303 } | 310 } |
| 304 return bytes_read; | 311 return bytes_read; |
| 305 } | 312 } |
| 306 | 313 |
| 307 void Socket::SetSocketError() { | 314 void Socket::SetSocketError() { |
| 308 socket_error_ = true; | 315 socket_error_ = true; |
| 309 DCHECK_NE(EAGAIN, errno); | 316 DCHECK_NE(EAGAIN, errno); |
| 310 DCHECK_NE(EWOULDBLOCK, errno); | 317 DCHECK_NE(EWOULDBLOCK, errno); |
| 311 Close(); | 318 Close(); |
| 312 } | 319 } |
| 313 | 320 |
| 314 int Socket::Read(void* buffer, size_t buffer_size) { | 321 int Socket::Read(void* buffer, size_t buffer_size) { |
| 315 if (!WaitForEvent(READ, kNoTimeout)) { | 322 return ReadWithTimeout(buffer, buffer_size, kNoTimeout); |
| 323 } | |
| 324 | |
| 325 int Socket::ReadWithTimeout(void* buffer, | |
| 326 size_t buffer_size, | |
| 327 int timeout_secs) { | |
| 328 if (!WaitForEvent(READ, timeout_secs)) { | |
| 316 SetSocketError(); | 329 SetSocketError(); |
| 317 return 0; | 330 return 0; |
| 318 } | 331 } |
| 319 int ret = HANDLE_EINTR(read(socket_, buffer, buffer_size)); | 332 int ret = HANDLE_EINTR(read(socket_, buffer, buffer_size)); |
| 320 if (ret < 0) { | 333 if (ret < 0) { |
| 321 PLOG(ERROR) << "read"; | 334 PLOG(ERROR) << "read"; |
| 322 SetSocketError(); | 335 SetSocketError(); |
| 323 } | 336 } |
| 324 return ret; | 337 return ret; |
| 325 } | 338 } |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 412 timeval* tv_ptr = NULL; | 425 timeval* tv_ptr = NULL; |
| 413 if (timeout_secs > 0) { | 426 if (timeout_secs > 0) { |
| 414 tv.tv_sec = timeout_secs; | 427 tv.tv_sec = timeout_secs; |
| 415 tv.tv_usec = 0; | 428 tv.tv_usec = 0; |
| 416 tv_ptr = &tv; | 429 tv_ptr = &tv; |
| 417 } | 430 } |
| 418 int max_fd = socket_; | 431 int max_fd = socket_; |
| 419 for (size_t i = 0; i < events_.size(); ++i) | 432 for (size_t i = 0; i < events_.size(); ++i) |
| 420 if (events_[i].fd > max_fd) | 433 if (events_[i].fd > max_fd) |
| 421 max_fd = events_[i].fd; | 434 max_fd = events_[i].fd; |
| 422 if (HANDLE_EINTR( | 435 if (HANDLE_EINTR(select(max_fd + 1, &read_fds, &write_fds, NULL, tv_ptr)) < |
|
Ted C
2016/10/08 00:16:32
i think the line wrapping before was easier to gro
jbudorick
2016/10/08 00:30:58
as did I, but git cl format disagreed.
| |
| 423 select(max_fd + 1, &read_fds, &write_fds, NULL, tv_ptr)) <= 0) { | 436 0) { |
| 424 PLOG(ERROR) << "select"; | 437 PLOG(ERROR) << "select"; |
| 425 return false; | 438 return false; |
| 426 } | 439 } |
| 427 bool event_was_fired = false; | 440 bool event_was_fired = false; |
| 428 for (size_t i = 0; i < events_.size(); ++i) { | 441 for (size_t i = 0; i < events_.size(); ++i) { |
| 429 if (FD_ISSET(events_[i].fd, &read_fds)) { | 442 if (FD_ISSET(events_[i].fd, &read_fds)) { |
| 430 events_[i].was_fired = true; | 443 events_[i].was_fired = true; |
| 431 event_was_fired = true; | 444 event_was_fired = true; |
| 432 } | 445 } |
| 433 } | 446 } |
| 434 return !event_was_fired; | 447 return !event_was_fired; |
| 435 } | 448 } |
| 436 | 449 |
| 437 // static | 450 // static |
| 438 pid_t Socket::GetUnixDomainSocketProcessOwner(const std::string& path) { | 451 pid_t Socket::GetUnixDomainSocketProcessOwner(const std::string& path) { |
| 439 Socket socket; | 452 Socket socket; |
| 440 if (!socket.ConnectUnix(path)) | 453 if (!socket.ConnectUnix(path)) |
| 441 return -1; | 454 return -1; |
| 442 ucred ucred; | 455 ucred ucred; |
| 443 socklen_t len = sizeof(ucred); | 456 socklen_t len = sizeof(ucred); |
| 444 if (getsockopt(socket.socket_, SOL_SOCKET, SO_PEERCRED, &ucred, &len) == -1) { | 457 if (getsockopt(socket.socket_, SOL_SOCKET, SO_PEERCRED, &ucred, &len) == -1) { |
| 445 CHECK_NE(ENOPROTOOPT, errno); | 458 CHECK_NE(ENOPROTOOPT, errno); |
| 446 return -1; | 459 return -1; |
| 447 } | 460 } |
| 448 return ucred.pid; | 461 return ucred.pid; |
| 449 } | 462 } |
| 450 | 463 |
| 451 } // namespace forwarder2 | 464 } // namespace forwarder2 |
| OLD | NEW |