1 |
root |
1.8 |
// This file is part of libptytty. Do not make local modifications. |
2 |
|
|
// http://software.schmorp.de/pkg/libptytty |
3 |
|
|
|
4 |
|
|
/*----------------------------------------------------------------------* |
5 |
root |
1.1 |
* File: fdpass.C |
6 |
|
|
*----------------------------------------------------------------------* |
7 |
|
|
* |
8 |
|
|
* All portions of code are copyright by their respective author/s. |
9 |
root |
1.2 |
* Copyright (c) 2005-2006 Marc Lehmann <pcg@goof.com> |
10 |
root |
1.1 |
* |
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 |
root |
1.3 |
#include "../config.h" |
27 |
root |
1.1 |
|
28 |
root |
1.5 |
#include <sys/uio.h> |
29 |
root |
1.1 |
#include <sys/types.h> |
30 |
|
|
#include <sys/socket.h> |
31 |
|
|
|
32 |
root |
1.7 |
#include "libptytty.h" |
33 |
root |
1.1 |
|
34 |
|
|
#ifndef CMSG_LEN // CMSG_SPACe && CMSG_LEN are rfc2292 extensions to unix |
35 |
|
|
# define CMSG_LEN(len) (sizeof (cmsghdr) + len) |
36 |
|
|
#endif |
37 |
|
|
|
38 |
root |
1.9 |
bool |
39 |
root |
1.7 |
ptytty::send_fd (int socket, int fd) |
40 |
root |
1.1 |
{ |
41 |
|
|
msghdr msg; |
42 |
|
|
iovec iov; |
43 |
|
|
char buf [CMSG_LEN (sizeof (int))]; |
44 |
|
|
char data = 0; |
45 |
|
|
|
46 |
|
|
iov.iov_base = &data; |
47 |
|
|
iov.iov_len = 1; |
48 |
|
|
|
49 |
|
|
msg.msg_name = 0; |
50 |
|
|
msg.msg_namelen = 0; |
51 |
|
|
msg.msg_iov = &iov; |
52 |
|
|
msg.msg_iovlen = 1; |
53 |
|
|
msg.msg_control = (void *)buf; |
54 |
|
|
msg.msg_controllen = sizeof buf; |
55 |
|
|
|
56 |
|
|
cmsghdr *cmsg = CMSG_FIRSTHDR (&msg); |
57 |
|
|
cmsg->cmsg_level = SOL_SOCKET; |
58 |
|
|
cmsg->cmsg_type = SCM_RIGHTS; |
59 |
|
|
cmsg->cmsg_len = CMSG_LEN (sizeof (int)); |
60 |
|
|
|
61 |
|
|
*(int *)CMSG_DATA (cmsg) = fd; |
62 |
|
|
|
63 |
|
|
msg.msg_controllen = cmsg->cmsg_len; |
64 |
|
|
|
65 |
root |
1.9 |
return sendmsg (socket, &msg, 0) >= 0; |
66 |
root |
1.1 |
} |
67 |
|
|
|
68 |
|
|
int |
69 |
root |
1.7 |
ptytty::recv_fd (int socket) |
70 |
root |
1.1 |
{ |
71 |
|
|
msghdr msg; |
72 |
|
|
iovec iov; |
73 |
|
|
char buf [CMSG_LEN (sizeof (int))]; /* ancillary data buffer */ |
74 |
|
|
char data = 1; |
75 |
|
|
|
76 |
|
|
iov.iov_base = &data; |
77 |
|
|
iov.iov_len = 1; |
78 |
|
|
|
79 |
|
|
msg.msg_name = 0; |
80 |
|
|
msg.msg_namelen = 0; |
81 |
|
|
msg.msg_iov = &iov; |
82 |
|
|
msg.msg_iovlen = 1; |
83 |
|
|
msg.msg_control = buf; |
84 |
|
|
msg.msg_controllen = sizeof buf; |
85 |
|
|
|
86 |
|
|
cmsghdr *cmsg = CMSG_FIRSTHDR (&msg); |
87 |
|
|
cmsg->cmsg_level = SOL_SOCKET; |
88 |
|
|
cmsg->cmsg_type = SCM_RIGHTS; |
89 |
|
|
cmsg->cmsg_len = CMSG_LEN (sizeof (int)); |
90 |
|
|
|
91 |
|
|
msg.msg_controllen = cmsg->cmsg_len; |
92 |
|
|
|
93 |
|
|
if (recvmsg (socket, &msg, 0) <= 0 |
94 |
|
|
|| data != 0 |
95 |
|
|
|| msg.msg_controllen < CMSG_LEN (sizeof (int)) |
96 |
|
|
|| cmsg->cmsg_level != SOL_SOCKET |
97 |
|
|
|| cmsg->cmsg_type != SCM_RIGHTS |
98 |
|
|
|| cmsg->cmsg_len < CMSG_LEN (sizeof (int))) |
99 |
|
|
return -1; |
100 |
|
|
|
101 |
|
|
return *(int *)CMSG_DATA (cmsg); |
102 |
|
|
} |
103 |
|
|
|