Chromium Code Reviews| 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 LOG(ERROR) << "Pipe name too long: " << pipe_name; | |
| 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 } | |
| 44 | |
| 45 // Make socket non-blocking | |
| 46 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0) { | |
| 47 PLOG(ERROR) << "fcntl(O_NONBLOCK) " << pipe_name; | |
| 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 strncpy(unix_addr->sun_path, pipe_name.c_str(), kMaxPipeNameLength); | |
|
palmer
2013/02/28 23:48:28
strncpy does not guarantee to NUL-terminate the st
Mark Mentovai
2013/03/01 03:19:17
Chris P. wrote:
jeremya
2013/03/01 03:49:25
I don't think it needs to be NUL-terminated, as th
| |
| 58 *unix_addr_len = | |
| 59 offsetof(struct sockaddr_un, sun_path) + pipe_name.length() + 1; | |
| 60 return fd; | |
| 61 } | |
| 62 | |
| 63 } // namespace | |
| 64 | |
| 65 bool CreateServerUnixDomainSocket(const base::FilePath& pipe_path, | |
| 66 int* server_listen_fd) { | |
| 67 DCHECK(server_listen_fd); | |
| 68 | |
| 69 std::string pipe_name = pipe_path.value(); | |
| 70 base::FilePath pipe_dir = pipe_path.DirName(); | |
| 71 | |
| 72 struct sockaddr_un unix_addr; | |
| 73 size_t unix_addr_len; | |
| 74 int fd = MakeUnixAddrForPath(pipe_name, &unix_addr, &unix_addr_len); | |
| 75 if (fd < 0) | |
| 76 return false; | |
| 77 file_util::ScopedFD scoped_fd(&fd); | |
| 78 | |
| 79 // Make sure the path we need exists. | |
| 80 if (!file_util::CreateDirectory(pipe_dir)) { | |
| 81 LOG(ERROR) << "Couldn't create directory: " << pipe_dir.value(); | |
| 82 return false; | |
| 83 } | |
| 84 | |
| 85 // Delete any old FS instances. | |
| 86 if (unlink(pipe_name.c_str()) < 0) | |
| 87 PLOG(ERROR) << "unlink " << pipe_name; | |
| 88 | |
| 89 // Bind the socket. | |
| 90 if (bind(fd, reinterpret_cast<const sockaddr*>(&unix_addr), | |
| 91 unix_addr_len) < 0) { | |
| 92 PLOG(ERROR) << "bind " << pipe_path.value(); | |
| 93 return false; | |
| 94 } | |
| 95 | |
| 96 // Start listening on the socket. | |
| 97 const int listen_queue_length = 1; | |
|
Mark Mentovai
2013/03/01 03:19:17
I don’t think you need a separate constant for thi
jeremya
2013/03/01 03:49:25
Done.
| |
| 98 if (listen(fd, listen_queue_length) < 0) { | |
| 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) | |
|
palmer
2013/02/28 23:48:28
This method works on FreeBSD >= 4.6 (which is why
jeremya
2013/03/01 03:49:25
Not sure what you're implying here. Should I remov
palmer
2013/03/01 19:14:30
I'd say:
#if defined(OS_MACOSX) || defined(OS_OPE
jeremya
2013/03/03 21:39:03
Done.
| |
| 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 #elif defined(OS_SOLARIS) | |
| 143 return false; | |
|
palmer
2013/02/28 23:48:28
Yikes!! Not that Solaris is an officially-supporte
jeremya
2013/03/01 03:49:25
Just copied from the ipc_channel_posix.cc implemen
| |
| 144 #else | |
|
palmer
2013/02/28 23:48:28
...and I'm not entirely sure this method *does* wo
jeremya
2013/03/01 03:49:25
Eh? What about Linux?
palmer
2013/03/01 19:14:30
This #else block works/should work fine as-is for
| |
| 145 struct ucred cred; | |
| 146 socklen_t cred_len = sizeof(cred); | |
| 147 if (getsockopt(fd, SOL_SOCKET, SO_PEERCRED, &cred, &cred_len) < 0) { | |
| 148 PLOG(ERROR) << "getsockopt " << fd; | |
| 149 return false; | |
| 150 } | |
| 151 if (static_cast<unsigned>(cred_len) < sizeof(cred)) { | |
| 152 NOTREACHED() << "Truncated ucred from SO_PEERCRED?"; | |
| 153 return false; | |
| 154 } | |
| 155 *client_euid = cred.uid; | |
| 156 return true; | |
| 157 #endif | |
| 158 } | |
| 159 | |
| 160 bool ServerAcceptConnection(int server_listen_fd, int* server_socket) { | |
| 161 DCHECK(server_socket); | |
| 162 | |
| 163 int accept_fd = HANDLE_EINTR(accept(server_listen_fd, NULL, 0)); | |
| 164 if (accept_fd < 0) | |
| 165 return false; | |
| 166 if (fcntl(accept_fd, F_SETFL, O_NONBLOCK) < 0) { | |
| 167 PLOG(ERROR) << "fcntl(O_NONBLOCK) " << accept_fd; | |
| 168 if (HANDLE_EINTR(close(accept_fd)) < 0) | |
|
Mark Mentovai
2013/03/01 03:19:17
You can use the scoper in this function too.
jeremya
2013/03/01 03:49:25
Done.
| |
| 169 PLOG(ERROR) << "close " << accept_fd; | |
| 170 return false; | |
| 171 } | |
| 172 | |
| 173 *server_socket = accept_fd; | |
| 174 return true; | |
| 175 } | |
| 176 | |
| 177 } // namespace IPC | |
| OLD | NEW |