]> andersk Git - openssh.git/blame - monitor_fdpass.c
- (stevesk) [monitor_fdpass.c] support for access rights style file
[openssh.git] / monitor_fdpass.c
CommitLineData
1853d1ef 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"
27RCSID("$OpenBSD: monitor_fdpass.c,v 1.1 2002/03/18 17:27:22 provos Exp $");
28
29#include <sys/uio.h>
30
31#include "log.h"
32#include "monitor_fdpass.h"
33
34void
35mm_send_fd(int socket, int fd)
36{
37 struct msghdr msg;
1853d1ef 38 struct iovec vec;
39 char ch;
dc90b259 40#ifndef HAVE_ACCRIGHTS_IN_MSGHDR
41 char tmp[CMSG_SPACE(sizeof(int))];
42 struct cmsghdr *cmsg;
43#endif
1853d1ef 44
45 memset(&msg, 0, sizeof(msg));
dc90b259 46#ifdef HAVE_ACCRIGHTS_IN_MSGHDR
47 msg.msg_accrights = (caddr_t)&fd;
48 msg.msg_accrightslen = sizeof(fd);
49#else
1853d1ef 50 msg.msg_control = (caddr_t)tmp;
51 msg.msg_controllen = CMSG_LEN(sizeof(int));
52 cmsg = CMSG_FIRSTHDR(&msg);
53 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
54 cmsg->cmsg_level = SOL_SOCKET;
55 cmsg->cmsg_type = SCM_RIGHTS;
56 *(int *)CMSG_DATA(cmsg) = fd;
dc90b259 57#endif
1853d1ef 58
59 vec.iov_base = &ch;
60 vec.iov_len = 1;
61 msg.msg_iov = &vec;
62 msg.msg_iovlen = 1;
63
64 if (sendmsg(socket, &msg, 0) == -1)
65 fatal("%s: sendmsg(%d)", __FUNCTION__, fd);
66}
67
68int
69mm_receive_fd(int socket)
70{
71 struct msghdr msg;
1853d1ef 72 struct iovec vec;
73 char ch;
dc90b259 74#ifndef HAVE_ACCRIGHTS_IN_MSGHDR
75 char tmp[CMSG_SPACE(sizeof(int))];
76 struct cmsghdr *cmsg;
77#else
78 int fd;
79#endif
1853d1ef 80
81 memset(&msg, 0, sizeof(msg));
82 vec.iov_base = &ch;
83 vec.iov_len = 1;
84 msg.msg_iov = &vec;
85 msg.msg_iovlen = 1;
dc90b259 86#ifdef HAVE_ACCRIGHTS_IN_MSGHDR
87 msg.msg_accrights = (caddr_t)&fd;
88 msg.msg_accrightslen = sizeof(fd);
89#else
1853d1ef 90 msg.msg_control = tmp;
91 msg.msg_controllen = sizeof(tmp);
dc90b259 92#endif
1853d1ef 93
94 if (recvmsg(socket, &msg, 0) == -1)
95 fatal("%s: recvmsg", __FUNCTION__);
96
dc90b259 97#ifdef HAVE_ACCRIGHTS_IN_MSGHDR
98 if (msg.msg_accrightslen != sizeof(fd))
99 fatal("%s: no fd", __FUNCTION__);
100 return fd;
101#else
1853d1ef 102 cmsg = CMSG_FIRSTHDR(&msg);
103 if (cmsg->cmsg_type != SCM_RIGHTS)
104 fatal("%s: expected type %d got %d", __FUNCTION__,
105 SCM_RIGHTS, cmsg->cmsg_type);
106 return (*(int *)CMSG_DATA(cmsg));
dc90b259 107#endif
1853d1ef 108}
This page took 0.094044 seconds and 5 git commands to generate.