rust/chg/src/sendfds.c
author Augie Fackler <augie@google.com>
Fri, 06 Dec 2019 15:19:47 -0500
changeset 43813 5a9e2ae9899b
parent 39969 208cb7a9d0fa
permissions -rw-r--r--
fuzz: use a more standard approach to allow local builds of fuzzers This is taken from the (improved since we started fuzzing) guide on ideal integrations. Rather than have our own wonky targets for building outside the fuzzer universe, we have a driver program we carry along and use when we're not using LibFuzzer. This will let us jettison a fair amount of goo. contrib/fuzz/standalone_fuzz_target_runner.cc is https://github.com/google/oss-fuzz/ file projects/example/my-api-repo/standalone from git revision c4579d9358a73ea5dbcc99cb985de1f2bf76dcf7, reformatted with out clang-format settings and a no-check-code comment added. It allows running a single test input through a fuzzer, rather than performing ongoing fuzzing as libfuzzer would. contrib/fuzz/FuzzedDataProvider.h is https://github.com/llvm/llvm-project/ file /compiler-rt/include/fuzzer/FuzzedDataProvider.h from git revision a44ef027ebca1598892ea9b104d6189aeb3bc2f0, reformatted with our clang-format settings and a no-check-code comment added. We can discard this if we instead want to add an hghave check for a new enough llvm that includes FuzzedDataProvder.h in the fuzzer headers. Differential Revision: https://phab.mercurial-scm.org/D7564

/*
 * Utility to send fds via Unix domain socket
 *
 * Copyright 2011, 2018 Yuya Nishihara <yuya@tcha.org>
 *
 * This software may be used and distributed according to the terms of the
 * GNU General Public License version 2 or any later version.
 */

#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>

#define MAX_FD_LEN 10

/*
 * Sends the given fds with 1-byte dummy payload.
 *
 * Returns the number of bytes sent on success, -1 on error and errno is set
 * appropriately.
 */
ssize_t sendfds(int sockfd, const int *fds, size_t fdlen)
{
	char dummy[1] = {0};
	struct iovec iov = {dummy, sizeof(dummy)};
	char fdbuf[CMSG_SPACE(sizeof(fds[0]) * MAX_FD_LEN)];
	struct msghdr msgh;
	struct cmsghdr *cmsg;

	/* just use a fixed-size buffer since we'll never send tons of fds */
	if (fdlen > MAX_FD_LEN) {
		errno = EINVAL;
		return -1;
	}

	memset(&msgh, 0, sizeof(msgh));
	msgh.msg_iov = &iov;
	msgh.msg_iovlen = 1;
	msgh.msg_control = fdbuf;
	msgh.msg_controllen = CMSG_SPACE(sizeof(fds[0]) * fdlen);

	cmsg = CMSG_FIRSTHDR(&msgh);
	cmsg->cmsg_level = SOL_SOCKET;
	cmsg->cmsg_type = SCM_RIGHTS;
	cmsg->cmsg_len = CMSG_LEN(sizeof(fds[0]) * fdlen);
	memcpy(CMSG_DATA(cmsg), fds, sizeof(fds[0]) * fdlen);
	msgh.msg_controllen = cmsg->cmsg_len;
	return sendmsg(sockfd, &msgh, 0);
}