1 |
// This file is part of libptytty. Do not make local modifications. |
2 |
// http://software.schmorp.de/pkg/libptytty |
3 |
|
4 |
/*----------------------------------------------------------------------* |
5 |
* File: fdpass.C |
6 |
*----------------------------------------------------------------------* |
7 |
* |
8 |
* All portions of code are copyright by their respective author/s. |
9 |
* Copyright (c) 2005-2006 Marc Lehmann <pcg@goof.com> |
10 |
* |
11 |
* This program is free software; you can redistribute it and/or modify |
12 |
* it under the terms of the GNU General Public License as published by |
13 |
* the Free Software Foundation; either version 2 of the License, or |
14 |
* (at your option) any later version. |
15 |
* |
16 |
* This program is distributed in the hope that it will be useful, |
17 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19 |
* GNU General Public License for more details. |
20 |
* |
21 |
* You should have received a copy of the GNU General Public License |
22 |
* along with this program; if not, write to the Free Software |
23 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
24 |
*----------------------------------------------------------------------*/ |
25 |
|
26 |
#include "../config.h" |
27 |
|
28 |
#include <cstddef> // needed by broken bsds for NULL used in sys/uio.h |
29 |
|
30 |
#include <sys/uio.h> |
31 |
#include <sys/types.h> |
32 |
#include <sys/socket.h> |
33 |
|
34 |
#include "libptytty.h" |
35 |
|
36 |
#ifndef CMSG_LEN // CMSG_SPACe && CMSG_LEN are rfc2292 extensions to unix |
37 |
# define CMSG_LEN(len) (sizeof (cmsghdr) + len) |
38 |
#endif |
39 |
|
40 |
bool |
41 |
ptytty::send_fd (int socket, int fd) |
42 |
{ |
43 |
msghdr msg; |
44 |
iovec iov; |
45 |
char buf [CMSG_LEN (sizeof (int))]; |
46 |
char data = 0; |
47 |
|
48 |
iov.iov_base = &data; |
49 |
iov.iov_len = 1; |
50 |
|
51 |
msg.msg_name = 0; |
52 |
msg.msg_namelen = 0; |
53 |
msg.msg_iov = &iov; |
54 |
msg.msg_iovlen = 1; |
55 |
msg.msg_control = (void *)buf; |
56 |
msg.msg_controllen = sizeof buf; |
57 |
|
58 |
cmsghdr *cmsg = CMSG_FIRSTHDR (&msg); |
59 |
cmsg->cmsg_level = SOL_SOCKET; |
60 |
cmsg->cmsg_type = SCM_RIGHTS; |
61 |
cmsg->cmsg_len = CMSG_LEN (sizeof (int)); |
62 |
|
63 |
*(int *)CMSG_DATA (cmsg) = fd; |
64 |
|
65 |
msg.msg_controllen = cmsg->cmsg_len; |
66 |
|
67 |
return sendmsg (socket, &msg, 0) >= 0; |
68 |
} |
69 |
|
70 |
int |
71 |
ptytty::recv_fd (int socket) |
72 |
{ |
73 |
msghdr msg; |
74 |
iovec iov; |
75 |
char buf [CMSG_LEN (sizeof (int))]; /* ancillary data buffer */ |
76 |
char data = 1; |
77 |
|
78 |
iov.iov_base = &data; |
79 |
iov.iov_len = 1; |
80 |
|
81 |
msg.msg_name = 0; |
82 |
msg.msg_namelen = 0; |
83 |
msg.msg_iov = &iov; |
84 |
msg.msg_iovlen = 1; |
85 |
msg.msg_control = buf; |
86 |
msg.msg_controllen = sizeof buf; |
87 |
|
88 |
cmsghdr *cmsg = CMSG_FIRSTHDR (&msg); |
89 |
cmsg->cmsg_level = SOL_SOCKET; |
90 |
cmsg->cmsg_type = SCM_RIGHTS; |
91 |
cmsg->cmsg_len = CMSG_LEN (sizeof (int)); |
92 |
|
93 |
msg.msg_controllen = cmsg->cmsg_len; |
94 |
|
95 |
if (recvmsg (socket, &msg, 0) <= 0 |
96 |
|| data != 0 |
97 |
|| msg.msg_controllen < CMSG_LEN (sizeof (int)) |
98 |
|| cmsg->cmsg_level != SOL_SOCKET |
99 |
|| cmsg->cmsg_type != SCM_RIGHTS |
100 |
|| cmsg->cmsg_len < CMSG_LEN (sizeof (int))) |
101 |
return -1; |
102 |
|
103 |
return *(int *)CMSG_DATA (cmsg); |
104 |
} |
105 |
|