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 // 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 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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/stringprintf.h" | 71 #include "base/strings/stringprintf.h" |
72 #include "base/strings/sys_string_conversions.h" | 72 #include "base/strings/sys_string_conversions.h" |
73 #include "base/strings/utf_string_conversions.h" | 73 #include "base/strings/utf_string_conversions.h" |
74 #include "base/threading/platform_thread.h" | 74 #include "base/threading/platform_thread.h" |
75 #include "base/time/time.h" | 75 #include "base/time/time.h" |
76 #include "base/timer/timer.h" | 76 #include "base/timer/timer.h" |
77 #include "chrome/browser/ui/process_singleton_dialog_linux.h" | |
78 #include "chrome/common/chrome_constants.h" | 77 #include "chrome/common/chrome_constants.h" |
79 #include "content/public/browser/browser_thread.h" | 78 #include "content/public/browser/browser_thread.h" |
80 #include "grit/chromium_strings.h" | 79 #include "grit/chromium_strings.h" |
81 #include "grit/generated_resources.h" | 80 #include "grit/generated_resources.h" |
82 #include "net/base/net_util.h" | 81 #include "net/base/net_util.h" |
83 #include "ui/base/l10n/l10n_util.h" | 82 #include "ui/base/l10n/l10n_util.h" |
84 | 83 |
| 84 #if defined(OS_LINUX) |
| 85 #include "chrome/browser/ui/process_singleton_dialog_linux.h" |
| 86 #endif |
| 87 |
85 #if defined(TOOLKIT_VIEWS) && !defined(OS_CHROMEOS) | 88 #if defined(TOOLKIT_VIEWS) && !defined(OS_CHROMEOS) |
86 #include "ui/views/linux_ui/linux_ui.h" | 89 #include "ui/views/linux_ui/linux_ui.h" |
87 #endif | 90 #endif |
88 | 91 |
89 using content::BrowserThread; | 92 using content::BrowserThread; |
90 | 93 |
91 const int ProcessSingleton::kTimeoutInSeconds; | 94 const int ProcessSingleton::kTimeoutInSeconds; |
92 | 95 |
93 namespace { | 96 namespace { |
94 | 97 |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 *pid = -1; | 294 *pid = -1; |
292 | 295 |
293 return true; | 296 return true; |
294 } | 297 } |
295 | 298 |
296 // Returns true if the user opted to unlock the profile. | 299 // Returns true if the user opted to unlock the profile. |
297 bool DisplayProfileInUseError(const base::FilePath& lock_path, | 300 bool DisplayProfileInUseError(const base::FilePath& lock_path, |
298 const std::string& hostname, | 301 const std::string& hostname, |
299 int pid) { | 302 int pid) { |
300 base::string16 error = l10n_util::GetStringFUTF16( | 303 base::string16 error = l10n_util::GetStringFUTF16( |
301 IDS_PROFILE_IN_USE_LINUX, | 304 IDS_PROFILE_IN_USE_POSIX, |
302 base::IntToString16(pid), | 305 base::IntToString16(pid), |
303 base::ASCIIToUTF16(hostname)); | 306 base::ASCIIToUTF16(hostname)); |
| 307 LOG(ERROR) << error; |
| 308 |
| 309 if (g_disable_prompt) |
| 310 return false; |
| 311 |
| 312 #if defined(OS_LINUX) |
304 base::string16 relaunch_button_text = l10n_util::GetStringUTF16( | 313 base::string16 relaunch_button_text = l10n_util::GetStringUTF16( |
305 IDS_PROFILE_IN_USE_LINUX_RELAUNCH); | 314 IDS_PROFILE_IN_USE_LINUX_RELAUNCH); |
306 LOG(ERROR) << base::SysWideToNativeMB(base::UTF16ToWide(error)).c_str(); | 315 return ShowProcessSingletonDialog(error, relaunch_button_text); |
307 if (!g_disable_prompt) | 316 #elif defined(OS_MACOSX) |
308 return ShowProcessSingletonDialog(error, relaunch_button_text); | 317 // On Mac, always usurp the lock. |
| 318 return true; |
| 319 #endif |
| 320 |
| 321 NOTREACHED(); |
309 return false; | 322 return false; |
310 } | 323 } |
311 | 324 |
312 bool IsChromeProcess(pid_t pid) { | 325 bool IsChromeProcess(pid_t pid) { |
313 base::FilePath other_chrome_path(base::GetProcessExecutablePath(pid)); | 326 base::FilePath other_chrome_path(base::GetProcessExecutablePath(pid)); |
314 return (!other_chrome_path.empty() && | 327 return (!other_chrome_path.empty() && |
315 other_chrome_path.BaseName() == | 328 other_chrome_path.BaseName() == |
316 base::FilePath(chrome::kBrowserProcessExecutableName)); | 329 base::FilePath(chrome::kBrowserProcessExecutableName)); |
317 } | 330 } |
318 | 331 |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
386 sizeof(addr))); | 399 sizeof(addr))); |
387 return (ret == 0); | 400 return (ret == 0); |
388 } else { | 401 } else { |
389 // File is missing, or other error. | 402 // File is missing, or other error. |
390 if (errno != ENOENT) | 403 if (errno != ENOENT) |
391 PLOG(ERROR) << "readlink failed"; | 404 PLOG(ERROR) << "readlink failed"; |
392 return false; | 405 return false; |
393 } | 406 } |
394 } | 407 } |
395 | 408 |
| 409 #if defined(OS_MACOSX) |
| 410 bool ReplaceOldSingletonLock(const base::FilePath& symlink_content, |
| 411 const base::FilePath& lock_path) { |
| 412 // Try taking an flock(2) on the file. Failure means the lock is taken so we |
| 413 // should quit. |
| 414 base::ScopedFD lock_fd(HANDLE_EINTR( |
| 415 open(lock_path.value().c_str(), O_RDWR | O_CREAT | O_SYMLINK, 0644))); |
| 416 if (!lock_fd.is_valid()) { |
| 417 PLOG(ERROR) << "Could not open singleton lock"; |
| 418 return false; |
| 419 } |
| 420 |
| 421 int rc = HANDLE_EINTR(flock(lock_fd.get(), LOCK_EX | LOCK_NB)); |
| 422 if (rc == -1) { |
| 423 if (errno == EWOULDBLOCK) { |
| 424 LOG(ERROR) << "Singleton lock held by old process."; |
| 425 } else { |
| 426 PLOG(ERROR) << "Error locking singleton lock"; |
| 427 } |
| 428 return false; |
| 429 } |
| 430 |
| 431 // Successfully taking the lock means we can replace it with the a new symlink |
| 432 // lock. We never flock() the lock file from now on. I.e. we assume that an |
| 433 // old version of Chrome will not run with the same user data dir after this |
| 434 // version has run. |
| 435 if (!base::DeleteFile(lock_path, false)) { |
| 436 PLOG(ERROR) << "Could not delete old singleton lock."; |
| 437 return false; |
| 438 } |
| 439 |
| 440 return SymlinkPath(symlink_content, lock_path); |
| 441 } |
| 442 #endif // defined(OS_MACOSX) |
| 443 |
396 } // namespace | 444 } // namespace |
397 | 445 |
398 /////////////////////////////////////////////////////////////////////////////// | 446 /////////////////////////////////////////////////////////////////////////////// |
399 // ProcessSingleton::LinuxWatcher | 447 // ProcessSingleton::LinuxWatcher |
400 // A helper class for a Linux specific implementation of the process singleton. | 448 // A helper class for a Linux specific implementation of the process singleton. |
401 // This class sets up a listener on the singleton socket and handles parsing | 449 // This class sets up a listener on the singleton socket and handles parsing |
402 // messages that come in on the singleton socket. | 450 // messages that come in on the singleton socket. |
403 class ProcessSingleton::LinuxWatcher | 451 class ProcessSingleton::LinuxWatcher |
404 : public base::MessageLoopForIO::Watcher, | 452 : public base::MessageLoopForIO::Watcher, |
405 public base::MessageLoop::DestructionObserver, | 453 public base::MessageLoop::DestructionObserver, |
(...skipping 470 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
876 // processes can find it out. | 924 // processes can find it out. |
877 base::FilePath symlink_content(base::StringPrintf( | 925 base::FilePath symlink_content(base::StringPrintf( |
878 "%s%c%u", | 926 "%s%c%u", |
879 net::GetHostName().c_str(), | 927 net::GetHostName().c_str(), |
880 kLockDelimiter, | 928 kLockDelimiter, |
881 current_pid_)); | 929 current_pid_)); |
882 | 930 |
883 // Create symbol link before binding the socket, to ensure only one instance | 931 // Create symbol link before binding the socket, to ensure only one instance |
884 // can have the socket open. | 932 // can have the socket open. |
885 if (!SymlinkPath(symlink_content, lock_path_)) { | 933 if (!SymlinkPath(symlink_content, lock_path_)) { |
886 // If we failed to create the lock, most likely another instance won the | 934 // TODO(jackhou): Remove this case once this code is stable on Mac. |
887 // startup race. | 935 // http://crbug.com/367612 |
| 936 #if defined(OS_MACOSX) |
| 937 // On Mac, an existing non-symlink lock file means the lock could be held by |
| 938 // the old process singleton code. If we can successfully replace the lock, |
| 939 // continue as normal. |
| 940 if (base::IsLink(lock_path_) || |
| 941 !ReplaceOldSingletonLock(symlink_content, lock_path_)) { |
888 return false; | 942 return false; |
| 943 } |
| 944 #else |
| 945 // If we failed to create the lock, most likely another instance won the |
| 946 // startup race. |
| 947 return false; |
| 948 #endif |
889 } | 949 } |
890 | 950 |
891 // Create the socket file somewhere in /tmp which is usually mounted as a | 951 // Create the socket file somewhere in /tmp which is usually mounted as a |
892 // normal filesystem. Some network filesystems (notably AFS) are screwy and | 952 // normal filesystem. Some network filesystems (notably AFS) are screwy and |
893 // do not support Unix domain sockets. | 953 // do not support Unix domain sockets. |
894 if (!socket_dir_.CreateUniqueTempDir()) { | 954 if (!socket_dir_.CreateUniqueTempDir()) { |
895 LOG(ERROR) << "Failed to create socket directory."; | 955 LOG(ERROR) << "Failed to create socket directory."; |
896 return false; | 956 return false; |
897 } | 957 } |
| 958 |
| 959 // Check that the directory was created with the correct permissions. |
| 960 int dir_mode = 0; |
| 961 CHECK(base::GetPosixFilePermissions(socket_dir_.path(), &dir_mode) && |
| 962 dir_mode == base::FILE_PERMISSION_USER_MASK) |
| 963 << "Temp directory mode is not 700: " << std::oct << dir_mode; |
| 964 |
898 // Setup the socket symlink and the two cookies. | 965 // Setup the socket symlink and the two cookies. |
899 base::FilePath socket_target_path = | 966 base::FilePath socket_target_path = |
900 socket_dir_.path().Append(chrome::kSingletonSocketFilename); | 967 socket_dir_.path().Append(chrome::kSingletonSocketFilename); |
901 base::FilePath cookie(GenerateCookie()); | 968 base::FilePath cookie(GenerateCookie()); |
902 base::FilePath remote_cookie_path = | 969 base::FilePath remote_cookie_path = |
903 socket_dir_.path().Append(chrome::kSingletonCookieFilename); | 970 socket_dir_.path().Append(chrome::kSingletonCookieFilename); |
904 UnlinkPath(socket_path_); | 971 UnlinkPath(socket_path_); |
905 UnlinkPath(cookie_path_); | 972 UnlinkPath(cookie_path_); |
906 if (!SymlinkPath(socket_target_path, socket_path_) || | 973 if (!SymlinkPath(socket_target_path, socket_path_) || |
907 !SymlinkPath(cookie, cookie_path_) || | 974 !SymlinkPath(cookie, cookie_path_) || |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
977 } | 1044 } |
978 | 1045 |
979 void ProcessSingleton::KillProcess(int pid) { | 1046 void ProcessSingleton::KillProcess(int pid) { |
980 // TODO(james.su@gmail.com): Is SIGKILL ok? | 1047 // TODO(james.su@gmail.com): Is SIGKILL ok? |
981 int rv = kill(static_cast<base::ProcessHandle>(pid), SIGKILL); | 1048 int rv = kill(static_cast<base::ProcessHandle>(pid), SIGKILL); |
982 // ESRCH = No Such Process (can happen if the other process is already in | 1049 // ESRCH = No Such Process (can happen if the other process is already in |
983 // progress of shutting down and finishes before we try to kill it). | 1050 // progress of shutting down and finishes before we try to kill it). |
984 DCHECK(rv == 0 || errno == ESRCH) << "Error killing process: " | 1051 DCHECK(rv == 0 || errno == ESRCH) << "Error killing process: " |
985 << safe_strerror(errno); | 1052 << safe_strerror(errno); |
986 } | 1053 } |
OLD | NEW |