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 #include "tools/android/forwarder2/daemon.h" | 5 #include "tools/android/forwarder2/daemon.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <fcntl.h> | 8 #include <fcntl.h> |
9 #include <signal.h> | 9 #include <signal.h> |
10 #include <sys/file.h> | 10 #include <sys/file.h> |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
91 string_builder.Append("Daemon (pid=%d) died unexpectedly with ", child_pid); | 91 string_builder.Append("Daemon (pid=%d) died unexpectedly with ", child_pid); |
92 if (WIFEXITED(status)) | 92 if (WIFEXITED(status)) |
93 string_builder.Append("status %d.", WEXITSTATUS(status)); | 93 string_builder.Append("status %d.", WEXITSTATUS(status)); |
94 else if (WIFSIGNALED(status)) | 94 else if (WIFSIGNALED(status)) |
95 string_builder.Append("signal %d.", WTERMSIG(status)); | 95 string_builder.Append("signal %d.", WTERMSIG(status)); |
96 else | 96 else |
97 string_builder.Append("unknown reason."); | 97 string_builder.Append("unknown reason."); |
98 SIGNAL_SAFE_LOG(ERROR, string_builder.buffer()); | 98 SIGNAL_SAFE_LOG(ERROR, string_builder.buffer()); |
99 } | 99 } |
100 | 100 |
101 // Note that 0 is written to |lock_owner_pid| in case the file is not locked. | |
102 bool GetFileLockOwnerPid(int fd, pid_t* lock_owner_pid) { | |
103 struct flock lock_info = {}; | |
104 lock_info.l_type = F_WRLCK; | |
105 lock_info.l_whence = SEEK_CUR; | |
106 const int ret = HANDLE_EINTR(fcntl(fd, F_GETLK, &lock_info)); | |
107 if (ret < 0) { | |
108 if (errno == EBADF) { | |
109 // Assume that the provided file descriptor corresponding to the PID file | |
110 // was valid until the daemon removed this file. | |
111 *lock_owner_pid = 0; | |
112 return true; | |
113 } | |
114 PError("fcntl"); | |
115 return false; | |
116 } | |
117 if (lock_info.l_type == F_UNLCK) { | |
118 *lock_owner_pid = 0; | |
119 return true; | |
120 } | |
121 CHECK_EQ(F_WRLCK /* exclusive lock */, lock_info.l_type); | |
122 *lock_owner_pid = lock_info.l_pid; | |
123 return true; | |
124 } | |
125 | |
126 scoped_ptr<Socket> ConnectToUnixDomainSocket( | 101 scoped_ptr<Socket> ConnectToUnixDomainSocket( |
127 const std::string& socket_name, | 102 const std::string& socket_name, |
128 int tries_count, | 103 int tries_count, |
129 int idle_time_msec, | 104 int idle_time_msec, |
130 const std::string& expected_welcome_message) { | 105 const std::string& expected_welcome_message) { |
131 for (int i = 0; i < tries_count; ++i) { | 106 for (int i = 0; i < tries_count; ++i) { |
132 scoped_ptr<Socket> socket(new Socket()); | 107 scoped_ptr<Socket> socket(new Socket()); |
133 if (!socket->ConnectUnix(socket_name)) { | 108 if (!socket->ConnectUnix(socket_name)) { |
134 if (idle_time_msec) | 109 if (idle_time_msec) |
135 usleep(idle_time_msec * 1000); | 110 usleep(idle_time_msec * 1000); |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
281 return true; | 256 return true; |
282 } | 257 } |
283 usleep(kIdleTimeMSec * 1000); | 258 usleep(kIdleTimeMSec * 1000); |
284 } | 259 } |
285 LOG(ERROR) << "Timed out while killing daemon. " | 260 LOG(ERROR) << "Timed out while killing daemon. " |
286 "It might still be tearing down."; | 261 "It might still be tearing down."; |
287 return false; | 262 return false; |
288 } | 263 } |
289 | 264 |
290 } // namespace forwarder2 | 265 } // namespace forwarder2 |
OLD | NEW |