1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
|
/*****************************************************************************
* Copyright 2014 - 2015 Yichao Yu <yyc1992@gmail.com> *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation; either version 2.1 of the *
* License, or (at your option) version 3, or any later version accepted *
* by the membership of KDE e.V. (or its successor approved by the *
* membership of KDE e.V.), which shall act as a proxy defined in *
* Section 6 of version 3 of the license. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with this library. If not, *
* see <http://www.gnu.org/licenses/>. *
*****************************************************************************/
#include "fd_utils.h"
#include <sys/socket.h>
#include <fcntl.h>
QTC_EXPORT bool
qtcSendFD(int sock, int fd)
{
QTC_RET_IF_FAIL(fd >= 0 && sock >= 0, false);
char buf = 0;
iovec iov;
iov.iov_base = &buf;
iov.iov_len = 1;
union {
struct cmsghdr cmsghdr;
char control[CMSG_SPACE(sizeof(int))];
} cmsgu;
memset(&cmsgu, 0, sizeof(cmsgu));
msghdr msg;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = cmsgu.control;
msg.msg_controllen = sizeof(cmsgu.control);
cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
cmsg->cmsg_len = CMSG_LEN(sizeof(int));
cmsg->cmsg_level = SOL_SOCKET;
cmsg->cmsg_type = SCM_RIGHTS;
memcpy(CMSG_DATA(cmsg), &fd, sizeof(int));
return sendmsg(sock, &msg, 0) >= 0;
}
QTC_EXPORT int
qtcRecvFD(int sock)
{
QTC_RET_IF_FAIL(sock >= 0, -1);
char buf = 0;
iovec iov;
iov.iov_base = &buf;
iov.iov_len = 1;
union {
struct cmsghdr cmsghdr;
char control[CMSG_SPACE(sizeof(int))];
} cmsgu;
memset(&cmsgu, 0, sizeof(cmsgu));
msghdr msg;
msg.msg_iov = &iov;
msg.msg_iovlen = 1;
msg.msg_control = cmsgu.control;
msg.msg_controllen = sizeof(cmsgu.control);
QTC_RET_IF_FAIL(recvmsg(sock, &msg, 0) >= 0, -1);
struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
QTC_RET_IF_FAIL(cmsg && cmsg->cmsg_len == CMSG_LEN(sizeof(int)) &&
cmsg->cmsg_level == SOL_SOCKET &&
cmsg->cmsg_type == SCM_RIGHTS, -1);
int fd;
memcpy(&fd, CMSG_DATA(cmsg), sizeof(int));
return fd;
}
QTC_EXPORT bool
qtcFDSetCloexec(int fd, bool cloexec)
{
long flags;
flags = fcntl(fd, F_GETFD, 0);
if (flags == -1) {
return false;
}
if (cloexec) {
flags |= FD_CLOEXEC;
} else {
flags &= ~FD_CLOEXEC;
}
return fcntl(fd, F_SETFD, flags) != -1;
}
QTC_EXPORT bool
qtcFDSetNonBlock(int fd, bool nonblock)
{
long flags;
flags = fcntl(fd, F_GETFL, 0);
if (flags == -1) {
return false;
}
if (nonblock) {
nonblock |= O_NONBLOCK;
} else {
nonblock &= ~O_NONBLOCK;
}
return fcntl(fd, F_SETFL, flags) != -1;
}
|