Home | History | Annotate | Line # | Download | only in dist
monitor_fdpass.c revision 1.9
      1  1.3      adam /*	$NetBSD: monitor_fdpass.c,v 1.9 2021/03/05 17:47:16 christos Exp $	*/
      2  1.9  christos /* $OpenBSD: monitor_fdpass.c,v 1.22 2020/10/18 11:32:01 djm Exp $ */
      3  1.9  christos 
      4  1.1  christos /*
      5  1.1  christos  * Copyright 2001 Niels Provos <provos (at) citi.umich.edu>
      6  1.1  christos  * All rights reserved.
      7  1.1  christos  *
      8  1.1  christos  * Redistribution and use in source and binary forms, with or without
      9  1.1  christos  * modification, are permitted provided that the following conditions
     10  1.1  christos  * are met:
     11  1.1  christos  * 1. Redistributions of source code must retain the above copyright
     12  1.1  christos  *    notice, this list of conditions and the following disclaimer.
     13  1.1  christos  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  christos  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  christos  *    documentation and/or other materials provided with the distribution.
     16  1.1  christos  *
     17  1.1  christos  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  1.1  christos  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  1.1  christos  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  1.1  christos  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  1.1  christos  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  1.1  christos  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  1.1  christos  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  1.1  christos  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  1.1  christos  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  1.1  christos  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  1.1  christos  */
     28  1.1  christos 
     29  1.2  christos #include "includes.h"
     30  1.3      adam __RCSID("$NetBSD: monitor_fdpass.c,v 1.9 2021/03/05 17:47:16 christos Exp $");
     31  1.1  christos #include <sys/types.h>
     32  1.1  christos #include <sys/socket.h>
     33  1.1  christos #include <sys/uio.h>
     34  1.1  christos 
     35  1.1  christos #include <errno.h>
     36  1.3      adam #include <poll.h>
     37  1.1  christos #include <string.h>
     38  1.1  christos #include <stdarg.h>
     39  1.1  christos 
     40  1.1  christos #include "log.h"
     41  1.1  christos #include "monitor_fdpass.h"
     42  1.1  christos 
     43  1.1  christos int
     44  1.1  christos mm_send_fd(int sock, int fd)
     45  1.1  christos {
     46  1.1  christos 	struct msghdr msg;
     47  1.1  christos 	union {
     48  1.1  christos 		struct cmsghdr hdr;
     49  1.2  christos 		char buf[1024];
     50  1.1  christos 	} cmsgbuf;
     51  1.1  christos 	struct cmsghdr *cmsg;
     52  1.1  christos 	struct iovec vec;
     53  1.1  christos 	char ch = '\0';
     54  1.1  christos 	ssize_t n;
     55  1.3      adam 	struct pollfd pfd;
     56  1.1  christos 
     57  1.2  christos 	if (sizeof(cmsgbuf.buf) < CMSG_SPACE(sizeof(int))) {
     58  1.2  christos 		error("%s: %zu < %zu, recompile", __func__,
     59  1.2  christos 		    sizeof(cmsgbuf.buf), CMSG_SPACE(sizeof(int)));
     60  1.2  christos 		return -1;
     61  1.2  christos 	}
     62  1.2  christos 
     63  1.1  christos 	memset(&msg, 0, sizeof(msg));
     64  1.5  christos 	memset(&cmsgbuf, 0, sizeof(cmsgbuf));
     65  1.2  christos 	msg.msg_control = &cmsgbuf.buf;
     66  1.2  christos 	msg.msg_controllen = CMSG_SPACE(sizeof(int));
     67  1.1  christos 	cmsg = CMSG_FIRSTHDR(&msg);
     68  1.1  christos 	cmsg->cmsg_len = CMSG_LEN(sizeof(int));
     69  1.1  christos 	cmsg->cmsg_level = SOL_SOCKET;
     70  1.1  christos 	cmsg->cmsg_type = SCM_RIGHTS;
     71  1.1  christos 	*(int *)CMSG_DATA(cmsg) = fd;
     72  1.2  christos 	msg.msg_controllen = cmsg->cmsg_len;
     73  1.1  christos 
     74  1.1  christos 	vec.iov_base = &ch;
     75  1.1  christos 	vec.iov_len = 1;
     76  1.1  christos 	msg.msg_iov = &vec;
     77  1.1  christos 	msg.msg_iovlen = 1;
     78  1.1  christos 
     79  1.3      adam 	pfd.fd = sock;
     80  1.3      adam 	pfd.events = POLLOUT;
     81  1.3      adam 	while ((n = sendmsg(sock, &msg, 0)) == -1 &&
     82  1.3      adam 	    (errno == EAGAIN || errno == EINTR)) {
     83  1.9  christos 		debug3_f("sendmsg(%d): %s", fd, strerror(errno));
     84  1.3      adam 		(void)poll(&pfd, 1, -1);
     85  1.3      adam 	}
     86  1.1  christos 	if (n == -1) {
     87  1.9  christos 		error_f("sendmsg(%d): %s", fd, strerror(errno));
     88  1.1  christos 		return -1;
     89  1.1  christos 	}
     90  1.1  christos 
     91  1.1  christos 	if (n != 1) {
     92  1.9  christos 		error_f("sendmsg: expected sent 1 got %zd", n);
     93  1.1  christos 		return -1;
     94  1.1  christos 	}
     95  1.1  christos 	return 0;
     96  1.1  christos }
     97  1.1  christos 
     98  1.1  christos int
     99  1.1  christos mm_receive_fd(int sock)
    100  1.1  christos {
    101  1.1  christos 	struct msghdr msg;
    102  1.1  christos 	union {
    103  1.1  christos 		struct cmsghdr hdr;
    104  1.2  christos 		char buf[1024];
    105  1.1  christos 	} cmsgbuf;
    106  1.1  christos 	struct cmsghdr *cmsg;
    107  1.1  christos 	struct iovec vec;
    108  1.1  christos 	ssize_t n;
    109  1.1  christos 	char ch;
    110  1.1  christos 	int fd;
    111  1.3      adam 	struct pollfd pfd;
    112  1.1  christos 
    113  1.2  christos 	if (sizeof(cmsgbuf.buf) < CMSG_SPACE(sizeof(int))) {
    114  1.2  christos 		error("%s: %zu < %zu, recompile", __func__,
    115  1.2  christos 		    sizeof(cmsgbuf.buf), CMSG_SPACE(sizeof(int)));
    116  1.2  christos 		return -1;
    117  1.2  christos 	}
    118  1.2  christos 
    119  1.1  christos 	memset(&msg, 0, sizeof(msg));
    120  1.5  christos 	memset(&cmsgbuf, 0, sizeof(cmsgbuf));
    121  1.1  christos 	vec.iov_base = &ch;
    122  1.1  christos 	vec.iov_len = 1;
    123  1.1  christos 	msg.msg_iov = &vec;
    124  1.1  christos 	msg.msg_iovlen = 1;
    125  1.1  christos 	msg.msg_control = &cmsgbuf.buf;
    126  1.2  christos 	msg.msg_controllen = CMSG_SPACE(sizeof(int));
    127  1.1  christos 
    128  1.3      adam 	pfd.fd = sock;
    129  1.3      adam 	pfd.events = POLLIN;
    130  1.3      adam 	while ((n = recvmsg(sock, &msg, 0)) == -1 &&
    131  1.3      adam 	    (errno == EAGAIN || errno == EINTR)) {
    132  1.9  christos 		debug3_f("recvmsg: %s", strerror(errno));
    133  1.3      adam 		(void)poll(&pfd, 1, -1);
    134  1.3      adam 	}
    135  1.1  christos 	if (n == -1) {
    136  1.9  christos 		error_f("recvmsg: %s", strerror(errno));
    137  1.1  christos 		return -1;
    138  1.1  christos 	}
    139  1.1  christos 
    140  1.1  christos 	if (n != 1) {
    141  1.9  christos 		error_f("recvmsg: expected received 1 got %zd", n);
    142  1.1  christos 		return -1;
    143  1.1  christos 	}
    144  1.1  christos 
    145  1.1  christos 	cmsg = CMSG_FIRSTHDR(&msg);
    146  1.1  christos 	if (cmsg == NULL) {
    147  1.9  christos 		error_f("no message header");
    148  1.1  christos 		return -1;
    149  1.1  christos 	}
    150  1.1  christos 
    151  1.1  christos 	if (cmsg->cmsg_type != SCM_RIGHTS) {
    152  1.9  christos 		error_f("expected %d got %d", SCM_RIGHTS, cmsg->cmsg_type);
    153  1.1  christos 		return -1;
    154  1.1  christos 	}
    155  1.1  christos 	fd = (*(int *)CMSG_DATA(cmsg));
    156  1.1  christos 	return fd;
    157  1.1  christos }
    158