Debian Codegraph @

src/common/cockpitfdpassing.c

raw
1 /*
2 * Copyright (C) 2021 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 #include "config.h"
7
8 #include "cockpitfdpassing.h"
9
10 #include <assert.h>
11 #include <errno.h>
12 #include <poll.h>
13 #include <string.h>
14 #include <unistd.h>
15
16 /**
17 * cockpit_socket_msghdr_add_fd:
18 * @msg: a msghdr to be passed to a future call to sendmsg
19 * @cmsg: a buffer
20 * @cmsgsize: sizeof cmsg
21 * @fd: the fd to add to the msghdr
22 *
23 * Adds a cmsg to the given msghdr structure, transmitting @fd.
24 *
25 * @cmsg should be a pointer to a variable of type `struct cmsghdr[2]`
26 * and @cmsgsize should be the sizeof this variable. This is used for
27 * temporary storage and needs to stay around until the sendmsg() call.
28 *
29 * This can't fail.
30 */
31 void
32 cockpit_socket_msghdr_add_fd (struct msghdr *msg,
33 struct cmsghdr *cmsg,
34 size_t cmsgsize,
35 int fd)
36 {
37 /* make sure user's buffer is big enough */
38 assert (CMSG_SPACE(sizeof fd) <= cmsgsize);
39
40 /* make sure nothing else is there */
41 assert (msg->msg_control == NULL);
42 assert (msg->msg_controllen == 0);
43
44 /* we have an fd to send: create an SCM_RIGHTS cmsg. */
45 cmsg->cmsg_len = CMSG_LEN(sizeof fd);
46 cmsg->cmsg_level = SOL_SOCKET;
47 cmsg->cmsg_type = SCM_RIGHTS;
48 memcpy (CMSG_DATA(cmsg), &fd, sizeof fd);
49
50 /* attach it to the msghdr */
51 msg->msg_control = cmsg;
52 msg->msg_controllen = CMSG_LEN(sizeof fd);
53 }
54
55 /**
56 * cockpit_socket_send_fd:
57 * @socket_fd: a unix socket
58 * @fd: the fd to send
59 *
60 * Calls sendmsg() to write a single nul byte, plus a single file
61 * descriptor, @fd.
62 *
63 * If sendmsg() is successful then this function returns %true.
64 * Otherwise, %false is returned and errno will be set.
65 */
66 bool
67 cockpit_socket_send_fd (int socket_fd,
68 int fd)
69 {
70 struct msghdr msg = { .msg_iov = (struct iovec[]){ {(char[]){""}, 1 } },
71 .msg_iovlen = 1 };
72
73 struct cmsghdr cmsg[2];
74 cockpit_socket_msghdr_add_fd (&msg, cmsg, sizeof cmsg, fd);
75
76 ssize_t s;
77 do
78 s = sendmsg (socket_fd, &msg, MSG_NOSIGNAL);
79 while (s == -1 && errno == EINTR);
80
81 return s != -1;
82 }
83
84 /**
85 * cockpit_socket_receive_fd:
86 * @socket_fd: a unix socket
87 * @out_fd: the received file descriptor
88 *
89 * Calls recvmsg() to receive a single byte and (hopefully) a single file
90 * descriptor. The byte is discarded. The return value of this
91 * function is equal to the return value of the recvmsg() call.
92 *
93 * A return value of -1 indicates a syscall fail (with errno set). 0
94 * means EOF. A return value of 1 means that we received a message, and
95 * will set @fd to any file descriptor we received with the message (or
96 * -1 otherwise).
97 */
98 int
99 cockpit_socket_receive_fd (int socket_fd,
100 int *out_fd)
101 {
102 struct cmsghdr cmsg[2];
103 struct msghdr msg = { .msg_iov = (struct iovec[]){ {(char[]){""}, 1 } },
104 .msg_iovlen = 1,
105 .msg_control = &cmsg,
106 .msg_controllen = CMSG_LEN (sizeof *out_fd) };
107 assert (msg.msg_controllen <= sizeof cmsg);
108
109 /* recvmsg() has no MSG_DONTWAIT, and e.g. sudo makes stdin non-blocking with `log_output` option */
110 struct pollfd socket_pfd = { .fd = socket_fd, .events = POLLIN };
111 int ret;
112 do
113 ret = poll (&socket_pfd, 1, -1);
114 while (ret == -1 && errno == EINTR);
115 if (ret <= 0) /* error or timeout; the latter should not happen, but pass it on as "EOF" */
116 return ret;
117
118 ssize_t s;
119 do
120 s = recvmsg (socket_fd, &msg, 0);
121 while (s == -1 && errno == EINTR);
122
123 if (s == 1)
124 {
125 if (msg.msg_controllen &&
126 cmsg->cmsg_level == SOL_SOCKET &&
127 cmsg->cmsg_type == SCM_RIGHTS)
128 {
129 /* We originally set .msg_controllen to exactly the space
130 * required to receive a single fd, so if we received any
131 * SCM_RIGHTS messages, then it must surely only be a single
132 * fd.
133 */
134 assert (cmsg->cmsg_len == CMSG_LEN (sizeof *out_fd));
135 memcpy (out_fd, CMSG_DATA(cmsg), sizeof *out_fd);
136 }
137 else
138 *out_fd = -1;
139 }
140
141 return s;
142 }