Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(774)

Side by Side Diff: ipc/ipc_channel_posix.cc

Issue 7167017: Fix to bug 75303 (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: git try Created 9 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "ipc/ipc_channel_posix.h" 5 #include "ipc/ipc_channel_posix.h"
6 6
7 #include <errno.h> 7 #include <errno.h>
8 #include <fcntl.h> 8 #include <fcntl.h>
9 #include <stddef.h> 9 #include <stddef.h>
10 #include <sys/types.h> 10 #include <sys/types.h>
11 #include <sys/socket.h> 11 #include <sys/socket.h>
12 #include <sys/stat.h> 12 #include <sys/stat.h>
13 #include <sys/un.h> 13 #include <sys/un.h>
14 14
15 #include <string> 15 #include <string>
16 #include <map> 16 #include <map>
17 17
18 #include "base/base_paths.h"
18 #include "base/command_line.h" 19 #include "base/command_line.h"
19 #include "base/eintr_wrapper.h" 20 #include "base/eintr_wrapper.h"
20 #include "base/file_path.h" 21 #include "base/file_path.h"
21 #include "base/file_util.h" 22 #include "base/file_util.h"
22 #include "base/global_descriptors_posix.h" 23 #include "base/global_descriptors_posix.h"
23 #include "base/logging.h" 24 #include "base/logging.h"
24 #include "base/memory/scoped_ptr.h" 25 #include "base/memory/scoped_ptr.h"
25 #include "base/memory/singleton.h" 26 #include "base/memory/singleton.h"
27 #include "base/path_service.h"
26 #include "base/process_util.h" 28 #include "base/process_util.h"
27 #include "base/string_util.h" 29 #include "base/string_util.h"
28 #include "base/synchronization/lock.h" 30 #include "base/synchronization/lock.h"
29 #include "ipc/ipc_descriptors.h" 31 #include "ipc/ipc_descriptors.h"
30 #include "ipc/ipc_switches.h" 32 #include "ipc/ipc_switches.h"
31 #include "ipc/file_descriptor_set_posix.h" 33 #include "ipc/file_descriptor_set_posix.h"
32 #include "ipc/ipc_logging.h" 34 #include "ipc/ipc_logging.h"
33 #include "ipc/ipc_message_utils.h" 35 #include "ipc/ipc_message_utils.h"
34 36
35 namespace IPC { 37 namespace IPC {
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
135 137
136 //------------------------------------------------------------------------------ 138 //------------------------------------------------------------------------------
137 // Verify that kMaxPipeNameLength is a decent size. 139 // Verify that kMaxPipeNameLength is a decent size.
138 COMPILE_ASSERT(sizeof(((sockaddr_un*)0)->sun_path) >= kMaxPipeNameLength, 140 COMPILE_ASSERT(sizeof(((sockaddr_un*)0)->sun_path) >= kMaxPipeNameLength,
139 BAD_SUN_PATH_LENGTH); 141 BAD_SUN_PATH_LENGTH);
140 142
141 // Creates a unix domain socket bound to the specified name that is listening 143 // Creates a unix domain socket bound to the specified name that is listening
142 // for connections. 144 // for connections.
143 bool CreateServerUnixDomainSocket(const std::string& pipe_name, 145 bool CreateServerUnixDomainSocket(const std::string& pipe_name,
144 int* server_listen_fd) { 146 int* server_listen_fd) {
147 FilePath temp_dir;
148 PathService::Get(base::DIR_TEMP, &temp_dir);
sanjeevr 2011/06/20 21:50:34 Document why this is needed.
149 std::string pipe_path= temp_dir.value()+ "/" + pipe_name;
sanjeevr 2011/06/20 21:50:34 Use FilePath::Append instead of appending the stri
145 DCHECK(server_listen_fd); 150 DCHECK(server_listen_fd);
146 DCHECK_GT(pipe_name.length(), 0u); 151 DCHECK_GT(pipe_path.length(), 0u);
147 DCHECK_LT(pipe_name.length(), kMaxPipeNameLength); 152 DCHECK_LT(pipe_path.length(), kMaxPipeNameLength);
148 153
149 if (pipe_name.length() == 0 || pipe_name.length() >= kMaxPipeNameLength) { 154 if (pipe_path.length() == 0 || pipe_path.length() >= kMaxPipeNameLength) {
150 return false; 155 return false;
151 } 156 }
152 157
153 // Create socket. 158 // Create socket.
154 int fd = socket(AF_UNIX, SOCK_STREAM, 0); 159 int fd = socket(AF_UNIX, SOCK_STREAM, 0);
155 if (fd < 0) { 160 if (fd < 0) {
156 return false; 161 return false;
157 } 162 }
158 163
159 // Make socket non-blocking 164 // Make socket non-blocking
160 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { 165 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
161 PLOG(ERROR) << "fcntl(O_NONBLOCK) " << pipe_name; 166 PLOG(ERROR) << "fcntl(O_NONBLOCK) " << pipe_path;
162 if (HANDLE_EINTR(close(fd)) < 0) 167 if (HANDLE_EINTR(close(fd)) < 0)
163 PLOG(ERROR) << "close " << pipe_name; 168 PLOG(ERROR) << "close " << pipe_path;
164 return false; 169 return false;
165 } 170 }
166 171
167 // Delete any old FS instances. 172 // Delete any old FS instances.
168 unlink(pipe_name.c_str()); 173 unlink(pipe_path.c_str());
169 174
170 // Make sure the path we need exists. 175 // Make sure the path we need exists.
171 FilePath path(pipe_name); 176 FilePath path(pipe_path);
172 FilePath dir_path = path.DirName(); 177 FilePath dir_path = path.DirName();
173 if (!file_util::CreateDirectory(dir_path)) { 178 if (!file_util::CreateDirectory(dir_path)) {
174 return false; 179 return false;
175 } 180 }
176 181
177 // Create unix_addr structure. 182 // Create unix_addr structure.
178 struct sockaddr_un unix_addr; 183 struct sockaddr_un unix_addr;
179 memset(&unix_addr, 0, sizeof(unix_addr)); 184 memset(&unix_addr, 0, sizeof(unix_addr));
180 unix_addr.sun_family = AF_UNIX; 185 unix_addr.sun_family = AF_UNIX;
181 int path_len = snprintf(unix_addr.sun_path, IPC::kMaxPipeNameLength, 186 int path_len = snprintf(unix_addr.sun_path, IPC::kMaxPipeNameLength,
182 "%s", pipe_name.c_str()); 187 "%s", pipe_path.c_str());
183 DCHECK_EQ(static_cast<int>(pipe_name.length()), path_len); 188 DCHECK_EQ(static_cast<int>(pipe_path.length()), path_len);
184 size_t unix_addr_len = offsetof(struct sockaddr_un, 189 size_t unix_addr_len = offsetof(struct sockaddr_un,
185 sun_path) + path_len + 1; 190 sun_path) + path_len + 1;
186 191
187 // Bind the socket. 192 // Bind the socket.
188 if (bind(fd, reinterpret_cast<const sockaddr*>(&unix_addr), 193 if (bind(fd, reinterpret_cast<const sockaddr*>(&unix_addr),
189 unix_addr_len) != 0) { 194 unix_addr_len) != 0) {
190 PLOG(ERROR) << "bind " << pipe_name; 195 PLOG(ERROR) << "bind " << pipe_path;
191 if (HANDLE_EINTR(close(fd)) < 0) 196 if (HANDLE_EINTR(close(fd)) < 0)
192 PLOG(ERROR) << "close " << pipe_name; 197 PLOG(ERROR) << "close " << pipe_path;
193 return false; 198 return false;
194 } 199 }
195 200
196 // Start listening on the socket. 201 // Start listening on the socket.
197 const int listen_queue_length = 1; 202 const int listen_queue_length = 1;
198 if (listen(fd, listen_queue_length) != 0) { 203 if (listen(fd, listen_queue_length) != 0) {
199 PLOG(ERROR) << "listen " << pipe_name; 204 PLOG(ERROR) << "listen " << pipe_path;
200 if (HANDLE_EINTR(close(fd)) < 0) 205 if (HANDLE_EINTR(close(fd)) < 0)
201 PLOG(ERROR) << "close " << pipe_name; 206 PLOG(ERROR) << "close " << pipe_path;
202 return false; 207 return false;
203 } 208 }
204 209
205 *server_listen_fd = fd; 210 *server_listen_fd = fd;
206 return true; 211 return true;
207 } 212 }
208 213
209 // Accept a connection on a socket we are listening to. 214 // Accept a connection on a socket we are listening to.
210 bool ServerAcceptConnection(int server_listen_fd, int* server_socket) { 215 bool ServerAcceptConnection(int server_listen_fd, int* server_socket) {
211 DCHECK(server_socket); 216 DCHECK(server_socket);
212 217
213 int accept_fd = HANDLE_EINTR(accept(server_listen_fd, NULL, 0)); 218 int accept_fd = HANDLE_EINTR(accept(server_listen_fd, NULL, 0));
214 if (accept_fd < 0) 219 if (accept_fd < 0)
215 return false; 220 return false;
216 if (fcntl(accept_fd, F_SETFL, O_NONBLOCK) == -1) { 221 if (fcntl(accept_fd, F_SETFL, O_NONBLOCK) == -1) {
217 PLOG(ERROR) << "fcntl(O_NONBLOCK) " << accept_fd; 222 PLOG(ERROR) << "fcntl(O_NONBLOCK) " << accept_fd;
218 if (HANDLE_EINTR(close(accept_fd)) < 0) 223 if (HANDLE_EINTR(close(accept_fd)) < 0)
219 PLOG(ERROR) << "close " << accept_fd; 224 PLOG(ERROR) << "close " << accept_fd;
220 return false; 225 return false;
221 } 226 }
222 227
223 *server_socket = accept_fd; 228 *server_socket = accept_fd;
224 return true; 229 return true;
225 } 230 }
226 231
227 bool CreateClientUnixDomainSocket(const std::string& pipe_name, 232 bool CreateClientUnixDomainSocket(const std::string& pipe_name,
228 int* client_socket) { 233 int* client_socket) {
234 FilePath temp_dir;
235 PathService::Get(base::DIR_TEMP, &temp_dir);
236 std::string pipe_path= temp_dir.value()+ "/" + pipe_name;
229 DCHECK(client_socket); 237 DCHECK(client_socket);
230 DCHECK_GT(pipe_name.length(), 0u); 238 DCHECK_GT(pipe_path.length(), 0u);
231 DCHECK_LT(pipe_name.length(), kMaxPipeNameLength); 239 DCHECK_LT(pipe_path.length(), kMaxPipeNameLength);
232 240
233 if (pipe_name.length() == 0 || pipe_name.length() >= kMaxPipeNameLength) { 241 if (pipe_path.length() == 0 || pipe_path.length() >= kMaxPipeNameLength) {
234 return false; 242 return false;
235 } 243 }
236 244
237 // Create socket. 245 // Create socket.
238 int fd = socket(AF_UNIX, SOCK_STREAM, 0); 246 int fd = socket(AF_UNIX, SOCK_STREAM, 0);
239 if (fd < 0) { 247 if (fd < 0) {
240 PLOG(ERROR) << "socket " << pipe_name; 248 PLOG(ERROR) << "socket " << pipe_path;
241 return false; 249 return false;
242 } 250 }
243 251
244 // Make socket non-blocking 252 // Make socket non-blocking
245 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { 253 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
246 PLOG(ERROR) << "fcntl(O_NONBLOCK) " << pipe_name; 254 PLOG(ERROR) << "fcntl(O_NONBLOCK) " << pipe_path;
247 if (HANDLE_EINTR(close(fd)) < 0) 255 if (HANDLE_EINTR(close(fd)) < 0)
248 PLOG(ERROR) << "close " << pipe_name; 256 PLOG(ERROR) << "close " << pipe_path;
249 return false; 257 return false;
250 } 258 }
251 259
252 // Create server side of socket. 260 // Create server side of socket.
253 struct sockaddr_un server_unix_addr; 261 struct sockaddr_un server_unix_addr;
254 memset(&server_unix_addr, 0, sizeof(server_unix_addr)); 262 memset(&server_unix_addr, 0, sizeof(server_unix_addr));
255 server_unix_addr.sun_family = AF_UNIX; 263 server_unix_addr.sun_family = AF_UNIX;
256 int path_len = snprintf(server_unix_addr.sun_path, IPC::kMaxPipeNameLength, 264 int path_len = snprintf(server_unix_addr.sun_path, IPC::kMaxPipeNameLength,
257 "%s", pipe_name.c_str()); 265 "%s", pipe_path.c_str());
258 DCHECK_EQ(static_cast<int>(pipe_name.length()), path_len); 266 DCHECK_EQ(static_cast<int>(pipe_path.length()), path_len);
259 size_t server_unix_addr_len = offsetof(struct sockaddr_un, 267 size_t server_unix_addr_len = offsetof(struct sockaddr_un,
260 sun_path) + path_len + 1; 268 sun_path) + path_len + 1;
261 269
262 if (HANDLE_EINTR(connect(fd, reinterpret_cast<sockaddr*>(&server_unix_addr), 270 if (HANDLE_EINTR(connect(fd, reinterpret_cast<sockaddr*>(&server_unix_addr),
263 server_unix_addr_len)) != 0) { 271 server_unix_addr_len)) != 0) {
264 PLOG(ERROR) << "connect " << pipe_name; 272 PLOG(ERROR) << "connect " << pipe_path;
265 if (HANDLE_EINTR(close(fd)) < 0) 273 if (HANDLE_EINTR(close(fd)) < 0)
266 PLOG(ERROR) << "close " << pipe_name; 274 PLOG(ERROR) << "close " << pipe_path;
267 return false; 275 return false;
268 } 276 }
269 277
270 *client_socket = fd; 278 *client_socket = fd;
271 return true; 279 return true;
272 } 280 }
273 281
274 bool SocketWriteErrorIsRecoverable() { 282 bool SocketWriteErrorIsRecoverable() {
275 #if defined(OS_MACOSX) 283 #if defined(OS_MACOSX)
276 // On OS X if sendmsg() is trying to send fds between processes and there 284 // On OS X if sendmsg() is trying to send fds between processes and there
(...skipping 914 matching lines...) Expand 10 before | Expand all | Expand 10 after
1191 1199
1192 bool Channel::GetClientEuid(uid_t* client_euid) const { 1200 bool Channel::GetClientEuid(uid_t* client_euid) const {
1193 return channel_impl_->GetClientEuid(client_euid); 1201 return channel_impl_->GetClientEuid(client_euid);
1194 } 1202 }
1195 1203
1196 void Channel::ResetToAcceptingConnectionState() { 1204 void Channel::ResetToAcceptingConnectionState() {
1197 channel_impl_->ResetToAcceptingConnectionState(); 1205 channel_impl_->ResetToAcceptingConnectionState();
1198 } 1206 }
1199 1207
1200 } // namespace IPC 1208 } // namespace IPC
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698