OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 // On Linux, when the user tries to launch a second copy of chrome, we check | 5 // On Linux, when the user tries to launch a second copy of chrome, we check |
6 // for a socket in the user's profile directory. If the socket file is open we | 6 // for a socket in the user's profile directory. If the socket file is open we |
7 // send a message to the first chrome browser process with the current | 7 // send a message to the first chrome browser process with the current |
8 // directory and second process command line flags. The second process then | 8 // directory and second process command line flags. The second process then |
9 // exits. | 9 // exits. |
10 // | 10 // |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
57 | 57 |
58 #include "base/base_paths.h" | 58 #include "base/base_paths.h" |
59 #include "base/bind.h" | 59 #include "base/bind.h" |
60 #include "base/command_line.h" | 60 #include "base/command_line.h" |
61 #include "base/files/file_descriptor_watcher_posix.h" | 61 #include "base/files/file_descriptor_watcher_posix.h" |
62 #include "base/files/file_path.h" | 62 #include "base/files/file_path.h" |
63 #include "base/files/file_util.h" | 63 #include "base/files/file_util.h" |
64 #include "base/location.h" | 64 #include "base/location.h" |
65 #include "base/logging.h" | 65 #include "base/logging.h" |
66 #include "base/macros.h" | 66 #include "base/macros.h" |
| 67 #include "base/memory/ptr_util.h" |
67 #include "base/memory/ref_counted.h" | 68 #include "base/memory/ref_counted.h" |
68 #include "base/message_loop/message_loop.h" | 69 #include "base/message_loop/message_loop.h" |
69 #include "base/metrics/histogram_macros.h" | 70 #include "base/metrics/histogram_macros.h" |
70 #include "base/path_service.h" | 71 #include "base/path_service.h" |
71 #include "base/posix/eintr_wrapper.h" | 72 #include "base/posix/eintr_wrapper.h" |
72 #include "base/posix/safe_strerror.h" | 73 #include "base/posix/safe_strerror.h" |
73 #include "base/rand_util.h" | 74 #include "base/rand_util.h" |
74 #include "base/sequenced_task_runner_helpers.h" | 75 #include "base/sequenced_task_runner_helpers.h" |
75 #include "base/single_thread_task_runner.h" | 76 #include "base/single_thread_task_runner.h" |
76 #include "base/single_thread_task_runner.h" | 77 #include "base/single_thread_task_runner.h" |
77 #include "base/stl_util.h" | |
78 #include "base/strings/string_number_conversions.h" | 78 #include "base/strings/string_number_conversions.h" |
79 #include "base/strings/string_split.h" | 79 #include "base/strings/string_split.h" |
80 #include "base/strings/string_util.h" | 80 #include "base/strings/string_util.h" |
81 #include "base/strings/stringprintf.h" | 81 #include "base/strings/stringprintf.h" |
82 #include "base/strings/sys_string_conversions.h" | 82 #include "base/strings/sys_string_conversions.h" |
83 #include "base/strings/utf_string_conversions.h" | 83 #include "base/strings/utf_string_conversions.h" |
84 #include "base/threading/platform_thread.h" | 84 #include "base/threading/platform_thread.h" |
85 #include "base/threading/thread_task_runner_handle.h" | 85 #include "base/threading/thread_task_runner_handle.h" |
86 #include "base/time/time.h" | 86 #include "base/time/time.h" |
87 #include "base/timer/timer.h" | 87 #include "base/timer/timer.h" |
(...skipping 451 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
539 void HandleMessage(const std::string& current_dir, | 539 void HandleMessage(const std::string& current_dir, |
540 const std::vector<std::string>& argv, | 540 const std::vector<std::string>& argv, |
541 SocketReader* reader); | 541 SocketReader* reader); |
542 | 542 |
543 private: | 543 private: |
544 friend struct BrowserThread::DeleteOnThread<BrowserThread::IO>; | 544 friend struct BrowserThread::DeleteOnThread<BrowserThread::IO>; |
545 friend class base::DeleteHelper<ProcessSingleton::LinuxWatcher>; | 545 friend class base::DeleteHelper<ProcessSingleton::LinuxWatcher>; |
546 | 546 |
547 ~LinuxWatcher() { | 547 ~LinuxWatcher() { |
548 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 548 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
549 base::STLDeleteElements(&readers_); | |
550 } | 549 } |
551 | 550 |
552 void OnSocketCanReadWithoutBlocking(int socket); | 551 void OnSocketCanReadWithoutBlocking(int socket); |
553 | 552 |
554 // Removes and deletes the SocketReader. | 553 // Removes and deletes the SocketReader. |
555 void RemoveSocketReader(SocketReader* reader); | 554 void RemoveSocketReader(SocketReader* reader); |
556 | 555 |
557 std::unique_ptr<base::FileDescriptorWatcher::Controller> socket_watcher_; | 556 std::unique_ptr<base::FileDescriptorWatcher::Controller> socket_watcher_; |
558 | 557 |
559 // A reference to the UI message loop (i.e., the message loop we were | 558 // A reference to the UI message loop (i.e., the message loop we were |
560 // constructed on). | 559 // constructed on). |
561 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; | 560 scoped_refptr<base::SingleThreadTaskRunner> ui_task_runner_; |
562 | 561 |
563 // The ProcessSingleton that owns us. | 562 // The ProcessSingleton that owns us. |
564 ProcessSingleton* const parent_; | 563 ProcessSingleton* const parent_; |
565 | 564 |
566 std::set<SocketReader*> readers_; | 565 std::set<std::unique_ptr<SocketReader>> readers_; |
567 | 566 |
568 DISALLOW_COPY_AND_ASSIGN(LinuxWatcher); | 567 DISALLOW_COPY_AND_ASSIGN(LinuxWatcher); |
569 }; | 568 }; |
570 | 569 |
571 void ProcessSingleton::LinuxWatcher::OnSocketCanReadWithoutBlocking( | 570 void ProcessSingleton::LinuxWatcher::OnSocketCanReadWithoutBlocking( |
572 int socket) { | 571 int socket) { |
573 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 572 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
574 // Accepting incoming client. | 573 // Accepting incoming client. |
575 sockaddr_un from; | 574 sockaddr_un from; |
576 socklen_t from_len = sizeof(from); | 575 socklen_t from_len = sizeof(from); |
577 int connection_socket = HANDLE_EINTR( | 576 int connection_socket = HANDLE_EINTR( |
578 accept(socket, reinterpret_cast<sockaddr*>(&from), &from_len)); | 577 accept(socket, reinterpret_cast<sockaddr*>(&from), &from_len)); |
579 if (-1 == connection_socket) { | 578 if (-1 == connection_socket) { |
580 PLOG(ERROR) << "accept() failed"; | 579 PLOG(ERROR) << "accept() failed"; |
581 return; | 580 return; |
582 } | 581 } |
583 DCHECK(base::SetNonBlocking(connection_socket)) | 582 DCHECK(base::SetNonBlocking(connection_socket)) |
584 << "Failed to make non-blocking socket."; | 583 << "Failed to make non-blocking socket."; |
585 SocketReader* reader = | 584 readers_.insert( |
586 new SocketReader(this, ui_task_runner_, connection_socket); | 585 base::MakeUnique<SocketReader>(this, ui_task_runner_, connection_socket)); |
587 readers_.insert(reader); | |
588 } | 586 } |
589 | 587 |
590 void ProcessSingleton::LinuxWatcher::StartListening(int socket) { | 588 void ProcessSingleton::LinuxWatcher::StartListening(int socket) { |
591 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 589 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
592 // Watch for client connections on this socket. | 590 // Watch for client connections on this socket. |
593 socket_watcher_ = base::FileDescriptorWatcher::WatchReadable( | 591 socket_watcher_ = base::FileDescriptorWatcher::WatchReadable( |
594 socket, base::Bind(&LinuxWatcher::OnSocketCanReadWithoutBlocking, | 592 socket, base::Bind(&LinuxWatcher::OnSocketCanReadWithoutBlocking, |
595 base::Unretained(this), socket)); | 593 base::Unretained(this), socket)); |
596 } | 594 } |
597 | 595 |
(...skipping 13 matching lines...) Expand all Loading... |
611 // Send back "SHUTDOWN" message, so that the client process can start up | 609 // Send back "SHUTDOWN" message, so that the client process can start up |
612 // without killing this process. | 610 // without killing this process. |
613 reader->FinishWithACK(kShutdownToken, arraysize(kShutdownToken) - 1); | 611 reader->FinishWithACK(kShutdownToken, arraysize(kShutdownToken) - 1); |
614 return; | 612 return; |
615 } | 613 } |
616 } | 614 } |
617 | 615 |
618 void ProcessSingleton::LinuxWatcher::RemoveSocketReader(SocketReader* reader) { | 616 void ProcessSingleton::LinuxWatcher::RemoveSocketReader(SocketReader* reader) { |
619 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 617 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
620 DCHECK(reader); | 618 DCHECK(reader); |
621 readers_.erase(reader); | 619 auto it = std::find_if(readers_.begin(), readers_.end(), |
622 delete reader; | 620 [reader](const std::unique_ptr<SocketReader>& ptr) { |
| 621 return ptr.get() == reader; |
| 622 }); |
| 623 readers_.erase(it); |
623 } | 624 } |
624 | 625 |
625 /////////////////////////////////////////////////////////////////////////////// | 626 /////////////////////////////////////////////////////////////////////////////// |
626 // ProcessSingleton::LinuxWatcher::SocketReader | 627 // ProcessSingleton::LinuxWatcher::SocketReader |
627 // | 628 // |
628 | 629 |
629 void ProcessSingleton::LinuxWatcher::SocketReader:: | 630 void ProcessSingleton::LinuxWatcher::SocketReader:: |
630 OnSocketCanReadWithoutBlocking() { | 631 OnSocketCanReadWithoutBlocking() { |
631 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 632 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
632 while (bytes_read_ < sizeof(buf_)) { | 633 while (bytes_read_ < sizeof(buf_)) { |
(...skipping 430 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1063 } | 1064 } |
1064 | 1065 |
1065 void ProcessSingleton::KillProcess(int pid) { | 1066 void ProcessSingleton::KillProcess(int pid) { |
1066 // TODO(james.su@gmail.com): Is SIGKILL ok? | 1067 // TODO(james.su@gmail.com): Is SIGKILL ok? |
1067 int rv = kill(static_cast<base::ProcessHandle>(pid), SIGKILL); | 1068 int rv = kill(static_cast<base::ProcessHandle>(pid), SIGKILL); |
1068 // ESRCH = No Such Process (can happen if the other process is already in | 1069 // ESRCH = No Such Process (can happen if the other process is already in |
1069 // progress of shutting down and finishes before we try to kill it). | 1070 // progress of shutting down and finishes before we try to kill it). |
1070 DCHECK(rv == 0 || errno == ESRCH) << "Error killing process: " | 1071 DCHECK(rv == 0 || errno == ESRCH) << "Error killing process: " |
1071 << base::safe_strerror(errno); | 1072 << base::safe_strerror(errno); |
1072 } | 1073 } |
OLD | NEW |