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

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: Style fixes 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 //Convert relative path of named pipe to absolute path
sanjeevr 2011/06/20 22:27:26 Style nit: You need a space after the //. Same bel
148 //to allow IPC when processes have different working directories.
149 FilePath temp_dir;
150 PathService::Get(base::DIR_TEMP, &temp_dir);
151 std::string pipe_path= temp_dir.Append(pipe_name).value();
sanjeevr 2011/06/20 22:27:26 Style nit: You need a space before the =
145 DCHECK(server_listen_fd); 152 DCHECK(server_listen_fd);
146 DCHECK_GT(pipe_name.length(), 0u); 153 DCHECK_GT(pipe_path.length(), 0u);
147 DCHECK_LT(pipe_name.length(), kMaxPipeNameLength); 154 DCHECK_LT(pipe_path.length(), kMaxPipeNameLength);
148 155
149 if (pipe_name.length() == 0 || pipe_name.length() >= kMaxPipeNameLength) { 156 if (pipe_path.length() == 0 || pipe_path.length() >= kMaxPipeNameLength) {
150 return false; 157 return false;
151 } 158 }
152 159
153 // Create socket. 160 // Create socket.
154 int fd = socket(AF_UNIX, SOCK_STREAM, 0); 161 int fd = socket(AF_UNIX, SOCK_STREAM, 0);
155 if (fd < 0) { 162 if (fd < 0) {
156 return false; 163 return false;
157 } 164 }
158 165
159 // Make socket non-blocking 166 // Make socket non-blocking
160 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { 167 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
161 PLOG(ERROR) << "fcntl(O_NONBLOCK) " << pipe_name; 168 PLOG(ERROR) << "fcntl(O_NONBLOCK) " << pipe_path;
162 if (HANDLE_EINTR(close(fd)) < 0) 169 if (HANDLE_EINTR(close(fd)) < 0)
163 PLOG(ERROR) << "close " << pipe_name; 170 PLOG(ERROR) << "close " << pipe_path;
164 return false; 171 return false;
165 } 172 }
166 173
167 // Delete any old FS instances. 174 // Delete any old FS instances.
168 unlink(pipe_name.c_str()); 175 unlink(pipe_path.c_str());
169 176
170 // Make sure the path we need exists. 177 // Make sure the path we need exists.
171 FilePath path(pipe_name); 178 FilePath path(pipe_path);
172 FilePath dir_path = path.DirName(); 179 FilePath dir_path = path.DirName();
173 if (!file_util::CreateDirectory(dir_path)) { 180 if (!file_util::CreateDirectory(dir_path)) {
174 return false; 181 return false;
175 } 182 }
176 183
177 // Create unix_addr structure. 184 // Create unix_addr structure.
178 struct sockaddr_un unix_addr; 185 struct sockaddr_un unix_addr;
179 memset(&unix_addr, 0, sizeof(unix_addr)); 186 memset(&unix_addr, 0, sizeof(unix_addr));
180 unix_addr.sun_family = AF_UNIX; 187 unix_addr.sun_family = AF_UNIX;
181 int path_len = snprintf(unix_addr.sun_path, IPC::kMaxPipeNameLength, 188 int path_len = snprintf(unix_addr.sun_path, IPC::kMaxPipeNameLength,
182 "%s", pipe_name.c_str()); 189 "%s", pipe_path.c_str());
183 DCHECK_EQ(static_cast<int>(pipe_name.length()), path_len); 190 DCHECK_EQ(static_cast<int>(pipe_path.length()), path_len);
184 size_t unix_addr_len = offsetof(struct sockaddr_un, 191 size_t unix_addr_len = offsetof(struct sockaddr_un,
185 sun_path) + path_len + 1; 192 sun_path) + path_len + 1;
186 193
187 // Bind the socket. 194 // Bind the socket.
188 if (bind(fd, reinterpret_cast<const sockaddr*>(&unix_addr), 195 if (bind(fd, reinterpret_cast<const sockaddr*>(&unix_addr),
189 unix_addr_len) != 0) { 196 unix_addr_len) != 0) {
190 PLOG(ERROR) << "bind " << pipe_name; 197 PLOG(ERROR) << "bind " << pipe_path;
191 if (HANDLE_EINTR(close(fd)) < 0) 198 if (HANDLE_EINTR(close(fd)) < 0)
192 PLOG(ERROR) << "close " << pipe_name; 199 PLOG(ERROR) << "close " << pipe_path;
193 return false; 200 return false;
194 } 201 }
195 202
196 // Start listening on the socket. 203 // Start listening on the socket.
197 const int listen_queue_length = 1; 204 const int listen_queue_length = 1;
198 if (listen(fd, listen_queue_length) != 0) { 205 if (listen(fd, listen_queue_length) != 0) {
199 PLOG(ERROR) << "listen " << pipe_name; 206 PLOG(ERROR) << "listen " << pipe_path;
200 if (HANDLE_EINTR(close(fd)) < 0) 207 if (HANDLE_EINTR(close(fd)) < 0)
201 PLOG(ERROR) << "close " << pipe_name; 208 PLOG(ERROR) << "close " << pipe_path;
202 return false; 209 return false;
203 } 210 }
204 211
205 *server_listen_fd = fd; 212 *server_listen_fd = fd;
206 return true; 213 return true;
207 } 214 }
208 215
209 // Accept a connection on a socket we are listening to. 216 // Accept a connection on a socket we are listening to.
210 bool ServerAcceptConnection(int server_listen_fd, int* server_socket) { 217 bool ServerAcceptConnection(int server_listen_fd, int* server_socket) {
211 DCHECK(server_socket); 218 DCHECK(server_socket);
212 219
213 int accept_fd = HANDLE_EINTR(accept(server_listen_fd, NULL, 0)); 220 int accept_fd = HANDLE_EINTR(accept(server_listen_fd, NULL, 0));
214 if (accept_fd < 0) 221 if (accept_fd < 0)
215 return false; 222 return false;
216 if (fcntl(accept_fd, F_SETFL, O_NONBLOCK) == -1) { 223 if (fcntl(accept_fd, F_SETFL, O_NONBLOCK) == -1) {
217 PLOG(ERROR) << "fcntl(O_NONBLOCK) " << accept_fd; 224 PLOG(ERROR) << "fcntl(O_NONBLOCK) " << accept_fd;
218 if (HANDLE_EINTR(close(accept_fd)) < 0) 225 if (HANDLE_EINTR(close(accept_fd)) < 0)
219 PLOG(ERROR) << "close " << accept_fd; 226 PLOG(ERROR) << "close " << accept_fd;
220 return false; 227 return false;
221 } 228 }
222 229
223 *server_socket = accept_fd; 230 *server_socket = accept_fd;
224 return true; 231 return true;
225 } 232 }
226 233
227 bool CreateClientUnixDomainSocket(const std::string& pipe_name, 234 bool CreateClientUnixDomainSocket(const std::string& pipe_name,
228 int* client_socket) { 235 int* client_socket) {
236 //Convert relative path of named pipe to absolute path
dmac 2011/06/20 22:30:23 spaces after //
237 //to allow IPC when processes have different working directories.
238 FilePath temp_dir;
239 PathService::Get(base::DIR_TEMP, &temp_dir);
240 std::string pipe_path= temp_dir.Append(pipe_name).value();
dmac 2011/06/20 22:30:23 space before =
229 DCHECK(client_socket); 241 DCHECK(client_socket);
230 DCHECK_GT(pipe_name.length(), 0u); 242 DCHECK_GT(pipe_path.length(), 0u);
231 DCHECK_LT(pipe_name.length(), kMaxPipeNameLength); 243 DCHECK_LT(pipe_path.length(), kMaxPipeNameLength);
232 244
233 if (pipe_name.length() == 0 || pipe_name.length() >= kMaxPipeNameLength) { 245 if (pipe_path.length() == 0 || pipe_path.length() >= kMaxPipeNameLength) {
234 return false; 246 return false;
235 } 247 }
236 248
237 // Create socket. 249 // Create socket.
238 int fd = socket(AF_UNIX, SOCK_STREAM, 0); 250 int fd = socket(AF_UNIX, SOCK_STREAM, 0);
239 if (fd < 0) { 251 if (fd < 0) {
240 PLOG(ERROR) << "socket " << pipe_name; 252 PLOG(ERROR) << "socket " << pipe_path;
241 return false; 253 return false;
242 } 254 }
243 255
244 // Make socket non-blocking 256 // Make socket non-blocking
245 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { 257 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) {
246 PLOG(ERROR) << "fcntl(O_NONBLOCK) " << pipe_name; 258 PLOG(ERROR) << "fcntl(O_NONBLOCK) " << pipe_path;
247 if (HANDLE_EINTR(close(fd)) < 0) 259 if (HANDLE_EINTR(close(fd)) < 0)
248 PLOG(ERROR) << "close " << pipe_name; 260 PLOG(ERROR) << "close " << pipe_path;
249 return false; 261 return false;
250 } 262 }
251 263
252 // Create server side of socket. 264 // Create server side of socket.
253 struct sockaddr_un server_unix_addr; 265 struct sockaddr_un server_unix_addr;
254 memset(&server_unix_addr, 0, sizeof(server_unix_addr)); 266 memset(&server_unix_addr, 0, sizeof(server_unix_addr));
255 server_unix_addr.sun_family = AF_UNIX; 267 server_unix_addr.sun_family = AF_UNIX;
256 int path_len = snprintf(server_unix_addr.sun_path, IPC::kMaxPipeNameLength, 268 int path_len = snprintf(server_unix_addr.sun_path, IPC::kMaxPipeNameLength,
257 "%s", pipe_name.c_str()); 269 "%s", pipe_path.c_str());
258 DCHECK_EQ(static_cast<int>(pipe_name.length()), path_len); 270 DCHECK_EQ(static_cast<int>(pipe_path.length()), path_len);
259 size_t server_unix_addr_len = offsetof(struct sockaddr_un, 271 size_t server_unix_addr_len = offsetof(struct sockaddr_un,
260 sun_path) + path_len + 1; 272 sun_path) + path_len + 1;
261 273
262 if (HANDLE_EINTR(connect(fd, reinterpret_cast<sockaddr*>(&server_unix_addr), 274 if (HANDLE_EINTR(connect(fd, reinterpret_cast<sockaddr*>(&server_unix_addr),
263 server_unix_addr_len)) != 0) { 275 server_unix_addr_len)) != 0) {
264 PLOG(ERROR) << "connect " << pipe_name; 276 PLOG(ERROR) << "connect " << pipe_path;
265 if (HANDLE_EINTR(close(fd)) < 0) 277 if (HANDLE_EINTR(close(fd)) < 0)
266 PLOG(ERROR) << "close " << pipe_name; 278 PLOG(ERROR) << "close " << pipe_path;
267 return false; 279 return false;
268 } 280 }
269 281
270 *client_socket = fd; 282 *client_socket = fd;
271 return true; 283 return true;
272 } 284 }
273 285
274 bool SocketWriteErrorIsRecoverable() { 286 bool SocketWriteErrorIsRecoverable() {
275 #if defined(OS_MACOSX) 287 #if defined(OS_MACOSX)
276 // On OS X if sendmsg() is trying to send fds between processes and there 288 // 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 1203
1192 bool Channel::GetClientEuid(uid_t* client_euid) const { 1204 bool Channel::GetClientEuid(uid_t* client_euid) const {
1193 return channel_impl_->GetClientEuid(client_euid); 1205 return channel_impl_->GetClientEuid(client_euid);
1194 } 1206 }
1195 1207
1196 void Channel::ResetToAcceptingConnectionState() { 1208 void Channel::ResetToAcceptingConnectionState() {
1197 channel_impl_->ResetToAcceptingConnectionState(); 1209 channel_impl_->ResetToAcceptingConnectionState();
1198 } 1210 }
1199 1211
1200 } // namespace IPC 1212 } // 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