1 /* Copyright 2020-2021 IGEL Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 #include "dlm-protocol.h"
20 #include <sys/socket.h>
21 #include <sys/types.h>
24 bool receive_dlm_client_request(int socket, struct dlm_client_request *request)
30 .iov_len = sizeof(*request),
37 while ((len = recvmsg(socket, &msg, 0)) < 0) {
42 if (len != sizeof(*request)) {
49 bool send_dlm_client_request(int socket, struct dlm_client_request *request)
53 .iov_len = sizeof(*request),
61 while (sendmsg(socket, &msg, 0) < 1) {
68 int receive_lease_fd(int socket)
71 char ctrl_buf[CMSG_SPACE(sizeof(lease_fd))];
74 struct iovec iov = {.iov_base = &data, .iov_len = sizeof(data)};
78 .msg_control = ctrl_buf,
79 .msg_controllen = sizeof(ctrl_buf),
83 while ((len = recvmsg(socket, &msg, 0)) <= 0) {
94 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
95 cmsg = CMSG_NXTHDR(&msg, cmsg)) {
96 if (cmsg->cmsg_level == SOL_SOCKET &&
97 cmsg->cmsg_type == SCM_RIGHTS) {
98 int nfds = (cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int);
99 int *fds = (int *)CMSG_DATA(cmsg);
106 /* Close any unexpected fds so we don't leak them. */
107 for (int i = 0; i < nfds; i++)
122 bool send_lease_fd(int socket, int lease)
127 .iov_len = sizeof(data),
130 char ctrl_buf[CMSG_SPACE(sizeof(lease))] = {0};
132 struct msghdr msg = {
135 .msg_controllen = sizeof(ctrl_buf),
136 .msg_control = ctrl_buf,
139 struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
140 cmsg->cmsg_level = SOL_SOCKET;
141 cmsg->cmsg_type = SCM_RIGHTS;
142 cmsg->cmsg_len = CMSG_LEN(sizeof(lease));
143 *((int *)CMSG_DATA(cmsg)) = lease;
145 if (sendmsg(socket, &msg, 0) < 0)