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 #ifndef IPC_IPC_CHANNEL_POSIX_H_ | 5 #ifndef IPC_IPC_CHANNEL_POSIX_H_ |
6 #define IPC_IPC_CHANNEL_POSIX_H_ | 6 #define IPC_IPC_CHANNEL_POSIX_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "ipc/ipc_channel.h" | 9 #include "ipc/ipc_channel.h" |
10 | 10 |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 bool AcceptsConnections() const; | 63 bool AcceptsConnections() const; |
64 bool HasAcceptedConnection() const; | 64 bool HasAcceptedConnection() const; |
65 bool GetClientEuid(uid_t* client_euid) const; | 65 bool GetClientEuid(uid_t* client_euid) const; |
66 void ResetToAcceptingConnectionState(); | 66 void ResetToAcceptingConnectionState(); |
67 static bool IsNamedServerInitialized(const std::string& channel_id); | 67 static bool IsNamedServerInitialized(const std::string& channel_id); |
68 #if defined(OS_LINUX) | 68 #if defined(OS_LINUX) |
69 static void SetGlobalPid(int pid); | 69 static void SetGlobalPid(int pid); |
70 #endif // OS_LINUX | 70 #endif // OS_LINUX |
71 | 71 |
72 private: | 72 private: |
| 73 enum ReadState { READ_SUCCEEDED, READ_FAILED, READ_PENDING }; |
| 74 |
73 bool CreatePipe(const IPC::ChannelHandle& channel_handle); | 75 bool CreatePipe(const IPC::ChannelHandle& channel_handle); |
74 | 76 |
75 bool ProcessIncomingMessages(); | 77 bool ProcessIncomingMessages(); |
76 bool ProcessOutgoingMessages(); | 78 bool ProcessOutgoingMessages(); |
77 | 79 |
78 bool AcceptConnection(); | 80 bool AcceptConnection(); |
79 void ClosePipeOnError(); | 81 void ClosePipeOnError(); |
80 int GetHelloMessageProcId(); | 82 int GetHelloMessageProcId(); |
81 void QueueHelloMessage(); | 83 void QueueHelloMessage(); |
82 bool IsHelloMessage(const Message* m) const; | 84 bool IsHelloMessage(const Message* m) const; |
83 | 85 |
84 // Reads data from the "regular" (non FD) pipe into the input buffers. The | 86 // Populates the given buffer with data from the pipe. |
85 // two output params will identify the data received. | |
86 // | 87 // |
87 // On success, returns true. If there is no data waiting, the pointers will | 88 // Returns the state of the read. On READ_SUCCESS, the number of bytes |
88 // both be set to NULL. Otherwise, they'll indicate the data read. This will | 89 // read will be placed into |*bytes_read| (which can be less than the |
89 // be inside the input_buf_ for short messages, and for long messages will | 90 // buffer size). On READ_FAILED, the channel will be closed. |
90 // automatically spill into the input_overflow_buf_. When in non-READWRITE | |
91 // mode this will also load any handles from the message into input_fds_. | |
92 // | 91 // |
93 // On failure, returns false. This means there was some kind of pipe error | 92 // If the return value is READ_PENDING, it means that there was no data |
94 // and we should not continue. | 93 // ready for reading. The implementation is then responsible for either |
95 bool ReadDataFromPipe(const char** begin, const char** end); | 94 // calling AsyncReadComplete with the number of bytes read into the |
| 95 // buffer, or ProcessIncomingMessages to try the read again (depending |
| 96 // on whether the platform's async I/O is "try again" or "write |
| 97 // asynchronously into your buffer"). |
| 98 ReadState ReadData(char* buffer, int buffer_len, int* bytes_read); |
| 99 |
| 100 // Takes the given data received from the IPC channel and dispatches any |
| 101 // fully completed messages. |
| 102 // |
| 103 // Returns true on success. False means channel error. |
| 104 bool DispatchInputData(const char* input_data, int input_data_len); |
96 | 105 |
97 #if defined(IPC_USES_READWRITE) | 106 #if defined(IPC_USES_READWRITE) |
98 // Reads the next message from the fd_pipe_ and appends them to the | 107 // Reads the next message from the fd_pipe_ and appends them to the |
99 // input_fds_ queue. Returns false if there was a message receiving error. | 108 // input_fds_ queue. Returns false if there was a message receiving error. |
100 // True means there was a message and it was processed properly, or there was | 109 // True means there was a message and it was processed properly, or there was |
101 // no messages. | 110 // no messages. |
102 bool ReadFileDescriptorsFromFDPipe(); | 111 bool ReadFileDescriptorsFromFDPipe(); |
103 #endif | 112 #endif |
104 | 113 |
105 // Loads the required file desciptors into the given message. Returns true | 114 // Loads the required file desciptors into the given message. Returns true |
106 // on success. False means a fatal channel error. | 115 // on success. False means a fatal channel error. |
107 // | 116 // |
108 // This will read from the input_fds_ and read more handles from the FD | 117 // This will read from the input_fds_ and read more handles from the FD |
109 // pipe if necessary. | 118 // pipe if necessary. |
110 bool PopulateMessageFileDescriptors(Message* msg); | 119 bool WillDispatchInputMessage(Message* msg); |
| 120 |
| 121 // Performs post-dispatch checks. Called when all input buffers are empty, |
| 122 // though there could be more data ready to be read from the OS. |
| 123 bool DidEmptyInputBuffers(); |
111 | 124 |
112 // Finds the set of file descriptors in the given message. On success, | 125 // Finds the set of file descriptors in the given message. On success, |
113 // appends the descriptors to the input_fds_ member and returns true | 126 // appends the descriptors to the input_fds_ member and returns true |
114 // | 127 // |
115 // Returns false if the message was truncated. In this case, any handles that | 128 // Returns false if the message was truncated. In this case, any handles that |
116 // were sent will be closed. | 129 // were sent will be closed. |
117 bool ExtractFileDescriptorsFromMsghdr(msghdr* msg); | 130 bool ExtractFileDescriptorsFromMsghdr(msghdr* msg); |
118 | 131 |
119 // Closes all handles in the input_fds_ list and clears the list. This is | 132 // Closes all handles in the input_fds_ list and clears the list. This is |
120 // used to clean up handles in error conditions to avoid leaking the handles. | 133 // used to clean up handles in error conditions to avoid leaking the handles. |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
163 | 176 |
164 // The "name" of our pipe. On Windows this is the global identifier for | 177 // The "name" of our pipe. On Windows this is the global identifier for |
165 // the pipe. On POSIX it's used as a key in a local map of file descriptors. | 178 // the pipe. On POSIX it's used as a key in a local map of file descriptors. |
166 std::string pipe_name_; | 179 std::string pipe_name_; |
167 | 180 |
168 Listener* listener_; | 181 Listener* listener_; |
169 | 182 |
170 // Messages to be sent are queued here. | 183 // Messages to be sent are queued here. |
171 std::queue<Message*> output_queue_; | 184 std::queue<Message*> output_queue_; |
172 | 185 |
173 // We read from the pipe into this buffer. Managed by ReadDataFromPipe, do | 186 // We read from the pipe into this buffer. Managed by DispatchInputData, do |
174 // not access directly outside that function. | 187 // not access directly outside that function. |
175 char input_buf_[Channel::kReadBufferSize]; | 188 char input_buf_[Channel::kReadBufferSize]; |
176 | 189 |
177 // Large messages that span multiple pipe buffers, get built-up using | 190 // Large messages that span multiple pipe buffers, get built-up using |
178 // this buffer. | 191 // this buffer. |
179 std::string input_overflow_buf_; | 192 std::string input_overflow_buf_; |
180 | 193 |
181 // We assume a worst case: kReadBufferSize bytes of messages, where each | 194 // We assume a worst case: kReadBufferSize bytes of messages, where each |
182 // message has no payload and a full complement of descriptors. | 195 // message has no payload and a full complement of descriptors. |
183 static const size_t kMaxReadFDs = | 196 static const size_t kMaxReadFDs = |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 | 229 |
217 // The maximum length of the name of a pipe for MODE_NAMED_SERVER or | 230 // The maximum length of the name of a pipe for MODE_NAMED_SERVER or |
218 // MODE_NAMED_CLIENT if you want to pass in your own socket. | 231 // MODE_NAMED_CLIENT if you want to pass in your own socket. |
219 // The standard size on linux is 108, mac is 104. To maintain consistency | 232 // The standard size on linux is 108, mac is 104. To maintain consistency |
220 // across platforms we standardize on the smaller value. | 233 // across platforms we standardize on the smaller value. |
221 static const size_t kMaxPipeNameLength = 104; | 234 static const size_t kMaxPipeNameLength = 104; |
222 | 235 |
223 } // namespace IPC | 236 } // namespace IPC |
224 | 237 |
225 #endif // IPC_IPC_CHANNEL_POSIX_H_ | 238 #endif // IPC_IPC_CHANNEL_POSIX_H_ |
OLD | NEW |