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