diff options
author | agl@chromium.org <agl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-06-18 00:44:42 +0000 |
---|---|---|
committer | agl@chromium.org <agl@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-06-18 00:44:42 +0000 |
commit | 43235a0aeb0a8de53bd6a7184fa1f4a71f5be083 (patch) | |
tree | 438c31b2bdf7a10e347c5dd0d0bb9c86f082ad4c /base | |
parent | 5f948943cc7581a276b2c4eb632af874a8fe469e (diff) | |
download | chromium_src-43235a0aeb0a8de53bd6a7184fa1f4a71f5be083.zip chromium_src-43235a0aeb0a8de53bd6a7184fa1f4a71f5be083.tar.gz chromium_src-43235a0aeb0a8de53bd6a7184fa1f4a71f5be083.tar.bz2 |
Linux: move SyncIPC function from Skia to base.
I'll be needing in some pending WebKit changes.
http://codereview.chromium.org/131006
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@18676 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/unix_domain_socket_posix.cc | 47 | ||||
-rw-r--r-- | base/unix_domain_socket_posix.h | 18 |
2 files changed, 65 insertions, 0 deletions
diff --git a/base/unix_domain_socket_posix.cc b/base/unix_domain_socket_posix.cc index 55cb186..024c086 100644 --- a/base/unix_domain_socket_posix.cc +++ b/base/unix_domain_socket_posix.cc @@ -10,6 +10,7 @@ #include "base/eintr_wrapper.h" #include "base/logging.h" +#include "base/pickle.h" namespace base { @@ -94,4 +95,50 @@ ssize_t RecvMsg(int fd, void* buf, size_t length, std::vector<int>* fds) { 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 index c0b40e8..4274a64 100644 --- a/base/unix_domain_socket_posix.h +++ b/base/unix_domain_socket_posix.h @@ -8,6 +8,8 @@ #include <sys/types.h> #include <vector> +class Pickle; + namespace base { // Use sendmsg to write the given msg and include a vector @@ -17,6 +19,22 @@ bool SendMsg(int fd, const void* msg, size_t length, // 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 |