diff options
author | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-12-22 20:02:29 +0000 |
---|---|---|
committer | brettw@chromium.org <brettw@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2010-12-22 20:02:29 +0000 |
commit | cf3ac397d5d729e299da8638c672055bba666e92 (patch) | |
tree | 4f3f8b634bdf3ebf92f424aef9b299e1a96d34fa /base | |
parent | 73db12c33f7b2e2ca53c2d962689b03e40e2106a (diff) | |
download | chromium_src-cf3ac397d5d729e299da8638c672055bba666e92.zip chromium_src-cf3ac397d5d729e299da8638c672055bba666e92.tar.gz chromium_src-cf3ac397d5d729e299da8638c672055bba666e92.tar.bz2 |
Move unix domain socket support out of base and into chrome common. This is not
used outside of Chrome.
TEST=it compiles
BUG=none
Review URL: http://codereview.chromium.org/5981006
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@69970 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/base.gypi | 1 | ||||
-rw-r--r-- | base/unix_domain_socket_posix.cc | 143 | ||||
-rw-r--r-- | base/unix_domain_socket_posix.h | 43 |
3 files changed, 0 insertions, 187 deletions
diff --git a/base/base.gypi b/base/base.gypi index b2dc0a2..edb6b78 100644 --- a/base/base.gypi +++ b/base/base.gypi @@ -269,7 +269,6 @@ 'tracked_objects.cc', 'tracked_objects.h', 'tuple.h', - 'unix_domain_socket_posix.cc', 'utf_offset_string_conversions.cc', 'utf_offset_string_conversions.h', 'utf_string_conversion_utils.cc', diff --git a/base/unix_domain_socket_posix.cc b/base/unix_domain_socket_posix.cc deleted file mode 100644 index 73fa260..0000000 --- a/base/unix_domain_socket_posix.cc +++ /dev/null @@ -1,143 +0,0 @@ -// Copyright (c) 2009 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 "base/unix_domain_socket_posix.h" - -#include <errno.h> -#include <unistd.h> -#include <sys/uio.h> -#include <sys/socket.h> - -#include "base/eintr_wrapper.h" -#include "base/logging.h" -#include "base/pickle.h" - -namespace base { - -bool SendMsg(int fd, const void* buf, size_t length, - const std::vector<int>& fds) { - struct msghdr msg; - memset(&msg, 0, sizeof(msg)); - struct iovec iov = {const_cast<void*>(buf), length}; - msg.msg_iov = &iov; - msg.msg_iovlen = 1; - - char* control_buffer = NULL; - if (fds.size()) { - const unsigned control_len = CMSG_SPACE(sizeof(int) * fds.size()); - control_buffer = new char[control_len]; - - struct cmsghdr *cmsg; - msg.msg_control = control_buffer; - msg.msg_controllen = control_len; - cmsg = CMSG_FIRSTHDR(&msg); - cmsg->cmsg_level = SOL_SOCKET; - cmsg->cmsg_type = SCM_RIGHTS; - cmsg->cmsg_len = CMSG_LEN(sizeof(int) * fds.size()); - memcpy(CMSG_DATA(cmsg), &fds[0], sizeof(int) * fds.size()); - msg.msg_controllen = cmsg->cmsg_len; - } - - const ssize_t r = HANDLE_EINTR(sendmsg(fd, &msg, 0)); - const bool ret = static_cast<ssize_t>(length) == r; - delete[] control_buffer; - return ret; -} - -ssize_t RecvMsg(int fd, void* buf, size_t length, std::vector<int>* fds) { - static const unsigned kMaxDescriptors = 16; - - fds->clear(); - - struct msghdr msg; - memset(&msg, 0, sizeof(msg)); - struct iovec iov = {buf, length}; - msg.msg_iov = &iov; - msg.msg_iovlen = 1; - - char control_buffer[CMSG_SPACE(sizeof(int) * kMaxDescriptors)]; - msg.msg_control = control_buffer; - msg.msg_controllen = sizeof(control_buffer); - - const ssize_t r = HANDLE_EINTR(recvmsg(fd, &msg, 0)); - if (r == -1) - return -1; - - int* wire_fds = NULL; - unsigned wire_fds_len = 0; - - if (msg.msg_controllen > 0) { - struct cmsghdr* cmsg; - for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) { - if (cmsg->cmsg_level == SOL_SOCKET && - cmsg->cmsg_type == SCM_RIGHTS) { - const unsigned payload_len = cmsg->cmsg_len - CMSG_LEN(0); - DCHECK(payload_len % sizeof(int) == 0); - wire_fds = reinterpret_cast<int*>(CMSG_DATA(cmsg)); - wire_fds_len = payload_len / sizeof(int); - break; - } - } - } - - if (msg.msg_flags & MSG_TRUNC || msg.msg_flags & MSG_CTRUNC) { - for (unsigned i = 0; i < wire_fds_len; ++i) - close(wire_fds[i]); - errno = EMSGSIZE; - return -1; - } - - fds->resize(wire_fds_len); - memcpy(&(*fds)[0], wire_fds, sizeof(int) * wire_fds_len); - - return r; -} - -ssize_t SendRecvMsg(int fd, uint8_t* reply, unsigned max_reply_len, int* result_fd, - const Pickle& request) { - int fds[2]; - - // This socketpair is only used for the IPC and is cleaned up before - // returning. - if (socketpair(AF_UNIX, SOCK_DGRAM, 0, fds) == -1) - return false; - - std::vector<int> fd_vector; - fd_vector.push_back(fds[1]); - if (!SendMsg(fd, request.data(), request.size(), fd_vector)) { - close(fds[0]); - close(fds[1]); - return -1; - } - close(fds[1]); - - fd_vector.clear(); - const ssize_t reply_len = RecvMsg(fds[0], reply, max_reply_len, &fd_vector); - close(fds[0]); - if (reply_len == -1) - return -1; - - if ((fd_vector.size() > 0 && result_fd == NULL) || fd_vector.size() > 1) { - for (std::vector<int>::const_iterator - i = fd_vector.begin(); i != fd_vector.end(); ++i) { - close(*i); - } - - NOTREACHED(); - - return -1; - } - - if (result_fd) { - if (fd_vector.size() == 0) { - *result_fd = -1; - } else { - *result_fd = fd_vector[0]; - } - } - - return reply_len; -} - -} // namespace base diff --git a/base/unix_domain_socket_posix.h b/base/unix_domain_socket_posix.h deleted file mode 100644 index 51c821b..0000000 --- a/base/unix_domain_socket_posix.h +++ /dev/null @@ -1,43 +0,0 @@ -// Copyright (c) 2009 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. - -#ifndef BASE_UNIX_DOMAIN_SOCKET_POSIX_H_ -#define BASE_UNIX_DOMAIN_SOCKET_POSIX_H_ -#pragma once - -#include <stdint.h> -#include <sys/types.h> -#include <vector> - -class Pickle; - -namespace base { - -// Use sendmsg to write the given msg and include a vector -// of file descriptors. Returns true iff successful. -bool SendMsg(int fd, const void* msg, size_t length, - const std::vector<int>& fds); -// Use recvmsg to read a message and an array of file descriptors. Returns -// -1 on failure. Note: will read, at most, 16 descriptors. -ssize_t RecvMsg(int fd, void* msg, size_t length, std::vector<int>* fds); -// Perform a sendmsg/recvmsg pair. -// 1. This process creates a UNIX DGRAM socketpair. -// 2. This proces writes a request to |fd| with an SCM_RIGHTS control message -// containing on end of the fresh socket pair. -// 3. This process blocks reading from the other end of the fresh socketpair. -// 4. The target process receives the request, processes it and writes the -// reply to the end of the socketpair contained in the request. -// 5. This process wakes up and continues. -// -// fd: descriptor to send the request on -// reply: buffer for the reply -// reply_len: size of |reply| -// result_fd: (may be NULL) the file descriptor returned in the reply (if any) -// request: the bytes to send in the request -ssize_t SendRecvMsg(int fd, uint8_t* reply, unsigned reply_len, int* result_fd, - const Pickle& request); - -} // namespace base - -#endif // BASE_UNIX_DOMAIN_SOCKET_POSIX_H_ |