codemod 2010-2016 to 2010-present
[hiphop-php.git] / hphp / util / afdt-util.cpp
blobc62a3b0210b28f963f8475e0dbbd1642b07cc5ac
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
16 #include "hphp/util/afdt-util.h"
17 #include <afdt.h>
18 #include <errno.h>
19 #include <limits.h>
21 namespace HPHP {
22 namespace afdt {
23 ///////////////////////////////////////////////////////////////////////////////
25 namespace detail {
27 namespace {
28 void append(std::vector<iovec>& iov, const void* addr, size_t size) {
29 iovec io;
30 io.iov_base = const_cast<void*>(addr);
31 io.iov_len = size;
32 iov.push_back(io);
36 void sappend(int afdt_fd,
37 std::vector<iovec>& iov, const void* addr, size_t size) {
38 if (iov.size() == IOV_MAX) {
39 send(afdt_fd, iov);
40 iov.clear();
42 append(iov, addr, size);
45 void rappend(int afdt_fd,
46 std::vector<iovec>& iov, void* addr, size_t size) {
47 if (iov.size() == IOV_MAX) {
48 recv(afdt_fd, iov);
49 iov.clear();
51 append(iov, addr, size);
54 void send(int afdt_fd, std::vector<iovec>& iov) {
55 struct msghdr msg;
56 memset(&msg, 0, sizeof(msg));
57 msg.msg_iov = &iov[0];
58 msg.msg_iovlen = iov.size();
59 ssize_t nwritten = sendmsg(afdt_fd, &msg, 0);
60 if (nwritten < 0) throw std::runtime_error("send failed");
61 for (auto& io : iov) {
62 nwritten -= io.iov_len;
64 if (nwritten) throw std::runtime_error("sent wrong number of bytes");
67 void recv(int afdt_fd, std::vector<iovec>& iov) {
68 struct msghdr msg;
69 memset(&msg, 0, sizeof(msg));
70 msg.msg_iov = &iov[0];
71 msg.msg_iovlen = iov.size();
72 ssize_t nread = recvmsg(afdt_fd, &msg, 0);
73 if (nread <= 0) throw std::runtime_error("recv failed");
74 for (auto& io : iov) {
75 nread -= io.iov_len;
77 if (nread) throw std::runtime_error("recv received wrong number of bytes");
82 bool send_fd(int afdt_fd, int fd) {
83 afdt_error_t err = AFDT_ERROR_T_INIT;
84 errno = 0;
85 int ret = afdt_send_fd_msg(afdt_fd, nullptr, 0, fd, &err);
86 if (ret < 0 && errno == 0) {
87 // Set non-empty errno if afdt_send_fd_msg doesn't set one on error
88 errno = EPROTO;
90 return ret >= 0;
93 int recv_fd(int afdt_fd) {
94 int fd;
95 afdt_error_t err = AFDT_ERROR_T_INIT;
96 uint32_t afdt_len = 0;
97 errno = 0;
98 if (afdt_recv_fd_msg(afdt_fd, nullptr, &afdt_len, &fd, &err) < 0) {
99 if (errno == 0) {
100 // Set non-empty errno if afdt_send_fd_msg doesn't set one on error
101 errno = EPROTO;
103 return -1;
105 return fd;
108 ///////////////////////////////////////////////////////////////////////////////