OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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> |
(...skipping 15 matching lines...) Expand all Loading... |
26 #include "base/string_util.h" | 26 #include "base/string_util.h" |
27 #include "ipc/ipc_descriptors.h" | 27 #include "ipc/ipc_descriptors.h" |
28 #include "ipc/ipc_switches.h" | 28 #include "ipc/ipc_switches.h" |
29 #include "ipc/file_descriptor_set_posix.h" | 29 #include "ipc/file_descriptor_set_posix.h" |
30 #include "ipc/ipc_logging.h" | 30 #include "ipc/ipc_logging.h" |
31 #include "ipc/ipc_message_utils.h" | 31 #include "ipc/ipc_message_utils.h" |
32 | 32 |
33 namespace IPC { | 33 namespace IPC { |
34 | 34 |
35 // IPC channels on Windows use named pipes (CreateNamedPipe()) with | 35 // IPC channels on Windows use named pipes (CreateNamedPipe()) with |
36 // channel ids as the pipe names. Channels on POSIX use sockets as | 36 // channel ids as the pipe names. Channels on POSIX use anonymous |
37 // pipes These don't quite line up. | 37 // Unix domain sockets created via socketpair() as pipes. These don't |
| 38 // quite line up. |
38 // | 39 // |
39 // When creating a child subprocess we use a socket pair and the parent side of | 40 // When creating a child subprocess, the parent side of the fork |
40 // the fork arranges it such that the initial control channel ends up on the | 41 // arranges it such that the initial control channel ends up on the |
41 // magic file descriptor kPrimaryIPCChannel in the child. Future | 42 // magic file descriptor kPrimaryIPCChannel in the child. Future |
42 // connections (file descriptors) can then be passed via that | 43 // connections (file descriptors) can then be passed via that |
43 // connection via sendmsg(). | 44 // connection via sendmsg(). |
44 // | |
45 // A POSIX IPC channel can also be set up as a server for a bound UNIX domain | |
46 // socket, and will handle multiple connect and disconnect sequences. Currently | |
47 // it is limited to one connection at a time. | |
48 | 45 |
49 //------------------------------------------------------------------------------ | 46 //------------------------------------------------------------------------------ |
50 namespace { | 47 namespace { |
51 | 48 |
52 // The PipeMap class works around this quirk related to unit tests: | 49 // The PipeMap class works around this quirk related to unit tests: |
53 // | 50 // |
54 // When running as a server, we install the client socket in a | 51 // When running as a server, we install the client socket in a |
55 // specific file descriptor number (@kPrimaryIPCChannel). However, we | 52 // specific file descriptor number (@kPrimaryIPCChannel). However, we |
56 // also have to support the case where we are running unittests in the | 53 // also have to support the case where we are running unittests in the |
57 // same process. (We do not support forking without execing.) | 54 // same process. (We do not support forking without execing.) |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 } | 121 } |
125 | 122 |
126 private: | 123 private: |
127 Lock lock_; | 124 Lock lock_; |
128 typedef std::map<std::string, int> ChannelToFDMap; | 125 typedef std::map<std::string, int> ChannelToFDMap; |
129 ChannelToFDMap map_; | 126 ChannelToFDMap map_; |
130 | 127 |
131 friend struct DefaultSingletonTraits<PipeMap>; | 128 friend struct DefaultSingletonTraits<PipeMap>; |
132 }; | 129 }; |
133 | 130 |
| 131 // Used to map a channel name to the equivalent FD # in the current process. |
| 132 // Returns -1 if the channel is unknown. |
| 133 int ChannelNameToFD(const std::string& channel_id) { |
| 134 // See the large block comment above PipeMap for the reasoning here. |
| 135 const int fd = PipeMap::GetInstance()->Lookup(channel_id); |
| 136 |
| 137 if (fd != -1) { |
| 138 int dup_fd = dup(fd); |
| 139 if (dup_fd < 0) |
| 140 PLOG(FATAL) << "dup(" << fd << ") " << channel_id; |
| 141 return dup_fd; |
| 142 } |
| 143 |
| 144 return fd; |
| 145 } |
| 146 |
134 //------------------------------------------------------------------------------ | 147 //------------------------------------------------------------------------------ |
135 // The standard size on linux is 108, mac is 104. To maintain consistency | 148 // The standard size on linux is 108, mac is 104. To maintain consistency |
136 // across platforms we standardize on the smaller value. | 149 // across platforms we standardize on the smaller value. |
137 const size_t kMaxPipeNameLength = 104; | 150 const size_t kMaxPipeNameLength = 104; |
138 COMPILE_ASSERT(sizeof(((sockaddr_un*)0)->sun_path) >= kMaxPipeNameLength, | 151 COMPILE_ASSERT(sizeof(((sockaddr_un*)0)->sun_path) >= kMaxPipeNameLength, |
139 BAD_SUN_PATH_LENGTH); | 152 BAD_SUN_PATH_LENGTH); |
140 | 153 |
141 // Creates a unix domain socket bound to the specified name that is listening | 154 // Creates a Fifo with the specified name ready to listen on. |
142 // for connections. | 155 bool CreateServerFifo(const std::string& pipe_name, int* server_listen_fd) { |
143 bool CreateServerUnixDomainSocket(const std::string& pipe_name, | |
144 int* server_listen_fd) { | |
145 DCHECK(server_listen_fd); | 156 DCHECK(server_listen_fd); |
146 DCHECK_GT(pipe_name.length(), 0u); | 157 DCHECK_GT(pipe_name.length(), 0u); |
147 DCHECK_LT(pipe_name.length(), kMaxPipeNameLength); | 158 DCHECK_LT(pipe_name.length(), kMaxPipeNameLength); |
148 | 159 |
149 if (pipe_name.length() == 0 || pipe_name.length() >= kMaxPipeNameLength) { | 160 if (pipe_name.length() == 0 || pipe_name.length() >= kMaxPipeNameLength) { |
150 return false; | 161 return false; |
151 } | 162 } |
152 | 163 |
153 // Create socket. | 164 // Create socket. |
154 int fd = socket(AF_UNIX, SOCK_STREAM, 0); | 165 int fd = socket(AF_UNIX, SOCK_STREAM, 0); |
155 if (fd < 0) { | 166 if (fd < 0) { |
156 return false; | 167 return false; |
157 } | 168 } |
158 | 169 |
159 // Make socket non-blocking | 170 // Make socket non-blocking |
160 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { | 171 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { |
161 PLOG(ERROR) << "fcntl(O_NONBLOCK) " << pipe_name; | 172 PLOG(ERROR) << "fcntl " << pipe_name; |
162 if (HANDLE_EINTR(close(fd)) < 0) | 173 if (HANDLE_EINTR(close(fd)) < 0) |
163 PLOG(ERROR) << "close " << pipe_name; | 174 PLOG(ERROR) << "close " << pipe_name; |
164 return false; | 175 return false; |
165 } | 176 } |
166 | 177 |
167 // Delete any old FS instances. | 178 // Delete any old FS instances. |
168 unlink(pipe_name.c_str()); | 179 unlink(pipe_name.c_str()); |
169 | 180 |
170 // Create unix_addr structure | 181 // Create unix_addr structure |
171 struct sockaddr_un unix_addr; | 182 struct sockaddr_un unix_addr; |
(...skipping 18 matching lines...) Expand all Loading... |
190 PLOG(ERROR) << "listen " << pipe_name; | 201 PLOG(ERROR) << "listen " << pipe_name; |
191 if (HANDLE_EINTR(close(fd)) < 0) | 202 if (HANDLE_EINTR(close(fd)) < 0) |
192 PLOG(ERROR) << "close " << pipe_name; | 203 PLOG(ERROR) << "close " << pipe_name; |
193 return false; | 204 return false; |
194 } | 205 } |
195 | 206 |
196 *server_listen_fd = fd; | 207 *server_listen_fd = fd; |
197 return true; | 208 return true; |
198 } | 209 } |
199 | 210 |
200 // Accept a connection on a socket we are listening to. | 211 // Accept a connection on a fifo. |
201 bool ServerAcceptConnection(int server_listen_fd, int* server_socket) { | 212 bool ServerAcceptFifoConnection(int server_listen_fd, int* server_socket) { |
202 DCHECK(server_socket); | 213 DCHECK(server_socket); |
203 | 214 |
204 int accept_fd = HANDLE_EINTR(accept(server_listen_fd, NULL, 0)); | 215 int accept_fd = HANDLE_EINTR(accept(server_listen_fd, NULL, 0)); |
205 if (accept_fd < 0) | 216 if (accept_fd < 0) |
206 return false; | 217 return false; |
207 if (fcntl(accept_fd, F_SETFL, O_NONBLOCK) == -1) { | 218 if (fcntl(accept_fd, F_SETFL, O_NONBLOCK) == -1) { |
208 PLOG(ERROR) << "fcntl(O_NONBLOCK) " << accept_fd; | 219 PLOG(ERROR) << "fcntl " << accept_fd; |
209 if (HANDLE_EINTR(close(accept_fd)) < 0) | 220 if (HANDLE_EINTR(close(accept_fd)) < 0) |
210 PLOG(ERROR) << "close " << accept_fd; | 221 PLOG(ERROR) << "close " << accept_fd; |
211 return false; | 222 return false; |
212 } | 223 } |
213 | 224 |
214 *server_socket = accept_fd; | 225 *server_socket = accept_fd; |
215 return true; | 226 return true; |
216 } | 227 } |
217 | 228 |
218 bool CreateClientUnixDomainSocket(const std::string& pipe_name, | 229 bool ClientConnectToFifo(const std::string &pipe_name, int* client_socket) { |
219 int* client_socket) { | |
220 DCHECK(client_socket); | 230 DCHECK(client_socket); |
221 DCHECK_GT(pipe_name.length(), 0u); | 231 DCHECK_GT(pipe_name.length(), 0u); |
222 DCHECK_LT(pipe_name.length(), kMaxPipeNameLength); | 232 DCHECK_LT(pipe_name.length(), kMaxPipeNameLength); |
223 | 233 |
224 if (pipe_name.length() == 0 || pipe_name.length() >= kMaxPipeNameLength) { | 234 if (pipe_name.length() == 0 || pipe_name.length() >= kMaxPipeNameLength) { |
225 return false; | 235 return false; |
226 } | 236 } |
227 | 237 |
228 // Create socket. | 238 // Create socket. |
229 int fd = socket(AF_UNIX, SOCK_STREAM, 0); | 239 int fd = socket(AF_UNIX, SOCK_STREAM, 0); |
230 if (fd < 0) { | 240 if (fd < 0) { |
231 PLOG(ERROR) << "socket " << pipe_name; | 241 PLOG(ERROR) << "socket " << pipe_name; |
232 return false; | 242 return false; |
233 } | 243 } |
234 | 244 |
235 // Make socket non-blocking | 245 // Make socket non-blocking |
236 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { | 246 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { |
237 PLOG(ERROR) << "fcntl(O_NONBLOCK) " << pipe_name; | 247 PLOG(ERROR) << "fcntl " << pipe_name; |
238 if (HANDLE_EINTR(close(fd)) < 0) | 248 if (HANDLE_EINTR(close(fd)) < 0) |
239 PLOG(ERROR) << "close " << pipe_name; | 249 PLOG(ERROR) << "close " << pipe_name; |
240 return false; | 250 return false; |
241 } | 251 } |
242 | 252 |
243 // Create server side of socket. | 253 // Create server side of socket. |
244 struct sockaddr_un server_unix_addr; | 254 struct sockaddr_un server_unix_addr; |
245 memset(&server_unix_addr, 0, sizeof(server_unix_addr)); | 255 memset(&server_unix_addr, 0, sizeof(server_unix_addr)); |
246 server_unix_addr.sun_family = AF_UNIX; | 256 server_unix_addr.sun_family = AF_UNIX; |
247 snprintf(server_unix_addr.sun_path, kMaxPipeNameLength, "%s", | 257 snprintf(server_unix_addr.sun_path, kMaxPipeNameLength, "%s", |
(...skipping 22 matching lines...) Expand all Loading... |
270 // EMSGSIZE presents a problem since the system APIs can only call us when | 280 // EMSGSIZE presents a problem since the system APIs can only call us when |
271 // there's room in the socket buffer and not when there is "enough" room. | 281 // there's room in the socket buffer and not when there is "enough" room. |
272 // | 282 // |
273 // The current behavior is to return to the event loop when EMSGSIZE is | 283 // The current behavior is to return to the event loop when EMSGSIZE is |
274 // received and hopefull service another FD. This is however still | 284 // received and hopefull service another FD. This is however still |
275 // technically a busy wait since the event loop will call us right back until | 285 // technically a busy wait since the event loop will call us right back until |
276 // the receiver has read enough data to allow passing the FD over atomically. | 286 // the receiver has read enough data to allow passing the FD over atomically. |
277 return errno == EAGAIN || errno == EMSGSIZE; | 287 return errno == EAGAIN || errno == EMSGSIZE; |
278 #else | 288 #else |
279 return errno == EAGAIN; | 289 return errno == EAGAIN; |
280 #endif // OS_MACOSX | 290 #endif |
281 } | 291 } |
282 | 292 |
283 } // namespace | 293 } // namespace |
284 //------------------------------------------------------------------------------ | 294 //------------------------------------------------------------------------------ |
285 | 295 |
286 Channel::ChannelImpl::ChannelImpl(const IPC::ChannelHandle& channel_handle, | 296 Channel::ChannelImpl::ChannelImpl(const IPC::ChannelHandle& channel_handle, |
287 Mode mode, Listener* listener) | 297 Mode mode, Listener* listener) |
288 : mode_(mode), | 298 : mode_(mode), |
289 is_blocked_on_write_(false), | 299 is_blocked_on_write_(false), |
290 waiting_connect_(true), | |
291 message_send_bytes_written_(0), | 300 message_send_bytes_written_(0), |
| 301 uses_fifo_(CommandLine::ForCurrentProcess()->HasSwitch( |
| 302 switches::kIPCUseFIFO)), |
292 server_listen_pipe_(-1), | 303 server_listen_pipe_(-1), |
293 pipe_(-1), | 304 pipe_(-1), |
294 client_pipe_(-1), | 305 client_pipe_(-1), |
295 #if defined(IPC_USES_READWRITE) | 306 #if defined(IPC_USES_READWRITE) |
296 fd_pipe_(-1), | 307 fd_pipe_(-1), |
297 remote_fd_pipe_(-1), | 308 remote_fd_pipe_(-1), |
298 #endif // IPC_USES_READWRITE | 309 #endif |
299 pipe_name_(channel_handle.name), | |
300 listener_(listener), | 310 listener_(listener), |
| 311 waiting_connect_(true), |
301 factory_(this) { | 312 factory_(this) { |
302 // Check to see if we want to implement using domain sockets. | 313 if (!CreatePipe(channel_handle, mode_)) { |
303 bool uses_domain_socket = false; | |
304 bool listening_socket = false; | |
305 if (mode_ == MODE_NAMED_SERVER) { | |
306 uses_domain_socket = true; | |
307 listening_socket = true; | |
308 mode_ = MODE_SERVER; | |
309 } else if (mode_ == MODE_NAMED_CLIENT) { | |
310 uses_domain_socket = true; | |
311 mode_ = MODE_CLIENT; | |
312 } | |
313 if (!CreatePipe(channel_handle, uses_domain_socket, listening_socket)) { | |
314 // The pipe may have been closed already. | |
315 const char *modestr = (mode_ == MODE_SERVER | |
316 || mode_ == MODE_NAMED_SERVER) ? "server" : "client"; | |
317 // The pipe may have been closed already. | 314 // The pipe may have been closed already. |
318 LOG(WARNING) << "Unable to create pipe named \"" << channel_handle.name | 315 LOG(WARNING) << "Unable to create pipe named \"" << channel_handle.name |
319 << "\" in " << modestr << " mode"; | 316 << "\" in " << (mode_ == MODE_SERVER ? "server" : "client") |
| 317 << " mode"; |
320 } | 318 } |
321 } | 319 } |
322 | 320 |
323 Channel::ChannelImpl::~ChannelImpl() { | 321 Channel::ChannelImpl::~ChannelImpl() { |
324 Close(); | 322 Close(); |
325 } | 323 } |
326 | 324 |
| 325 // static |
| 326 void AddChannelSocket(const std::string& name, int socket) { |
| 327 PipeMap::GetInstance()->Insert(name, socket); |
| 328 } |
| 329 |
| 330 // static |
| 331 void RemoveAndCloseChannelSocket(const std::string& name) { |
| 332 PipeMap::GetInstance()->RemoveAndClose(name); |
| 333 } |
| 334 |
| 335 // static |
| 336 bool ChannelSocketExists(const std::string& name) { |
| 337 return PipeMap::GetInstance()->Lookup(name) != -1; |
| 338 } |
| 339 |
| 340 // static |
327 bool SocketPair(int* fd1, int* fd2) { | 341 bool SocketPair(int* fd1, int* fd2) { |
328 int pipe_fds[2]; | 342 int pipe_fds[2]; |
329 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pipe_fds) != 0) { | 343 if (socketpair(AF_UNIX, SOCK_STREAM, 0, pipe_fds) != 0) { |
330 PLOG(ERROR) << "socketpair()"; | 344 PLOG(ERROR) << "socketpair()"; |
331 return false; | 345 return false; |
332 } | 346 } |
333 | 347 |
334 // Set both ends to be non-blocking. | 348 // Set both ends to be non-blocking. |
335 if (fcntl(pipe_fds[0], F_SETFL, O_NONBLOCK) == -1 || | 349 if (fcntl(pipe_fds[0], F_SETFL, O_NONBLOCK) == -1 || |
336 fcntl(pipe_fds[1], F_SETFL, O_NONBLOCK) == -1) { | 350 fcntl(pipe_fds[1], F_SETFL, O_NONBLOCK) == -1) { |
337 PLOG(ERROR) << "fcntl(O_NONBLOCK)"; | 351 PLOG(ERROR) << "fcntl(O_NONBLOCK)"; |
338 if (HANDLE_EINTR(close(pipe_fds[0])) < 0) | 352 if (HANDLE_EINTR(close(pipe_fds[0])) < 0) |
339 PLOG(ERROR) << "close"; | 353 PLOG(ERROR) << "close"; |
340 if (HANDLE_EINTR(close(pipe_fds[1])) < 0) | 354 if (HANDLE_EINTR(close(pipe_fds[1])) < 0) |
341 PLOG(ERROR) << "close"; | 355 PLOG(ERROR) << "close"; |
342 return false; | 356 return false; |
343 } | 357 } |
344 | 358 |
345 *fd1 = pipe_fds[0]; | 359 *fd1 = pipe_fds[0]; |
346 *fd2 = pipe_fds[1]; | 360 *fd2 = pipe_fds[1]; |
347 | 361 |
348 return true; | 362 return true; |
349 } | 363 } |
350 | 364 |
351 bool Channel::ChannelImpl::CreatePipe(const IPC::ChannelHandle& channel_handle, | 365 bool Channel::ChannelImpl::CreatePipe(const IPC::ChannelHandle &channel_handle, |
352 bool uses_domain_sockets, | 366 Mode mode) { |
353 bool listening_socket) { | |
354 DCHECK(server_listen_pipe_ == -1 && pipe_ == -1); | 367 DCHECK(server_listen_pipe_ == -1 && pipe_ == -1); |
355 | 368 pipe_name_ = channel_handle.name; |
356 // Four possible cases: | 369 pipe_ = channel_handle.socket.fd; |
357 // 1) It's a channel wrapping a pipe that is given to us. | 370 if (uses_fifo_) { |
358 // 2) It's for a named channel, so we create it. | 371 // This only happens in unit tests; see the comment above PipeMap. |
359 // 3) It's for a client that we implement ourself. This is used | 372 // TODO(playmobil): We shouldn't need to create fifos on disk. |
360 // in unittesting. | 373 // TODO(playmobil): If we do, they should be in the user data directory. |
361 // 4) It's the initial IPC channel: | 374 // TODO(playmobil): Cleanup any stale fifos. |
362 // 4a) Client side: Pull the pipe out of the GlobalDescriptors set. | 375 if (mode == MODE_SERVER) { |
363 // 4b) Server side: create the pipe. | 376 if (!CreateServerFifo(pipe_name_, &server_listen_pipe_)) { |
364 | |
365 if (channel_handle.socket.fd != -1) { | |
366 // Case 1 from comment above. | |
367 pipe_ = channel_handle.socket.fd; | |
368 #if defined(IPC_USES_READWRITE) | |
369 // Test the socket passed into us to make sure it is nonblocking. | |
370 // We don't want to call read/write on a blocking socket. | |
371 int value = fcntl(pipe_, F_GETFL); | |
372 if (value == -1) { | |
373 PLOG(ERROR) << "fcntl(F_GETFL) " << pipe_name_; | |
374 return false; | |
375 } | |
376 if (!(value & O_NONBLOCK)) { | |
377 LOG(ERROR) << "Socket " << pipe_name_ << " must be O_NONBLOCK"; | |
378 return false; | |
379 } | |
380 #endif // IPC_USES_READWRITE | |
381 } else if (uses_domain_sockets) { | |
382 // Case 2 from comment above. | |
383 must_unlink_ = true; | |
384 if (mode_ == MODE_SERVER) { | |
385 if (!CreateServerUnixDomainSocket(pipe_name_, &pipe_)) { | |
386 return false; | 377 return false; |
387 } | 378 } |
388 } else if (mode_ == MODE_CLIENT) { | 379 } else { |
389 if (!CreateClientUnixDomainSocket(pipe_name_, &pipe_)) { | 380 if (!ClientConnectToFifo(pipe_name_, &pipe_)) { |
390 return false; | 381 return false; |
391 } | 382 } |
| 383 waiting_connect_ = false; |
392 } | 384 } |
393 } else { | 385 } else { |
394 pipe_ = PipeMap::GetInstance()->Lookup(pipe_name_); | 386 // This is the normal (non-unit-test) case, where we're using sockets. |
395 if (mode_ == MODE_CLIENT) { | 387 // Three possible cases: |
396 if (pipe_ != -1) { | 388 // 1) It's for a channel we already have a pipe for; reuse it. |
397 // Case 3 from comment above. | 389 // 2) It's the initial IPC channel: |
398 // We only allow one connection. | 390 // 2a) Server side: create the pipe. |
399 pipe_ = HANDLE_EINTR(dup(pipe_)); | 391 // 2b) Client side: Pull the pipe out of the GlobalDescriptors set. |
400 PipeMap::GetInstance()->RemoveAndClose(pipe_name_); | 392 if (pipe_ < 0) { |
| 393 pipe_ = ChannelNameToFD(pipe_name_); |
| 394 } |
| 395 if (pipe_ < 0) { |
| 396 // Initial IPC channel. |
| 397 if (mode == MODE_SERVER) { |
| 398 if (!SocketPair(&pipe_, &client_pipe_)) |
| 399 return false; |
| 400 AddChannelSocket(pipe_name_, client_pipe_); |
401 } else { | 401 } else { |
402 // Case 4a from comment above. | |
403 // Guard against inappropriate reuse of the initial IPC channel. If | 402 // Guard against inappropriate reuse of the initial IPC channel. If |
404 // an IPC channel closes and someone attempts to reuse it by name, the | 403 // an IPC channel closes and someone attempts to reuse it by name, the |
405 // initial channel must not be recycled here. http://crbug.com/26754. | 404 // initial channel must not be recycled here. http://crbug.com/26754. |
406 static bool used_initial_channel = false; | 405 static bool used_initial_channel = false; |
407 if (used_initial_channel) { | 406 if (used_initial_channel) { |
408 LOG(FATAL) << "Denying attempt to reuse initial IPC channel for " | 407 LOG(FATAL) << "Denying attempt to reuse initial IPC channel for " |
409 << pipe_name_; | 408 << pipe_name_; |
410 return false; | 409 return false; |
411 } | 410 } |
412 used_initial_channel = true; | 411 used_initial_channel = true; |
413 | 412 |
414 pipe_ = base::GlobalDescriptors::GetInstance()->Get(kPrimaryIPCChannel); | 413 pipe_ = base::GlobalDescriptors::GetInstance()->Get(kPrimaryIPCChannel); |
415 } | 414 } |
416 } else if (mode_ == MODE_SERVER) { | |
417 // Case 4b from comment above. | |
418 if (pipe_ != -1) { | |
419 LOG(ERROR) << "Server already exists for " << pipe_name_; | |
420 return false; | |
421 } | |
422 if (!SocketPair(&pipe_, &client_pipe_)) | |
423 return false; | |
424 PipeMap::GetInstance()->Insert(pipe_name_, client_pipe_); | |
425 } else { | 415 } else { |
426 LOG(FATAL) << "Unknown mode " << mode_; | 416 waiting_connect_ = mode == MODE_SERVER; |
427 return false; | |
428 } | 417 } |
429 } | 418 } |
430 | 419 |
431 if (mode_ == MODE_SERVER) { | 420 // Create the Hello message to be sent when Connect is called |
432 if (listening_socket) { | 421 scoped_ptr<Message> msg(new Message(MSG_ROUTING_NONE, |
433 server_listen_pipe_ = pipe_; | 422 HELLO_MESSAGE_TYPE, |
434 pipe_ = -1; | 423 IPC::Message::PRIORITY_NORMAL)); |
| 424 #if defined(IPC_USES_READWRITE) |
| 425 if (!uses_fifo_) { |
| 426 // Create a dedicated socketpair() for exchanging file descriptors. |
| 427 // See comments for IPC_USES_READWRITE for details. |
| 428 if (mode == MODE_SERVER) { |
| 429 fd_pipe_ = -1; |
| 430 } else if (remote_fd_pipe_ == -1) { |
| 431 if (!SocketPair(&fd_pipe_, &remote_fd_pipe_)) { |
| 432 return false; |
| 433 } |
435 } | 434 } |
436 } | 435 } |
| 436 #endif |
| 437 if (!msg->WriteInt(base::GetCurrentProcId())) { |
| 438 Close(); |
| 439 return false; |
| 440 } |
437 | 441 |
438 #if defined(IPC_USES_READWRITE) | 442 output_queue_.push(msg.release()); |
439 // Create a dedicated socketpair() for exchanging file descriptors. | |
440 // See comments for IPC_USES_READWRITE for details. | |
441 if (mode_ == MODE_CLIENT) { | |
442 if (!SocketPair(&fd_pipe_, &remote_fd_pipe_)) { | |
443 return false; | |
444 } | |
445 } | |
446 #endif // IPC_USES_READWRITE | |
447 | |
448 return true; | 443 return true; |
449 } | 444 } |
450 | 445 |
451 bool Channel::ChannelImpl::Connect() { | 446 bool Channel::ChannelImpl::Connect() { |
452 if (server_listen_pipe_ == -1 && pipe_ == -1) { | 447 if (mode_ == MODE_SERVER && uses_fifo_) { |
453 NOTREACHED() << "Must call create on a channel before calling connect"; | 448 if (server_listen_pipe_ == -1) { |
454 return false; | 449 return false; |
455 } | 450 } |
456 | |
457 bool did_connect = true; | |
458 if (server_listen_pipe_ != -1) { | |
459 // Watch the pipe for connections, and turn any connections into | |
460 // active sockets. | |
461 MessageLoopForIO::current()->WatchFileDescriptor( | 451 MessageLoopForIO::current()->WatchFileDescriptor( |
462 server_listen_pipe_, | 452 server_listen_pipe_, |
463 true, | 453 true, |
464 MessageLoopForIO::WATCH_READ, | 454 MessageLoopForIO::WATCH_READ, |
465 &server_listen_connection_watcher_, | 455 &server_listen_connection_watcher_, |
466 this); | 456 this); |
467 } else { | 457 } else { |
468 did_connect = AcceptConnection(); | 458 if (pipe_ == -1) { |
| 459 return false; |
| 460 } |
| 461 MessageLoopForIO::current()->WatchFileDescriptor( |
| 462 pipe_, |
| 463 true, |
| 464 MessageLoopForIO::WATCH_READ, |
| 465 &read_watcher_, |
| 466 this); |
| 467 waiting_connect_ = mode_ == MODE_SERVER; |
469 } | 468 } |
470 return did_connect; | 469 |
| 470 if (!waiting_connect_) |
| 471 return ProcessOutgoingMessages(); |
| 472 return true; |
471 } | 473 } |
472 | 474 |
473 bool Channel::ChannelImpl::ProcessIncomingMessages() { | 475 bool Channel::ChannelImpl::ProcessIncomingMessages() { |
474 ssize_t bytes_read = 0; | 476 ssize_t bytes_read = 0; |
475 | 477 |
476 struct msghdr msg = {0}; | 478 struct msghdr msg = {0}; |
477 struct iovec iov = {input_buf_, Channel::kReadBufferSize}; | 479 struct iovec iov = {input_buf_, Channel::kReadBufferSize}; |
478 | 480 |
479 msg.msg_iovlen = 1; | 481 msg.msg_iovlen = 1; |
480 msg.msg_control = input_cmsg_buf_; | 482 msg.msg_control = input_cmsg_buf_; |
481 | 483 |
482 for (;;) { | 484 for (;;) { |
483 msg.msg_iov = &iov; | 485 msg.msg_iov = &iov; |
484 | 486 |
485 if (bytes_read == 0) { | 487 if (bytes_read == 0) { |
486 if (pipe_ == -1) | 488 if (pipe_ == -1) |
487 return false; | 489 return false; |
488 | 490 |
489 // Read from pipe. | 491 // Read from pipe. |
490 // recvmsg() returns 0 if the connection has closed or EAGAIN if no data | 492 // recvmsg() returns 0 if the connection has closed or EAGAIN if no data |
491 // is waiting on the pipe. | 493 // is waiting on the pipe. |
492 #if defined(IPC_USES_READWRITE) | 494 #if defined(IPC_USES_READWRITE) |
493 if (fd_pipe_ >= 0) { | 495 if (fd_pipe_ >= 0) { |
494 bytes_read = HANDLE_EINTR(read(pipe_, input_buf_, | 496 bytes_read = HANDLE_EINTR(read(pipe_, input_buf_, |
495 Channel::kReadBufferSize)); | 497 Channel::kReadBufferSize)); |
496 msg.msg_controllen = 0; | 498 msg.msg_controllen = 0; |
497 } else | 499 } else |
498 #endif // IPC_USES_READWRITE | 500 #endif |
499 { | 501 { |
500 msg.msg_controllen = sizeof(input_cmsg_buf_); | 502 msg.msg_controllen = sizeof(input_cmsg_buf_); |
501 bytes_read = HANDLE_EINTR(recvmsg(pipe_, &msg, MSG_DONTWAIT)); | 503 bytes_read = HANDLE_EINTR(recvmsg(pipe_, &msg, MSG_DONTWAIT)); |
502 } | 504 } |
503 if (bytes_read < 0) { | 505 if (bytes_read < 0) { |
504 if (errno == EAGAIN) { | 506 if (errno == EAGAIN) { |
505 return true; | 507 return true; |
506 #if defined(OS_MACOSX) | 508 #if defined(OS_MACOSX) |
507 } else if (errno == EPERM) { | 509 } else if (errno == EPERM) { |
508 // On OSX, reading from a pipe with no listener returns EPERM | 510 // On OSX, reading from a pipe with no listener returns EPERM |
509 // treat this as a special case to prevent spurious error messages | 511 // treat this as a special case to prevent spurious error messages |
510 // to the console. | 512 // to the console. |
511 return false; | 513 return false; |
512 #endif // OS_MACOSX | 514 #endif // defined(OS_MACOSX) |
513 } else if (errno == ECONNRESET || errno == EPIPE) { | 515 } else if (errno == ECONNRESET || errno == EPIPE) { |
514 return false; | 516 return false; |
515 } else { | 517 } else { |
516 PLOG(ERROR) << "pipe error (" << pipe_ << ")"; | 518 PLOG(ERROR) << "pipe error (" << pipe_ << ")"; |
517 return false; | 519 return false; |
518 } | 520 } |
519 } else if (bytes_read == 0) { | 521 } else if (bytes_read == 0) { |
520 // The pipe has closed... | 522 // The pipe has closed... |
521 return false; | 523 return false; |
522 } | 524 } |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
557 DCHECK(payload_len % sizeof(int) == 0); | 559 DCHECK(payload_len % sizeof(int) == 0); |
558 wire_fds = reinterpret_cast<int*>(CMSG_DATA(cmsg)); | 560 wire_fds = reinterpret_cast<int*>(CMSG_DATA(cmsg)); |
559 num_wire_fds = payload_len / 4; | 561 num_wire_fds = payload_len / 4; |
560 | 562 |
561 if (msg.msg_flags & MSG_CTRUNC) { | 563 if (msg.msg_flags & MSG_CTRUNC) { |
562 LOG(ERROR) << "SCM_RIGHTS message was truncated" | 564 LOG(ERROR) << "SCM_RIGHTS message was truncated" |
563 << " cmsg_len:" << cmsg->cmsg_len | 565 << " cmsg_len:" << cmsg->cmsg_len |
564 << " fd:" << pipe_; | 566 << " fd:" << pipe_; |
565 for (unsigned i = 0; i < num_wire_fds; ++i) | 567 for (unsigned i = 0; i < num_wire_fds; ++i) |
566 if (HANDLE_EINTR(close(wire_fds[i])) < 0) | 568 if (HANDLE_EINTR(close(wire_fds[i])) < 0) |
567 PLOG(ERROR) << "close " << i; | 569 PLOG(ERROR) << "close" << i; |
568 return false; | 570 return false; |
569 } | 571 } |
570 break; | 572 break; |
571 } | 573 } |
572 } | 574 } |
573 } | 575 } |
574 | 576 |
575 // Process messages from input buffer. | 577 // Process messages from input buffer. |
576 const char *p; | 578 const char *p; |
577 const char *end; | 579 const char *end; |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
616 int len = static_cast<int>(message_tail - p); | 618 int len = static_cast<int>(message_tail - p); |
617 Message m(p, len); | 619 Message m(p, len); |
618 const uint16 header_fds = m.header()->num_fds; | 620 const uint16 header_fds = m.header()->num_fds; |
619 if (header_fds) { | 621 if (header_fds) { |
620 // the message has file descriptors | 622 // the message has file descriptors |
621 const char* error = NULL; | 623 const char* error = NULL; |
622 if (header_fds > num_fds - fds_i) { | 624 if (header_fds > num_fds - fds_i) { |
623 // the message has been completely received, but we didn't get | 625 // the message has been completely received, but we didn't get |
624 // enough file descriptors. | 626 // enough file descriptors. |
625 #if defined(IPC_USES_READWRITE) | 627 #if defined(IPC_USES_READWRITE) |
626 char dummy; | 628 if (!uses_fifo_) { |
627 struct iovec fd_pipe_iov = { &dummy, 1 }; | 629 char dummy; |
628 msg.msg_iov = &fd_pipe_iov; | 630 struct iovec fd_pipe_iov = { &dummy, 1 }; |
629 msg.msg_controllen = sizeof(input_cmsg_buf_); | 631 msg.msg_iov = &fd_pipe_iov; |
630 ssize_t n = HANDLE_EINTR(recvmsg(fd_pipe_, &msg, MSG_DONTWAIT)); | 632 msg.msg_controllen = sizeof(input_cmsg_buf_); |
631 if (n == 1 && msg.msg_controllen > 0) { | 633 ssize_t n = HANDLE_EINTR(recvmsg(fd_pipe_, &msg, MSG_DONTWAIT)); |
632 for (struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); cmsg; | 634 if (n == 1 && msg.msg_controllen > 0) { |
633 cmsg = CMSG_NXTHDR(&msg, cmsg)) { | 635 for (struct cmsghdr* cmsg = CMSG_FIRSTHDR(&msg); cmsg; |
634 if (cmsg->cmsg_level == SOL_SOCKET && | 636 cmsg = CMSG_NXTHDR(&msg, cmsg)) { |
635 cmsg->cmsg_type == SCM_RIGHTS) { | 637 if (cmsg->cmsg_level == SOL_SOCKET && |
636 const unsigned payload_len = cmsg->cmsg_len - CMSG_LEN(0); | 638 cmsg->cmsg_type == SCM_RIGHTS) { |
637 DCHECK(payload_len % sizeof(int) == 0); | 639 const unsigned payload_len = cmsg->cmsg_len - CMSG_LEN(0); |
638 wire_fds = reinterpret_cast<int*>(CMSG_DATA(cmsg)); | 640 DCHECK(payload_len % sizeof(int) == 0); |
639 num_wire_fds = payload_len / 4; | 641 wire_fds = reinterpret_cast<int*>(CMSG_DATA(cmsg)); |
| 642 num_wire_fds = payload_len / 4; |
640 | 643 |
641 if (msg.msg_flags & MSG_CTRUNC) { | 644 if (msg.msg_flags & MSG_CTRUNC) { |
642 LOG(ERROR) << "SCM_RIGHTS message was truncated" | 645 LOG(ERROR) << "SCM_RIGHTS message was truncated" |
643 << " cmsg_len:" << cmsg->cmsg_len | 646 << " cmsg_len:" << cmsg->cmsg_len |
644 << " fd:" << pipe_; | 647 << " fd:" << pipe_; |
645 for (unsigned i = 0; i < num_wire_fds; ++i) | 648 for (unsigned i = 0; i < num_wire_fds; ++i) |
646 if (HANDLE_EINTR(close(wire_fds[i])) < 0) | 649 if (HANDLE_EINTR(close(wire_fds[i])) < 0) |
647 PLOG(ERROR) << "close " << i; | 650 PLOG(ERROR) << "close" << i; |
648 return false; | 651 return false; |
| 652 } |
| 653 break; |
649 } | 654 } |
650 break; | |
651 } | 655 } |
652 } | 656 if (input_overflow_fds_.empty()) { |
653 if (input_overflow_fds_.empty()) { | 657 fds = wire_fds; |
654 fds = wire_fds; | 658 num_fds = num_wire_fds; |
655 num_fds = num_wire_fds; | 659 } else { |
656 } else { | 660 if (num_wire_fds > 0) { |
657 if (num_wire_fds > 0) { | 661 const size_t prev_size = input_overflow_fds_.size(); |
658 const size_t prev_size = input_overflow_fds_.size(); | 662 input_overflow_fds_.resize(prev_size + num_wire_fds); |
659 input_overflow_fds_.resize(prev_size + num_wire_fds); | 663 memcpy(&input_overflow_fds_[prev_size], wire_fds, |
660 memcpy(&input_overflow_fds_[prev_size], wire_fds, | 664 num_wire_fds * sizeof(int)); |
661 num_wire_fds * sizeof(int)); | 665 } |
| 666 fds = &input_overflow_fds_[0]; |
| 667 num_fds = input_overflow_fds_.size(); |
662 } | 668 } |
663 fds = &input_overflow_fds_[0]; | |
664 num_fds = input_overflow_fds_.size(); | |
665 } | 669 } |
666 } | 670 } |
667 if (header_fds > num_fds - fds_i) | 671 if (header_fds > num_fds - fds_i) |
668 #endif // IPC_USES_READWRITE | 672 #endif |
669 error = "Message needs unreceived descriptors"; | 673 error = "Message needs unreceived descriptors"; |
670 } | 674 } |
671 | 675 |
672 if (header_fds > | 676 if (header_fds > |
673 FileDescriptorSet::MAX_DESCRIPTORS_PER_MESSAGE) { | 677 FileDescriptorSet::MAX_DESCRIPTORS_PER_MESSAGE) { |
674 // There are too many descriptors in this message | 678 // There are too many descriptors in this message |
675 error = "Message requires an excessive number of descriptors"; | 679 error = "Message requires an excessive number of descriptors"; |
676 } | 680 } |
677 | 681 |
678 if (error) { | 682 if (error) { |
679 LOG(WARNING) << error | 683 LOG(WARNING) << error |
680 << " channel:" << this | 684 << " channel:" << this |
681 << " message-type:" << m.type() | 685 << " message-type:" << m.type() |
682 << " header()->num_fds:" << header_fds | 686 << " header()->num_fds:" << header_fds |
683 << " num_fds:" << num_fds | 687 << " num_fds:" << num_fds |
684 << " fds_i:" << fds_i; | 688 << " fds_i:" << fds_i; |
685 #if defined(CHROMIUM_SELINUX) | 689 #if defined(CHROMIUM_SELINUX) |
686 LOG(WARNING) << "In the case of SELinux this can be caused when " | 690 LOG(WARNING) << "In the case of SELinux this can be caused when " |
687 "using a --user-data-dir to which the default " | 691 "using a --user-data-dir to which the default " |
688 "policy doesn't give the renderer access to. "; | 692 "policy doesn't give the renderer access to. "; |
689 #endif // CHROMIUM_SELINUX | 693 #endif |
690 // close the existing file descriptors so that we don't leak them | 694 // close the existing file descriptors so that we don't leak them |
691 for (unsigned i = fds_i; i < num_fds; ++i) | 695 for (unsigned i = fds_i; i < num_fds; ++i) |
692 if (HANDLE_EINTR(close(fds[i])) < 0) | 696 if (HANDLE_EINTR(close(fds[i])) < 0) |
693 PLOG(ERROR) << "close " << i; | 697 PLOG(ERROR) << "close" << i; |
694 input_overflow_fds_.clear(); | 698 input_overflow_fds_.clear(); |
695 // abort the connection | 699 // abort the connection |
696 return false; | 700 return false; |
697 } | 701 } |
698 | 702 |
699 m.file_descriptor_set()->SetDescriptors( | 703 m.file_descriptor_set()->SetDescriptors( |
700 &fds[fds_i], header_fds); | 704 &fds[fds_i], header_fds); |
701 fds_i += header_fds; | 705 fds_i += header_fds; |
702 } | 706 } |
703 DVLOG(2) << "received message on channel @" << this | 707 DVLOG(2) << "received message on channel @" << this |
704 << " with type " << m.type() << " on fd " << pipe_; | 708 << " with type " << m.type() << " on fd " << pipe_; |
705 if (IsHelloMessage(&m)) { | 709 if (m.routing_id() == MSG_ROUTING_NONE && |
| 710 m.type() == HELLO_MESSAGE_TYPE) { |
706 // The Hello message contains only the process id. | 711 // The Hello message contains only the process id. |
707 void *iter = NULL; | 712 void *iter = NULL; |
708 int pid; | 713 int pid; |
709 if (!m.ReadInt(&iter, &pid)) { | 714 if (!m.ReadInt(&iter, &pid)) { |
710 NOTREACHED(); | 715 NOTREACHED(); |
711 } | 716 } |
712 #if defined(IPC_USES_READWRITE) | 717 #if defined(IPC_USES_READWRITE) |
713 if (mode_ == MODE_SERVER) { | 718 if (mode_ == MODE_SERVER && !uses_fifo_) { |
714 // With IPC_USES_READWRITE, the Hello message from the client to the | 719 // On non-Mac, the Hello message from the client to the server |
715 // server also contains the fd_pipe_, which will be used for all | 720 // also contains the fd_pipe_, which will be used for all |
716 // subsequent file descriptor passing. | 721 // subsequent file descriptor passing. |
717 DCHECK_EQ(m.file_descriptor_set()->size(), 1U); | 722 DCHECK_EQ(m.file_descriptor_set()->size(), 1U); |
718 base::FileDescriptor descriptor; | 723 base::FileDescriptor descriptor; |
719 if (!m.ReadFileDescriptor(&iter, &descriptor)) { | 724 if (!m.ReadFileDescriptor(&iter, &descriptor)) { |
720 NOTREACHED(); | 725 NOTREACHED(); |
721 } | 726 } |
722 fd_pipe_ = descriptor.fd; | 727 fd_pipe_ = descriptor.fd; |
723 CHECK(descriptor.auto_close); | 728 CHECK(descriptor.auto_close); |
724 } | 729 } |
725 #endif // IPC_USES_READWRITE | 730 #endif |
726 listener_->OnChannelConnected(pid); | 731 listener_->OnChannelConnected(pid); |
727 } else { | 732 } else { |
728 listener_->OnMessageReceived(m); | 733 listener_->OnMessageReceived(m); |
729 } | 734 } |
730 p = message_tail; | 735 p = message_tail; |
731 } else { | 736 } else { |
732 // Last message is partial. | 737 // Last message is partial. |
733 break; | 738 break; |
734 } | 739 } |
735 input_overflow_fds_ = std::vector<int>(&fds[fds_i], &fds[num_fds]); | 740 input_overflow_fds_ = std::vector<int>(&fds[fds_i], &fds[num_fds]); |
736 fds_i = 0; | 741 fds_i = 0; |
737 fds = &input_overflow_fds_[0]; | 742 fds = &input_overflow_fds_[0]; |
738 num_fds = input_overflow_fds_.size(); | 743 num_fds = input_overflow_fds_.size(); |
739 } | 744 } |
740 input_overflow_buf_.assign(p, end - p); | 745 input_overflow_buf_.assign(p, end - p); |
741 input_overflow_fds_ = std::vector<int>(&fds[fds_i], &fds[num_fds]); | 746 input_overflow_fds_ = std::vector<int>(&fds[fds_i], &fds[num_fds]); |
742 | 747 |
743 // When the input data buffer is empty, the overflow fds should be too. If | 748 // When the input data buffer is empty, the overflow fds should be too. If |
744 // this is not the case, we probably have a rogue renderer which is trying | 749 // this is not the case, we probably have a rogue renderer which is trying |
745 // to fill our descriptor table. | 750 // to fill our descriptor table. |
746 if (input_overflow_buf_.empty() && !input_overflow_fds_.empty()) { | 751 if (input_overflow_buf_.empty() && !input_overflow_fds_.empty()) { |
747 // We close these descriptors in Close() | 752 // We close these descriptors in Close() |
748 return false; | 753 return false; |
749 } | 754 } |
750 | 755 |
751 bytes_read = 0; // Get more data. | 756 bytes_read = 0; // Get more data. |
752 } | 757 } |
| 758 |
| 759 return true; |
753 } | 760 } |
754 | 761 |
755 bool Channel::ChannelImpl::ProcessOutgoingMessages() { | 762 bool Channel::ChannelImpl::ProcessOutgoingMessages() { |
756 DCHECK(!waiting_connect_); // Why are we trying to send messages if there's | 763 DCHECK(!waiting_connect_); // Why are we trying to send messages if there's |
757 // no connection? | 764 // no connection? |
758 if (output_queue_.empty()) | 765 is_blocked_on_write_ = false; |
| 766 |
| 767 if (output_queue_.empty()) { |
759 return true; | 768 return true; |
| 769 } |
760 | 770 |
761 if (pipe_ == -1) | 771 if (pipe_ == -1) { |
762 return false; | 772 return false; |
| 773 } |
763 | 774 |
764 // Write out all the messages we can till the write blocks or there are no | 775 // Write out all the messages we can till the write blocks or there are no |
765 // more outgoing messages. | 776 // more outgoing messages. |
766 while (!output_queue_.empty()) { | 777 while (!output_queue_.empty()) { |
767 Message* msg = output_queue_.front(); | 778 Message* msg = output_queue_.front(); |
768 | 779 |
| 780 #if defined(IPC_USES_READWRITE) |
| 781 scoped_ptr<Message> hello; |
| 782 if (remote_fd_pipe_ != -1 && |
| 783 msg->routing_id() == MSG_ROUTING_NONE && |
| 784 msg->type() == HELLO_MESSAGE_TYPE) { |
| 785 hello.reset(new Message(MSG_ROUTING_NONE, |
| 786 HELLO_MESSAGE_TYPE, |
| 787 IPC::Message::PRIORITY_NORMAL)); |
| 788 void* iter = NULL; |
| 789 int pid; |
| 790 if (!msg->ReadInt(&iter, &pid) || |
| 791 !hello->WriteInt(pid)) { |
| 792 NOTREACHED(); |
| 793 } |
| 794 DCHECK_EQ(hello->size(), msg->size()); |
| 795 if (!hello->WriteFileDescriptor(base::FileDescriptor(remote_fd_pipe_, |
| 796 false))) { |
| 797 NOTREACHED(); |
| 798 } |
| 799 msg = hello.get(); |
| 800 DCHECK_EQ(msg->file_descriptor_set()->size(), 1U); |
| 801 } |
| 802 #endif |
| 803 |
769 size_t amt_to_write = msg->size() - message_send_bytes_written_; | 804 size_t amt_to_write = msg->size() - message_send_bytes_written_; |
770 DCHECK(amt_to_write != 0); | 805 DCHECK(amt_to_write != 0); |
771 const char* out_bytes = reinterpret_cast<const char*>(msg->data()) + | 806 const char* out_bytes = reinterpret_cast<const char*>(msg->data()) + |
772 message_send_bytes_written_; | 807 message_send_bytes_written_; |
773 | 808 |
774 struct msghdr msgh = {0}; | 809 struct msghdr msgh = {0}; |
775 struct iovec iov = {const_cast<char*>(out_bytes), amt_to_write}; | 810 struct iovec iov = {const_cast<char*>(out_bytes), amt_to_write}; |
776 msgh.msg_iov = &iov; | 811 msgh.msg_iov = &iov; |
777 msgh.msg_iovlen = 1; | 812 msgh.msg_iovlen = 1; |
778 char buf[CMSG_SPACE( | 813 char buf[CMSG_SPACE( |
(...skipping 27 matching lines...) Expand all Loading... |
806 cmsg->cmsg_len = CMSG_LEN(sizeof(int) * num_fds); | 841 cmsg->cmsg_len = CMSG_LEN(sizeof(int) * num_fds); |
807 msg->file_descriptor_set()->GetDescriptors( | 842 msg->file_descriptor_set()->GetDescriptors( |
808 reinterpret_cast<int*>(CMSG_DATA(cmsg))); | 843 reinterpret_cast<int*>(CMSG_DATA(cmsg))); |
809 msgh.msg_controllen = cmsg->cmsg_len; | 844 msgh.msg_controllen = cmsg->cmsg_len; |
810 | 845 |
811 // DCHECK_LE above already checks that | 846 // DCHECK_LE above already checks that |
812 // num_fds < MAX_DESCRIPTORS_PER_MESSAGE so no danger of overflow. | 847 // num_fds < MAX_DESCRIPTORS_PER_MESSAGE so no danger of overflow. |
813 msg->header()->num_fds = static_cast<uint16>(num_fds); | 848 msg->header()->num_fds = static_cast<uint16>(num_fds); |
814 | 849 |
815 #if defined(IPC_USES_READWRITE) | 850 #if defined(IPC_USES_READWRITE) |
816 if (!IsHelloMessage(msg)) { | 851 if (!uses_fifo_ && |
| 852 (msg->routing_id() != MSG_ROUTING_NONE || |
| 853 msg->type() != HELLO_MESSAGE_TYPE)) { |
817 // Only the Hello message sends the file descriptor with the message. | 854 // Only the Hello message sends the file descriptor with the message. |
818 // Subsequently, we can send file descriptors on the dedicated | 855 // Subsequently, we can send file descriptors on the dedicated |
819 // fd_pipe_ which makes Seccomp sandbox operation more efficient. | 856 // fd_pipe_ which makes Seccomp sandbox operation more efficient. |
820 struct iovec fd_pipe_iov = { const_cast<char *>(""), 1 }; | 857 struct iovec fd_pipe_iov = { const_cast<char *>(""), 1 }; |
821 msgh.msg_iov = &fd_pipe_iov; | 858 msgh.msg_iov = &fd_pipe_iov; |
822 fd_written = fd_pipe_; | 859 fd_written = fd_pipe_; |
823 bytes_written = HANDLE_EINTR(sendmsg(fd_pipe_, &msgh, MSG_DONTWAIT)); | 860 bytes_written = HANDLE_EINTR(sendmsg(fd_pipe_, &msgh, MSG_DONTWAIT)); |
824 msgh.msg_iov = &iov; | 861 msgh.msg_iov = &iov; |
825 msgh.msg_controllen = 0; | 862 msgh.msg_controllen = 0; |
826 if (bytes_written > 0) { | 863 if (bytes_written > 0) { |
827 msg->file_descriptor_set()->CommitAll(); | 864 msg->file_descriptor_set()->CommitAll(); |
828 } | 865 } |
829 } | 866 } |
830 #endif // IPC_USES_READWRITE | 867 #endif |
831 } | 868 } |
832 | 869 |
833 if (bytes_written == 1) { | 870 if (bytes_written == 1) { |
834 fd_written = pipe_; | 871 fd_written = pipe_; |
835 #if defined(IPC_USES_READWRITE) | 872 #if defined(IPC_USES_READWRITE) |
836 if (mode_ != MODE_SERVER && IsHelloMessage(msg)) { | 873 if (mode_ != MODE_SERVER && !uses_fifo_ && |
| 874 msg->routing_id() == MSG_ROUTING_NONE && |
| 875 msg->type() == HELLO_MESSAGE_TYPE) { |
837 DCHECK_EQ(msg->file_descriptor_set()->size(), 1U); | 876 DCHECK_EQ(msg->file_descriptor_set()->size(), 1U); |
838 } | 877 } |
839 if (!msgh.msg_controllen) { | 878 if (!uses_fifo_ && !msgh.msg_controllen) { |
840 bytes_written = HANDLE_EINTR(write(pipe_, out_bytes, amt_to_write)); | 879 bytes_written = HANDLE_EINTR(write(pipe_, out_bytes, amt_to_write)); |
841 } else | 880 } else |
842 #endif // IPC_USES_READWRITE | 881 #endif |
843 { | 882 { |
844 bytes_written = HANDLE_EINTR(sendmsg(pipe_, &msgh, MSG_DONTWAIT)); | 883 bytes_written = HANDLE_EINTR(sendmsg(pipe_, &msgh, MSG_DONTWAIT)); |
845 } | 884 } |
846 } | 885 } |
847 if (bytes_written > 0) | 886 if (bytes_written > 0) |
848 msg->file_descriptor_set()->CommitAll(); | 887 msg->file_descriptor_set()->CommitAll(); |
849 | 888 |
850 if (bytes_written < 0 && !SocketWriteErrorIsRecoverable()) { | 889 if (bytes_written < 0 && !SocketWriteErrorIsRecoverable()) { |
851 #if defined(OS_MACOSX) | 890 #if defined(OS_MACOSX) |
852 // On OSX writing to a pipe with no listener returns EPERM. | 891 // On OSX writing to a pipe with no listener returns EPERM. |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
894 return true; | 933 return true; |
895 } | 934 } |
896 | 935 |
897 bool Channel::ChannelImpl::Send(Message* message) { | 936 bool Channel::ChannelImpl::Send(Message* message) { |
898 DVLOG(2) << "sending message @" << message << " on channel @" << this | 937 DVLOG(2) << "sending message @" << message << " on channel @" << this |
899 << " with type " << message->type() | 938 << " with type " << message->type() |
900 << " (" << output_queue_.size() << " in queue)"; | 939 << " (" << output_queue_.size() << " in queue)"; |
901 | 940 |
902 #ifdef IPC_MESSAGE_LOG_ENABLED | 941 #ifdef IPC_MESSAGE_LOG_ENABLED |
903 Logging::GetInstance()->OnSendMessage(message, ""); | 942 Logging::GetInstance()->OnSendMessage(message, ""); |
904 #endif // IPC_MESSAGE_LOG_ENABLED | 943 #endif |
905 | 944 |
906 output_queue_.push(message); | 945 output_queue_.push(message); |
907 if (!is_blocked_on_write_ && !waiting_connect_) { | 946 if (!waiting_connect_) { |
908 return ProcessOutgoingMessages(); | 947 if (!is_blocked_on_write_) { |
| 948 if (!ProcessOutgoingMessages()) |
| 949 return false; |
| 950 } |
909 } | 951 } |
910 | 952 |
911 return true; | 953 return true; |
912 } | 954 } |
913 | 955 |
914 int Channel::ChannelImpl::GetClientFileDescriptor() const { | 956 int Channel::ChannelImpl::GetClientFileDescriptor() const { |
915 return client_pipe_; | 957 return client_pipe_; |
916 } | 958 } |
917 | 959 |
918 bool Channel::ChannelImpl::AcceptsConnections() const { | 960 // Called by libevent when we can read from th pipe without blocking. |
919 return server_listen_pipe_ != -1; | 961 void Channel::ChannelImpl::OnFileCanReadWithoutBlocking(int fd) { |
920 } | 962 bool send_server_hello_msg = false; |
| 963 if (waiting_connect_ && mode_ == MODE_SERVER) { |
| 964 if (uses_fifo_) { |
| 965 if (!ServerAcceptFifoConnection(server_listen_pipe_, &pipe_)) { |
| 966 Close(); |
| 967 } |
921 | 968 |
922 bool Channel::ChannelImpl::HasAcceptedConnection() const { | 969 // No need to watch the listening socket any longer since only one client |
923 return AcceptsConnections() && pipe_ != -1; | 970 // can connect. So unregister with libevent. |
924 } | 971 server_listen_connection_watcher_.StopWatchingFileDescriptor(); |
925 | 972 |
926 void Channel::ChannelImpl::ResetToAcceptingConnectionState() { | 973 // Start watching our end of the socket. |
927 if (pipe_ == -1) | 974 MessageLoopForIO::current()->WatchFileDescriptor( |
928 return; | 975 pipe_, |
| 976 true, |
| 977 MessageLoopForIO::WATCH_READ, |
| 978 &read_watcher_, |
| 979 this); |
929 | 980 |
930 // Unregister libevent for the unix domain socket and close it. | 981 waiting_connect_ = false; |
931 read_watcher_.StopWatchingFileDescriptor(); | 982 } else { |
932 write_watcher_.StopWatchingFileDescriptor(); | 983 // In the case of a socketpair() the server starts listening on its end |
933 if (HANDLE_EINTR(close(pipe_)) < 0) | 984 // of the pipe in Connect(). |
934 PLOG(ERROR) << "close pipe_ " << pipe_name_; | 985 waiting_connect_ = false; |
935 pipe_ = -1; | 986 } |
936 #if defined(IPC_USES_READWRITE) | 987 send_server_hello_msg = true; |
937 if (fd_pipe_ != -1) { | |
938 if (HANDLE_EINTR(close(fd_pipe_)) < 0) | |
939 PLOG(ERROR) << "close fd_pipe_ " << pipe_name_; | |
940 fd_pipe_ = -1; | |
941 } | |
942 if (remote_fd_pipe_ != -1) { | |
943 if (HANDLE_EINTR(close(remote_fd_pipe_)) < 0) | |
944 PLOG(ERROR) << "close remote_fd_pipe_ " << pipe_name_; | |
945 remote_fd_pipe_ = -1; | |
946 } | |
947 #endif // IPC_USES_READWRITE | |
948 | |
949 while (!output_queue_.empty()) { | |
950 Message* m = output_queue_.front(); | |
951 output_queue_.pop(); | |
952 delete m; | |
953 } | 988 } |
954 | 989 |
955 // Close any outstanding, received file descriptors. | 990 if (!waiting_connect_ && fd == pipe_) { |
956 for (std::vector<int>::iterator | 991 if (!ProcessIncomingMessages()) { |
957 i = input_overflow_fds_.begin(); i != input_overflow_fds_.end(); ++i) { | |
958 if (HANDLE_EINTR(close(*i)) < 0) | |
959 PLOG(ERROR) << "close"; | |
960 } | |
961 input_overflow_fds_.clear(); | |
962 } | |
963 | |
964 // Called by libevent when we can read from the pipe without blocking. | |
965 void Channel::ChannelImpl::OnFileCanReadWithoutBlocking(int fd) { | |
966 bool send_server_hello_msg = false; | |
967 if (fd == server_listen_pipe_) { | |
968 int new_pipe = 0; | |
969 if (!ServerAcceptConnection(server_listen_pipe_, &new_pipe)) { | |
970 Close(); | 992 Close(); |
971 listener_->OnChannelListenError(); | 993 listener_->OnChannelError(); |
972 } | 994 // The OnChannelError() call may delete this, so we need to exit now. |
973 | |
974 if (pipe_ != -1) { | |
975 // We already have a connection. We only handle one at a time. | |
976 // close our new descriptor. | |
977 if (HANDLE_EINTR(shutdown(new_pipe, SHUT_RDWR)) < 0) | |
978 PLOG(ERROR) << "shutdown " << pipe_name_; | |
979 if (HANDLE_EINTR(close(new_pipe)) < 0) | |
980 PLOG(ERROR) << "close " << pipe_name_; | |
981 listener_->OnChannelDenied(); | |
982 return; | 995 return; |
983 } | 996 } |
984 pipe_ = new_pipe; | |
985 | |
986 if (!AcceptConnection()) { | |
987 NOTREACHED() << "AcceptConnection should not fail on server"; | |
988 } | |
989 send_server_hello_msg = true; | |
990 waiting_connect_ = false; | |
991 } else if (fd == pipe_) { | |
992 if (waiting_connect_ && mode_ == MODE_SERVER) { | |
993 send_server_hello_msg = true; | |
994 waiting_connect_ = false; | |
995 } | |
996 if (!ProcessIncomingMessages()) { | |
997 ClosePipeOnError(); | |
998 } | |
999 } else { | |
1000 NOTREACHED() << "Unknown pipe " << fd; | |
1001 } | 997 } |
1002 | 998 |
1003 // If we're a server and handshaking, then we want to make sure that we | 999 // If we're a server and handshaking, then we want to make sure that we |
1004 // only send our handshake message after we've processed the client's. | 1000 // only send our handshake message after we've processed the client's. |
1005 // This gives us a chance to kill the client if the incoming handshake | 1001 // This gives us a chance to kill the client if the incoming handshake |
1006 // is invalid. | 1002 // is invalid. |
1007 if (send_server_hello_msg) { | 1003 if (send_server_hello_msg) { |
1008 ProcessOutgoingMessages(); | 1004 ProcessOutgoingMessages(); |
1009 } | 1005 } |
1010 } | 1006 } |
1011 | 1007 |
1012 // Called by libevent when we can write to the pipe without blocking. | 1008 // Called by libevent when we can write to the pipe without blocking. |
1013 void Channel::ChannelImpl::OnFileCanWriteWithoutBlocking(int fd) { | 1009 void Channel::ChannelImpl::OnFileCanWriteWithoutBlocking(int fd) { |
1014 DCHECK(fd == pipe_); | |
1015 is_blocked_on_write_ = false; | |
1016 if (!ProcessOutgoingMessages()) { | 1010 if (!ProcessOutgoingMessages()) { |
1017 ClosePipeOnError(); | 1011 Close(); |
| 1012 listener_->OnChannelError(); |
1018 } | 1013 } |
1019 } | 1014 } |
1020 | 1015 |
1021 bool Channel::ChannelImpl::AcceptConnection() { | |
1022 MessageLoopForIO::current()->WatchFileDescriptor(pipe_, | |
1023 true, | |
1024 MessageLoopForIO::WATCH_READ, | |
1025 &read_watcher_, | |
1026 this); | |
1027 QueueHelloMessage(); | |
1028 | |
1029 if (mode_ == MODE_CLIENT) { | |
1030 // If we are a client we want to send a hello message out immediately. | |
1031 // In server mode we will send a hello message when we receive one from a | |
1032 // client. | |
1033 waiting_connect_ = false; | |
1034 return ProcessOutgoingMessages(); | |
1035 } else { | |
1036 waiting_connect_ = true; | |
1037 return true; | |
1038 } | |
1039 } | |
1040 | |
1041 void Channel::ChannelImpl::ClosePipeOnError() { | |
1042 if (HasAcceptedConnection()) { | |
1043 ResetToAcceptingConnectionState(); | |
1044 listener_->OnChannelError(); | |
1045 } else { | |
1046 Close(); | |
1047 if (AcceptsConnections()) { | |
1048 listener_->OnChannelListenError(); | |
1049 } else { | |
1050 listener_->OnChannelError(); | |
1051 } | |
1052 } | |
1053 } | |
1054 | |
1055 void Channel::ChannelImpl::QueueHelloMessage() { | |
1056 // Create the Hello message | |
1057 scoped_ptr<Message> msg(new Message(MSG_ROUTING_NONE, | |
1058 HELLO_MESSAGE_TYPE, | |
1059 IPC::Message::PRIORITY_NORMAL)); | |
1060 | |
1061 if (!msg->WriteInt(base::GetCurrentProcId())) { | |
1062 NOTREACHED() << "Unable to pickle hello message proc id"; | |
1063 } | |
1064 #if defined(IPC_USES_READWRITE) | |
1065 scoped_ptr<Message> hello; | |
1066 if (remote_fd_pipe_ != -1) { | |
1067 if (!msg->WriteFileDescriptor(base::FileDescriptor(remote_fd_pipe_, | |
1068 false))) { | |
1069 NOTREACHED() << "Unable to pickle hello message file descriptors"; | |
1070 } | |
1071 DCHECK_EQ(msg->file_descriptor_set()->size(), 1U); | |
1072 } | |
1073 #endif // IPC_USES_READWRITE | |
1074 output_queue_.push(msg.release()); | |
1075 } | |
1076 | |
1077 bool Channel::ChannelImpl::IsHelloMessage(const Message* m) const { | |
1078 return m->routing_id() == MSG_ROUTING_NONE && m->type() == HELLO_MESSAGE_TYPE; | |
1079 } | |
1080 | |
1081 void Channel::ChannelImpl::Close() { | 1016 void Channel::ChannelImpl::Close() { |
1082 // Close can be called multiple time, so we need to make sure we're | 1017 // Close can be called multiple time, so we need to make sure we're |
1083 // idempotent. | 1018 // idempotent. |
1084 | 1019 |
1085 ResetToAcceptingConnectionState(); | 1020 // Unregister libevent for the listening socket and close it. |
| 1021 server_listen_connection_watcher_.StopWatchingFileDescriptor(); |
1086 | 1022 |
1087 if (must_unlink_) { | |
1088 unlink(pipe_name_.c_str()); | |
1089 must_unlink_ = false; | |
1090 } | |
1091 if (server_listen_pipe_ != -1) { | 1023 if (server_listen_pipe_ != -1) { |
1092 if (HANDLE_EINTR(close(server_listen_pipe_)) < 0) | 1024 if (HANDLE_EINTR(close(server_listen_pipe_)) < 0) |
1093 PLOG(ERROR) << "close " << server_listen_pipe_; | 1025 PLOG(ERROR) << "close " << server_listen_pipe_; |
1094 server_listen_pipe_ = -1; | 1026 server_listen_pipe_ = -1; |
1095 // Unregister libevent for the listening socket and close it. | |
1096 server_listen_connection_watcher_.StopWatchingFileDescriptor(); | |
1097 } | 1027 } |
1098 | 1028 |
| 1029 // Unregister libevent for the FIFO and close it. |
| 1030 read_watcher_.StopWatchingFileDescriptor(); |
| 1031 write_watcher_.StopWatchingFileDescriptor(); |
| 1032 if (pipe_ != -1) { |
| 1033 if (HANDLE_EINTR(close(pipe_)) < 0) |
| 1034 PLOG(ERROR) << "close " << pipe_; |
| 1035 pipe_ = -1; |
| 1036 } |
1099 if (client_pipe_ != -1) { | 1037 if (client_pipe_ != -1) { |
1100 PipeMap::GetInstance()->RemoveAndClose(pipe_name_); | 1038 PipeMap::GetInstance()->RemoveAndClose(pipe_name_); |
1101 client_pipe_ = -1; | 1039 client_pipe_ = -1; |
1102 } | 1040 } |
| 1041 #if defined(IPC_USES_READWRITE) |
| 1042 if (fd_pipe_ != -1) { |
| 1043 if (HANDLE_EINTR(close(fd_pipe_)) < 0) |
| 1044 PLOG(ERROR) << "close " << fd_pipe_; |
| 1045 fd_pipe_ = -1; |
| 1046 } |
| 1047 if (remote_fd_pipe_ != -1) { |
| 1048 if (HANDLE_EINTR(close(remote_fd_pipe_)) < 0) |
| 1049 PLOG(ERROR) << "close " << remote_fd_pipe_; |
| 1050 remote_fd_pipe_ = -1; |
| 1051 } |
| 1052 #endif |
| 1053 |
| 1054 if (uses_fifo_) { |
| 1055 // Unlink the FIFO |
| 1056 unlink(pipe_name_.c_str()); |
| 1057 } |
| 1058 |
| 1059 while (!output_queue_.empty()) { |
| 1060 Message* m = output_queue_.front(); |
| 1061 output_queue_.pop(); |
| 1062 delete m; |
| 1063 } |
| 1064 |
| 1065 // Close any outstanding, received file descriptors |
| 1066 for (std::vector<int>::iterator |
| 1067 i = input_overflow_fds_.begin(); i != input_overflow_fds_.end(); ++i) { |
| 1068 if (HANDLE_EINTR(close(*i)) < 0) |
| 1069 PLOG(ERROR) << "close " << *i; |
| 1070 } |
| 1071 input_overflow_fds_.clear(); |
1103 } | 1072 } |
1104 | 1073 |
1105 //------------------------------------------------------------------------------ | 1074 //------------------------------------------------------------------------------ |
1106 // Channel's methods simply call through to ChannelImpl. | 1075 // Channel's methods simply call through to ChannelImpl. |
1107 Channel::Channel(const IPC::ChannelHandle& channel_handle, Mode mode, | 1076 Channel::Channel(const IPC::ChannelHandle& channel_handle, Mode mode, |
1108 Listener* listener) | 1077 Listener* listener) |
1109 : channel_impl_(new ChannelImpl(channel_handle, mode, listener)) { | 1078 : channel_impl_(new ChannelImpl(channel_handle, mode, listener)) { |
1110 } | 1079 } |
1111 | 1080 |
1112 Channel::~Channel() { | 1081 Channel::~Channel() { |
(...skipping 13 matching lines...) Expand all Loading... |
1126 } | 1095 } |
1127 | 1096 |
1128 bool Channel::Send(Message* message) { | 1097 bool Channel::Send(Message* message) { |
1129 return channel_impl_->Send(message); | 1098 return channel_impl_->Send(message); |
1130 } | 1099 } |
1131 | 1100 |
1132 int Channel::GetClientFileDescriptor() const { | 1101 int Channel::GetClientFileDescriptor() const { |
1133 return channel_impl_->GetClientFileDescriptor(); | 1102 return channel_impl_->GetClientFileDescriptor(); |
1134 } | 1103 } |
1135 | 1104 |
1136 bool Channel::AcceptsConnections() const { | |
1137 return channel_impl_->AcceptsConnections(); | |
1138 } | |
1139 | |
1140 bool Channel::HasAcceptedConnection() const { | |
1141 return channel_impl_->HasAcceptedConnection(); | |
1142 } | |
1143 | |
1144 void Channel::ResetToAcceptingConnectionState() { | |
1145 channel_impl_->ResetToAcceptingConnectionState(); | |
1146 } | |
1147 | |
1148 } // namespace IPC | 1105 } // namespace IPC |
OLD | NEW |