wez@chromium.org | 7cce091 | 2011-04-06 21:01:44 +0900 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #ifndef IPC_IPC_CHANNEL_POSIX_H_ |
| 6 | #define IPC_IPC_CHANNEL_POSIX_H_ |
thakis@chromium.org | 01d1452 | 2010-07-27 08:08:24 +0900 | [diff] [blame] | 7 | #pragma once |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 8 | |
| 9 | #include "ipc/ipc_channel.h" |
| 10 | |
| 11 | #include <sys/socket.h> // for CMSG macros |
| 12 | |
| 13 | #include <queue> |
| 14 | #include <string> |
| 15 | #include <vector> |
| 16 | |
| 17 | #include "base/message_loop.h" |
| 18 | #include "ipc/file_descriptor_set_posix.h" |
| 19 | |
dmaclach@chromium.org | 2680d3a | 2010-12-09 06:22:24 +0900 | [diff] [blame] | 20 | #if !defined(OS_MACOSX) |
| 21 | // On Linux, the seccomp sandbox makes it very expensive to call |
| 22 | // recvmsg() and sendmsg(). The restriction on calling read() and write(), which |
| 23 | // are cheap, is that we can't pass file descriptors over them. |
| 24 | // |
| 25 | // As we cannot anticipate when the sender will provide us with file |
| 26 | // descriptors, we have to make the decision about whether we call read() or |
| 27 | // recvmsg() before we actually make the call. The easiest option is to |
brettw@chromium.org | 0d7e019 | 2011-05-14 04:22:14 +0900 | [diff] [blame] | 28 | // create a dedicated socketpair() for exchanging file descriptors. Any file |
| 29 | // descriptors are split out of a message, with the non-file-descriptor payload |
| 30 | // going over the normal connection, and the file descriptors being sent |
| 31 | // separately over the other channel. When read()ing from a channel, we'll |
| 32 | // notice if the message was supposed to have come with file descriptors and |
| 33 | // use recvmsg on the other socketpair to retrieve them and combine them |
| 34 | // back with the rest of the message. |
| 35 | // |
dmaclach@chromium.org | 2680d3a | 2010-12-09 06:22:24 +0900 | [diff] [blame] | 36 | // Mac can also run in IPC_USES_READWRITE mode if necessary, but at this time |
| 37 | // doesn't take a performance hit from recvmsg and sendmsg, so it doesn't |
| 38 | // make sense to waste resources on having the separate dedicated socketpair. |
| 39 | // It is however useful for debugging between Linux and Mac to be able to turn |
| 40 | // this switch 'on' on the Mac as well. |
brettw@chromium.org | 0d7e019 | 2011-05-14 04:22:14 +0900 | [diff] [blame] | 41 | // |
dmaclach@chromium.org | 2680d3a | 2010-12-09 06:22:24 +0900 | [diff] [blame] | 42 | // The HELLO message from the client to the server is always sent using |
| 43 | // sendmsg because it will contain the file descriptor that the server |
| 44 | // needs to send file descriptors in later messages. |
| 45 | #define IPC_USES_READWRITE 1 |
| 46 | #endif |
| 47 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 48 | namespace IPC { |
| 49 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 50 | class Channel::ChannelImpl : public MessageLoopForIO::Watcher { |
| 51 | public: |
| 52 | // Mirror methods of Channel, see ipc_channel.h for description. |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 53 | ChannelImpl(const IPC::ChannelHandle& channel_handle, Mode mode, |
dmaclach@chromium.org | 058c4a7 | 2010-12-09 04:28:09 +0900 | [diff] [blame] | 54 | Listener* listener); |
hans@chromium.org | 78b7593 | 2011-05-25 18:08:19 +0900 | [diff] [blame] | 55 | virtual ~ChannelImpl(); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 56 | bool Connect(); |
| 57 | void Close(); |
| 58 | void set_listener(Listener* listener) { listener_ = listener; } |
| 59 | bool Send(Message* message); |
phajdan.jr@chromium.org | af9455b | 2011-09-20 02:08:12 +0900 | [diff] [blame^] | 60 | int GetClientFileDescriptor(); |
| 61 | int TakeClientFileDescriptor(); |
| 62 | void CloseClientFileDescriptor(); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 63 | bool AcceptsConnections() const; |
| 64 | bool HasAcceptedConnection() const; |
wez@chromium.org | 7cce091 | 2011-04-06 21:01:44 +0900 | [diff] [blame] | 65 | bool GetClientEuid(uid_t* client_euid) const; |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 66 | void ResetToAcceptingConnectionState(); |
kkania@chromium.org | f37b4e5 | 2011-08-09 15:46:06 +0900 | [diff] [blame] | 67 | static bool IsNamedServerInitialized(const std::string& channel_id); |
jamescook@chromium.org | 2d471f0 | 2011-09-01 06:11:04 +0900 | [diff] [blame] | 68 | #if defined(OS_LINUX) |
| 69 | static void SetGlobalPid(int pid); |
| 70 | #endif // OS_LINUX |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 71 | |
| 72 | private: |
dmaclach@chromium.org | f146c29 | 2011-02-04 05:35:09 +0900 | [diff] [blame] | 73 | bool CreatePipe(const IPC::ChannelHandle& channel_handle); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 74 | |
| 75 | bool ProcessIncomingMessages(); |
| 76 | bool ProcessOutgoingMessages(); |
| 77 | |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 78 | bool AcceptConnection(); |
| 79 | void ClosePipeOnError(); |
jamescook@chromium.org | 2d471f0 | 2011-09-01 06:11:04 +0900 | [diff] [blame] | 80 | int GetHelloMessageProcId(); |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 81 | void QueueHelloMessage(); |
| 82 | bool IsHelloMessage(const Message* m) const; |
| 83 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 84 | // MessageLoopForIO::Watcher implementation. |
| 85 | virtual void OnFileCanReadWithoutBlocking(int fd); |
| 86 | virtual void OnFileCanWriteWithoutBlocking(int fd); |
| 87 | |
| 88 | Mode mode_; |
| 89 | |
| 90 | // After accepting one client connection on our server socket we want to |
| 91 | // stop listening. |
| 92 | MessageLoopForIO::FileDescriptorWatcher server_listen_connection_watcher_; |
| 93 | MessageLoopForIO::FileDescriptorWatcher read_watcher_; |
| 94 | MessageLoopForIO::FileDescriptorWatcher write_watcher_; |
| 95 | |
| 96 | // Indicates whether we're currently blocked waiting for a write to complete. |
| 97 | bool is_blocked_on_write_; |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 98 | bool waiting_connect_; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 99 | |
| 100 | // If sending a message blocks then we use this variable |
| 101 | // to keep track of where we are. |
| 102 | size_t message_send_bytes_written_; |
| 103 | |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 104 | // File descriptor we're listening on for new connections if we listen |
| 105 | // for connections. |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 106 | int server_listen_pipe_; |
| 107 | |
| 108 | // The pipe used for communication. |
| 109 | int pipe_; |
| 110 | |
| 111 | // For a server, the client end of our socketpair() -- the other end of our |
| 112 | // pipe_ that is passed to the client. |
| 113 | int client_pipe_; |
phajdan.jr@chromium.org | af9455b | 2011-09-20 02:08:12 +0900 | [diff] [blame^] | 114 | base::Lock client_pipe_lock_; // Lock that protects |client_pipe_|. |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 115 | |
dmaclach@chromium.org | 2680d3a | 2010-12-09 06:22:24 +0900 | [diff] [blame] | 116 | #if defined(IPC_USES_READWRITE) |
pvalchev@google.com | c45765a | 2010-05-20 03:17:53 +0900 | [diff] [blame] | 117 | // Linux/BSD use a dedicated socketpair() for passing file descriptors. |
agl@chromium.org | a81f84a | 2009-09-05 06:34:05 +0900 | [diff] [blame] | 118 | int fd_pipe_; |
| 119 | int remote_fd_pipe_; |
| 120 | #endif |
| 121 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 122 | // The "name" of our pipe. On Windows this is the global identifier for |
| 123 | // the pipe. On POSIX it's used as a key in a local map of file descriptors. |
| 124 | std::string pipe_name_; |
| 125 | |
| 126 | Listener* listener_; |
| 127 | |
| 128 | // Messages to be sent are queued here. |
| 129 | std::queue<Message*> output_queue_; |
| 130 | |
| 131 | // We read from the pipe into this buffer |
| 132 | char input_buf_[Channel::kReadBufferSize]; |
| 133 | |
pkasting@chromium.org | 9687a8f | 2011-09-01 09:50:13 +0900 | [diff] [blame] | 134 | // We assume a worst case: kReadBufferSize bytes of messages, where each |
| 135 | // message has no payload and a full complement of descriptors. |
| 136 | static const size_t kMaxReadFDs = |
| 137 | (Channel::kReadBufferSize / sizeof(IPC::Message::Header)) * |
| 138 | FileDescriptorSet::kMaxDescriptorsPerMessage; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 139 | |
| 140 | // This is a control message buffer large enough to hold kMaxReadFDs |
| 141 | #if defined(OS_MACOSX) |
| 142 | // TODO(agl): OSX appears to have non-constant CMSG macros! |
| 143 | char input_cmsg_buf_[1024]; |
| 144 | #else |
pkasting@chromium.org | 9687a8f | 2011-09-01 09:50:13 +0900 | [diff] [blame] | 145 | char input_cmsg_buf_[CMSG_SPACE(sizeof(int) * kMaxReadFDs)]; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 146 | #endif |
| 147 | |
| 148 | // Large messages that span multiple pipe buffers, get built-up using |
| 149 | // this buffer. |
| 150 | std::string input_overflow_buf_; |
| 151 | std::vector<int> input_overflow_fds_; |
| 152 | |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 153 | // True if we are responsible for unlinking the unix domain socket file. |
| 154 | bool must_unlink_; |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 155 | |
jamescook@chromium.org | 2d471f0 | 2011-09-01 06:11:04 +0900 | [diff] [blame] | 156 | #if defined(OS_LINUX) |
| 157 | // If non-zero, overrides the process ID sent in the hello message. |
| 158 | static int global_pid_; |
| 159 | #endif // OS_LINUX |
| 160 | |
dmaclach@chromium.org | 2812db6 | 2011-03-03 07:27:14 +0900 | [diff] [blame] | 161 | DISALLOW_IMPLICIT_CONSTRUCTORS(ChannelImpl); |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 162 | }; |
| 163 | |
dmaclach@chromium.org | c1d3d42 | 2010-12-20 15:59:23 +0900 | [diff] [blame] | 164 | // The maximum length of the name of a pipe for MODE_NAMED_SERVER or |
| 165 | // MODE_NAMED_CLIENT if you want to pass in your own socket. |
| 166 | // The standard size on linux is 108, mac is 104. To maintain consistency |
| 167 | // across platforms we standardize on the smaller value. |
| 168 | static const size_t kMaxPipeNameLength = 104; |
| 169 | |
agl@chromium.org | 1c6dcf2 | 2009-07-23 08:57:21 +0900 | [diff] [blame] | 170 | } // namespace IPC |
| 171 | |
| 172 | #endif // IPC_IPC_CHANNEL_POSIX_H_ |