annotate rust/chg/src/sendfds.c @ 47957:d44740725b95

rust: Rename Manifest to Manifestlog, ManifestEntry to Manifest This appears to match the terminology used in Python code and on https://www.mercurial-scm.org/wiki/Manifest Differential Revision: https://phab.mercurial-scm.org/D11404
author Simon Sapin <simon.sapin@octobus.net>
date Mon, 13 Sep 2021 13:01:25 +0200
parents 208cb7a9d0fa
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
39969
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
1 /*
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
2 * Utility to send fds via Unix domain socket
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
3 *
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
4 * Copyright 2011, 2018 Yuya Nishihara <yuya@tcha.org>
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
5 *
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
6 * This software may be used and distributed according to the terms of the
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
7 * GNU General Public License version 2 or any later version.
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
8 */
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
9
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
10 #include <errno.h>
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
11 #include <stdlib.h>
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
12 #include <string.h>
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
13 #include <sys/socket.h>
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
14 #include <sys/types.h>
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
15
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
16 #define MAX_FD_LEN 10
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
17
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
18 /*
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
19 * Sends the given fds with 1-byte dummy payload.
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
20 *
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
21 * Returns the number of bytes sent on success, -1 on error and errno is set
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
22 * appropriately.
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
23 */
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
24 ssize_t sendfds(int sockfd, const int *fds, size_t fdlen)
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
25 {
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
26 char dummy[1] = {0};
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
27 struct iovec iov = {dummy, sizeof(dummy)};
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
28 char fdbuf[CMSG_SPACE(sizeof(fds[0]) * MAX_FD_LEN)];
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
29 struct msghdr msgh;
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
30 struct cmsghdr *cmsg;
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
31
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
32 /* just use a fixed-size buffer since we'll never send tons of fds */
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
33 if (fdlen > MAX_FD_LEN) {
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
34 errno = EINVAL;
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
35 return -1;
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
36 }
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
37
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
38 memset(&msgh, 0, sizeof(msgh));
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
39 msgh.msg_iov = &iov;
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
40 msgh.msg_iovlen = 1;
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
41 msgh.msg_control = fdbuf;
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
42 msgh.msg_controllen = CMSG_SPACE(sizeof(fds[0]) * fdlen);
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
43
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
44 cmsg = CMSG_FIRSTHDR(&msgh);
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
45 cmsg->cmsg_level = SOL_SOCKET;
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
46 cmsg->cmsg_type = SCM_RIGHTS;
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
47 cmsg->cmsg_len = CMSG_LEN(sizeof(fds[0]) * fdlen);
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
48 memcpy(CMSG_DATA(cmsg), fds, sizeof(fds[0]) * fdlen);
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
49 msgh.msg_controllen = cmsg->cmsg_len;
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
50 return sendmsg(sockfd, &msgh, 0);
208cb7a9d0fa rust-chg: add function to send fds via domain socket
Yuya Nishihara <yuya@tcha.org>
parents:
diff changeset
51 }