Index: ipc/unix_domain_socket_util.cc |
diff --git a/ipc/unix_domain_socket_util.cc b/ipc/unix_domain_socket_util.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..1611d8cc14211ba121bf056daf9b3045faab649b |
--- /dev/null |
+++ b/ipc/unix_domain_socket_util.cc |
@@ -0,0 +1,171 @@ |
+// Copyright 2013 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "ipc/unix_domain_socket_util.h" |
+ |
+#include <fcntl.h> |
+#include <sys/socket.h> |
+#include <sys/stat.h> |
+#include <sys/un.h> |
+#include <unistd.h> |
+ |
+#include "base/file_util.h" |
+#include "base/files/file_path.h" |
+#include "base/logging.h" |
+#include "base/posix/eintr_wrapper.h" |
+ |
+namespace IPC { |
+ |
+// Verify that kMaxPipeNameLength is a decent size. |
+COMPILE_ASSERT(sizeof(((sockaddr_un*)0)->sun_path) >= kMaxPipeNameLength, |
+ BAD_SUN_PATH_LENGTH); |
+ |
+namespace { |
+ |
+// returns fd (>= 0) on success, -1 on failure. |
+int MakeUnixAddrForPath(const std::string& pipe_name, |
+ struct sockaddr_un* unix_addr, |
+ size_t* unix_addr_len) { |
+ DCHECK_GT(pipe_name.length(), 0u); |
+ DCHECK_LT(pipe_name.length(), kMaxPipeNameLength); |
+ |
+ if (pipe_name.length() == 0 || pipe_name.length() >= kMaxPipeNameLength) |
+ return -1; |
+ |
+ // Create socket. |
+ int fd = socket(AF_UNIX, SOCK_STREAM, 0); |
+ if (fd < 0) |
+ return -1; |
+ |
+ // Make socket non-blocking |
+ if (fcntl(fd, F_SETFL, O_NONBLOCK) == -1) { |
+ PLOG(ERROR) << "fcntl(O_NONBLOCK) " << pipe_name; |
+ if (HANDLE_EINTR(close(fd)) < 0) |
+ 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.
|
+ return -1; |
+ } |
+ |
+ // Create unix_addr structure. |
+ DCHECK(unix_addr); |
+ memset(unix_addr, 0, sizeof(struct sockaddr_un)); |
+ unix_addr->sun_family = AF_UNIX; |
+ 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
|
+ "%s", pipe_name.c_str()); |
+ DCHECK_EQ(static_cast<int>(pipe_name.length()), path_len); |
+ *unix_addr_len = offsetof(struct sockaddr_un, sun_path) + path_len + 1; |
+ return fd; |
+} |
+ |
+} // namespace |
+ |
+bool CreateServerUnixDomainSocket(const base::FilePath& pipe_path, |
+ int* server_listen_fd) { |
+ DCHECK(server_listen_fd); |
+ |
+ std::string pipe_name = pipe_path.value(); |
+ base::FilePath pipe_dir = pipe_path.DirName(); |
+ |
+ struct sockaddr_un unix_addr; |
+ size_t unix_addr_len; |
+ int fd = MakeUnixAddrForPath(pipe_name, &unix_addr, &unix_addr_len); |
+ if (fd < 0) |
+ return false; |
+ 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
|
+ |
+ // Make sure the path we need exists. |
+ if (!file_util::CreateDirectory(pipe_dir)) |
+ return false; |
+ |
+ // Delete any old FS instances. |
+ if (unlink(pipe_name.c_str()) < 0) |
+ PLOG(ERROR) << "unlink " << pipe_name; |
+ |
+ // Bind the socket. |
+ if (bind(fd, reinterpret_cast<const sockaddr*>(&unix_addr), |
+ 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.
|
+ PLOG(ERROR) << "bind " << pipe_path.value(); |
+ return false; |
+ } |
+ |
+ // Start listening on the socket. |
+ const int listen_queue_length = 1; |
+ if (listen(fd, listen_queue_length) != 0) { |
+ PLOG(ERROR) << "listen " << pipe_path.value(); |
+ return false; |
+ } |
+ |
+ *server_listen_fd = fd; |
+ return true; |
+} |
+ |
+bool CreateClientUnixDomainSocket(const base::FilePath& pipe_path, |
+ int* client_socket) { |
+ DCHECK(client_socket); |
+ |
+ std::string pipe_name = pipe_path.value(); |
+ base::FilePath pipe_dir = pipe_path.DirName(); |
+ |
+ struct sockaddr_un unix_addr; |
+ size_t unix_addr_len; |
+ int fd = MakeUnixAddrForPath(pipe_name, &unix_addr, &unix_addr_len); |
+ if (fd < 0) |
+ return false; |
+ 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.
|
+ |
+ if (HANDLE_EINTR(connect(fd, reinterpret_cast<sockaddr*>(&unix_addr), |
+ unix_addr_len)) != 0) { |
+ PLOG(ERROR) << "connect " << pipe_path.value(); |
+ return false; |
+ } |
+ |
+ *client_socket = fd; |
+ return true; |
+} |
+ |
+bool GetPeerEuid(int fd, uid_t* client_euid) { |
+#if defined(OS_MACOSX) || defined(OS_OPENBSD) |
+ uid_t peer_euid; |
+ gid_t peer_gid; |
+ if (getpeereid(fd, &peer_euid, &peer_gid) != 0) { |
+ PLOG(ERROR) << "getpeereid " << fd; |
+ return false; |
+ } |
+ *client_euid = peer_euid; |
+ return true; |
+#elif defined(OS_SOLARIS) |
+ return false; |
+#else |
+ struct ucred cred; |
+ socklen_t cred_len = sizeof(cred); |
+ if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cred, &cred_len) != 0) { |
+ PLOG(ERROR) << "getsockopt " << fd; |
+ return false; |
+ } |
+ if (static_cast<unsigned>(cred_len) < sizeof(cred)) { |
+ NOTREACHED() << "Truncated ucred from SO_PEERCRED?"; |
+ return false; |
+ } |
+ *client_euid = cred.uid; |
+ return true; |
+#endif |
+} |
+ |
+bool ServerAcceptConnection(int server_listen_fd, int* server_socket) { |
+ DCHECK(server_socket); |
+ |
+ int accept_fd = HANDLE_EINTR(accept(server_listen_fd, NULL, 0)); |
+ if (accept_fd < 0) |
+ return false; |
+ if (fcntl(accept_fd, F_SETFL, O_NONBLOCK) == -1) { |
+ PLOG(ERROR) << "fcntl(O_NONBLOCK) " << accept_fd; |
+ if (HANDLE_EINTR(close(accept_fd)) < 0) |
+ PLOG(ERROR) << "close " << accept_fd; |
+ return false; |
+ } |
+ |
+ *server_socket = accept_fd; |
+ return true; |
+} |
+ |
+} // namespace IPC |
Mark Mentovai
2013/02/28 04:57:56
These functions look like they’d be pretty easy to
|