| 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 #include "base/base_paths.h" | 57 #include "base/base_paths.h" | 
| 58 #include "base/bind.h" | 58 #include "base/bind.h" | 
| 59 #include "base/command_line.h" | 59 #include "base/command_line.h" | 
| 60 #include "base/files/file_path.h" | 60 #include "base/files/file_path.h" | 
| 61 #include "base/files/file_util.h" | 61 #include "base/files/file_util.h" | 
| 62 #include "base/location.h" | 62 #include "base/location.h" | 
| 63 #include "base/logging.h" | 63 #include "base/logging.h" | 
| 64 #include "base/macros.h" | 64 #include "base/macros.h" | 
| 65 #include "base/memory/ref_counted.h" | 65 #include "base/memory/ref_counted.h" | 
| 66 #include "base/message_loop/message_loop.h" | 66 #include "base/message_loop/message_loop.h" | 
|  | 67 #include "base/metrics/histogram_macros.h" | 
| 67 #include "base/path_service.h" | 68 #include "base/path_service.h" | 
| 68 #include "base/posix/eintr_wrapper.h" | 69 #include "base/posix/eintr_wrapper.h" | 
| 69 #include "base/posix/safe_strerror.h" | 70 #include "base/posix/safe_strerror.h" | 
| 70 #include "base/rand_util.h" | 71 #include "base/rand_util.h" | 
| 71 #include "base/sequenced_task_runner_helpers.h" | 72 #include "base/sequenced_task_runner_helpers.h" | 
| 72 #include "base/single_thread_task_runner.h" | 73 #include "base/single_thread_task_runner.h" | 
| 73 #include "base/single_thread_task_runner.h" | 74 #include "base/single_thread_task_runner.h" | 
| 74 #include "base/stl_util.h" | 75 #include "base/stl_util.h" | 
| 75 #include "base/strings/string_number_conversions.h" | 76 #include "base/strings/string_number_conversions.h" | 
| 76 #include "base/strings/string_split.h" | 77 #include "base/strings/string_split.h" | 
| (...skipping 811 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 888   return NotifyOtherProcessWithTimeoutOrCreate( | 889   return NotifyOtherProcessWithTimeoutOrCreate( | 
| 889       *base::CommandLine::ForCurrentProcess(), kRetryAttempts, | 890       *base::CommandLine::ForCurrentProcess(), kRetryAttempts, | 
| 890       base::TimeDelta::FromSeconds(kTimeoutInSeconds)); | 891       base::TimeDelta::FromSeconds(kTimeoutInSeconds)); | 
| 891 } | 892 } | 
| 892 | 893 | 
| 893 ProcessSingleton::NotifyResult | 894 ProcessSingleton::NotifyResult | 
| 894 ProcessSingleton::NotifyOtherProcessWithTimeoutOrCreate( | 895 ProcessSingleton::NotifyOtherProcessWithTimeoutOrCreate( | 
| 895     const base::CommandLine& command_line, | 896     const base::CommandLine& command_line, | 
| 896     int retry_attempts, | 897     int retry_attempts, | 
| 897     const base::TimeDelta& timeout) { | 898     const base::TimeDelta& timeout) { | 
|  | 899   const base::TimeTicks begin_ticks = base::TimeTicks::Now(); | 
| 898   NotifyResult result = NotifyOtherProcessWithTimeout( | 900   NotifyResult result = NotifyOtherProcessWithTimeout( | 
| 899       command_line, retry_attempts, timeout, true); | 901       command_line, retry_attempts, timeout, true); | 
| 900   if (result != PROCESS_NONE) | 902   if (result != PROCESS_NONE) { | 
|  | 903     if (result == PROCESS_NOTIFIED) { | 
|  | 904       UMA_HISTOGRAM_MEDIUM_TIMES("Chrome.ProcessSingleton.TimeToNotify", | 
|  | 905                                  base::TimeTicks::Now() - begin_ticks); | 
|  | 906     } else { | 
|  | 907       UMA_HISTOGRAM_MEDIUM_TIMES("Chrome.ProcessSingleton.TimeToFailure", | 
|  | 908                                  base::TimeTicks::Now() - begin_ticks); | 
|  | 909     } | 
| 901     return result; | 910     return result; | 
| 902   if (Create()) | 911   } | 
|  | 912 | 
|  | 913   if (Create()) { | 
|  | 914     UMA_HISTOGRAM_MEDIUM_TIMES("Chrome.ProcessSingleton.TimeToCreate", | 
|  | 915                                base::TimeTicks::Now() - begin_ticks); | 
| 903     return PROCESS_NONE; | 916     return PROCESS_NONE; | 
|  | 917   } | 
|  | 918 | 
| 904   // If the Create() failed, try again to notify. (It could be that another | 919   // If the Create() failed, try again to notify. (It could be that another | 
| 905   // instance was starting at the same time and managed to grab the lock before | 920   // instance was starting at the same time and managed to grab the lock before | 
| 906   // we did.) | 921   // we did.) | 
| 907   // This time, we don't want to kill anything if we aren't successful, since we | 922   // This time, we don't want to kill anything if we aren't successful, since we | 
| 908   // aren't going to try to take over the lock ourselves. | 923   // aren't going to try to take over the lock ourselves. | 
| 909   result = NotifyOtherProcessWithTimeout( | 924   result = NotifyOtherProcessWithTimeout( | 
| 910       command_line, retry_attempts, timeout, false); | 925       command_line, retry_attempts, timeout, false); | 
|  | 926 | 
|  | 927   if (result == PROCESS_NOTIFIED) { | 
|  | 928     UMA_HISTOGRAM_MEDIUM_TIMES("Chrome.ProcessSingleton.TimeToNotify", | 
|  | 929                                base::TimeTicks::Now() - begin_ticks); | 
|  | 930   } else { | 
|  | 931     UMA_HISTOGRAM_MEDIUM_TIMES("Chrome.ProcessSingleton.TimeToFailure", | 
|  | 932                                base::TimeTicks::Now() - begin_ticks); | 
|  | 933   } | 
|  | 934 | 
| 911   if (result != PROCESS_NONE) | 935   if (result != PROCESS_NONE) | 
| 912     return result; | 936     return result; | 
| 913 | 937 | 
| 914   return LOCK_ERROR; | 938   return LOCK_ERROR; | 
| 915 } | 939 } | 
| 916 | 940 | 
| 917 void ProcessSingleton::OverrideCurrentPidForTesting(base::ProcessId pid) { | 941 void ProcessSingleton::OverrideCurrentPidForTesting(base::ProcessId pid) { | 
| 918   current_pid_ = pid; | 942   current_pid_ = pid; | 
| 919 } | 943 } | 
| 920 | 944 | 
| (...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
| 1055 } | 1079 } | 
| 1056 | 1080 | 
| 1057 void ProcessSingleton::KillProcess(int pid) { | 1081 void ProcessSingleton::KillProcess(int pid) { | 
| 1058   // TODO(james.su@gmail.com): Is SIGKILL ok? | 1082   // TODO(james.su@gmail.com): Is SIGKILL ok? | 
| 1059   int rv = kill(static_cast<base::ProcessHandle>(pid), SIGKILL); | 1083   int rv = kill(static_cast<base::ProcessHandle>(pid), SIGKILL); | 
| 1060   // ESRCH = No Such Process (can happen if the other process is already in | 1084   // ESRCH = No Such Process (can happen if the other process is already in | 
| 1061   // progress of shutting down and finishes before we try to kill it). | 1085   // progress of shutting down and finishes before we try to kill it). | 
| 1062   DCHECK(rv == 0 || errno == ESRCH) << "Error killing process: " | 1086   DCHECK(rv == 0 || errno == ESRCH) << "Error killing process: " | 
| 1063                                     << base::safe_strerror(errno); | 1087                                     << base::safe_strerror(errno); | 
| 1064 } | 1088 } | 
| OLD | NEW | 
|---|