ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/fdpass.C
Revision: 1.6
Committed: Sun Jan 22 00:48:13 2006 UTC (18 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.5: +2 -5 lines
Log Message:
*** empty log message ***

File Contents

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