ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/fdpass.C
Revision: 1.9
Committed: Sun Jan 22 16:36:40 2006 UTC (18 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
CVS Tags: rel-7_2
Changes since 1.8: +2 -2 lines
Log Message:
*** empty log message ***

File Contents

# Content
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 <sys/uio.h>
29 #include <sys/types.h>
30 #include <sys/socket.h>
31
32 #include "libptytty.h"
33
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 bool
39 ptytty::send_fd (int socket, int fd)
40 {
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 return sendmsg (socket, &msg, 0) >= 0;
66 }
67
68 int
69 ptytty::recv_fd (int socket)
70 {
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