ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/rxvt-unicode/src/fdpass.C
Revision: 1.5
Committed: Sat Jan 21 22:16:37 2006 UTC (18 years, 4 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.4: +1 -0 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     #include "rxvt.h"
25 root 1.1
26 root 1.5 #include <sys/uio.h>
27 root 1.1 #include <sys/types.h>
28     #include <sys/socket.h>
29    
30     #include "fdpass.h"
31    
32     #include <cstdio> //d
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     int
39     rxvt_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);
66     }
67    
68     int
69     rxvt_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