OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "ipc/unix_domain_socket_util.h" | |
6 | |
7 #include <fcntl.h> | |
8 #include <sys/socket.h> | |
9 #include <sys/stat.h> | |
10 #include <sys/un.h> | |
11 #include <unistd.h> | |
12 | |
13 #include "base/file_util.h" | |
14 #include "base/files/file_path.h" | |
15 #include "base/logging.h" | |
16 #include "base/posix/eintr_wrapper.h" | |
17 | |
18 namespace IPC { | |
19 | |
20 // Verify that kMaxPipeNameLength is a decent size. | |
21 COMPILE_ASSERT(sizeof(((sockaddr_un*)0)->sun_path) >= kMaxPipeNameLength, | |
22 BAD_SUN_PATH_LENGTH); | |
23 | |
24 namespace { | |
25 | |
26 // returns fd (>= 0) on success, -1 on failure. | |
27 int MakeUnixAddrForPath(const std::string& pipe_name, | |
Mark Mentovai
2013/03/05 20:08:03
The comment should explain these parameters, becau
Mark Mentovai
2013/03/05 20:08:03
Similar comments about the use of the name “pipe”
jeremya
2013/03/06 05:03:18
Done.
jeremya
2013/03/06 05:03:18
Done.
| |
28 struct sockaddr_un* unix_addr, | |
29 size_t* unix_addr_len) { | |
30 DCHECK_GT(pipe_name.length(), 0u); | |
31 DCHECK_LT(pipe_name.length(), kMaxPipeNameLength); | |
32 | |
33 if (pipe_name.length() == 0 || pipe_name.length() >= kMaxPipeNameLength) { | |
Mark Mentovai
2013/03/05 20:08:03
I know you said that you didn’t think you needed t
jeremya
2013/03/06 05:03:18
I see. How confusing. I added a comment here to cl
| |
34 LOG(ERROR) << "Pipe name too long: " << pipe_name; | |
Mark Mentovai
2013/03/05 20:08:03
Or short.
jeremya
2013/03/06 05:03:18
Done.
| |
35 return -1; | |
36 } | |
37 | |
38 // Create socket. | |
39 int fd = socket(AF_UNIX, SOCK_STREAM, 0); | |
40 if (fd < 0) { | |
41 PLOG(ERROR) << "socket"; | |
42 return -1; | |
43 } | |
Mark Mentovai
2013/03/05 20:08:03
You should make a scoped_fd here (and return scope
jeremya
2013/03/06 05:03:18
Done.
| |
44 | |
45 // Make socket non-blocking | |
46 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) { | |
47 PLOG(ERROR) << "fcntl(O_NONBLOCK) " << pipe_name; | |
Mark Mentovai
2013/03/05 20:08:03
pipe_name is meaningless for error reporting for a
jeremya
2013/03/06 05:03:18
Done.
| |
48 if (HANDLE_EINTR(close(fd)) < 0) | |
49 PLOG(ERROR) << "close " << pipe_name; | |
50 return -1; | |
51 } | |
52 | |
53 // Create unix_addr structure. | |
54 DCHECK(unix_addr); | |
55 memset(unix_addr, 0, sizeof(struct sockaddr_un)); | |
56 unix_addr->sun_family = AF_UNIX; | |
57 unix_addr->sun_len = pipe_name.length(); | |
58 strncpy(unix_addr->sun_path, pipe_name.c_str(), kMaxPipeNameLength); | |
59 *unix_addr_len = | |
60 offsetof(struct sockaddr_un, sun_path) + pipe_name.length() + 1; | |
Mark Mentovai
2013/03/05 20:08:03
I think that the +1 isn’t necessary.
jeremya
2013/03/06 05:03:18
Done.
| |
61 return fd; | |
62 } | |
63 | |
64 } // namespace | |
65 | |
66 bool CreateServerUnixDomainSocket(const base::FilePath& pipe_path, | |
67 int* server_listen_fd) { | |
68 DCHECK(server_listen_fd); | |
69 | |
70 std::string pipe_name = pipe_path.value(); | |
71 base::FilePath pipe_dir = pipe_path.DirName(); | |
72 | |
73 struct sockaddr_un unix_addr; | |
74 size_t unix_addr_len; | |
75 int fd = MakeUnixAddrForPath(pipe_name, &unix_addr, &unix_addr_len); | |
76 if (fd < 0) | |
77 return false; | |
78 file_util::ScopedFD scoped_fd(&fd); | |
79 | |
80 // Make sure the path we need exists. | |
81 if (!file_util::CreateDirectory(pipe_dir)) { | |
jeremya
2013/03/05 03:52:44
Hm, problem: IPC channels normally want to be used
Mark Mentovai
2013/03/05 20:08:03
jeremya wrote:
jeremya
2013/03/06 05:03:18
Allowing IO might not be so bad as this is a rare
| |
82 LOG(ERROR) << "Couldn't create directory: " << pipe_dir.value(); | |
83 return false; | |
84 } | |
85 | |
86 // Delete any old FS instances. | |
87 if (unlink(pipe_name.c_str()) < 0 && errno != ENOENT) | |
Mark Mentovai
2013/03/05 20:08:03
This is where you want to #include <errno.h>.
jeremya
2013/03/06 05:03:18
Done.
| |
88 PLOG(ERROR) << "unlink " << pipe_name; | |
89 | |
90 // Bind the socket. | |
91 if (bind(fd, reinterpret_cast<const sockaddr*>(&unix_addr), | |
92 unix_addr_len) < 0) { | |
93 PLOG(ERROR) << "bind " << pipe_path.value(); | |
94 return false; | |
95 } | |
96 | |
97 // Start listening on the socket. | |
98 if (listen(fd, 1) < 0) { | |
Mark Mentovai
2013/03/05 20:08:03
1 is small. Use SOMAXCONN?
jeremya
2013/03/06 05:03:18
Done.
| |
99 PLOG(ERROR) << "listen " << pipe_path.value(); | |
100 unlink(pipe_name.c_str()); | |
101 return false; | |
102 } | |
103 | |
104 *server_listen_fd = *scoped_fd.release(); | |
105 return true; | |
106 } | |
107 | |
108 bool CreateClientUnixDomainSocket(const base::FilePath& pipe_path, | |
109 int* client_socket) { | |
110 DCHECK(client_socket); | |
111 | |
112 std::string pipe_name = pipe_path.value(); | |
113 base::FilePath pipe_dir = pipe_path.DirName(); | |
114 | |
115 struct sockaddr_un unix_addr; | |
116 size_t unix_addr_len; | |
117 int fd = MakeUnixAddrForPath(pipe_name, &unix_addr, &unix_addr_len); | |
118 if (fd < 0) | |
119 return false; | |
120 file_util::ScopedFD scoped_fd(&fd); | |
121 | |
122 if (HANDLE_EINTR(connect(fd, reinterpret_cast<sockaddr*>(&unix_addr), | |
123 unix_addr_len)) < 0) { | |
124 PLOG(ERROR) << "connect " << pipe_path.value(); | |
125 return false; | |
126 } | |
127 | |
128 *client_socket = *scoped_fd.release(); | |
129 return true; | |
130 } | |
131 | |
132 bool GetPeerEuid(int fd, uid_t* client_euid) { | |
133 #if defined(OS_MACOSX) || defined(OS_OPENBSD) || defined(OS_FREEBSD) | |
134 uid_t peer_euid; | |
135 gid_t peer_gid; | |
136 if (getpeereid(fd, &peer_euid, &peer_gid) < 0) { | |
137 PLOG(ERROR) << "getpeereid " << fd; | |
138 return false; | |
139 } | |
140 *client_euid = peer_euid; | |
141 return true; | |
142 #else | |
143 struct ucred cred; | |
144 socklen_t cred_len = sizeof(cred); | |
145 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cred, &cred_len) < 0) { | |
146 PLOG(ERROR) << "getsockopt " << fd; | |
147 return false; | |
148 } | |
149 if (static_cast<unsigned>(cred_len) < sizeof(cred)) { | |
150 NOTREACHED() << "Truncated ucred from SO_PEERCRED?"; | |
151 return false; | |
152 } | |
153 *client_euid = cred.uid; | |
154 return true; | |
155 #endif | |
156 } | |
157 | |
158 bool ServerAcceptConnection(int server_listen_fd, int* server_socket) { | |
159 DCHECK(server_socket); | |
160 | |
161 int accept_fd = HANDLE_EINTR(accept(server_listen_fd, NULL, 0)); | |
162 if (accept_fd < 0) | |
163 return false; | |
Mark Mentovai
2013/03/05 20:08:03
“false” may be a little simplistic as a return val
jeremya
2013/03/06 05:03:18
Addressed in a previous reply.
| |
164 file_util::ScopedFD scoped_fd(&accept_fd); | |
165 if (fcntl(accept_fd, F_SETFL, O_NONBLOCK) < 0) { | |
166 PLOG(ERROR) << "fcntl(O_NONBLOCK) " << accept_fd; | |
167 return false; | |
168 } | |
169 | |
170 *server_socket = *scoped_fd.release(); | |
171 return true; | |
172 } | |
173 | |
174 } // namespace IPC | |
OLD | NEW |