File: scm-code.c

package info (click to toggle)
criu 4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 11,500 kB
  • sloc: ansic: 139,280; python: 7,484; sh: 3,824; java: 2,799; makefile: 2,659; asm: 1,137; perl: 206; xml: 117; exp: 45
file content (117 lines) | stat: -rw-r--r-- 3,135 bytes parent folder | download | duplicates (3)
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
111
112
113
114
115
116
117
#ifndef __sys
#error "The __sys macro is required"
#endif

static void scm_fdset_init_chunk(struct scm_fdset *fdset, int nr_fds, void *data, unsigned ch_size)
{
	struct cmsghdr *cmsg;
	static char dummy;

	fdset->hdr.msg_controllen = CMSG_LEN(sizeof(int) * nr_fds);

	cmsg = CMSG_FIRSTHDR(&fdset->hdr);
	cmsg->cmsg_len = fdset->hdr.msg_controllen;

	if (data) {
		fdset->iov.iov_base = data;
		fdset->iov.iov_len = nr_fds * ch_size;
	} else {
		fdset->iov.iov_base = &dummy;
		fdset->iov.iov_len = 1;
	}
}

static int *scm_fdset_init(struct scm_fdset *fdset, struct sockaddr_un *saddr, int saddr_len)
{
	struct cmsghdr *cmsg;

	BUILD_BUG_ON(sizeof(fdset->msg_buf) < (CMSG_SPACE(sizeof(int) * CR_SCM_MAX_FD)));

	fdset->iov.iov_base = (void *)0xdeadbeef;

	fdset->hdr.msg_iov = &fdset->iov;
	fdset->hdr.msg_iovlen = 1;
	fdset->hdr.msg_name = (struct sockaddr *)saddr;
	fdset->hdr.msg_namelen = saddr_len;

	fdset->hdr.msg_control = &fdset->msg_buf;
	fdset->hdr.msg_controllen = CMSG_LEN(sizeof(int) * CR_SCM_MAX_FD);

	cmsg = CMSG_FIRSTHDR(&fdset->hdr);
	cmsg->cmsg_len = fdset->hdr.msg_controllen;
	cmsg->cmsg_level = SOL_SOCKET;
	cmsg->cmsg_type = SCM_RIGHTS;

	return (int *)CMSG_DATA(cmsg);
}

int send_fds(int sock, struct sockaddr_un *saddr, int len, int *fds, int nr_fds, void *data, unsigned ch_size)
{
	/* In musl_libc the msghdr structure has pads which has to be zeroed */
	struct scm_fdset fdset = {};
	int *cmsg_data;
	int i, min_fd, ret;

	cmsg_data = scm_fdset_init(&fdset, saddr, len);
	for (i = 0; i < nr_fds; i += min_fd) {
		min_fd = min(CR_SCM_MAX_FD, nr_fds - i);
		scm_fdset_init_chunk(&fdset, min_fd, data, ch_size);
		memcpy(cmsg_data, &fds[i], sizeof(int) * min_fd);

		ret = __sys(sendmsg)(sock, &fdset.hdr, 0);
		if (ret <= 0)
			return ret ?: -1;

		if (data)
			data += min_fd * ch_size;
	}

	return 0;
}

int __recv_fds(int sock, int *fds, int nr_fds, void *data, unsigned ch_size, int flags)
{
	/* In musl_libc the msghdr structure has pads which has to be zeroed */
	struct scm_fdset fdset = {};
	struct cmsghdr *cmsg;
	int *cmsg_data;
	int ret;
	int i, min_fd;

	cmsg_data = scm_fdset_init(&fdset, NULL, 0);
	for (i = 0; i < nr_fds; i += min_fd) {
		min_fd = min(CR_SCM_MAX_FD, nr_fds - i);
		scm_fdset_init_chunk(&fdset, min_fd, data, ch_size);

		ret = __sys(recvmsg)(sock, &fdset.hdr, flags);
		if (ret <= 0)
			return ret ? __sys_err(ret) : -ENOMSG;

		cmsg = CMSG_FIRSTHDR(&fdset.hdr);
		if (!cmsg || cmsg->cmsg_type != SCM_RIGHTS)
			return -EINVAL;
		if (fdset.hdr.msg_flags & MSG_CTRUNC)
			return -ENFILE;

		min_fd = (cmsg->cmsg_len - sizeof(struct cmsghdr)) / sizeof(int);
		/*
		 * In case if kernel screwed the recipient, most probably
		 * the caller stack frame will be overwritten, just scream
		 * and exit.
		 *
		 * FIXME Need to sanitize util.h to be able to include it
		 * into files which do not have glibc and a couple of
		 * sys_write_ helpers. Meawhile opencoded BUG_ON here.
		 */
		BUG_ON(min_fd > CR_SCM_MAX_FD);

		if (unlikely(min_fd <= 0))
			return -EBADFD;

		memcpy(&fds[i], cmsg_data, sizeof(int) * min_fd);
		if (data)
			data += ch_size * min_fd;
	}

	return 0;
}