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 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 #include "base/base_paths.h" | 55 #include "base/base_paths.h" |
56 #include "base/basictypes.h" | 56 #include "base/basictypes.h" |
57 #include "base/bind.h" | 57 #include "base/bind.h" |
58 #include "base/command_line.h" | 58 #include "base/command_line.h" |
59 #include "base/files/file_path.h" | 59 #include "base/files/file_path.h" |
60 #include "base/files/file_util.h" | 60 #include "base/files/file_util.h" |
61 #include "base/logging.h" | 61 #include "base/logging.h" |
62 #include "base/message_loop/message_loop.h" | 62 #include "base/message_loop/message_loop.h" |
63 #include "base/path_service.h" | 63 #include "base/path_service.h" |
64 #include "base/posix/eintr_wrapper.h" | 64 #include "base/posix/eintr_wrapper.h" |
| 65 #include "base/posix/safe_strerror.h" |
65 #include "base/rand_util.h" | 66 #include "base/rand_util.h" |
66 #include "base/safe_strerror_posix.h" | |
67 #include "base/sequenced_task_runner_helpers.h" | 67 #include "base/sequenced_task_runner_helpers.h" |
68 #include "base/stl_util.h" | 68 #include "base/stl_util.h" |
69 #include "base/strings/string_number_conversions.h" | 69 #include "base/strings/string_number_conversions.h" |
70 #include "base/strings/string_split.h" | 70 #include "base/strings/string_split.h" |
71 #include "base/strings/string_util.h" | 71 #include "base/strings/string_util.h" |
72 #include "base/strings/stringprintf.h" | 72 #include "base/strings/stringprintf.h" |
73 #include "base/strings/sys_string_conversions.h" | 73 #include "base/strings/sys_string_conversions.h" |
74 #include "base/strings/utf_string_conversions.h" | 74 #include "base/strings/utf_string_conversions.h" |
75 #include "base/threading/platform_thread.h" | 75 #include "base/threading/platform_thread.h" |
76 #include "base/time/time.h" | 76 #include "base/time/time.h" |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
128 if (-1 == flags) | 128 if (-1 == flags) |
129 return flags; | 129 return flags; |
130 if (flags & FD_CLOEXEC) | 130 if (flags & FD_CLOEXEC) |
131 return 0; | 131 return 0; |
132 return fcntl(fd, F_SETFD, flags | FD_CLOEXEC); | 132 return fcntl(fd, F_SETFD, flags | FD_CLOEXEC); |
133 } | 133 } |
134 | 134 |
135 // Close a socket and check return value. | 135 // Close a socket and check return value. |
136 void CloseSocket(int fd) { | 136 void CloseSocket(int fd) { |
137 int rv = IGNORE_EINTR(close(fd)); | 137 int rv = IGNORE_EINTR(close(fd)); |
138 DCHECK_EQ(0, rv) << "Error closing socket: " << safe_strerror(errno); | 138 DCHECK_EQ(0, rv) << "Error closing socket: " << base::safe_strerror(errno); |
139 } | 139 } |
140 | 140 |
141 // Write a message to a socket fd. | 141 // Write a message to a socket fd. |
142 bool WriteToSocket(int fd, const char *message, size_t length) { | 142 bool WriteToSocket(int fd, const char *message, size_t length) { |
143 DCHECK(message); | 143 DCHECK(message); |
144 DCHECK(length); | 144 DCHECK(length); |
145 size_t bytes_written = 0; | 145 size_t bytes_written = 0; |
146 do { | 146 do { |
147 ssize_t rv = HANDLE_EINTR( | 147 ssize_t rv = HANDLE_EINTR( |
148 write(fd, message + bytes_written, length - bytes_written)); | 148 write(fd, message + bytes_written, length - bytes_written)); |
(...skipping 854 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1003 | 1003 |
1004 SetupSocket(socket_target_path.value(), &sock, &addr); | 1004 SetupSocket(socket_target_path.value(), &sock, &addr); |
1005 | 1005 |
1006 if (bind(sock, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) < 0) { | 1006 if (bind(sock, reinterpret_cast<sockaddr*>(&addr), sizeof(addr)) < 0) { |
1007 PLOG(ERROR) << "Failed to bind() " << socket_target_path.value(); | 1007 PLOG(ERROR) << "Failed to bind() " << socket_target_path.value(); |
1008 CloseSocket(sock); | 1008 CloseSocket(sock); |
1009 return false; | 1009 return false; |
1010 } | 1010 } |
1011 | 1011 |
1012 if (listen(sock, 5) < 0) | 1012 if (listen(sock, 5) < 0) |
1013 NOTREACHED() << "listen failed: " << safe_strerror(errno); | 1013 NOTREACHED() << "listen failed: " << base::safe_strerror(errno); |
1014 | 1014 |
1015 DCHECK(BrowserThread::IsMessageLoopValid(BrowserThread::IO)); | 1015 DCHECK(BrowserThread::IsMessageLoopValid(BrowserThread::IO)); |
1016 BrowserThread::PostTask( | 1016 BrowserThread::PostTask( |
1017 BrowserThread::IO, | 1017 BrowserThread::IO, |
1018 FROM_HERE, | 1018 FROM_HERE, |
1019 base::Bind(&ProcessSingleton::LinuxWatcher::StartListening, | 1019 base::Bind(&ProcessSingleton::LinuxWatcher::StartListening, |
1020 watcher_.get(), | 1020 watcher_.get(), |
1021 sock)); | 1021 sock)); |
1022 | 1022 |
1023 return true; | 1023 return true; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1062 LOG(ERROR) << "Failed to extract pid from path: " << lock_path_.value(); | 1062 LOG(ERROR) << "Failed to extract pid from path: " << lock_path_.value(); |
1063 return true; | 1063 return true; |
1064 } | 1064 } |
1065 | 1065 |
1066 void ProcessSingleton::KillProcess(int pid) { | 1066 void ProcessSingleton::KillProcess(int pid) { |
1067 // TODO(james.su@gmail.com): Is SIGKILL ok? | 1067 // TODO(james.su@gmail.com): Is SIGKILL ok? |
1068 int rv = kill(static_cast<base::ProcessHandle>(pid), SIGKILL); | 1068 int rv = kill(static_cast<base::ProcessHandle>(pid), SIGKILL); |
1069 // 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 |
1070 // 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). |
1071 DCHECK(rv == 0 || errno == ESRCH) << "Error killing process: " | 1071 DCHECK(rv == 0 || errno == ESRCH) << "Error killing process: " |
1072 << safe_strerror(errno); | 1072 << base::safe_strerror(errno); |
1073 } | 1073 } |
OLD | NEW |