]> andersk Git - openssh.git/blob - monitor_fdpass.c
- stevesk@cvs.openbsd.org 2002/03/24 17:53:16
[openssh.git] / monitor_fdpass.c
1 /*
2  * Copyright 2001 Niels Provos <provos@citi.umich.edu>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #include "includes.h"
27 RCSID("$OpenBSD: monitor_fdpass.c,v 1.2 2002/03/24 17:53:16 stevesk Exp $");
28
29 #include <sys/uio.h>
30
31 #include "log.h"
32 #include "monitor_fdpass.h"
33
34 void
35 mm_send_fd(int socket, int fd)
36 {
37         struct msghdr msg;
38         struct iovec vec;
39         char ch = '\0';
40         int n;
41 #ifndef HAVE_ACCRIGHTS_IN_MSGHDR
42         char tmp[CMSG_SPACE(sizeof(int))];
43         struct cmsghdr *cmsg;
44 #endif
45
46         memset(&msg, 0, sizeof(msg));
47 #ifdef HAVE_ACCRIGHTS_IN_MSGHDR
48         msg.msg_accrights = (caddr_t)&fd;
49         msg.msg_accrightslen = sizeof(fd);
50 #else
51         msg.msg_control = (caddr_t)tmp;
52         msg.msg_controllen = CMSG_LEN(sizeof(int));
53         cmsg = CMSG_FIRSTHDR(&msg);
54         cmsg->cmsg_len = CMSG_LEN(sizeof(int));
55         cmsg->cmsg_level = SOL_SOCKET;
56         cmsg->cmsg_type = SCM_RIGHTS;
57         *(int *)CMSG_DATA(cmsg) = fd;
58 #endif
59
60         vec.iov_base = &ch;
61         vec.iov_len = 1;
62         msg.msg_iov = &vec;
63         msg.msg_iovlen = 1;
64
65         if ((n = sendmsg(socket, &msg, 0)) == -1)
66                 fatal("%s: sendmsg(%d): %s", __FUNCTION__, fd,
67                     strerror(errno));
68         if (n != 1)
69                 fatal("%s: sendmsg: expected sent 1 got %d",
70                     __FUNCTION__, n);
71 }
72
73 int
74 mm_receive_fd(int socket)
75 {
76         struct msghdr msg;
77         struct iovec vec;
78         char ch;
79         int fd, n;
80 #ifndef HAVE_ACCRIGHTS_IN_MSGHDR
81         char tmp[CMSG_SPACE(sizeof(int))];
82         struct cmsghdr *cmsg;
83 #endif
84
85         memset(&msg, 0, sizeof(msg));
86         vec.iov_base = &ch;
87         vec.iov_len = 1;
88         msg.msg_iov = &vec;
89         msg.msg_iovlen = 1;
90 #ifdef HAVE_ACCRIGHTS_IN_MSGHDR
91         msg.msg_accrights = (caddr_t)&fd;
92         msg.msg_accrightslen = sizeof(fd);
93 #else
94         msg.msg_control = tmp;
95         msg.msg_controllen = sizeof(tmp);
96 #endif
97
98         if ((n = recvmsg(socket, &msg, 0)) == -1)
99                 fatal("%s: recvmsg: %s", __FUNCTION__, strerror(errno));
100         if (n != 1)
101                 fatal("%s: recvmsg: expected received 1 got %d",
102                     __FUNCTION__, n);
103
104 #ifdef HAVE_ACCRIGHTS_IN_MSGHDR
105         if (msg.msg_accrightslen != sizeof(fd))
106                 fatal("%s: no fd", __FUNCTION__);
107 #else
108         cmsg = CMSG_FIRSTHDR(&msg);
109         if (cmsg->cmsg_type != SCM_RIGHTS)
110                 fatal("%s: expected type %d got %d", __FUNCTION__,
111                     SCM_RIGHTS, cmsg->cmsg_type);
112         fd = (*(int *)CMSG_DATA(cmsg));
113 #endif
114         return fd;
115 }
This page took 0.054207 seconds and 5 git commands to generate.