blob: 8ea9ad68bd959f484ddfd93870032e1689234432 [file] [log] [blame]
Colin Crossab7c9f12016-01-14 15:35:40 -08001/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <errno.h>
18#include <string.h>
19
20#include "LeakPipe.h"
21
22#include "log.h"
23
Colin Cross1fa81f52017-06-21 13:13:00 -070024namespace android {
25
Colin Crossab7c9f12016-01-14 15:35:40 -080026bool LeakPipe::SendFd(int sock, int fd) {
Colin Cross401319a2017-06-22 10:50:05 -070027 struct msghdr hdr {};
28 struct iovec iov {};
Colin Crossab7c9f12016-01-14 15:35:40 -080029 unsigned int data = 0xfdfdfdfd;
30 alignas(struct cmsghdr) char cmsgbuf[CMSG_SPACE(sizeof(int))];
31
32 hdr.msg_iov = &iov;
33 hdr.msg_iovlen = 1;
34 iov.iov_base = &data;
35 iov.iov_len = sizeof(data);
36
37 hdr.msg_control = cmsgbuf;
38 hdr.msg_controllen = CMSG_LEN(sizeof(int));
39
40 struct cmsghdr* cmsg = CMSG_FIRSTHDR(&hdr);
41 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
42 cmsg->cmsg_level = SOL_SOCKET;
43 cmsg->cmsg_type = SCM_RIGHTS;
44
45 *(int*)CMSG_DATA(cmsg) = fd;
46
47 int ret = sendmsg(sock, &hdr, 0);
48 if (ret < 0) {
Christopher Ferris56b8d862017-05-03 17:34:29 -070049 MEM_ALOGE("failed to send fd: %s", strerror(errno));
Colin Crossab7c9f12016-01-14 15:35:40 -080050 return false;
51 }
52 if (ret == 0) {
Christopher Ferris56b8d862017-05-03 17:34:29 -070053 MEM_ALOGE("eof when sending fd");
Colin Crossab7c9f12016-01-14 15:35:40 -080054 return false;
55 }
56
57 return true;
58}
59
60int LeakPipe::ReceiveFd(int sock) {
Colin Cross401319a2017-06-22 10:50:05 -070061 struct msghdr hdr {};
62 struct iovec iov {};
Colin Crossab7c9f12016-01-14 15:35:40 -080063 unsigned int data;
64 alignas(struct cmsghdr) char cmsgbuf[CMSG_SPACE(sizeof(int))];
65
66 hdr.msg_iov = &iov;
67 hdr.msg_iovlen = 1;
68 iov.iov_base = &data;
69 iov.iov_len = sizeof(data);
70
71 hdr.msg_control = cmsgbuf;
72 hdr.msg_controllen = CMSG_LEN(sizeof(int));
73
74 int ret = recvmsg(sock, &hdr, 0);
75 if (ret < 0) {
Christopher Ferris56b8d862017-05-03 17:34:29 -070076 MEM_ALOGE("failed to receive fd: %s", strerror(errno));
Colin Crossab7c9f12016-01-14 15:35:40 -080077 return -1;
78 }
79 if (ret == 0) {
Christopher Ferris56b8d862017-05-03 17:34:29 -070080 MEM_ALOGE("eof when receiving fd");
Colin Crossab7c9f12016-01-14 15:35:40 -080081 return -1;
82 }
83
84 struct cmsghdr* cmsg = CMSG_FIRSTHDR(&hdr);
85 if (cmsg == NULL || cmsg->cmsg_level != SOL_SOCKET || cmsg->cmsg_type != SCM_RIGHTS) {
Christopher Ferris56b8d862017-05-03 17:34:29 -070086 MEM_ALOGE("missing fd while receiving fd");
Colin Crossab7c9f12016-01-14 15:35:40 -080087 return -1;
88 }
89
90 return *(int*)CMSG_DATA(cmsg);
91}
Colin Cross1fa81f52017-06-21 13:13:00 -070092
93} // namespace android