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, | |
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) | |
34 return -1; | |
35 | |
36 // Create socket. | |
37 int fd = socket(AF_UNIX, SOCK_STREAM, 0); | |
38 if (fd < 0) | |
39 return -1; | |
40 | |
41 // Make socket non-blocking | |
42 if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { | |
43 PLOG(ERROR) << "fcntl(O_NONBLOCK) " << pipe_name; | |
44 if (HANDLE_EINTR(close(fd)) < 0) | |
45 PLOG(ERROR) << "close " << pipe_name; | |
Mark Mentovai
2013/02/28 04:57:56
Wanna use your FD scoper here too? You’d need to r
jeremya
2013/02/28 05:58:18
Done.
| |
46 return -1; | |
47 } | |
48 | |
49 // Create unix_addr structure. | |
50 DCHECK(unix_addr); | |
51 memset(unix_addr, 0, sizeof(struct sockaddr_un)); | |
52 unix_addr->sun_family = AF_UNIX; | |
53 int path_len = snprintf(unix_addr->sun_path, kMaxPipeNameLength, | |
Mark Mentovai
2013/02/28 04:57:56
Why not just strncpy?
jeremya
2013/02/28 05:58:18
Done. Do I need to check the return value? strncpy
| |
54 "%s", pipe_name.c_str()); | |
55 DCHECK_EQ(static_cast<int>(pipe_name.length()), path_len); | |
56 *unix_addr_len = offsetof(struct sockaddr_un, sun_path) + path_len + 1; | |
57 return fd; | |
58 } | |
59 | |
60 } // namespace | |
61 | |
62 bool CreateServerUnixDomainSocket(const base::FilePath& pipe_path, | |
63 int* server_listen_fd) { | |
64 DCHECK(server_listen_fd); | |
65 | |
66 std::string pipe_name = pipe_path.value(); | |
67 base::FilePath pipe_dir = pipe_path.DirName(); | |
68 | |
69 struct sockaddr_un unix_addr; | |
70 size_t unix_addr_len; | |
71 int fd = MakeUnixAddrForPath(pipe_name, &unix_addr, &unix_addr_len); | |
72 if (fd < 0) | |
73 return false; | |
74 file_util::ScopedFD scoped_fd(&fd); | |
Mark Mentovai
2013/02/28 04:57:56
…meaning fd will be closed when this function retu
jeremya
2013/02/28 05:58:18
... yep. good catch, i found this independently af
| |
75 | |
76 // Make sure the path we need exists. | |
77 if (!file_util::CreateDirectory(pipe_dir)) | |
78 return false; | |
79 | |
80 // Delete any old FS instances. | |
81 if (unlink(pipe_name.c_str()) < 0) | |
82 PLOG(ERROR) << "unlink " << pipe_name; | |
83 | |
84 // Bind the socket. | |
85 if (bind(fd, reinterpret_cast<const sockaddr*>(&unix_addr), | |
86 unix_addr_len) != 0) { | |
Mark Mentovai
2013/02/28 04:57:56
Sometimes you write <0 (on line 81), and sometimes
jeremya
2013/02/28 05:58:18
Done.
| |
87 PLOG(ERROR) << "bind " << pipe_path.value(); | |
88 return false; | |
89 } | |
90 | |
91 // Start listening on the socket. | |
92 const int listen_queue_length = 1; | |
93 if (listen(fd, listen_queue_length) != 0) { | |
94 PLOG(ERROR) << "listen " << pipe_path.value(); | |
95 return false; | |
96 } | |
97 | |
98 *server_listen_fd = fd; | |
99 return true; | |
100 } | |
101 | |
102 bool CreateClientUnixDomainSocket(const base::FilePath& pipe_path, | |
103 int* client_socket) { | |
104 DCHECK(client_socket); | |
105 | |
106 std::string pipe_name = pipe_path.value(); | |
107 base::FilePath pipe_dir = pipe_path.DirName(); | |
108 | |
109 struct sockaddr_un unix_addr; | |
110 size_t unix_addr_len; | |
111 int fd = MakeUnixAddrForPath(pipe_name, &unix_addr, &unix_addr_len); | |
112 if (fd < 0) | |
113 return false; | |
114 file_util::ScopedFD scoped_fd(&fd); | |
Mark Mentovai
2013/02/28 04:57:56
Same thing here.
jeremya
2013/02/28 05:58:18
Done.
| |
115 | |
116 if (HANDLE_EINTR(connect(fd, reinterpret_cast<sockaddr*>(&unix_addr), | |
117 unix_addr_len)) != 0) { | |
118 PLOG(ERROR) << "connect " << pipe_path.value(); | |
119 return false; | |
120 } | |
121 | |
122 *client_socket = fd; | |
123 return true; | |
124 } | |
125 | |
126 bool GetPeerEuid(int fd, uid_t* client_euid) { | |
127 #if defined(OS_MACOSX) || defined(OS_OPENBSD) | |
128 uid_t peer_euid; | |
129 gid_t peer_gid; | |
130 if (getpeereid(fd, &peer_euid, &peer_gid) != 0) { | |
131 PLOG(ERROR) << "getpeereid " << fd; | |
132 return false; | |
133 } | |
134 *client_euid = peer_euid; | |
135 return true; | |
136 #elif defined(OS_SOLARIS) | |
137 return false; | |
138 #else | |
139 struct ucred cred; | |
140 socklen_t cred_len = sizeof(cred); | |
141 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cred, &cred_len) != 0) { | |
142 PLOG(ERROR) << "getsockopt " << fd; | |
143 return false; | |
144 } | |
145 if (static_cast<unsigned>(cred_len) < sizeof(cred)) { | |
146 NOTREACHED() << "Truncated ucred from SO_PEERCRED?"; | |
147 return false; | |
148 } | |
149 *client_euid = cred.uid; | |
150 return true; | |
151 #endif | |
152 } | |
153 | |
154 bool ServerAcceptConnection(int server_listen_fd, int* server_socket) { | |
155 DCHECK(server_socket); | |
156 | |
157 int accept_fd = HANDLE_EINTR(accept(server_listen_fd, NULL, 0)); | |
158 if (accept_fd < 0) | |
159 return false; | |
160 if (fcntl(accept_fd, F_SETFL, O_NONBLOCK) == -1) { | |
161 PLOG(ERROR) << "fcntl(O_NONBLOCK) " << accept_fd; | |
162 if (HANDLE_EINTR(close(accept_fd)) < 0) | |
163 PLOG(ERROR) << "close " << accept_fd; | |
164 return false; | |
165 } | |
166 | |
167 *server_socket = accept_fd; | |
168 return true; | |
169 } | |
170 | |
171 } // namespace IPC | |
Mark Mentovai
2013/02/28 04:57:56
These functions look like they’d be pretty easy to
| |
OLD | NEW |