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