OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 <errno.h> | 5 #include <errno.h> |
6 #include <fcntl.h> | 6 #include <fcntl.h> |
7 #include <sys/file.h> | 7 #include <sys/file.h> |
8 | 8 |
9 #include "chrome/browser/process_singleton.h" | 9 #include "chrome/browser/process_singleton.h" |
10 | 10 |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
55 // This space intentionally left blank. | 55 // This space intentionally left blank. |
56 return PROCESS_NONE; | 56 return PROCESS_NONE; |
57 } | 57 } |
58 | 58 |
59 ProcessSingleton::NotifyResult ProcessSingleton::NotifyOtherProcessOrCreate() { | 59 ProcessSingleton::NotifyResult ProcessSingleton::NotifyOtherProcessOrCreate() { |
60 // Windows tries NotifyOtherProcess() first. | 60 // Windows tries NotifyOtherProcess() first. |
61 return Create() ? PROCESS_NONE : PROFILE_IN_USE; | 61 return Create() ? PROCESS_NONE : PROFILE_IN_USE; |
62 } | 62 } |
63 | 63 |
64 // Attempt to acquire an exclusive lock on an empty file in the | 64 // Attempt to acquire an exclusive lock on an empty file in the |
65 // profile directory. Returns |true| if it gets the lock. | 65 // profile directory. Returns |true| if it gets the lock. Returns |
66 // TODO(shess): The older code always returned |true|. Monitor the | 66 // |false| if the lock is held, or if there is an error. |
67 // histograms and convert the marked failure cases to |false| once | |
68 // it's clear that it is safe to do. http://crbug.com/58986 | |
69 // TODO(shess): Rather than logging failures, popup an alert. Punting | 67 // TODO(shess): Rather than logging failures, popup an alert. Punting |
70 // that for now because it would require confidence that this code is | 68 // that for now because it would require confidence that this code is |
71 // never called in a situation where an alert wouldn't work. | 69 // never called in a situation where an alert wouldn't work. |
72 // http://crbug.com/59061 | 70 // http://crbug.com/59061 |
73 bool ProcessSingleton::Create() { | 71 bool ProcessSingleton::Create() { |
74 DCHECK_EQ(-1, lock_fd_) << "lock_path_ is already open."; | 72 DCHECK_EQ(-1, lock_fd_) << "lock_path_ is already open."; |
75 | 73 |
76 lock_fd_ = HANDLE_EINTR(open(lock_path_.value().c_str(), | 74 lock_fd_ = HANDLE_EINTR(open(lock_path_.value().c_str(), |
77 O_RDONLY | O_CREAT, 0644)); | 75 O_RDONLY | O_CREAT, 0644)); |
78 if (lock_fd_ == -1) { | 76 if (lock_fd_ == -1) { |
79 const int capture_errno = errno; | 77 const int capture_errno = errno; |
80 DPCHECK(lock_fd_ != -1) << "Unexpected failure opening profile lockfile"; | 78 DPCHECK(lock_fd_ != -1) << "Unexpected failure opening profile lockfile"; |
81 UMA_HISTOGRAM_ENUMERATION("ProcessSingleton.OpenError", | 79 UMA_HISTOGRAM_ENUMERATION("ProcessSingleton.OpenError", |
82 capture_errno, kMaxErrno); | 80 capture_errno, kMaxErrno); |
83 // TODO(shess): Change to |false|. | 81 return false; |
84 return true; | |
85 } | 82 } |
86 | 83 |
87 // Acquire an exclusive lock in non-blocking fashion. If the lock | 84 // Acquire an exclusive lock in non-blocking fashion. If the lock |
88 // is already held, this will return |EWOULDBLOCK|. | 85 // is already held, this will return |EWOULDBLOCK|. |
89 int rc = HANDLE_EINTR(flock(lock_fd_, LOCK_EX|LOCK_NB)); | 86 int rc = HANDLE_EINTR(flock(lock_fd_, LOCK_EX|LOCK_NB)); |
90 if (rc == -1) { | 87 if (rc == -1) { |
91 const int capture_errno = errno; | 88 const int capture_errno = errno; |
92 DPCHECK(errno == EWOULDBLOCK) | 89 DPCHECK(errno == EWOULDBLOCK) |
93 << "Unexpected failure locking profile lockfile"; | 90 << "Unexpected failure locking profile lockfile"; |
94 | 91 |
95 Cleanup(); | 92 Cleanup(); |
96 | 93 |
97 // Other errors indicate something crazy is happening. | 94 // Other errors indicate something crazy is happening. |
98 if (capture_errno != EWOULDBLOCK) { | 95 if (capture_errno != EWOULDBLOCK) { |
99 UMA_HISTOGRAM_ENUMERATION("ProcessSingleton.LockError", | 96 UMA_HISTOGRAM_ENUMERATION("ProcessSingleton.LockError", |
100 capture_errno, kMaxErrno); | 97 capture_errno, kMaxErrno); |
101 // TODO(shess): Change to |false|. | 98 return false; |
102 return true; | |
103 } | 99 } |
104 | 100 |
105 // The file is open by another process and locked. | 101 // The file is open by another process and locked. |
106 LOG(ERROR) << "Unable to obtain profile lock."; | 102 LOG(ERROR) << "Unable to obtain profile lock."; |
107 return false; | 103 return false; |
108 } | 104 } |
109 | 105 |
110 return true; | 106 return true; |
111 } | 107 } |
112 | 108 |
113 void ProcessSingleton::Cleanup() { | 109 void ProcessSingleton::Cleanup() { |
114 // Closing the file also releases the lock. | 110 // Closing the file also releases the lock. |
115 if (lock_fd_ != -1) { | 111 if (lock_fd_ != -1) { |
116 int rc = HANDLE_EINTR(close(lock_fd_)); | 112 int rc = HANDLE_EINTR(close(lock_fd_)); |
117 DPCHECK(!rc) << "Closing lock_fd_:"; | 113 DPCHECK(!rc) << "Closing lock_fd_:"; |
118 } | 114 } |
119 lock_fd_ = -1; | 115 lock_fd_ = -1; |
120 } | 116 } |
OLD | NEW |